Skip to content

Commit

Permalink
added libtommath-0.25
Browse files Browse the repository at this point in the history
  • Loading branch information
Tom St Denis authored and sjaeckel committed Jul 15, 2010
1 parent 03cc01b commit c1da6aa
Show file tree
Hide file tree
Showing 121 changed files with 2,945 additions and 2,593 deletions.
4 changes: 4 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
LibTomMath is hereby released into the Public Domain.

-- Tom St Denis

Binary file modified bn.pdf
Binary file not shown.
2 changes: 1 addition & 1 deletion bn.tex
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
\documentclass[]{article}
\begin{document}

\title{LibTomMath v0.24 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
\title{LibTomMath v0.25 \\ A Free Multiple Precision Integer Library \\ http://math.libtomcrypt.org }
\author{Tom St Denis \\ [email protected]}
\maketitle
\newpage
Expand Down
41 changes: 41 additions & 0 deletions bn_error.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
* The library is free for all purposes without any express
* guarantee it works.
*
* Tom St Denis, [email protected], http://math.libtomcrypt.org
*/
#include <tommath.h>

static const struct {
int code;
char *msg;
} msgs[] = {
{ MP_OKAY, "Successful" },
{ MP_MEM, "Out of heap" },
{ MP_VAL, "Value out of range" }
};

/* return a char * string for a given code */
char *mp_error_to_string(int code)
{
int x;

/* scan the lookup table for the given message */
for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
if (msgs[x].code == code) {
return msgs[x].msg;
}
}

/* generic reply for invalid code */
return "Invalid error code";
}

21 changes: 10 additions & 11 deletions bn_fast_mp_invmod.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand All @@ -26,6 +26,14 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
mp_int x, y, u, v, B, D;
int res, neg;

/* 2. [modified] if a,b are both even then return an error!
*
* That is if gcd(a,b) = 2**k * q then obviously there is no inverse.
*/
if (mp_iseven (a) == 1 && mp_iseven (b) == 1) {
return MP_VAL;
}

/* init all our temps */
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
return res;
Expand All @@ -41,15 +49,6 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
goto __ERR;
}

/* 2. [modified] if x,y are both even then return an error!
*
* That is if gcd(x,y) = 2 * k then obviously there is no inverse.
*/
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
res = MP_VAL;
goto __ERR;
}

/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
goto __ERR;
Expand Down
25 changes: 22 additions & 3 deletions bn_fast_mp_montgomery_reduce.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand Down Expand Up @@ -38,6 +38,9 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
}
}

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

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

/* now we have to propagate the carries and
* shift the words downward [all those least
* significant digits we zeroed].
*/
{
register mp_digit *tmpx;
register mp_word *_W, *_W1;

/* nox fix rest of carries */

/* alias for current word */
_W1 = W + ix;

/* alias for next word, where the carry goes */
_W = W + ++ix;

for (; ix <= n->used * 2 + 1; ix++) {
Expand All @@ -119,15 +133,20 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* array of mp_word to mp_digit than calling mp_rshd
* we just copy them in the right order
*/

/* alias for destination word */
tmpx = x->dp;

/* alias for shifted double precision result */
_W = W + n->used;

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

/* zero oldused digits, if the input a was larger than
* m->used+1 we'll have to clear the digits */
* m->used+1 we'll have to clear the digits
*/
for (; ix < olduse; ix++) {
*tmpx++ = 0;
}
Expand Down
4 changes: 2 additions & 2 deletions bn_fast_s_mp_mul_digs.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand Down
6 changes: 3 additions & 3 deletions bn_fast_s_mp_mul_high_digs.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand All @@ -12,7 +12,7 @@
*
* Tom St Denis, [email protected], http://math.libtomcrypt.org
*/
#include <tommath.h>
#include <tommath.h>

/* this is a modified version of fast_s_mp_mul_digs that only produces
* output digits *above* digs. See the comments for fast_s_mp_mul_digs
Expand Down
27 changes: 19 additions & 8 deletions bn_fast_s_mp_sqr.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand Down Expand Up @@ -48,14 +48,14 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)

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

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

Expand Down Expand Up @@ -95,7 +96,12 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
olduse = b->used;
b->used = newused;

/* now compute digits */
/* now compute digits
*
* We have to double the inner product sums, add in the
* outer product sums, propagate carries and convert
* to single precision.
*/
{
register mp_digit *tmpb;

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

/* propagate carry forwards [from the previous digit] */
W[ix] = W[ix] + (W[ix - 1] >> ((mp_word) DIGIT_BIT));

/* store the current digit now that the carry isn't
* needed
*/
*tmpb++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
}
/* set the last value. Note even if the carry is zero
* this is required since the next step will not zero
/* set the last value. Note even if the carry is zero
* this is required since the next step will not zero
* it if b originally had a value at b->dp[2*a.used]
*/
*tmpb++ = (mp_digit) (W[(newused) - 1] & ((mp_word) MP_MASK));

/* clear high digits */
/* clear high digits of b if there were any originally */
for (; ix < olduse; ix++) {
*tmpb++ = 0;
}
Expand Down
11 changes: 9 additions & 2 deletions bn_mp_2expt.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand All @@ -24,11 +24,18 @@ mp_2expt (mp_int * a, int b)
{
int res;

/* zero a as per default */
mp_zero (a);

/* grow a to accomodate the single bit */
if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
return res;
}

/* set the used count of where the bit will go */
a->used = b / DIGIT_BIT + 1;

/* put the single bit in its place */
a->dp[b / DIGIT_BIT] = 1 << (b % DIGIT_BIT);

return MP_OKAY;
Expand Down
15 changes: 11 additions & 4 deletions bn_mp_abs.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand All @@ -22,9 +22,16 @@ int
mp_abs (mp_int * a, mp_int * b)
{
int res;
if ((res = mp_copy (a, b)) != MP_OKAY) {
return res;

/* copy a to b */
if (a != b) {
if ((res = mp_copy (a, b)) != MP_OKAY) {
return res;
}
}

/* force the sign of b to positive */
b->sign = MP_ZPOS;

return MP_OKAY;
}
4 changes: 2 additions & 2 deletions bn_mp_add.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand Down
10 changes: 7 additions & 3 deletions bn_mp_add_d.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand Down Expand Up @@ -82,7 +82,11 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
c->used = 1;

/* the result is a single digit */
*tmpc++ = b - a->dp[0];
if (a->used == 1) {
*tmpc++ = b - a->dp[0];
} else {
*tmpc++ = b;
}

/* setup count so the clearing of oldused
* can fall through correctly
Expand Down
4 changes: 2 additions & 2 deletions bn_mp_addmod.c
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/* LibTomMath, multiple-precision integer library -- Tom St Denis
*
* LibTomMath is library that provides for multiple-precision
* LibTomMath is a library that provides multiple-precision
* integer arithmetic as well as number theoretic functionality.
*
* The library is designed directly after the MPI library by
* The library was designed directly after the MPI library by
* Michael Fromberger but has been written from scratch with
* additional optimizations in place.
*
Expand Down
Loading

0 comments on commit c1da6aa

Please sign in to comment.