Skip to content

Commit c1da6aa

Browse files
Tom St Denissjaeckel
Tom St Denis
authored andcommitted
added libtommath-0.25
1 parent 03cc01b commit c1da6aa

File tree

121 files changed

+2945
-2593
lines changed

Some content is hidden

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

121 files changed

+2945
-2593
lines changed

LICENSE

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
LibTomMath is hereby released into the Public Domain.
2+
3+
-- Tom St Denis
4+

bn.pdf

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

bn_error.c

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/* LibTomMath, multiple-precision integer library -- Tom St Denis
2+
*
3+
* LibTomMath is a library that provides multiple-precision
4+
* integer arithmetic as well as number theoretic functionality.
5+
*
6+
* The library was 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+
static const struct {
18+
int code;
19+
char *msg;
20+
} msgs[] = {
21+
{ MP_OKAY, "Successful" },
22+
{ MP_MEM, "Out of heap" },
23+
{ MP_VAL, "Value out of range" }
24+
};
25+
26+
/* return a char * string for a given code */
27+
char *mp_error_to_string(int code)
28+
{
29+
int x;
30+
31+
/* scan the lookup table for the given message */
32+
for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
33+
if (msgs[x].code == code) {
34+
return msgs[x].msg;
35+
}
36+
}
37+
38+
/* generic reply for invalid code */
39+
return "Invalid error code";
40+
}
41+

bn_fast_mp_invmod.c

+10-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -26,6 +26,14 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
2626
mp_int x, y, u, v, B, D;
2727
int res, neg;
2828

29+
/* 2. [modified] if a,b are both even then return an error!
30+
*
31+
* That is if gcd(a,b) = 2**k * q then obviously there is no inverse.
32+
*/
33+
if (mp_iseven (a) == 1 && mp_iseven (b) == 1) {
34+
return MP_VAL;
35+
}
36+
2937
/* init all our temps */
3038
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
3139
return res;
@@ -41,15 +49,6 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
4149
goto __ERR;
4250
}
4351

44-
/* 2. [modified] if x,y are both even then return an error!
45-
*
46-
* That is if gcd(x,y) = 2 * k then obviously there is no inverse.
47-
*/
48-
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
49-
res = MP_VAL;
50-
goto __ERR;
51-
}
52-
5352
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
5453
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
5554
goto __ERR;

bn_fast_mp_montgomery_reduce.c

+22-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -38,6 +38,9 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
3838
}
3939
}
4040

41+
/* first we have to get the digits of the input into
42+
* an array of double precision words W[...]
43+
*/
4144
{
4245
register mp_word *_W;
4346
register mp_digit *tmpx;
@@ -56,6 +59,9 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
5659
}
5760
}
5861

