libtom/libtommath

1 parent e549ccf commit 4b7111d96e806561a648a7bf9de5a371d784070b Tom St Denis committed with sjaeckel Dec 23, 2004
16 TODO
 @@ -0,0 +1,16 @@ +things for book in order of importance... + +- Fix up pseudo-code [only] for combas that are not consistent with source +- Start in chapter 3 [basics] and work up... + - re-write to prose [less abrupt] + - clean up pseudo code [spacing] + - more examples where appropriate and figures + +Goal: + - Get sync done by mid January [roughly 8-12 hours work] + - Finish ch3-6 by end of January [roughly 12-16 hours of work] + - Finish ch7-end by mid Feb [roughly 20-24 hours of work]. + +Goal isn't "first edition" but merely cleaner to read. + +
BIN bn.pdf
Binary file not shown.
2 bn.tex
 @@ -49,7 +49,7 @@ \begin{document} \frontmatter \pagestyle{empty} -\title{LibTomMath User Manual \\ v0.32} +\title{LibTomMath User Manual \\ v0.33} \author{Tom St Denis \\ tomstdenis@iahu.ca} \maketitle This text, the library and the accompanying textbook are all hereby placed in the public domain. This book has been
34 bn_fast_mp_invmod.c
 @@ -39,20 +39,20 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) /* x == modulus, y == value to invert */ if ((res = mp_copy (b, &x)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* we need y = |a| */ if ((res = mp_abs (a, &y)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ if ((res = mp_copy (&x, &u)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_copy (&y, &v)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } mp_set (&D, 1); @@ -61,57 +61,57 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) while (mp_iseven (&u) == 1) { /* 4.1 u = u/2 */ if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* 4.2 if B is odd then */ if (mp_isodd (&B) == 1) { if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* B = B/2 */ if ((res = mp_div_2 (&B, &B)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* 5. while v is even do */ while (mp_iseven (&v) == 1) { /* 5.1 v = v/2 */ if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* 5.2 if D is odd then */ if (mp_isodd (&D) == 1) { /* D = (D-x)/2 */ if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* D = D/2 */ if ((res = mp_div_2 (&D, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* 6. if u >= v then */ if (mp_cmp (&u, &v) != MP_LT) { /* u = u - v, B = B - D */ if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } else { /* v - v - u, D = D - B */ if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } @@ -125,21 +125,21 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c) /* if v != 1 then there is no inverse */ if (mp_cmp_d (&v, 1) != MP_EQ) { res = MP_VAL; - goto __ERR; + goto LBL_ERR; } /* b is now the inverse */ neg = a->sign; while (D.sign == MP_NEG) { if ((res = mp_add (&D, b, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } mp_exch (&D, c); c->sign = neg; res = MP_OKAY; -__ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); +LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL); return res; } #endif
5 bn_fast_s_mp_mul_digs.c
 @@ -50,7 +50,7 @@ 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; @@ -80,6 +80,9 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs) _W = _W >> ((mp_word)DIGIT_BIT); } + /* store final carry */ + W[ix] = _W; + /* setup dest */ olduse = c->used; c->used = digs;
5 bn_fast_s_mp_mul_high_digs.c
 @@ -42,7 +42,7 @@ 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; @@ -70,6 +70,9 @@ 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); } + + /* store final carry */ + W[ix] = _W; /* setup dest */ olduse = c->used;
2 bn_fast_s_mp_sqr.c
 @@ -60,7 +60,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;
56 bn_mp_div.c
 @@ -49,23 +49,23 @@ 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_copy(a, &ta)) != MP_OKAY) || - ((res = mp_copy(b, &tb)) != MP_OKAY) || + if (((res = mp_abs(a, &ta)) != 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 __ERR; + goto LBL_ERR; } while (n-- >= 0) { if (mp_cmp(&tb, &ta) != MP_GT) { if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) || ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) { - goto __ERR; + goto LBL_ERR; } } if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) || ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) { - goto __ERR; + goto LBL_ERR; } } @@ -74,13 +74,13 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d) n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); if (c != NULL) { mp_exch(c, &q); - c->sign = n2; + c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; } if (d != NULL) { mp_exch(d, &ta); - d->sign = n; + d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n; } -__ERR: +LBL_ERR: mp_clear_multi(&ta, &tb, &tq, &q, NULL); return res; } @@ -129,19 +129,19 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) q.used = a->used + 2; if ((res = mp_init (&t1)) != MP_OKAY) { - goto __Q; + goto LBL_Q; } if ((res = mp_init (&t2)) != MP_OKAY) { - goto __T1; + goto LBL_T1; } if ((res = mp_init_copy (&x, a)) != MP_OKAY) { - goto __T2; + goto LBL_T2; } if ((res = mp_init_copy (&y, b)) != MP_OKAY) { - goto __X; + goto LBL_X; } /* fix the sign */ @@ -153,10 +153,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) if (norm < (int)(DIGIT_BIT-1)) { norm = (DIGIT_BIT-1) - norm; if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } } else { norm = 0; @@ -168,13 +168,13 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) /* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */ if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */ - goto __Y; + goto LBL_Y; } while (mp_cmp (&x, &y) != MP_LT) { ++(q.dp[n - t]); if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } } @@ -216,7 +216,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) t1.dp[1] = y.dp[t]; t1.used = 2; if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } /* find right hand */ @@ -228,27 +228,27 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } /* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */ if (x.sign == MP_NEG) { if ((res = mp_copy (&y, &t1)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; @@ -275,11 +275,11 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d) res = MP_OKAY; -__Y:mp_clear (&y); -__X:mp_clear (&x); -__T2:mp_clear (&t2); -__T1:mp_clear (&t1); -__Q:mp_clear (&q); +LBL_Y:mp_clear (&y); +LBL_X:mp_clear (&x); +LBL_T2:mp_clear (&t2); +LBL_T1:mp_clear (&t1); +LBL_Q:mp_clear (&q); return res; }
2 bn_mp_dr_reduce.c
 @@ -20,7 +20,7 @@ * Based on algorithm from the paper * * "Generating Efficient Primes for Discrete Log Cryptosystems" - * Chae Hoon Lim, Pil Loong Lee, + * Chae Hoon Lim, Pil Joong Lee, * POSTECH Information Research Laboratories * * The modulus must be of a special format [see manual]
2 bn_mp_exptmod.c
 @@ -61,7 +61,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) return err; #else /* no invmod */ - return MP_VAL + return MP_VAL; #endif }
