Skip to content

Commit 49bef06

Browse files
Tom St Denissjaeckel
Tom St Denis
authored andcommitted
added libtommath-0.21
1 parent 0fe7a2d commit 49bef06

24 files changed

+153
-213
lines changed

bn.pdf

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
124124
_W = W + n->used;
125125

126126
for (ix = 0; ix < n->used + 1; ix++) {
127-
*tmpx++ = *_W++ & ((mp_word) MP_MASK);
127+
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
128128
}
129129

130130
/* zero oldused digits, if the input a was larger than

bn_mp_div.c

+25-14
Original file line numberDiff line numberDiff line change
@@ -14,22 +14,25 @@
1414
*/
1515
#include <tommath.h>
1616

17-
/* integer signed division. c*b + d == a [e.g. a/b, c=quotient, d=remainder]
17+
/* integer signed division.
18+
* c*b + d == a [e.g. a/b, c=quotient, d=remainder]
1819
* HAC pp.598 Algorithm 14.20
1920
*
20-
* Note that the description in HAC is horribly incomplete. For example,
21-
* it doesn't consider the case where digits are removed from 'x' in the inner
22-
* loop. It also doesn't consider the case that y has fewer than three digits, etc..
21+
* Note that the description in HAC is horribly
22+
* incomplete. For example, it doesn't consider
23+
* the case where digits are removed from 'x' in
24+
* the inner loop. It also doesn't consider the
25+
* case that y has fewer than three digits, etc..
2326
*
24-
* The overall algorithm is as described as 14.20 from HAC but fixed to treat these cases.
27+
* The overall algorithm is as described as
28+
* 14.20 from HAC but fixed to treat these cases.
2529
*/
2630
int
2731
mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
2832
{
2933
mp_int q, x, y, t1, t2;
3034
int res, n, t, i, norm, neg;
3135

32-
3336
/* is divisor zero ? */
3437
if (mp_iszero (b) == 1) {
3538
return MP_VAL;
@@ -73,7 +76,7 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
7376
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
7477
x.sign = y.sign = MP_ZPOS;
7578

76-
/* normalize both x and y, ensure that y >= b/2, [b == 2^DIGIT_BIT] */
79+
/* normalize both x and y, ensure that y >= b/2, [b == 2**DIGIT_BIT] */
7780
norm = mp_count_bits(&y) % DIGIT_BIT;
7881
if (norm < (int)(DIGIT_BIT-1)) {
7982
norm = (DIGIT_BIT-1) - norm;
@@ -91,8 +94,8 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
9194
n = x.used - 1;
9295
t = y.used - 1;
9396

94-
/* step 2. while (x >= y*b^n-t) do { q[n-t] += 1; x -= y*b^{n-t} } */
95-
if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b^{n-t} */
97+
/* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
98+
if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
9699
goto __Y;
97100
}
98101

@@ -111,7 +114,8 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
111114
if (i > x.used)
112115
continue;
113116

114-
/* step 3.1 if xi == yt then set q{i-t-1} to b-1, otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
117+
/* step 3.1 if xi == yt then set q{i-t-1} to b-1,
118+
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
115119
if (x.dp[i] == y.dp[t]) {
116120
q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
117121
} else {
@@ -124,7 +128,11 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
124128
q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
125129
}
126130

127-
/* step 3.2 while (q{i-t-1} * (yt * b + y{t-1})) > xi * b^2 + xi-1 * b + xi-2 do q{i-t-1} -= 1; */
131+
/* while (q{i-t-1} * (yt * b + y{t-1})) >
132+
xi * b**2 + xi-1 * b + xi-2
133+
134+
do q{i-t-1} -= 1;
135+
*/
128136
q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK;
129137
do {
130138
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK;
@@ -145,7 +153,7 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
145153
t2.used = 3;
146154
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
147155

148-
/* step 3.3 x = x - q{i-t-1} * y * b^{i-t-1} */
156+
/* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
149157
if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) {
150158
goto __Y;
151159
}
@@ -158,7 +166,7 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
158166
goto __Y;
159167
}
160168

161-
/* step 3.4 if x < 0 then { x = x + y*b^{i-t-1}; q{i-t-1} -= 1; } */
169+
/* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
162170
if (x.sign == MP_NEG) {
163171
if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
164172
goto __Y;
@@ -174,7 +182,10 @@ mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
174182
}
175183
}
176184

177-
/* now q is the quotient and x is the remainder [which we have to normalize] */
185+
/* now q is the quotient and x is the remainder
186+
* [which we have to normalize]
187+
*/
188+
178189
/* get sign before writing to c */
179190
x.sign = a->sign;
180191

bn_mp_div_3.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
4646
} else {
4747
t = 0;
4848
}
49-
q.dp[ix] = t;
49+
q.dp[ix] = (mp_digit)t;
5050
}
5151

5252
if (d != NULL) {
53-
*d = w;
53+
*d = (mp_digit)w;
5454
}
5555

5656
if (c != NULL) {

bn_mp_div_d.c

+5-4
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ int
1919
mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
2020
{
2121
mp_int q;
22-
mp_word w, t;
22+
mp_word w;
23+
mp_digit t;
2324
int res, ix;
2425

2526
if (b == 0) {
@@ -41,16 +42,16 @@ mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
4142
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
4243

4344
if (w >= b) {
44-
t = w / b;
45+
t = (mp_digit)(w / b);
4546
w = w % b;
4647
} else {
4748
t = 0;
4849
}
49-
q.dp[ix] = t;
50+
q.dp[ix] = (mp_digit)t;
5051
}
5152

5253
if (d != NULL) {
53-
*d = w;
54+
*d = (mp_digit)w;
5455
}
5556

5657
if (c != NULL) {

bn_mp_dr_reduce.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
6060
/* compute (x mod B**m) + mp * [x/B**m] inline and inplace */
6161
for (i = 0; i < m; i++) {
6262
r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
63-
*tmpx1++ = r & MP_MASK;
64-
mu = r >> ((mp_word)DIGIT_BIT);
63+
*tmpx1++ = (mp_digit)(r & MP_MASK);
64+
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
6565
}
6666

6767
/* set final carry */

bn_mp_montgomery_reduce.c

+4-4
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,10 @@ 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++) +
65-
((mp_word) u) + ((mp_word) * tmpx);
66-
u = (r >> ((mp_word) DIGIT_BIT));
67-
*tmpx++ = (r & ((mp_word) MP_MASK));
64+
r = ((mp_word) mu) * ((mp_word) * tmpn++) +
65+
((mp_word) u) + ((mp_word) * tmpx);
66+
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
67+
*tmpx++ = (mp_digit)(r & ((mp_word) MP_MASK));
6868
}
6969
/* propagate carries */
7070
while (u) {

bn_mp_mul_d.c

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
3333

3434
/* set the new temporary used count */
3535
c->used = pa + 1;
36+
c->sign = a->sign;
3637

3738
{
3839
register mp_digit u, *tmpa, *tmpc;

bn_mp_n_root.c

+21-13
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,13 @@
1616

1717
/* find the n'th root of an integer
1818
*
19-
* Result found such that (c)^b <= a and (c+1)^b > a
19+
* Result found such that (c)**b <= a and (c+1)**b > a
2020
*
21-
* This algorithm uses Newton's approximation x[i+1] = x[i] - f(x[i])/f'(x[i])
22-
* which will find the root in log(N) time where each step involves a fair bit. This
23-
* is not meant to find huge roots [square and cube at most].
21+
* This algorithm uses Newton's approximation
22+
* x[i+1] = x[i] - f(x[i])/f'(x[i])
23+
* which will find the root in log(N) time where
24+
* each step involves a fair bit. This is not meant to
25+
* find huge roots [square and cube, etc].
2426
*/
2527
int
2628
mp_n_root (mp_int * a, mp_digit b, mp_int * c)
@@ -58,33 +60,39 @@ mp_n_root (mp_int * a, mp_digit b, mp_int * c)
5860
goto __T3;
5961
}
6062

61-
/* t2 = t1 - ((t1^b - a) / (b * t1^(b-1))) */
62-
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) { /* t3 = t1^(b-1) */
63+
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
64+
65+
/* t3 = t1**(b-1) */
66+
if ((res = mp_expt_d (&t1, b - 1, &t3)) != MP_OKAY) {
6367
goto __T3;
6468
}
6569

6670
/* numerator */
67-
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) { /* t2 = t1^b */
71+
/* t2 = t1**b */
72+
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
6873
goto __T3;
6974
}
7075

71-
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) { /* t2 = t1^b - a */
76+
/* t2 = t1**b - a */
77+
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
7278
goto __T3;
7379
}
7480

75-
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) { /* t3 = t1^(b-1) * b */
81+
/* denominator */
82+
/* t3 = t1**(b-1) * b */
83+
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
7684
goto __T3;
7785
}
7886

79-
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) { /* t3 = (t1^b - a)/(b * t1^(b-1)) */
87+
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
88+
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
8089
goto __T3;
8190
}
8291

