diff --git a/TODO b/TODO new file mode 100644 index 0000000..deffba1 --- /dev/null +++ b/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. + + diff --git a/bn.pdf b/bn.pdf index fbd5b2a..9b873e1 100644 Binary files a/bn.pdf and b/bn.pdf differ diff --git a/bn.tex b/bn.tex index 74a4f01..962d6ea 100644 --- a/bn.tex +++ b/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 diff --git a/bn_fast_mp_invmod.c b/bn_fast_mp_invmod.c index 492a3f1..b5b9f10 100644 --- a/bn_fast_mp_invmod.c +++ b/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,17 +61,17 @@ top: 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; } } @@ -79,18 +79,18 @@ top: 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; } } @@ -98,20 +98,20 @@ top: 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 @@ top: /* 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 diff --git a/bn_fast_s_mp_mul_digs.c b/bn_fast_s_mp_mul_digs.c index 92b50bb..e1ff5f3 100644 --- a/bn_fast_s_mp_mul_digs.c +++ b/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; diff --git a/bn_fast_s_mp_mul_high_digs.c b/bn_fast_s_mp_mul_high_digs.c index 9e0cf55..064a9dd 100644 --- a/bn_fast_s_mp_mul_high_digs.c +++ b/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; diff --git a/bn_fast_s_mp_sqr.c b/bn_fast_s_mp_sqr.c index 9f6962d..d6014ab 100644 --- a/bn_fast_s_mp_sqr.c +++ b/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; diff --git a/bn_mp_div.c b/bn_mp_div.c index 39d921a..6b2b8f0 100644 --- a/bn_mp_div.c +++ b/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; } diff --git a/bn_mp_dr_reduce.c b/bn_mp_dr_reduce.c index 308b80a..9bb7ad7 100644 --- a/bn_mp_dr_reduce.c +++ b/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] diff --git a/bn_mp_exptmod.c b/bn_mp_exptmod.c index da88fec..7309170 100644 --- a/bn_mp_exptmod.c +++ b/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 } diff --git a/bn_mp_exptmod_fast.c b/bn_mp_exptmod_fast.c index 4351e60..255e9d9 100644 --- a/bn_mp_exptmod_fast.c +++ b/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,10 +271,10 @@ 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 */ @@ -282,10 +282,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) 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]); diff --git a/bn_mp_gcd.c b/bn_mp_gcd.c index 1cd21fc..6265df1 100644 --- a/bn_mp_gcd.c +++ b/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 diff --git a/bn_mp_invmod_slow.c b/bn_mp_invmod_slow.c index 8ecb009..c1884c0 100644 --- a/bn_mp_invmod_slow.c +++ b/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,24 +61,24 @@ top: 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; } } @@ -86,24 +86,24 @@ top: 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; } } @@ -111,28 +111,28 @@ top: 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 @@ top: /* 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 diff --git a/bn_mp_jacobi.c b/bn_mp_jacobi.c index 1c69cfd..74cbbf3 100644 --- a/bn_mp_jacobi.c +++ b/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 diff --git a/bn_mp_lcm.c b/bn_mp_lcm.c index 340d757..8e3a759 100644 --- a/bn_mp_lcm.c +++ b/bn_mp_lcm.c @@ -28,20 +28,20 @@ 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); } @@ -49,7 +49,7 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c) /* fix the sign to positive */ c->sign = MP_ZPOS; -__T: +LBL_T: mp_clear_multi (&t1, &t2, NULL); return res; } diff --git a/bn_mp_mod_2d.c b/bn_mp_mod_2d.c index f81a0d4..589e4ba 100644 --- a/bn_mp_mod_2d.c +++ b/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; } diff --git a/bn_mp_n_root.c b/bn_mp_n_root.c index 9489903..7b11aa2 100644 --- a/bn_mp_n_root.c +++ b/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 diff --git a/bn_mp_prime_fermat.c b/bn_mp_prime_fermat.c index fe17aaa..fd74dbe 100644 --- a/bn_mp_prime_fermat.c +++ b/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 diff --git a/bn_mp_prime_is_divisible.c b/bn_mp_prime_is_divisible.c index 22ec1ae..f85fe7c 100644 --- a/bn_mp_prime_is_divisible.c +++ b/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; } diff --git a/bn_mp_prime_is_prime.c b/bn_mp_prime_is_prime.c index c2354d2..188053a 100644 --- a/bn_mp_prime_is_prime.c +++ b/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 diff --git a/bn_mp_prime_miller_rabin.c b/bn_mp_prime_miller_rabin.c index 22dec2f..758a2c3 100644 --- a/bn_mp_prime_miller_rabin.c +++ b/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,12 +72,12 @@ 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; @@ -85,15 +85,15 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) /* 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 diff --git a/bn_mp_prime_next_prime.c b/bn_mp_prime_next_prime.c index c478ce5..24f93c4 100644 --- a/bn_mp_prime_next_prime.c +++ b/bn_mp_prime_next_prime.c @@ -35,10 +35,10 @@ 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] @@ -46,17 +46,17 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) * 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; } diff --git a/bn_mp_prime_random_ex.c b/bn_mp_prime_random_ex.c index 2c4f4f0..2010ebe 100644 --- a/bn_mp_prime_random_ex.c +++ b/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; diff --git a/bn_prime_tab.c b/bn_prime_tab.c index 18ecc47..14306c2 100644 --- a/bn_prime_tab.c +++ b/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, diff --git a/bn_s_mp_exptmod.c b/bn_s_mp_exptmod.c index 4f1032a..01a766f 100644 --- a/bn_s_mp_exptmod.c +++ b/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,20 +203,20 @@ 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; } } } @@ -224,9 +224,9 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) 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]); diff --git a/callgraph.txt b/callgraph.txt index 56d4f8b..4dc4cba 100644 --- a/callgraph.txt +++ b/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 diff --git a/changes.txt b/changes.txt index 6a86209..0d1ec2e 100644 --- a/changes.txt +++ b/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 diff --git a/demo/demo.c b/demo/demo.c index 53eb3cf..62615cd 100644 --- a/demo/demo.c +++ b/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; } diff --git a/demo/timing.c b/demo/timing.c index 865c444..7b27d53 100644 --- a/demo/timing.c +++ b/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); diff --git a/etc/mersenne.c b/etc/mersenne.c index da6c111..1cd5b50 100644 --- a/etc/mersenne.c +++ b/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; } diff --git a/etc/pprime.c b/etc/pprime.c index cccb748..26e0d84 100644 --- a/etc/pprime.c +++ b/etc/pprime.c @@ -189,7 +189,7 @@ 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 */ @@ -197,34 +197,34 @@ pprime (int k, int li, mp_int * p, mp_int * q) 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,7 +275,7 @@ 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) @@ -283,7 +283,7 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* 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,7 +292,7 @@ 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) @@ -300,7 +300,7 @@ pprime (int k, int li, mp_int * p, mp_int * q) /* 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; } diff --git a/etc/tune.c b/etc/tune.c index bc101be..14aace2 100644 --- a/etc/tune.c +++ b/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); diff --git a/logs/add.log b/logs/add.log index d44c4cd..fa11039 100644 --- a/logs/add.log +++ b/logs/add.log @@ -1,16 +1,16 @@ -224 222 -448 330 -672 436 -896 520 -1120 612 -1344 696 -1568 810 -1792 912 -2016 1006 -2240 1116 -2464 1152 -2688 1284 -2912 1348 -3136 1486 -3360 1580 -3584 1636 +480 88 +960 113 +1440 138 +1920 163 +2400 202 +2880 226 +3360 251 +3840 272 +4320 296 +4800 320 +5280 344 +5760 368 +6240 392 +6720 416 +7200 440 +7680 464 diff --git a/logs/expt.log b/logs/expt.log index e69de29..e65e927 100644 --- a/logs/expt.log +++ b/logs/expt.log @@ -0,0 +1,7 @@ +513 1499509 +769 3682671 +1025 8098887 +2049 49332743 +2561 89647783 +3073 149440713 +4097 326135364 diff --git a/logs/expt_2k.log b/logs/expt_2k.log index e69de29..d106280 100644 --- a/logs/expt_2k.log +++ b/logs/expt_2k.log @@ -0,0 +1,6 @@ +521 1423346 +607 1841305 +1279 8375656 +2203 34104708 +3217 83830729 +4253 167916804 diff --git a/logs/expt_dr.log b/logs/expt_dr.log index e69de29..6cfc874 100644 --- a/logs/expt_dr.log +++ b/logs/expt_dr.log @@ -0,0 +1,7 @@ +532 1803110 +784 3607375 +1036 6089790 +1540 14739797 +2072 33251589 +3080 82794331 +4116 165212734 diff --git a/logs/mult.log b/logs/mult.log index a2c9c18..864de46 100644 --- a/logs/mult.log +++ b/logs/mult.log @@ -1,143 +1,143 @@ -140 1272 -195 1428 -252 1996 -307 2586 -364 3464 -420 4420 -476 5260 -532 6430 -588 7692 -644 8704 -699 10226 -755 11670 -812 13190 -865 14834 -924 16738 -979 18362 -1036 20660 -1092 22776 -1148 24848 -1204 27168 -1260 29930 -1316 32258 -1370 35172 -1422 37534 -1482 40390 -1537 43990 -1589 46946 -1652 50438 -1703 52902 -1764 56646 -1820 59892 -1876 63248 -1932 66872 -1988 72596 -2042 74662 -2100 78512 -2156 82944 -2211 87444 -2268 92170 -2324 95534 -2380 100484 -2435 105024 -2491 109460 -2546 114154 -2603 118946 -2660 124110 -2716 129300 -2771 134274 -2828 139594 -2883 145234 -2939 150332 -2996 155750 -3048 161718 -3108 167492 -3162 173882 -3219 179766 -3276 185560 -3330 191826 -3388 197822 -3442 204176 -3500 210682 -3556 217236 -3612 223484 -3666 230714 -3724 237744 -3779 244080 -3835 250970 -3890 257914 -3947 265162 -4001 272128 -4060 279108 -4116 287606 -4171 294716 -4227 302806 -4284 310260 -4340 318564 -4395 326164 -4443 334034 -4508 342108 -4561 351810 -4618 358828 -4675 367332 -4732 376140 -4787 384172 -4841 393308 -4899 402036 -4955 411286 -5010 420290 -5067 429688 -5124 438810 -5180 448130 -5235 457264 -5290 467390 -5348 476586 -5404 486120 -5459 496512 -5516 506624 -5569 516346 -5628 526604 -5684 536544 -5740 546936 -5796 557284 -5852 568106 -5907 578824 -5963 589204 -6019 600176 -6076 610564 -6127 621972 -6188 633564 -6244 644730 -6300 655288 -6354 667402 -6412 678824 -6467 690594 -6522 702718 -6580 714148 -6636 725608 -6690 737834 -6747 750100 -6804 762202 -6860 774184 -6916 787298 -6971 798734 -7028 811162 -7083 824570 -7139 837738 -7196 2579488 -7245 2626714 -7308 2643582 -7364 2698746 -7416 2734106 -7476 2773372 -7530 2816738 -7588 2859204 -7643 2938596 -7698 2919716 -7754 2988542 -7812 3026520 -7867 3058304 -7924 3115790 -7977 3161450 -8035 3203138 -8092 3244056 +271 580 +390 861 +511 1177 +630 1598 +749 2115 +871 2670 +991 3276 +1111 3987 +1231 4722 +1351 5474 +1471 6281 +1589 7126 +1710 8114 +1831 8988 +1946 10038 +2071 10995 +2188 12286 +2310 13152 +2430 14480 +2549 15521 +2671 17171 +2790 18081 +2911 19754 +3031 20809 +3150 22849 +3269 23757 +3391 25772 +3508 26832 +3631 29304 +3750 30149 +3865 32581 +3988 33644 +4111 36565 +4231 37309 +4351 40152 +4471 41188 +4590 44658 +4710 45256 +4827 48538 +4951 49490 +5070 53472 +5190 53902 +5308 57619 +5431 58509 +5550 63044 +5664 63333 +5791 67542 +5911 68279 +6028 73477 +6150 73475 +6271 78189 +6390 78842 +6510 84691 +6631 84444 +6751 89721 +6871 90186 +6991 96665 +7111 96119 +7231 101937 +7350 102212 +7471 109439 +7591 108491 +7709 114965 +7829 115025 +7951 123002 +8071 121630 +8190 128725 +8311 128536 +8430 137298 +8550 135568 +8671 143265 +8791 142793 +8911 152432 +9030 150202 +9151 158616 +9271 157848 +9391 168374 +9511 165651 +9627 174775 +9750 173375 +9871 185067 +9985 181845 +10111 191708 +10229 190239 +10351 202585 +10467 198704 +10591 209193 +10711 207322 +10831 220842 +10950 215882 +11071 227761 +11191 225501 +11311 239669 +11430 234809 +11550 243511 +11671 255947 +11791 255243 +11906 267828 +12029 263437 +12149 276571 +12270 275579 +12390 288963 +12510 284001 +12631 298196 +12751 297018 +12869 310848 +12990 305369 +13111 319086 +13230 318940 +13349 333685 +13471 327495 +13588 343678 +13711 341817 +13831 357181 +13948 350440 +14071 367526 +14189 365330 +14311 381551 +14429 374149 +14549 392203 +14670 389764 +14791 406761 +14910 398652 +15026 417718 +15150 414733 +15269 432759 +15390 1037071 +15511 1053454 +15631 1069198 +15748 1086164 +15871 1112820 +15991 1129676 +16111 1145924 +16230 1163016 +16345 1179911 +16471 1197048 +16586 1214352 +16711 1232095 +16829 1249338 +16947 1266987 +17071 1284181 +17188 1302521 +17311 1320539 diff --git a/logs/sqr.log b/logs/sqr.log index 3e175ac..0898342 100644 --- a/logs/sqr.log +++ b/logs/sqr.log @@ -1,143 +1,143 @@ -139 806 -195 1212 -252 1604 -307 2260 -364 2892 -420 3308 -476 4152 -532 4814 -588 5754 -644 6684 -700 7226 -756 8324 -808 9092 -866 10068 -924 11204 -976 12918 -1036 13656 -1092 15248 -1148 15956 -1204 17270 -1260 19894 -1316 20516 -1370 21864 -1428 25554 -1483 26138 -1540 27086 -1596 29246 -1652 32210 -1707 32704 -1764 35142 -1820 39050 -1876 39256 -1931 41574 -1985 45070 -2044 46352 -2099 48114 -2155 51332 -2212 53268 -2267 55890 -2324 59054 -2380 60206 -2434 63540 -2491 66084 -2547 68590 -2604 74332 -2660 74784 -2715 77974 -2772 79924 -2826 82914 -2884 87210 -2929 89076 -2996 92480 -3052 96814 -3108 99990 -3162 102550 -3219 105396 -3276 109284 -3332 113752 -3387 116628 -3444 120782 -3500 122938 -3556 127940 -3612 303656 -3667 312212 -3724 324376 -3779 329204 -3833 340910 -3892 353850 -3943 362348 -4003 367780 -4056 380448 -4114 393616 -4172 404104 -4227 415148 -4284 409770 -4339 436648 -4394 442970 -4451 463096 -4507 472056 -4564 485780 -4616 496286 -4675 507612 -4732 519524 -4788 536768 -4843 542754 -4899 553090 -4956 571986 -5012 586340 -5068 599606 -5124 613670 -5179 624256 -5235 636266 -5292 655518 -5348 668142 -5403 677266 -5460 696040 -5516 712772 -5570 723942 -5628 739052 -5684 755350 -5739 769962 -5790 775258 -5851 790128 -5908 814536 -5962 827278 -6018 844510 -6076 851606 -6130 865748 -6188 894752 -6244 900474 -6300 928174 -6356 928440 -6410 957758 -6468 981134 -6524 994088 -6580 1011124 -6636 1027178 -6692 1045466 -6747 1056910 -6804 1083784 -6860 1104706 -6915 1116450 -6972 1137894 -7028 1154670 -7084 1158064 -7138 1188734 -7196 1214218 -7249 1226822 -7307 1247528 -7363 1255338 -7420 1291104 -7475 1297940 -7532 1324994 -7587 1340274 -7644 1342596 -7698 1381418 -7756 1382904 -7812 1432588 -7867 1443632 -7922 1465092 -7979 1496804 -8036 1520142 -8092 1539566 +271 552 +389 883 +510 1191 +629 1572 +750 1996 +863 2428 +991 2891 +1108 3539 +1231 4182 +1351 4980 +1471 5771 +1590 6551 +1711 7313 +1830 8240 +1951 9184 +2070 10087 +2191 11140 +2311 12111 +2431 13219 +2550 14247 +2669 15353 +2791 16446 +2911 17692 +3029 18848 +3151 20028 +3268 21282 +3391 22696 +3511 23971 +3631 25303 +3751 26675 +3871 28245 +3990 29736 +4111 31124 +4229 32714 +4347 34397 +4471 35877 +4587 37269 +4710 39011 +4831 40884 +4950 42501 +5070 44005 +5191 46026 +5310 48168 +5431 49801 +5551 51385 +5671 53604 +5787 55942 +5910 57757 +6031 59391 +6151 61754 +6271 64234 +6390 66110 +6511 67845 +6627 70474 +6751 73113 +6871 75064 +6990 76940 +7111 79681 +7230 82548 +7351 84597 +7471 86507 +7591 89497 +7711 225216 +7831 232192 +7951 239583 +8071 247302 +8191 255497 +8308 261587 +8431 271490 +8550 279492 +8671 286927 +8790 294680 +8910 302974 +9030 311300 +9150 318635 +9271 326740 +9390 335304 +9511 344297 +9630 352056 +9748 358652 +9870 369723 +9991 379119 +10111 386982 +10231 396075 +10349 404396 +10470 415375 +10590 424146 +10711 433390 +10829 442662 +10950 453238 +11071 462178 +11186 469811 +11311 482529 +11431 493214 +11550 503210 +11671 513486 +11791 524244 +11911 535277 +12031 544872 +12151 555695 +12271 566893 +12391 578385 +12510 588658 +12628 596914 +12751 611324 +12871 623437 +12991 633907 +13110 645605 +13231 657684 +13351 670037 +13471 680939 +13591 693047 +13710 705363 +13829 718178 +13949 727930 +14069 739641 +14190 754817 +14310 768192 +14431 779875 +14551 792655 +14667 802847 +14791 819806 +14911 831684 +15031 844936 +15151 858813 +15270 873037 +15387 882123 +15510 899117 +15631 913465 +15750 927989 +15870 940790 +15991 954948 +16110 969483 +16231 984544 +16350 997837 +16470 1012445 +16590 1027834 +16710 1043032 +16831 1056394 +16951 1071408 +17069 1097263 +17191 1113364 +17306 1123650 diff --git a/logs/sub.log b/logs/sub.log index cf2bcd6..a42d91e 100644 --- a/logs/sub.log +++ b/logs/sub.log @@ -1,16 +1,16 @@ -224 216 -448 324 -672 428 -896 532 -1120 648 -1344 766 -1568 862 -1792 928 -2016 1070 -2240 1128 -2464 1250 -2688 1344 -2912 1436 -3136 1542 -3360 1628 -3584 1696 +480 87 +960 114 +1440 139 +1920 159 +2400 204 +2880 228 +3360 250 +3840 273 +4320 300 +4800 321 +5280 348 +5760 370 +6240 393 +6720 420 +7200 444 +7680 466 diff --git a/makefile b/makefile index 4fe2256..164a0ab 100644 --- a/makefile +++ b/makefile @@ -1,10 +1,14 @@ #Makefile for GCC # #Tom St Denis + +#version of library +VERSION=0.33 + CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare #for speed -CFLAGS += -O3 -funroll-loops +CFLAGS += -O3 -funroll-all-loops #for size #CFLAGS += -Os @@ -15,13 +19,15 @@ CFLAGS += -fomit-frame-pointer #debug #CFLAGS += -g3 -VERSION=0.32 +#install as this user +USER=root +GROUP=root default: libtommath.a #default files to install LIBNAME=libtommath.a -HEADERS=tommath.h +HEADERS=tommath.h tommath_class.h tommath_superclass.h #LIBPATH-The directory for libtommath to be installed to. #INCPATH-The directory to install the header files for libtommath. @@ -61,7 +67,6 @@ libtommath.a: $(OBJECTS) $(AR) $(ARFLAGS) libtommath.a $(OBJECTS) ranlib libtommath.a - #make a profiled library (takes a while!!!) # # This will build the library with profile generation @@ -86,19 +91,19 @@ profiled_single: ranlib libtommath.a install: libtommath.a - install -d -g root -o root $(DESTDIR)$(LIBPATH) - install -d -g root -o root $(DESTDIR)$(INCPATH) - install -g root -o root $(LIBNAME) $(DESTDIR)$(LIBPATH) - install -g root -o root $(HEADERS) $(DESTDIR)$(INCPATH) + install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH) + install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH) + install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH) + install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH) test: libtommath.a demo/demo.o - $(CC) demo/demo.o libtommath.a -o test + $(CC) $(CFLAGS) demo/demo.o libtommath.a -o test mtest: test - cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest -s + cd mtest ; $(CC) $(CFLAGS) mtest.c -o mtest timing: libtommath.a - $(CC) $(CFLAGS) -DTIMER demo/timing.c libtommath.a -o ltmtest -s + $(CC) $(CFLAGS) -DTIMER demo/timing.c libtommath.a -o ltmtest # makes the LTM book DVI file, requires tetex, perl and makeindex [part of tetex I think] docdvi: tommath.src diff --git a/makefile.icc b/makefile.icc index 09117b7..3775b20 100644 --- a/makefile.icc +++ b/makefile.icc @@ -21,6 +21,10 @@ CFLAGS += -I./ # Default to just generic max opts CFLAGS += -O3 -xN +#install as this user +USER=root +GROUP=root + default: libtommath.a #default files to install @@ -89,10 +93,10 @@ profiled_single: ranlib libtommath.a install: libtommath.a - install -d -g root -o root $(DESTDIR)$(LIBPATH) - install -d -g root -o root $(DESTDIR)$(INCPATH) - install -g root -o root $(LIBNAME) $(DESTDIR)$(LIBPATH) - install -g root -o root $(HEADERS) $(DESTDIR)$(INCPATH) + install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(LIBPATH) + install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH) + install -g $(GROUP) -o $(USER) $(LIBNAME) $(DESTDIR)$(LIBPATH) + install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH) test: libtommath.a demo/demo.o $(CC) demo/demo.o libtommath.a -o test diff --git a/makefile.shared b/makefile.shared index 96bbf32..86a3786 100644 --- a/makefile.shared +++ b/makefile.shared @@ -1,10 +1,9 @@ #Makefile for GCC # #Tom St Denis -VERSION=0:32 +VERSION=0:33 CC = libtool --mode=compile gcc - CFLAGS += -I./ -Wall -W -Wshadow -Wsign-compare #for speed @@ -16,11 +15,15 @@ CFLAGS += -O3 -funroll-loops #x86 optimizations [should be valid for any GCC install though] CFLAGS += -fomit-frame-pointer +#install as this user +USER=root +GROUP=root + default: libtommath.la #default files to install LIBNAME=libtommath.la -HEADERS=tommath.h +HEADERS=tommath.h tommath_class.h tommath_superclass.h #LIBPATH-The directory for libtommath to be installed to. #INCPATH-The directory to install the header files for libtommath. @@ -60,8 +63,8 @@ libtommath.la: $(OBJECTS) libtool --mode=link gcc *.lo -o libtommath.la -rpath $(LIBPATH) -version-info $(VERSION) libtool --mode=link gcc *.o -o libtommath.a libtool --mode=install install -c libtommath.la $(LIBPATH)/libtommath.la - install -d -g root -o root $(DESTDIR)$(INCPATH) - install -g root -o root $(HEADERS) $(DESTDIR)$(INCPATH) + install -d -g $(GROUP) -o $(USER) $(DESTDIR)$(INCPATH) + install -g $(GROUP) -o $(USER) $(HEADERS) $(DESTDIR)$(INCPATH) test: libtommath.a demo/demo.o gcc $(CFLAGS) -c demo/demo.c -o demo/demo.o diff --git a/mtest/mtest.c b/mtest/mtest.c index ef0e093..d46f456 100644 --- a/mtest/mtest.c +++ b/mtest/mtest.c @@ -46,7 +46,7 @@ void rand_num(mp_int *a) int n, size; unsigned char buf[2048]; - size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 1031; + size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101; buf[0] = (fgetc(rng)&1)?1:0; fread(buf+1, 1, size, rng); while (buf[1] == 0) buf[1] = fgetc(rng); @@ -58,7 +58,7 @@ void rand_num2(mp_int *a) int n, size; unsigned char buf[2048]; - size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 97; + size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101; buf[0] = (fgetc(rng)&1)?1:0; fread(buf+1, 1, size, rng); while (buf[1] == 0) buf[1] = fgetc(rng); diff --git a/poster.pdf b/poster.pdf index 60999da..e0b4f84 100644 Binary files a/poster.pdf and b/poster.pdf differ diff --git a/pre_gen/mpi.c b/pre_gen/mpi.c index 78a73f0..7d832e7 100644 --- a/pre_gen/mpi.c +++ b/pre_gen/mpi.c @@ -87,20 +87,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); @@ -109,17 +109,17 @@ top: 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; } } @@ -127,18 +127,18 @@ top: 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; } } @@ -146,20 +146,20 @@ top: 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; } } @@ -173,21 +173,21 @@ top: /* 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 @@ -420,7 +420,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; @@ -450,6 +450,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; @@ -519,7 +522,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; @@ -547,6 +550,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; @@ -636,7 +642,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; @@ -1539,23 +1545,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; } } @@ -1564,13 +1570,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; } @@ -1619,19 +1625,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 */ @@ -1643,10 +1649,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; @@ -1658,13 +1664,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; } } @@ -1706,7 +1712,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 */ @@ -1718,27 +1724,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; @@ -1765,11 +1771,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; } @@ -2199,7 +2205,7 @@ int mp_dr_is_modulus(mp_int *a) * 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] @@ -2457,7 +2463,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 } @@ -2588,11 +2594,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) */ @@ -2608,7 +2614,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) { @@ -2618,24 +2624,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 @@ -2649,45 +2655,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; } } @@ -2727,10 +2733,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; } @@ -2744,19 +2750,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 */ @@ -2771,10 +2777,10 @@ 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 */ @@ -2782,10 +2788,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode) 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; } } } @@ -2799,15 +2805,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]); @@ -3059,7 +3065,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 */ @@ -3073,24 +3079,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; } } @@ -3103,23 +3109,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 @@ -3556,24 +3562,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); @@ -3583,24 +3589,24 @@ top: 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; } } @@ -3608,24 +3614,24 @@ top: 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; } } @@ -3633,28 +3639,28 @@ top: 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; } } @@ -3667,27 +3673,27 @@ top: /* 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 @@ -3856,13 +3862,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 */ @@ -3890,18 +3896,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 @@ -4227,20 +4233,20 @@ 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); } @@ -4248,7 +4254,7 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c) /* fix the sign to positive */ c->sign = MP_ZPOS; -__T: +LBL_T: mp_clear_multi (&t1, &t2, NULL); return res; } @@ -4402,7 +4408,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; } @@ -5085,11 +5091,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 */ @@ -5102,52 +5108,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; @@ -5165,9 +5171,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 @@ -5304,7 +5310,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? */ @@ -5313,7 +5319,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 @@ -5352,8 +5358,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; } @@ -5410,7 +5416,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; } @@ -5433,20 +5439,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 @@ -5496,12 +5502,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 @@ -5511,15 +5517,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 */ @@ -5528,12 +5534,12 @@ 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; @@ -5541,15 +5547,15 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result) /* 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 @@ -5594,10 +5600,10 @@ 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] @@ -5605,17 +5611,17 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style) * 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; } } @@ -5653,7 +5659,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; } } @@ -5679,8 +5685,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 */ @@ -5692,7 +5698,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 */ @@ -5702,9 +5708,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; @@ -5717,7 +5723,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; } @@ -5828,7 +5834,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); @@ -5837,7 +5843,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; @@ -5846,7 +5852,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; @@ -7996,7 +8002,7 @@ mp_zero (mp_int * a) * * 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, @@ -8261,10 +8267,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 @@ -8276,23 +8282,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; } } @@ -8301,16 +8307,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); @@ -8350,10 +8356,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; } @@ -8367,19 +8373,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 */ @@ -8394,20 +8400,20 @@ 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; } } } @@ -8415,9 +8421,9 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y) 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]); diff --git a/tommath.h b/tommath.h index 896d389..7cc92c2 100644 --- a/tommath.h +++ b/tommath.h @@ -442,7 +442,7 @@ int mp_exptmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d); #endif /* table of first PRIME_SIZE primes */ -extern const mp_digit __prime_tab[]; +extern const mp_digit ltm_prime_tab[]; /* result=1 if a is divisible by one of the first PRIME_SIZE primes */ int mp_prime_is_divisible(mp_int *a, int *result); diff --git a/tommath.pdf b/tommath.pdf index 18cac6f..88e2dc7 100644 Binary files a/tommath.pdf and b/tommath.pdf differ diff --git a/tommath.tex b/tommath.tex index d0ac947..9c4dc82 100644 --- a/tommath.tex +++ b/tommath.tex @@ -3420,7 +3420,7 @@ is copied to $b$, leading digits are removed and the remaining leading digit is 027 \} 028 029 /* if the modulus is larger than the value than return */ -030 if (b > (int) (a->used * DIGIT_BIT)) \{ +030 if (b >= (int) (a->used * DIGIT_BIT)) \{ 031 res = mp_copy (a, c); 032 return res; 033 \} @@ -3896,7 +3896,7 @@ and addition operations in the nested loop in parallel. 049 050 /* clear the carry */ 051 _W = 0; -052 for (ix = 0; ix <= pa; ix++) \{ +052 for (ix = 0; ix < pa; ix++) \{ 053 int tx, ty; 054 int iy; 055 mp_digit *tmpx, *tmpy; @@ -3927,27 +3927,30 @@ and addition operations in the nested loop in parallel. 079 _W = _W >> ((mp_word)DIGIT_BIT); 080 \} 081 -082 /* setup dest */ -083 olduse = c->used; -084 c->used = digs; -085 -086 \{ -087 register mp_digit *tmpc; -088 tmpc = c->dp; -089 for (ix = 0; ix < digs; ix++) \{ -090 /* now extract the previous digit [below the carry] */ -091 *tmpc++ = W[ix]; -092 \} -093 -094 /* clear unused digits [that existed in the old copy of c] */ -095 for (; ix < olduse; ix++) \{ -096 *tmpc++ = 0; -097 \} -098 \} -099 mp_clamp (c); -100 return MP_OKAY; -101 \} -102 #endif +082 /* store final carry */ +083 W[ix] = _W; +084 +085 /* setup dest */ +086 olduse = c->used; +087 c->used = digs; +088 +089 \{ +090 register mp_digit *tmpc; +091 tmpc = c->dp; +092 for (ix = 0; ix < digs; ix++) \{ +093 /* now extract the previous digit [below the carry] */ +094 *tmpc++ = W[ix]; +095 \} +096 +097 /* clear unused digits [that existed in the old copy of c] */ +098 for (; ix < olduse; ix++) \{ +099 *tmpc++ = 0; +100 \} +101 \} +102 mp_clamp (c); +103 return MP_OKAY; +104 \} +105 #endif \end{alltt} \end{small} @@ -3955,7 +3958,7 @@ The memset on line @47,memset@ clears the initial $\hat W$ array to zero in a si implementation a series of aliases (\textit{lines 62, 63 and 76}) are used to simplify the inner $O(n^2)$ loop. In this case a new alias $\_\hat W$ has been added which refers to the double precision columns offset by $ix$ in each pass. -The inner loop on lines 89, 79 and 80 is where the algorithm will spend the majority of the time, which is why it has been +The inner loop on lines 92, 79 and 80 is where the algorithm will spend the majority of the time, which is why it has been stripped to the bones of any extra baggage\footnote{Hence the pointer aliases.}. On x86 processors the multiplication and additions amount to at the very least five instructions (\textit{two loads, two additions, one multiply}) while on the ARMv4 processors they amount to only three (\textit{one load, one store, one multiply-add}). For both of the x86 and ARMv4 processors the GCC compiler performs a good job at unrolling the loop @@ -5100,7 +5103,7 @@ squares in place. 059 060 /* number of output digits to produce */ 061 W1 = 0; -062 for (ix = 0; ix <= pa; ix++) \{ +062 for (ix = 0; ix < pa; ix++) \{ 063 int tx, ty, iy; 064 mp_word _W; 065 mp_digit *tmpy; @@ -6739,7 +6742,7 @@ at step 3. 019 * Based on algorithm from the paper 020 * 021 * "Generating Efficient Primes for Discrete Log Cryptosystems" -022 * Chae Hoon Lim, Pil Loong Lee, +022 * Chae Hoon Lim, Pil Joong Lee, 023 * POSTECH Information Research Laboratories 024 * 025 * The modulus must be of a special format [see manual] @@ -7594,7 +7597,7 @@ algorithm since their arguments are essentially the same (\textit{two mp\_ints a 060 return err; 061 #else 062 /* no invmod */ -063 return MP_VAL +063 return MP_VAL; 064 #endif 065 \} 066 @@ -7866,10 +7869,10 @@ a Left-to-Right algorithm is used to process the remaining few bits. 069 070 /* create mu, used for Barrett reduction */ 071 if ((err = mp_init (&mu)) != MP_OKAY) \{ -072 goto __M; +072 goto LBL_M; 073 \} 074 if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) \{ -075 goto __MU; +075 goto LBL_MU; 076 \} 077 078 /* create M table @@ -7881,23 +7884,23 @@ a Left-to-Right algorithm is used to process the remaining few bits. 084 * computed though accept for M[0] and M[1] 085 */ 086 if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) \{ -087 goto __MU; +087 goto LBL_MU; 088 \} 089 090 /* compute the value at M[1<<(winsize-1)] by squaring 091 * M[1] (winsize-1) times 092 */ 093 if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) \{ -094 goto __MU; +094 goto LBL_MU; 095 \} 096 097 for (x = 0; x < (winsize - 1); x++) \{ 098 if ((err = mp_sqr (&M[1 << (winsize - 1)], 099 &M[1 << (winsize - 1)])) != MP_OKAY) \{ -100 goto __MU; +100 goto LBL_MU; 101 \} 102 if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) \{ -103 goto __MU; +103 goto LBL_MU; 104 \} 105 \} 106 @@ -7906,16 +7909,16 @@ a Left-to-Right algorithm is used to process the remaining few bits. 109 */ 110 for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) \{ 111 if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) \{ -112 goto __MU; +112 goto LBL_MU; 113 \} 114 if ((err = mp_reduce (&M[x], P, &mu)) != MP_OKAY) \{ -115 goto __MU; +115 goto LBL_MU; 116 \} 117 \} 118 119 /* setup result */ 120 if ((err = mp_init (&res)) != MP_OKAY) \{ -121 goto __MU; +121 goto LBL_MU; 122 \} 123 mp_set (&res, 1); 124 @@ -7955,10 +7958,10 @@ a Left-to-Right algorithm is used to process the remaining few bits. 158 /* if the bit is zero and mode == 1 then we square */ 159 if (mode == 1 && y == 0) \{ 160 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{ -161 goto __RES; +161 goto LBL_RES; 162 \} 163 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{ -164 goto __RES; +164 goto LBL_RES; 165 \} 166 continue; 167 \} @@ -7972,19 +7975,19 @@ a Left-to-Right algorithm is used to process the remaining few bits. 175 /* square first */ 176 for (x = 0; x < winsize; x++) \{ 177 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{ -178 goto __RES; +178 goto LBL_RES; 179 \} 180 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{ -181 goto __RES; +181 goto LBL_RES; 182 \} 183 \} 184 185 /* then multiply */ 186 if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) \{ -187 goto __RES; +187 goto LBL_RES; 188 \} 189 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{ -190 goto __RES; +190 goto LBL_RES; 191 \} 192 193 /* empty window and reset */ @@ -7999,20 +8002,20 @@ a Left-to-Right algorithm is used to process the remaining few bits. 202 /* square then multiply if the bit is set */ 203 for (x = 0; x < bitcpy; x++) \{ 204 if ((err = mp_sqr (&res, &res)) != MP_OKAY) \{ -205 goto __RES; +205 goto LBL_RES; 206 \} 207 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{ -208 goto __RES; +208 goto LBL_RES; 209 \} 210 211 bitbuf <<= 1; 212 if ((bitbuf & (1 << winsize)) != 0) \{ 213 /* then multiply */ 214 if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) \{ -215 goto __RES; +215 goto LBL_RES; 216 \} 217 if ((err = mp_reduce (&res, P, &mu)) != MP_OKAY) \{ -218 goto __RES; +218 goto LBL_RES; 219 \} 220 \} 221 \} @@ -8020,9 +8023,9 @@ a Left-to-Right algorithm is used to process the remaining few bits. 223 224 mp_exch (&res, Y); 225 err = MP_OKAY; -226 __RES:mp_clear (&res); -227 __MU:mp_clear (&mu); -228 __M: +226 LBL_RES:mp_clear (&res); +227 LBL_MU:mp_clear (&mu); +228 LBL_M: 229 mp_clear(&M[1]); 230 for (x = 1<<(winsize-1); x < (1 << winsize); x++) \{ 231 mp_clear (&M[x]); @@ -8386,23 +8389,23 @@ respectively be replaced with a zero. 048 049 mp_set(&tq, 1); 050 n = mp_count_bits(a) - mp_count_bits(b); -051 if (((res = mp_copy(a, &ta)) != MP_OKAY) || -052 ((res = mp_copy(b, &tb)) != MP_OKAY) || +051 if (((res = mp_abs(a, &ta)) != MP_OKAY) || +052 ((res = mp_abs(b, &tb)) != MP_OKAY) || 053 ((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) || 054 ((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) \{ -055 goto __ERR; +055 goto LBL_ERR; 056 \} 057 058 while (n-- >= 0) \{ 059 if (mp_cmp(&tb, &ta) != MP_GT) \{ 060 if (((res = mp_sub(&ta, &tb, &ta)) != MP_OKAY) || 061 ((res = mp_add(&q, &tq, &q)) != MP_OKAY)) \{ -062 goto __ERR; +062 goto LBL_ERR; 063 \} 064 \} 065 if (((res = mp_div_2d(&tb, 1, &tb, NULL)) != MP_OKAY) || 066 ((res = mp_div_2d(&tq, 1, &tq, NULL)) != MP_OKAY)) \{ -067 goto __ERR; +067 goto LBL_ERR; 068 \} 069 \} 070 @@ -8411,13 +8414,13 @@ respectively be replaced with a zero. 073 n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); 074 if (c != NULL) \{ 075 mp_exch(c, &q); -076 c->sign = n2; +076 c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; 077 \} 078 if (d != NULL) \{ 079 mp_exch(d, &ta); -080 d->sign = n; +080 d->sign = (mp_iszero(d) == MP_YES) ? MP_ZPOS : n; 081 \} -082 __ERR: +082 LBL_ERR: 083 mp_clear_multi(&ta, &tb, &tq, &q, NULL); 084 return res; 085 \} @@ -8466,19 +8469,19 @@ respectively be replaced with a zero. 128 q.used = a->used + 2; 129 130 if ((res = mp_init (&t1)) != MP_OKAY) \{ -131 goto __Q; +131 goto LBL_Q; 132 \} 133 134 if ((res = mp_init (&t2)) != MP_OKAY) \{ -135 goto __T1; +135 goto LBL_T1; 136 \} 137 138 if ((res = mp_init_copy (&x, a)) != MP_OKAY) \{ -139 goto __T2; +139 goto LBL_T2; 140 \} 141 142 if ((res = mp_init_copy (&y, b)) != MP_OKAY) \{ -143 goto __X; +143 goto LBL_X; 144 \} 145 146 /* fix the sign */ @@ -8490,10 +8493,10 @@ respectively be replaced with a zero. 152 if (norm < (int)(DIGIT_BIT-1)) \{ 153 norm = (DIGIT_BIT-1) - norm; 154 if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) \{ -155 goto __Y; +155 goto LBL_Y; 156 \} 157 if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) \{ -158 goto __Y; +158 goto LBL_Y; 159 \} 160 \} else \{ 161 norm = 0; @@ -8505,13 +8508,13 @@ respectively be replaced with a zero. 167 168 /* while (x >= y*b**n-t) do \{ q[n-t] += 1; x -= y*b**\{n-t\} \} */ 169 if ((res = mp_lshd (&y, n - t)) != MP_OKAY) \{ /* y = y*b**\{n-t\} */ -170 goto __Y; +170 goto LBL_Y; 171 \} 172 173 while (mp_cmp (&x, &y) != MP_LT) \{ 174 ++(q.dp[n - t]); 175 if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) \{ -176 goto __Y; +176 goto LBL_Y; 177 \} 178 \} 179 @@ -8553,7 +8556,7 @@ respectively be replaced with a zero. 215 t1.dp[1] = y.dp[t]; 216 t1.used = 2; 217 if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) \{ -218 goto __Y; +218 goto LBL_Y; 219 \} 220 221 /* find right hand */ @@ -8565,27 +8568,27 @@ respectively be replaced with a zero. 227 228 /* step 3.3 x = x - q\{i-t-1\} * y * b**\{i-t-1\} */ 229 if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) \{ -230 goto __Y; +230 goto LBL_Y; 231 \} 232 233 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{ -234 goto __Y; +234 goto LBL_Y; 235 \} 236 237 if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) \{ -238 goto __Y; +238 goto LBL_Y; 239 \} 240 241 /* if x < 0 then \{ x = x + y*b**\{i-t-1\}; q\{i-t-1\} -= 1; \} */ 242 if (x.sign == MP_NEG) \{ 243 if ((res = mp_copy (&y, &t1)) != MP_OKAY) \{ -244 goto __Y; +244 goto LBL_Y; 245 \} 246 if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) \{ -247 goto __Y; +247 goto LBL_Y; 248 \} 249 if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) \{ -250 goto __Y; +250 goto LBL_Y; 251 \} 252 253 q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; @@ -8612,11 +8615,11 @@ respectively be replaced with a zero. 274 275 res = MP_OKAY; 276 -277 __Y:mp_clear (&y); -278 __X:mp_clear (&x); -279 __T2:mp_clear (&t2); -280 __T1:mp_clear (&t1); -281 __Q:mp_clear (&q); +277 LBL_Y:mp_clear (&y); +278 LBL_X:mp_clear (&x); +279 LBL_T2:mp_clear (&t2); +280 LBL_T1:mp_clear (&t1); +281 LBL_Q:mp_clear (&q); 282 return res; 283 \} 284 @@ -9130,11 +9133,11 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where 039 \} 040 041 if ((res = mp_init (&t2)) != MP_OKAY) \{ -042 goto __T1; +042 goto LBL_T1; 043 \} 044 045 if ((res = mp_init (&t3)) != MP_OKAY) \{ -046 goto __T2; +046 goto LBL_T2; 047 \} 048 049 /* if a is negative fudge the sign but keep track */ @@ -9147,52 +9150,52 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where 056 do \{ 057 /* t1 = t2 */ 058 if ((res = mp_copy (&t2, &t1)) != MP_OKAY) \{ -059 goto __T3; +059 goto LBL_T3; 060 \} 061 062 /* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */ 063 064 /* t3 = t1**(b-1) */ 065 if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) \{ -066 goto __T3; +066 goto LBL_T3; 067 \} 068 069 /* numerator */ 070 /* t2 = t1**b */ 071 if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) \{ -072 goto __T3; +072 goto LBL_T3; 073 \} 074 075 /* t2 = t1**b - a */ 076 if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) \{ -077 goto __T3; +077 goto LBL_T3; 078 \} 079 080 /* denominator */ 081 /* t3 = t1**(b-1) * b */ 082 if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) \{ -083 goto __T3; +083 goto LBL_T3; 084 \} 085 086 /* t3 = (t1**b - a)/(b * t1**(b-1)) */ 087 if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) \{ -088 goto __T3; +088 goto LBL_T3; 089 \} 090 091 if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) \{ -092 goto __T3; +092 goto LBL_T3; 093 \} 094 \} while (mp_cmp (&t1, &t2) != MP_EQ); 095 096 /* result can be off by a few so check */ 097 for (;;) \{ 098 if ((res = mp_expt_d (&t1, b, &t2)) != MP_OKAY) \{ -099 goto __T3; +099 goto LBL_T3; 100 \} 101 102 if (mp_cmp (&t2, a) == MP_GT) \{ 103 if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) \{ -104 goto __T3; +104 goto LBL_T3; 105 \} 106 \} else \{ 107 break; @@ -9210,9 +9213,9 @@ root. Ideally this algorithm is meant to find the $n$'th root of an input where 119 120 res = MP_OKAY; 121 -122 __T3:mp_clear (&t3); -123 __T2:mp_clear (&t2); -124 __T1:mp_clear (&t1); +122 LBL_T3:mp_clear (&t3); +123 LBL_T2:mp_clear (&t2); +124 LBL_T1:mp_clear (&t1); 125 return res; 126 \} 127 #endif @@ -9771,7 +9774,7 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear 042 \} 043 044 if ((res = mp_init_copy (&v, b)) != MP_OKAY) \{ -045 goto __U; +045 goto LBL_U; 046 \} 047 048 /* must be positive for the remainder of the algorithm */ @@ -9785,24 +9788,24 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear 056 if (k > 0) \{ 057 /* divide the power of two out */ 058 if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) \{ -059 goto __V; +059 goto LBL_V; 060 \} 061 062 if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) \{ -063 goto __V; +063 goto LBL_V; 064 \} 065 \} 066 067 /* divide any remaining factors of two out */ 068 if (u_lsb != k) \{ 069 if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) \{ -070 goto __V; +070 goto LBL_V; 071 \} 072 \} 073 074 if (v_lsb != k) \{ 075 if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) \{ -076 goto __V; +076 goto LBL_V; 077 \} 078 \} 079 @@ -9815,23 +9818,23 @@ must be adjusted by multiplying by the common factors of two ($2^k$) removed ear 086 087 /* subtract smallest from largest */ 088 if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) \{ -089 goto __V; +089 goto LBL_V; 090 \} 091 092 /* Divide out all factors of two */ 093 if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) \{ -094 goto __V; +094 goto LBL_V; 095 \} 096 \} 097 098 /* multiply by 2**k which we divided out at the beginning */ 099 if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) \{ -100 goto __V; +100 goto LBL_V; 101 \} 102 c->sign = MP_ZPOS; 103 res = MP_OKAY; -104 __V:mp_clear (&u); -105 __U:mp_clear (&v); +104 LBL_V:mp_clear (&u); +105 LBL_U:mp_clear (&v); 106 return res; 107 \} 108 #endif @@ -9904,20 +9907,20 @@ dividing the product of the two inputs by their greatest common divisor. 027 028 /* t1 = get the GCD of the two inputs */ 029 if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) \{ -030 goto __T; +030 goto LBL_T; 031 \} 032 033 /* divide the smallest by the GCD */ 034 if (mp_cmp_mag(a, b) == MP_LT) \{ 035 /* store quotient in t2 such that t2 * b is the LCM */ 036 if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) \{ -037 goto __T; +037 goto LBL_T; 038 \} 039 res = mp_mul(b, &t2, c); 040 \} else \{ 041 /* store quotient in t2 such that t2 * a is the LCM */ 042 if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) \{ -043 goto __T; +043 goto LBL_T; 044 \} 045 res = mp_mul(a, &t2, c); 046 \} @@ -9925,7 +9928,7 @@ dividing the product of the two inputs by their greatest common divisor. 048 /* fix the sign to positive */ 049 c->sign = MP_ZPOS; 050 -051 __T: +051 LBL_T: 052 mp_clear_multi (&t1, &t2, NULL); 053 return res; 054 \} @@ -10123,13 +10126,13 @@ $\left ( {p' \over a'} \right )$ which is multiplied against the current Jacobi 049 \} 050 051 if ((res = mp_init (&p1)) != MP_OKAY) \{ -052 goto __A1; +052 goto LBL_A1; 053 \} 054 055 /* divide out larger power of two */ 056 k = mp_cnt_lsb(&a1); 057 if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) \{ -058 goto __P1; +058 goto LBL_P1; 059 \} 060 061 /* step 4. if e is even set s=1 */ @@ -10157,18 +10160,18 @@ $\left ( {p' \over a'} \right )$ which is multiplied against the current Jacobi 083 \} else \{ 084 /* n1 = n mod a1 */ 085 if ((res = mp_mod (p, &a1, &p1)) != MP_OKAY) \{ -086 goto __P1; +086 goto LBL_P1; 087 \} 088 if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) \{ -089 goto __P1; +089 goto LBL_P1; 090 \} 091 *c = s * r; 092 \} 093 094 /* done */ 095 res = MP_OKAY; -096 __P1:mp_clear (&p1); -097 __A1:mp_clear (&a1); +096 LBL_P1:mp_clear (&p1); +097 LBL_A1:mp_clear (&a1); 098 return res; 099 \} 100 #endif @@ -10406,8 +10409,8 @@ This algorithm attempts to determine if a candidate integer $n$ is composite by 028 *result = MP_NO; 029 030 for (ix = 0; ix < PRIME_SIZE; ix++) \{ -031 /* what is a mod __prime_tab[ix] */ -032 if ((err = mp_mod_d (a, __prime_tab[ix], &res)) != MP_OKAY) \{ +031 /* what is a mod LBL_prime_tab[ix] */ +032 if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) \{ 033 return err; 034 \} 035 @@ -10431,7 +10434,7 @@ mp\_digit. The table \_\_prime\_tab is defined in the following file. \hspace{-5.1mm}{\bf File}: bn\_prime\_tab.c \vspace{-3mm} \begin{alltt} -016 const mp_digit __prime_tab[] = \{ +016 const mp_digit ltm_prime_tab[] = \{ 017 0x0002, 0x0003, 0x0005, 0x0007, 0x000B, 0x000D, 0x0011, 0x0013, 018 0x0017, 0x001D, 0x001F, 0x0025, 0x0029, 0x002B, 0x002F, 0x0035, 019 0x003B, 0x003D, 0x0043, 0x0047, 0x0049, 0x004F, 0x0053, 0x0059, @@ -10547,7 +10550,7 @@ determine the result. 042 043 /* compute t = b**a mod a */ 044 if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) \{ -045 goto __T; +045 goto LBL_T; 046 \} 047 048 /* is it equal to b? */ @@ -10556,7 +10559,7 @@ determine the result. 051 \} 052 053 err = MP_OKAY; -054 __T:mp_clear (&t); +054 LBL_T:mp_clear (&t); 055 return err; 056 \} 057 #endif @@ -10638,12 +10641,12 @@ composite then it is \textit{probably} prime. 039 return err; 040 \} 041 if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) \{ -042 goto __N1; +042 goto LBL_N1; 043 \} 044 045 /* set 2**s * r = n1 */ 046 if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) \{ -047 goto __N1; +047 goto LBL_N1; 048 \} 049 050 /* count the number of least significant bits @@ -10653,15 +10656,15 @@ composite then it is \textit{probably} prime. 054 055 /* now divide n - 1 by 2**s */ 056 if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) \{ -057 goto __R; +057 goto LBL_R; 058 \} 059 060 /* compute y = b**r mod a */ 061 if ((err = mp_init (&y)) != MP_OKAY) \{ -062 goto __R; +062 goto LBL_R; 063 \} 064 if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) \{ -065 goto __Y; +065 goto LBL_Y; 066 \} 067 068 /* if y != 1 and y != n1 do */ @@ -10670,12 +10673,12 @@ composite then it is \textit{probably} prime. 071 /* while j <= s-1 and y != n1 */ 072 while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) \{ 073 if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) \{ -074 goto __Y; +074 goto LBL_Y; 075 \} 076 077 /* if y == 1 then composite */ 078 if (mp_cmp_d (&y, 1) == MP_EQ) \{ -079 goto __Y; +079 goto LBL_Y; 080 \} 081 082 ++j; @@ -10683,15 +10686,15 @@ composite then it is \textit{probably} prime. 084 085 /* if y != n1 then composite */ 086 if (mp_cmp (&y, &n1) != MP_EQ) \{ -087 goto __Y; +087 goto LBL_Y; 088 \} 089 \} 090 091 /* probably prime now */ 092 *result = MP_YES; -093 __Y:mp_clear (&y); -094 __R:mp_clear (&r); -095 __N1:mp_clear (&n1); +093 LBL_Y:mp_clear (&y); +094 LBL_R:mp_clear (&r); +095 LBL_N1:mp_clear (&n1); 096 return err; 097 \} 098 #endif diff --git a/tommath_class.h b/tommath_class.h index 2a17d43..53bfa31 100644 --- a/tommath_class.h +++ b/tommath_class.h @@ -242,6 +242,7 @@ #define BN_MP_INIT_MULTI_C #define BN_MP_SET_C #define BN_MP_COUNT_BITS_C + #define BN_MP_ABS_C #define BN_MP_MUL_2D_C #define BN_MP_CMP_C #define BN_MP_SUB_C