58 bn_mp_exptmod_fast.c
 @@ -88,11 +88,11 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) #ifdef BN_MP_MONTGOMERY_SETUP_C /* now setup montgomery */ if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) { - goto __M; + goto LBL_M; } #else err = MP_VAL; - goto __M; + goto LBL_M; #endif /* automatically pick the comba one if available (saves quite a few calls/ifs) */ @@ -108,7 +108,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) redux = mp_montgomery_reduce; #else err = MP_VAL; - goto __M; + goto LBL_M; #endif } } else if (redmode == 1) { @@ -118,24 +118,24 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) redux = mp_dr_reduce; #else err = MP_VAL; - goto __M; + goto LBL_M; #endif } else { #if defined(BN_MP_REDUCE_2K_SETUP_C) && defined(BN_MP_REDUCE_2K_C) /* setup DR reduction for moduli of the form 2**k - b */ if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) { - goto __M; + goto LBL_M; } redux = mp_reduce_2k; #else err = MP_VAL; - goto __M; + goto LBL_M; #endif } /* setup result */ if ((err = mp_init (&res)) != MP_OKAY) { - goto __M; + goto LBL_M; } /* create M table @@ -149,45 +149,45 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) #ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C /* now we need R mod m */ if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } #else err = MP_VAL; - goto __RES; + goto LBL_RES; #endif /* now set M[1] to G * R mod m */ if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } else { mp_set(&res, 1); if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } /* 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 __RES; + goto LBL_RES; } for (x = 0; x < (winsize - 1); x++) { if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } /* create upper table */ for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&M[x], P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } @@ -227,10 +227,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) /* if the bit is zero and mode == 1 then we square */ if (mode == 1 && y == 0) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&res, P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } continue; } @@ -244,19 +244,19 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) /* square first */ for (x = 0; x < winsize; x++) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&res, P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } /* then multiply */ if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&res, P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } /* empty window and reset */ @@ -271,21 +271,21 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) /* square then multiply if the bit is set */ for (x = 0; x < bitcpy; x++) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&res, P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } /* get next bit of the window */ bitbuf <<= 1; if ((bitbuf & (1 << winsize)) != 0) { /* then multiply */ if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = redux (&res, P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } } @@ -299,15 +299,15 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) * of R. */ if ((err = redux(&res, P, mp)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } /* swap res with Y */ mp_exch (&res, Y); err = MP_OKAY; -__RES:mp_clear (&res); -__M: +LBL_RES:mp_clear (&res); +LBL_M: mp_clear(&M[1]); for (x = 1<<(winsize-1); x < (1 << winsize); x++) { mp_clear (&M[x]);
20 bn_mp_gcd.c
 @@ -43,7 +43,7 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) } if ((res = mp_init_copy (&v, b)) != MP_OKAY) { - goto __U; + goto LBL_U; } /* must be positive for the remainder of the algorithm */ @@ -57,24 +57,24 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) if (k > 0) { /* divide the power of two out */ if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) { - goto __V; + goto LBL_V; } if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) { - goto __V; + goto LBL_V; } } /* divide any remaining factors of two out */ if (u_lsb != k) { if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { - goto __V; + goto LBL_V; } } if (v_lsb != k) { if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { - goto __V; + goto LBL_V; } } @@ -87,23 +87,23 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c) /* subtract smallest from largest */ if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) { - goto __V; + goto LBL_V; } /* Divide out all factors of two */ if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) { - goto __V; + goto LBL_V; } } /* multiply by 2**k which we divided out at the beginning */ if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) { - goto __V; + goto LBL_V; } c->sign = MP_ZPOS; res = MP_OKAY; -__V:mp_clear (&u); -__U:mp_clear (&v); +LBL_V:mp_clear (&u); +LBL_U:mp_clear (&v); return res; } #endif
