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_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_cnt_lsb.c b/bn_mp_cnt_lsb.c index a617767..92cc4e8 100644 --- a/bn_mp_cnt_lsb.c +++ b/bn_mp_cnt_lsb.c @@ -26,12 +26,12 @@ 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; } /* 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; diff --git a/bn_mp_div.c b/bn_mp_div.c index 336a57b..2b87399 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; } @@ -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) @@ -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; } @@ -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); @@ -196,15 +196,16 @@ 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)); } - /* 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 { @@ -255,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; @@ -269,7 +270,9 @@ 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); + if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) { + goto LBL_Y; + } mp_exch (&x, d); } diff --git a/bn_mp_div_d.c b/bn_mp_div_d.c index 579221d..dbaeb10 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; } @@ -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_dr_reduce.c b/bn_mp_dr_reduce.c index 84c3006..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) { - 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_export.c b/bn_mp_export.c index 1a82e4a..7829315 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); } @@ -47,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) { @@ -73,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 93f4f19..8b12d24 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; @@ -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 */ @@ -69,9 +71,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_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_exteuclid.c b/bn_mp_exteuclid.c index c486e1c..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) @@ -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); + 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_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_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); } 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); } 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_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_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_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_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_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_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_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..d407e9d 100644 --- a/bn_mp_read_radix.c +++ b/bn_mp_read_radix.c @@ -43,12 +43,12 @@ 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] */ - 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; @@ -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_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_reduce_2k.c b/bn_mp_reduce_2k.c index cfa59c7..d19b847 100644 --- a/bn_mp_reduce_2k.c +++ b/bn_mp_reduce_2k.c @@ -20,35 +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) { - s_mp_sub(a, n, a); + 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 076018c..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,33 +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) { - s_mp_sub(a, n, a); + 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_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 f1d72ef..5167ce0 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 @@ -42,11 +42,13 @@ if (a->used >= KARATSUBA_SQR_CUTOFF) { 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; 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 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_toom_mul.c b/bn_mp_toom_mul.c index 3d8ab99..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; @@ -46,13 +46,15 @@ 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); + if ((res = mp_mod_2d(&a1, DIGIT_BIT * B, &a1)) != MP_OKAY) { + goto ERR; + } if ((res = mp_copy(a, &a2)) != MP_OKAY) { 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; @@ -62,23 +64,23 @@ 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; } 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; @@ -92,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; } @@ -105,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; @@ -123,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; } @@ -136,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) { @@ -158,125 +160,125 @@ 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; - } - /* 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; - } - - 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; - } - + + /* 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; + } + + 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 /* $Source$ */ diff --git a/bn_mp_toom_sqr.c b/bn_mp_toom_sqr.c index cbee45c..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); - 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; @@ -115,108 +117,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 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; diff --git a/tommath.h b/tommath.h index 0097b5e..16cd023 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,9 +246,9 @@ 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_isneg(a) (((a)->sign) ? 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_ZPOS) ? MP_YES : MP_NO) /* set to zero */ void mp_zero(mp_int *a); @@ -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); @@ -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; \