added libtommath-0.25
This commit is contained in:
parent
03cc01b578
commit
c1da6aa2de
|
@ -0,0 +1,4 @@
|
||||||
|
LibTomMath is hereby released into the Public Domain.
|
||||||
|
|
||||||
|
-- Tom St Denis
|
||||||
|
|
2
bn.tex
2
bn.tex
|
@ -1,7 +1,7 @@
|
||||||
\documentclass[]{article}
|
\documentclass[]{article}
|
||||||
\begin{document}
|
\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 \\ tomstdenis@iahu.ca}
|
\author{Tom St Denis \\ tomstdenis@iahu.ca}
|
||||||
\maketitle
|
\maketitle
|
||||||
\newpage
|
\newpage
|
||||||
|
|
|
@ -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, tomstdenis@iahu.ca, 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";
|
||||||
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -26,6 +26,14 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||||
mp_int x, y, u, v, B, D;
|
mp_int x, y, u, v, B, D;
|
||||||
int res, neg;
|
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 */
|
/* init all our temps */
|
||||||
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
|
if ((res = mp_init_multi(&x, &y, &u, &v, &B, &D, NULL)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
|
@ -41,15 +49,6 @@ fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||||
goto __ERR;
|
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 */
|
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
||||||
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
||||||
goto __ERR;
|
goto __ERR;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -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_word *_W;
|
||||||
register mp_digit *tmpx;
|
register mp_digit *tmpx;
|
||||||
|
@ -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++) {
|
for (ix = 0; ix < n->used; ix++) {
|
||||||
/* mu = ai * m' mod b
|
/* mu = ai * m' mod b
|
||||||
*
|
*
|
||||||
|
@ -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);
|
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_digit *tmpx;
|
||||||
register mp_word *_W, *_W1;
|
register mp_word *_W, *_W1;
|
||||||
|
|
||||||
/* nox fix rest of carries */
|
/* nox fix rest of carries */
|
||||||
|
|
||||||
|
/* alias for current word */
|
||||||
_W1 = W + ix;
|
_W1 = W + ix;
|
||||||
|
|
||||||
|
/* alias for next word, where the carry goes */
|
||||||
_W = W + ++ix;
|
_W = W + ++ix;
|
||||||
|
|
||||||
for (; ix <= n->used * 2 + 1; ix++) {
|
for (; ix <= n->used * 2 + 1; ix++) {
|
||||||
|
@ -119,7 +133,11 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||||
* array of mp_word to mp_digit than calling mp_rshd
|
* array of mp_word to mp_digit than calling mp_rshd
|
||||||
* we just copy them in the right order
|
* we just copy them in the right order
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
/* alias for destination word */
|
||||||
tmpx = x->dp;
|
tmpx = x->dp;
|
||||||
|
|
||||||
|
/* alias for shifted double precision result */
|
||||||
_W = W + n->used;
|
_W = W + n->used;
|
||||||
|
|
||||||
for (ix = 0; ix < n->used + 1; ix++) {
|
for (ix = 0; ix < n->used + 1; ix++) {
|
||||||
|
@ -127,7 +145,8 @@ fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* zero oldused digits, if the input a was larger than
|
/* 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++) {
|
for (; ix < olduse; ix++) {
|
||||||
*tmpx++ = 0;
|
*tmpx++ = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -12,7 +12,7 @@
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* this is a modified version of fast_s_mp_mul_digs that only produces
|
/* 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
|
* output digits *above* digs. See the comments for fast_s_mp_mul_digs
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -48,14 +48,14 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||||
|
|
||||||
/* zero temp buffer (columns)
|
/* zero temp buffer (columns)
|
||||||
* Note that there are two buffers. Since squaring requires
|
* 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
|
* computing a product and doubling it (a relatively expensive
|
||||||
* op to perform n**2 times if you don't have to) the inner and
|
* op to perform n**2 times if you don't have to) the inner and
|
||||||
* outer products are computed in different buffers. This way
|
* outer products are computed in different buffers. This way
|
||||||
* the inner product can be doubled using n doublings instead of
|
* the inner product can be doubled using n doublings instead of
|
||||||
* n**2
|
* n**2
|
||||||
*/
|
*/
|
||||||
memset (W, 0, newused * sizeof (mp_word));
|
memset (W, 0, newused * sizeof (mp_word));
|
||||||
memset (W2, 0, newused * sizeof (mp_word));
|
memset (W2, 0, newused * sizeof (mp_word));
|
||||||
|
|
||||||
/* This computes the inner product. To simplify the inner N**2 loop
|
/* 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)
|
||||||
* Note that every outer product is computed
|
* Note that every outer product is computed
|
||||||
* for a particular column only once which means that
|
* for a particular column only once which means that
|
||||||
* there is no need todo a double precision addition
|
* 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]);
|
W2[ix + ix] = ((mp_word)a->dp[ix]) * ((mp_word)a->dp[ix]);
|
||||||
|
|
||||||
|
@ -95,7 +96,12 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||||
olduse = b->used;
|
olduse = b->used;
|
||||||
b->used = newused;
|
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;
|
register mp_digit *tmpb;
|
||||||
|
|
||||||
|
@ -109,16 +115,21 @@ fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||||
/* double/add next digit */
|
/* double/add next digit */
|
||||||
W[ix] += W[ix] + W2[ix];
|
W[ix] += W[ix] + W2[ix];
|
||||||
|
|
||||||
|
/* propagate carry forwards [from the previous digit] */
|
||||||
W[ix] = W[ix] + (W[ix - 1] >> ((mp_word) DIGIT_BIT));
|
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));
|
*tmpb++ = (mp_digit) (W[ix - 1] & ((mp_word) MP_MASK));
|
||||||
}
|
}
|
||||||
/* set the last value. Note even if the carry is zero
|
/* set the last value. Note even if the carry is zero
|
||||||
* this is required since the next step will not zero
|
* this is required since the next step will not zero
|
||||||
* it if b originally had a value at b->dp[2*a.used]
|
* it if b originally had a value at b->dp[2*a.used]
|
||||||
*/
|
*/
|
||||||
*tmpb++ = (mp_digit) (W[(newused) - 1] & ((mp_word) MP_MASK));
|
*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++) {
|
for (; ix < olduse; ix++) {
|
||||||
*tmpb++ = 0;
|
*tmpb++ = 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -24,11 +24,18 @@ mp_2expt (mp_int * a, int b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
|
/* zero a as per default */
|
||||||
mp_zero (a);
|
mp_zero (a);
|
||||||
|
|
||||||
|
/* grow a to accomodate the single bit */
|
||||||
if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
|
if ((res = mp_grow (a, b / DIGIT_BIT + 1)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* set the used count of where the bit will go */
|
||||||
a->used = b / DIGIT_BIT + 1;
|
a->used = b / DIGIT_BIT + 1;
|
||||||
|
|
||||||
|
/* put the single bit in its place */
|
||||||
a->dp[b / DIGIT_BIT] = 1 << (b % DIGIT_BIT);
|
a->dp[b / DIGIT_BIT] = 1 << (b % DIGIT_BIT);
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
|
|
15
bn_mp_abs.c
15
bn_mp_abs.c
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -22,9 +22,16 @@ int
|
||||||
mp_abs (mp_int * a, mp_int * b)
|
mp_abs (mp_int * a, mp_int * b)
|
||||||
{
|
{
|
||||||
int res;
|
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;
|
b->sign = MP_ZPOS;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -82,7 +82,11 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
|
||||||
c->used = 1;
|
c->used = 1;
|
||||||
|
|
||||||
/* the result is a single digit */
|
/* 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
|
/* setup count so the clearing of oldused
|
||||||
* can fall through correctly
|
* can fall through correctly
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -24,9 +24,14 @@
|
||||||
void
|
void
|
||||||
mp_clamp (mp_int * a)
|
mp_clamp (mp_int * a)
|
||||||
{
|
{
|
||||||
|
/* decrease used while the most significant digit is
|
||||||
|
* zero.
|
||||||
|
*/
|
||||||
while (a->used > 0 && a->dp[a->used - 1] == 0) {
|
while (a->used > 0 && a->dp[a->used - 1] == 0) {
|
||||||
--(a->used);
|
--(a->used);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* reset the sign flag if used == 0 */
|
||||||
if (a->used == 0) {
|
if (a->used == 0) {
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
|
@ -18,6 +18,7 @@
|
||||||
void
|
void
|
||||||
mp_clear (mp_int * a)
|
mp_clear (mp_int * a)
|
||||||
{
|
{
|
||||||
|
/* only do anything if a hasn't been freed previously */
|
||||||
if (a->dp != NULL) {
|
if (a->dp != NULL) {
|
||||||
/* first zero the digits */
|
/* first zero the digits */
|
||||||
memset (a->dp, 0, sizeof (mp_digit) * a->used);
|
memset (a->dp, 0, sizeof (mp_digit) * a->used);
|
||||||
|
@ -28,5 +29,6 @@ mp_clear (mp_int * a)
|
||||||
/* reset members to make debugging easier */
|
/* reset members to make debugging easier */
|
||||||
a->dp = NULL;
|
a->dp = NULL;
|
||||||
a->alloc = a->used = 0;
|
a->alloc = a->used = 0;
|
||||||
|
a->sign = MP_ZPOS;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,28 @@
|
||||||
|
/* 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, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
|
*/
|
||||||
|
#include <tommath.h>
|
||||||
|
#include <stdarg.h>
|
||||||
|
|
||||||
|
void mp_clear_multi(mp_int *mp, ...)
|
||||||
|
{
|
||||||
|
mp_int* next_mp = mp;
|
||||||
|
va_list args;
|
||||||
|
va_start(args, mp);
|
||||||
|
while (next_mp != NULL) {
|
||||||
|
mp_clear(next_mp);
|
||||||
|
next_mp = va_arg(args, mp_int*);
|
||||||
|
}
|
||||||
|
va_end(args);
|
||||||
|
}
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -18,15 +18,17 @@
|
||||||
int
|
int
|
||||||
mp_cmp_d (mp_int * a, mp_digit b)
|
mp_cmp_d (mp_int * a, mp_digit b)
|
||||||
{
|
{
|
||||||
|
/* compare based on sign */
|
||||||
if (a->sign == MP_NEG) {
|
if (a->sign == MP_NEG) {
|
||||||
return MP_LT;
|
return MP_LT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* compare based on magnitude */
|
||||||
if (a->used > 1) {
|
if (a->used > 1) {
|
||||||
return MP_GT;
|
return MP_GT;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* compare the only digit of a to b */
|
||||||
if (a->dp[0] > b) {
|
if (a->dp[0] > b) {
|
||||||
return MP_GT;
|
return MP_GT;
|
||||||
} else if (a->dp[0] < b) {
|
} else if (a->dp[0] < b) {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -20,6 +20,7 @@ int mp_cnt_lsb(mp_int *a)
|
||||||
int x;
|
int x;
|
||||||
mp_digit q;
|
mp_digit q;
|
||||||
|
|
||||||
|
/* easy out */
|
||||||
if (mp_iszero(a) == 1) {
|
if (mp_iszero(a) == 1) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
16
bn_mp_copy.c
16
bn_mp_copy.c
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -26,8 +26,10 @@ mp_copy (mp_int * a, mp_int * b)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* grow dest */
|
/* grow dest */
|
||||||
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
|
if (b->alloc < a->used) {
|
||||||
return res;
|
if ((res = mp_grow (b, a->used)) != MP_OKAY) {
|
||||||
|
return res;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* zero b and copy the parameters over */
|
/* zero b and copy the parameters over */
|
||||||
|
@ -35,7 +37,11 @@ mp_copy (mp_int * a, mp_int * b)
|
||||||
register mp_digit *tmpa, *tmpb;
|
register mp_digit *tmpa, *tmpb;
|
||||||
|
|
||||||
/* pointer aliases */
|
/* pointer aliases */
|
||||||
|
|
||||||
|
/* source */
|
||||||
tmpa = a->dp;
|
tmpa = a->dp;
|
||||||
|
|
||||||
|
/* destination */
|
||||||
tmpb = b->dp;
|
tmpb = b->dp;
|
||||||
|
|
||||||
/* copy all the digits */
|
/* copy all the digits */
|
||||||
|
@ -48,6 +54,8 @@ mp_copy (mp_int * a, mp_int * b)
|
||||||
*tmpb++ = 0;
|
*tmpb++ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* copy used count and sign */
|
||||||
b->used = a->used;
|
b->used = a->used;
|
||||||
b->sign = a->sign;
|
b->sign = a->sign;
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -35,10 +35,17 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||||
w = 0;
|
w = 0;
|
||||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
||||||
|
|
||||||
if (w >= 3) {
|
if (w >= 3) {
|
||||||
|
/* multiply w by [1/3] */
|
||||||
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
|
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
|
||||||
|
|
||||||
|
/* now subtract 3 * [w/3] from w, to get the remainder */
|
||||||
w -= (t << ((mp_word)1)) + t;
|
w -= (t << ((mp_word)1)) + t;
|
||||||
|
|
||||||
|
/* fixup the remainder as required since
|
||||||
|
* the optimization is not exact.
|
||||||
|
*/
|
||||||
while (w >= 3) {
|
while (w >= 3) {
|
||||||
t += 1;
|
t += 1;
|
||||||
w -= 3;
|
w -= 3;
|
||||||
|
@ -48,11 +55,13 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||||
}
|
}
|
||||||
q.dp[ix] = (mp_digit)t;
|
q.dp[ix] = (mp_digit)t;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* [optional] store the remainder */
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
*d = (mp_digit)w;
|
*d = (mp_digit)w;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* [optional] store the quotient */
|
||||||
if (c != NULL) {
|
if (c != NULL) {
|
||||||
mp_clamp(&q);
|
mp_clamp(&q);
|
||||||
mp_exch(&q, c);
|
mp_exch(&q, c);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,34 +1,37 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* determines if a number is a valid DR modulus */
|
/* determines if a number is a valid DR modulus */
|
||||||
int mp_dr_is_modulus(mp_int *a)
|
int mp_dr_is_modulus(mp_int *a)
|
||||||
{
|
{
|
||||||
int ix;
|
int ix;
|
||||||
|
|
||||||
/* must be at least two digits */
|
/* must be at least two digits */
|
||||||
if (a->used < 2) {
|
if (a->used < 2) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (ix = 1; ix < a->used; ix++) {
|
/* must be of the form b**k - a [a <= b] so all
|
||||||
if (a->dp[ix] != MP_MASK) {
|
* but the first digit must be equal to -1 (mod b).
|
||||||
return 0;
|
*/
|
||||||
}
|
for (ix = 1; ix < a->used; ix++) {
|
||||||
}
|
if (a->dp[ix] != MP_MASK) {
|
||||||
return 1;
|
return 0;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
return 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -57,7 +57,7 @@ top:
|
||||||
/* set carry to zero */
|
/* set carry to zero */
|
||||||
mu = 0;
|
mu = 0;
|
||||||
|
|
||||||
/* compute (x mod B**m) + mp * [x/B**m] inline and inplace */
|
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */
|
||||||
for (i = 0; i < m; i++) {
|
for (i = 0; i < m; i++) {
|
||||||
r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
|
r = ((mp_word)*tmpx2++) * ((mp_word)k) + *tmpx1 + mu;
|
||||||
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
*tmpx1++ = (mp_digit)(r & MP_MASK);
|
||||||
|
|
|
@ -1,26 +1,26 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* determines the setup value */
|
/* determines the setup value */
|
||||||
void mp_dr_setup(mp_int *a, mp_digit *d)
|
void mp_dr_setup(mp_int *a, mp_digit *d)
|
||||||
{
|
{
|
||||||
/* the casts are required if DIGIT_BIT is one less than
|
/* the casts are required if DIGIT_BIT is one less than
|
||||||
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
|
||||||
*/
|
*/
|
||||||
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
|
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
|
||||||
((mp_word)a->dp[0]));
|
((mp_word)a->dp[0]));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -22,7 +22,7 @@ mp_exch (mp_int * a, mp_int * b)
|
||||||
{
|
{
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
|
||||||
t = *a;
|
t = *a;
|
||||||
*a = *b;
|
*a = *b;
|
||||||
*b = t;
|
*b = t;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -60,7 +60,10 @@ mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* is it a DR modulus? */
|
||||||
dr = mp_dr_is_modulus(P);
|
dr = mp_dr_is_modulus(P);
|
||||||
|
|
||||||
|
/* if not, is it a uDR modulus? */
|
||||||
if (dr == 0) {
|
if (dr == 0) {
|
||||||
dr = mp_reduce_is_2k(P) << 1;
|
dr = mp_reduce_is_2k(P) << 1;
|
||||||
}
|
}
|
||||||
|
@ -69,6 +72,7 @@ mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||||
if (mp_isodd (P) == 1 || dr != 0) {
|
if (mp_isodd (P) == 1 || dr != 0) {
|
||||||
return mp_exptmod_fast (G, X, P, Y, dr);
|
return mp_exptmod_fast (G, X, P, Y, dr);
|
||||||
} else {
|
} else {
|
||||||
|
/* otherwise use the generic Barrett reduction technique */
|
||||||
return s_mp_exptmod (G, X, P, Y);
|
return s_mp_exptmod (G, X, P, Y);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -94,15 +94,15 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
P->used < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||||
redux = fast_mp_montgomery_reduce;
|
redux = fast_mp_montgomery_reduce;
|
||||||
} else {
|
} else {
|
||||||
/* use slower baselien method */
|
/* use slower baseline Montgomery method */
|
||||||
redux = mp_montgomery_reduce;
|
redux = mp_montgomery_reduce;
|
||||||
}
|
}
|
||||||
} else if (redmode == 1) {
|
} else if (redmode == 1) {
|
||||||
/* setup DR reduction */
|
/* setup DR reduction for moduli of the form B**k - b */
|
||||||
mp_dr_setup(P, &mp);
|
mp_dr_setup(P, &mp);
|
||||||
redux = mp_dr_reduce;
|
redux = mp_dr_reduce;
|
||||||
} else {
|
} else {
|
||||||
/* setup 2k reduction */
|
/* setup DR reduction for moduli of the form 2**k - b */
|
||||||
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
|
if ((err = mp_reduce_2k_setup(P, &mp)) != MP_OKAY) {
|
||||||
goto __M;
|
goto __M;
|
||||||
}
|
}
|
||||||
|
@ -111,7 +111,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
|
|
||||||
/* setup result */
|
/* setup result */
|
||||||
if ((err = mp_init (&res)) != MP_OKAY) {
|
if ((err = mp_init (&res)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto __M;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create M table
|
/* create M table
|
||||||
|
@ -173,15 +173,17 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* grab next digit as required */
|
/* grab next digit as required */
|
||||||
if (--bitcnt == 0) {
|
if (--bitcnt == 0) {
|
||||||
|
/* if digidx == -1 we are out of digits so break */
|
||||||
if (digidx == -1) {
|
if (digidx == -1) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
buf = X->dp[digidx--];
|
/* read next digit and reset bitcnt */
|
||||||
bitcnt = (int) DIGIT_BIT;
|
buf = X->dp[digidx--];
|
||||||
|
bitcnt = (int)DIGIT_BIT;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* grab the next msb from the exponent */
|
/* grab the next msb from the exponent */
|
||||||
y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
|
y = (mp_digit)(buf >> (DIGIT_BIT - 1)) & 1;
|
||||||
buf <<= (mp_digit)1;
|
buf <<= (mp_digit)1;
|
||||||
|
|
||||||
/* if the bit is zero and mode == 0 then we ignore it
|
/* if the bit is zero and mode == 0 then we ignore it
|
||||||
|
@ -206,7 +208,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
|
|
||||||
/* else we add it to the window */
|
/* else we add it to the window */
|
||||||
bitbuf |= (y << (winsize - ++bitcpy));
|
bitbuf |= (y << (winsize - ++bitcpy));
|
||||||
mode = 2;
|
mode = 2;
|
||||||
|
|
||||||
if (bitcpy == winsize) {
|
if (bitcpy == winsize) {
|
||||||
/* ok window is filled so square as required and multiply */
|
/* ok window is filled so square as required and multiply */
|
||||||
|
@ -231,7 +233,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
/* empty window and reset */
|
/* empty window and reset */
|
||||||
bitcpy = 0;
|
bitcpy = 0;
|
||||||
bitbuf = 0;
|
bitbuf = 0;
|
||||||
mode = 1;
|
mode = 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -246,6 +248,7 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
goto __RES;
|
goto __RES;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get next bit of the window */
|
||||||
bitbuf <<= 1;
|
bitbuf <<= 1;
|
||||||
if ((bitbuf & (1 << winsize)) != 0) {
|
if ((bitbuf & (1 << winsize)) != 0) {
|
||||||
/* then multiply */
|
/* then multiply */
|
||||||
|
@ -260,12 +263,18 @@ mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||||
}
|
}
|
||||||
|
|
||||||
if (redmode == 0) {
|
if (redmode == 0) {
|
||||||
/* fixup result if Montgomery reduction is used */
|
/* fixup result if Montgomery reduction is used
|
||||||
|
* recall that any value in a Montgomery system is
|
||||||
|
* actually multiplied by R mod n. So we have
|
||||||
|
* to reduce one more time to cancel out the factor
|
||||||
|
* of R.
|
||||||
|
*/
|
||||||
if ((err = mp_montgomery_reduce (&res, P, mp)) != MP_OKAY) {
|
if ((err = mp_montgomery_reduce (&res, P, mp)) != MP_OKAY) {
|
||||||
goto __RES;
|
goto __RES;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* swap res with Y */
|
||||||
mp_exch (&res, Y);
|
mp_exch (&res, Y);
|
||||||
err = MP_OKAY;
|
err = MP_OKAY;
|
||||||
__RES:mp_clear (&res);
|
__RES:mp_clear (&res);
|
||||||
|
|
122
bn_mp_fread.c
122
bn_mp_fread.c
|
@ -1,61 +1,61 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* read a bigint from a file stream in ASCII */
|
/* read a bigint from a file stream in ASCII */
|
||||||
int mp_fread(mp_int *a, int radix, FILE *stream)
|
int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||||
{
|
{
|
||||||
int err, ch, neg, y;
|
int err, ch, neg, y;
|
||||||
|
|
||||||
/* clear a */
|
/* clear a */
|
||||||
mp_zero(a);
|
mp_zero(a);
|
||||||
|
|
||||||
/* if first digit is - then set negative */
|
/* if first digit is - then set negative */
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
if (ch == '-') {
|
if (ch == '-') {
|
||||||
neg = MP_NEG;
|
neg = MP_NEG;
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
} else {
|
} else {
|
||||||
neg = MP_ZPOS;
|
neg = MP_ZPOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (;;) {
|
for (;;) {
|
||||||
/* find y in the radix map */
|
/* find y in the radix map */
|
||||||
for (y = 0; y < radix; y++) {
|
for (y = 0; y < radix; y++) {
|
||||||
if (mp_s_rmap[y] == ch) {
|
if (mp_s_rmap[y] == ch) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (y == radix) {
|
if (y == radix) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* shift up and add */
|
/* shift up and add */
|
||||||
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
|
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
|
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
ch = fgetc(stream);
|
ch = fgetc(stream);
|
||||||
}
|
}
|
||||||
if (mp_cmp_d(a, 0) != MP_EQ) {
|
if (mp_cmp_d(a, 0) != MP_EQ) {
|
||||||
a->sign = neg;
|
a->sign = neg;
|
||||||
}
|
}
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
37
bn_mp_gcd.c
37
bn_mp_gcd.c
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -23,16 +23,21 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||||
|
|
||||||
/* either zero than gcd is the largest */
|
/* either zero than gcd is the largest */
|
||||||
if (mp_iszero (a) == 1 && mp_iszero (b) == 0) {
|
if (mp_iszero (a) == 1 && mp_iszero (b) == 0) {
|
||||||
return mp_copy (b, c);
|
return mp_abs (b, c);
|
||||||
}
|
}
|
||||||
if (mp_iszero (a) == 0 && mp_iszero (b) == 1) {
|
if (mp_iszero (a) == 0 && mp_iszero (b) == 1) {
|
||||||
return mp_copy (a, c);
|
return mp_abs (a, c);
|
||||||
}
|
}
|
||||||
if (mp_iszero (a) == 1 && mp_iszero (b) == 1) {
|
|
||||||
|
/* optimized. At this point if a == 0 then
|
||||||
|
* b must equal zero too
|
||||||
|
*/
|
||||||
|
if (mp_iszero (a) == 1) {
|
||||||
mp_zero(c);
|
mp_zero(c);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* get copies of a and b we can modify */
|
||||||
if ((res = mp_init_copy (&u, a)) != MP_OKAY) {
|
if ((res = mp_init_copy (&u, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
@ -49,12 +54,15 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||||
v_lsb = mp_cnt_lsb(&v);
|
v_lsb = mp_cnt_lsb(&v);
|
||||||
k = MIN(u_lsb, v_lsb);
|
k = MIN(u_lsb, v_lsb);
|
||||||
|
|
||||||
if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
|
if (k > 0) {
|
||||||
goto __V;
|
/* divide the power of two out */
|
||||||
}
|
if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
|
||||||
|
goto __V;
|
||||||
|
}
|
||||||
|
|
||||||
if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
|
||||||
goto __V;
|
goto __V;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* divide any remaining factors of two out */
|
/* divide any remaining factors of two out */
|
||||||
|
@ -69,10 +77,11 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||||
goto __V;
|
goto __V;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
while (mp_iszero(&v) == 0) {
|
while (mp_iszero(&v) == 0) {
|
||||||
/* make sure v is the largest */
|
/* make sure v is the largest */
|
||||||
if (mp_cmp_mag(&u, &v) == MP_GT) {
|
if (mp_cmp_mag(&u, &v) == MP_GT) {
|
||||||
|
/* swap u and v to make sure v is >= u */
|
||||||
mp_exch(&u, &v);
|
mp_exch(&u, &v);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -86,10 +95,10 @@ mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||||
goto __V;
|
goto __V;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* multiply by 2**k which we divided out at the beginning */
|
/* multiply by 2**k which we divided out at the beginning */
|
||||||
if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
|
if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
|
||||||
goto __V;
|
goto __V;
|
||||||
}
|
}
|
||||||
c->sign = MP_ZPOS;
|
c->sign = MP_ZPOS;
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -23,7 +23,7 @@ mp_grow (mp_int * a, int size)
|
||||||
/* if the alloc size is smaller alloc more ram */
|
/* if the alloc size is smaller alloc more ram */
|
||||||
if (a->alloc < size) {
|
if (a->alloc < size) {
|
||||||
/* ensure there are always at least MP_PREC digits extra on top */
|
/* ensure there are always at least MP_PREC digits extra on top */
|
||||||
size += (MP_PREC * 2) - (size & (MP_PREC - 1));
|
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||||
|
|
||||||
a->dp = OPT_CAST realloc (a->dp, sizeof (mp_digit) * size);
|
a->dp = OPT_CAST realloc (a->dp, sizeof (mp_digit) * size);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
|
|
|
@ -1,10 +1,10 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
|
@ -18,7 +18,7 @@
|
||||||
int
|
int
|
||||||
mp_init (mp_int * a)
|
mp_init (mp_int * a)
|
||||||
{
|
{
|
||||||
/* allocate ram required and clear it */
|
/* allocate memory required and clear it */
|
||||||
a->dp = OPT_CAST calloc (sizeof (mp_digit), MP_PREC);
|
a->dp = OPT_CAST calloc (sizeof (mp_digit), MP_PREC);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -49,16 +49,4 @@ int mp_init_multi(mp_int *mp, ...)
|
||||||
}
|
}
|
||||||
va_end(args);
|
va_end(args);
|
||||||
return res; /* Assumed ok, if error flagged above. */
|
return res; /* Assumed ok, if error flagged above. */
|
||||||
}
|
}
|
||||||
|
|
||||||
void mp_clear_multi(mp_int *mp, ...)
|
|
||||||
{
|
|
||||||
mp_int* next_mp = mp;
|
|
||||||
va_list args;
|
|
||||||
va_start(args, mp);
|
|
||||||
while (next_mp != NULL) {
|
|
||||||
mp_clear(next_mp);
|
|
||||||
next_mp = va_arg(args, mp_int*);
|
|
||||||
}
|
|
||||||
va_end(args);
|
|
||||||
}
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -14,22 +14,21 @@
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* init a mp_init and grow it to a given size */
|
/* init an mp_init for a given size */
|
||||||
int
|
int
|
||||||
mp_init_size (mp_int * a, int size)
|
mp_init_size (mp_int * a, int size)
|
||||||
{
|
{
|
||||||
|
|
||||||
/* pad size so there are always extra digits */
|
/* pad size so there are always extra digits */
|
||||||
size += (MP_PREC * 2) - (size & (MP_PREC - 1));
|
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||||
|
|
||||||
/* alloc mem */
|
/* alloc mem */
|
||||||
a->dp = OPT_CAST calloc (sizeof (mp_digit), size);
|
a->dp = OPT_CAST calloc (sizeof (mp_digit), size);
|
||||||
if (a->dp == NULL) {
|
if (a->dp == NULL) {
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
}
|
}
|
||||||
a->used = 0;
|
a->used = 0;
|
||||||
a->alloc = size;
|
a->alloc = size;
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -24,6 +24,11 @@ mp_jacobi (mp_int * a, mp_int * p, int *c)
|
||||||
int k, s, r, res;
|
int k, s, r, res;
|
||||||
mp_digit residue;
|
mp_digit residue;
|
||||||
|
|
||||||
|
/* if p <= 0 return MP_VAL */
|
||||||
|
if (mp_cmp_d(p, 0) != MP_GT) {
|
||||||
|
return MP_VAL;
|
||||||
|
}
|
||||||
|
|
||||||
/* step 1. if a == 0, return 0 */
|
/* step 1. if a == 0, return 0 */
|
||||||
if (mp_iszero (a) == 1) {
|
if (mp_iszero (a) == 1) {
|
||||||
*c = 0;
|
*c = 0;
|
||||||
|
@ -37,7 +42,7 @@ mp_jacobi (mp_int * a, mp_int * p, int *c)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* default */
|
/* default */
|
||||||
k = s = 0;
|
s = 0;
|
||||||
|
|
||||||
/* step 3. write a = a1 * 2**k */
|
/* step 3. write a = a1 * 2**k */
|
||||||
if ((res = mp_init_copy (&a1, a)) != MP_OKAY) {
|
if ((res = mp_init_copy (&a1, a)) != MP_OKAY) {
|
||||||
|
@ -48,11 +53,10 @@ mp_jacobi (mp_int * a, mp_int * p, int *c)
|
||||||
goto __A1;
|
goto __A1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (mp_iseven (&a1) == 1) {
|
/* divide out larger power of two */
|
||||||
k = k + 1;
|
k = mp_cnt_lsb(&a1);
|
||||||
if ((res = mp_div_2 (&a1, &a1)) != MP_OKAY) {
|
if ((res = mp_div_2d(&a1, k, &a1, NULL)) != MP_OKAY) {
|
||||||
goto __P1;
|
goto __P1;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/* step 4. if e is even set s=1 */
|
/* step 4. if e is even set s=1 */
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -20,7 +20,6 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
||||||
{
|
{
|
||||||
int x, res;
|
int x, res;
|
||||||
|
|
||||||
|
|
||||||
/* if b is <= 0 then zero the int */
|
/* if b is <= 0 then zero the int */
|
||||||
if (b <= 0) {
|
if (b <= 0) {
|
||||||
mp_zero (c);
|
mp_zero (c);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -20,13 +20,14 @@ mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||||
{
|
{
|
||||||
int res, neg;
|
int res, neg;
|
||||||
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
|
||||||
|
|
||||||
|
/* use Toom-Cook? */
|
||||||
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||||
res = mp_toom_mul(a, b, c);
|
res = mp_toom_mul(a, b, c);
|
||||||
|
/* use Karatsuba? */
|
||||||
} else if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
} else if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
||||||
res = mp_karatsuba_mul (a, b, c);
|
res = mp_karatsuba_mul (a, b, c);
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
/* can we use the fast multiplier?
|
/* can we use the fast multiplier?
|
||||||
*
|
*
|
||||||
* The fast multiplier can be used if the output will
|
* The fast multiplier can be used if the output will
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -61,7 +61,7 @@ mp_mul_2 (mp_int * a, mp_int * b)
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
/* add a MSB which is always 1 at this point */
|
/* add a MSB which is always 1 at this point */
|
||||||
*tmpb = 1;
|
*tmpb = 1;
|
||||||
++b->used;
|
++(b->used);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now zero any excess digits on the destination
|
/* now zero any excess digits on the destination
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -72,7 +72,7 @@ mp_mul_2d (mp_int * a, int b, mp_int * c)
|
||||||
|
|
||||||
/* set final carry */
|
/* set final carry */
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
c->dp[c->used++] = r;
|
c->dp[(c->used)++] = r;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
mp_clamp (c);
|
mp_clamp (c);
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -48,7 +48,7 @@ mp_n_root (mp_int * a, mp_digit b, mp_int * c)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if a is negative fudge the sign but keep track */
|
/* if a is negative fudge the sign but keep track */
|
||||||
neg = a->sign;
|
neg = a->sign;
|
||||||
a->sign = MP_ZPOS;
|
a->sign = MP_ZPOS;
|
||||||
|
|
||||||
/* t2 = 2 */
|
/* t2 = 2 */
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -28,7 +28,7 @@ mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
||||||
mp_int t;
|
mp_int t;
|
||||||
int err;
|
int err;
|
||||||
|
|
||||||
/* default to fail */
|
/* default to composite */
|
||||||
*result = 0;
|
*result = 0;
|
||||||
|
|
||||||
/* ensure b > 1 */
|
/* ensure b > 1 */
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -31,9 +31,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
||||||
}
|
}
|
||||||
|
|
||||||
/* force positive */
|
/* force positive */
|
||||||
if (a->sign == MP_NEG) {
|
a->sign = MP_ZPOS;
|
||||||
a->sign = MP_ZPOS;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* simple algo if a is less than the largest prime in the table */
|
/* simple algo if a is less than the largest prime in the table */
|
||||||
if (mp_cmp_d(a, __prime_tab[PRIME_SIZE-1]) == MP_LT) {
|
if (mp_cmp_d(a, __prime_tab[PRIME_SIZE-1]) == MP_LT) {
|
||||||
|
@ -137,8 +135,8 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
|
||||||
goto __ERR;
|
goto __ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if step == MAX then skip test */
|
/* if didn't pass sieve and step == MAX then skip test */
|
||||||
if (step >= ((((mp_digit)1)<<DIGIT_BIT) - kstep)) {
|
if (y == 1 && step >= ((((mp_digit)1)<<DIGIT_BIT) - kstep)) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,54 +1,54 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* returns size of ASCII reprensentation */
|
/* returns size of ASCII reprensentation */
|
||||||
int
|
int
|
||||||
mp_radix_size (mp_int * a, int radix)
|
mp_radix_size (mp_int * a, int radix)
|
||||||
{
|
{
|
||||||
int res, digs;
|
int res, digs;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
mp_digit d;
|
mp_digit d;
|
||||||
|
|
||||||
/* special case for binary */
|
/* special case for binary */
|
||||||
if (radix == 2) {
|
if (radix == 2) {
|
||||||
return mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1;
|
return mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (radix < 2 || radix > 64) {
|
if (radix < 2 || radix > 64) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
digs = 0;
|
digs = 0;
|
||||||
if (t.sign == MP_NEG) {
|
if (t.sign == MP_NEG) {
|
||||||
++digs;
|
++digs;
|
||||||
t.sign = MP_ZPOS;
|
t.sign = MP_ZPOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (mp_iszero (&t) == 0) {
|
while (mp_iszero (&t) == 0) {
|
||||||
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
||||||
mp_clear (&t);
|
mp_clear (&t);
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
++digs;
|
++digs;
|
||||||
}
|
}
|
||||||
mp_clear (&t);
|
mp_clear (&t);
|
||||||
return digs + 1;
|
return digs + 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,18 +1,18 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* chars used in radix conversions */
|
/* chars used in radix conversions */
|
||||||
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
const char *mp_s_rmap = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/";
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,77 +1,77 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* read a string [ASCII] in a given radix */
|
/* read a string [ASCII] in a given radix */
|
||||||
int
|
int
|
||||||
mp_read_radix (mp_int * a, char *str, int radix)
|
mp_read_radix (mp_int * a, char *str, int radix)
|
||||||
{
|
{
|
||||||
int y, res, neg;
|
int y, res, neg;
|
||||||
char ch;
|
char ch;
|
||||||
|
|
||||||
/* make sure the radix is ok */
|
/* make sure the radix is ok */
|
||||||
if (radix < 2 || radix > 64) {
|
if (radix < 2 || radix > 64) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if the leading digit is a
|
/* if the leading digit is a
|
||||||
* minus set the sign to negative.
|
* minus set the sign to negative.
|
||||||
*/
|
*/
|
||||||
if (*str == '-') {
|
if (*str == '-') {
|
||||||
++str;
|
++str;
|
||||||
neg = MP_NEG;
|
neg = MP_NEG;
|
||||||
} else {
|
} else {
|
||||||
neg = MP_ZPOS;
|
neg = MP_ZPOS;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the integer to the default of zero */
|
/* set the integer to the default of zero */
|
||||||
mp_zero (a);
|
mp_zero (a);
|
||||||
|
|
||||||
/* process each digit of the string */
|
/* process each digit of the string */
|
||||||
while (*str) {
|
while (*str) {
|
||||||
/* if the radix < 36 the conversion is case insensitive
|
/* if the radix < 36 the conversion is case insensitive
|
||||||
* this allows numbers like 1AB and 1ab to represent the same value
|
* this allows numbers like 1AB and 1ab to represent the same value
|
||||||
* [e.g. in hex]
|
* [e.g. in hex]
|
||||||
*/
|
*/
|
||||||
ch = (char) ((radix < 36) ? toupper (*str) : *str);
|
ch = (char) ((radix < 36) ? toupper (*str) : *str);
|
||||||
for (y = 0; y < 64; y++) {
|
for (y = 0; y < 64; y++) {
|
||||||
if (ch == mp_s_rmap[y]) {
|
if (ch == mp_s_rmap[y]) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* if the char was found in the map
|
/* if the char was found in the map
|
||||||
* and is less than the given radix add it
|
* and is less than the given radix add it
|
||||||
* to the number, otherwise exit the loop.
|
* to the number, otherwise exit the loop.
|
||||||
*/
|
*/
|
||||||
if (y < radix) {
|
if (y < radix) {
|
||||||
if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
|
if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) {
|
if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
++str;
|
++str;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set the sign only if a != 0 */
|
/* set the sign only if a != 0 */
|
||||||
if (mp_iszero(a) != 1) {
|
if (mp_iszero(a) != 1) {
|
||||||
a->sign = neg;
|
a->sign = neg;
|
||||||
}
|
}
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,56 +1,56 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* reduces a modulo n where n is of the form 2**p - k */
|
/* reduces a modulo n where n is of the form 2**p - k */
|
||||||
int
|
int
|
||||||
mp_reduce_2k(mp_int *a, mp_int *n, mp_digit k)
|
mp_reduce_2k(mp_int *a, mp_int *n, mp_digit k)
|
||||||
{
|
{
|
||||||
mp_int q;
|
mp_int q;
|
||||||
int p, res;
|
int p, res;
|
||||||
|
|
||||||
if ((res = mp_init(&q)) != MP_OKAY) {
|
if ((res = mp_init(&q)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = mp_count_bits(n);
|
p = mp_count_bits(n);
|
||||||
top:
|
top:
|
||||||
/* q = a/2**p, a = a mod 2**p */
|
/* q = a/2**p, a = a mod 2**p */
|
||||||
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (k != 1) {
|
if (k != 1) {
|
||||||
/* q = q * k */
|
/* q = q * k */
|
||||||
if ((res = mp_mul_d(&q, k, &q)) != MP_OKAY) {
|
if ((res = mp_mul_d(&q, k, &q)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* a = a + q */
|
/* a = a + q */
|
||||||
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||||
s_mp_sub(a, n, a);
|
s_mp_sub(a, n, a);
|
||||||
goto top;
|
goto top;
|
||||||
}
|
}
|
||||||
|
|
||||||
ERR:
|
ERR:
|
||||||
mp_clear(&q);
|
mp_clear(&q);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,42 +1,42 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* determines the setup value */
|
/* determines the setup value */
|
||||||
int
|
int
|
||||||
mp_reduce_2k_setup(mp_int *a, mp_digit *d)
|
mp_reduce_2k_setup(mp_int *a, mp_digit *d)
|
||||||
{
|
{
|
||||||
int res, p;
|
int res, p;
|
||||||
mp_int tmp;
|
mp_int tmp;
|
||||||
|
|
||||||
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
p = mp_count_bits(a);
|
p = mp_count_bits(a);
|
||||||
if ((res = mp_2expt(&tmp, p)) != MP_OKAY) {
|
if ((res = mp_2expt(&tmp, p)) != MP_OKAY) {
|
||||||
mp_clear(&tmp);
|
mp_clear(&tmp);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) {
|
if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) {
|
||||||
mp_clear(&tmp);
|
mp_clear(&tmp);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
*d = tmp.dp[0];
|
*d = tmp.dp[0];
|
||||||
mp_clear(&tmp);
|
mp_clear(&tmp);
|
||||||
return MP_OKAY;
|
return MP_OKAY;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,38 +1,38 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* determines if mp_reduce_2k can be used */
|
/* determines if mp_reduce_2k can be used */
|
||||||
int
|
int
|
||||||
mp_reduce_is_2k(mp_int *a)
|
mp_reduce_is_2k(mp_int *a)
|
||||||
{
|
{
|
||||||
int ix, iy;
|
int ix, iy;
|
||||||
|
|
||||||
if (a->used == 0) {
|
if (a->used == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
} else if (a->used == 1) {
|
} else if (a->used == 1) {
|
||||||
return 1;
|
return 1;
|
||||||
} else if (a->used > 1) {
|
} else if (a->used > 1) {
|
||||||
iy = mp_count_bits(a);
|
iy = mp_count_bits(a);
|
||||||
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
||||||
if ((a->dp[ix/DIGIT_BIT] &
|
if ((a->dp[ix/DIGIT_BIT] &
|
||||||
((mp_digit)1 << (mp_digit)(ix % DIGIT_BIT))) == 0) {
|
((mp_digit)1 << (mp_digit)(ix % DIGIT_BIT))) == 0) {
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,29 +1,29 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* pre-calculate the value required for Barrett reduction
|
/* pre-calculate the value required for Barrett reduction
|
||||||
* For a given modulus "b" it calulates the value required in "a"
|
* For a given modulus "b" it calulates the value required in "a"
|
||||||
*/
|
*/
|
||||||
int
|
int
|
||||||
mp_reduce_setup (mp_int * a, mp_int * b)
|
mp_reduce_setup (mp_int * a, mp_int * b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
|
||||||
if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
|
if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
return mp_div (a, b, a, NULL);
|
return mp_div (a, b, a, NULL);
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -20,5 +20,5 @@ mp_set (mp_int * a, mp_digit b)
|
||||||
{
|
{
|
||||||
mp_zero (a);
|
mp_zero (a);
|
||||||
a->dp[0] = b & MP_MASK;
|
a->dp[0] = b & MP_MASK;
|
||||||
a->used = (a->dp[0] != 0) ? 1 : 0;
|
a->used = (a->dp[0] != 0) ? 1 : 0;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -19,13 +19,14 @@ int
|
||||||
mp_sqr (mp_int * a, mp_int * b)
|
mp_sqr (mp_int * a, mp_int * b)
|
||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
|
/* use Toom-Cook? */
|
||||||
if (a->used >= TOOM_SQR_CUTOFF) {
|
if (a->used >= TOOM_SQR_CUTOFF) {
|
||||||
res = mp_toom_sqr(a, b);
|
res = mp_toom_sqr(a, b);
|
||||||
|
/* Karatsuba? */
|
||||||
} else if (a->used >= KARATSUBA_SQR_CUTOFF) {
|
} else if (a->used >= KARATSUBA_SQR_CUTOFF) {
|
||||||
res = mp_karatsuba_sqr (a, b);
|
res = mp_karatsuba_sqr (a, b);
|
||||||
} else {
|
} else {
|
||||||
|
/* can we use the fast comba multiplier? */
|
||||||
/* can we use the fast multiplier? */
|
|
||||||
if ((a->used * 2 + 1) < MP_WARRAY &&
|
if ((a->used * 2 + 1) < MP_WARRAY &&
|
||||||
a->used <
|
a->used <
|
||||||
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
|
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -21,7 +21,6 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
|
||||||
int res;
|
int res;
|
||||||
mp_int t;
|
mp_int t;
|
||||||
|
|
||||||
|
|
||||||
if ((res = mp_init (&t)) != MP_OKAY) {
|
if ((res = mp_init (&t)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
@ -45,7 +45,11 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
|
||||||
|
|
||||||
/* if a <= b simply fix the single digit */
|
/* if a <= b simply fix the single digit */
|
||||||
if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) {
|
if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) {
|
||||||
*tmpc++ = b - *tmpa;
|
if (a->used == 1) {
|
||||||
|
*tmpc++ = b - *tmpa;
|
||||||
|
} else {
|
||||||
|
*tmpc++ = b;
|
||||||
|
}
|
||||||
ix = 1;
|
ix = 1;
|
||||||
|
|
||||||
/* negative/1digit */
|
/* negative/1digit */
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
546
bn_mp_toom_mul.c
546
bn_mp_toom_mul.c
|
@ -1,273 +1,273 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* multiplication using the Toom-Cook 3-way algorithm */
|
/* multiplication using the Toom-Cook 3-way algorithm */
|
||||||
int
|
int
|
||||||
mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
mp_toom_mul(mp_int *a, mp_int *b, mp_int *c)
|
||||||
{
|
{
|
||||||
mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
|
mp_int w0, w1, w2, w3, w4, tmp1, tmp2, a0, a1, a2, b0, b1, b2;
|
||||||
int res, B;
|
int res, B;
|
||||||
|
|
||||||
/* init temps */
|
/* init temps */
|
||||||
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
|
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4,
|
||||||
&a0, &a1, &a2, &b0, &b1,
|
&a0, &a1, &a2, &b0, &b1,
|
||||||
&b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
|
&b2, &tmp1, &tmp2, NULL)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* B */
|
/* B */
|
||||||
B = MIN(a->used, b->used) / 3;
|
B = MIN(a->used, b->used) / 3;
|
||||||
|
|
||||||
/* a = a2 * B**2 + a1 * B + a0 */
|
/* a = a2 * B**2 + a1 * B + a0 */
|
||||||
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
|
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_copy(a, &a1)) != MP_OKAY) {
|
if ((res = mp_copy(a, &a1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
mp_rshd(&a1, B);
|
mp_rshd(&a1, B);
|
||||||
mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
|
mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
|
||||||
|
|
||||||
if ((res = mp_copy(a, &a2)) != MP_OKAY) {
|
if ((res = mp_copy(a, &a2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
mp_rshd(&a2, B*2);
|
mp_rshd(&a2, B*2);
|
||||||
|
|
||||||
/* b = b2 * B**2 + b1 * B + b0 */
|
/* b = b2 * B**2 + b1 * B + b0 */
|
||||||
if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
|
if ((res = mp_mod_2d(b, DIGIT_BIT * B, &b0)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_copy(b, &b1)) != MP_OKAY) {
|
if ((res = mp_copy(b, &b1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
mp_rshd(&b1, B);
|
mp_rshd(&b1, B);
|
||||||
mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
|
mp_mod_2d(&b1, DIGIT_BIT * B, &b1);
|
||||||
|
|
||||||
if ((res = mp_copy(b, &b2)) != MP_OKAY) {
|
if ((res = mp_copy(b, &b2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
mp_rshd(&b2, B*2);
|
mp_rshd(&b2, B*2);
|
||||||
|
|
||||||
/* w0 = a0*b0 */
|
/* w0 = a0*b0 */
|
||||||
if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
|
if ((res = mp_mul(&a0, &b0, &w0)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* w4 = a2 * b2 */
|
/* w4 = a2 * b2 */
|
||||||
if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
|
if ((res = mp_mul(&a2, &b2, &w4)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
|
/* w1 = (a2 + 2(a1 + 2a0))(b2 + 2(b1 + 2b0)) */
|
||||||
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
|
if ((res = mp_mul_2(&b0, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
|
if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
|
if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
|
if ((res = mp_add(&tmp2, &b2, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
|
if ((res = mp_mul(&tmp1, &tmp2, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
|
/* w3 = (a0 + 2(a1 + 2a2))(b0 + 2(b1 + 2b2)) */
|
||||||
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
|
if ((res = mp_mul_2(&b2, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
|
if ((res = mp_add(&tmp2, &b1, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
|
if ((res = mp_mul_2(&tmp2, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
|
if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
|
if ((res = mp_mul(&tmp1, &tmp2, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
|
/* w2 = (a2 + a1 + a0)(b2 + b1 + b0) */
|
||||||
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&b2, &b1, &tmp2)) != MP_OKAY) {
|
if ((res = mp_add(&b2, &b1, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
|
if ((res = mp_add(&tmp2, &b0, &tmp2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
|
if ((res = mp_mul(&tmp1, &tmp2, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now solve the matrix
|
/* now solve the matrix
|
||||||
|
|
||||||
0 0 0 0 1
|
0 0 0 0 1
|
||||||
1 2 4 8 16
|
1 2 4 8 16
|
||||||
1 1 1 1 1
|
1 1 1 1 1
|
||||||
16 8 4 2 1
|
16 8 4 2 1
|
||||||
1 0 0 0 0
|
1 0 0 0 0
|
||||||
|
|
||||||
using 12 subtractions, 4 shifts,
|
using 12 subtractions, 4 shifts,
|
||||||
2 small divisions and 1 small multiplication
|
2 small divisions and 1 small multiplication
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* r1 - r4 */
|
/* r1 - r4 */
|
||||||
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - r0 */
|
/* r3 - r0 */
|
||||||
if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1/2 */
|
/* r1/2 */
|
||||||
if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
|
if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3/2 */
|
/* r3/2 */
|
||||||
if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
|
if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r2 - r0 - r4 */
|
/* r2 - r0 - r4 */
|
||||||
if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1 - r2 */
|
/* r1 - r2 */
|
||||||
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - r2 */
|
/* r3 - r2 */
|
||||||
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1 - 8r0 */
|
/* r1 - 8r0 */
|
||||||
if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - 8r4 */
|
/* r3 - 8r4 */
|
||||||
if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* 3r2 - r1 - r3 */
|
/* 3r2 - r1 - r3 */
|
||||||
if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
|
if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1 - r2 */
|
/* r1 - r2 */
|
||||||
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - r2 */
|
/* r3 - r2 */
|
||||||
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1/3 */
|
/* r1/3 */
|
||||||
if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
|
if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3/3 */
|
/* r3/3 */
|
||||||
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
|
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* at this point shift W[n] by B*n */
|
/* at this point shift W[n] by B*n */
|
||||||
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
|
if ((res = mp_add(&w0, &w1, c)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, c, c)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
ERR:
|
ERR:
|
||||||
mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
|
mp_clear_multi(&w0, &w1, &w2, &w3, &w4,
|
||||||
&a0, &a1, &a2, &b0, &b1,
|
&a0, &a1, &a2, &b0, &b1,
|
||||||
&b2, &tmp1, &tmp2, NULL);
|
&b2, &tmp1, &tmp2, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
440
bn_mp_toom_sqr.c
440
bn_mp_toom_sqr.c
|
@ -1,220 +1,220 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
* The library is free for all purposes without any express
|
* The library is free for all purposes without any express
|
||||||
* guarantee it works.
|
* guarantee it works.
|
||||||
*
|
*
|
||||||
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
* Tom St Denis, tomstdenis@iahu.ca, http://math.libtomcrypt.org
|
||||||
*/
|
*/
|
||||||
#include <tommath.h>
|
#include <tommath.h>
|
||||||
|
|
||||||
/* squaring using Toom-Cook 3-way algorithm */
|
/* squaring using Toom-Cook 3-way algorithm */
|
||||||
int
|
int
|
||||||
mp_toom_sqr(mp_int *a, mp_int *b)
|
mp_toom_sqr(mp_int *a, mp_int *b)
|
||||||
{
|
{
|
||||||
mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
|
mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
|
||||||
int res, B;
|
int res, B;
|
||||||
|
|
||||||
/* init temps */
|
/* init temps */
|
||||||
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != MP_OKAY) {
|
if ((res = mp_init_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* B */
|
/* B */
|
||||||
B = a->used / 3;
|
B = a->used / 3;
|
||||||
|
|
||||||
/* a = a2 * B^2 + a1 * B + a0 */
|
/* a = a2 * B^2 + a1 * B + a0 */
|
||||||
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
|
if ((res = mp_mod_2d(a, DIGIT_BIT * B, &a0)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_copy(a, &a1)) != MP_OKAY) {
|
if ((res = mp_copy(a, &a1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
mp_rshd(&a1, B);
|
mp_rshd(&a1, B);
|
||||||
mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
|
mp_mod_2d(&a1, DIGIT_BIT * B, &a1);
|
||||||
|
|
||||||
if ((res = mp_copy(a, &a2)) != MP_OKAY) {
|
if ((res = mp_copy(a, &a2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
mp_rshd(&a2, B*2);
|
mp_rshd(&a2, B*2);
|
||||||
|
|
||||||
/* w0 = a0*a0 */
|
/* w0 = a0*a0 */
|
||||||
if ((res = mp_sqr(&a0, &w0)) != MP_OKAY) {
|
if ((res = mp_sqr(&a0, &w0)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* w4 = a2 * a2 */
|
/* w4 = a2 * a2 */
|
||||||
if ((res = mp_sqr(&a2, &w4)) != MP_OKAY) {
|
if ((res = mp_sqr(&a2, &w4)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* w1 = (a2 + 2(a1 + 2a0))**2 */
|
/* w1 = (a2 + 2(a1 + 2a0))**2 */
|
||||||
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&a0, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a2, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sqr(&tmp1, &w1)) != MP_OKAY) {
|
if ((res = mp_sqr(&tmp1, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* w3 = (a0 + 2(a1 + 2a2))**2 */
|
/* w3 = (a0 + 2(a1 + 2a2))**2 */
|
||||||
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&a2, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2(&tmp1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_sqr(&tmp1, &w3)) != MP_OKAY) {
|
if ((res = mp_sqr(&tmp1, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* w2 = (a2 + a1 + a0)**2 */
|
/* w2 = (a2 + a1 + a0)**2 */
|
||||||
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&a2, &a1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, &a0, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sqr(&tmp1, &w2)) != MP_OKAY) {
|
if ((res = mp_sqr(&tmp1, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now solve the matrix
|
/* now solve the matrix
|
||||||
|
|
||||||
0 0 0 0 1
|
0 0 0 0 1
|
||||||
1 2 4 8 16
|
1 2 4 8 16
|
||||||
1 1 1 1 1
|
1 1 1 1 1
|
||||||
16 8 4 2 1
|
16 8 4 2 1
|
||||||
1 0 0 0 0
|
1 0 0 0 0
|
||||||
|
|
||||||
using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication.
|
using 12 subtractions, 4 shifts, 2 small divisions and 1 small multiplication.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
/* r1 - r4 */
|
/* r1 - r4 */
|
||||||
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &w4, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - r0 */
|
/* r3 - r0 */
|
||||||
if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &w0, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1/2 */
|
/* r1/2 */
|
||||||
if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
|
if ((res = mp_div_2(&w1, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3/2 */
|
/* r3/2 */
|
||||||
if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
|
if ((res = mp_div_2(&w3, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r2 - r0 - r4 */
|
/* r2 - r0 - r4 */
|
||||||
if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w0, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w4, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1 - r2 */
|
/* r1 - r2 */
|
||||||
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - r2 */
|
/* r3 - r2 */
|
||||||
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1 - 8r0 */
|
/* r1 - 8r0 */
|
||||||
if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2d(&w0, 3, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &tmp1, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - 8r4 */
|
/* r3 - 8r4 */
|
||||||
if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
|
if ((res = mp_mul_2d(&w4, 3, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &tmp1, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* 3r2 - r1 - r3 */
|
/* 3r2 - r1 - r3 */
|
||||||
if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
|
if ((res = mp_mul_d(&w2, 3, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w1, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
|
if ((res = mp_sub(&w2, &w3, &w2)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1 - r2 */
|
/* r1 - r2 */
|
||||||
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
if ((res = mp_sub(&w1, &w2, &w1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3 - r2 */
|
/* r3 - r2 */
|
||||||
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
if ((res = mp_sub(&w3, &w2, &w3)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r1/3 */
|
/* r1/3 */
|
||||||
if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
|
if ((res = mp_div_3(&w1, &w1, NULL)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
/* r3/3 */
|
/* r3/3 */
|
||||||
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
|
if ((res = mp_div_3(&w3, &w3, NULL)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* at this point shift W[n] by B*n */
|
/* at this point shift W[n] by B*n */
|
||||||
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w1, 1*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w2, 2*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w3, 3*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
|
if ((res = mp_lshd(&w4, 4*B)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
|
if ((res = mp_add(&w0, &w1, b)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&w2, &w3, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
|
if ((res = mp_add(&w4, &tmp1, &tmp1)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
|
if ((res = mp_add(&tmp1, b, b)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
ERR:
|
ERR:
|
||||||
mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
|
mp_clear_multi(&w0, &w1, &w2, &w3, &w4, &a0, &a1, &a2, &tmp1, NULL);
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
|
@ -1,9 +1,9 @@
|
||||||
/* LibTomMath, multiple-precision integer library -- Tom St Denis
|
/* 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.
|
* 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
|
* Michael Fromberger but has been written from scratch with
|
||||||
* additional optimizations in place.
|
* additional optimizations in place.
|
||||||
*
|
*
|
||||||
|
|
Some files were not shown because too many files have changed in this diff Show More
Loading…
Reference in New Issue