8392
if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) {
8493
goto __T3;
8594
}
86-
}
87-
while (mp_cmp (&t1, &t2) != MP_EQ);
95+
} while (mp_cmp (&t1, &t2) != MP_EQ);
8896

8997
/* result can be off by a few so check */
9098
for (;;) {
@@ -94,7 +102,7 @@ mp_n_root (mp_int * a, mp_digit b, mp_int * c)
94102

95103
if (mp_cmp (&t2, a) == MP_GT) {
96104
if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) {
97-
goto __T3;
105+
goto __T3;
98106
}
99107
} else {
100108
break;

bn_mp_reduce.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,8 @@ mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
3232
/* q1 = x / b**(k-1) */
3333
mp_rshd (&q, um - 1);
3434

35-
/* according to HAC this is optimization is ok */
36-
if (((unsigned long) m->used) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
35+
/* according to HAC this optimization is ok */
36+
if (((unsigned long) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
3737
if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) {
3838
goto CLEANUP;
3939
}
@@ -73,7 +73,7 @@ mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
7373
/* Back off if it's too big */
7474
while (mp_cmp (x, m) != MP_LT) {
7575
if ((res = s_mp_sub (x, m, x)) != MP_OKAY) {
76-
break;
76+
goto CLEANUP;
7777
}
7878
}
7979

bn_radix.c

+3-1
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ mp_read_radix (mp_int * a, char *str, int radix)
5656
}
5757
++str;
5858
}
59-
a->sign = neg;
59+
if (mp_iszero(a) != 1) {
60+
a->sign = neg;
61+
}
6062
return MP_OKAY;
6163
}
6264

