forked from wolfSSL/wolfssl
remove trailing spaces.
This commit is contained in:
@ -33,7 +33,7 @@
|
||||
/* in case user set USE_FAST_MATH there */
|
||||
#include <wolfssl/wolfcrypt/settings.h>
|
||||
|
||||
#ifndef NO_BIG_INT
|
||||
#ifndef NO_BIG_INT
|
||||
|
||||
#ifndef USE_FAST_MATH
|
||||
|
||||
@ -168,7 +168,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)) {
|
||||
@ -416,7 +416,7 @@ void mp_zero (mp_int * a)
|
||||
}
|
||||
|
||||
|
||||
/* 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
|
||||
@ -440,7 +440,7 @@ mp_clamp (mp_int * a)
|
||||
}
|
||||
|
||||
|
||||
/* 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
|
||||
@ -513,8 +513,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.
|
||||
@ -532,7 +532,7 @@ void mp_rshd (mp_int * a, int b)
|
||||
*bottom++ = 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* remove excess digits */
|
||||
a->used -= b;
|
||||
}
|
||||
@ -662,7 +662,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;
|
||||
@ -765,7 +765,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
mp_clear(&tmpG);
|
||||
mp_clear(&tmpX);
|
||||
return err;
|
||||
#else
|
||||
#else
|
||||
/* no invmod */
|
||||
return MP_VAL;
|
||||
#endif
|
||||
@ -793,7 +793,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) == 1 || dr != 0) {
|
||||
@ -813,7 +813,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
|
||||
}
|
||||
|
||||
|
||||
/* b = |a|
|
||||
/* b = |a|
|
||||
*
|
||||
* Simple function copies the input and fixes the sign to positive
|
||||
*/
|
||||
@ -857,10 +857,10 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
|
||||
|
||||
/* 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)
|
||||
@ -1006,7 +1006,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)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
@ -1138,14 +1138,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;
|
||||
@ -1171,7 +1171,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;
|
||||
}
|
||||
@ -1208,7 +1208,7 @@ mp_cmp (mp_int * a, mp_int * b)
|
||||
return MP_GT;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* compare digits */
|
||||
if (a->sign == MP_NEG) {
|
||||
/* if negative compare opposite direction */
|
||||
@ -1303,7 +1303,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
|
||||
/* init our temps */
|
||||
if ((res = mp_init_multi(&ta, &tb, &tq, &q, 0, 0)) != MP_OKAY) {
|
||||
return res;
|
||||
@ -1313,7 +1313,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
mp_set(&tq, 1);
|
||||
n = mp_count_bits(a) - mp_count_bits(b);
|
||||
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
||||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||
((res = mp_abs(b, &tb)) != MP_OKAY) ||
|
||||
((res = mp_mul_2d(&tb, n, &tb)) != MP_OKAY) ||
|
||||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
|
||||
goto LBL_ERR;
|
||||
@ -1491,8 +1491,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++) {
|
||||
@ -1631,7 +1631,7 @@ mp_sub (mp_int * a, mp_int * b, mp_int * c)
|
||||
int mp_reduce_is_2k_l(mp_int *a)
|
||||
{
|
||||
int ix, iy;
|
||||
|
||||
|
||||
if (a->used == 0) {
|
||||
return MP_NO;
|
||||
} else if (a->used == 1) {
|
||||
@ -1644,7 +1644,7 @@ int mp_reduce_is_2k_l(mp_int *a)
|
||||
}
|
||||
}
|
||||
return (iy >= (a->used/2)) ? MP_YES : MP_NO;
|
||||
|
||||
|
||||
}
|
||||
return MP_NO;
|
||||
}
|
||||
@ -1655,7 +1655,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) {
|
||||
@ -1664,7 +1664,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) {
|
||||
@ -1774,7 +1774,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
|
||||
/* 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;
|
||||
@ -1790,7 +1790,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
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
|
||||
@ -1841,7 +1841,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y,
|
||||
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
#else
|
||||
#else
|
||||
err = MP_VAL;
|
||||
goto LBL_RES;
|
||||
#endif
|
||||
@ -2075,7 +2075,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
W = (mp_word*)XMALLOC(sizeof(mp_word) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||
if (W == NULL)
|
||||
if (W == NULL)
|
||||
return MP_MEM;
|
||||
#endif
|
||||
|
||||
@ -2316,7 +2316,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]));
|
||||
}
|
||||
|
||||
@ -2400,35 +2400,35 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
|
||||
{
|
||||
mp_int q;
|
||||
int p, res;
|
||||
|
||||
|
||||
if ((res = mp_init(&q)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
p = mp_count_bits(n);
|
||||
|
||||
p = mp_count_bits(n);
|
||||
top:
|
||||
/* q = a/2**p, a = a mod 2**p */
|
||||
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if (d != 1) {
|
||||
/* q = q * d */
|
||||
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
||||
if ((res = mp_mul_d(&q, d, &q)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
/* a = a + q */
|
||||
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||
s_mp_sub(a, n, a);
|
||||
goto top;
|
||||
}
|
||||
|
||||
|
||||
ERR:
|
||||
mp_clear(&q);
|
||||
return res;
|
||||
@ -2440,29 +2440,29 @@ 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;
|
||||
}
|
||||
|
||||
|
||||
/* computes a = 2**b
|
||||
/* computes a = 2**b
|
||||
*
|
||||
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
||||
* as required.
|
||||
@ -2578,8 +2578,8 @@ mp_sqr (mp_int * a, mp_int * b)
|
||||
{
|
||||
#ifdef BN_FAST_S_MP_SQR_C
|
||||
/* can we use the fast comba multiplier? */
|
||||
if ((a->used * 2 + 1) < MP_WARRAY &&
|
||||
a->used <
|
||||
if ((a->used * 2 + 1) < MP_WARRAY &&
|
||||
a->used <
|
||||
(1 << (sizeof(mp_word) * CHAR_BIT - 2*DIGIT_BIT - 1))) {
|
||||
res = fast_s_mp_sqr (a, b);
|
||||
} else
|
||||
@ -2604,18 +2604,18 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
{
|
||||
/* 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
|
||||
res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
|
||||
@ -2649,24 +2649,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;
|
||||
}
|
||||
@ -2678,8 +2678,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++) {
|
||||
@ -2699,14 +2699,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_digit) ( (((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;
|
||||
@ -2744,7 +2744,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
||||
mp_exch(&q, c);
|
||||
}
|
||||
mp_clear(&q);
|
||||
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -2755,8 +2755,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, 0,
|
||||
DYNAMIC_TYPE_BIGINT);
|
||||
@ -2779,10 +2779,10 @@ int mp_init_size (mp_int * a, int size)
|
||||
|
||||
|
||||
/* 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.
|
||||
@ -2812,13 +2812,13 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||
if (W == NULL)
|
||||
if (W == NULL)
|
||||
return MP_MEM;
|
||||
#endif
|
||||
|
||||
/* 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;
|
||||
@ -2839,7 +2839,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
|
||||
*/
|
||||
@ -2893,15 +2893,15 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
/* 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.
|
||||
@ -2931,13 +2931,13 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||
if (W == NULL)
|
||||
if (W == NULL)
|
||||
return MP_MEM;
|
||||
#endif
|
||||
|
||||
/* 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;
|
||||
@ -2950,7 +2950,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);
|
||||
@ -3028,7 +3028,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]);
|
||||
@ -3060,7 +3060,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
|
||||
/* 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)
|
||||
@ -3073,7 +3073,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);
|
||||
}
|
||||
@ -3095,10 +3095,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;
|
||||
|
||||
@ -3208,7 +3208,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 */
|
||||
@ -3226,7 +3226,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;
|
||||
@ -3237,22 +3237,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[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
@ -3260,7 +3260,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[(mp_digit)(1 << (winsize - 1))],
|
||||
if ((err = mp_sqr (&M[(mp_digit)(1 << (winsize - 1))],
|
||||
&M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
@ -3407,7 +3407,7 @@ LBL_M:
|
||||
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;
|
||||
}
|
||||
@ -3415,7 +3415,7 @@ int mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
}
|
||||
|
||||
|
||||
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
||||
/* reduces x mod m, assumes 0 < x < m**2, mu is
|
||||
* precomputed via mp_reduce_setup.
|
||||
* From HAC pp.604 Algorithm 14.42
|
||||
*/
|
||||
@ -3430,7 +3430,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
}
|
||||
|
||||
/* q1 = x / b**(k-1) */
|
||||
mp_rshd (&q, um - 1);
|
||||
mp_rshd (&q, um - 1);
|
||||
|
||||
/* according to HAC this optimization is ok */
|
||||
if (((mp_word) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
|
||||
@ -3446,8 +3446,8 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
#else
|
||||
{
|
||||
#else
|
||||
{
|
||||
res = MP_VAL;
|
||||
goto CLEANUP;
|
||||
}
|
||||
@ -3455,7 +3455,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
}
|
||||
|
||||
/* q3 = q2 / b**(k+1) */
|
||||
mp_rshd (&q, um + 1);
|
||||
mp_rshd (&q, um + 1);
|
||||
|
||||
/* x = x mod b**(k+1), quick (no division) */
|
||||
if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
|
||||
@ -3487,7 +3487,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
goto CLEANUP;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
CLEANUP:
|
||||
mp_clear (&q);
|
||||
|
||||
@ -3495,7 +3495,7 @@ CLEANUP:
|
||||
}
|
||||
|
||||
|
||||
/* reduces a modulo n where n is of the form 2**p - d
|
||||
/* reduces a modulo n where n is of the form 2**p - d
|
||||
This differs from reduce_2k since "d" can be larger
|
||||
than a single digit.
|
||||
*/
|
||||
@ -3503,33 +3503,33 @@ int mp_reduce_2k_l(mp_int *a, mp_int *n, mp_int *d)
|
||||
{
|
||||
mp_int q;
|
||||
int p, res;
|
||||
|
||||
|
||||
if ((res = mp_init(&q)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
p = mp_count_bits(n);
|
||||
|
||||
p = mp_count_bits(n);
|
||||
top:
|
||||
/* q = a/2**p, a = a mod 2**p */
|
||||
if ((res = mp_div_2d(a, p, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* q = q * d */
|
||||
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
|
||||
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
/* a = a + q */
|
||||
if ((res = s_mp_add(a, &q, a)) != MP_OKAY) {
|
||||
goto ERR;
|
||||
}
|
||||
|
||||
|
||||
if (mp_cmp_mag(a, n) != MP_LT) {
|
||||
s_mp_sub(a, n, a);
|
||||
goto top;
|
||||
}
|
||||
|
||||
|
||||
ERR:
|
||||
mp_clear(&q);
|
||||
return res;
|
||||
@ -3541,19 +3541,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;
|
||||
@ -3650,17 +3650,17 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
if (pa > MP_WARRAY)
|
||||
return MP_RANGE; /* TAO range check */
|
||||
|
||||
|
||||
#ifdef WOLFSSL_SMALL_STACK
|
||||
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||
if (W == NULL)
|
||||
if (W == NULL)
|
||||
return MP_MEM;
|
||||
#endif
|
||||
|
||||
/* 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;
|
||||
|
||||
@ -3672,7 +3672,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);
|
||||
@ -3688,7 +3688,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;
|
||||
@ -3723,7 +3723,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 */
|
||||
@ -4036,13 +4036,13 @@ static 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);
|
||||
@ -4051,17 +4051,17 @@ static 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;
|
||||
}
|
||||
|
||||
@ -4117,11 +4117,11 @@ const mp_digit ltm_prime_tab[] = {
|
||||
};
|
||||
|
||||
|
||||
/* 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.
|
||||
*/
|
||||
static int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
@ -4135,7 +4135,7 @@ static 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) {
|
||||
@ -4200,7 +4200,7 @@ LBL_N1:mp_clear (&n1);
|
||||
}
|
||||
|
||||
|
||||
/* 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
|
||||
@ -4392,17 +4392,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) {
|
||||
@ -4439,8 +4439,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;
|
||||
@ -4451,7 +4451,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) {
|
||||
/* if the radix < 36 the conversion is case insensitive
|
||||
@ -4465,9 +4465,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) {
|
||||
@ -4481,7 +4481,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
}
|
||||
++str;
|
||||
}
|
||||
|
||||
|
||||
/* set the sign only if a != 0 */
|
||||
if (mp_iszero(a) != 1) {
|
||||
a->sign = neg;
|
||||
|
Reference in New Issue
Block a user