Permalink
Browse files

added libtommath-0.19

  • Loading branch information...
1 parent 0ef44ce commit ef490f30f6413fed7de880643c9a369155d621be Tom St Denis committed with sjaeckel Jun 6, 2003
Showing with 15,628 additions and 14,371 deletions.
  1. BIN bn.pdf
  2. +1 −1 bn.tex
  3. +3 −3 bn_mp_exptmod.c
  4. +1 −0 bn_mp_exptmod_fast.c
  5. +2 −0 bn_mp_karatsuba_mul.c
  6. +3 −2 bn_mp_montgomery_reduce.c
  7. +2 −1 bn_mp_reduce_is_2k.c
  8. +11 −6 bn_mp_toom_mul.c
  9. +11 −3 bn_radix.c
  10. +9 −4 bn_s_mp_exptmod.c
  11. +6 −6 bn_s_mp_sqr.c
  12. +7 −0 changes.txt
  13. +6 −0 etc/drprimes.txt
  14. +1 −1 etc/mersenne.c
  15. +41 −14 etc/tune.c
  16. +1 −1 makefile
  17. +1 −1 makefile.bcc
  18. 0 demo/test.c → poster.out
  19. BIN poster.pdf
  20. +8 −4 poster.tex
  21. +6,758 −6,735 pre_gen/mpi.c
  22. +143 −0 tommath.out
  23. +652 −290 tommath.src
  24. +7,961 −7,299 tommath.tex
