From 614da3deecb1bc06279ab616334499eded564574 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 25 Oct 2015 16:09:26 +0100 Subject: [PATCH 01/18] fix indentation (part 1) --- bn_fast_s_mp_mul_digs.c | 2 +- bn_mp_expt_d_ex.c | 4 ++-- bn_mp_read_unsigned_bin.c | 10 +++++----- bn_mp_sqr.c | 2 +- 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c index adda3f5..4d4000c 100644 --- a/bn_fast_s_mp_mul_digs.c +++ b/bn_fast_s_mp_mul_digs.c @@ -78,7 +78,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) /* make next carry */ _W = _W >> ((mp_word)DIGIT_BIT); - } + } /* setup dest */ olduse = c->used; diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c index 93f4f19..ab840b8 100644 --- a/bn_mp_expt_d_ex.c +++ b/bn_mp_expt_d_ex.c @@ -69,9 +69,9 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast) /* shift to next bit */ b <<= 1; } - } /* if ... else */ + } /* if ... else */ - mp_clear (&g); + mp_clear (&g); return MP_OKAY; } #endif diff --git a/bn_mp_read_unsigned_bin.c b/bn_mp_read_unsigned_bin.c index 3598ec8..4d0472c 100644 --- a/bn_mp_read_unsigned_bin.c +++ b/bn_mp_read_unsigned_bin.c @@ -37,12 +37,12 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c) } #ifndef MP_8BIT - a->dp[0] |= *b++; - a->used += 1; + a->dp[0] |= *b++; + a->used += 1; #else - a->dp[0] = (*b & MP_MASK); - a->dp[1] |= ((*b++ >> 7U) & 1); - a->used += 2; + a->dp[0] = (*b & MP_MASK); + a->dp[1] |= ((*b++ >> 7U) & 1); + a->used += 2; #endif } mp_clamp (a); diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c index f1d72ef..fa5a8a6 100644 --- a/bn_mp_sqr.c +++ b/bn_mp_sqr.c @@ -29,7 +29,7 @@ mp_sqr (mp_int * a, mp_int * b) } else #endif #ifdef BN_MP_KARATSUBA_SQR_C -if (a->used >= KARATSUBA_SQR_CUTOFF) { + if (a->used >= KARATSUBA_SQR_CUTOFF) { res = mp_karatsuba_sqr (a, b); } else #endif From 2d40b4deb1c918afe11d21447ebff75542f2c576 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 25 Oct 2015 16:09:43 +0100 Subject: [PATCH 02/18] fix indentation (part 2) --- bn_mp_toom_mul.c | 200 +++++++++++++++++++++++------------------------ bn_mp_toom_sqr.c | 196 +++++++++++++++++++++++----------------------- 2 files changed, 198 insertions(+), 198 deletions(-) diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c index 3d8ab99..f0df3c2 100644 --- a/bn_mp_toom_mul.c +++ b/bn_mp_toom_mul.c @@ -171,110 +171,110 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) 2 small divisions and 1 small multiplication */ - /* r1 - r4 */ - if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - r0 */ - if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) { - goto ERR; - } - /* r1/2 */ - if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3/2 */ - if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) { - goto ERR; - } - /* r2 - r0 - r4 */ - if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) { - goto ERR; - } - /* r1 - r2 */ - if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - r2 */ - if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { - goto ERR; - } - /* r1 - 8r0 */ - if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - 8r4 */ - if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) { - goto ERR; - } - /* 3r2 - r1 - r3 */ - if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) { - goto ERR; - } - /* r1 - r2 */ - if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - r2 */ - if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { - goto ERR; - } - /* r1/3 */ - if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) { - goto ERR; - } - /* r3/3 */ - if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { - goto ERR; - } + /* r1 - r4 */ + if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - r0 */ + if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) { + goto ERR; + } + /* r1/2 */ + if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3/2 */ + if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) { + goto ERR; + } + /* r2 - r0 - r4 */ + if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) { + goto ERR; + } + /* r1 - r2 */ + if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - r2 */ + if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { + goto ERR; + } + /* r1 - 8r0 */ + if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - 8r4 */ + if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) { + goto ERR; + } + /* 3r2 - r1 - r3 */ + if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) { + goto ERR; + } + /* r1 - r2 */ + if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - r2 */ + if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { + goto ERR; + } + /* r1/3 */ + if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) { + goto ERR; + } + /* r3/3 */ + if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { + goto ERR; + } - /* at this point shift W[n] by B*n */ - if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { - goto ERR; - } + /* at this point shift W[n] by B*n */ + if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { + goto ERR; + } - if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) { - goto ERR; - } + if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) { + goto ERR; + } ERR: - mp_clear_multi(&w0, &w1, &w2, &w3, &w4, - &a0, &a1, &a2, &b0, &b1, - &b2, &tmp1, &tmp2, NULL); - return res; + mp_clear_multi(&w0, &w1, &w2, &w3, &w4, + &a0, &a1, &a2, &b0, &b1, + &b2, &tmp1, &tmp2, NULL); + return res; } #endif diff --git a/bn_mp_toom_sqr.c b/bn_mp_toom_sqr.c index cbee45c..f8208a6 100644 --- a/bn_mp_toom_sqr.c +++ b/bn_mp_toom_sqr.c @@ -115,108 +115,108 @@ mp_toom_sqr(mp_int *a, mp_int *b) using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication. */ - /* r1 - r4 */ - if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - r0 */ - if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) { - goto ERR; - } - /* r1/2 */ - if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3/2 */ - if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) { - goto ERR; - } - /* r2 - r0 - r4 */ - if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) { - goto ERR; - } - /* r1 - r2 */ - if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - r2 */ - if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { - goto ERR; - } - /* r1 - 8r0 */ - if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - 8r4 */ - if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) { - goto ERR; - } - /* 3r2 - r1 - r3 */ - if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) { - goto ERR; - } - /* r1 - r2 */ - if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { - goto ERR; - } - /* r3 - r2 */ - if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { - goto ERR; - } - /* r1/3 */ - if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) { - goto ERR; - } - /* r3/3 */ - if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { - goto ERR; - } + /* r1 - r4 */ + if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - r0 */ + if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) { + goto ERR; + } + /* r1/2 */ + if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3/2 */ + if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) { + goto ERR; + } + /* r2 - r0 - r4 */ + if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) { + goto ERR; + } + /* r1 - r2 */ + if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - r2 */ + if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { + goto ERR; + } + /* r1 - 8r0 */ + if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - 8r4 */ + if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) { + goto ERR; + } + /* 3r2 - r1 - r3 */ + if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) { + goto ERR; + } + /* r1 - r2 */ + if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) { + goto ERR; + } + /* r3 - r2 */ + if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) { + goto ERR; + } + /* r1/3 */ + if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) { + goto ERR; + } + /* r3/3 */ + if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { + goto ERR; + } - /* at this point shift W[n] by B*n */ - if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { - goto ERR; - } + /* at this point shift W[n] by B*n */ + if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { + goto ERR; + } - if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) { - goto ERR; - } - if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) { - goto ERR; - } + if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) { + goto ERR; + } + if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) { + goto ERR; + } ERR: - mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL); - return res; + mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL); + return res; } #endif From 2404bd3c13a27f68d2cb090031c549f3795c8997 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 11 Oct 2015 12:09:29 +0200 Subject: [PATCH 03/18] suspicious use of ; --- bn_mp_cnt_lsb.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c index a617767..d862762 100644 --- a/bn_mp_cnt_lsb.c +++ b/bn_mp_cnt_lsb.c @@ -31,7 +31,7 @@ int mp_cnt_lsb(mp_int *a) } /* scan lower digits until non-zero */ - for (x = 0; x < a->used && a->dp[x] == 0; x++); + for (x = 0; x < a->used && a->dp[x] == 0; x++) {} q = a->dp[x]; x *= DIGIT_BIT; From 759ed1f050872ba89d30b4d7490066d7b378ad2e Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 11 Oct 2015 12:11:00 +0200 Subject: [PATCH 04/18] union initialization is not supported by ISO C --- bn_mp_export.c | 3 ++- bn_mp_import.c | 3 ++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/bn_mp_export.c b/bn_mp_export.c index 1a82e4a..3712fc7 100644 --- a/bn_mp_export.c +++ b/bn_mp_export.c @@ -34,7 +34,8 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, union { unsigned int i; char c[4]; - } lint = {0x01020304}; + } lint; + lint.i = 0x01020304; endian = (lint.c[0] == 4 ? -1 : 1); } diff --git a/bn_mp_import.c b/bn_mp_import.c index b5d4ed9..c106e02 100644 --- a/bn_mp_import.c +++ b/bn_mp_import.c @@ -30,7 +30,8 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size, union { unsigned int i; char c[4]; - } lint = {0x01020304}; + } lint; + lint.i = 0x01020304; endian = (lint.c[0] == 4 ? -1 : 1); } From 4430c6bc5f150697c864e038b3b9a23be9489e75 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 11 Oct 2015 15:54:43 +0200 Subject: [PATCH 05/18] array declaration with its size --- tommath.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tommath.h b/tommath.h index 0097b5e..1391d95 100644 --- a/tommath.h +++ b/tommath.h @@ -504,7 +504,7 @@ int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); #endif /* table of first PRIME_SIZE primes */ -extern const mp_digit ltm_prime_tab[]; +extern const mp_digit ltm_prime_tab[PRIME_SIZE]; /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ int mp_prime_is_divisible(mp_int *a, int *result); From 09b8fd9c986b96f60abb6660e639b3a3c8a91168 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 11 Oct 2015 19:01:04 +0200 Subject: [PATCH 06/18] explicit block --- bn_mp_div.c | 3 ++- bn_mp_mul.c | 3 ++- bn_mp_shrink.c | 3 ++- bn_mp_sqr.c | 2 ++ 4 files changed, 8 insertions(+), 3 deletions(-) diff --git a/bn_mp_div.c b/bn_mp_div.c index 336a57b..f50a8d7 100644 --- a/bn_mp_div.c +++ b/bn_mp_div.c @@ -196,8 +196,9 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); tmp |= ((mp_word) x.dp[i - 1]); tmp /= ((mp_word) y.dp[t]); - if (tmp > (mp_word) MP_MASK) + if (tmp > (mp_word) MP_MASK) { tmp = MP_MASK; + } q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); } diff --git a/bn_mp_mul.c b/bn_mp_mul.c index 74b5135..d7f0074 100644 --- a/bn_mp_mul.c +++ b/bn_mp_mul.c @@ -49,12 +49,13 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c) res = fast_s_mp_mul_digs (a, b, c, digs); } else #endif + { #ifdef BN_S_MP_MUL_DIGS_C res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */ #else res = MP_VAL; #endif - + } } c->sign = (c->used > 0) ? neg : MP_ZPOS; return res; diff --git a/bn_mp_shrink.c b/bn_mp_shrink.c index 0eaa09e..bbc1208 100644 --- a/bn_mp_shrink.c +++ b/bn_mp_shrink.c @@ -21,8 +21,9 @@ int mp_shrink (mp_int * a) mp_digit *tmp; int used = 1; - if(a->used > 0) + if(a->used > 0) { used = a->used; + } if (a->alloc != used) { if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) { diff --git a/bn_mp_sqr.c b/bn_mp_sqr.c index fa5a8a6..5167ce0 100644 --- a/bn_mp_sqr.c +++ b/bn_mp_sqr.c @@ -42,11 +42,13 @@ mp_sqr (mp_int * a, mp_int * b) res = fast_s_mp_sqr (a, b); } else #endif + { #ifdef BN_S_MP_SQR_C res = s_mp_sqr (a, b); #else res = MP_VAL; #endif + } } b->sign = MP_ZPOS; return res; From b6636ee46ace49e7dd2f0d08640fa9f99f95b213 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 11 Oct 2015 19:02:01 +0200 Subject: [PATCH 07/18] uppercase L avoid confusion between lowercase l and digit 1 --- bn_mp_get_long.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bn_mp_get_long.c b/bn_mp_get_long.c index 8286082..7f2a54b 100644 --- a/bn_mp_get_long.c +++ b/bn_mp_get_long.c @@ -31,7 +31,7 @@ unsigned long mp_get_long(mp_int * a) /* get most significant digit of result */ res = DIGIT(a,i); -#if ULONG_MAX != 0xfffffffful || DIGIT_BIT < 32 +#if ULONG_MAX != 0xffffffffuL || DIGIT_BIT < 32 while (--i >= 0) { res = (res << DIGIT_BIT) | DIGIT(a,i); } From 84db6f9dbc48809ee3e2e0d7271d73a68a27fe27 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sat, 17 Oct 2015 18:12:48 +0200 Subject: [PATCH 08/18] add parentheses for explicit operator precedence --- tommath.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/tommath.h b/tommath.h index 1391d95..b806c3b 100644 --- a/tommath.h +++ b/tommath.h @@ -25,11 +25,11 @@ #include #ifndef MIN - #define MIN(x,y) ((x)<(y)?(x):(y)) + #define MIN(x,y) (((x) < (y)) ? (x) : (y)) #endif #ifndef MAX - #define MAX(x,y) ((x)>(y)?(x):(y)) + #define MAX(x,y) (((x) > (y)) ? (x) : (y)) #endif #ifdef __cplusplus @@ -200,7 +200,7 @@ extern int KARATSUBA_MUL_CUTOFF, #endif /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ -#define MP_WARRAY (1 << (sizeof(mp_word) * CHAR_BIT - 2 * DIGIT_BIT + 1)) +#define MP_WARRAY (1 << ((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT) + 1)) /* the infamous mp_int structure */ typedef struct { @@ -246,8 +246,8 @@ int mp_init_size(mp_int *a, int size); /* ---> Basic Manipulations <--- */ #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) -#define mp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) -#define mp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) +#define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) +#define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) #define mp_isneg(a) (((a)->sign) ? MP_YES : MP_NO) /* set to zero */ @@ -639,14 +639,14 @@ int func_name (mp_int * a, type b) \ mp_zero (a); \ \ /* set four bits at a time */ \ - for (x = 0; x < sizeof(type) * 2; x++) { \ + for (x = 0; x < (sizeof(type) * 2); x++) { \ /* shift the number up four bits */ \ if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \ return res; \ } \ \ /* OR in the top four bits of the source */ \ - a->dp[0] |= (b >> ((sizeof(type)) * 8 - 4)) & 15; \ + a->dp[0] |= (b >> ((sizeof(type) * 8) - 4)) & 15; \ \ /* shift the source up to the next four bits */ \ b <<= 4; \ From 9f7811624af983ee6f12d205b955a645bfcc43f6 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sat, 17 Oct 2015 18:27:56 +0200 Subject: [PATCH 09/18] add parentheses for explicit operator association --- tommath.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tommath.h b/tommath.h index b806c3b..742bb9a 100644 --- a/tommath.h +++ b/tommath.h @@ -200,7 +200,7 @@ extern int KARATSUBA_MUL_CUTOFF, #endif /* size of comba arrays, should be at least 2 * 2**(BITS_PER_WORD - BITS_PER_DIGIT*2) */ -#define MP_WARRAY (1 << ((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT) + 1)) +#define MP_WARRAY (1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) + 1)) /* the infamous mp_int structure */ typedef struct { From e25f1701e45bd3facb25b20190111be779ccdacc Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 25 Oct 2015 16:21:17 +0100 Subject: [PATCH 10/18] explicit condition (part H) --- tommath.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tommath.h b/tommath.h index 742bb9a..5dcfdb6 100644 --- a/tommath.h +++ b/tommath.h @@ -248,7 +248,7 @@ int mp_init_size(mp_int *a, int size); #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) #define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) #define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) -#define mp_isneg(a) (((a)->sign) ? MP_YES : MP_NO) +#define mp_isneg(a) (((a)->sign != 0) ? MP_YES : MP_NO) /* set to zero */ void mp_zero(mp_int *a); From 38f90d1b1777048798f3881c61896b34f7dc4261 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 25 Oct 2015 16:49:26 +0100 Subject: [PATCH 11/18] explicit condition (part C) --- bn_mp_div_d.c | 2 +- bn_mp_export.c | 4 ++-- bn_mp_expt_d_ex.c | 4 ++-- bn_mp_init_multi.c | 2 +- bn_mp_is_square.c | 14 +++++++------- bn_mp_mod.c | 2 +- bn_mp_montgomery_reduce.c | 2 +- bn_mp_prime_random_ex.c | 10 +++++----- bn_mp_read_radix.c | 2 +- 9 files changed, 21 insertions(+), 21 deletions(-) diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c index 579221d..55f3831 100644 --- a/bn_mp_div_d.c +++ b/bn_mp_div_d.c @@ -20,7 +20,7 @@ static int s_is_power_of_two(mp_digit b, int *p) int x; /* fast return if no power of two */ - if ((b==0) || (b & (b-1))) { + if ((b == 0) || ((b & (b-1)) != 0)) { return 0; } diff --git a/bn_mp_export.c b/bn_mp_export.c index 3712fc7..7829315 100644 --- a/bn_mp_export.c +++ b/bn_mp_export.c @@ -48,7 +48,7 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, nail_bytes = nails / 8; bits = mp_count_bits(&t); - count = bits / (size * 8 - nails) + (bits % (size * 8 - nails) ? 1 : 0); + count = bits / (size * 8 - nails) + ((bits % (size * 8 - nails) != 0) ? 1 : 0); for (i = 0; i < count; ++i) { for (j = 0; j < size; ++j) { @@ -74,7 +74,7 @@ int mp_export(void* rop, size_t* countp, int order, size_t size, mp_clear(&t); - if (countp) { + if (countp != NULL) { *countp = count; } diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c index ab840b8..bc15878 100644 --- a/bn_mp_expt_d_ex.c +++ b/bn_mp_expt_d_ex.c @@ -30,10 +30,10 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast) /* set initial result */ mp_set (c, 1); - if (fast) { + if (fast != 0) { while (b > 0) { /* if the bit is set multiply */ - if (b & 1) { + if ((b & 1) != 0) { if ((res = mp_mul (c, &g, c)) != MP_OKAY) { mp_clear (&g); return res; diff --git a/bn_mp_init_multi.c b/bn_mp_init_multi.c index 830068c..388b813 100644 --- a/bn_mp_init_multi.c +++ b/bn_mp_init_multi.c @@ -37,7 +37,7 @@ int mp_init_multi(mp_int *mp, ...) /* now start cleaning up */ cur_arg = mp; va_start(clean_args, mp); - while (n--) { + while (n-- != 0) { mp_clear(cur_arg); cur_arg = va_arg(clean_args, mp_int*); } diff --git a/bn_mp_is_square.c b/bn_mp_is_square.c index 5348ca9..8c2f221 100644 --- a/bn_mp_is_square.c +++ b/bn_mp_is_square.c @@ -82,13 +82,13 @@ int mp_is_square(mp_int *arg,int *ret) * free "t" so the easiest way is to goto ERR. We know that res * is already equal to MP_OKAY from the mp_mod call */ - if ( (1L<<(r%11)) & 0x5C4L ) goto ERR; - if ( (1L<<(r%13)) & 0x9E4L ) goto ERR; - if ( (1L<<(r%17)) & 0x5CE8L ) goto ERR; - if ( (1L<<(r%19)) & 0x4F50CL ) goto ERR; - if ( (1L<<(r%23)) & 0x7ACCA0L ) goto ERR; - if ( (1L<<(r%29)) & 0xC2EDD0CL ) goto ERR; - if ( (1L<<(r%31)) & 0x6DE2B848L ) goto ERR; + if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR; + if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR; + if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR; + if (((1L<<(r%19)) & 0x4F50CL) != 0L) goto ERR; + if (((1L<<(r%23)) & 0x7ACCA0L) != 0L) goto ERR; + if (((1L<<(r%29)) & 0xC2EDD0CL) != 0L) goto ERR; + if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR; /* Final check - is sqr(sqrt(arg)) == arg ? */ if ((res = mp_sqrt(arg,&t)) != MP_OKAY) { diff --git a/bn_mp_mod.c b/bn_mp_mod.c index 589972d..f7eb1c4 100644 --- a/bn_mp_mod.c +++ b/bn_mp_mod.c @@ -31,7 +31,7 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c) return res; } - if (mp_iszero(&t) || t.sign == b->sign) { + if (mp_iszero(&t) != MP_NO || t.sign == b->sign) { res = MP_OKAY; mp_exch (&t, c); } else { diff --git a/bn_mp_montgomery_reduce.c b/bn_mp_montgomery_reduce.c index ffdd127..e92a988 100644 --- a/bn_mp_montgomery_reduce.c +++ b/bn_mp_montgomery_reduce.c @@ -85,7 +85,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) /* propagate carries upwards as required*/ - while (u) { + while (u != 0) { *tmpx += u; u = *tmpx >> DIGIT_BIT; *tmpx++ &= MP_MASK; diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c index 981a8fe..3dfc425 100644 --- a/bn_mp_prime_random_ex.c +++ b/bn_mp_prime_random_ex.c @@ -41,7 +41,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback } /* LTM_PRIME_SAFE implies LTM_PRIME_BBS */ - if (flags & LTM_PRIME_SAFE) { + if ((flags & LTM_PRIME_SAFE) != 0) { flags |= LTM_PRIME_BBS; } @@ -60,13 +60,13 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback /* calc the maskOR_msb */ maskOR_msb = 0; maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; - if (flags & LTM_PRIME_2MSB_ON) { + if ((flags & LTM_PRIME_2MSB_ON) != 0) { maskOR_msb |= 0x80 >> ((9 - size) & 7); } /* get the maskOR_lsb */ maskOR_lsb = 1; - if (flags & LTM_PRIME_BBS) { + if ((flags & LTM_PRIME_BBS) != 0) { maskOR_lsb |= 3; } @@ -94,7 +94,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback continue; } - if (flags & LTM_PRIME_SAFE) { + if ((flags & LTM_PRIME_SAFE) != 0) { /* see if (a-1)/2 is prime */ if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; } if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; } @@ -104,7 +104,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback } } while (res == MP_NO); - if (flags & LTM_PRIME_SAFE) { + if ((flags & LTM_PRIME_SAFE) != 0) { /* restore a to the original value */ if ((err = mp_mul_2(a, a)) != MP_OKAY) { goto error; } if ((err = mp_add_d(a, 1, a)) != MP_OKAY) { goto error; } diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c index 178339e..31b12ac 100644 --- a/bn_mp_read_radix.c +++ b/bn_mp_read_radix.c @@ -43,7 +43,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix) mp_zero (a); /* process each digit of the string */ - while (*str) { + while (*str != '\0') { /* if the radix <= 36 the conversion is case insensitive * this allows numbers like 1AB and 1ab to represent the same value * [e.g. in hex] From 64177349fcb7a1b94eea5b608291b8c00bf6cafc Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 25 Oct 2015 16:25:20 +0100 Subject: [PATCH 12/18] avoid side effects on right hand of logical operator --- bn_mp_expt_d_ex.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/bn_mp_expt_d_ex.c b/bn_mp_expt_d_ex.c index bc15878..8b12d24 100644 --- a/bn_mp_expt_d_ex.c +++ b/bn_mp_expt_d_ex.c @@ -41,9 +41,11 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast) } /* square */ - if (b > 1 && (res = mp_sqr (&g, &g)) != MP_OKAY) { - mp_clear (&g); - return res; + if (b > 1) { + if ((res = mp_sqr (&g, &g)) != MP_OKAY) { + mp_clear (&g); + return res; + } } /* shift to next bit */ From 0522eef2882b941afb2fe3637a1bf1c34a25e2a3 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 18 Oct 2015 17:04:43 +0200 Subject: [PATCH 13/18] refactor cast in ternary op --- bn_mp_read_radix.c | 2 +- bn_mp_to_signed_bin.c | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c index 31b12ac..8d3c710 100644 --- a/bn_mp_read_radix.c +++ b/bn_mp_read_radix.c @@ -48,7 +48,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix) * this allows numbers like 1AB and 1ab to represent the same value * [e.g. in hex] */ - ch = (char) ((radix <= 36) ? toupper ((int)*str) : *str); + ch = (radix <= 36) ? (char)toupper((int)*str) : *str; for (y = 0; y < 64; y++) { if (ch == mp_s_rmap[y]) { break; diff --git a/bn_mp_to_signed_bin.c b/bn_mp_to_signed_bin.c index 0c0149e..2898908 100644 --- a/bn_mp_to_signed_bin.c +++ b/bn_mp_to_signed_bin.c @@ -23,7 +23,7 @@ int mp_to_signed_bin (mp_int * a, unsigned char *b) if ((res = mp_to_unsigned_bin (a, b + 1)) != MP_OKAY) { return res; } - b[0] = (unsigned char) ((a->sign == MP_ZPOS) ? 0 : 1); + b[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1; return MP_OKAY; } #endif From b9abe0a316e0053ae868ea63213722803914e85f Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 25 Oct 2015 16:34:43 +0100 Subject: [PATCH 14/18] refactor with macros MP_NO/MP_YES --- bn_fast_mp_invmod.c | 12 ++++++------ bn_mp_cnt_lsb.c | 2 +- bn_mp_div.c | 4 ++-- bn_mp_div_d.c | 2 +- bn_mp_exptmod.c | 2 +- bn_mp_gcd.c | 2 +- bn_mp_invmod.c | 4 ++-- bn_mp_invmod_slow.c | 14 +++++++------- bn_mp_jacobi.c | 2 +- bn_mp_prime_next_prime.c | 2 +- bn_mp_read_radix.c | 2 +- bn_mp_to_unsigned_bin.c | 2 +- bn_mp_toradix.c | 4 ++-- bn_mp_toradix_n.c | 2 +- 14 files changed, 28 insertions(+), 28 deletions(-) diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c index c595247..25e145b 100644 --- a/bn_fast_mp_invmod.c +++ b/bn_fast_mp_invmod.c @@ -27,7 +27,7 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) int res, neg; /* 2. [modified] b must be odd */ - if (mp_iseven (b) == 1) { + if (mp_iseven (b) == MP_YES) { return MP_VAL; } @@ -57,13 +57,13 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) top: /* 4. while u is even do */ - while (mp_iseven (&u) == 1) { + while (mp_iseven (&u) == MP_YES) { /* 4.1 u = u/2 */ if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { goto LBL_ERR; } /* 4.2 if B is odd then */ - if (mp_isodd (&B) == 1) { + if (mp_isodd (&B) == MP_YES) { if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { goto LBL_ERR; } @@ -75,13 +75,13 @@ top: } /* 5. while v is even do */ - while (mp_iseven (&v) == 1) { + while (mp_iseven (&v) == MP_YES) { /* 5.1 v = v/2 */ if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { goto LBL_ERR; } /* 5.2 if D is odd then */ - if (mp_isodd (&D) == 1) { + if (mp_isodd (&D) == MP_YES) { /* D = (D-x)/2 */ if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { goto LBL_ERR; @@ -115,7 +115,7 @@ top: } /* if not zero goto step 4 */ - if (mp_iszero (&u) == 0) { + if (mp_iszero (&u) == MP_NO) { goto top; } diff --git a/bn_mp_cnt_lsb.c b/bn_mp_cnt_lsb.c index d862762..92cc4e8 100644 --- a/bn_mp_cnt_lsb.c +++ b/bn_mp_cnt_lsb.c @@ -26,7 +26,7 @@ int mp_cnt_lsb(mp_int *a) mp_digit q, qq; /* easy out */ - if (mp_iszero(a) == 1) { + if (mp_iszero(a) == MP_YES) { return 0; } diff --git a/bn_mp_div.c b/bn_mp_div.c index f50a8d7..c33fac8 100644 --- a/bn_mp_div.c +++ b/bn_mp_div.c @@ -24,7 +24,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) int res, n, n2; /* is divisor zero ? */ - if (mp_iszero (b) == 1) { + if (mp_iszero (b) == MP_YES) { return MP_VAL; } @@ -106,7 +106,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) int res, n, t, i, norm, neg; /* is divisor zero ? */ - if (mp_iszero (b) == 1) { + if (mp_iszero (b) == MP_YES) { return MP_VAL; } diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c index 55f3831..dbaeb10 100644 --- a/bn_mp_div_d.c +++ b/bn_mp_div_d.c @@ -47,7 +47,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) } /* quick outs */ - if (b == 1 || mp_iszero(a) == 1) { + if (b == 1 || mp_iszero(a) == MP_YES) { if (d != NULL) { *d = 0; } diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c index da0a8d3..1d9059e 100644 --- a/bn_mp_exptmod.c +++ b/bn_mp_exptmod.c @@ -89,7 +89,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) /* if the modulus is odd or dr != 0 use the montgomery method */ #ifdef BN_MP_EXPTMOD_FAST_C - if (mp_isodd (P) == 1 || dr != 0) { + if (mp_isodd (P) == MP_YES || dr != 0) { return mp_exptmod_fast (G, X, P, Y, dr); } else { #endif diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c index 7d1c1c6..8cf6e00 100644 --- a/bn_mp_gcd.c +++ b/bn_mp_gcd.c @@ -70,7 +70,7 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) } } - while (mp_iszero(&v) == 0) { + while (mp_iszero(&v) == MP_NO) { /* make sure v is the largest */ if (mp_cmp_mag(&u, &v) == MP_GT) { /* swap u and v to make sure v is >= u */ diff --git a/bn_mp_invmod.c b/bn_mp_invmod.c index 67a9a61..c0eb2c0 100644 --- a/bn_mp_invmod.c +++ b/bn_mp_invmod.c @@ -19,13 +19,13 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c) { /* b cannot be negative */ - if (b->sign == MP_NEG || mp_iszero(b) == 1) { + if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) { return MP_VAL; } #ifdef BN_FAST_MP_INVMOD_C /* if the modulus is odd we can use a faster routine instead */ - if (mp_isodd (b) == 1) { + if (mp_isodd (b) == MP_YES) { return fast_mp_invmod (a, b, c); } #endif diff --git a/bn_mp_invmod_slow.c b/bn_mp_invmod_slow.c index e0adda9..50ce2c1 100644 --- a/bn_mp_invmod_slow.c +++ b/bn_mp_invmod_slow.c @@ -22,7 +22,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) int res; /* b cannot be negative */ - if (b->sign == MP_NEG || mp_iszero(b) == 1) { + if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) { return MP_VAL; } @@ -41,7 +41,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) } /* 2. [modified] if x,y are both even then return an error! */ - if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) { + if (mp_iseven (&x) == MP_YES && mp_iseven (&y) == MP_YES) { res = MP_VAL; goto LBL_ERR; } @@ -58,13 +58,13 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) top: /* 4. while u is even do */ - while (mp_iseven (&u) == 1) { + while (mp_iseven (&u) == MP_YES) { /* 4.1 u = u/2 */ if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { goto LBL_ERR; } /* 4.2 if A or B is odd then */ - if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) { + if (mp_isodd (&A) == MP_YES || mp_isodd (&B) == MP_YES) { /* A = (A+y)/2, B = (B-x)/2 */ if ((res = mp_add (&A, &y, &A)) != MP_OKAY) { goto LBL_ERR; @@ -83,13 +83,13 @@ top: } /* 5. while v is even do */ - while (mp_iseven (&v) == 1) { + while (mp_iseven (&v) == MP_YES) { /* 5.1 v = v/2 */ if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { goto LBL_ERR; } /* 5.2 if C or D is odd then */ - if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) { + if (mp_isodd (&C) == MP_YES || mp_isodd (&D) == MP_YES) { /* C = (C+y)/2, D = (D-x)/2 */ if ((res = mp_add (&C, &y, &C)) != MP_OKAY) { goto LBL_ERR; @@ -137,7 +137,7 @@ top: } /* if not zero goto step 4 */ - if (mp_iszero (&u) == 0) + if (mp_iszero (&u) == MP_NO) goto top; /* now a = C, b = D, gcd == g*v */ diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c index b0722df..b8427a5 100644 --- a/bn_mp_jacobi.c +++ b/bn_mp_jacobi.c @@ -30,7 +30,7 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c) } /* step 1. if a == 0, return 0 */ - if (mp_iszero (a) == 1) { + if (mp_iszero (a) == MP_YES) { *c = 0; return MP_OKAY; } diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c index e306f3b..a977a44 100644 --- a/bn_mp_prime_next_prime.c +++ b/bn_mp_prime_next_prime.c @@ -84,7 +84,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { return err; }; } } else { - if (mp_iseven(a) == 1) { + if (mp_iseven(a) == MP_YES) { /* force odd */ if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { return err; diff --git a/bn_mp_read_radix.c b/bn_mp_read_radix.c index 8d3c710..d407e9d 100644 --- a/bn_mp_read_radix.c +++ b/bn_mp_read_radix.c @@ -73,7 +73,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix) } /* set the sign only if a != 0 */ - if (mp_iszero(a) != 1) { + if (mp_iszero(a) != MP_YES) { a->sign = neg; } return MP_OKAY; diff --git a/bn_mp_to_unsigned_bin.c b/bn_mp_to_unsigned_bin.c index 9655b31..132a6ca 100644 --- a/bn_mp_to_unsigned_bin.c +++ b/bn_mp_to_unsigned_bin.c @@ -26,7 +26,7 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b) } x = 0; - while (mp_iszero (&t) == 0) { + while (mp_iszero (&t) == MP_NO) { #ifndef MP_8BIT b[x++] = (unsigned char) (t.dp[0] & 255); #else diff --git a/bn_mp_toradix.c b/bn_mp_toradix.c index f14fd07..94efc7a 100644 --- a/bn_mp_toradix.c +++ b/bn_mp_toradix.c @@ -29,7 +29,7 @@ int mp_toradix (mp_int * a, char *str, int radix) } /* quick out if its zero */ - if (mp_iszero(a) == 1) { + if (mp_iszero(a) == MP_YES) { *str++ = '0'; *str = '\0'; return MP_OKAY; @@ -47,7 +47,7 @@ int mp_toradix (mp_int * a, char *str, int radix) } digs = 0; - while (mp_iszero (&t) == 0) { + while (mp_iszero (&t) == MP_NO) { if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) { mp_clear (&t); return res; diff --git a/bn_mp_toradix_n.c b/bn_mp_toradix_n.c index 173372b..eb8195f 100644 --- a/bn_mp_toradix_n.c +++ b/bn_mp_toradix_n.c @@ -56,7 +56,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) } digs = 0; - while (mp_iszero (&t) == 0) { + while (mp_iszero (&t) == MP_NO) { if (--maxlen < 1) { /* no more room */ break; From 5bed36d99701b3e27268ddbcc7206fd069deecb6 Mon Sep 17 00:00:00 2001 From: Francois Perrad Date: Sun, 11 Oct 2015 10:52:10 +0200 Subject: [PATCH 15/18] explicit ignoring the return value of function --- bn_mp_div.c | 2 +- bn_mp_dr_reduce.c | 2 +- bn_mp_exteuclid.c | 6 +++--- bn_mp_reduce_2k.c | 2 +- bn_mp_reduce_2k_l.c | 2 +- bn_mp_toom_mul.c | 4 ++-- bn_mp_toom_sqr.c | 2 +- 7 files changed, 10 insertions(+), 10 deletions(-) diff --git a/bn_mp_div.c b/bn_mp_div.c index c33fac8..3dc28c9 100644 --- a/bn_mp_div.c +++ b/bn_mp_div.c @@ -270,7 +270,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } if (d != NULL) { - mp_div_2d (&x, norm, &x, NULL); + (void)mp_div_2d (&x, norm, &x, NULL); mp_exch (&x, d); } diff --git a/bn_mp_dr_reduce.c b/bn_mp_dr_reduce.c index 84c3006..62e9612 100644 --- a/bn_mp_dr_reduce.c +++ b/bn_mp_dr_reduce.c @@ -82,7 +82,7 @@ top: * Each successive "recursion" makes the input smaller and smaller. */ if (mp_cmp_mag (x, n) != MP_LT) { - s_mp_sub(x, n, x); + (void)s_mp_sub(x, n, x); goto top; } return MP_OKAY; diff --git a/bn_mp_exteuclid.c b/bn_mp_exteuclid.c index c486e1c..3b0bb4a 100644 --- a/bn_mp_exteuclid.c +++ b/bn_mp_exteuclid.c @@ -61,9 +61,9 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) /* make sure U3 >= 0 */ if (u3.sign == MP_NEG) { - mp_neg(&u1, &u1); - mp_neg(&u2, &u2); - mp_neg(&u3, &u3); + (void)mp_neg(&u1, &u1); + (void)mp_neg(&u2, &u2); + (void)mp_neg(&u3, &u3); } /* copy result out */ diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c index cfa59c7..f03bc11 100644 --- a/bn_mp_reduce_2k.c +++ b/bn_mp_reduce_2k.c @@ -45,7 +45,7 @@ top: } if (mp_cmp_mag(a, n) != MP_LT) { - s_mp_sub(a, n, a); + (void)s_mp_sub(a, n, a); goto top; } diff --git a/bn_mp_reduce_2k_l.c b/bn_mp_reduce_2k_l.c index 076018c..0ee5402 100644 --- a/bn_mp_reduce_2k_l.c +++ b/bn_mp_reduce_2k_l.c @@ -46,7 +46,7 @@ top: } if (mp_cmp_mag(a, n) != MP_LT) { - s_mp_sub(a, n, a); + (void)s_mp_sub(a, n, a); goto top; } diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c index f0df3c2..81fec9f 100644 --- a/bn_mp_toom_mul.c +++ b/bn_mp_toom_mul.c @@ -46,7 +46,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) goto ERR; } mp_rshd(&a1, B); - mp_mod_2d(&a1, DIGIT_BIT * B, &a1); + (void)mp_mod_2d(&a1, DIGIT_BIT * B, &a1); if ((res = mp_copy(a, &a2)) != MP_OKAY) { goto ERR; @@ -62,7 +62,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) goto ERR; } mp_rshd(&b1, B); - mp_mod_2d(&b1, DIGIT_BIT * B, &b1); + (void)mp_mod_2d(&b1, DIGIT_BIT * B, &b1); if ((res = mp_copy(b, &b2)) != MP_OKAY) { goto ERR; diff --git a/bn_mp_toom_sqr.c b/bn_mp_toom_sqr.c index f8208a6..d2c096c 100644 --- a/bn_mp_toom_sqr.c +++ b/bn_mp_toom_sqr.c @@ -39,7 +39,7 @@ mp_toom_sqr(mp_int *a, mp_int *b) goto ERR; } mp_rshd(&a1, B); - mp_mod_2d(&a1, DIGIT_BIT * B, &a1); + (void)mp_mod_2d(&a1, DIGIT_BIT * B, &a1); if ((res = mp_copy(a, &a2)) != MP_OKAY) { goto ERR; From 1c1baaa755f8a45ff61d3f281fb6c0b801f79469 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 12 Nov 2015 01:18:00 +0100 Subject: [PATCH 16/18] Don't cast the potential problems away, handle them appropriately --- bn_mp_div.c | 4 +++- bn_mp_dr_reduce.c | 4 +++- bn_mp_exteuclid.c | 6 +++--- bn_mp_reduce_2k.c | 4 +++- bn_mp_reduce_2k_l.c | 4 +++- bn_mp_toom_mul.c | 4 +++- bn_mp_toom_sqr.c | 4 +++- 7 files changed, 21 insertions(+), 9 deletions(-) diff --git a/bn_mp_div.c b/bn_mp_div.c index 3dc28c9..630f2dc 100644 --- a/bn_mp_div.c +++ b/bn_mp_div.c @@ -270,7 +270,9 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } if (d != NULL) { - (void)mp_div_2d (&x, norm, &x, NULL); + if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) { + goto LBL_Y; + } mp_exch (&x, d); } diff --git a/bn_mp_dr_reduce.c b/bn_mp_dr_reduce.c index 62e9612..7cfe462 100644 --- a/bn_mp_dr_reduce.c +++ b/bn_mp_dr_reduce.c @@ -82,7 +82,9 @@ top: * Each successive "recursion" makes the input smaller and smaller. */ if (mp_cmp_mag (x, n) != MP_LT) { - (void)s_mp_sub(x, n, x); + if ((err = s_mp_sub(x, n, x)) != MP_OKAY) { + return err; + } goto top; } return MP_OKAY; diff --git a/bn_mp_exteuclid.c b/bn_mp_exteuclid.c index 3b0bb4a..624f81d 100644 --- a/bn_mp_exteuclid.c +++ b/bn_mp_exteuclid.c @@ -61,9 +61,9 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) /* make sure U3 >= 0 */ if (u3.sign == MP_NEG) { - (void)mp_neg(&u1, &u1); - (void)mp_neg(&u2, &u2); - (void)mp_neg(&u3, &u3); + if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto _ERR; } + if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto _ERR; } + if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto _ERR; } } /* copy result out */ diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c index f03bc11..6abae6c 100644 --- a/bn_mp_reduce_2k.c +++ b/bn_mp_reduce_2k.c @@ -45,7 +45,9 @@ top: } if (mp_cmp_mag(a, n) != MP_LT) { - (void)s_mp_sub(a, n, a); + if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { + goto ERR; + } goto top; } diff --git a/bn_mp_reduce_2k_l.c b/bn_mp_reduce_2k_l.c index 0ee5402..84198a3 100644 --- a/bn_mp_reduce_2k_l.c +++ b/bn_mp_reduce_2k_l.c @@ -46,7 +46,9 @@ top: } if (mp_cmp_mag(a, n) != MP_LT) { - (void)s_mp_sub(a, n, a); + if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { + goto ERR; + } goto top; } diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c index 81fec9f..e2a4ac8 100644 --- a/bn_mp_toom_mul.c +++ b/bn_mp_toom_mul.c @@ -46,7 +46,9 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) goto ERR; } mp_rshd(&a1, B); - (void)mp_mod_2d(&a1, DIGIT_BIT * B, &a1); + if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) { + goto ERR; + } if ((res = mp_copy(a, &a2)) != MP_OKAY) { goto ERR; diff --git a/bn_mp_toom_sqr.c b/bn_mp_toom_sqr.c index d2c096c..0fe967b 100644 --- a/bn_mp_toom_sqr.c +++ b/bn_mp_toom_sqr.c @@ -39,7 +39,9 @@ mp_toom_sqr(mp_int *a, mp_int *b) goto ERR; } mp_rshd(&a1, B); - (void)mp_mod_2d(&a1, DIGIT_BIT * B, &a1); + if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) { + goto ERR; + } if ((res = mp_copy(a, &a2)) != MP_OKAY) { goto ERR; From 00ff6da1cce3e5f41b000c4761ad1b74bec69599 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 12 Nov 2015 01:18:15 +0100 Subject: [PATCH 17/18] trim trailing spaces --- bn_mp_div.c | 32 ++++++++++----------- bn_mp_exteuclid.c | 2 +- bn_mp_reduce_2k.c | 16 +++++------ bn_mp_reduce_2k_l.c | 18 ++++++------ bn_mp_toom_mul.c | 70 ++++++++++++++++++++++----------------------- 5 files changed, 69 insertions(+), 69 deletions(-) diff --git a/bn_mp_div.c b/bn_mp_div.c index 630f2dc..2b87399 100644 --- a/bn_mp_div.c +++ b/bn_mp_div.c @@ -40,7 +40,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) } return res; } - + /* init our temps */ if ((res = mp_init_multi(&ta, &tb, &tq, &q, NULL)) != MP_OKAY) { return res; @@ -50,7 +50,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) mp_set(&tq, 1); n = mp_count_bits(a) - mp_count_bits(b); if (((res = mp_abs(a, &ta)) != MP_OKAY) || - ((res = mp_abs(b, &tb)) != MP_OKAY) || + ((res = mp_abs(b, &tb)) != MP_OKAY) || ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) { goto LBL_ERR; @@ -87,17 +87,17 @@ LBL_ERR: #else -/* integer signed division. +/* integer signed division. * c*b + d == a [e.g. a/b, c=quotient, d=remainder] * HAC pp.598 Algorithm 14.20 * - * Note that the description in HAC is horribly - * incomplete. For example, it doesn't consider - * the case where digits are removed from 'x' in - * the inner loop. It also doesn't consider the + * Note that the description in HAC is horribly + * incomplete. For example, it doesn't consider + * the case where digits are removed from 'x' in + * the inner loop. It also doesn't consider the * case that y has fewer than three digits, etc.. * - * The overall algorithm is as described as + * The overall algorithm is as described as * 14.20 from HAC but fixed to treat these cases. */ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) @@ -187,7 +187,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) continue; } - /* step 3.1 if xi == yt then set q{i-t-1} to b-1, + /* step 3.1 if xi == yt then set q{i-t-1} to b-1, * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ if (x.dp[i] == y.dp[t]) { q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); @@ -202,10 +202,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); } - /* while (q{i-t-1} * (yt * b + y{t-1})) > - xi * b**2 + xi-1 * b + xi-2 - - do q{i-t-1} -= 1; + /* while (q{i-t-1} * (yt * b + y{t-1})) > + xi * b**2 + xi-1 * b + xi-2 + + do q{i-t-1} -= 1; */ q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; do { @@ -256,10 +256,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } } - /* now q is the quotient and x is the remainder - * [which we have to normalize] + /* now q is the quotient and x is the remainder + * [which we have to normalize] */ - + /* get sign before writing to c */ x.sign = x.used == 0 ? MP_ZPOS : a->sign; diff --git a/bn_mp_exteuclid.c b/bn_mp_exteuclid.c index 624f81d..f04fca0 100644 --- a/bn_mp_exteuclid.c +++ b/bn_mp_exteuclid.c @@ -15,7 +15,7 @@ * Tom St Denis, tstdenis82@gmail.com, http://libtom.org */ -/* Extended euclidean algorithm of (a, b) produces +/* Extended euclidean algorithm of (a, b) produces a*u1 + b*u2 = u3 */ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3) diff --git a/bn_mp_reduce_2k.c b/bn_mp_reduce_2k.c index 6abae6c..d19b847 100644 --- a/bn_mp_reduce_2k.c +++ b/bn_mp_reduce_2k.c @@ -20,37 +20,37 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d) { mp_int q; int p, res; - + if ((res = mp_init(&q)) != MP_OKAY) { return res; } - - p = mp_count_bits(n); + + p = mp_count_bits(n); top: /* q = a/2**p, a = a mod 2**p */ if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { goto ERR; } - + if (d != 1) { /* q = q * d */ - if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { + if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) { goto ERR; } } - + /* a = a + q */ if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { goto ERR; } - + if (mp_cmp_mag(a, n) != MP_LT) { if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { goto ERR; } goto top; } - + ERR: mp_clear(&q); return res; diff --git a/bn_mp_reduce_2k_l.c b/bn_mp_reduce_2k_l.c index 84198a3..675065f 100644 --- a/bn_mp_reduce_2k_l.c +++ b/bn_mp_reduce_2k_l.c @@ -15,7 +15,7 @@ * Tom St Denis, tstdenis82@gmail.com, http://libtom.org */ -/* reduces a modulo n where n is of the form 2**p - d +/* reduces a modulo n where n is of the form 2**p - d This differs from reduce_2k since "d" can be larger than a single digit. */ @@ -23,35 +23,35 @@ int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d) { mp_int q; int p, res; - + if ((res = mp_init(&q)) != MP_OKAY) { return res; } - - p = mp_count_bits(n); + + p = mp_count_bits(n); top: /* q = a/2**p, a = a mod 2**p */ if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) { goto ERR; } - + /* q = q * d */ - if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { + if ((res = mp_mul(&q, d, &q)) != MP_OKAY) { goto ERR; } - + /* a = a + q */ if ((res = s_mp_add(a, &q, a)) != MP_OKAY) { goto ERR; } - + if (mp_cmp_mag(a, n) != MP_LT) { if ((res = s_mp_sub(a, n, a)) != MP_OKAY) { goto ERR; } goto top; } - + ERR: mp_clear(&q); return res; diff --git a/bn_mp_toom_mul.c b/bn_mp_toom_mul.c index e2a4ac8..942680d 100644 --- a/bn_mp_toom_mul.c +++ b/bn_mp_toom_mul.c @@ -15,28 +15,28 @@ * Tom St Denis, tstdenis82@gmail.com, http://libtom.org */ -/* multiplication using the Toom-Cook 3-way algorithm +/* multiplication using the Toom-Cook 3-way algorithm * - * Much more complicated than Karatsuba but has a lower - * asymptotic running time of O(N**1.464). This algorithm is - * only particularly useful on VERY large inputs + * Much more complicated than Karatsuba but has a lower + * asymptotic running time of O(N**1.464). This algorithm is + * only particularly useful on VERY large inputs * (we're talking 1000s of digits here...). */ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) { mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2; int res, B; - + /* init temps */ - if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, - &a0, &a1, &a2, &b0, &b1, + if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, + &a0, &a1, &a2, &b0, &b1, &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) { return res; } - + /* B */ B = MIN(a->used, b->used) / 3; - + /* a = a2 * B**2 + a1 * B + a0 */ if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) { goto ERR; @@ -54,7 +54,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) goto ERR; } mp_rshd(&a2, B*2); - + /* b = b2 * B**2 + b1 * B + b0 */ if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) { goto ERR; @@ -70,17 +70,17 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) goto ERR; } mp_rshd(&b2, B*2); - + /* w0 = a0*b0 */ if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) { goto ERR; } - + /* w4 = a2 * b2 */ if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) { goto ERR; } - + /* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */ if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) { goto ERR; @@ -94,7 +94,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) { goto ERR; } - + if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) { goto ERR; } @@ -107,11 +107,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) { goto ERR; } - + if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) { goto ERR; } - + /* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */ if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) { goto ERR; @@ -125,7 +125,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) { goto ERR; } - + if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) { goto ERR; } @@ -138,11 +138,11 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) { goto ERR; } - + if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) { goto ERR; } - + /* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */ if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) { @@ -160,19 +160,19 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) { goto ERR; } - - /* now solve the matrix - + + /* now solve the matrix + 0 0 0 0 1 1 2 4 8 16 1 1 1 1 1 16 8 4 2 1 1 0 0 0 0 - - using 12 subtractions, 4 shifts, - 2 small divisions and 1 small multiplication + + using 12 subtractions, 4 shifts, + 2 small divisions and 1 small multiplication */ - + /* r1 - r4 */ if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) { goto ERR; @@ -244,7 +244,7 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) { goto ERR; } - + /* at this point shift W[n] by B*n */ if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) { goto ERR; @@ -257,8 +257,8 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) } if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) { goto ERR; - } - + } + if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) { goto ERR; } @@ -270,15 +270,15 @@ int mp_toom_mul(mp_int *a, mp_int *b, mp_int *c) } if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) { goto ERR; - } - + } + ERR: - mp_clear_multi(&w0, &w1, &w2, &w3, &w4, - &a0, &a1, &a2, &b0, &b1, + mp_clear_multi(&w0, &w1, &w2, &w3, &w4, + &a0, &a1, &a2, &b0, &b1, &b2, &tmp1, &tmp2, NULL); return res; -} - +} + #endif /* $Source$ */ From bd39da2397ff12de264feab94b94a6cd9a48bb44 Mon Sep 17 00:00:00 2001 From: Steffen Jaeckel Date: Thu, 12 Nov 2015 01:33:25 +0100 Subject: [PATCH 18/18] use correct value to compare to in mp_isneg() macro --- tommath.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tommath.h b/tommath.h index 5dcfdb6..16cd023 100644 --- a/tommath.h +++ b/tommath.h @@ -248,7 +248,7 @@ int mp_init_size(mp_int *a, int size); #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) #define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) #define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) -#define mp_isneg(a) (((a)->sign != 0) ? MP_YES : MP_NO) +#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) /* set to zero */ void mp_zero(mp_int *a);