Skip to content

Commit c343371

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

20 files changed

+292
-513
lines changed

bn.pdf

110 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.26 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
4+
\title{LibTomMath v0.27 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
55
\author{Tom St Denis \\ tomstdenis@iahu.ca}
66
\maketitle
77
\newpage

bn_mp_add_d.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -56,9 +56,6 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
5656

5757
/* if a is positive */
5858
if (a->sign == MP_ZPOS) {
59-
/* setup size */
60-
c->used = a->used + 1;
61-
6259
/* add digit, after this we're propagating
6360
* the carry.
6461
*/
@@ -75,6 +72,9 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
7572
/* set final carry */
7673
ix++;
7774
*tmpc++ = mu;
75+
76+
/* setup size */
77+
c->used = a->used + 1;
7878
} else {
7979
/* a was negative and |a| < b */
8080
c->used = 1;

bn_mp_dr_reduce.c

+8-8
Original file line numberDiff line numberDiff line change
@@ -26,39 +26,39 @@
2626
*
2727
* Has been modified to use algorithm 7.10 from the LTM book instead
2828
*
29-
* Input x must be in the range 0 <= x <= (n-1)^2
29+
* Input x must be in the range 0 <= x <= (n-1)**2
3030
*/
3131
int
3232
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
3333
{
3434
int err, i, m;
3535
mp_word r;
3636
mp_digit mu, *tmpx1, *tmpx2;
37-
37+
3838
/* m = digits in modulus */
3939
m = n->used;
40-
40+
4141
/* ensure that "x" has at least 2m digits */
4242
if (x->alloc < m + m) {
4343
if ((err = mp_grow (x, m + m)) != MP_OKAY) {
4444
return err;
4545
}
4646
}
4747

48-
/* top of loop, this is where the code resumes if
48+
/* top of loop, this is where the code resumes if
4949
* another reduction pass is required.
5050
*/
5151
top:
5252
/* aliases for digits */
5353
/* alias for lower half of x */
5454
tmpx1 = x->dp;
55-
55+
5656
/* alias for upper half of x, or x/B**m */
5757
tmpx2 = x->dp + m;
58-
58+
5959
/* set carry to zero */
6060
mu = 0;
61-
61+
6262
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
6363
for (i = 0; i < m; i++) {
6464
r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
@@ -77,7 +77,7 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
7777
/* clamp, sub and return */
7878
mp_clamp (x);
7979

80-
/* if x >= n then subtract and reduce again
80+
/* if x >= n then subtract and reduce again
8181
* Each successive "recursion" makes the input smaller and smaller.
8282
*/
8383
if (mp_cmp_mag (x, n) != MP_LT) {

bn_mp_exptmod_fast.c

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

17-
/* computes Y == G^X mod P, HAC pp.616, Algorithm 14.85
17+
/* computes Y == G**X mod P, HAC pp.616, Algorithm 14.85
1818
*
1919
* Uses a left-to-right k-ary sliding window to compute the modular exponentiation.
2020
* The value of k changes based on the size of the exponent.
@@ -34,10 +34,10 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
3434
mp_int M[TAB_SIZE], res;
3535
mp_digit buf, mp;
3636
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
37-
37+
3838
/* use a pointer to the reduction algorithm. This allows us to use
3939
* one of many reduction algorithms without modding the guts of
40-
* the code with if statements everywhere.
40+
* the code with if statements everywhere.
4141
*/
4242
int (*redux)(mp_int*,mp_int*,mp_digit);
4343

@@ -68,7 +68,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
6868
/* init M array */
6969
/* init first cell */
7070
if ((err = mp_init(&M[1])) != MP_OKAY) {
71-
return err;
71+
return err;
7272
}
7373

7474
/* now init the second half of the array */
@@ -88,7 +88,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
8888
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
8989
goto __M;
9090
}
91-
91+
9292
/* automatically pick the comba one if available (saves quite a few calls/ifs) */
9393
if (((P->used * 2 + 1) < MP_WARRAY) &&
9494
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {

bn_mp_grow.c

+15-3
Original file line numberDiff line numberDiff line change
@@ -19,17 +19,29 @@ int
1919
mp_grow (mp_int * a, int size)
2020
{
2121
int i;
22+
mp_digit *tmp;
23+
2224

2325
/* if the alloc size is smaller alloc more ram */
2426
if (a->alloc < size) {
2527
/* ensure there are always at least MP_PREC digits extra on top */
26-
size += (MP_PREC * 2) - (size % MP_PREC);
28+
size += (MP_PREC * 2) - (size % MP_PREC);
2729

28-
a->dp = OPT_CAST realloc (a->dp, sizeof (mp_digit) * size);
29-
if (a->dp == NULL) {
30+
/* reallocate the array a->dp
31+
*
32+
* We store the return in a temporary variable
33+
* in case the operation failed we don't want
34+
* to overwrite the dp member of a.
35+
*/
36+
tmp = OPT_CAST realloc (a->dp, sizeof (mp_digit) * size);
37+
if (tmp == NULL) {
38+
/* reallocation failed but "a" is still valid [can be freed] */
3039
return MP_MEM;
3140
}
3241

42+
/* reallocation succeeded so set a->dp */
43+
a->dp = tmp;
44+
3345
/* zero excess digits */
3446
i = a->alloc;
3547
a->alloc = size;

bn_mp_mod_2d.c

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

17-
/* calc a value mod 2^b */
17+
/* calc a value mod 2**b */
1818
int
1919
mp_mod_2d (mp_int * a, int b, mp_int * c)
2020
{

bn_mp_mul_d.c

+31-31
Original file line numberDiff line numberDiff line change
@@ -18,55 +18,55 @@
1818
int
1919
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
2020
{
21-
int res, pa, olduse;
21+
mp_digit u, *tmpa, *tmpc;
22+
mp_word r;
23+
int ix, res, olduse;
2224

2325
/* make sure c is big enough to hold a*b */
24-
pa = a->used;
25-
if (c->alloc < pa + 1) {
26-
if ((res = mp_grow (c, pa + 1)) != MP_OKAY) {
26+
if (c->alloc < a->used + 1) {
27+
if ((res = mp_grow (c, a->used + 1)) != MP_OKAY) {
2728
return res;
2829
}
2930
}
3031

3132
/* get the original destinations used count */
3233
olduse = c->used;
3334

34-
/* set the new temporary used count */
35-
c->used = pa + 1;
35+
/* set the sign */
3636
c->sign = a->sign;
3737

38-
{
39-
register mp_digit u, *tmpa, *tmpc;
40-
register mp_word r;
41-
register int ix;
38+
/* alias for a->dp [source] */
39+
tmpa = a->dp;
4240

43-
/* alias for a->dp [source] */
44-
tmpa = a->dp;
41+
/* alias for c->dp [dest] */
42+
tmpc = c->dp;
4543

46-
/* alias for c->dp [dest] */
47-
tmpc = c->dp;
44+
/* zero carry */
45+
u = 0;
4846

49-
/* zero carry */
50-
u = 0;
51-
for (ix = 0; ix < pa; ix++) {
52-
/* compute product and carry sum for this term */
53-
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
47+
/* compute columns */
48+
for (ix = 0; ix < a->used; ix++) {
49+
/* compute product and carry sum for this term */
50+
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b);
5451

55-
/* mask off higher bits to get a single digit */
56-
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
52+
/* mask off higher bits to get a single digit */
53+
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));
5754

58-
/* send carry into next iteration */
59-
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
60-
}
61-
/* store final carry [if any] */
62-
*tmpc++ = u;
55+
/* send carry into next iteration */
56+
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
57+
}
6358

64-
/* now zero digits above the top */
65-
for (; pa < olduse; pa++) {
66-
*tmpc++ = 0;
67-
}
59+
/* store final carry [if any] */
60+
*tmpc++ = u;
61+
62+
/* now zero digits above the top */
63+
while (ix++ < olduse) {
64+
*tmpc++ = 0;
6865
}
6966

70-
mp_clamp (c);
67+
/* set used count */
68+
c->used = a->used + 1;
69+
mp_clamp(c);
70+
7171
return MP_OKAY;
7272
}

bn_mp_sub_d.c

+2-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,8 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
7373
}
7474
}
7575

76-
for (; ix < oldused; ix++) {
76+
/* zero excess digits */
77+
while (ix++ < oldused) {
7778
*tmpc++ = 0;
7879
}
7980
mp_clamp(c);

bn_mp_toom_sqr.c

+23-23
Original file line numberDiff line numberDiff line change
@@ -15,21 +15,21 @@
1515
#include <tommath.h>
1616

1717
/* squaring using Toom-Cook 3-way algorithm */
18-
int
18+
int
1919
mp_toom_sqr(mp_int *a, mp_int *b)
2020
{
2121
mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
2222
int res, B;
23-
23+
2424
/* init temps */
2525
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != MP_OKAY) {
2626
return res;
2727
}
2828

2929
/* B */
3030
B = a->used / 3;
31-
32-
/* a = a2 * B^2 + a1 * B + a0 */
31+
32+
/* a = a2 * B**2 + a1 * B + a0 */
3333
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
3434
goto ERR;
3535
}
@@ -44,17 +44,17 @@ mp_toom_sqr(mp_int *a, mp_int *b)
4444
goto ERR;
4545
}
4646
mp_rshd(&a2, B*2);
47-
47+
4848
/* w0 = a0*a0 */
4949
if ((res = mp_sqr(&a0, &w0)) != MP_OKAY) {
5050
goto ERR;
5151
}
52-
52+
5353
/* w4 = a2 * a2 */
5454
if ((res = mp_sqr(&a2, &w4)) != MP_OKAY) {
5555
goto ERR;
5656
}
57-
57+
5858
/* w1 = (a2 + 2(a1 + 2a0))**2 */
5959
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
6060
goto ERR;
@@ -68,11 +68,11 @@ mp_toom_sqr(mp_int *a, mp_int *b)
6868
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
6969
goto ERR;
7070
}
71-
71+
7272
if ((res = mp_sqr(&tmp1, &w1)) != MP_OKAY) {
7373
goto ERR;
7474
}
75-
75+
7676
/* w3 = (a0 + 2(a1 + 2a2))**2 */
7777
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
7878
goto ERR;
@@ -86,11 +86,11 @@ mp_toom_sqr(mp_int *a, mp_int *b)
8686
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
8787
goto ERR;
8888
}
89-
89+
9090
if ((res = mp_sqr(&tmp1, &w3)) != MP_OKAY) {
9191
goto ERR;
9292
}
93-
93+
9494

9595
/* w2 = (a2 + a1 + a0)**2 */
9696
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
@@ -102,18 +102,18 @@ mp_toom_sqr(mp_int *a, mp_int *b)
102102
if ((res = mp_sqr(&tmp1, &w2)) != MP_OKAY) {
103103
goto ERR;
104104
}
105-
106-
/* now solve the matrix
107-
105+
106+
/* now solve the matrix
107+
108108
0 0 0 0 1
109109
1 2 4 8 16
110110
1 1 1 1 1
111111
16 8 4 2 1
112112
1 0 0 0 0
113-
113+
114114
using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication.
115115
*/
116-
116+
117117
/* r1 - r4 */
118118
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
119119
goto ERR;
@@ -185,7 +185,7 @@ mp_toom_sqr(mp_int *a, mp_int *b)
185185
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
186186
goto ERR;
187187
}
188-
188+
189189
/* at this point shift W[n] by B*n */
190190
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
191191
goto ERR;
@@ -198,8 +198,8 @@ mp_toom_sqr(mp_int *a, mp_int *b)
198198
}
199199
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
200200
goto ERR;
201-
}
202-
201+
}
202+
203203
if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
204204
goto ERR;
205205
}
@@ -211,10 +211,10 @@ mp_toom_sqr(mp_int *a, mp_int *b)
211211
}
212212
if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
213213
goto ERR;
214-
}
215-
214+
}
215+
216216
ERR:
217217
mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
218218
return res;
219-
}
220-
219+
}
220+

0 commit comments

Comments
 (0)