View
Binary file not shown.
View
@@ -1,7 +1,7 @@
\documentclass[]{article}
\begin{document}
-\title{LibTomMath v0.18 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
+\title{LibTomMath v0.19 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
\author{Tom St Denis \\ tomstdenis@iahu.ca}
\maketitle
\newpage
View
@@ -64,9 +64,9 @@ mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
if (dr == 0) {
dr = mp_reduce_is_2k(P) << 1;
}
-
- /* if the modulus is odd use the fast method */
- if ((mp_isodd (P) == 1 || dr != 0) && P->used > 4) {
+
+ /* if the modulus is odd or dr != 0 use the fast method */
+ if (mp_isodd (P) == 1 || dr != 0) {
return mp_exptmod_fast (G, X, P, Y, dr);
} else {
return s_mp_exptmod (G, X, P, Y);
@@ -80,6 +80,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
if (((P->used * 2 + 1) < MP_WARRAY) &&
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
redux = fast_mp_montgomery_reduce;
+
} else {
/* use slower baselien method */
redux = mp_montgomery_reduce;
@@ -49,6 +49,7 @@ mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
int B, err;
+ /* default the return code to an error */
err = MP_MEM;
/* min # of digits */
@@ -149,6 +150,7 @@ mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
if (mp_add (&t1, &x1y1, c) != MP_OKAY)
goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
+ /* Algorithm succeeded set the return code to MP_OKAY */
err = MP_OKAY;
X1Y1:mp_clear (&x1y1);
@@ -68,14 +68,15 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
}
/* propagate carries */
while (u) {
- *tmpx += u;
- u = *tmpx >> DIGIT_BIT;
+ *tmpx += u;
+ u = *tmpx >> DIGIT_BIT;
*tmpx++ &= MP_MASK;
}
}
}
/* x = x/b**n.used */
+ mp_clamp(x);
mp_rshd (x, n->used);
/* if A >= m then A = A - m */
@@ -27,7 +27,8 @@ mp_reduce_is_2k(mp_int *a)
} else if (a->used > 1) {
iy = mp_count_bits(a);
for (ix = DIGIT_BIT; ix < iy; ix++) {
- if ((a->dp[ix/DIGIT_BIT] & ((mp_digit)1 << (mp_digit)(ix % DIGIT_BIT))) == 0) {
+ if ((a->dp[ix/DIGIT_BIT] &
+ ((mp_digit)1 << (mp_digit)(ix % DIGIT_BIT))) == 0) {
return 0;
}
}
View
@@ -14,22 +14,24 @@
*/
#include <tommath.h>
-/* multiplication using Toom-Cook 3-way algorithm */
+/* multiplication using the Toom-Cook 3-way algorithm */
int
mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
{
mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
int res, B;
/* init temps */
- if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &b0, &b1, &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
+ if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
+ &a0, &a1, &a2, &b0, &b1,
+ &b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
return res;
}
/* B */
B = MIN(a->used, b->used) / 3;
- /* a = a2 * B^2 + a1 * B + a0 */
+ /* a = a2 * B**2 + a1 * B + a0 */
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
goto ERR;
}
@@ -45,7 +47,7 @@ mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
}
mp_rshd(&a2, B*2);
- /* b = b2 * B^2 + b1 * B + b0 */
+ /* b = b2 * B**2 + b1 * B + b0 */
if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
goto ERR;
}
@@ -159,7 +161,8 @@ mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
16 8 4 2 1
1 0 0 0 0
- using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication
+ using 12 subtractions, 4 shifts,
+ 2 small divisions and 1 small multiplication
*/
/* r1 - r4 */
@@ -262,7 +265,9 @@ mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
}
ERR:
- mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &b0, &b1, &b2, &tmp1, &tmp2, NULL);
+ mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
+ &a0, &a1, &a2, &b0, &b1,
+ &b2, &tmp1, &tmp2, NULL);
return res;
}
View
@@ -40,16 +40,16 @@ mp_read_radix (mp_int * a, char *str, int radix)
ch = (char) ((radix < 36) ? toupper (*str) : *str);
for (y = 0; y < 64; y++) {
if (ch == s_rmap[y]) {
- break;
+ break;
}
}
if (y < radix) {
if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
- return res;
+ return res;
}
if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) {
- return res;
+ return res;
}
} else {
break;
@@ -72,6 +72,14 @@ mp_toradix (mp_int * a, char *str, int radix)
if (radix < 2 || radix > 64) {
return MP_VAL;
}
+
+ /* quick out if its zero */
+ if (mp_iszero(a) == 1) {
+ *str++ = '0';
+ *str = '\0';
+ return MP_OKAY;
+ }
+
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
return res;
View
@@ -65,21 +65,26 @@ s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
/* create M table
*
- * The M table contains powers of the input base, e.g. M[x] = G**x mod P
+ * The M table contains powers of the base,
+ * e.g. M[x] = G**x mod P
*
- * The first half of the table is not computed though accept for M[0] and M[1]
+ * The first half of the table is not
+ * computed though accept for M[0] and M[1]
*/
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
goto __MU;
}
- /* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
+ /* compute the value at M[1<<(winsize-1)] by squaring
+ * M[1] (winsize-1) times
+ */
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
goto __MU;
}
for (x = 0; x < (winsize - 1); x++) {
- if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
+ if ((err = mp_sqr (&M[1 << (winsize - 1)],
+ &M[1 << (winsize - 1)])) != MP_OKAY) {
goto __MU;
}
if ((err = mp_reduce (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
View
@@ -24,19 +24,19 @@ s_mp_sqr (mp_int * a, mp_int * b)
mp_digit u, tmpx, *tmpt;
pa = a->used;
- if ((res = mp_init_size (&t, pa + pa + 1)) != MP_OKAY) {
+ if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) {
return res;
}
- t.used = pa + pa + 1;
+ t.used = 2*pa + 1;
for (ix = 0; ix < pa; ix++) {
/* first calculate the digit at 2*ix */
/* calculate double precision result */
- r = ((mp_word) t.dp[ix + ix]) +
+ r = ((mp_word) t.dp[2*ix]) +
((mp_word) a->dp[ix]) * ((mp_word) a->dp[ix]);
/* store lower part in result */
- t.dp[ix + ix] = (mp_digit) (r & ((mp_word) MP_MASK));
+ t.dp[2*ix] = (mp_digit) (r & ((mp_word) MP_MASK));
/* get the carry */
u = (r >> ((mp_word) DIGIT_BIT));
@@ -45,14 +45,14 @@ s_mp_sqr (mp_int * a, mp_int * b)
tmpx = a->dp[ix];
/* alias for where to store the results */
- tmpt = t.dp + (ix + ix + 1);
+ tmpt = t.dp + (2*ix + 1);
for (iy = ix + 1; iy < pa; iy++) {
/* first calculate the product */
r = ((mp_word) tmpx) * ((mp_word) a->dp[iy]);
/* now calculate the double precision result, note we use
- * addition instead of *2 since its easier to optimize
+ * addition instead of *2 since it's easier to optimize
*/
r = ((mp_word) * tmpt) + r + r + ((mp_word) u);
View
@@ -1,3 +1,10 @@
+June 6th, 2003
+v0.19 -- Fixed a bug in mp_montgomery_reduce() which was introduced when I tweaked mp_rshd() in the previous release.
+ Essentially the digits were not trimmed before the compare which cause a subtraction to occur all the time.
+ -- Fixed up etc/tune.c a bit to stop testing new cutoffs after 16 failures [to find more optimal points].
+ Brute force ho!
+
+
May 29th, 2003
v0.18 -- Fixed a bug in s_mp_sqr which would handle carries properly just not very elegantly.
(e.g. correct result, just bad looking code)
View
@@ -0,0 +1,6 @@
+224-bit prime:
+p == 26959946667150639794667015087019630673637144422540572481103341844143
+
+532-bit prime:
+p == 14059105607947488696282932836518693308967803494693489478439861164411992439598399594747002144074658928593502845729752797260025831423419686528151609940203368691747
+
View
@@ -57,7 +57,7 @@ is_mersenne (long s, int *pp)
/* if u == 0 then its prime */
if (mp_iszero (&u) == 1) {
- mp_prime_is_prime(&n, 3, pp);
+ mp_prime_is_prime(&n, 8, pp);
if (*pp != 1) printf("FAILURE\n");
}
Oops, something went wrong. Retry.

0 comments on commit ef490f3

Please sign in to comment.