bn_s_mp_sqr.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ s_mp_sqr (mp_int * a, mp_int * b)
3939
t.dp[2*ix] = (mp_digit) (r & ((mp_word) MP_MASK));
4040

4141
/* get the carry */
42-
u = (r >> ((mp_word) DIGIT_BIT));
42+
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
4343

4444
/* left hand side of A[ix] * A[iy] */
4545
tmpx = a->dp[ix];
@@ -60,13 +60,13 @@ s_mp_sqr (mp_int * a, mp_int * b)
6060
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
6161

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

changes.txt

+4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
June 19th, 2003
2+
v0.21 -- Fixed bug in mp_mul_d which would not handle sign correctly [would not always forward it]
3+
-- Removed the #line lines from gen.pl [was in violation of ISO C]
4+
15
June 8th, 2003
26
v0.20 -- Removed the book from the package. Added the TDCAL license document.
37
-- This release is officially pure-bred TDCAL again [last officially TDCAL based release was v0.16]

demo/demo.c

+2
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,8 @@ int main(void)
162162
fprintf(log, "%d %9llu\n", cnt*DIGIT_BIT, (((unsigned long long)rr)*CLOCKS_PER_SEC)/tt);
163163
}
164164
fclose(log);
165+
166+
return 0;
165167

166168
log = fopen("logs/sub.log", "w");
167169
for (cnt = 8; cnt <= 128; cnt += 8) {

etc/2kprime.1

+2-1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
259-bits (k = 17745) = 926336713898529563388567880069503262826159877325124512315660672063305037101743
1+
256-bits (k = 36113) = 115792089237316195423570985008687907853269984665640564039457584007913129603823
2+
512-bits (k = 38117) = 13407807929942597099574024998205846127479365820592393377723561443721764030073546976801874298166903427690031858186486050853753882811946569946433649006045979

etc/makefile.msvc

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
#
33
#Tom St Denis
44

5-
CFLAGS = /I../ /Ogityb2 /Gs /DWIN32 /W3
5+
CFLAGS = /I../ /Ox /DWIN32 /W3
66

77
pprime: pprime.obj
88
cl pprime.obj ../tommath.lib

gen.pl

-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@
99
foreach my $filename (glob "bn*.c") {
1010
open( SRC, "<$filename" ) or die "Couldn't open $filename for reading: $!";
1111
print OUT "/* Start: $filename */\n";
12-
print OUT qq[#line 0 "$filename"\n];
1312
print OUT while <SRC>;
1413
print OUT "\n/* End: $filename */\n\n";
1514
close SRC or die "Error closing $filename after reading: $!";

logs/add.log

-16
Original file line numberDiff line numberDiff line change
@@ -1,16 +0,0 @@
1-
224 11069160
2-
448 9156136
3-
672 8089755
4-
896 7399424
5-
1120 6389352
6-
1344 5818648
7-
1568 5257112
8-
1792 4982160
9-
2016 4527856
10-
2240 4325312
11-
2464 4051760
12-
2688 3767640
13-
2912 3612520
14-
3136 3415208
15-
3360 3258656
16-
3584 3113360

0 commit comments

Comments
 (0)