50 bn_mp_invmod_slow.c
 @@ -34,24 +34,24 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) /* x = a, y = b */ if ((res = mp_copy (a, &x)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_copy (b, &y)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* 2. [modified] if x,y are both even then return an error! */ if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) { res = MP_VAL; - goto __ERR; + goto LBL_ERR; } /* 3. u=x, v=y, A=1, B=0, C=0,D=1 */ if ((res = mp_copy (&x, &u)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_copy (&y, &v)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } mp_set (&A, 1); mp_set (&D, 1); @@ -61,78 +61,78 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) while (mp_iseven (&u) == 1) { /* 4.1 u = u/2 */ if ((res = mp_div_2 (&u, &u)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* 4.2 if A or B is odd then */ if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) { /* A = (A+y)/2, B = (B-x)/2 */ if ((res = mp_add (&A, &y, &A)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* A = A/2, B = B/2 */ if ((res = mp_div_2 (&A, &A)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_div_2 (&B, &B)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* 5. while v is even do */ while (mp_iseven (&v) == 1) { /* 5.1 v = v/2 */ if ((res = mp_div_2 (&v, &v)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* 5.2 if C or D is odd then */ if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) { /* C = (C+y)/2, D = (D-x)/2 */ if ((res = mp_add (&C, &y, &C)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* C = C/2, D = D/2 */ if ((res = mp_div_2 (&C, &C)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_div_2 (&D, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* 6. if u >= v then */ if (mp_cmp (&u, &v) != MP_LT) { /* u = u - v, A = A - C, B = B - D */ if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } else { /* v - v - u, C = C - A, D = D - B */ if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } @@ -145,27 +145,27 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c) /* if v != 1 then there is no inverse */ if (mp_cmp_d (&v, 1) != MP_EQ) { res = MP_VAL; - goto __ERR; + goto LBL_ERR; } /* if its too low */ while (mp_cmp_d(&C, 0) == MP_LT) { if ((res = mp_add(&C, b, &C)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* too big */ while (mp_cmp_mag(&C, b) != MP_LT) { if ((res = mp_sub(&C, b, &C)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } } /* C is now the inverse */ mp_exch (&C, c); res = MP_OKAY; -__ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); +LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL); return res; } #endif
12 bn_mp_jacobi.c
 @@ -50,13 +50,13 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c) } if ((res = mp_init (&p1)) != MP_OKAY) { - goto __A1; + goto LBL_A1; } /* divide out larger power of two */ k = mp_cnt_lsb(&a1); if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) { - goto __P1; + goto LBL_P1; } /* step 4. if e is even set s=1 */ @@ -84,18 +84,18 @@ int mp_jacobi (mp_int * a, mp_int * p, int *c) } else { /* n1 = n mod a1 */ if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) { - goto __P1; + goto LBL_P1; } if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) { - goto __P1; + goto LBL_P1; } *c = s * r; } /* done */ res = MP_OKAY; -__P1:mp_clear (&p1); -__A1:mp_clear (&a1); +LBL_P1:mp_clear (&p1); +LBL_A1:mp_clear (&a1); return res; } #endif
8 bn_mp_lcm.c
 @@ -28,28 +28,28 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c) /* t1 = get the GCD of the two inputs */ if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) { - goto __T; + goto LBL_T; } /* divide the smallest by the GCD */ if (mp_cmp_mag(a, b) == MP_LT) { /* store quotient in t2 such that t2 * b is the LCM */ if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) { - goto __T; + goto LBL_T; } res = mp_mul(b, &t2, c); } else { /* store quotient in t2 such that t2 * a is the LCM */ if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) { - goto __T; + goto LBL_T; } res = mp_mul(a, &t2, c); } /* fix the sign to positive */ c->sign = MP_ZPOS; -__T: +LBL_T: mp_clear_multi (&t1, &t2, NULL); return res; }
2 bn_mp_mod_2d.c
 @@ -28,7 +28,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c) } /* if the modulus is larger than the value than return */ - if (b > (int) (a->used * DIGIT_BIT)) { + if (b >= (int) (a->used * DIGIT_BIT)) { res = mp_copy (a, c); return res; }
28 bn_mp_n_root.c
 @@ -40,11 +40,11 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c) } if ((res = mp_init (&t2)) != MP_OKAY) { - goto __T1; + goto LBL_T1; } if ((res = mp_init (&t3)) != MP_OKAY) { - goto __T2; + goto LBL_T2; } /* if a is negative fudge the sign but keep track */ @@ -57,52 +57,52 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c) do { /* t1 = t2 */ if ((res = mp_copy (&t2, &t1)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ /* t3 = t1**(b-1) */ if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } /* numerator */ /* t2 = t1**b */ if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } /* t2 = t1**b - a */ if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } /* denominator */ /* t3 = t1**(b-1) * b */ if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } /* t3 = (t1**b - a)/(b * t1**(b-1)) */ if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } } while (mp_cmp (&t1, &t2) != MP_EQ); /* result can be off by a few so check */ for (;;) { if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } if (mp_cmp (&t2, a) == MP_GT) { if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) { - goto __T3; + goto LBL_T3; } } else { break; @@ -120,9 +120,9 @@ int mp_n_root (mp_int * a, mp_digit b, mp_int * c) res = MP_OKAY; -__T3:mp_clear (&t3); -__T2:mp_clear (&t2); -__T1:mp_clear (&t1); +LBL_T3:mp_clear (&t3); +LBL_T2:mp_clear (&t2); +LBL_T1:mp_clear (&t1); return res; } #endif
