Skip to content

Commit 6e73234

Browse files
Tom St Denissjaeckel
Tom St Denis
authored andcommitted
added libtommath-0.26
1 parent c1da6aa commit 6e73234

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+725
-224
lines changed

bn.pdf

6 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.25 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
4+
\title{LibTomMath v0.26 \\ 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

+4-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,10 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
4545
register mp_word *_W;
4646
register mp_digit *tmpx;
4747

48-
_W = W;
48+
/* alias for the W[] array */
49+
_W = W;
50+
51+
/* alias for the digits of x*/
4952
tmpx = x->dp;
5053

5154
/* copy the digits of a into W[0..a->used-1] */

bn_mp_add_d.c

-2
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
4242
return res;
4343
}
4444

45-
4645
/* old number of used digits in c */
4746
oldused = c->used;
4847

@@ -76,7 +75,6 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
7675
/* set final carry */
7776
ix++;
7877
*tmpc++ = mu;
79-
8078
} else {
8179
/* a was negative and |a| < b */
8280
c->used = 1;

bn_mp_cmp.c

+6-6
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,12 @@ int
1919
mp_cmp (mp_int * a, mp_int * b)
2020
{
2121
/* compare based on sign */
22-
if (a->sign == MP_NEG && b->sign == MP_ZPOS) {
23-
return MP_LT;
24-
}
25-
26-
if (a->sign == MP_ZPOS && b->sign == MP_NEG) {
27-
return MP_GT;
22+
if (a->sign != b->sign) {
23+
if (a->sign == MP_NEG) {
24+
return MP_LT;
25+
} else {
26+
return MP_GT;
27+
}
2828
}
2929

3030
/* compare digits */

bn_mp_cmp_mag.c

+13-6
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,30 @@ int
1919
mp_cmp_mag (mp_int * a, mp_int * b)
2020
{
2121
int n;
22+
mp_digit *tmpa, *tmpb;
2223

2324
/* compare based on # of non-zero digits */
2425
if (a->used > b->used) {
2526
return MP_GT;
26-
}
27+
}
2728

2829
if (a->used < b->used) {
2930
return MP_LT;
3031
}
3132

33+
/* alias for a */
34+
tmpa = a->dp + (a->used - 1);
35+
36+
/* alias for b */
37+
tmpb = b->dp + (a->used - 1);
38+
3239
/* compare based on digits */
33-
for (n = a->used - 1; n >= 0; n--) {
34-
if (a->dp[n] > b->dp[n]) {
40+
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
41+
if (*tmpa > *tmpb) {
3542
return MP_GT;
36-
}
37-
38-
if (a->dp[n] < b->dp[n]) {
43+
}
44+
45+
if (*tmpa < *tmpb) {
3946
return MP_LT;
4047
}
4148
}

bn_mp_div.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -111,8 +111,9 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
111111

112112
/* step 3. for i from n down to (t + 1) */
113113
for (i = n; i >= (t + 1); i--) {
114-
if (i > x.used)
114+
if (i > x.used) {
115115
continue;
116+
}
116117

117118
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
118119
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */

bn_mp_dr_reduce.c

+4-2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* The modulus must be of a special format [see manual]
2626
*
2727
* Has been modified to use algorithm 7.10 from the LTM book instead
28+
*
29+
* Input x must be in the range 0 <= x <= (n-1)^2
2830
*/
2931
int
3032
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
@@ -63,10 +65,10 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
6365
*tmpx1++ = (mp_digit)(r & MP_MASK);
6466
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
6567
}
66-
68+
6769
/* set final carry */
6870
*tmpx1++ = mu;
69-
71+
7072
/* zero words above m */
7173
for (i = m + 1; i < x->used; i++) {
7274
*tmpx1++ = 0;

bn_mp_init_multi.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,5 @@ int mp_init_multi(mp_int *mp, ...)
4949
}
5050
va_end(args);
5151
return res; /* Assumed ok, if error flagged above. */
52-
}
52+
}
53+

bn_mp_lcm.c

+24-11
Original file line numberDiff line numberDiff line change
@@ -14,29 +14,42 @@
1414
*/
1515
#include <tommath.h>
1616

17-
/* computes least common multiple as a*b/(a, b) */
17+
/* computes least common multiple as |a*b|/(a, b) */
1818
int
1919
mp_lcm (mp_int * a, mp_int * b, mp_int * c)
2020
{
2121
int res;
22-
mp_int t;
22+
mp_int t1, t2;
2323

2424

25-
if ((res = mp_init (&t)) != MP_OKAY) {
25+
if ((res = mp_init_multi (&t1, &t2, NULL)) != MP_OKAY) {
2626
return res;
2727
}
2828

29-
if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
30-
mp_clear (&t);
31-
return res;
29+
/* t1 = get the GCD of the two inputs */
30+
if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) {
31+
goto __T;
3232
}
3333

34-
if ((res = mp_gcd (a, b, c)) != MP_OKAY) {
35-
mp_clear (&t);
36-
return res;
34+
/* divide the smallest by the GCD */
35+
if (mp_cmp_mag(a, b) == MP_LT) {
36+
/* store quotient in t2 such that t2 * b is the LCM */
37+
if ((res = mp_div(a, &t1, &t2, NULL)) != MP_OKAY) {
38+
goto __T;
39+
}
40+
res = mp_mul(b, &t2, c);
41+
} else {
42+
/* store quotient in t2 such that t2 * a is the LCM */
43+
if ((res = mp_div(b, &t1, &t2, NULL)) != MP_OKAY) {
44+
goto __T;
45+
}
46+
res = mp_mul(a, &t2, c);
3747
}
3848

39-
res = mp_div (&t, c, c, NULL);
40-
mp_clear (&t);
49+
/* fix the sign to positive */
50+
c->sign = MP_ZPOS;
51+
52+
__T:
53+
mp_clear_multi (&t1, &t2, NULL);
4154
return res;
4255
}

bn_mp_montgomery_reduce.c

+28-4
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,14 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
4343
x->used = digs;
4444

4545
for (ix = 0; ix < n->used; ix++) {
46-
/* mu = ai * m' mod b */
46+
/* mu = ai * rho mod b
47+
*
48+
* The value of rho must be precalculated via
49+
* bn_mp_montgomery_setup() such that
50+
* it equals -1/n0 mod b this allows the
51+
* following inner loop to reduce the
52+
* input one digit at a time
53+
*/
4754
mu = ((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK;
4855

4956
/* a = a + mu * m * b**i */
@@ -52,21 +59,31 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
5259
register mp_digit *tmpn, *tmpx, u;
5360
register mp_word r;
5461

55-
/* aliases */
62+
/* alias for digits of the modulus */
5663
tmpn = n->dp;
64+
65+
/* alias for the digits of x [the input] */
5766
tmpx = x->dp + ix;
5867

5968
/* set the carry to zero */
6069
u = 0;
6170

6271
/* Multiply and add in place */
6372
for (iy = 0; iy < n->used; iy++) {
73+
/* compute product and sum */
6474
r = ((mp_word)mu) * ((mp_word)*tmpn++) +
6575
((mp_word) u) + ((mp_word) * tmpx);
76+
77+
/* get carry */
6678
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
79+
80+
/* fix digit */
6781
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
6882
}
69-
/* propagate carries */
83+
/* At this point the ix'th digit of x should be zero */
84+
85+
86+
/* propagate carries upwards as required*/
7087
while (u) {
7188
*tmpx += u;
7289
u = *tmpx >> DIGIT_BIT;
@@ -75,11 +92,18 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
7592
}
7693
}
7794

95+
/* at this point the n.used'th least
96+
* significant digits of x are all zero
97+
* which means we can shift x to the
98+
* right by n.used digits and the
99+
* residue is unchanged.
100+
*/
101+
78102
/* x = x/b**n.used */
79103
mp_clamp(x);
80104
mp_rshd (x, n->used);
81105

82-
/* if A >= m then A = A - m */
106+
/* if x >= n then x = x - n */
83107
if (mp_cmp_mag (x, n) != MP_LT) {
84108
return s_mp_sub (x, n, x);
85109
}

bn_mp_neg.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ mp_neg (mp_int * a, mp_int * b)
2222
if ((res = mp_copy (a, b)) != MP_OKAY) {
2323
return res;
2424
}
25-
b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
25+
if (mp_iszero(b) != 1) {
26+
b->sign = (a->sign == MP_ZPOS) ? MP_NEG : MP_ZPOS;
27+
}
2628
return MP_OKAY;
2729
}

bn_mp_read_signed_bin.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,17 @@ mp_read_signed_bin (mp_int * a, unsigned char *b, int c)
2020
{
2121
int res;
2222

23+
/* read magnitude */
2324
if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) {
2425
return res;
2526
}
26-
a->sign = ((b[0] == (unsigned char) 0) ? MP_ZPOS : MP_NEG);
27+
28+
/* first byte is 0 for positive, non-zero for negative */
29+
if (b[0] == 0) {
30+
a->sign = MP_ZPOS;
31+
} else {
32+
a->sign = MP_NEG;
33+
}
34+
2735
return MP_OKAY;
2836
}

bn_mp_read_unsigned_bin.c

+11
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,18 @@ int
1919
mp_read_unsigned_bin (mp_int * a, unsigned char *b, int c)
2020
{
2121
int res;
22+
23+
/* make sure there are at least two digits */
24+
if (a->alloc < 2) {
25+
if ((res = mp_grow(a, 2)) != MP_OKAY) {
26+
return res;
27+
}
28+
}
29+
30+
/* zero the int */
2231
mp_zero (a);
32+
33+
/* read the bytes in */
2334
while (c-- > 0) {
2435
if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) {
2536
return res;

bn_mp_set_int.c

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ mp_set_int (mp_int * a, unsigned int b)
2121
int x, res;
2222

2323
mp_zero (a);
24+
2425
/* set four bits at a time */
2526
for (x = 0; x < 8; x++) {
2627
/* shift the number up four bits */

bn_s_mp_mul_digs.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,15 @@ s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
6161
/* compute the columns of the output and propagate the carry */
6262
for (iy = 0; iy < pb; iy++) {
6363
/* compute the column as a mp_word */
64-
r = ((mp_word) *tmpt) +
65-
((mp_word)tmpx) * ((mp_word)*tmpy++) +
66-
((mp_word) u);
64+
r = ((mp_word)*tmpt) +
65+
((mp_word)tmpx) * ((mp_word)*tmpy++) +
66+
((mp_word) u);
6767

6868
/* the new column is the lower part of the result */
6969
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
7070

7171
/* get the carry word from the result */
72-
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
72+
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
7373
}
7474
/* set carry if it is placed below digs */
7575
if (ix + iy < digs) {

bn_s_mp_mul_high_digs.c

+4-3
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
2626
mp_word r;
2727
mp_digit tmpx, *tmpt, *tmpy;
2828

29-
3029
/* can we use the fast multiplier? */
3130
if (((a->used + b->used + 1) < MP_WARRAY)
3231
&& MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
@@ -55,13 +54,15 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
5554

5655
for (iy = digs - ix; iy < pb; iy++) {
5756
/* calculate the double precision result */
58-
r = ((mp_word) * tmpt) + ((mp_word)tmpx) * ((mp_word)*tmpy++) + ((mp_word) u);
57+
r = ((mp_word)*tmpt) +
58+
((mp_word)tmpx) * ((mp_word)*tmpy++) +
59+
((mp_word) u);
5960

6061
/* get the lower part */
6162
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
6263

6364
/* carry the carry */
64-
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
65+
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
6566
}
6667
*tmpt = u;
6768
}

bn_s_mp_sqr.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,19 @@ s_mp_sqr (mp_int * a, mp_int * b)
5454
/* now calculate the double precision result, note we use
5555
* addition instead of *2 since it's easier to optimize
5656
*/
57-
r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
57+
r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
5858

5959
/* store lower part */
6060
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
6161

6262
/* get carry */
63-
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
63+
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
6464
}
6565
/* propagate upwards */
6666
while (u != ((mp_digit) 0)) {
67-
r = ((mp_word) * tmpt) + ((mp_word) u);
67+
r = ((mp_word) *tmpt) + ((mp_word) u);
6868
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
69-
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
69+
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
7070
}
7171
}
7272

changes.txt

+10
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,13 @@
1+
Aug 29th, 2003
2+
v0.26 -- Fixed typo that caused warning with GCC 3.2
3+
-- Martin Marcel noticed a bug in mp_neg() that allowed negative zeroes.
4+
Also, Martin is the fellow who noted the bugs in mp_gcd() of 0.24/0.25.
5+
-- Martin Marcel noticed an optimization [and slight bug] in mp_lcm().
6+
-- Added fix to mp_read_unsigned_bin to prevent a buffer overflow.
7+
-- Beefed up the comments in the baseline multipliers [and montgomery]
8+
-- Added "mont" demo to the makefile.msvc in etc/
9+
-- Optimized sign compares in mp_cmp from 4 to 2 cases.
10+
111
Aug 4th, 2003
212
v0.25 -- Fix to mp_gcd again... oops (0,-a) == (-a, 0) == a
313
-- Fix to mp_clear which didn't reset the sign [Greg Rose]

0 commit comments

Comments
 (0)