forked from wolfSSL/wolfssl
remove trailing spaces.
This commit is contained in:
+139
-139
@@ -33,7 +33,7 @@
|
|||||||
/* in case user set USE_FAST_MATH there */
|
/* in case user set USE_FAST_MATH there */
|
||||||
#include <wolfssl/wolfcrypt/settings.h>
|
#include <wolfssl/wolfcrypt/settings.h>
|
||||||
|
|
||||||
#ifndef NO_BIG_INT
|
#ifndef NO_BIG_INT
|
||||||
|
|
||||||
#ifndef USE_FAST_MATH
|
#ifndef USE_FAST_MATH
|
||||||
|
|
||||||
@@ -168,7 +168,7 @@ mp_count_bits (mp_int * a)
|
|||||||
|
|
||||||
/* get number of digits and add that */
|
/* get number of digits and add that */
|
||||||
r = (a->used - 1) * DIGIT_BIT;
|
r = (a->used - 1) * DIGIT_BIT;
|
||||||
|
|
||||||
/* take the last digit and count the bits in it */
|
/* take the last digit and count the bits in it */
|
||||||
q = a->dp[a->used - 1];
|
q = a->dp[a->used - 1];
|
||||||
while (q > ((mp_digit) 0)) {
|
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
|
* This is used to ensure that leading zero digits are
|
||||||
* trimed and the leading "used" digit will be non-zero
|
* 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
|
* mp_int pointers around
|
||||||
*/
|
*/
|
||||||
void
|
void
|
||||||
@@ -513,8 +513,8 @@ void mp_rshd (mp_int * a, int b)
|
|||||||
/* top [offset into digits] */
|
/* top [offset into digits] */
|
||||||
top = a->dp + b;
|
top = a->dp + b;
|
||||||
|
|
||||||
/* this is implemented as a sliding window where
|
/* this is implemented as a sliding window where
|
||||||
* the window is b-digits long and digits from
|
* the window is b-digits long and digits from
|
||||||
* the top of the window are copied to the bottom
|
* the top of the window are copied to the bottom
|
||||||
*
|
*
|
||||||
* e.g.
|
* e.g.
|
||||||
@@ -532,7 +532,7 @@ void mp_rshd (mp_int * a, int b)
|
|||||||
*bottom++ = 0;
|
*bottom++ = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* remove excess digits */
|
/* remove excess digits */
|
||||||
a->used -= b;
|
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 */
|
/* set the carry to the carry bits of the current word */
|
||||||
r = rr;
|
r = rr;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* set final carry */
|
/* set final carry */
|
||||||
if (r != 0) {
|
if (r != 0) {
|
||||||
c->dp[(c->used)++] = r;
|
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(&tmpG);
|
||||||
mp_clear(&tmpX);
|
mp_clear(&tmpX);
|
||||||
return err;
|
return err;
|
||||||
#else
|
#else
|
||||||
/* no invmod */
|
/* no invmod */
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
#endif
|
#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;
|
dr = mp_reduce_is_2k(P) << 1;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* if the modulus is odd or dr != 0 use the montgomery method */
|
/* if the modulus is odd or dr != 0 use the montgomery method */
|
||||||
#ifdef BN_MP_EXPTMOD_FAST_C
|
#ifdef BN_MP_EXPTMOD_FAST_C
|
||||||
if (mp_isodd (P) == 1 || dr != 0) {
|
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
|
* 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,
|
/* computes the modular inverse via binary extended euclidean algorithm,
|
||||||
* that is c = 1/a mod b
|
* 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
|
* odd as per HAC Note 14.64 on pp. 610
|
||||||
*/
|
*/
|
||||||
int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
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 */
|
/* init temps */
|
||||||
if ((res = mp_init_multi(&x, &y, &u, &v,
|
if ((res = mp_init_multi(&x, &y, &u, &v,
|
||||||
&A, &B)) != MP_OKAY) {
|
&A, &B)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
@@ -1138,14 +1138,14 @@ top:
|
|||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* too big */
|
/* too big */
|
||||||
while (mp_cmp_mag(&C, b) != MP_LT) {
|
while (mp_cmp_mag(&C, b) != MP_LT) {
|
||||||
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
|
if ((res = mp_sub(&C, b, &C)) != MP_OKAY) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* C is now the inverse */
|
/* C is now the inverse */
|
||||||
mp_exch (&C, c);
|
mp_exch (&C, c);
|
||||||
res = MP_OKAY;
|
res = MP_OKAY;
|
||||||
@@ -1171,7 +1171,7 @@ int mp_cmp_mag (mp_int * a, mp_int * b)
|
|||||||
if (a->used > b->used) {
|
if (a->used > b->used) {
|
||||||
return MP_GT;
|
return MP_GT;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (a->used < b->used) {
|
if (a->used < b->used) {
|
||||||
return MP_LT;
|
return MP_LT;
|
||||||
}
|
}
|
||||||
@@ -1208,7 +1208,7 @@ mp_cmp (mp_int * a, mp_int * b)
|
|||||||
return MP_GT;
|
return MP_GT;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compare digits */
|
/* compare digits */
|
||||||
if (a->sign == MP_NEG) {
|
if (a->sign == MP_NEG) {
|
||||||
/* if negative compare opposite direction */
|
/* 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;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* init our temps */
|
/* init our temps */
|
||||||
if ((res = mp_init_multi(&ta, &tb, &tq, &q, 0, 0)) != MP_OKAY) {
|
if ((res = mp_init_multi(&ta, &tb, &tq, &q, 0, 0)) != MP_OKAY) {
|
||||||
return res;
|
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);
|
mp_set(&tq, 1);
|
||||||
n = mp_count_bits(a) - mp_count_bits(b);
|
n = mp_count_bits(a) - mp_count_bits(b);
|
||||||
if (((res = mp_abs(a, &ta)) != MP_OKAY) ||
|
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(&tb, n, &tb)) != MP_OKAY) ||
|
||||||
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
|
((res = mp_mul_2d(&tq, n, &tq)) != MP_OKAY)) {
|
||||||
goto LBL_ERR;
|
goto LBL_ERR;
|
||||||
@@ -1491,8 +1491,8 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
*tmpc++ &= MP_MASK;
|
*tmpc++ &= MP_MASK;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now copy higher words if any, that is in A+B
|
/* now copy higher words if any, that is in A+B
|
||||||
* if A or B has more digits add those in
|
* if A or B has more digits add those in
|
||||||
*/
|
*/
|
||||||
if (min != max) {
|
if (min != max) {
|
||||||
for (; i < max; i++) {
|
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 mp_reduce_is_2k_l(mp_int *a)
|
||||||
{
|
{
|
||||||
int ix, iy;
|
int ix, iy;
|
||||||
|
|
||||||
if (a->used == 0) {
|
if (a->used == 0) {
|
||||||
return MP_NO;
|
return MP_NO;
|
||||||
} else if (a->used == 1) {
|
} 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 (iy >= (a->used/2)) ? MP_YES : MP_NO;
|
||||||
|
|
||||||
}
|
}
|
||||||
return MP_NO;
|
return MP_NO;
|
||||||
}
|
}
|
||||||
@@ -1655,7 +1655,7 @@ int mp_reduce_is_2k(mp_int *a)
|
|||||||
{
|
{
|
||||||
int ix, iy, iw;
|
int ix, iy, iw;
|
||||||
mp_digit iz;
|
mp_digit iz;
|
||||||
|
|
||||||
if (a->used == 0) {
|
if (a->used == 0) {
|
||||||
return MP_NO;
|
return MP_NO;
|
||||||
} else if (a->used == 1) {
|
} else if (a->used == 1) {
|
||||||
@@ -1664,7 +1664,7 @@ int mp_reduce_is_2k(mp_int *a)
|
|||||||
iy = mp_count_bits(a);
|
iy = mp_count_bits(a);
|
||||||
iz = 1;
|
iz = 1;
|
||||||
iw = 1;
|
iw = 1;
|
||||||
|
|
||||||
/* Test every bit from the second digit up, must be 1 */
|
/* Test every bit from the second digit up, must be 1 */
|
||||||
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
for (ix = DIGIT_BIT; ix < iy; ix++) {
|
||||||
if ((a->dp[iw] & iz) == 0) {
|
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 */
|
/* determine and setup reduction code */
|
||||||
if (redmode == 0) {
|
if (redmode == 0) {
|
||||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||||
/* now setup montgomery */
|
/* now setup montgomery */
|
||||||
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
||||||
goto LBL_M;
|
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) &&
|
if (((P->used * 2 + 1) < MP_WARRAY) &&
|
||||||
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
|
||||||
#endif
|
#endif
|
||||||
{
|
{
|
||||||
#ifdef BN_MP_MONTGOMERY_REDUCE_C
|
#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) {
|
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
err = MP_VAL;
|
err = MP_VAL;
|
||||||
goto LBL_RES;
|
goto LBL_RES;
|
||||||
#endif
|
#endif
|
||||||
@@ -2075,7 +2075,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
|||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
W = (mp_word*)XMALLOC(sizeof(mp_word) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
W = (mp_word*)XMALLOC(sizeof(mp_word) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||||
if (W == NULL)
|
if (W == NULL)
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
#endif
|
#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 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]));
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2400,35 +2400,35 @@ int mp_reduce_2k(mp_int *a, mp_int *n, mp_digit d)
|
|||||||
{
|
{
|
||||||
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 (d != 1) {
|
if (d != 1) {
|
||||||
/* q = q * d */
|
/* 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;
|
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;
|
||||||
@@ -2440,29 +2440,29 @@ int 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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/* computes a = 2**b
|
/* computes a = 2**b
|
||||||
*
|
*
|
||||||
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
* Simple algorithm which zeroes the int, grows it then just sets one bit
|
||||||
* as required.
|
* as required.
|
||||||
@@ -2578,8 +2578,8 @@ mp_sqr (mp_int * a, mp_int * b)
|
|||||||
{
|
{
|
||||||
#ifdef BN_FAST_S_MP_SQR_C
|
#ifdef BN_FAST_S_MP_SQR_C
|
||||||
/* can we use the fast comba multiplier? */
|
/* can we use the fast comba 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))) {
|
||||||
res = fast_s_mp_sqr (a, b);
|
res = fast_s_mp_sqr (a, b);
|
||||||
} else
|
} else
|
||||||
@@ -2604,18 +2604,18 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
|||||||
{
|
{
|
||||||
/* 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
|
||||||
* have less than MP_WARRAY digits and the number of
|
* have less than MP_WARRAY digits and the number of
|
||||||
* digits won't affect carry propagation
|
* digits won't affect carry propagation
|
||||||
*/
|
*/
|
||||||
int digs = a->used + b->used + 1;
|
int digs = a->used + b->used + 1;
|
||||||
|
|
||||||
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
#ifdef BN_FAST_S_MP_MUL_DIGS_C
|
||||||
if ((digs < MP_WARRAY) &&
|
if ((digs < MP_WARRAY) &&
|
||||||
MIN(a->used, b->used) <=
|
MIN(a->used, b->used) <=
|
||||||
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||||
res = fast_s_mp_mul_digs (a, b, c, digs);
|
res = fast_s_mp_mul_digs (a, b, c, digs);
|
||||||
} else
|
} else
|
||||||
#endif
|
#endif
|
||||||
#ifdef BN_S_MP_MUL_DIGS_C
|
#ifdef BN_S_MP_MUL_DIGS_C
|
||||||
res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
|
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 */
|
/* alias for source */
|
||||||
tmpa = a->dp;
|
tmpa = a->dp;
|
||||||
|
|
||||||
/* alias for dest */
|
/* alias for dest */
|
||||||
tmpb = b->dp;
|
tmpb = b->dp;
|
||||||
|
|
||||||
/* carry */
|
/* carry */
|
||||||
r = 0;
|
r = 0;
|
||||||
for (x = 0; x < a->used; x++) {
|
for (x = 0; x < a->used; x++) {
|
||||||
|
|
||||||
/* get what will be the *next* carry bit from the
|
/* get what will be the *next* carry bit from the
|
||||||
* MSB of the current digit
|
* MSB of the current digit
|
||||||
*/
|
*/
|
||||||
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
rr = *tmpa >> ((mp_digit)(DIGIT_BIT - 1));
|
||||||
|
|
||||||
/* now shift up this digit, add in the carry [from the previous] */
|
/* now shift up this digit, add in the carry [from the previous] */
|
||||||
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
*tmpb++ = ((*tmpa++ << ((mp_digit)1)) | r) & MP_MASK;
|
||||||
|
|
||||||
/* copy the carry that would be from the source
|
/* copy the carry that would be from the source
|
||||||
* digit into the next iteration
|
* digit into the next iteration
|
||||||
*/
|
*/
|
||||||
r = rr;
|
r = rr;
|
||||||
}
|
}
|
||||||
@@ -2678,8 +2678,8 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
|||||||
++(b->used);
|
++(b->used);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now zero any excess digits on the destination
|
/* now zero any excess digits on the destination
|
||||||
* that we didn't write to
|
* that we didn't write to
|
||||||
*/
|
*/
|
||||||
tmpb = b->dp + b->used;
|
tmpb = b->dp + b->used;
|
||||||
for (x = b->used; x < oldused; x++) {
|
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_word w, t;
|
||||||
mp_digit b;
|
mp_digit b;
|
||||||
int res, ix;
|
int res, ix;
|
||||||
|
|
||||||
/* b = 2**DIGIT_BIT / 3 */
|
/* b = 2**DIGIT_BIT / 3 */
|
||||||
b = (mp_digit) ( (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3) );
|
b = (mp_digit) ( (((mp_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3) );
|
||||||
|
|
||||||
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.used = a->used;
|
q.used = a->used;
|
||||||
q.sign = a->sign;
|
q.sign = a->sign;
|
||||||
w = 0;
|
w = 0;
|
||||||
@@ -2744,7 +2744,7 @@ mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
|
|||||||
mp_exch(&q, c);
|
mp_exch(&q, c);
|
||||||
}
|
}
|
||||||
mp_clear(&q);
|
mp_clear(&q);
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -2755,8 +2755,8 @@ int mp_init_size (mp_int * a, int size)
|
|||||||
int x;
|
int x;
|
||||||
|
|
||||||
/* pad size so there are always extra digits */
|
/* pad size so there are always extra digits */
|
||||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||||
|
|
||||||
/* alloc mem */
|
/* alloc mem */
|
||||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size, 0,
|
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size, 0,
|
||||||
DYNAMIC_TYPE_BIGINT);
|
DYNAMIC_TYPE_BIGINT);
|
||||||
@@ -2779,10 +2779,10 @@ int mp_init_size (mp_int * a, int size)
|
|||||||
|
|
||||||
|
|
||||||
/* the jist of squaring...
|
/* the jist of squaring...
|
||||||
* you do like mult except the offset of the tmpx [one that
|
* you do like mult except the offset of the tmpx [one that
|
||||||
* starts closer to zero] can't equal the offset of tmpy.
|
* starts closer to zero] can't equal the offset of tmpy.
|
||||||
* So basically you set up iy like before then you min it with
|
* 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
|
* you add in the inner loop
|
||||||
|
|
||||||
After that loop you do the squares and add them in.
|
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
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||||
if (W == NULL)
|
if (W == NULL)
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* number of output digits to produce */
|
/* number of output digits to produce */
|
||||||
W1 = 0;
|
W1 = 0;
|
||||||
for (ix = 0; ix < pa; ix++) {
|
for (ix = 0; ix < pa; ix++) {
|
||||||
int tx, ty, iy;
|
int tx, ty, iy;
|
||||||
mp_word _W;
|
mp_word _W;
|
||||||
mp_digit *tmpy;
|
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);
|
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
|
* we halve the distance since they approach at a rate of 2x
|
||||||
* and we have to round because odd cases need to be executed
|
* 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
|
/* Fast (comba) multiplier
|
||||||
*
|
*
|
||||||
* This is the fast column-array [comba] multiplier. It is
|
* This is the fast column-array [comba] multiplier. It is
|
||||||
* designed to compute the columns of the product first
|
* designed to compute the columns of the product first
|
||||||
* then handle the carries afterwards. This has the effect
|
* then handle the carries afterwards. This has the effect
|
||||||
* of making the nested loops that compute the columns very
|
* of making the nested loops that compute the columns very
|
||||||
* simple and schedulable on super-scalar processors.
|
* simple and schedulable on super-scalar processors.
|
||||||
*
|
*
|
||||||
* This has been modified to produce a variable number of
|
* This has been modified to produce a variable number of
|
||||||
* digits of output so if say only a half-product is required
|
* digits of output so if say only a half-product is required
|
||||||
* you don't have to compute the upper half (a feature
|
* you don't have to compute the upper half (a feature
|
||||||
* required for fast Barrett reduction).
|
* required for fast Barrett reduction).
|
||||||
*
|
*
|
||||||
* Based on Algorithm 14.12 on pp.595 of HAC.
|
* 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
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||||
if (W == NULL)
|
if (W == NULL)
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* clear the carry */
|
/* clear the carry */
|
||||||
_W = 0;
|
_W = 0;
|
||||||
for (ix = 0; ix < pa; ix++) {
|
for (ix = 0; ix < pa; ix++) {
|
||||||
int tx, ty;
|
int tx, ty;
|
||||||
int iy;
|
int iy;
|
||||||
mp_digit *tmpx, *tmpy;
|
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;
|
tmpx = a->dp + tx;
|
||||||
tmpy = b->dp + ty;
|
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) { ... }
|
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||||
*/
|
*/
|
||||||
iy = MIN(a->used-tx, ty+1);
|
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 */
|
/* alias for where to store the results */
|
||||||
tmpt = t.dp + (2*ix + 1);
|
tmpt = t.dp + (2*ix + 1);
|
||||||
|
|
||||||
for (iy = ix + 1; iy < pa; iy++) {
|
for (iy = ix + 1; iy < pa; iy++) {
|
||||||
/* first calculate the product */
|
/* first calculate the product */
|
||||||
r = ((mp_word)tmpx) * ((mp_word)a->dp[iy]);
|
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
|
/* 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.
|
* many digits of output are created.
|
||||||
*/
|
*/
|
||||||
int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
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? */
|
/* can we use the fast multiplier? */
|
||||||
if (((digs) < MP_WARRAY) &&
|
if (((digs) < MP_WARRAY) &&
|
||||||
MIN (a->used, b->used) <
|
MIN (a->used, b->used) <
|
||||||
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) {
|
||||||
return fast_s_mp_mul_digs (a, b, c, digs);
|
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 */
|
/* setup some aliases */
|
||||||
/* copy of the digit from a used within the nested loop */
|
/* copy of the digit from a used within the nested loop */
|
||||||
tmpx = a->dp[ix];
|
tmpx = a->dp[ix];
|
||||||
|
|
||||||
/* an alias for the destination shifted ix places */
|
/* an alias for the destination shifted ix places */
|
||||||
tmpt = t.dp + ix;
|
tmpt = t.dp + ix;
|
||||||
|
|
||||||
/* an alias for the digits of b */
|
/* an alias for the digits of b */
|
||||||
tmpy = b->dp;
|
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 M array */
|
||||||
/* init first cell */
|
/* init first cell */
|
||||||
if ((err = mp_init(&M[1])) != MP_OKAY) {
|
if ((err = mp_init(&M[1])) != MP_OKAY) {
|
||||||
return err;
|
return err;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* now init the second half of the array */
|
/* 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) {
|
if ((err = mp_init (&mu)) != MP_OKAY) {
|
||||||
goto LBL_M;
|
goto LBL_M;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (redmode == 0) {
|
if (redmode == 0) {
|
||||||
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
||||||
goto LBL_MU;
|
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;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
redux = mp_reduce_2k_l;
|
redux = mp_reduce_2k_l;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* create M table
|
/* 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
|
* 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]
|
* computed though accept for M[0] and M[1]
|
||||||
*/
|
*/
|
||||||
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
||||||
goto LBL_MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||||
* M[1] (winsize-1) times
|
* M[1] (winsize-1) times
|
||||||
*/
|
*/
|
||||||
if ((err = mp_copy (&M[1], &M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
|
if ((err = mp_copy (&M[1], &M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
|
||||||
goto LBL_MU;
|
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++) {
|
for (x = 0; x < (winsize - 1); x++) {
|
||||||
/* square it */
|
/* 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) {
|
&M[(mp_digit)(1 << (winsize - 1))])) != MP_OKAY) {
|
||||||
goto LBL_MU;
|
goto LBL_MU;
|
||||||
}
|
}
|
||||||
@@ -3407,7 +3407,7 @@ LBL_M:
|
|||||||
int mp_reduce_setup (mp_int * a, mp_int * b)
|
int 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;
|
||||||
}
|
}
|
||||||
@@ -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.
|
* precomputed via mp_reduce_setup.
|
||||||
* From HAC pp.604 Algorithm 14.42
|
* 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) */
|
/* q1 = x / b**(k-1) */
|
||||||
mp_rshd (&q, um - 1);
|
mp_rshd (&q, um - 1);
|
||||||
|
|
||||||
/* according to HAC this optimization is ok */
|
/* according to HAC this optimization is ok */
|
||||||
if (((mp_word) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
|
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) {
|
if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
|
||||||
goto CLEANUP;
|
goto CLEANUP;
|
||||||
}
|
}
|
||||||
#else
|
#else
|
||||||
{
|
{
|
||||||
res = MP_VAL;
|
res = MP_VAL;
|
||||||
goto CLEANUP;
|
goto CLEANUP;
|
||||||
}
|
}
|
||||||
@@ -3455,7 +3455,7 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* q3 = q2 / b**(k+1) */
|
/* q3 = q2 / b**(k+1) */
|
||||||
mp_rshd (&q, um + 1);
|
mp_rshd (&q, um + 1);
|
||||||
|
|
||||||
/* x = x mod b**(k+1), quick (no division) */
|
/* x = x mod b**(k+1), quick (no division) */
|
||||||
if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
|
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;
|
goto CLEANUP;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
CLEANUP:
|
CLEANUP:
|
||||||
mp_clear (&q);
|
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
|
This differs from reduce_2k since "d" can be larger
|
||||||
than a single digit.
|
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;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* q = q * d */
|
/* q = q * d */
|
||||||
if ((res = mp_mul(&q, d, &q)) != MP_OKAY) {
|
if ((res = mp_mul(&q, d, &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;
|
||||||
@@ -3541,19 +3541,19 @@ int mp_reduce_2k_setup_l(mp_int *a, mp_int *d)
|
|||||||
{
|
{
|
||||||
int res;
|
int res;
|
||||||
mp_int tmp;
|
mp_int tmp;
|
||||||
|
|
||||||
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
if ((res = mp_init(&tmp)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
|
if ((res = mp_2expt(&tmp, mp_count_bits(a))) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
|
if ((res = s_mp_sub(&tmp, a, d)) != MP_OKAY) {
|
||||||
goto ERR;
|
goto ERR;
|
||||||
}
|
}
|
||||||
|
|
||||||
ERR:
|
ERR:
|
||||||
mp_clear(&tmp);
|
mp_clear(&tmp);
|
||||||
return res;
|
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)
|
if (pa > MP_WARRAY)
|
||||||
return MP_RANGE; /* TAO range check */
|
return MP_RANGE; /* TAO range check */
|
||||||
|
|
||||||
#ifdef WOLFSSL_SMALL_STACK
|
#ifdef WOLFSSL_SMALL_STACK
|
||||||
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
W = (mp_digit*)XMALLOC(sizeof(mp_digit) * MP_WARRAY, 0, DYNAMIC_TYPE_BIGINT);
|
||||||
if (W == NULL)
|
if (W == NULL)
|
||||||
return MP_MEM;
|
return MP_MEM;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
/* number of output digits to produce */
|
/* number of output digits to produce */
|
||||||
pa = a->used + b->used;
|
pa = a->used + b->used;
|
||||||
_W = 0;
|
_W = 0;
|
||||||
for (ix = digs; ix < pa; ix++) {
|
for (ix = digs; ix < pa; ix++) {
|
||||||
int tx, ty, iy;
|
int tx, ty, iy;
|
||||||
mp_digit *tmpx, *tmpy;
|
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;
|
tmpx = a->dp + tx;
|
||||||
tmpy = b->dp + ty;
|
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) { ... }
|
while (tx++ < a->used && ty-- >= 0) { ... }
|
||||||
*/
|
*/
|
||||||
iy = MIN(a->used-tx, ty+1);
|
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 */
|
/* make next carry */
|
||||||
_W = _W >> ((mp_word)DIGIT_BIT);
|
_W = _W >> ((mp_word)DIGIT_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* setup dest */
|
/* setup dest */
|
||||||
olduse = c->used;
|
olduse = c->used;
|
||||||
c->used = pa;
|
c->used = pa;
|
||||||
@@ -3723,7 +3723,7 @@ int mp_set_int (mp_int * a, unsigned long b)
|
|||||||
int x, res;
|
int x, res;
|
||||||
|
|
||||||
mp_zero (a);
|
mp_zero (a);
|
||||||
|
|
||||||
/* set four bits at a time */
|
/* set four bits at a time */
|
||||||
for (x = 0; x < 8; x++) {
|
for (x = 0; x < 8; x++) {
|
||||||
/* shift the number up four bits */
|
/* 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) {
|
if ((res = mp_init_size(&q, a->used)) != MP_OKAY) {
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
||||||
q.used = a->used;
|
q.used = a->used;
|
||||||
q.sign = a->sign;
|
q.sign = a->sign;
|
||||||
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 >= b) {
|
if (w >= b) {
|
||||||
t = (mp_digit)(w / b);
|
t = (mp_digit)(w / b);
|
||||||
w -= ((mp_word)t) * ((mp_word)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;
|
q.dp[ix] = (mp_digit)t;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (d != NULL) {
|
if (d != NULL) {
|
||||||
*d = (mp_digit)w;
|
*d = (mp_digit)w;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (c != NULL) {
|
if (c != NULL) {
|
||||||
mp_clamp(&q);
|
mp_clamp(&q);
|
||||||
mp_exch(&q, c);
|
mp_exch(&q, c);
|
||||||
}
|
}
|
||||||
mp_clear(&q);
|
mp_clear(&q);
|
||||||
|
|
||||||
return res;
|
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
|
* HAC pp. 139 Algorithm 4.24
|
||||||
*
|
*
|
||||||
* Sets result to 0 if definitely composite or 1 if probably prime.
|
* 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.
|
* very much lower.
|
||||||
*/
|
*/
|
||||||
static int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
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 */
|
/* ensure b > 1 */
|
||||||
if (mp_cmp_d(b, 1) != MP_GT) {
|
if (mp_cmp_d(b, 1) != MP_GT) {
|
||||||
return MP_VAL;
|
return MP_VAL;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* get n1 = a - 1 */
|
/* get n1 = a - 1 */
|
||||||
if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
|
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
|
* of the first PRIME_SIZE primes or not
|
||||||
*
|
*
|
||||||
* sets result to 0 if not, 1 if yes
|
* 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 */
|
/* swap u and v to make sure v is >= u */
|
||||||
mp_exch(&u, &v);
|
mp_exch(&u, &v);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* subtract smallest from largest */
|
/* subtract smallest from largest */
|
||||||
if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
|
if ((res = s_mp_sub(&v, &u, &v)) != MP_OKAY) {
|
||||||
goto LBL_V;
|
goto LBL_V;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Divide out all factors of two */
|
/* Divide out all factors of two */
|
||||||
if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
|
if ((res = mp_div_2d(&v, mp_cnt_lsb(&v), &v, NULL)) != MP_OKAY) {
|
||||||
goto LBL_V;
|
goto LBL_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) {
|
||||||
@@ -4439,8 +4439,8 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
|||||||
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;
|
||||||
@@ -4451,7 +4451,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
|||||||
|
|
||||||
/* 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
|
||||||
@@ -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
|
* 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) {
|
||||||
@@ -4481,7 +4481,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
|||||||
}
|
}
|
||||||
++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;
|
||||||
|
|||||||
Reference in New Issue
Block a user