4 bn_mp_prime_fermat.c
 @@ -43,7 +43,7 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result) /* compute t = b**a mod a */ if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) { - goto __T; + goto LBL_T; } /* is it equal to b? */ @@ -52,7 +52,7 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result) } err = MP_OKAY; -__T:mp_clear (&t); +LBL_T:mp_clear (&t); return err; } #endif
4 bn_mp_prime_is_divisible.c
 @@ -29,8 +29,8 @@ int mp_prime_is_divisible (mp_int * a, int *result) *result = MP_NO; for (ix = 0; ix < PRIME_SIZE; ix++) { - /* what is a mod __prime_tab[ix] */ - if ((err = mp_mod_d (a, __prime_tab[ix], &res)) != MP_OKAY) { + /* what is a mod LBL_prime_tab[ix] */ + if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) { return err; }
10 bn_mp_prime_is_prime.c
 @@ -37,7 +37,7 @@ int mp_prime_is_prime (mp_int * a, int t, int *result) /* is the input equal to one of the primes in the table? */ for (ix = 0; ix < PRIME_SIZE; ix++) { - if (mp_cmp_d(a, __prime_tab[ix]) == MP_EQ) { + if (mp_cmp_d(a, ltm_prime_tab[ix]) == MP_EQ) { *result = 1; return MP_OKAY; } @@ -60,20 +60,20 @@ int mp_prime_is_prime (mp_int * a, int t, int *result) for (ix = 0; ix < t; ix++) { /* set the prime */ - mp_set (&b, __prime_tab[ix]); + mp_set (&b, ltm_prime_tab[ix]); if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) { - goto __B; + goto LBL_B; } if (res == MP_NO) { - goto __B; + goto LBL_B; } } /* passed the test */ *result = MP_YES; -__B:mp_clear (&b); +LBL_B:mp_clear (&b); return err; } #endif
22 bn_mp_prime_miller_rabin.c
 @@ -40,12 +40,12 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) return err; } if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) { - goto __N1; + goto LBL_N1; } /* set 2**s * r = n1 */ if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) { - goto __N1; + goto LBL_N1; } /* count the number of least significant bits @@ -55,15 +55,15 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) /* now divide n - 1 by 2**s */ if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) { - goto __R; + goto LBL_R; } /* compute y = b**r mod a */ if ((err = mp_init (&y)) != MP_OKAY) { - goto __R; + goto LBL_R; } if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } /* if y != 1 and y != n1 do */ @@ -72,28 +72,28 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) /* while j <= s-1 and y != n1 */ while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) { if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } /* if y == 1 then composite */ if (mp_cmp_d (&y, 1) == MP_EQ) { - goto __Y; + goto LBL_Y; } ++j; } /* if y != n1 then composite */ if (mp_cmp (&y, &n1) != MP_EQ) { - goto __Y; + goto LBL_Y; } } /* probably prime now */ *result = MP_YES; -__Y:mp_clear (&y); -__R:mp_clear (&r); -__N1:mp_clear (&n1); +LBL_Y:mp_clear (&y); +LBL_R:mp_clear (&r); +LBL_N1:mp_clear (&n1); return err; } #endif
