Skip to content

Commit eed6765

Browse files
Tom St Denissjaeckel
Tom St Denis
authored andcommitted
added libtommath-0.23
1 parent 4c1d3f0 commit eed6765

36 files changed

+600
-243
lines changed

bn.pdf

363 Bytes
Binary file not shown.

bn.tex

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
\documentclass[]{article}
22
\begin{document}
33

4-
\title{LibTomMath v0.22 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
4+
\title{LibTomMath v0.23 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
55
\author{Tom St Denis \\ tomstdenis@iahu.ca}
66
\maketitle
77
\newpage

bn_fast_mp_montgomery_reduce.c

+2-3
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
6464
* that W[ix-1] have the carry cleared (see after the inner loop)
6565
*/
6666
register mp_digit mu;
67-
mu = (((mp_digit) (W[ix] & MP_MASK)) * rho) & MP_MASK;
67+
mu = ((W[ix] & MP_MASK) * rho) & MP_MASK;
6868

6969
/* a = a + mu * m * b**i
7070
*
@@ -93,15 +93,14 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
9393

9494
/* inner loop */
9595
for (iy = 0; iy < n->used; iy++) {
96-
*_W++ += ((mp_word) mu) * ((mp_word) * tmpn++);
96+
*_W++ += ((mp_word)mu) * ((mp_word)*tmpn++);
9797
}
9898
}
9999

100100
/* now fix carry for next digit, W[ix+1] */
101101
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
102102
}
103103

104-
105104
{
106105
register mp_digit *tmpx;
107106
register mp_word *_W, *_W1;

bn_fast_s_mp_mul_digs.c

+10-3
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
8181
pb = MIN (b->used, digs - ix);
8282

8383
for (iy = 0; iy < pb; iy++) {
84-
*_W++ += ((mp_word) tmpx) * ((mp_word) * tmpy++);
84+
*_W++ += ((mp_word)tmpx) * ((mp_word)*tmpy++);
8585
}
8686
}
8787

@@ -104,20 +104,27 @@ fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
104104
* from N*(N+N*c)==N**2 + c*N**2 to N**2 + N*c where c is the
105105
* cost of the shifting. On very small numbers this is slower
106106
* but on most cryptographic size numbers it is faster.
107+
*
108+
* In this particular implementation we feed the carries from
109+
* behind which means when the loop terminates we still have one
110+
* last digit to copy
107111
*/
108112
tmpc = c->dp;
109113
for (ix = 1; ix < digs; ix++) {
114+
/* forward the carry from the previous temp */
110115
W[ix] += (W[ix - 1] >> ((mp_word) DIGIT_BIT));
116+
117+
/* now extract the previous digit [below the carry] */
111118
*tmpc++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
112119
}
120+
/* fetch the last digit */
113121
*tmpc++ = (mp_digit) (W[digs - 1] & ((mp_word) MP_MASK));
114122

115-
/* clear unused */
123+
/* clear unused digits [that existed in the old copy of c] */
116124
for (; ix < olduse; ix++) {
117125
*tmpc++ = 0;
118126
}
119127
}
120-
121128
mp_clamp (c);
122129
return MP_OKAY;
123130
}

bn_fast_s_mp_mul_high_digs.c

+6-3
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
7171

7272
/* compute column products for digits above the minimum */
7373
for (; iy < pb; iy++) {
74-
*_W++ += ((mp_word) tmpx) * ((mp_word) * tmpy++);
74+
*_W++ += ((mp_word) tmpx) * ((mp_word)*tmpy++);
7575
}
7676
}
7777
}
@@ -80,12 +80,15 @@ fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
8080
oldused = c->used;
8181
c->used = newused;
8282

83-
/* now convert the array W downto what we need */
83+
/* now convert the array W downto what we need
84+
*
85+
* See comments in bn_fast_s_mp_mul_digs.c
86+
*/
8487
for (ix = digs + 1; ix < newused; ix++) {
8588
W[ix] += (W[ix - 1] >> ((mp_word) DIGIT_BIT));
8689
c->dp[ix - 1] = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
8790
}
88-
c->dp[(pa + pb + 1) - 1] = (mp_digit) (W[(pa + pb + 1) - 1] & ((mp_word) MP_MASK));
91+
c->dp[newused - 1] = (mp_digit) (W[newused - 1] & ((mp_word) MP_MASK));
8992

9093
for (; ix < oldused; ix++) {
9194
c->dp[ix] = 0;

bn_fast_s_mp_sqr.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
6868
* for a particular column only once which means that
6969
* there is no need todo a double precision addition
7070
*/
71-
W2[ix + ix] = ((mp_word) a->dp[ix]) * ((mp_word) a->dp[ix]);
71+
W2[ix + ix] = ((mp_word)a->dp[ix]) * ((mp_word)a->dp[ix]);
7272

7373
{
7474
register mp_digit tmpx, *tmpy;
@@ -86,7 +86,7 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
8686

8787
/* inner products */
8888
for (iy = ix + 1; iy < pa; iy++) {
89-
*_W++ += ((mp_word) tmpx) * ((mp_word) * tmpy++);
89+
*_W++ += ((mp_word)tmpx) * ((mp_word)*tmpy++);
9090
}
9191
}
9292
}

bn_mp_clear.c

