diff options
Diffstat (limited to 'libtommath/pre_gen/mpi.c')
-rw-r--r-- | libtommath/pre_gen/mpi.c | 993 |
1 files changed, 735 insertions, 258 deletions
diff --git a/libtommath/pre_gen/mpi.c b/libtommath/pre_gen/mpi.c index d2224c0..1b1052a 100644 --- a/libtommath/pre_gen/mpi.c +++ b/libtommath/pre_gen/mpi.c @@ -43,6 +43,10 @@ char *mp_error_to_string(int code) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_error.c */ /* Start: bn_fast_mp_invmod.c */ @@ -63,10 +67,10 @@ char *mp_error_to_string(int code) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* computes the modular inverse via binary extended euclidean algorithm, - * that is c = 1/a mod b +/* computes the modular inverse via binary extended euclidean algorithm, + * that is c = 1/a mod b * - * Based on slow invmod except this is optimized for the case where b is + * Based on slow invmod except this is optimized for the case where b is * odd as per HAC Note 14.64 on pp. 610 */ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) @@ -191,6 +195,10 @@ LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_fast_mp_invmod.c */ /* Start: bn_fast_mp_montgomery_reduce.c */ @@ -363,6 +371,10 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_fast_mp_montgomery_reduce.c */ /* Start: bn_fast_s_mp_mul_digs.c */ @@ -385,15 +397,15 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) /* Fast (comba) multiplier * - * This is the fast column-array [comba] multiplier. It is - * designed to compute the columns of the product first - * then handle the carries afterwards. This has the effect + * This is the fast column-array [comba] multiplier. It is + * designed to compute the columns of the product first + * then handle the carries afterwards. This has the effect * of making the nested loops that compute the columns very * simple and schedulable on super-scalar processors. * - * This has been modified to produce a variable number of - * digits of output so if say only a half-product is required - * you don't have to compute the upper half (a feature + * This has been modified to produce a variable number of + * digits of output so if say only a half-product is required + * you don't have to compute the upper half (a feature * required for fast Barrett reduction). * * Based on Algorithm 14.12 on pp.595 of HAC. @@ -417,7 +429,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) /* clear the carry */ _W = 0; - for (ix = 0; ix < pa; ix++) { + for (ix = 0; ix < pa; ix++) { int tx, ty; int iy; mp_digit *tmpx, *tmpy; @@ -430,7 +442,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) tmpx = a->dp + tx; tmpy = b->dp + ty; - /* this is the number of times the loop will iterrate, essentially + /* this is the number of times the loop will iterrate, essentially while (tx++ < a->used && ty-- >= 0) { ... } */ iy = MIN(a->used-tx, ty+1); @@ -470,6 +482,10 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_fast_s_mp_mul_digs.c */ /* Start: bn_fast_s_mp_mul_high_digs.c */ @@ -516,7 +532,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) /* number of output digits to produce */ pa = a->used + b->used; _W = 0; - for (ix = digs; ix < pa; ix++) { + for (ix = digs; ix < pa; ix++) { int tx, ty, iy; mp_digit *tmpx, *tmpy; @@ -528,7 +544,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) tmpx = a->dp + tx; tmpy = b->dp + ty; - /* this is the number of times the loop will iterrate, essentially its + /* this is the number of times the loop will iterrate, essentially its while (tx++ < a->used && ty-- >= 0) { ... } */ iy = MIN(a->used-tx, ty+1); @@ -544,7 +560,7 @@ int fast_s_mp_mul_high_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; c->used = pa; @@ -568,6 +584,10 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_fast_s_mp_mul_high_digs.c */ /* Start: bn_fast_s_mp_sqr.c */ @@ -589,10 +609,10 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) */ /* the jist of squaring... - * you do like mult except the offset of the tmpx [one that - * starts closer to zero] can't equal the offset of tmpy. + * you do like mult except the offset of the tmpx [one that + * starts closer to zero] can't equal the offset of tmpy. * So basically you set up iy like before then you min it with - * (ty-tx) so that it never happens. You double all those + * (ty-tx) so that it never happens. You double all those * you add in the inner loop After that loop you do the squares and add them in. @@ -614,7 +634,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) /* number of output digits to produce */ W1 = 0; - for (ix = 0; ix < pa; ix++) { + for (ix = 0; ix < pa; ix++) { int tx, ty, iy; mp_word _W; mp_digit *tmpy; @@ -635,7 +655,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) */ iy = MIN(a->used-tx, ty+1); - /* now for squaring tx can never equal ty + /* now for squaring tx can never equal ty * we halve the distance since they approach at a rate of 2x * and we have to round because odd cases need to be executed */ @@ -682,6 +702,10 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_fast_s_mp_sqr.c */ /* Start: bn_mp_2expt.c */ @@ -702,7 +726,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* computes a = 2**b +/* computes a = 2**b * * Simple algorithm which zeroes the int, grows it then just sets one bit * as required. @@ -730,6 +754,10 @@ mp_2expt (mp_int * a, int b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_2expt.c */ /* Start: bn_mp_abs.c */ @@ -750,7 +778,7 @@ mp_2expt (mp_int * a, int b) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* b = |a| +/* b = |a| * * Simple function copies the input and fixes the sign to positive */ @@ -773,6 +801,10 @@ mp_abs (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_abs.c */ /* Start: bn_mp_add.c */ @@ -826,6 +858,10 @@ int mp_add (mp_int * a, mp_int * b, mp_int * c) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_add.c */ /* Start: bn_mp_add_d.c */ @@ -938,6 +974,10 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_add_d.c */ /* Start: bn_mp_addmod.c */ @@ -979,6 +1019,10 @@ mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_addmod.c */ /* Start: bn_mp_and.c */ @@ -1036,6 +1080,10 @@ mp_and (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_and.c */ /* Start: bn_mp_clamp.c */ @@ -1056,7 +1104,7 @@ mp_and (mp_int * a, mp_int * b, mp_int * c) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* trim unused digits +/* trim unused digits * * This is used to ensure that leading zero digits are * trimed and the leading "used" digit will be non-zero @@ -1080,6 +1128,10 @@ mp_clamp (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_clamp.c */ /* Start: bn_mp_clear.c */ @@ -1124,6 +1176,10 @@ mp_clear (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_clear.c */ /* Start: bn_mp_clear_multi.c */ @@ -1145,7 +1201,7 @@ mp_clear (mp_int * a) */ #include <stdarg.h> -void mp_clear_multi(mp_int *mp, ...) +void mp_clear_multi(mp_int *mp, ...) { mp_int* next_mp = mp; va_list args; @@ -1158,6 +1214,10 @@ void mp_clear_multi(mp_int *mp, ...) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_clear_multi.c */ /* Start: bn_mp_cmp.c */ @@ -1190,7 +1250,7 @@ mp_cmp (mp_int * a, mp_int * b) return MP_GT; } } - + /* compare digits */ if (a->sign == MP_NEG) { /* if negative compare opposite direction */ @@ -1201,6 +1261,10 @@ mp_cmp (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_cmp.c */ /* Start: bn_mp_cmp_d.c */ @@ -1245,6 +1309,10 @@ int mp_cmp_d(mp_int * a, mp_digit b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_cmp_d.c */ /* Start: bn_mp_cmp_mag.c */ @@ -1275,7 +1343,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b) if (a->used > b->used) { return MP_GT; } - + if (a->used < b->used) { return MP_LT; } @@ -1300,6 +1368,10 @@ int mp_cmp_mag (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_cmp_mag.c */ /* Start: bn_mp_cnt_lsb.c */ @@ -1320,7 +1392,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -static const int lnz[16] = { +static const int lnz[16] = { 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0 }; @@ -1353,6 +1425,10 @@ int mp_cnt_lsb(mp_int *a) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_cnt_lsb.c */ /* Start: bn_mp_copy.c */ @@ -1421,6 +1497,10 @@ mp_copy (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_copy.c */ /* Start: bn_mp_count_bits.c */ @@ -1455,7 +1535,7 @@ mp_count_bits (mp_int * a) /* get number of digits and add that */ r = (a->used - 1) * DIGIT_BIT; - + /* take the last digit and count the bits in it */ q = a->dp[a->used - 1]; while (q > ((mp_digit) 0)) { @@ -1466,6 +1546,10 @@ mp_count_bits (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_count_bits.c */ /* Start: bn_mp_div.c */ @@ -1521,7 +1605,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; @@ -1558,17 +1642,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) @@ -1658,7 +1742,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); @@ -1672,10 +1756,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 { @@ -1726,10 +1810,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; @@ -1758,6 +1842,10 @@ LBL_Q:mp_clear (&q); #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_div.c */ /* Start: bn_mp_div_2.c */ @@ -1826,6 +1914,10 @@ int mp_div_2(mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_div_2.c */ /* Start: bn_mp_div_2d.c */ @@ -1923,6 +2015,10 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_div_2d.c */ /* Start: bn_mp_div_3.c */ @@ -1951,14 +2047,14 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) mp_word w, t; mp_digit b; int res, ix; - + /* b = 2**DIGIT_BIT / 3 */ b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { return res; } - + q.used = a->used; q.sign = a->sign; w = 0; @@ -1996,12 +2092,16 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d) mp_exch(&q, c); } mp_clear(&q); - + return res; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_div_3.c */ /* Start: bn_mp_div_d.c */ @@ -2086,13 +2186,13 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) if ((res = mp_init_size(&q, a->used)) != MP_OKAY) { return res; } - + q.used = a->used; q.sign = a->sign; w = 0; for (ix = a->used - 1; ix >= 0; ix--) { w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); - + if (w >= b) { t = (mp_digit)(w / b); w -= ((mp_word)t) * ((mp_word)b); @@ -2101,22 +2201,26 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d) } q.dp[ix] = (mp_digit)t; } - + if (d != NULL) { *d = (mp_digit)w; } - + if (c != NULL) { mp_clamp(&q); mp_exch(&q, c); } mp_clear(&q); - + return res; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_div_d.c */ /* Start: bn_mp_dr_is_modulus.c */ @@ -2160,6 +2264,10 @@ int mp_dr_is_modulus(mp_int *a) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_dr_is_modulus.c */ /* Start: bn_mp_dr_reduce.c */ @@ -2254,6 +2362,10 @@ top: } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_dr_reduce.c */ /* Start: bn_mp_dr_setup.c */ @@ -2280,12 +2392,16 @@ void mp_dr_setup(mp_int *a, mp_digit *d) /* the casts are required if DIGIT_BIT is one less than * the number of bits in a mp_digit [e.g. DIGIT_BIT==31] */ - *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - + *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - ((mp_word)a->dp[0])); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_dr_setup.c */ /* Start: bn_mp_exch.c */ @@ -2306,7 +2422,7 @@ void mp_dr_setup(mp_int *a, mp_digit *d) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* swap the elements of two integers, for cases where you can't simply swap the +/* swap the elements of two integers, for cases where you can't simply swap the * mp_int pointers around */ void @@ -2320,6 +2436,10 @@ mp_exch (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_exch.c */ /* Start: bn_mp_expt_d.c */ @@ -2377,6 +2497,10 @@ int mp_expt_d (mp_int * a, mp_digit b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_expt_d.c */ /* Start: bn_mp_exptmod.c */ @@ -2441,7 +2565,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) err = mp_exptmod(&tmpG, &tmpX, P, Y); mp_clear_multi(&tmpG, &tmpX, NULL); return err; -#else +#else /* no invmod */ return MP_VAL; #endif @@ -2468,7 +2592,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) dr = mp_reduce_is_2k(P) << 1; } #endif - + /* 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) { @@ -2489,6 +2613,10 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_exptmod.c */ /* Start: bn_mp_exptmod_fast.c */ @@ -2578,7 +2706,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode /* determine and setup reduction code */ if (redmode == 0) { -#ifdef BN_MP_MONTGOMERY_SETUP_C +#ifdef BN_MP_MONTGOMERY_SETUP_C /* now setup montgomery */ if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { goto LBL_M; @@ -2593,7 +2721,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode if (((P->used * 2 + 1) < MP_WARRAY) && P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { redux = fast_mp_montgomery_reduce; - } else + } else #endif { #ifdef BN_MP_MONTGOMERY_REDUCE_C @@ -2644,7 +2772,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) { goto LBL_RES; } -#else +#else err = MP_VAL; goto LBL_RES; #endif @@ -2809,6 +2937,11 @@ LBL_M: } #endif + +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_exptmod_fast.c */ /* Start: bn_mp_exteuclid.c */ @@ -2829,7 +2962,7 @@ LBL_M: * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* 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) @@ -2891,6 +3024,10 @@ _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_exteuclid.c */ /* Start: bn_mp_fread.c */ @@ -2915,10 +3052,10 @@ _ERR: mp_clear_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL int mp_fread(mp_int *a, int radix, FILE *stream) { int err, ch, neg, y; - + /* clear a */ mp_zero(a); - + /* if first digit is - then set negative */ ch = fgetc(stream); if (ch == '-') { @@ -2927,7 +3064,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream) } else { neg = MP_ZPOS; } - + for (;;) { /* find y in the radix map */ for (y = 0; y < radix; y++) { @@ -2938,7 +3075,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream) if (y == radix) { break; } - + /* shift up and add */ if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) { return err; @@ -2946,18 +3083,22 @@ int mp_fread(mp_int *a, int radix, FILE *stream) if ((err = mp_add_d(a, y, a)) != MP_OKAY) { return err; } - + ch = fgetc(stream); } if (mp_cmp_d(a, 0) != MP_EQ) { a->sign = neg; } - + return MP_OKAY; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_fread.c */ /* Start: bn_mp_fwrite.c */ @@ -2982,7 +3123,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream) { char *buf; int err, len, x; - + if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) { return err; } @@ -2991,25 +3132,29 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream) if (buf == NULL) { return MP_MEM; } - + if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) { XFREE (buf); return err; } - + for (x = 0; x < len; x++) { if (fputc(buf[x], stream) == EOF) { XFREE (buf); return MP_VAL; } } - + XFREE (buf); return MP_OKAY; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_fwrite.c */ /* Start: bn_mp_gcd.c */ @@ -3091,17 +3236,17 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) /* swap u and v to make sure v is >= u */ mp_exch(&u, &v); } - + /* subtract smallest from largest */ if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { goto LBL_V; } - + /* Divide out all factors of two */ if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { goto LBL_V; - } - } + } + } /* multiply by 2**k which we divided out at the beginning */ if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { @@ -3115,6 +3260,10 @@ LBL_U:mp_clear (&v); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_gcd.c */ /* Start: bn_mp_get_int.c */ @@ -3136,7 +3285,7 @@ LBL_U:mp_clear (&v); */ /* get the lower 32-bits of an mp_int */ -unsigned long mp_get_int(mp_int * a) +unsigned long mp_get_int(mp_int * a) { int i; unsigned long res; @@ -3150,7 +3299,7 @@ unsigned long mp_get_int(mp_int * a) /* get most significant digit of result */ res = DIGIT(a,i); - + while (--i >= 0) { res = (res << DIGIT_BIT) | DIGIT(a,i); } @@ -3160,6 +3309,10 @@ unsigned long mp_get_int(mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_get_int.c */ /* Start: bn_mp_grow.c */ @@ -3217,6 +3370,10 @@ int mp_grow (mp_int * a, int size) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_grow.c */ /* Start: bn_mp_init.c */ @@ -3263,6 +3420,10 @@ int mp_init (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_init.c */ /* Start: bn_mp_init_copy.c */ @@ -3295,6 +3456,10 @@ int mp_init_copy (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_init_copy.c */ /* Start: bn_mp_init_multi.c */ @@ -3316,7 +3481,7 @@ int mp_init_copy (mp_int * a, mp_int * b) */ #include <stdarg.h> -int mp_init_multi(mp_int *mp, ...) +int mp_init_multi(mp_int *mp, ...) { mp_err res = MP_OKAY; /* Assume ok until proven otherwise */ int n = 0; /* Number of ok inits */ @@ -3330,11 +3495,11 @@ int mp_init_multi(mp_int *mp, ...) succeeded in init-ing, then return error. */ va_list clean_args; - + /* end the current list */ va_end(args); - - /* now start cleaning up */ + + /* now start cleaning up */ cur_arg = mp; va_start(clean_args, mp); while (n--) { @@ -3354,6 +3519,10 @@ int mp_init_multi(mp_int *mp, ...) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_init_multi.c */ /* Start: bn_mp_init_set.c */ @@ -3386,6 +3555,10 @@ int mp_init_set (mp_int * a, mp_digit b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_init_set.c */ /* Start: bn_mp_init_set_int.c */ @@ -3417,6 +3590,10 @@ int mp_init_set_int (mp_int * a, unsigned long b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_init_set_int.c */ /* Start: bn_mp_init_size.c */ @@ -3444,7 +3621,7 @@ int mp_init_size (mp_int * a, int size) /* pad size so there are always extra digits */ size += (MP_PREC * 2) - (size % MP_PREC); - + /* alloc mem */ a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size); if (a->dp == NULL) { @@ -3465,6 +3642,10 @@ int mp_init_size (mp_int * a, int size) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_init_size.c */ /* Start: bn_mp_invmod.c */ @@ -3508,6 +3689,10 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_invmod.c */ /* Start: bn_mp_invmod_slow.c */ @@ -3540,7 +3725,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) } /* init temps */ - if ((res = mp_init_multi(&x, &y, &u, &v, + if ((res = mp_init_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL)) != MP_OKAY) { return res; } @@ -3667,14 +3852,14 @@ top: goto LBL_ERR; } } - + /* too big */ while (mp_cmp_mag(&C, b) != MP_LT) { if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { goto LBL_ERR; } } - + /* C is now the inverse */ mp_exch (&C, c); res = MP_OKAY; @@ -3683,6 +3868,10 @@ LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_invmod_slow.c */ /* Start: bn_mp_is_square.c */ @@ -3726,7 +3915,7 @@ static const char rem_105[105] = { }; /* Store non-zero to ret if arg is square, and zero if not */ -int mp_is_square(mp_int *arg,int *ret) +int mp_is_square(mp_int *arg,int *ret) { int res; mp_digit c; @@ -3734,7 +3923,7 @@ int mp_is_square(mp_int *arg,int *ret) unsigned long r; /* Default to Non-square :) */ - *ret = MP_NO; + *ret = MP_NO; if (arg->sign == MP_NEG) { return MP_VAL; @@ -3768,8 +3957,8 @@ int mp_is_square(mp_int *arg,int *ret) r = mp_get_int(&t); /* Check for other prime modules, note it's not an ERROR but we must * 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 - */ + * 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; @@ -3792,6 +3981,10 @@ ERR:mp_clear(&t); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_is_square.c */ /* Start: bn_mp_jacobi.c */ @@ -3897,6 +4090,10 @@ LBL_A1:mp_clear (&a1); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_jacobi.c */ /* Start: bn_mp_karatsuba_mul.c */ @@ -3917,33 +4114,33 @@ LBL_A1:mp_clear (&a1); * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* c = |a| * |b| using Karatsuba Multiplication using +/* c = |a| * |b| using Karatsuba Multiplication using * three half size multiplications * - * Let B represent the radix [e.g. 2**DIGIT_BIT] and - * let n represent half of the number of digits in + * Let B represent the radix [e.g. 2**DIGIT_BIT] and + * let n represent half of the number of digits in * the min(a,b) * * a = a1 * B**n + a0 * b = b1 * B**n + b0 * - * Then, a * b => + * Then, a * b => a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0 * - * Note that a1b1 and a0b0 are used twice and only need to be - * computed once. So in total three half size (half # of - * digit) multiplications are performed, a0b0, a1b1 and + * Note that a1b1 and a0b0 are used twice and only need to be + * computed once. So in total three half size (half # of + * digit) multiplications are performed, a0b0, a1b1 and * (a1+b1)(a0+b0) * * Note that a multiplication of half the digits requires - * 1/4th the number of single precision multiplications so in - * total after one call 25% of the single precision multiplications - * are saved. Note also that the call to mp_mul can end up back - * in this function if the a0, a1, b0, or b1 are above the threshold. - * This is known as divide-and-conquer and leads to the famous - * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than - * the standard O(N**2) that the baseline/comba methods use. - * Generally though the overhead of this method doesn't pay off + * 1/4th the number of single precision multiplications so in + * total after one call 25% of the single precision multiplications + * are saved. Note also that the call to mp_mul can end up back + * in this function if the a0, a1, b0, or b1 are above the threshold. + * This is known as divide-and-conquer and leads to the famous + * O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than + * the standard O(N**2) that the baseline/comba methods use. + * Generally though the overhead of this method doesn't pay off * until a certain size (N ~ 80) is reached. */ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) @@ -4011,7 +4208,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) } } - /* only need to clamp the lower words since by definition the + /* only need to clamp the lower words since by definition the * upper words x1/y1 must have a known number of digits */ mp_clamp (&x0); @@ -4019,7 +4216,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c) /* now calc the products x0y0 and x1y1 */ /* after this x0 is no longer required, free temp [x0==t2]! */ - if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY) + if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY) goto X1Y1; /* x0y0 = x0*y0 */ if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY) goto X1Y1; /* x1y1 = x1*y1 */ @@ -4064,6 +4261,10 @@ ERR: } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_karatsuba_mul.c */ /* Start: bn_mp_karatsuba_sqr.c */ @@ -4084,11 +4285,11 @@ ERR: * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* Karatsuba squaring, computes b = a*a using three +/* Karatsuba squaring, computes b = a*a using three * half size squarings * - * See comments of karatsuba_mul for details. It - * is essentially the same algorithm but merely + * See comments of karatsuba_mul for details. It + * is essentially the same algorithm but merely * tuned to perform recursive squarings. */ int mp_karatsuba_sqr (mp_int * a, mp_int * b) @@ -4185,6 +4386,10 @@ ERR: } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_karatsuba_sqr.c */ /* Start: bn_mp_lcm.c */ @@ -4245,6 +4450,10 @@ LBL_T: } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_lcm.c */ /* Start: bn_mp_lshd.c */ @@ -4312,6 +4521,10 @@ int mp_lshd (mp_int * a, int b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_lshd.c */ /* Start: bn_mp_mod.c */ @@ -4360,6 +4573,10 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mod.c */ /* Start: bn_mp_mod_2d.c */ @@ -4415,6 +4632,10 @@ mp_mod_2d (mp_int * a, int b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mod_2d.c */ /* Start: bn_mp_mod_d.c */ @@ -4442,6 +4663,10 @@ mp_mod_d (mp_int * a, mp_digit b, mp_digit * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mod_d.c */ /* Start: bn_mp_montgomery_calc_normalization.c */ @@ -4501,6 +4726,10 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_montgomery_calc_normalization.c */ /* Start: bn_mp_montgomery_reduce.c */ @@ -4619,6 +4848,10 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_montgomery_reduce.c */ /* Start: bn_mp_montgomery_setup.c */ @@ -4678,6 +4911,10 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_montgomery_setup.c */ /* Start: bn_mp_mul.c */ @@ -4708,29 +4945,29 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c) #ifdef BN_MP_TOOM_MUL_C if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) { res = mp_toom_mul(a, b, c); - } else + } else #endif #ifdef BN_MP_KARATSUBA_MUL_C /* use Karatsuba? */ if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) { res = mp_karatsuba_mul (a, b, c); - } else + } else #endif { /* can we use the fast multiplier? * - * The fast multiplier can be used if the output will - * have less than MP_WARRAY digits and the number of + * The fast multiplier can be used if the output will + * have less than MP_WARRAY digits and the number of * digits won't affect carry propagation */ int digs = a->used + b->used + 1; #ifdef BN_FAST_S_MP_MUL_DIGS_C if ((digs < MP_WARRAY) && - MIN(a->used, b->used) <= + MIN(a->used, b->used) <= (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { res = fast_s_mp_mul_digs (a, b, c, digs); - } else + } else #endif #ifdef BN_S_MP_MUL_DIGS_C res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */ @@ -4744,6 +4981,10 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mul.c */ /* Start: bn_mp_mul_2.c */ @@ -4784,24 +5025,24 @@ int mp_mul_2(mp_int * a, mp_int * b) /* alias for source */ tmpa = a->dp; - + /* alias for dest */ tmpb = b->dp; /* carry */ r = 0; for (x = 0; x < a->used; x++) { - - /* get what will be the *next* carry bit from the - * MSB of the current digit + + /* get what will be the *next* carry bit from the + * MSB of the current digit */ rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1)); - + /* now shift up this digit, add in the carry [from the previous] */ *tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK; - - /* copy the carry that would be from the source - * digit into the next iteration + + /* copy the carry that would be from the source + * digit into the next iteration */ r = rr; } @@ -4813,8 +5054,8 @@ int mp_mul_2(mp_int * a, mp_int * b) ++(b->used); } - /* now zero any excess digits on the destination - * that we didn't write to + /* now zero any excess digits on the destination + * that we didn't write to */ tmpb = b->dp + b->used; for (x = b->used; x < oldused; x++) { @@ -4826,6 +5067,10 @@ int mp_mul_2(mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mul_2.c */ /* Start: bn_mp_mul_2d.c */ @@ -4900,7 +5145,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c) /* set the carry to the carry bits of the current word */ r = rr; } - + /* set final carry */ if (r != 0) { c->dp[(c->used)++] = r; @@ -4911,6 +5156,10 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mul_2d.c */ /* Start: bn_mp_mul_d.c */ @@ -4990,6 +5239,10 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mul_d.c */ /* Start: bn_mp_mulmod.c */ @@ -5030,6 +5283,10 @@ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_mulmod.c */ /* Start: bn_mp_n_root.c */ @@ -5050,14 +5307,14 @@ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* find the n'th root of an integer +/* find the n'th root of an integer * - * Result found such that (c)**b <= a and (c+1)**b > a + * Result found such that (c)**b <= a and (c+1)**b > a * - * This algorithm uses Newton's approximation - * x[i+1] = x[i] - f(x[i])/f'(x[i]) - * which will find the root in log(N) time where - * each step involves a fair bit. This is not meant to + * This algorithm uses Newton's approximation + * x[i+1] = x[i] - f(x[i])/f'(x[i]) + * which will find the root in log(N) time where + * each step involves a fair bit. This is not meant to * find huge roots [square and cube, etc]. */ int mp_n_root (mp_int * a, mp_digit b, mp_int * c) @@ -5096,31 +5353,31 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c) } /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ - + /* t3 = t1**(b-1) */ - if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) { + if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) { goto LBL_T3; } /* numerator */ /* t2 = t1**b */ - if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) { + if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) { goto LBL_T3; } /* t2 = t1**b - a */ - if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) { + if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) { goto LBL_T3; } /* denominator */ /* t3 = t1**(b-1) * b */ - if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) { + if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) { goto LBL_T3; } /* t3 = (t1**b - a)/(b * t1**(b-1)) */ - if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) { + if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) { goto LBL_T3; } @@ -5162,6 +5419,10 @@ LBL_T1:mp_clear (&t1); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_n_root.c */ /* Start: bn_mp_neg.c */ @@ -5202,6 +5463,10 @@ int mp_neg (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_neg.c */ /* Start: bn_mp_or.c */ @@ -5252,6 +5517,10 @@ int mp_or (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_or.c */ /* Start: bn_mp_prime_fermat.c */ @@ -5273,7 +5542,7 @@ int mp_or (mp_int * a, mp_int * b, mp_int * c) */ /* performs one Fermat test. - * + * * If "a" were prime then b**a == b (mod a) since the order of * the multiplicative sub-group would be phi(a) = a-1. That means * it would be the same as b**(a mod (a-1)) == b**1 == b (mod a). @@ -5314,6 +5583,10 @@ LBL_T:mp_clear (&t); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_fermat.c */ /* Start: bn_mp_prime_is_divisible.c */ @@ -5334,7 +5607,7 @@ LBL_T:mp_clear (&t); * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* determines if an integers is divisible by one +/* determines if an integers is divisible by one * of the first PRIME_SIZE primes or not * * sets result to 0 if not, 1 if yes @@ -5364,6 +5637,10 @@ int mp_prime_is_divisible (mp_int * a, int *result) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_is_divisible.c */ /* Start: bn_mp_prime_is_prime.c */ @@ -5447,6 +5724,10 @@ LBL_B:mp_clear (&b); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_is_prime.c */ /* Start: bn_mp_prime_miller_rabin.c */ @@ -5467,11 +5748,11 @@ LBL_B:mp_clear (&b); * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* Miller-Rabin test of "a" to the base of "b" as described in +/* Miller-Rabin test of "a" to the base of "b" as described in * HAC pp. 139 Algorithm 4.24 * * Sets result to 0 if definitely composite or 1 if probably prime. - * Randomly the chance of error is no more than 1/4 and often + * Randomly the chance of error is no more than 1/4 and often * very much lower. */ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) @@ -5485,7 +5766,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) /* ensure b > 1 */ if (mp_cmp_d(b, 1) != MP_GT) { return MP_VAL; - } + } /* get n1 = a - 1 */ if ((err = mp_init_copy (&n1, a)) != MP_OKAY) { @@ -5550,6 +5831,10 @@ LBL_N1:mp_clear (&n1); } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_miller_rabin.c */ /* Start: bn_mp_prime_next_prime.c */ @@ -5720,6 +6005,10 @@ LBL_ERR: #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_next_prime.c */ /* Start: bn_mp_prime_rabin_miller_trials.c */ @@ -5772,6 +6061,10 @@ int mp_prime_rabin_miller_trials(int size) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_rabin_miller_trials.c */ /* Start: bn_mp_prime_random_ex.c */ @@ -5795,7 +6088,7 @@ int mp_prime_rabin_miller_trials(int size) /* makes a truly random prime of a given size (bits), * * Flags are as follows: - * + * * LTM_PRIME_BBS - make prime congruent to 3 mod 4 * LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS) * LTM_PRIME_2MSB_OFF - make the 2nd highest bit zero @@ -5840,7 +6133,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0; if (flags & LTM_PRIME_2MSB_ON) { maskOR_msb |= 0x80 >> ((9 - size) & 7); - } + } /* get the maskOR_lsb */ maskOR_lsb = 1; @@ -5854,7 +6147,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback err = MP_VAL; goto error; } - + /* work over the MSbyte */ tmp[0] &= maskAND; tmp[0] |= 1 << ((size - 1) & 7); @@ -5868,7 +6161,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback /* is it prime? */ if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; } - if (res == MP_NO) { + if (res == MP_NO) { continue; } @@ -5876,7 +6169,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback /* 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; } - + /* is it prime? */ if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; } } @@ -5897,6 +6190,10 @@ error: #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_prime_random_ex.c */ /* Start: bn_mp_radix_size.c */ @@ -5956,7 +6253,7 @@ int mp_radix_size (mp_int * a, int radix, int *size) } /* force temp to positive */ - t.sign = MP_ZPOS; + t.sign = MP_ZPOS; /* fetch out all of the digits */ while (mp_iszero (&t) == MP_NO) { @@ -5975,6 +6272,10 @@ int mp_radix_size (mp_int * a, int radix, int *size) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_radix_size.c */ /* Start: bn_mp_radix_smap.c */ @@ -5999,6 +6300,10 @@ int mp_radix_size (mp_int * a, int radix, int *size) const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/"; #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_radix_smap.c */ /* Start: bn_mp_rand.c */ @@ -6054,6 +6359,10 @@ mp_rand (mp_int * a, int digits) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_rand.c */ /* Start: bn_mp_read_radix.c */ @@ -6088,8 +6397,8 @@ int mp_read_radix (mp_int * a, const char *str, int radix) return MP_VAL; } - /* if the leading digit is a - * minus set the sign to negative. + /* if the leading digit is a + * minus set the sign to negative. */ if (*str == '-') { ++str; @@ -6100,23 +6409,23 @@ int mp_read_radix (mp_int * a, const char *str, int radix) /* set the integer to the default of zero */ mp_zero (a); - + /* process each digit of the string */ while (*str) { /* 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 (*str) : *str); + ch = (char) ((radix < 36) ? toupper ((int)*str) : *str); for (y = 0; y < 64; y++) { if (ch == mp_s_rmap[y]) { break; } } - /* if the char was found in the map + /* if the char was found in the map * and is less than the given radix add it - * to the number, otherwise exit the loop. + * to the number, otherwise exit the loop. */ if (y < radix) { if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) { @@ -6130,7 +6439,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix) } ++str; } - + /* set the sign only if a != 0 */ if (mp_iszero(a) != 1) { a->sign = neg; @@ -6139,6 +6448,10 @@ int mp_read_radix (mp_int * a, const char *str, int radix) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_read_radix.c */ /* Start: bn_mp_read_signed_bin.c */ @@ -6180,6 +6493,10 @@ int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_read_signed_bin.c */ /* Start: bn_mp_read_unsigned_bin.c */ @@ -6235,6 +6552,10 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_read_unsigned_bin.c */ /* Start: bn_mp_reduce.c */ @@ -6255,7 +6576,7 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* reduces x mod m, assumes 0 < x < m**2, mu is +/* reduces x mod m, assumes 0 < x < m**2, mu is * precomputed via mp_reduce_setup. * From HAC pp.604 Algorithm 14.42 */ @@ -6270,7 +6591,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu) } /* q1 = x / b**(k-1) */ - mp_rshd (&q, um - 1); + mp_rshd (&q, um - 1); /* according to HAC this optimization is ok */ if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) { @@ -6286,8 +6607,8 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu) if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) { goto CLEANUP; } -#else - { +#else + { res = MP_VAL; goto CLEANUP; } @@ -6295,7 +6616,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu) } /* q3 = q2 / b**(k+1) */ - mp_rshd (&q, um + 1); + mp_rshd (&q, um + 1); /* x = x mod b**(k+1), quick (no division) */ if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) { @@ -6327,7 +6648,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu) goto CLEANUP; } } - + CLEANUP: mp_clear (&q); @@ -6335,6 +6656,10 @@ CLEANUP: } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce.c */ /* Start: bn_mp_reduce_2k.c */ @@ -6360,35 +6685,35 @@ 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); goto top; } - + ERR: mp_clear(&q); return res; @@ -6396,6 +6721,10 @@ ERR: #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_2k.c */ /* Start: bn_mp_reduce_2k_l.c */ @@ -6416,7 +6745,7 @@ ERR: * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* 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. */ @@ -6424,33 +6753,33 @@ 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); goto top; } - + ERR: mp_clear(&q); return res; @@ -6458,6 +6787,10 @@ ERR: #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_2k_l.c */ /* Start: bn_mp_reduce_2k_setup.c */ @@ -6483,28 +6816,32 @@ int mp_reduce_2k_setup(mp_int *a, mp_digit *d) { int res, p; mp_int tmp; - + if ((res = mp_init(&tmp)) != MP_OKAY) { return res; } - + p = mp_count_bits(a); if ((res = mp_2expt(&tmp, p)) != MP_OKAY) { mp_clear(&tmp); return res; } - + if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) { mp_clear(&tmp); return res; } - + *d = tmp.dp[0]; mp_clear(&tmp); return MP_OKAY; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_2k_setup.c */ /* Start: bn_mp_reduce_2k_setup_l.c */ @@ -6530,25 +6867,29 @@ int mp_reduce_2k_setup_l(mp_int *a, mp_int *d) { int res; mp_int tmp; - + if ((res = mp_init(&tmp)) != MP_OKAY) { return res; } - + if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) { goto ERR; } - + if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) { goto ERR; } - + ERR: mp_clear(&tmp); return res; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_2k_setup_l.c */ /* Start: bn_mp_reduce_is_2k.c */ @@ -6574,7 +6915,7 @@ int mp_reduce_is_2k(mp_int *a) { int ix, iy, iw; mp_digit iz; - + if (a->used == 0) { return MP_NO; } else if (a->used == 1) { @@ -6583,7 +6924,7 @@ int mp_reduce_is_2k(mp_int *a) iy = mp_count_bits(a); iz = 1; iw = 1; - + /* Test every bit from the second digit up, must be 1 */ for (ix = DIGIT_BIT; ix < iy; ix++) { if ((a->dp[iw] & iz) == 0) { @@ -6601,6 +6942,10 @@ int mp_reduce_is_2k(mp_int *a) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_is_2k.c */ /* Start: bn_mp_reduce_is_2k_l.c */ @@ -6625,7 +6970,7 @@ int mp_reduce_is_2k(mp_int *a) int mp_reduce_is_2k_l(mp_int *a) { int ix, iy; - + if (a->used == 0) { return MP_NO; } else if (a->used == 1) { @@ -6638,13 +6983,17 @@ int mp_reduce_is_2k_l(mp_int *a) } } return (iy >= (a->used/2)) ? MP_YES : MP_NO; - + } return MP_NO; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_is_2k_l.c */ /* Start: bn_mp_reduce_setup.c */ @@ -6671,7 +7020,7 @@ int mp_reduce_is_2k_l(mp_int *a) int mp_reduce_setup (mp_int * a, mp_int * b) { int res; - + if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) { return res; } @@ -6679,6 +7028,10 @@ int mp_reduce_setup (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_reduce_setup.c */ /* Start: bn_mp_rshd.c */ @@ -6726,8 +7079,8 @@ void mp_rshd (mp_int * a, int b) /* top [offset into digits] */ top = a->dp + b; - /* this is implemented as a sliding window where - * the window is b-digits long and digits from + /* this is implemented as a sliding window where + * the window is b-digits long and digits from * the top of the window are copied to the bottom * * e.g. @@ -6745,12 +7098,16 @@ void mp_rshd (mp_int * a, int b) *bottom++ = 0; } } - + /* remove excess digits */ a->used -= b; } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_rshd.c */ /* Start: bn_mp_set.c */ @@ -6780,6 +7137,10 @@ void mp_set (mp_int * a, mp_digit b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_set.c */ /* Start: bn_mp_set_int.c */ @@ -6806,7 +7167,7 @@ int mp_set_int (mp_int * a, unsigned long b) int x, res; mp_zero (a); - + /* set four bits at a time */ for (x = 0; x < 8; x++) { /* shift the number up four bits */ @@ -6828,6 +7189,10 @@ int mp_set_int (mp_int * a, unsigned long b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_set_int.c */ /* Start: bn_mp_shrink.c */ @@ -6853,10 +7218,10 @@ int mp_shrink (mp_int * a) { mp_digit *tmp; int used = 1; - + 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) { return MP_MEM; @@ -6868,6 +7233,10 @@ int mp_shrink (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_shrink.c */ /* Start: bn_mp_signed_bin_size.c */ @@ -6895,6 +7264,10 @@ int mp_signed_bin_size (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_signed_bin_size.c */ /* Start: bn_mp_sqr.c */ @@ -6926,18 +7299,18 @@ mp_sqr (mp_int * a, mp_int * b) if (a->used >= TOOM_SQR_CUTOFF) { res = mp_toom_sqr(a, b); /* Karatsuba? */ - } else + } else #endif #ifdef BN_MP_KARATSUBA_SQR_C if (a->used >= KARATSUBA_SQR_CUTOFF) { res = mp_karatsuba_sqr (a, b); - } else + } else #endif { #ifdef BN_FAST_S_MP_SQR_C /* can we use the fast comba multiplier? */ - if ((a->used * 2 + 1) < MP_WARRAY && - a->used < + if ((a->used * 2 + 1) < MP_WARRAY && + a->used < (1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) { res = fast_s_mp_sqr (a, b); } else @@ -6953,6 +7326,10 @@ if (a->used >= KARATSUBA_SQR_CUTOFF) { } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_sqr.c */ /* Start: bn_mp_sqrmod.c */ @@ -6994,6 +7371,10 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_sqrmod.c */ /* Start: bn_mp_sqrt.c */ @@ -7016,7 +7397,7 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c) */ /* this function is less generic than mp_n_root, simpler and faster */ -int mp_sqrt(mp_int *arg, mp_int *ret) +int mp_sqrt(mp_int *arg, mp_int *ret) { int res; mp_int t1,t2; @@ -7043,7 +7424,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret) /* First approx. (not very bad for large arg) */ mp_rshd (&t1,t1.used/2); - /* t1 > 0 */ + /* t1 > 0 */ if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { goto E1; } @@ -7054,7 +7435,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret) goto E1; } /* And now t1 > sqrt(arg) */ - do { + do { if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) { goto E1; } @@ -7076,6 +7457,10 @@ E2: mp_clear(&t1); #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_sqrt.c */ /* Start: bn_mp_sub.c */ @@ -7135,6 +7520,10 @@ mp_sub (mp_int * a, mp_int * b, mp_int * c) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_sub.c */ /* Start: bn_mp_sub_d.c */ @@ -7228,6 +7617,10 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_sub_d.c */ /* Start: bn_mp_submod.c */ @@ -7270,6 +7663,10 @@ mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_submod.c */ /* Start: bn_mp_to_signed_bin.c */ @@ -7303,6 +7700,10 @@ int mp_to_signed_bin (mp_int * a, unsigned char *b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_to_signed_bin.c */ /* Start: bn_mp_to_signed_bin_n.c */ @@ -7334,6 +7735,10 @@ int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_to_signed_bin_n.c */ /* Start: bn_mp_to_unsigned_bin.c */ @@ -7382,6 +7787,10 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_to_unsigned_bin.c */ /* Start: bn_mp_to_unsigned_bin_n.c */ @@ -7413,6 +7822,10 @@ int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_to_unsigned_bin_n.c */ /* Start: bn_mp_toom_mul.c */ @@ -7433,28 +7846,28 @@ int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* 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; @@ -7470,7 +7883,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; @@ -7486,17 +7899,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; @@ -7510,7 +7923,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; } @@ -7523,11 +7936,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; @@ -7541,7 +7954,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; } @@ -7554,11 +7967,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) { @@ -7576,19 +7989,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; @@ -7660,7 +8073,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; @@ -7673,8 +8086,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; } @@ -7686,17 +8099,21 @@ 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$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_toom_mul.c */ /* Start: bn_mp_toom_sqr.c */ @@ -7923,6 +8340,10 @@ ERR: #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_toom_sqr.c */ /* Start: bn_mp_toradix.c */ @@ -7998,6 +8419,10 @@ int mp_toradix (mp_int * a, char *str, int radix) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_toradix.c */ /* Start: bn_mp_toradix_n.c */ @@ -8018,9 +8443,9 @@ int mp_toradix (mp_int * a, char *str, int radix) * Tom St Denis, tomstdenis@gmail.com, http://math.libtomcrypt.com */ -/* stores a bignum as a ASCII string in a given radix (2..64) +/* stores a bignum as a ASCII string in a given radix (2..64) * - * Stores upto maxlen-1 chars and always a NULL byte + * Stores upto maxlen-1 chars and always a NULL byte */ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) { @@ -8053,7 +8478,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) /* store the flag and mark the number as positive */ *str++ = '-'; t.sign = MP_ZPOS; - + /* subtract a char */ --maxlen; } @@ -8086,6 +8511,10 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_toradix_n.c */ /* Start: bn_mp_unsigned_bin_size.c */ @@ -8114,6 +8543,10 @@ int mp_unsigned_bin_size (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_unsigned_bin_size.c */ /* Start: bn_mp_xor.c */ @@ -8165,6 +8598,10 @@ mp_xor (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_xor.c */ /* Start: bn_mp_zero.c */ @@ -8201,6 +8638,10 @@ void mp_zero (mp_int * a) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_mp_zero.c */ /* Start: bn_prime_tab.c */ @@ -8262,6 +8703,10 @@ const mp_digit ltm_prime_tab[] = { }; #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_prime_tab.c */ /* Start: bn_reverse.c */ @@ -8301,6 +8746,10 @@ bn_reverse (unsigned char *s, int len) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_reverse.c */ /* Start: bn_s_mp_add.c */ @@ -8380,8 +8829,8 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c) *tmpc++ &= MP_MASK; } - /* now copy higher words if any, that is in A+B - * if A or B has more digits add those in + /* now copy higher words if any, that is in A+B + * if A or B has more digits add those in */ if (min != max) { for (; i < max; i++) { @@ -8410,6 +8859,10 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_s_mp_add.c */ /* Start: bn_s_mp_exptmod.c */ @@ -8469,7 +8922,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) /* init M array */ /* init first cell */ if ((err = mp_init(&M[1])) != MP_OKAY) { - return err; + return err; } /* now init the second half of the array */ @@ -8487,7 +8940,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) if ((err = mp_init (&mu)) != MP_OKAY) { goto LBL_M; } - + if (redmode == 0) { if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { goto LBL_MU; @@ -8498,22 +8951,22 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) goto LBL_MU; } redux = mp_reduce_2k_l; - } + } /* create M table * - * The M table contains powers of the base, + * The M table contains powers of the base, * e.g. M[x] = G**x mod P * - * The first half of the table is not + * The first half of the table is not * computed though accept for M[0] and M[1] */ if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { goto LBL_MU; } - /* compute the value at M[1<<(winsize-1)] by squaring - * M[1] (winsize-1) times + /* compute the value at M[1<<(winsize-1)] by squaring + * M[1] (winsize-1) times */ if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) { goto LBL_MU; @@ -8521,7 +8974,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) for (x = 0; x < (winsize - 1); x++) { /* square it */ - if ((err = mp_sqr (&M[1 << (winsize - 1)], + if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { goto LBL_MU; } @@ -8662,6 +9115,10 @@ LBL_M: } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_s_mp_exptmod.c */ /* Start: bn_s_mp_mul_digs.c */ @@ -8683,7 +9140,7 @@ LBL_M: */ /* multiplies |a| * |b| and only computes upto digs digits of result - * HAC pp. 595, Algorithm 14.12 Modified so you can control how + * HAC pp. 595, Algorithm 14.12 Modified so you can control how * many digits of output are created. */ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) @@ -8696,7 +9153,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) /* can we use the fast multiplier? */ if (((digs) < MP_WARRAY) && - MIN (a->used, b->used) < + MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { return fast_s_mp_mul_digs (a, b, c, digs); } @@ -8718,10 +9175,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) /* setup some aliases */ /* copy of the digit from a used within the nested loop */ tmpx = a->dp[ix]; - + /* an alias for the destination shifted ix places */ tmpt = t.dp + ix; - + /* an alias for the digits of b */ tmpy = b->dp; @@ -8752,6 +9209,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_s_mp_mul_digs.c */ /* Start: bn_s_mp_mul_high_digs.c */ @@ -8833,6 +9294,10 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_s_mp_mul_high_digs.c */ /* Start: bn_s_mp_sqr.c */ @@ -8886,7 +9351,7 @@ int s_mp_sqr (mp_int * a, mp_int * b) /* alias for where to store the results */ tmpt = t.dp + (2*ix + 1); - + for (iy = ix + 1; iy < pa; iy++) { /* first calculate the product */ r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]); @@ -8917,6 +9382,10 @@ int s_mp_sqr (mp_int * a, mp_int * b) } #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_s_mp_sqr.c */ /* Start: bn_s_mp_sub.c */ @@ -9006,6 +9475,10 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c) #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bn_s_mp_sub.c */ /* Start: bncore.c */ @@ -9032,16 +9505,20 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c) ------------------------------------------------------------- Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-) AMD Athlon64 /GCC v3.4.4 / 80/ 120/LTM 0.35 - + */ int KARATSUBA_MUL_CUTOFF = 80, /* Min. number of digits before Karatsuba multiplication is used. */ KARATSUBA_SQR_CUTOFF = 120, /* Min. number of digits before Karatsuba squaring is used. */ - + TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */ - TOOM_SQR_CUTOFF = 400; + TOOM_SQR_CUTOFF = 400; #endif +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */ + /* End: bncore.c */ |