26 bn_mp_prime_next_prime.c
 @@ -35,28 +35,28 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) a->sign = MP_ZPOS; /* simple algo if a is less than the largest prime in the table */ - if (mp_cmp_d(a, __prime_tab[PRIME_SIZE-1]) == MP_LT) { + if (mp_cmp_d(a, ltm_prime_tab[PRIME_SIZE-1]) == MP_LT) { /* find which prime it is bigger than */ for (x = PRIME_SIZE - 2; x >= 0; x--) { - if (mp_cmp_d(a, __prime_tab[x]) != MP_LT) { + if (mp_cmp_d(a, ltm_prime_tab[x]) != MP_LT) { if (bbs_style == 1) { /* ok we found a prime smaller or * equal [so the next is larger] * * however, the prime must be * congruent to 3 mod 4 */ - if ((__prime_tab[x + 1] & 3) != 3) { + if ((ltm_prime_tab[x + 1] & 3) != 3) { /* scan upwards for a prime congruent to 3 mod 4 */ for (y = x + 1; y < PRIME_SIZE; y++) { - if ((__prime_tab[y] & 3) == 3) { - mp_set(a, __prime_tab[y]); + if ((ltm_prime_tab[y] & 3) == 3) { + mp_set(a, ltm_prime_tab[y]); return MP_OKAY; } } } } else { - mp_set(a, __prime_tab[x + 1]); + mp_set(a, ltm_prime_tab[x + 1]); return MP_OKAY; } } @@ -94,7 +94,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) /* generate the restable */ for (x = 1; x < PRIME_SIZE; x++) { - if ((err = mp_mod_d(a, __prime_tab[x], res_tab + x)) != MP_OKAY) { + if ((err = mp_mod_d(a, ltm_prime_tab[x], res_tab + x)) != MP_OKAY) { return err; } } @@ -120,8 +120,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) res_tab[x] += kstep; /* subtract the modulus [instead of using division] */ - if (res_tab[x] >= __prime_tab[x]) { - res_tab[x] -= __prime_tab[x]; + if (res_tab[x] >= ltm_prime_tab[x]) { + res_tab[x] -= ltm_prime_tab[x]; } /* set flag if zero */ @@ -133,7 +133,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) /* add the step */ if ((err = mp_add_d(a, step, a)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } /* if didn't pass sieve and step == MAX then skip test */ @@ -143,9 +143,9 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) /* is this prime? */ for (x = 0; x < t; x++) { - mp_set(&b, __prime_tab[t]); + mp_set(&b, ltm_prime_tab[t]); if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) { - goto __ERR; + goto LBL_ERR; } if (res == MP_NO) { break; @@ -158,7 +158,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) } err = MP_OKAY; -__ERR: +LBL_ERR: mp_clear(&b); return err; }
6 bn_mp_prime_random_ex.c
 @@ -47,7 +47,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback } /* calc the byte size */ - bsize = (size>>3)+(size&7?1:0); + bsize = (size>>3) + ((size&7)?1:0); /* we need a buffer of bsize bytes */ tmp = OPT_CAST(unsigned char) XMALLOC(bsize); @@ -56,7 +56,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback } /* calc the maskAND value for the MSbyte*/ - maskAND = 0xFF >> (8 - (size & 7)); + maskAND = ((size&7) == 0) ? 0xFF : (0xFF >> (8 - (size & 7))); /* calc the maskOR_msb */ maskOR_msb = 0; @@ -65,7 +65,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback maskOR_msb |= 1 << ((size - 2) & 7); } else if (flags & LTM_PRIME_2MSB_OFF) { maskAND &= ~(1 << ((size - 2) & 7)); - } + } /* get the maskOR_lsb */ maskOR_lsb = 0;
