Skip to content

Commit 4c1d3f0

Browse files
Tom St Denissjaeckel
Tom St Denis
authored andcommitted
added libtommath-0.22
1 parent 49bef06 commit 4c1d3f0

37 files changed

+1858
-1447
lines changed

bn.pdf

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

+4-4
Original file line numberDiff line numberDiff line change
@@ -66,8 +66,8 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
6666
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
6767
goto __ERR;
6868
}
69-
/* 4.2 if A or B is odd then */
70-
if (mp_iseven (&B) == 0) {
69+
/* 4.2 if B is odd then */
70+
if (mp_isodd (&B) == 1) {
7171
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
7272
goto __ERR;
7373
}
@@ -84,8 +84,8 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
8484
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
8585
goto __ERR;
8686
}
87-
/* 5.2 if C,D are even then */
88-
if (mp_iseven (&D) == 0) {
87+
/* 5.2 if D is odd then */
88+
if (mp_isodd (&D) == 1) {
8989
/* D = (D-x)/2 */
9090
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
9191
goto __ERR;

bn_mp_cnt_lsb.c

+40
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2+
*
3+
* LibTomMath is library that provides for multiple-precision
4+
* integer arithmetic as well as number theoretic functionality.
5+
*
6+
* The library is designed directly after the MPI library by
7+
* Michael Fromberger but has been written from scratch with
8+
* additional optimizations in place.
9+
*
10+
* The library is free for all purposes without any express
11+
* guarantee it works.
12+
*
13+
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14+
*/
15+
#include <tommath.h>
16+
17+
/* Counts the number of lsbs which are zero before the first zero bit */
18+
int mp_cnt_lsb(mp_int *a)
19+
{
20+
int x;
21+
mp_digit q;
22+
23+
if (mp_iszero(a) == 1) {
24+
return 0;
25+
}
26+
27+
/* scan lower digits until non-zero */
28+
for (x = 0; x < a->used && a->dp[x] == 0; x++);
29+
q = a->dp[x];
30+
x *= DIGIT_BIT;
31+
32+
/* now scan this digit until a 1 is found */
33+
while ((q & 1) == 0) {
34+
q >>= 1;
35+
x += 1;
36+
}
37+
38+
return x;
39+
}
40+

bn_mp_div_2d.c

+5-2
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,14 @@ mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
5858
/* shift any bit count < DIGIT_BIT */
5959
D = (mp_digit) (b % DIGIT_BIT);
6060
if (D != 0) {
61-
register mp_digit *tmpc, mask;
61+
register mp_digit *tmpc, mask, shift;
6262

6363
/* mask */
6464
mask = (((mp_digit)1) << D) - 1;
6565

66+
/* shift for lsb */
67+
shift = DIGIT_BIT - D;
68+
6669
/* alias */
6770
tmpc = c->dp + (c->used - 1);
6871

@@ -73,7 +76,7 @@ mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
7376
rr = *tmpc & mask;
7477

7578
/* shift the current word and mix in the carry bits from the previous word */
76-
*tmpc = (*tmpc >> D) | (r << (DIGIT_BIT - D));
79+
*tmpc = (*tmpc >> D) | (r << shift);
7780
--tmpc;
7881

7982
/* set the carry to the carry bits of the current word found above */

bn_mp_div_3.c

+64-64
Original file line numberDiff line numberDiff line change
@@ -1,64 +1,64 @@
1-
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2-
*
3-
* LibTomMath is library that provides for multiple-precision
4-
* integer arithmetic as well as number theoretic functionality.
5-
*
6-
* The library is designed directly after the MPI library by
7-
* Michael Fromberger but has been written from scratch with
8-
* additional optimizations in place.
9-
*
10-
* The library is free for all purposes without any express
11-
* guarantee it works.
12-
*
13-
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14-
*/
15-
#include <tommath.h>
16-
17-
/* divide by three (based on routine from MPI and the GMP manual) */
18-
int
19-
mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
20-
{
21-
mp_int q;
22-
mp_word w, t;
23-
mp_digit b;
24-
int res, ix;
25-
26-
/* b = 2**DIGIT_BIT / 3 */
27-
b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
28-
29-
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
30-
return res;
31-
}
32-
33-
q.used = a->used;
34-
q.sign = a->sign;
35-
w = 0;
36-
for (ix = a->used - 1; ix >= 0; ix--) {
37-
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
38-
39-
if (w >= 3) {
40-
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
41-
w -= (t << ((mp_word)1)) + t;
42-
while (w >= 3) {
43-
t += 1;
44-
w -= 3;
45-
}
46-
} else {
47-
t = 0;
48-
}
49-
q.dp[ix] = (mp_digit)t;
50-
}
51-
52-
if (d != NULL) {
53-
*d = (mp_digit)w;
54-
}
55-
56-
if (c != NULL) {
57-
mp_clamp(&q);
58-
mp_exch(&q, c);
59-
}
60-
mp_clear(&q);
61-
62-
return res;
63-
}
64-
1+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2+
*
3+
* LibTomMath is library that provides for multiple-precision
4+
* integer arithmetic as well as number theoretic functionality.
5+
*
6+
* The library is designed directly after the MPI library by
7+
* Michael Fromberger but has been written from scratch with
8+
* additional optimizations in place.
9+
*
10+
* The library is free for all purposes without any express
11+
* guarantee it works.
12+
*
13+
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14+
*/
15+
#include <tommath.h>
16+
17+
/* divide by three (based on routine from MPI and the GMP manual) */
18+
int
19+
mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
20+
{
21+
mp_int q;
22+
mp_word w, t;
23+
mp_digit b;
24+
int res, ix;
25+
26+
/* b = 2**DIGIT_BIT / 3 */
27+
b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
28+
29+
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
30+
return res;
31+
}
32+
33+
q.used = a->used;
34+
q.sign = a->sign;
35+
w = 0;
36+
for (ix = a->used - 1; ix >= 0; ix--) {
37+
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
38+
39+
if (w >= 3) {
40+
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
41+
w -= (t << ((mp_word)1)) + t;
42+
while (w >= 3) {
43+
t += 1;
44+
w -= 3;
45+
}
46+
} else {
47+
t = 0;
48+
}
49+
q.dp[ix] = (mp_digit)t;
50+
}
51+
52+
if (d != NULL) {
53+
*d = (mp_digit)w;
54+
}
55+
56+
if (c != NULL) {
57+
mp_clamp(&q);
58+
mp_exch(&q, c);
59+
}
60+
mp_clear(&q);
61+
62+
return res;
63+
}
64+

bn_mp_exptmod_fast.c

+21-6
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,17 @@
2121
*
2222
* Uses Montgomery or Diminished Radix reduction [whichever appropriate]
2323
*/
24+
25+
#ifdef MP_LOW_MEM
26+
#define TAB_SIZE 32
27+
#else
28+
#define TAB_SIZE 256
29+
#endif
30+
2431
int
2532
mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
2633
{
27-
mp_int M[256], res;
34+
mp_int M[TAB_SIZE], res;
2835
mp_digit buf, mp;
2936
int err, bitbuf, bitcpy, bitcnt, mode, digidx, x, y, winsize;
3037

@@ -58,17 +65,24 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
5865
}
5966
#endif
6067

68+
/* init M array */
69+
/* init first cell */
70+
if ((err = mp_init(&M[1])) != MP_OKAY) {
71+
return err;
72+
}
6173

62-
/* init G array */
63-
for (x = 0; x < (1 << winsize); x++) {
64-
if ((err = mp_init (&M[x])) != MP_OKAY) {
65-
for (y = 0; y < x; y++) {
74+
/* now init the second half of the array */
75+
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
76+
if ((err = mp_init(&M[x])) != MP_OKAY) {
77+
for (y = 1<<(winsize-1); y < x; y++) {
6678
mp_clear (&M[y]);
6779
}
80+
mp_clear(&M[1]);
6881
return err;
6982
}
7083
}
7184

85+
7286
/* determine and setup reduction code */
7387
if (redmode == 0) {
7488
/* now setup montgomery */
@@ -257,7 +271,8 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
257271
err = MP_OKAY;
258272
__RES:mp_clear (&res);
259273
__M:
260-
for (x = 0; x < (1 << winsize); x++) {
274+
mp_clear(&M[1]);
275+
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
261276
mp_clear (&M[x]);
262277
}
263278
return err;

bn_mp_fread.c

+61
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2+
*
3+
* LibTomMath is library that provides for multiple-precision
4+
* integer arithmetic as well as number theoretic functionality.
5+
*
6+
* The library is designed directly after the MPI library by
7+
* Michael Fromberger but has been written from scratch with
8+
* additional optimizations in place.
9+
*
10+
* The library is free for all purposes without any express
11+
* guarantee it works.
12+
*
13+
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14+
*/
15+
#include <tommath.h>
16+
17+
/* read a bigint from a file stream in ASCII */
18+
int mp_fread(mp_int *a, int radix, FILE *stream)
19+
{
20+
int err, ch, neg, y;
21+
22+
/* clear a */
23+
mp_zero(a);
24+
25+
/* if first digit is - then set negative */
26+
ch = fgetc(stream);
27+
if (ch == '-') {
28+
neg = MP_NEG;
29+
ch = fgetc(stream);
30+
} else {
31+
neg = MP_ZPOS;
32+
}
33+
34+
for (;;) {
35+
/* find y in the radix map */
36+
for (y = 0; y < radix; y++) {
37+
if (mp_s_rmap[y] == ch) {
38+
break;
39+
}
40+
}
41+
if (y == radix) {
42+
break;
43+
}
44+
45+
/* shift up and add */
46+
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
47+
return err;
48+
}
49+
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
50+
return err;
51+
}
52+
53+
ch = fgetc(stream);
54+
}
55+
if (mp_cmp_d(a, 0) != MP_EQ) {
56+
a->sign = neg;
57+
}
58+
59+
return MP_OKAY;
60+
}
61+

bn_mp_fwrite.c

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2+
*
3+
* LibTomMath is library that provides for multiple-precision
4+
* integer arithmetic as well as number theoretic functionality.
5+
*
6+
* The library is designed directly after the MPI library by
7+
* Michael Fromberger but has been written from scratch with
8+
* additional optimizations in place.
9+
*
10+
* The library is free for all purposes without any express
11+
* guarantee it works.
12+
*
13+
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
14+
*/
15+
#include <tommath.h>
16+
17+
int mp_fwrite(mp_int *a, int radix, FILE *stream)
18+
{
19+
char *buf;
20+
int err, len, x;
21+
22+
len = mp_radix_size(a, radix);
23+
if (len == 0) {
24+
return MP_VAL;
25+
}
26+
27+
buf = malloc(len);
28+
if (buf == NULL) {
29+
return MP_MEM;
30+
}
31+
32+
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
33+
free(buf);
34+
return err;
35+
}
36+
37+
for (x = 0; x < len; x++) {
38+
if (fputc(buf[x], stream) == EOF) {
39+
free(buf);
40+
return MP_VAL;
41+
}
42+
}
43+
44+
free(buf);
45+
return MP_OKAY;
46+
}
47+

0 commit comments

Comments
 (0)