+1-2
Original file line numberDiff line numberDiff line change
@@ -19,15 +19,14 @@ void
1919
mp_clear (mp_int * a)
2020
{
2121
if (a->dp != NULL) {
22-
2322
/* first zero the digits */
2423
memset (a->dp, 0, sizeof (mp_digit) * a->used);
2524

2625
/* free ram */
2726
free (a->dp);
2827

2928
/* reset members to make debugging easier */
30-
a->dp = NULL;
29+
a->dp = NULL;
3130
a->alloc = a->used = 0;
3231
}
3332
}

bn_mp_div_d.c

+41-3
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,19 @@
1414
*/
1515
#include <tommath.h>
1616

17+
static int s_is_power_of_two(mp_digit b, int *p)
18+
{
19+
int x;
20+
21+
for (x = 1; x < DIGIT_BIT; x++) {
22+
if (b == (((mp_digit)1)<<x)) {
23+
*p = x;
24+
return 1;
25+
}
26+
}
27+
return 0;
28+
}
29+
1730
/* single digit division (based on routine from MPI) */
1831
int
1932
mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
@@ -22,15 +35,40 @@ mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
2235
mp_word w;
2336
mp_digit t;
2437
int res, ix;
25-
38+
39+
/* cannot divide by zero */
2640
if (b == 0) {
2741
return MP_VAL;
2842
}
29-
43+
44+
/* quick outs */
45+
if (b == 1 || mp_iszero(a) == 1) {
46+
if (d != NULL) {
47+
*d = 0;
48+
}
49+
if (c != NULL) {
50+
return mp_copy(a, c);
51+
}
52+
return MP_OKAY;
53+
}
54+
55+
/* power of two ? */
56+
if (s_is_power_of_two(b, &ix) == 1) {
57+
if (d != NULL) {
58+
*d = a->dp[0] & ((1<<ix) - 1);
59+
}
60+
if (c != NULL) {
61+
return mp_div_2d(a, ix, c, NULL);
62+
}
63+
return MP_OKAY;
64+
}
65+
66+
/* three? */
3067
if (b == 3) {
3168
return mp_div_3(a, c, d);
3269
}
33-
70+
71+
/* no easy answer [c'est la vie]. Just division */
3472
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
3573
return res;
3674
}

bn_mp_exptmod_fast.c

-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,6 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
8282
}
8383
}
8484

85-
8685
/* determine and setup reduction code */
8786
if (redmode == 0) {
8887
/* now setup montgomery */

bn_mp_montgomery_reduce.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
4444

4545
for (ix = 0; ix < n->used; ix++) {
4646
/* mu = ai * m' mod b */
47-
mu = (x->dp[ix] * rho) & MP_MASK;
47+
mu = ((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK;
4848

4949
/* a = a + mu * m * b**i */
5050
{
@@ -61,7 +61,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
6161

6262
/* Multiply and add in place */
6363
for (iy = 0; iy < n->used; iy++) {
64-
r = ((mp_word) mu) * ((mp_word) * tmpn++) +
64+
r = ((mp_word)mu) * ((mp_word)*tmpn++) +
6565
((mp_word) u) + ((mp_word) * tmpx);
6666
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
6767
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));

bn_mp_mul_d.c

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
5050
u = 0;
5151
for (ix = 0; ix < pa; ix++) {
5252
/* compute product and carry sum for this term */
53-
r = ((mp_word) u) + ((mp_word) * tmpa++) * ((mp_word) b);
53+
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
5454

5555
/* mask off higher bits to get a single digit */
5656
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));

bn_mp_prime_fermat.c

+5
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,11 @@ mp_prime_fermat (mp_int * a, mp_int * b, int *result)
3131
/* default to fail */
3232
*result = 0;
3333

34+
/* ensure b > 1 */
35+
if (mp_cmp_d(b, 1) != MP_GT) {
36+
return MP_VAL;
37+
}
38+
3439
/* init t */
3540
if ((err = mp_init (&t)) != MP_OKAY) {
3641
return err;

bn_mp_prime_is_prime.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
/* performs a variable number of rounds of Miller-Rabin
1818
*
1919
* Probability of error after t rounds is no more than
20-
* (1/4)^t when 1 <= t <= 256
20+
* (1/4)^t when 1 <= t <= PRIME_SIZE
2121
*
2222
* Sets result to 1 if probably prime, 0 otherwise
2323
*/
@@ -31,7 +31,7 @@ mp_prime_is_prime (mp_int * a, int t, int *result)
3131
*result = 0;
3232

3333
/* valid value of t? */
34-
if (t < 1 || t > PRIME_SIZE) {
34+
if (t <= 0 || t > PRIME_SIZE) {
3535
return MP_VAL;
3636
}
3737

@@ -47,6 +47,8 @@ mp_prime_is_prime (mp_int * a, int t, int *result)
4747
if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
4848
return err;
4949
}
50+
51+
/* return if it was trivially divisible */
5052
if (res == 1) {
5153
return MP_OKAY;
5254
}

bn_mp_prime_miller_rabin.c

+11-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
3030
/* default */
3131
*result = 0;
3232

33+
/* ensure b > 1 */
34+
if (mp_cmp_d(b, 1) != MP_GT) {
35+
return MP_VAL;
36+
}
37+
3338
/* get n1 = a - 1 */
3439
if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
3540
return err;
@@ -42,8 +47,13 @@ mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
4247
if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) {
4348
goto __N1;
4449
}
45-
50+
51+
/* count the number of least significant bits
52+
* which are zero
53+
*/
4654
s = mp_cnt_lsb(&r);
55+
56+
/* now divide n - 1 by 2^s */
4757
if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) {
4858
goto __R;
4959
}

0 commit comments

Comments
 (0)