2 bn_prime_tab.c
 @@ -14,7 +14,7 @@ * * Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org */ -const mp_digit __prime_tab[] = { +const mp_digit ltm_prime_tab[] = { 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013, 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035, 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059,
44 bn_s_mp_exptmod.c
 @@ -70,10 +70,10 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) /* create mu, used for Barrett reduction */ if ((err = mp_init (&mu)) != MP_OKAY) { - goto __M; + goto LBL_M; } if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } /* create M table @@ -85,23 +85,23 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) * computed though accept for M[0] and M[1] */ if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) { - goto __MU; + goto LBL_MU; } /* 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 __MU; + goto LBL_MU; } for (x = 0; x < (winsize - 1); x++) { if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) { - goto __MU; + goto LBL_MU; } if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } } @@ -110,16 +110,16 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) */ for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) { if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) { - goto __MU; + goto LBL_MU; } if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } } /* setup result */ if ((err = mp_init (&res)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } mp_set (&res, 1); @@ -159,10 +159,10 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) /* if the bit is zero and mode == 1 then we square */ if (mode == 1 && y == 0) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } continue; } @@ -176,19 +176,19 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) /* square first */ for (x = 0; x < winsize; x++) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } /* then multiply */ if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } /* empty window and reset */ @@ -203,30 +203,30 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) /* square then multiply if the bit is set */ for (x = 0; x < bitcpy; x++) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } bitbuf <<= 1; if ((bitbuf & (1 << winsize)) != 0) { /* then multiply */ if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) { - goto __RES; + goto LBL_RES; } } } } mp_exch (&res, Y); err = MP_OKAY; -__RES:mp_clear (&res); -__MU:mp_clear (&mu); -__M: +LBL_RES:mp_clear (&res); +LBL_MU:mp_clear (&mu); +LBL_M: mp_clear(&M[1]); for (x = 1<<(winsize-1); x < (1 << winsize); x++) { mp_clear (&M[x]);
25 callgraph.txt
 @@ -245,6 +245,7 @@ BN_MP_SQRT_C | | +--->BN_MP_INIT_MULTI_C | | | +--->BN_MP_CLEAR_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -298,6 +299,7 @@ BN_MP_SQRT_C | | +--->BN_MP_CLEAR_C | +--->BN_MP_SET_C | +--->BN_MP_COUNT_BITS_C +| +--->BN_MP_ABS_C | +--->BN_MP_MUL_2D_C | | +--->BN_MP_GROW_C | | +--->BN_MP_LSHD_C @@ -404,6 +406,7 @@ BN_MP_IS_SQUARE_C | | | +--->BN_MP_CLEAR_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -700,6 +703,7 @@ BN_MP_IS_SQUARE_C | | | +--->BN_MP_INIT_MULTI_C | | | | +--->BN_MP_CLEAR_C | | | +--->BN_MP_COUNT_BITS_C +| | | +--->BN_MP_ABS_C | | | +--->BN_MP_MUL_2D_C | | | | +--->BN_MP_GROW_C | | | | +--->BN_MP_LSHD_C @@ -753,6 +757,7 @@ BN_MP_IS_SQUARE_C | | | +--->BN_MP_CLEAR_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -2618,6 +2623,7 @@ BN_MP_SUBMOD_C | | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -2838,6 +2844,7 @@ BN_MP_SQRMOD_C | | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -3313,6 +3320,7 @@ BN_MP_N_ROOT_C | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_CLEAR_C | +--->BN_MP_COUNT_BITS_C +| +--->BN_MP_ABS_C | +--->BN_MP_MUL_2D_C | | +--->BN_MP_GROW_C | | +--->BN_MP_LSHD_C @@ -4322,6 +4330,7 @@ BN_MP_PRIME_RANDOM_EX_C | | | | | +--->BN_MP_ZERO_C | | | | | +--->BN_MP_INIT_MULTI_C | | | | | +--->BN_MP_COUNT_BITS_C +| | | | | +--->BN_MP_ABS_C | | | | | +--->BN_MP_MUL_2D_C | | | | | | +--->BN_MP_GROW_C | | | | | | +--->BN_MP_LSHD_C @@ -4548,6 +4557,7 @@ BN_MP_MOD_C | | +--->BN_MP_CLEAR_C | +--->BN_MP_SET_C | +--->BN_MP_COUNT_BITS_C +| +--->BN_MP_ABS_C | +--->BN_MP_MUL_2D_C | | +--->BN_MP_GROW_C | | +--->BN_MP_LSHD_C @@ -5600,6 +5610,7 @@ BN_MP_PRIME_IS_PRIME_C | | | | +--->BN_MP_ZERO_C | | | | +--->BN_MP_INIT_MULTI_C | | | | +--->BN_MP_COUNT_BITS_C +| | | | +--->BN_MP_ABS_C | | | | +--->BN_MP_MUL_2D_C | | | | | +--->BN_MP_GROW_C | | | | | +--->BN_MP_LSHD_C @@ -5809,6 +5820,7 @@ BN_MP_EXPTMOD_FAST_C | | | +--->BN_MP_ZERO_C | | | +--->BN_MP_INIT_MULTI_C | | | +--->BN_MP_SET_C +| | | +--->BN_MP_ABS_C | | | +--->BN_MP_MUL_2D_C | | | | +--->BN_MP_GROW_C | | | | +--->BN_MP_LSHD_C @@ -5865,6 +5877,7 @@ BN_MP_EXPTMOD_FAST_C | | | +--->BN_MP_GROW_C | | +--->BN_MP_ZERO_C | | +--->BN_MP_INIT_MULTI_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -6284,6 +6297,7 @@ BN_MP_MULMOD_C | | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -7339,6 +7353,7 @@ BN_MP_PRIME_NEXT_PRIME_C | | | | +--->BN_MP_ZERO_C | | | | +--->BN_MP_INIT_MULTI_C | | | | +--->BN_MP_COUNT_BITS_C +| | | | +--->BN_MP_ABS_C | | | | +--->BN_MP_MUL_2D_C | | | | | +--->BN_MP_GROW_C | | | | | +--->BN_MP_LSHD_C @@ -7465,6 +7480,7 @@ BN_MP_LCM_C | +--->BN_MP_ZERO_C | +--->BN_MP_SET_C | +--->BN_MP_COUNT_BITS_C +| +--->BN_MP_ABS_C | +--->BN_MP_MUL_2D_C | | +--->BN_MP_GROW_C | | +--->BN_MP_LSHD_C @@ -7928,6 +7944,7 @@ BN_S_MP_EXPTMOD_C | | +--->BN_MP_ZERO_C | | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_SET_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -7974,6 +7991,7 @@ BN_S_MP_EXPTMOD_C | | +--->BN_MP_ZERO_C | | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_SET_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -8372,6 +8390,7 @@ BN_MP_DIV_C | +--->BN_MP_CLEAR_C +--->BN_MP_SET_C +--->BN_MP_COUNT_BITS_C ++--->BN_MP_ABS_C +--->BN_MP_MUL_2D_C | +--->BN_MP_GROW_C | +--->BN_MP_LSHD_C @@ -8465,6 +8484,7 @@ BN_MP_ADDMOD_C | | +--->BN_MP_INIT_MULTI_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -8551,6 +8571,7 @@ BN_MP_REDUCE_C | | | +--->BN_MP_CLEAR_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -8766,6 +8787,7 @@ BN_MP_JACOBI_C | | | +--->BN_MP_CLEAR_C | | +--->BN_MP_SET_C | | +--->BN_MP_COUNT_BITS_C +| | +--->BN_MP_ABS_C | | +--->BN_MP_MUL_2D_C | | | +--->BN_MP_GROW_C | | | +--->BN_MP_LSHD_C @@ -8912,6 +8934,7 @@ BN_MP_EXTEUCLID_C | +--->BN_MP_CMP_MAG_C | +--->BN_MP_ZERO_C | +--->BN_MP_COUNT_BITS_C +| +--->BN_MP_ABS_C | +--->BN_MP_MUL_2D_C | | +--->BN_MP_GROW_C | | +--->BN_MP_LSHD_C @@ -9078,6 +9101,7 @@ BN_MP_REDUCE_SETUP_C | | +--->BN_MP_CLEAR_C | +--->BN_MP_SET_C | +--->BN_MP_COUNT_BITS_C +| +--->BN_MP_ABS_C | +--->BN_MP_MUL_2D_C | | +--->BN_MP_GROW_C | | +--->BN_MP_LSHD_C @@ -10118,6 +10142,7 @@ BN_MP_PRIME_MILLER_RABIN_C | | | +--->BN_MP_INIT_MULTI_C | | | +--->BN_MP_SET_C | | | +--->BN_MP_COUNT_BITS_C +| | | +--->BN_MP_ABS_C | | | +--->BN_MP_MUL_2D_C | | | | +--->BN_MP_GROW_C | | | | +--->BN_MP_LSHD_C
9 changes.txt
 @@ -1,3 +1,12 @@ +December 23rd, 2004 +v0.33 -- Fixed "small" variant for mp_div() which would munge with negative dividends... + -- Fixed bug in mp_prime_random_ex() which would set the most significant byte to zero when + no special flags were set + -- Fixed overflow [minor] bug in fast_s_mp_sqr() + -- Made the makefiles easier to configure the group/user that ltm will install as + -- Fixed "final carry" bug in comba multipliers. (Volkan Ceylan) + -- Matt Johnston pointed out a missing semi-colon in mp_exptmod + October 29th, 2004 v0.32 -- Added "makefile.shared" for shared object support -- Added more to the build options/configs in the manual
6 demo/demo.c
 @@ -11,9 +11,9 @@ void ndraw(mp_int *a, char *name) { - char buf[4096]; + char buf[16000]; printf("%s: ", name); - mp_toradix(a, buf, 64); + mp_toradix(a, buf, 10); printf("%s\n", buf); } @@ -395,7 +395,7 @@ draw(&a);draw(&b);draw(&c);draw(&d); mp_div(&a, &b, &e, &f); if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) { - printf("div %lu failure!\n", div_n); + printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e), mp_cmp(&d, &f)); draw(&a);draw(&b);draw(&c);draw(&d); draw(&e); draw(&f); return 0; }
