trim trailing spaces
This commit is contained in:
parent
a0a86c696a
commit
15681f9a12
@ -15,10 +15,10 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||
* that is c = 1/a mod b
|
||||
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||
* that is c = 1/a mod b
|
||||
*
|
||||
* Based on slow invmod except this is optimized for the case where b is
|
||||
* Based on slow invmod except this is optimized for the case where b is
|
||||
* odd as per HAC Note 14.64 on pp. 610
|
||||
*/
|
||||
int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
@ -17,15 +17,15 @@
|
||||
|
||||
/* Fast (comba) multiplier
|
||||
*
|
||||
* This is the fast column-array [comba] multiplier. It is
|
||||
* designed to compute the columns of the product first
|
||||
* then handle the carries afterwards. This has the effect
|
||||
* This is the fast column-array [comba] multiplier. It is
|
||||
* designed to compute the columns of the product first
|
||||
* then handle the carries afterwards. This has the effect
|
||||
* of making the nested loops that compute the columns very
|
||||
* simple and schedulable on super-scalar processors.
|
||||
*
|
||||
* This has been modified to produce a variable number of
|
||||
* digits of output so if say only a half-product is required
|
||||
* you don't have to compute the upper half (a feature
|
||||
* This has been modified to produce a variable number of
|
||||
* digits of output so if say only a half-product is required
|
||||
* you don't have to compute the upper half (a feature
|
||||
* required for fast Barrett reduction).
|
||||
*
|
||||
* Based on Algorithm 14.12 on pp.595 of HAC.
|
||||
@ -49,7 +49,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
/* clear the carry */
|
||||
_W = 0;
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
int tx, ty;
|
||||
int iy;
|
||||
mp_digit *tmpx, *tmpy;
|
||||
@ -62,7 +62,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = b->dp + ty;
|
||||
|
||||
/* this is the number of times the loop will iterrate, essentially
|
||||
/* this is the number of times the loop will iterrate, essentially
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
@ -41,7 +41,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* number of output digits to produce */
|
||||
pa = a->used + b->used;
|
||||
_W = 0;
|
||||
for (ix = digs; ix < pa; ix++) {
|
||||
for (ix = digs; ix < pa; ix++) {
|
||||
int tx, ty, iy;
|
||||
mp_digit *tmpx, *tmpy;
|
||||
|
||||
@ -53,7 +53,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
tmpx = a->dp + tx;
|
||||
tmpy = b->dp + ty;
|
||||
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
/* this is the number of times the loop will iterrate, essentially its
|
||||
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||
*/
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
@ -69,7 +69,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* make next carry */
|
||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
||||
}
|
||||
|
||||
|
||||
/* setup dest */
|
||||
olduse = c->used;
|
||||
c->used = pa;
|
||||
|
@ -16,10 +16,10 @@
|
||||
*/
|
||||
|
||||
/* the jist of squaring...
|
||||
* you do like mult except the offset of the tmpx [one that
|
||||
* starts closer to zero] can't equal the offset of tmpy.
|
||||
* you do like mult except the offset of the tmpx [one that
|
||||
* starts closer to zero] can't equal the offset of tmpy.
|
||||
* So basically you set up iy like before then you min it with
|
||||
* (ty-tx) so that it never happens. You double all those
|
||||
* (ty-tx) so that it never happens. You double all those
|
||||
* you add in the inner loop
|
||||
|
||||
After that loop you do the squares and add them in.
|
||||
@ -41,7 +41,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
/* number of output digits to produce */
|
||||
W1 = 0;
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
for (ix = 0; ix < pa; ix++) {
|
||||
int tx, ty, iy;
|
||||
mp_word _W;
|
||||
mp_digit *tmpy;
|
||||
@ -62,7 +62,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
*/
|
||||
iy = MIN(a->used-tx, ty+1);
|
||||
|
||||
/* now for squaring tx can never equal ty
|
||||
/* now for squaring tx can never equal ty
|
||||
* we halve the distance since they approach at a rate of 2x
|
||||
* and we have to round because odd cases need to be executed
|
||||
*/
|
||||
|
@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* computes a = 2**b
|
||||
/* computes a = 2**b
|
||||
*
|
||||
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
||||
* as required.
|
||||
|
@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* b = |a|
|
||||
/* b = |a|
|
||||
*
|
||||
* Simple function copies the input and fixes the sign to positive
|
||||
*/
|
||||
|
@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* trim unused digits
|
||||
/* trim unused digits
|
||||
*
|
||||
* This is used to ensure that leading zero digits are
|
||||
* trimed and the leading "used" digit will be non-zero
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
|
||||
void mp_clear_multi(mp_int *mp, ...)
|
||||
void mp_clear_multi(mp_int *mp, ...)
|
||||
{
|
||||
mp_int* next_mp = mp;
|
||||
va_list args;
|
||||
|
@ -27,7 +27,7 @@ mp_cmp (mp_int * a, mp_int * b)
|
||||
return MP_GT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* compare digits */
|
||||
if (a->sign == MP_NEG) {
|
||||
/* if negative compare opposite direction */
|
||||
|
@ -25,7 +25,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b)
|
||||
if (a->used > b->used) {
|
||||
return MP_GT;
|
||||
}
|
||||
|
||||
|
||||
if (a->used < b->used) {
|
||||
return MP_LT;
|
||||
}
|
||||
|
@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
static const int lnz[16] = {
|
||||
static const int lnz[16] = {
|
||||
4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0
|
||||
};
|
||||
|
||||
|
@ -29,7 +29,7 @@ mp_count_bits (mp_int * a)
|
||||
|
||||
/* get number of digits and add that */
|
||||
r = (a->used - 1) * DIGIT_BIT;
|
||||
|
||||
|
||||
/* take the last digit and count the bits in it */
|
||||
q = a->dp[a->used - 1];
|
||||
while (q > ((mp_digit) 0)) {
|
||||
|
@ -23,14 +23,14 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||
mp_word w, t;
|
||||
mp_digit b;
|
||||
int res, ix;
|
||||
|
||||
|
||||
/* b = 2**DIGIT_BIT / 3 */
|
||||
b = (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3);
|
||||
|
||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
q.used = a->used;
|
||||
q.sign = a->sign;
|
||||
w = 0;
|
||||
@ -68,7 +68,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||
mp_exch(&q, c);
|
||||
}
|
||||
mp_clear(&q);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -79,13 +79,13 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
q.used = a->used;
|
||||
q.sign = a->sign;
|
||||
w = 0;
|
||||
for (ix = a->used - 1; ix >= 0; ix--) {
|
||||
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
|
||||
|
||||
|
||||
if (w >= b) {
|
||||
t = (mp_digit)(w / b);
|
||||
w -= ((mp_word)t) * ((mp_word)b);
|
||||
@ -94,17 +94,17 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
|
||||
}
|
||||
q.dp[ix] = (mp_digit)t;
|
||||
}
|
||||
|
||||
|
||||
if (d != NULL) {
|
||||
*d = (mp_digit)w;
|
||||
}
|
||||
|
||||
|
||||
if (c != NULL) {
|
||||
mp_clamp(&q);
|
||||
mp_exch(&q, c);
|
||||
}
|
||||
mp_clear(&q);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -21,7 +21,7 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
|
||||
/* the casts are required if DIGIT_BIT is one less than
|
||||
* 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]));
|
||||
}
|
||||
|
||||
|
@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* swap the elements of two integers, for cases where you can't simply swap the
|
||||
/* swap the elements of two integers, for cases where you can't simply swap the
|
||||
* mp_int pointers around
|
||||
*/
|
||||
void
|
||||
|
@ -18,7 +18,7 @@
|
||||
/* based on gmp's mpz_export.
|
||||
* see http://gmplib.org/manual/Integer-Import-and-Export.html
|
||||
*/
|
||||
int mp_export(void* rop, size_t* countp, int order, size_t size,
|
||||
int mp_export(void* rop, size_t* countp, int order, size_t size,
|
||||
int endian, size_t nails, mp_int* op) {
|
||||
int result;
|
||||
size_t odd_nails, nail_bytes, i, j, bits, count;
|
||||
@ -31,12 +31,12 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
|
||||
}
|
||||
|
||||
if (endian == 0) {
|
||||
union {
|
||||
unsigned int i;
|
||||
char c[4];
|
||||
union {
|
||||
unsigned int i;
|
||||
char c[4];
|
||||
} lint;
|
||||
lint.i = 0x01020304;
|
||||
|
||||
lint.i = 0x01020304;
|
||||
|
||||
endian = (lint.c[0] == 4) ? -1 : 1;
|
||||
}
|
||||
|
||||
@ -53,7 +53,7 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
|
||||
for (i = 0; i < count; ++i) {
|
||||
for (j = 0; j < size; ++j) {
|
||||
unsigned char* byte = (
|
||||
(unsigned char*)rop +
|
||||
(unsigned char*)rop +
|
||||
(((order == -1) ? i : ((count - 1) - i)) * size) +
|
||||
((endian == -1) ? j : ((size - 1) - j))
|
||||
);
|
||||
|
@ -59,7 +59,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
err = mp_exptmod(&tmpG, &tmpX, P, Y);
|
||||
mp_clear_multi(&tmpG, &tmpX, NULL);
|
||||
return err;
|
||||
#else
|
||||
#else
|
||||
/* no invmod */
|
||||
return MP_VAL;
|
||||
#endif
|
||||
@ -86,7 +86,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
dr = mp_reduce_is_2k(P) << 1;
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
/* if the modulus is odd or dr != 0 use the montgomery method */
|
||||
#ifdef BN_MP_EXPTMOD_FAST_C
|
||||
if ((mp_isodd (P) == MP_YES) || (dr != 0)) {
|
||||
|
@ -84,7 +84,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
|
||||
/* determine and setup reduction code */
|
||||
if (redmode == 0) {
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
/* now setup montgomery */
|
||||
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
@ -99,7 +99,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
if ((((P->used * 2) + 1) < MP_WARRAY) &&
|
||||
(P->used < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
||||
redux = fast_mp_montgomery_reduce;
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
||||
|
@ -20,10 +20,10 @@
|
||||
int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
{
|
||||
int err, ch, neg, y;
|
||||
|
||||
|
||||
/* clear a */
|
||||
mp_zero(a);
|
||||
|
||||
|
||||
/* if first digit is - then set negative */
|
||||
ch = fgetc(stream);
|
||||
if (ch == '-') {
|
||||
@ -32,7 +32,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
} else {
|
||||
neg = MP_ZPOS;
|
||||
}
|
||||
|
||||
|
||||
for (;;) {
|
||||
/* find y in the radix map */
|
||||
for (y = 0; y < radix; y++) {
|
||||
@ -43,7 +43,7 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
if (y == radix) {
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
/* shift up and add */
|
||||
if ((err = mp_mul_d(a, radix, a)) != MP_OKAY) {
|
||||
return err;
|
||||
@ -51,13 +51,13 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
|
||||
if ((err = mp_add_d(a, y, a)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
ch = fgetc(stream);
|
||||
}
|
||||
if (mp_cmp_d(a, 0) != MP_EQ) {
|
||||
a->sign = neg;
|
||||
}
|
||||
|
||||
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -20,7 +20,7 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
||||
{
|
||||
char *buf;
|
||||
int err, len, x;
|
||||
|
||||
|
||||
if ((err = mp_radix_size(a, radix, &len)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
@ -29,19 +29,19 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
||||
if (buf == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
||||
|
||||
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
|
||||
XFREE (buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
|
||||
for (x = 0; x < len; x++) {
|
||||
if (fputc(buf[x], stream) == EOF) {
|
||||
XFREE (buf);
|
||||
return MP_VAL;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
XFREE (buf);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
@ -76,17 +76,17 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||
/* swap u and v to make sure v is >= u */
|
||||
mp_exch(&u, &v);
|
||||
}
|
||||
|
||||
|
||||
/* subtract smallest from largest */
|
||||
if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
|
||||
goto LBL_V;
|
||||
}
|
||||
|
||||
|
||||
/* Divide out all factors of two */
|
||||
if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
|
||||
goto LBL_V;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/* multiply by 2**k which we divided out at the beginning */
|
||||
if ((res = mp_mul_2d (&u, k, c)) != MP_OKAY) {
|
||||
|
@ -18,7 +18,7 @@
|
||||
/* based on gmp's mpz_import.
|
||||
* see http://gmplib.org/manual/Integer-Import-and-Export.html
|
||||
*/
|
||||
int mp_import(mp_int* rop, size_t count, int order, size_t size,
|
||||
int mp_import(mp_int* rop, size_t count, int order, size_t size,
|
||||
int endian, size_t nails, const void* op) {
|
||||
int result;
|
||||
size_t odd_nails, nail_bytes, i, j;
|
||||
@ -27,12 +27,12 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
|
||||
mp_zero(rop);
|
||||
|
||||
if (endian == 0) {
|
||||
union {
|
||||
unsigned int i;
|
||||
char c[4];
|
||||
union {
|
||||
unsigned int i;
|
||||
char c[4];
|
||||
} lint;
|
||||
lint.i = 0x01020304;
|
||||
|
||||
lint.i = 0x01020304;
|
||||
|
||||
endian = (lint.c[0] == 4) ? -1 : 1;
|
||||
}
|
||||
|
||||
@ -46,7 +46,7 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
|
||||
for (i = 0; i < count; ++i) {
|
||||
for (j = 0; j < (size - nail_bytes); ++j) {
|
||||
unsigned char byte = *(
|
||||
(unsigned char*)op +
|
||||
(unsigned char*)op +
|
||||
(((order == 1) ? i : ((count - 1) - i)) * size) +
|
||||
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))
|
||||
);
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
#include <stdarg.h>
|
||||
|
||||
int mp_init_multi(mp_int *mp, ...)
|
||||
int mp_init_multi(mp_int *mp, ...)
|
||||
{
|
||||
mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
|
||||
int n = 0; /* Number of ok inits */
|
||||
@ -30,8 +30,8 @@ int mp_init_multi(mp_int *mp, ...)
|
||||
succeeded in init-ing, then return error.
|
||||
*/
|
||||
va_list clean_args;
|
||||
|
||||
/* now start cleaning up */
|
||||
|
||||
/* now start cleaning up */
|
||||
cur_arg = mp;
|
||||
va_start(clean_args, mp);
|
||||
while (n-- != 0) {
|
||||
|
@ -21,8 +21,8 @@ int mp_init_size (mp_int * a, int size)
|
||||
int x;
|
||||
|
||||
/* pad size so there are always extra digits */
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
/* alloc mem */
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
|
||||
if (a->dp == NULL) {
|
||||
|
@ -27,7 +27,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
|
||||
/* init temps */
|
||||
if ((res = mp_init_multi(&x, &y, &u, &v,
|
||||
if ((res = mp_init_multi(&x, &y, &u, &v,
|
||||
&A, &B, &C, &D, NULL)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
@ -154,14 +154,14 @@ top:
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* too big */
|
||||
while (mp_cmp_mag(&C, b) != MP_LT) {
|
||||
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* C is now the inverse */
|
||||
mp_exch (&C, c);
|
||||
res = MP_OKAY;
|
||||
|
@ -38,7 +38,7 @@ static const char rem_105[105] = {
|
||||
};
|
||||
|
||||
/* Store non-zero to ret if arg is square, and zero if not */
|
||||
int mp_is_square(mp_int *arg,int *ret)
|
||||
int mp_is_square(mp_int *arg,int *ret)
|
||||
{
|
||||
int res;
|
||||
mp_digit c;
|
||||
@ -46,7 +46,7 @@ int mp_is_square(mp_int *arg,int *ret)
|
||||
unsigned long r;
|
||||
|
||||
/* Default to Non-square :) */
|
||||
*ret = MP_NO;
|
||||
*ret = MP_NO;
|
||||
|
||||
if (arg->sign == MP_NEG) {
|
||||
return MP_VAL;
|
||||
@ -80,8 +80,8 @@ int mp_is_square(mp_int *arg,int *ret)
|
||||
r = mp_get_int(&t);
|
||||
/* Check for other prime modules, note it's not an ERROR but we must
|
||||
* free "t" so the easiest way is to goto ERR. We know that res
|
||||
* is already equal to MP_OKAY from the mp_mod call
|
||||
*/
|
||||
* is already equal to MP_OKAY from the mp_mod call
|
||||
*/
|
||||
if (((1L<<(r%11)) & 0x5C4L) != 0L) goto ERR;
|
||||
if (((1L<<(r%13)) & 0x9E4L) != 0L) goto ERR;
|
||||
if (((1L<<(r%17)) & 0x5CE8L) != 0L) goto ERR;
|
||||
|
@ -15,33 +15,33 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* c = |a| * |b| using Karatsuba Multiplication using
|
||||
/* c = |a| * |b| using Karatsuba Multiplication using
|
||||
* three half size multiplications
|
||||
*
|
||||
* Let B represent the radix [e.g. 2**DIGIT_BIT] and
|
||||
* let n represent half of the number of digits in
|
||||
* Let B represent the radix [e.g. 2**DIGIT_BIT] and
|
||||
* let n represent half of the number of digits in
|
||||
* the min(a,b)
|
||||
*
|
||||
* a = a1 * B**n + a0
|
||||
* b = b1 * B**n + b0
|
||||
*
|
||||
* Then, a * b =>
|
||||
* Then, a * b =>
|
||||
a1b1 * B**2n + ((a1 + a0)(b1 + b0) - (a0b0 + a1b1)) * B + a0b0
|
||||
*
|
||||
* Note that a1b1 and a0b0 are used twice and only need to be
|
||||
* computed once. So in total three half size (half # of
|
||||
* digit) multiplications are performed, a0b0, a1b1 and
|
||||
* Note that a1b1 and a0b0 are used twice and only need to be
|
||||
* computed once. So in total three half size (half # of
|
||||
* digit) multiplications are performed, a0b0, a1b1 and
|
||||
* (a1+b1)(a0+b0)
|
||||
*
|
||||
* Note that a multiplication of half the digits requires
|
||||
* 1/4th the number of single precision multiplications so in
|
||||
* total after one call 25% of the single precision multiplications
|
||||
* are saved. Note also that the call to mp_mul can end up back
|
||||
* in this function if the a0, a1, b0, or b1 are above the threshold.
|
||||
* This is known as divide-and-conquer and leads to the famous
|
||||
* O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
|
||||
* the standard O(N**2) that the baseline/comba methods use.
|
||||
* Generally though the overhead of this method doesn't pay off
|
||||
* 1/4th the number of single precision multiplications so in
|
||||
* total after one call 25% of the single precision multiplications
|
||||
* are saved. Note also that the call to mp_mul can end up back
|
||||
* in this function if the a0, a1, b0, or b1 are above the threshold.
|
||||
* This is known as divide-and-conquer and leads to the famous
|
||||
* O(N**lg(3)) or O(N**1.584) work which is asymptopically lower than
|
||||
* the standard O(N**2) that the baseline/comba methods use.
|
||||
* Generally though the overhead of this method doesn't pay off
|
||||
* until a certain size (N ~ 80) is reached.
|
||||
*/
|
||||
int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
@ -109,7 +109,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
}
|
||||
|
||||
/* only need to clamp the lower words since by definition the
|
||||
/* only need to clamp the lower words since by definition the
|
||||
* upper words x1/y1 must have a known number of digits
|
||||
*/
|
||||
mp_clamp (&x0);
|
||||
@ -117,7 +117,7 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
||||
/* now calc the products x0y0 and x1y1 */
|
||||
/* after this x0 is no longer required, free temp [x0==t2]! */
|
||||
if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
|
||||
if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
|
||||
goto X1Y1; /* x0y0 = x0*y0 */
|
||||
if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
|
||||
goto X1Y1; /* x1y1 = x1*y1 */
|
||||
|
@ -15,11 +15,11 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* Karatsuba squaring, computes b = a*a using three
|
||||
/* Karatsuba squaring, computes b = a*a using three
|
||||
* half size squarings
|
||||
*
|
||||
* See comments of karatsuba_mul for details. It
|
||||
* is essentially the same algorithm but merely
|
||||
* See comments of karatsuba_mul for details. It
|
||||
* is essentially the same algorithm but merely
|
||||
* tuned to perform recursive squarings.
|
||||
*/
|
||||
int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
||||
|
12
bn_mp_mul.c
12
bn_mp_mul.c
@ -25,29 +25,29 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
#ifdef BN_MP_TOOM_MUL_C
|
||||
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||
res = mp_toom_mul(a, b, c);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||
/* use Karatsuba? */
|
||||
if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
||||
res = mp_karatsuba_mul (a, b, c);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
/* can we use the fast multiplier?
|
||||
*
|
||||
* The fast multiplier can be used if the output will
|
||||
* have less than MP_WARRAY digits and the number of
|
||||
* The fast multiplier can be used if the output will
|
||||
* have less than MP_WARRAY digits and the number of
|
||||
* digits won't affect carry propagation
|
||||
*/
|
||||
int digs = a->used + b->used + 1;
|
||||
|
||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||
if ((digs < MP_WARRAY) &&
|
||||
(MIN(a->used, b->used) <=
|
||||
(MIN(a->used, b->used) <=
|
||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
||||
res = fast_s_mp_mul_digs (a, b, c, digs);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_S_MP_MUL_DIGS_C
|
||||
|
@ -35,24 +35,24 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
||||
|
||||
/* alias for source */
|
||||
tmpa = a->dp;
|
||||
|
||||
|
||||
/* alias for dest */
|
||||
tmpb = b->dp;
|
||||
|
||||
/* carry */
|
||||
r = 0;
|
||||
for (x = 0; x < a->used; x++) {
|
||||
|
||||
/* get what will be the *next* carry bit from the
|
||||
* MSB of the current digit
|
||||
|
||||
/* get what will be the *next* carry bit from the
|
||||
* MSB of the current digit
|
||||
*/
|
||||
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
||||
|
||||
|
||||
/* now shift up this digit, add in the carry [from the previous] */
|
||||
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
||||
|
||||
/* copy the carry that would be from the source
|
||||
* digit into the next iteration
|
||||
|
||||
/* copy the carry that would be from the source
|
||||
* digit into the next iteration
|
||||
*/
|
||||
r = rr;
|
||||
}
|
||||
@ -64,8 +64,8 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
||||
++(b->used);
|
||||
}
|
||||
|
||||
/* now zero any excess digits on the destination
|
||||
* that we didn't write to
|
||||
/* now zero any excess digits on the destination
|
||||
* that we didn't write to
|
||||
*/
|
||||
tmpb = b->dp + b->used;
|
||||
for (x = b->used; x < oldused; x++) {
|
||||
|
@ -69,7 +69,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
||||
/* set the carry to the carry bits of the current word */
|
||||
r = rr;
|
||||
}
|
||||
|
||||
|
||||
/* set final carry */
|
||||
if (r != 0) {
|
||||
c->dp[(c->used)++] = r;
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* performs one Fermat test.
|
||||
*
|
||||
*
|
||||
* If "a" were prime then b**a == b (mod a) since the order of
|
||||
* the multiplicative sub-group would be phi(a) = a-1. That means
|
||||
* it would be the same as b**(a mod (a-1)) == b**1 == b (mod a).
|
||||
|
@ -15,7 +15,7 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* determines if an integers is divisible by one
|
||||
/* determines if an integers is divisible by one
|
||||
* of the first PRIME_SIZE primes or not
|
||||
*
|
||||
* sets result to 0 if not, 1 if yes
|
||||
|
@ -15,11 +15,11 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* Miller-Rabin test of "a" to the base of "b" as described in
|
||||
/* Miller-Rabin test of "a" to the base of "b" as described in
|
||||
* HAC pp. 139 Algorithm 4.24
|
||||
*
|
||||
* Sets result to 0 if definitely composite or 1 if probably prime.
|
||||
* Randomly the chance of error is no more than 1/4 and often
|
||||
* Randomly the chance of error is no more than 1/4 and often
|
||||
* very much lower.
|
||||
*/
|
||||
int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
@ -33,7 +33,7 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
/* ensure b > 1 */
|
||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
||||
return MP_VAL;
|
||||
}
|
||||
}
|
||||
|
||||
/* get n1 = a - 1 */
|
||||
if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
|
||||
|
@ -18,7 +18,7 @@
|
||||
/* makes a truly random prime of a given size (bits),
|
||||
*
|
||||
* Flags are as follows:
|
||||
*
|
||||
*
|
||||
* LTM_PRIME_BBS - make prime congruent to 3 mod 4
|
||||
* LTM_PRIME_SAFE - make sure (p-1)/2 is prime as well (implies LTM_PRIME_BBS)
|
||||
* LTM_PRIME_2MSB_ON - make the 2nd highest bit one
|
||||
@ -62,7 +62,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
maskOR_msb_offset = ((size & 7) == 1) ? 1 : 0;
|
||||
if ((flags & LTM_PRIME_2MSB_ON) != 0) {
|
||||
maskOR_msb |= 0x80 >> ((9 - size) & 7);
|
||||
}
|
||||
}
|
||||
|
||||
/* get the maskOR_lsb */
|
||||
maskOR_lsb = 1;
|
||||
@ -76,7 +76,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
err = MP_VAL;
|
||||
goto error;
|
||||
}
|
||||
|
||||
|
||||
/* work over the MSbyte */
|
||||
tmp[0] &= maskAND;
|
||||
tmp[0] |= 1 << ((size - 1) & 7);
|
||||
@ -90,7 +90,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
|
||||
/* is it prime? */
|
||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
||||
if (res == MP_NO) {
|
||||
if (res == MP_NO) {
|
||||
continue;
|
||||
}
|
||||
|
||||
@ -98,7 +98,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
|
||||
/* see if (a-1)/2 is prime */
|
||||
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; }
|
||||
if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; }
|
||||
|
||||
|
||||
/* is it prime? */
|
||||
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
|
||||
}
|
||||
|
@ -54,7 +54,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
|
||||
}
|
||||
|
||||
/* force temp to positive */
|
||||
t.sign = MP_ZPOS;
|
||||
t.sign = MP_ZPOS;
|
||||
|
||||
/* fetch out all of the digits */
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
|
@ -29,8 +29,8 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
/* if the leading digit is a
|
||||
* minus set the sign to negative.
|
||||
/* if the leading digit is a
|
||||
* minus set the sign to negative.
|
||||
*/
|
||||
if (*str == '-') {
|
||||
++str;
|
||||
@ -41,7 +41,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
|
||||
/* set the integer to the default of zero */
|
||||
mp_zero (a);
|
||||
|
||||
|
||||
/* process each digit of the string */
|
||||
while (*str != '\0') {
|
||||
/* if the radix <= 36 the conversion is case insensitive
|
||||
@ -55,9 +55,9 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
}
|
||||
}
|
||||
|
||||
/* 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
|
||||
* to the number, otherwise exit the loop.
|
||||
* to the number, otherwise exit the loop.
|
||||
*/
|
||||
if (y < radix) {
|
||||
if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
|
||||
|
@ -20,22 +20,22 @@ int mp_reduce_2k_setup(mp_int *a, mp_digit *d)
|
||||
{
|
||||
int res, p;
|
||||
mp_int tmp;
|
||||
|
||||
|
||||
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
p = mp_count_bits(a);
|
||||
if ((res = mp_2expt(&tmp, p)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if ((res = s_mp_sub(&tmp, a, &tmp)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
*d = tmp.dp[0];
|
||||
mp_clear(&tmp);
|
||||
return MP_OKAY;
|
||||
|
@ -20,19 +20,19 @@ int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
|
||||
{
|
||||
int res;
|
||||
mp_int tmp;
|
||||
|
||||
|
||||
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
ERR:
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
|
@ -20,7 +20,7 @@ int mp_reduce_is_2k(mp_int *a)
|
||||
{
|
||||
int ix, iy, iw;
|
||||
mp_digit iz;
|
||||
|
||||
|
||||
if (a->used == 0) {
|
||||
return MP_NO;
|
||||
} else if (a->used == 1) {
|
||||
@ -29,7 +29,7 @@ int mp_reduce_is_2k(mp_int *a)
|
||||
iy = mp_count_bits(a);
|
||||
iz = 1;
|
||||
iw = 1;
|
||||
|
||||
|
||||
/* Test every bit from the second digit up, must be 1 */
|
||||
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
||||
if ((a->dp[iw] & iz) == 0) {
|
||||
|
@ -19,7 +19,7 @@
|
||||
int mp_reduce_is_2k_l(mp_int *a)
|
||||
{
|
||||
int ix, iy;
|
||||
|
||||
|
||||
if (a->used == 0) {
|
||||
return MP_NO;
|
||||
} else if (a->used == 1) {
|
||||
@ -32,7 +32,7 @@ int mp_reduce_is_2k_l(mp_int *a)
|
||||
}
|
||||
}
|
||||
return (iy >= (a->used/2)) ? MP_YES : MP_NO;
|
||||
|
||||
|
||||
}
|
||||
return MP_NO;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@
|
||||
int mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
|
||||
if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
@ -42,8 +42,8 @@ void mp_rshd (mp_int * a, int b)
|
||||
/* top [offset into digits] */
|
||||
top = a->dp + b;
|
||||
|
||||
/* this is implemented as a sliding window where
|
||||
* the window is b-digits long and digits from
|
||||
/* this is implemented as a sliding window where
|
||||
* the window is b-digits long and digits from
|
||||
* the top of the window are copied to the bottom
|
||||
*
|
||||
* e.g.
|
||||
@ -61,7 +61,7 @@ void mp_rshd (mp_int * a, int b)
|
||||
*bottom++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* remove excess digits */
|
||||
a->used -= b;
|
||||
}
|
||||
|
@ -21,7 +21,7 @@ int mp_set_int (mp_int * a, unsigned long b)
|
||||
int x, res;
|
||||
|
||||
mp_zero (a);
|
||||
|
||||
|
||||
/* set four bits at a time */
|
||||
for (x = 0; x < 8; x++) {
|
||||
/* shift the number up four bits */
|
||||
|
@ -20,11 +20,11 @@ int mp_shrink (mp_int * a)
|
||||
{
|
||||
mp_digit *tmp;
|
||||
int used = 1;
|
||||
|
||||
|
||||
if(a->used > 0) {
|
||||
used = a->used;
|
||||
}
|
||||
|
||||
|
||||
if (a->alloc != used) {
|
||||
if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) {
|
||||
return MP_MEM;
|
||||
|
@ -26,12 +26,12 @@ mp_sqr (mp_int * a, mp_int * b)
|
||||
if (a->used >= TOOM_SQR_CUTOFF) {
|
||||
res = mp_toom_sqr(a, b);
|
||||
/* Karatsuba? */
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_SQR_C
|
||||
if (a->used >= KARATSUBA_SQR_CUTOFF) {
|
||||
res = mp_karatsuba_sqr (a, b);
|
||||
} else
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_FAST_S_MP_SQR_C
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* this function is less generic than mp_n_root, simpler and faster */
|
||||
int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
{
|
||||
int res;
|
||||
mp_int t1,t2;
|
||||
@ -43,7 +43,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
/* First approx. (not very bad for large arg) */
|
||||
mp_rshd (&t1,t1.used/2);
|
||||
|
||||
/* t1 > 0 */
|
||||
/* t1 > 0 */
|
||||
if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
|
||||
goto E1;
|
||||
}
|
||||
@ -54,7 +54,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
goto E1;
|
||||
}
|
||||
/* And now t1 > sqrt(arg) */
|
||||
do {
|
||||
do {
|
||||
if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
|
||||
goto E1;
|
||||
}
|
||||
|
@ -15,9 +15,9 @@
|
||||
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
|
||||
*/
|
||||
|
||||
/* stores a bignum as a ASCII string in a given radix (2..64)
|
||||
/* stores a bignum as a ASCII string in a given radix (2..64)
|
||||
*
|
||||
* Stores upto maxlen-1 chars and always a NULL byte
|
||||
* Stores upto maxlen-1 chars and always a NULL byte
|
||||
*/
|
||||
int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
{
|
||||
@ -50,7 +50,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
/* store the flag and mark the number as positive */
|
||||
*str++ = '-';
|
||||
t.sign = MP_ZPOS;
|
||||
|
||||
|
||||
/* subtract a char */
|
||||
--maxlen;
|
||||
}
|
||||
|
@ -74,8 +74,8 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
|
||||
*tmpc++ &= MP_MASK;
|
||||
}
|
||||
|
||||
/* now copy higher words if any, that is in A+B
|
||||
* if A or B has more digits add those in
|
||||
/* now copy higher words if any, that is in A+B
|
||||
* if A or B has more digits add those in
|
||||
*/
|
||||
if (min != max) {
|
||||
for (; i < max; i++) {
|
||||
|
@ -54,7 +54,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
/* init M array */
|
||||
/* init first cell */
|
||||
if ((err = mp_init(&M[1])) != MP_OKAY) {
|
||||
return err;
|
||||
return err;
|
||||
}
|
||||
|
||||
/* now init the second half of the array */
|
||||
@ -72,7 +72,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
if ((err = mp_init (&mu)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
}
|
||||
|
||||
|
||||
if (redmode == 0) {
|
||||
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
@ -83,22 +83,22 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
goto LBL_MU;
|
||||
}
|
||||
redux = mp_reduce_2k_l;
|
||||
}
|
||||
}
|
||||
|
||||
/* create M table
|
||||
*
|
||||
* The M table contains powers of the base,
|
||||
* The M table contains powers of the base,
|
||||
* e.g. M[x] = G**x mod P
|
||||
*
|
||||
* The first half of the table is not
|
||||
* The first half of the table is not
|
||||
* computed though accept for M[0] and M[1]
|
||||
*/
|
||||
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||
* M[1] (winsize-1) times
|
||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||
* M[1] (winsize-1) times
|
||||
*/
|
||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
@ -106,7 +106,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
|
||||
for (x = 0; x < (winsize - 1); x++) {
|
||||
/* square it */
|
||||
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
@ -16,7 +16,7 @@
|
||||
*/
|
||||
|
||||
/* multiplies |a| * |b| and only computes upto digs digits of result
|
||||
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
||||
* HAC pp. 595, Algorithm 14.12 Modified so you can control how
|
||||
* many digits of output are created.
|
||||
*/
|
||||
int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
@ -29,7 +29,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
/* can we use the fast multiplier? */
|
||||
if (((digs) < MP_WARRAY) &&
|
||||
(MIN (a->used, b->used) <
|
||||
(MIN (a->used, b->used) <
|
||||
(1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
|
||||
return fast_s_mp_mul_digs (a, b, c, digs);
|
||||
}
|
||||
@ -51,10 +51,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* setup some aliases */
|
||||
/* copy of the digit from a used within the nested loop */
|
||||
tmpx = a->dp[ix];
|
||||
|
||||
|
||||
/* an alias for the destination shifted ix places */
|
||||
tmpt = t.dp + ix;
|
||||
|
||||
|
||||
/* an alias for the digits of b */
|
||||
tmpy = b->dp;
|
||||
|
||||
|
@ -48,7 +48,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
/* alias for where to store the results */
|
||||
tmpt = t.dp + ((2 * ix) + 1);
|
||||
|
||||
|
||||
for (iy = ix + 1; iy < pa; iy++) {
|
||||
/* first calculate the product */
|
||||
r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
|
||||
|
6
bncore.c
6
bncore.c
@ -21,14 +21,14 @@
|
||||
-------------------------------------------------------------
|
||||
Intel P4 Northwood /GCC v3.4.1 / 88/ 128/LTM 0.32 ;-)
|
||||
AMD Athlon64 /GCC v3.4.4 / 80/ 120/LTM 0.35
|
||||
|
||||
|
||||
*/
|
||||
|
||||
int KARATSUBA_MUL_CUTOFF = 80, /* Min. number of digits before Karatsuba multiplication is used. */
|
||||
KARATSUBA_SQR_CUTOFF = 120, /* Min. number of digits before Karatsuba squaring is used. */
|
||||
|
||||
|
||||
TOOM_MUL_CUTOFF = 350, /* no optimal values of these are known yet so set em high */
|
||||
TOOM_SQR_CUTOFF = 400;
|
||||
TOOM_SQR_CUTOFF = 400;
|
||||
#endif
|
||||
|
||||
/* ref: $Format:%D$ */
|
||||
|
@ -60,9 +60,9 @@
|
||||
#undef BN_FAST_MP_INVMOD_C
|
||||
|
||||
/* To safely undefine these you have to make sure your RSA key won't exceed the Comba threshold
|
||||
* which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]
|
||||
* which is roughly 255 digits [7140 bits for 32-bit machines, 15300 bits for 64-bit machines]
|
||||
* which means roughly speaking you can handle upto 2536-bit RSA keys with these defined without
|
||||
* trouble.
|
||||
* trouble.
|
||||
*/
|
||||
#undef BN_S_MP_MUL_DIGS_C
|
||||
#undef BN_S_MP_SQR_C
|
||||
|
Loading…
x
Reference in New Issue
Block a user