62+
/* now we proceed to zero successive digits
63+
* from the least significant upwards
64+
*/
5965
for (ix = 0; ix < n->used; ix++) {
6066
/* mu = ai * m' mod b
6167
*
@@ -101,12 +107,20 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
101107
W[ix + 1] += W[ix] >> ((mp_word) DIGIT_BIT);
102108
}
103109

110+
/* now we have to propagate the carries and
111+
* shift the words downward [all those least
112+
* significant digits we zeroed].
113+
*/
104114
{
105115
register mp_digit *tmpx;
106116
register mp_word *_W, *_W1;
107117

108118
/* nox fix rest of carries */
119+
120+
/* alias for current word */
109121
_W1 = W + ix;
122+
123+
/* alias for next word, where the carry goes */
110124
_W = W + ++ix;
111125

112126
for (; ix <= n->used * 2 + 1; ix++) {
@@ -119,15 +133,20 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
119133
* array of mp_word to mp_digit than calling mp_rshd
120134
* we just copy them in the right order
121135
*/
136+
137+
/* alias for destination word */
122138
tmpx = x->dp;
139+
140+
/* alias for shifted double precision result */
123141
_W = W + n->used;
124142

125143
for (ix = 0; ix < n->used + 1; ix++) {
126144
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
127145
}
128146

129147
/* zero oldused digits, if the input a was larger than
130-
* m->used+1 we'll have to clear the digits */
148+
* m->used+1 we'll have to clear the digits
149+
*/
131150
for (; ix < olduse; ix++) {
132151
*tmpx++ = 0;
133152
}

bn_fast_s_mp_mul_digs.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*

bn_fast_s_mp_mul_high_digs.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -12,7 +12,7 @@
1212
*
1313
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
1414
*/
15-
#include <tommath.h>
15+
#include <tommath.h>
1616

1717
/* this is a modified version of fast_s_mp_mul_digs that only produces
1818
* output digits *above* digs. See the comments for fast_s_mp_mul_digs

bn_fast_s_mp_sqr.c

+19-8
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -48,14 +48,14 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
4848

4949
/* zero temp buffer (columns)
5050
* Note that there are two buffers. Since squaring requires
51-
* a outter and inner product and the inner product requires
51+
* a outer and inner product and the inner product requires
5252
* computing a product and doubling it (a relatively expensive
5353
* op to perform n**2 times if you don't have to) the inner and
5454
* outer products are computed in different buffers. This way
5555
* the inner product can be doubled using n doublings instead of
5656
* n**2
5757
*/
58-
memset (W, 0, newused * sizeof (mp_word));
58+
memset (W, 0, newused * sizeof (mp_word));
5959
memset (W2, 0, newused * sizeof (mp_word));
6060

6161
/* This computes the inner product. To simplify the inner N**2 loop
@@ -67,6 +67,7 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
6767
* Note that every outer product is computed
6868
* for a particular column only once which means that
6969
* there is no need todo a double precision addition
70+
* into the W2[] array.
7071
*/
7172
W2[ix + ix] = ((mp_word)a->dp[ix]) * ((mp_word)a->dp[ix]);
7273

@@ -95,7 +96,12 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
9596
olduse = b->used;
9697
b->used = newused;
9798

98-
/* now compute digits */
99+
/* now compute digits
100+
*
101+
* We have to double the inner product sums, add in the
102+
* outer product sums, propagate carries and convert
103+
* to single precision.
104+
*/
99105
{
100106
register mp_digit *tmpb;
101107

@@ -109,16 +115,21 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
109115
/* double/add next digit */
110116
W[ix] += W[ix] + W2[ix];
111117

118+
/* propagate carry forwards [from the previous digit] */
112119
W[ix] = W[ix] + (W[ix - 1] >> ((mp_word) DIGIT_BIT));
120+
121+
/* store the current digit now that the carry isn't
122+
* needed
123+
*/
113124
*tmpb++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
114125
}
115-
/* set the last value. Note even if the carry is zero
116-
* this is required since the next step will not zero
126+
/* set the last value. Note even if the carry is zero
127+
* this is required since the next step will not zero
117128
* it if b originally had a value at b->dp[2*a.used]
118129
*/
119130
*tmpb++ = (mp_digit) (W[(newused) - 1] & ((mp_word) MP_MASK));
120131

121-
/* clear high digits */
132+
/* clear high digits of b if there were any originally */
122133
for (; ix < olduse; ix++) {
123134
*tmpb++ = 0;
124135
}

bn_mp_2expt.c

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -24,11 +24,18 @@ mp_2expt (mp_int * a, int b)
2424
{
2525
int res;
2626

27+
/* zero a as per default */
2728
mp_zero (a);
29+
30+
/* grow a to accomodate the single bit */
2831
if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
2932
return res;
3033
}
34+
35+
/* set the used count of where the bit will go */
3136
a->used = b / DIGIT_BIT + 1;
37+
38+
/* put the single bit in its place */
3239
a->dp[b / DIGIT_BIT] = 1 << (b % DIGIT_BIT);
3340

3441
return MP_OKAY;

bn_mp_abs.c

+11-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -22,9 +22,16 @@ int
2222
mp_abs (mp_int * a, mp_int * b)
2323
{
2424
int res;
25-
if ((res = mp_copy (a, b)) != MP_OKAY) {
26-
return res;
25+
26+
/* copy a to b */
27+
if (a != b) {
28+
if ((res = mp_copy (a, b)) != MP_OKAY) {
29+
return res;
30+
}
2731
}
32+
33+
/* force the sign of b to positive */
2834
b->sign = MP_ZPOS;
35+
2936
return MP_OKAY;
3037
}

bn_mp_add.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*

bn_mp_add_d.c

+7-3
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*
@@ -82,7 +82,11 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
8282
c->used = 1;
8383

8484
/* the result is a single digit */
85-
*tmpc++ = b - a->dp[0];
85+
if (a->used == 1) {
86+
*tmpc++ = b - a->dp[0];
87+
} else {
88+
*tmpc++ = b;
89+
}
8690

8791
/* setup count so the clearing of oldused
8892
* can fall through correctly

bn_mp_addmod.c

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
/* LibTomMath, multiple-precision integer library -- Tom St Denis
22
*
3-
* LibTomMath is library that provides for multiple-precision
3+
* LibTomMath is a library that provides multiple-precision
44
* integer arithmetic as well as number theoretic functionality.
55
*
6-
* The library is designed directly after the MPI library by
6+
* The library was designed directly after the MPI library by
77
* Michael Fromberger but has been written from scratch with
88
* additional optimizations in place.
99
*

0 commit comments

Comments
 (0)