10 demo/timing.c
 @@ -38,14 +38,13 @@ int lbit(void) } } -#if defined(__i386__) || defined(_M_IX86) || defined(_M_AMD64) /* RDTSC from Scott Duplichan */ static ulong64 TIMFUNC (void) { #if defined __GNUC__ - #ifdef __i386__ - ulong64 a; - __asm__ __volatile__ ("rdtsc ":"=A" (a)); + #if defined(__i386__) || defined(__x86_64__) + unsigned long long a; + __asm__ __volatile__ ("rdtsc\nmovl %%eax,%0\nmovl %%edx,4+%0\n"::"m"(a):"%eax","%edx"); return a; #else /* gcc-IA64 version */ unsigned long result; @@ -69,9 +68,6 @@ static ulong64 TIMFUNC (void) #error need rdtsc function for this build #endif } -#else -#define TIMFUNC clock -#endif #define DO(x) x; x; //#define DO4(x) DO2(x); DO2(x);
18 etc/mersenne.c
 @@ -18,15 +18,15 @@ is_mersenne (long s, int *pp) } if ((res = mp_init (&u)) != MP_OKAY) { - goto __N; + goto LBL_N; } /* n = 2^s - 1 */ if ((res = mp_2expt(&n, s)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } if ((res = mp_sub_d (&n, 1, &n)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } /* set u=4 */ @@ -36,22 +36,22 @@ is_mersenne (long s, int *pp) for (k = 1; k <= s - 2; k++) { /* u = u^2 - 2 mod n */ if ((res = mp_sqr (&u, &u)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } if ((res = mp_sub_d (&u, 2, &u)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } /* make sure u is positive */ while (u.sign == MP_NEG) { if ((res = mp_add (&u, &n, &u)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } } /* reduce */ if ((res = mp_reduce_2k (&u, &n, 1)) != MP_OKAY) { - goto __MU; + goto LBL_MU; } } @@ -62,8 +62,8 @@ is_mersenne (long s, int *pp) } res = MP_OKAY; -__MU:mp_clear (&u); -__N:mp_clear (&n); +LBL_MU:mp_clear (&u); +LBL_N:mp_clear (&n); return res; }
54 etc/pprime.c
 @@ -189,42 +189,42 @@ pprime (int k, int li, mp_int * p, mp_int * q) } if ((res = mp_init (&v)) != MP_OKAY) { - goto __C; + goto LBL_C; } /* product of first 50 primes */ if ((res = mp_read_radix (&v, "19078266889580195013601891820992757757219839668357012055907516904309700014933909014729740190", 10)) != MP_OKAY) { - goto __V; + goto LBL_V; } if ((res = mp_init (&a)) != MP_OKAY) { - goto __V; + goto LBL_V; } /* set the prime */ mp_set (&a, prime_digit ()); if ((res = mp_init (&b)) != MP_OKAY) { - goto __A; + goto LBL_A; } if ((res = mp_init (&n)) != MP_OKAY) { - goto __B; + goto LBL_B; } if ((res = mp_init (&x)) != MP_OKAY) { - goto __N; + goto LBL_N; } if ((res = mp_init (&y)) != MP_OKAY) { - goto __X; + goto LBL_X; } if ((res = mp_init (&z)) != MP_OKAY) { - goto __Y; + goto LBL_Y; } /* now loop making the single digit */ @@ -236,25 +236,25 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* now compute z = a * b * 2 */ if ((res = mp_mul (&a, &b, &z)) != MP_OKAY) { /* z = a * b */ - goto __Z; + goto LBL_Z; } if ((res = mp_copy (&z, &c)) != MP_OKAY) { /* c = a * b */ - goto __Z; + goto LBL_Z; } if ((res = mp_mul_2 (&z, &z)) != MP_OKAY) { /* z = 2 * a * b */ - goto __Z; + goto LBL_Z; } /* n = z + 1 */ if ((res = mp_add_d (&z, 1, &n)) != MP_OKAY) { /* n = z + 1 */ - goto __Z; + goto LBL_Z; } /* check (n, v) == 1 */ if ((res = mp_gcd (&n, &v, &y)) != MP_OKAY) { /* y = (n, v) */ - goto __Z; + goto LBL_Z; } if (mp_cmp_d (&y, 1) != MP_EQ) @@ -266,7 +266,7 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* compute x^a mod n */ if ((res = mp_exptmod (&x, &a, &n, &y)) != MP_OKAY) { /* y = x^a mod n */ - goto __Z; + goto LBL_Z; } /* if y == 1 loop */ @@ -275,15 +275,15 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* now x^2a mod n */ if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2a mod n */ - goto __Z; + goto LBL_Z; } if (mp_cmp_d (&y, 1) == MP_EQ) continue; /* compute x^b mod n */ if ((res = mp_exptmod (&x, &b, &n, &y)) != MP_OKAY) { /* y = x^b mod n */ - goto __Z; + goto LBL_Z; } /* if y == 1 loop */ @@ -292,15 +292,15 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* now x^2b mod n */ if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2b mod n */ - goto __Z; + goto LBL_Z; } if (mp_cmp_d (&y, 1) == MP_EQ) continue; /* compute x^c mod n == x^ab mod n */ if ((res = mp_exptmod (&x, &c, &n, &y)) != MP_OKAY) { /* y = x^ab mod n */ - goto __Z; + goto LBL_Z; } /* if y == 1 loop */ @@ -309,7 +309,7 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* now compute (x^c mod n)^2 */ if ((res = mp_sqrmod (&y, &n, &y)) != MP_OKAY) { /* y = x^2ab mod n */ - goto __Z; + goto LBL_Z; } /* y should be 1 */ @@ -346,14 +346,14 @@ pprime (int k, int li, mp_int * p, mp_int * q) mp_exch (&n, p); res = MP_OKAY; -__Z:mp_clear (&z); -__Y:mp_clear (&y); -__X:mp_clear (&x); -__N:mp_clear (&n); -__B:mp_clear (&b); -__A:mp_clear (&a); -__V:mp_clear (&v); -__C:mp_clear (&c); +LBL_Z:mp_clear (&z); +LBL_Y:mp_clear (&y); +LBL_X:mp_clear (&x); +LBL_N:mp_clear (&n); +LBL_B:mp_clear (&b); +LBL_A:mp_clear (&a); +LBL_V:mp_clear (&v); +LBL_C:mp_clear (&c); return res; }
6 etc/tune.c
 @@ -14,9 +14,9 @@ #ifndef X86_TIMER /* generic ISO C timer */ -ulong64 __T; -void t_start(void) { __T = clock(); } -ulong64 t_read(void) { return clock() - __T; } +ulong64 LBL_T; +void t_start(void) { LBL_T = clock(); } +ulong64 t_read(void) { return clock() - LBL_T; } #else extern void t_start(void);