format with astyle (step 2)

This commit is contained in:
Francois Perrad 2017-08-29 22:23:48 +02:00
parent 45771cc91c
commit 9eed07f09b
32 changed files with 735 additions and 735 deletions

View File

@ -16,12 +16,12 @@
*/ */
static const struct { static const struct {
int code; int code;
const char *msg; const char *msg;
} msgs[] = { } msgs[] = {
{ MP_OKAY, "Successful" }, { MP_OKAY, "Successful" },
{ MP_MEM, "Out of heap" }, { MP_MEM, "Out of heap" },
{ MP_VAL, "Value out of range" } { MP_VAL, "Value out of range" }
}; };
/* return a char * string for a given code */ /* return a char * string for a given code */
@ -31,9 +31,9 @@ const char *mp_error_to_string(int code)
/* scan the lookup table for the given message */ /* scan the lookup table for the given message */
for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) { for (x = 0; x < (int)(sizeof(msgs) / sizeof(msgs[0])); x++) {
if (msgs[x].code == code) { if (msgs[x].code == code) {
return msgs[x].msg; return msgs[x].msg;
} }
} }
/* generic reply for invalid code */ /* generic reply for invalid code */

View File

@ -33,23 +33,23 @@
*/ */
int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs) int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY]; mp_digit W[MP_WARRAY];
mp_word _W; mp_word _W;
/* grow the destination as required */ /* grow the destination as required */
if (c->alloc < digs) { if (c->alloc < digs) {
if ((res = mp_grow(c, digs)) != MP_OKAY) { if ((res = mp_grow(c, digs)) != MP_OKAY) {
return res; return res;
} }
} }
/* number of output digits to produce */ /* number of output digits to produce */
pa = MIN(digs, a->used + b->used); pa = MIN(digs, a->used + b->used);
/* 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;
@ -78,27 +78,27 @@ int fast_s_mp_mul_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;
{ {
mp_digit *tmpc; mp_digit *tmpc;
tmpc = c->dp; tmpc = c->dp;
for (ix = 0; ix < (pa + 1); ix++) { for (ix = 0; ix < (pa + 1); ix++) {
/* now extract the previous digit [below the carry] */ /* now extract the previous digit [below the carry] */
*tmpc++ = W[ix]; *tmpc++ = W[ix];
} }
/* clear unused digits [that existed in the old copy of c] */ /* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) { for (; ix < olduse; ix++) {
*tmpc++ = 0; *tmpc++ = 0;
} }
} }
mp_clamp(c); mp_clamp(c);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -26,22 +26,22 @@
*/ */
int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs) int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY]; mp_digit W[MP_WARRAY];
mp_word _W; mp_word _W;
/* grow the destination as required */ /* grow the destination as required */
pa = a->used + b->used; pa = a->used + b->used;
if (c->alloc < pa) { if (c->alloc < pa) {
if ((res = mp_grow(c, pa)) != MP_OKAY) { if ((res = mp_grow(c, pa)) != MP_OKAY) {
return res; return res;
} }
} }
/* 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;
@ -68,28 +68,28 @@ 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;
{ {
mp_digit *tmpc; mp_digit *tmpc;
tmpc = c->dp + digs; tmpc = c->dp + digs;
for (ix = digs; ix < pa; ix++) { for (ix = digs; ix < pa; ix++) {
/* now extract the previous digit [below the carry] */ /* now extract the previous digit [below the carry] */
*tmpc++ = W[ix]; *tmpc++ = W[ix];
} }
/* clear unused digits [that existed in the old copy of c] */ /* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) { for (; ix < olduse; ix++) {
*tmpc++ = 0; *tmpc++ = 0;
} }
} }
mp_clamp(c); mp_clamp(c);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -27,21 +27,21 @@ After that loop you do the squares and add them in.
int fast_s_mp_sqr(mp_int *a, mp_int *b) int fast_s_mp_sqr(mp_int *a, mp_int *b)
{ {
int olduse, res, pa, ix, iz; int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY], *tmpx; mp_digit W[MP_WARRAY], *tmpx;
mp_word W1; mp_word W1;
/* grow the destination as required */ /* grow the destination as required */
pa = a->used + a->used; pa = a->used + a->used;
if (b->alloc < pa) { if (b->alloc < pa) {
if ((res = mp_grow(b, pa)) != MP_OKAY) { if ((res = mp_grow(b, pa)) != MP_OKAY) {
return res; return res;
} }
} }
/* 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;
@ -86,26 +86,26 @@ int fast_s_mp_sqr(mp_int *a, mp_int *b)
/* make next carry */ /* make next carry */
W1 = _W >> ((mp_word)DIGIT_BIT); W1 = _W >> ((mp_word)DIGIT_BIT);
} }
/* setup dest */ /* setup dest */
olduse = b->used; olduse = b->used;
b->used = a->used+a->used; b->used = a->used+a->used;
{ {
mp_digit *tmpb; mp_digit *tmpb;
tmpb = b->dp; tmpb = b->dp;
for (ix = 0; ix < pa; ix++) { for (ix = 0; ix < pa; ix++) {
*tmpb++ = W[ix] & MP_MASK; *tmpb++ = W[ix] & MP_MASK;
} }
/* clear unused digits [that existed in the old copy of c] */ /* clear unused digits [that existed in the old copy of c] */
for (; ix < olduse; ix++) { for (; ix < olduse; ix++) {
*tmpb++ = 0; *tmpb++ = 0;
} }
} }
mp_clamp(b); mp_clamp(b);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -22,23 +22,23 @@
*/ */
int mp_2expt(mp_int *a, int b) int mp_2expt(mp_int *a, int b)
{ {
int res; int res;
/* zero a as per default */ /* zero a as per default */
mp_zero(a); mp_zero(a);
/* grow a to accomodate the single bit */ /* grow a to accomodate the single bit */
if ((res = mp_grow(a, (b / DIGIT_BIT) + 1)) != MP_OKAY) { if ((res = mp_grow(a, (b / DIGIT_BIT) + 1)) != MP_OKAY) {
return res; return res;
} }
/* set the used count of where the bit will go */ /* set the used count of where the bit will go */
a->used = (b / DIGIT_BIT) + 1; a->used = (b / DIGIT_BIT) + 1;
/* put the single bit in its place */ /* put the single bit in its place */
a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT); a->dp[b / DIGIT_BIT] = ((mp_digit)1) << (b % DIGIT_BIT);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -21,19 +21,19 @@
*/ */
int mp_abs(mp_int *a, mp_int *b) int mp_abs(mp_int *a, mp_int *b)
{ {
int res; int res;
/* copy a to b */ /* copy a to b */
if (a != b) { if (a != b) {
if ((res = mp_copy(a, b)) != MP_OKAY) { if ((res = mp_copy(a, b)) != MP_OKAY) {
return res; return res;
} }
} }
/* force the sign of b to positive */ /* force the sign of b to positive */
b->sign = MP_ZPOS; b->sign = MP_ZPOS;
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -18,90 +18,90 @@
/* single digit addition */ /* single digit addition */
int mp_add_d(mp_int *a, mp_digit b, mp_int *c) int mp_add_d(mp_int *a, mp_digit b, mp_int *c)
{ {
int res, ix, oldused; int res, ix, oldused;
mp_digit *tmpa, *tmpc, mu; mp_digit *tmpa, *tmpc, mu;
/* grow c as required */ /* grow c as required */
if (c->alloc < (a->used + 1)) { if (c->alloc < (a->used + 1)) {
if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) { if ((res = mp_grow(c, a->used + 1)) != MP_OKAY) {
return res; return res;
} }
} }
/* if a is negative and |a| >= b, call c = |a| - b */ /* if a is negative and |a| >= b, call c = |a| - b */
if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) { if ((a->sign == MP_NEG) && ((a->used > 1) || (a->dp[0] >= b))) {
/* temporarily fix sign of a */ /* temporarily fix sign of a */
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
/* c = |a| - b */ /* c = |a| - b */
res = mp_sub_d(a, b, c); res = mp_sub_d(a, b, c);
/* fix sign */ /* fix sign */
a->sign = c->sign = MP_NEG; a->sign = c->sign = MP_NEG;
/* clamp */ /* clamp */
mp_clamp(c); mp_clamp(c);
return res; return res;
} }
/* old number of used digits in c */ /* old number of used digits in c */
oldused = c->used; oldused = c->used;
/* source alias */ /* source alias */
tmpa = a->dp; tmpa = a->dp;
/* destination alias */ /* destination alias */
tmpc = c->dp; tmpc = c->dp;
/* if a is positive */ /* if a is positive */
if (a->sign == MP_ZPOS) { if (a->sign == MP_ZPOS) {
/* add digit, after this we're propagating /* add digit, after this we're propagating
* the carry. * the carry.
*/ */
*tmpc = *tmpa++ + b; *tmpc = *tmpa++ + b;
mu = *tmpc >> DIGIT_BIT; mu = *tmpc >> DIGIT_BIT;
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
/* now handle rest of the digits */ /* now handle rest of the digits */
for (ix = 1; ix < a->used; ix++) { for (ix = 1; ix < a->used; ix++) {
*tmpc = *tmpa++ + mu; *tmpc = *tmpa++ + mu;
mu = *tmpc >> DIGIT_BIT; mu = *tmpc >> DIGIT_BIT;
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
} }
/* set final carry */ /* set final carry */
ix++; ix++;
*tmpc++ = mu; *tmpc++ = mu;
/* setup size */ /* setup size */
c->used = a->used + 1; c->used = a->used + 1;
} else { } else {
/* a was negative and |a| < b */ /* a was negative and |a| < b */
c->used = 1; c->used = 1;
/* the result is a single digit */ /* the result is a single digit */
if (a->used == 1) { if (a->used == 1) {
*tmpc++ = b - a->dp[0]; *tmpc++ = b - a->dp[0];
} else { } else {
*tmpc++ = b; *tmpc++ = b;
} }
/* setup count so the clearing of oldused /* setup count so the clearing of oldused
* can fall through correctly * can fall through correctly
*/ */
ix = 1; ix = 1;
} }
/* sign always positive */ /* sign always positive */
c->sign = MP_ZPOS; c->sign = MP_ZPOS;
/* now zero to oldused */ /* now zero to oldused */
while (ix++ < oldused) { while (ix++ < oldused) {
*tmpc++ = 0; *tmpc++ = 0;
} }
mp_clamp(c); mp_clamp(c);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -18,20 +18,20 @@
/* d = a + b (mod c) */ /* d = a + b (mod c) */
int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d) int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
{ {
int res; int res;
mp_int t; mp_int t;
if ((res = mp_init(&t)) != MP_OKAY) { if ((res = mp_init(&t)) != MP_OKAY) {
return res; return res;
} }
if ((res = mp_add(a, b, &t)) != MP_OKAY) { if ((res = mp_add(a, b, &t)) != MP_OKAY) {
mp_clear(&t); mp_clear(&t);
return res; return res;
} }
res = mp_mod(&t, c, d); res = mp_mod(&t, c, d);
mp_clear(&t); mp_clear(&t);
return res; return res;
} }
#endif #endif

View File

@ -18,36 +18,36 @@
/* AND two ints together */ /* AND two ints together */
int mp_and(mp_int *a, mp_int *b, mp_int *c) int mp_and(mp_int *a, mp_int *b, mp_int *c)
{ {
int res, ix, px; int res, ix, px;
mp_int t, *x; mp_int t, *x;
if (a->used > b->used) { if (a->used > b->used) {
if ((res = mp_init_copy(&t, a)) != MP_OKAY) { if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
return res; return res;
} }
px = b->used; px = b->used;
x = b; x = b;
} else { } else {
if ((res = mp_init_copy(&t, b)) != MP_OKAY) { if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
return res; return res;
} }
px = a->used; px = a->used;
x = a; x = a;
} }
for (ix = 0; ix < px; ix++) { for (ix = 0; ix < px; ix++) {
t.dp[ix] &= x->dp[ix]; t.dp[ix] &= x->dp[ix];
} }
/* zero digits above the last from the smallest mp_int */ /* zero digits above the last from the smallest mp_int */
for (; ix < t.used; ix++) { for (; ix < t.used; ix++) {
t.dp[ix] = 0; t.dp[ix] = 0;
} }
mp_clamp(&t); mp_clamp(&t);
mp_exch(c, &t); mp_exch(c, &t);
mp_clear(&t); mp_clear(&t);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -24,17 +24,17 @@
*/ */
void mp_clamp(mp_int *a) void mp_clamp(mp_int *a)
{ {
/* decrease used while the most significant digit is /* decrease used while the most significant digit is
* zero. * zero.
*/ */
while ((a->used > 0) && (a->dp[a->used - 1] == 0)) { while ((a->used > 0) && (a->dp[a->used - 1] == 0)) {
--(a->used); --(a->used);
} }
/* reset the sign flag if used == 0 */ /* reset the sign flag if used == 0 */
if (a->used == 0) { if (a->used == 0) {
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
} }
} }
#endif #endif

View File

@ -18,23 +18,23 @@
/* clear one (frees) */ /* clear one (frees) */
void mp_clear(mp_int *a) void mp_clear(mp_int *a)
{ {
int i; int i;
/* only do anything if a hasn't been freed previously */ /* only do anything if a hasn't been freed previously */
if (a->dp != NULL) { if (a->dp != NULL) {
/* first zero the digits */ /* first zero the digits */
for (i = 0; i < a->used; i++) { for (i = 0; i < a->used; i++) {
a->dp[i] = 0; a->dp[i] = 0;
} }
/* free ram */ /* free ram */
XFREE(a->dp); XFREE(a->dp);
/* reset members to make debugging easier */ /* reset members to make debugging easier */
a->dp = NULL; a->dp = NULL;
a->alloc = a->used = 0; a->alloc = a->used = 0;
a->sign = MP_ZPOS; a->sign = MP_ZPOS;
} }
} }
#endif #endif

View File

@ -18,14 +18,14 @@
void mp_clear_multi(mp_int *mp, ...) void mp_clear_multi(mp_int *mp, ...)
{ {
mp_int* next_mp = mp; mp_int *next_mp = mp;
va_list args; va_list args;
va_start(args, mp); va_start(args, mp);
while (next_mp != NULL) { while (next_mp != NULL) {
mp_clear(next_mp); mp_clear(next_mp);
next_mp = va_arg(args, mp_int*); next_mp = va_arg(args, mp_int *);
} }
va_end(args); va_end(args);
} }
#endif #endif

View File

@ -18,22 +18,22 @@
/* compare two ints (signed)*/ /* compare two ints (signed)*/
int mp_cmp(mp_int *a, mp_int *b) int mp_cmp(mp_int *a, mp_int *b)
{ {
/* compare based on sign */ /* compare based on sign */
if (a->sign != b->sign) { if (a->sign != b->sign) {
if (a->sign == MP_NEG) { if (a->sign == MP_NEG) {
return MP_LT; return MP_LT;
} else { } else {
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 */
return mp_cmp_mag(b, a); return mp_cmp_mag(b, a);
} else { } else {
return mp_cmp_mag(a, b); return mp_cmp_mag(a, b);
} }
} }
#endif #endif

View File

@ -18,24 +18,24 @@
/* compare a digit */ /* compare a digit */
int mp_cmp_d(mp_int *a, mp_digit b) int mp_cmp_d(mp_int *a, mp_digit b)
{ {
/* compare based on sign */ /* compare based on sign */
if (a->sign == MP_NEG) { if (a->sign == MP_NEG) {
return MP_LT; return MP_LT;
} }
/* compare based on magnitude */ /* compare based on magnitude */
if (a->used > 1) { if (a->used > 1) {
return MP_GT; return MP_GT;
} }
/* compare the only digit of a to b */ /* compare the only digit of a to b */
if (a->dp[0] > b) { if (a->dp[0] > b) {
return MP_GT; return MP_GT;
} else if (a->dp[0] < b) { } else if (a->dp[0] < b) {
return MP_LT; return MP_LT;
} else { } else {
return MP_EQ; return MP_EQ;
} }
} }
#endif #endif

View File

@ -18,35 +18,35 @@
/* compare maginitude of two ints (unsigned) */ /* compare maginitude of two ints (unsigned) */
int mp_cmp_mag(mp_int *a, mp_int *b) int mp_cmp_mag(mp_int *a, mp_int *b)
{ {
int n; int n;
mp_digit *tmpa, *tmpb; mp_digit *tmpa, *tmpb;
/* compare based on # of non-zero digits */ /* compare based on # of non-zero digits */
if (a->used > b->used) { if (a->used > b->used) {
return MP_GT;
}
if (a->used < b->used) {
return MP_LT;
}
/* alias for a */
tmpa = a->dp + (a->used - 1);
/* alias for b */
tmpb = b->dp + (a->used - 1);
/* compare based on digits */
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
if (*tmpa > *tmpb) {
return MP_GT; return MP_GT;
} }
if (*tmpa < *tmpb) { if (a->used < b->used) {
return MP_LT; return MP_LT;
} }
}
return MP_EQ; /* alias for a */
tmpa = a->dp + (a->used - 1);
/* alias for b */
tmpb = b->dp + (a->used - 1);
/* compare based on digits */
for (n = 0; n < a->used; ++n, --tmpa, --tmpb) {
if (*tmpa > *tmpb) {
return MP_GT;
}
if (*tmpa < *tmpb) {
return MP_LT;
}
}
return MP_EQ;
} }
#endif #endif

View File

@ -18,47 +18,47 @@
/* copy, b = a */ /* copy, b = a */
int mp_copy(mp_int *a, mp_int *b) int mp_copy(mp_int *a, mp_int *b)
{ {
int res, n; int res, n;
/* if dst == src do nothing */ /* if dst == src do nothing */
if (a == b) { if (a == b) {
return MP_OKAY; return MP_OKAY;
} }
/* grow dest */ /* grow dest */
if (b->alloc < a->used) { if (b->alloc < a->used) {
if ((res = mp_grow(b, a->used)) != MP_OKAY) { if ((res = mp_grow(b, a->used)) != MP_OKAY) {
return res; return res;
} }
} }
/* zero b and copy the parameters over */ /* zero b and copy the parameters over */
{ {
mp_digit *tmpa, *tmpb; mp_digit *tmpa, *tmpb;
/* pointer aliases */ /* pointer aliases */
/* source */ /* source */
tmpa = a->dp; tmpa = a->dp;
/* destination */ /* destination */
tmpb = b->dp; tmpb = b->dp;
/* copy all the digits */ /* copy all the digits */
for (n = 0; n < a->used; n++) { for (n = 0; n < a->used; n++) {
*tmpb++ = *tmpa++; *tmpb++ = *tmpa++;
} }
/* clear high digits */ /* clear high digits */
for (; n < b->used; n++) { for (; n < b->used; n++) {
*tmpb++ = 0; *tmpb++ = 0;
} }
} }
/* copy used count and sign */ /* copy used count and sign */
b->used = a->used; b->used = a->used;
b->sign = a->sign; b->sign = a->sign;
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -18,24 +18,24 @@
/* returns the number of bits in an int */ /* returns the number of bits in an int */
int mp_count_bits(mp_int *a) int mp_count_bits(mp_int *a)
{ {
int r; int r;
mp_digit q; mp_digit q;
/* shortcut */ /* shortcut */
if (a->used == 0) { if (a->used == 0) {
return 0; return 0;
} }
/* 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)) {
++r; ++r;
q >>= ((mp_digit) 1); q >>= ((mp_digit) 1);
} }
return r; return r;
} }
#endif #endif

View File

@ -18,48 +18,48 @@
/* b = a/2 */ /* b = a/2 */
int mp_div_2(mp_int *a, mp_int *b) int mp_div_2(mp_int *a, mp_int *b)
{ {
int x, res, oldused; int x, res, oldused;
/* copy */ /* copy */
if (b->alloc < a->used) { if (b->alloc < a->used) {
if ((res = mp_grow(b, a->used)) != MP_OKAY) { if ((res = mp_grow(b, a->used)) != MP_OKAY) {
return res; return res;
} }
} }
oldused = b->used; oldused = b->used;
b->used = a->used; b->used = a->used;
{ {
mp_digit r, rr, *tmpa, *tmpb; mp_digit r, rr, *tmpa, *tmpb;
/* source alias */ /* source alias */
tmpa = a->dp + b->used - 1; tmpa = a->dp + b->used - 1;
/* dest alias */ /* dest alias */
tmpb = b->dp + b->used - 1; tmpb = b->dp + b->used - 1;
/* carry */ /* carry */
r = 0; r = 0;
for (x = b->used - 1; x >= 0; x--) { for (x = b->used - 1; x >= 0; x--) {
/* get the carry for the next iteration */ /* get the carry for the next iteration */
rr = *tmpa & 1; rr = *tmpa & 1;
/* shift the current digit, add in carry and store */ /* shift the current digit, add in carry and store */
*tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1)); *tmpb-- = (*tmpa-- >> 1) | (r << (DIGIT_BIT - 1));
/* forward carry to next iteration */ /* forward carry to next iteration */
r = rr; r = rr;
} }
/* zero excess digits */ /* zero excess digits */
tmpb = b->dp + b->used; tmpb = b->dp + b->used;
for (x = b->used; x < oldused; x++) { for (x = b->used; x < oldused; x++) {
*tmpb++ = 0; *tmpb++ = 0;
} }
} }
b->sign = a->sign; b->sign = a->sign;
mp_clamp(b); mp_clamp(b);
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -18,57 +18,57 @@
/* divide by three (based on routine from MPI and the GMP manual) */ /* divide by three (based on routine from MPI and the GMP manual) */
int mp_div_3(mp_int *a, mp_int *c, mp_digit *d) int mp_div_3(mp_int *a, mp_int *c, mp_digit *d)
{ {
mp_int q; mp_int q;
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_word)1) << ((mp_word)DIGIT_BIT)) / ((mp_word)3); b = (((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;
for (ix = a->used - 1; ix >= 0; ix--) { for (ix = a->used - 1; ix >= 0; ix--) {
w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]); w = (w << ((mp_word)DIGIT_BIT)) | ((mp_word)a->dp[ix]);
if (w >= 3) { if (w >= 3) {
/* multiply w by [1/3] */ /* multiply w by [1/3] */
t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT); t = (w * ((mp_word)b)) >> ((mp_word)DIGIT_BIT);
/* now subtract 3 * [w/3] from w, to get the remainder */ /* now subtract 3 * [w/3] from w, to get the remainder */
w -= t+t+t; w -= t+t+t;
/* fixup the remainder as required since /* fixup the remainder as required since
* the optimization is not exact. * the optimization is not exact.
*/ */
while (w >= 3) { while (w >= 3) {
t += 1; t += 1;
w -= 3; w -= 3;
} }
} else { } else {
t = 0; t = 0;
} }
q.dp[ix] = (mp_digit)t; q.dp[ix] = (mp_digit)t;
} }
/* [optional] store the remainder */ /* [optional] store the remainder */
if (d != NULL) { if (d != NULL) {
*d = (mp_digit)w; *d = (mp_digit)w;
} }
/* [optional] store the quotient */ /* [optional] store the quotient */
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;
} }
#endif #endif

View File

@ -36,76 +36,76 @@ static int s_is_power_of_two(mp_digit b, int *p)
/* single digit division (based on routine from MPI) */ /* single digit division (based on routine from MPI) */
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d) int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
{ {
mp_int q; mp_int q;
mp_word w; mp_word w;
mp_digit t; mp_digit t;
int res, ix; int res, ix;
/* cannot divide by zero */ /* cannot divide by zero */
if (b == 0) { if (b == 0) {
return MP_VAL; return MP_VAL;
} }
/* quick outs */ /* quick outs */
if ((b == 1) || (mp_iszero(a) == MP_YES)) { if ((b == 1) || (mp_iszero(a) == MP_YES)) {
if (d != NULL) { if (d != NULL) {
*d = 0; *d = 0;
} }
if (c != NULL) { if (c != NULL) {
return mp_copy(a, c); return mp_copy(a, c);
} }
return MP_OKAY; return MP_OKAY;
} }
/* power of two ? */ /* power of two ? */
if (s_is_power_of_two(b, &ix) == 1) { if (s_is_power_of_two(b, &ix) == 1) {
if (d != NULL) { if (d != NULL) {
*d = a->dp[0] & ((((mp_digit)1)<<ix) - 1); *d = a->dp[0] & ((((mp_digit)1)<<ix) - 1);
} }
if (c != NULL) { if (c != NULL) {
return mp_div_2d(a, ix, c, NULL); return mp_div_2d(a, ix, c, NULL);
} }
return MP_OKAY; return MP_OKAY;
} }
#ifdef BN_MP_DIV_3_C #ifdef BN_MP_DIV_3_C
/* three? */ /* three? */
if (b == 3) { if (b == 3) {
return mp_div_3(a, c, d); return mp_div_3(a, c, d);
} }
#endif #endif
/* no easy answer [c'est la vie]. Just division */ /* no easy answer [c'est la vie]. Just division */
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);
} else { } else {
t = 0; t = 0;
} }
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;
} }
#endif #endif

View File

@ -29,9 +29,9 @@ int mp_dr_is_modulus(mp_int *a)
* but the first digit must be equal to -1 (mod b). * but the first digit must be equal to -1 (mod b).
*/ */
for (ix = 1; ix < a->used; ix++) { for (ix = 1; ix < a->used; ix++) {
if (a->dp[ix] != MP_MASK) { if (a->dp[ix] != MP_MASK) {
return 0; return 0;
} }
} }
return 1; return 1;
} }

View File

@ -31,62 +31,62 @@
*/ */
int mp_dr_reduce(mp_int *x, mp_int *n, mp_digit k) int mp_dr_reduce(mp_int *x, mp_int *n, mp_digit k)
{ {
int err, i, m; int err, i, m;
mp_word r; mp_word r;
mp_digit mu, *tmpx1, *tmpx2; mp_digit mu, *tmpx1, *tmpx2;
/* m = digits in modulus */ /* m = digits in modulus */
m = n->used; m = n->used;
/* ensure that "x" has at least 2m digits */ /* ensure that "x" has at least 2m digits */
if (x->alloc < (m + m)) { if (x->alloc < (m + m)) {
if ((err = mp_grow(x, m + m)) != MP_OKAY) { if ((err = mp_grow(x, m + m)) != MP_OKAY) {
return err; return err;
} }
} }
/* top of loop, this is where the code resumes if /* top of loop, this is where the code resumes if
* another reduction pass is required. * another reduction pass is required.
*/ */
top: top:
/* aliases for digits */ /* aliases for digits */
/* alias for lower half of x */ /* alias for lower half of x */
tmpx1 = x->dp; tmpx1 = x->dp;
/* alias for upper half of x, or x/B**m */ /* alias for upper half of x, or x/B**m */
tmpx2 = x->dp + m; tmpx2 = x->dp + m;
/* set carry to zero */ /* set carry to zero */
mu = 0; mu = 0;
/* compute (x mod B**m) + k * [x/B**m] inline and inplace */ /* compute (x mod B**m) + k * [x/B**m] inline and inplace */
for (i = 0; i < m; i++) { for (i = 0; i < m; i++) {
r = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu; r = (((mp_word)*tmpx2++) * (mp_word)k) + *tmpx1 + mu;
*tmpx1++ = (mp_digit)(r & MP_MASK); *tmpx1++ = (mp_digit)(r & MP_MASK);
mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT)); mu = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
} }
/* set final carry */ /* set final carry */
*tmpx1++ = mu; *tmpx1++ = mu;
/* zero words above m */ /* zero words above m */
for (i = m + 1; i < x->used; i++) { for (i = m + 1; i < x->used; i++) {
*tmpx1++ = 0; *tmpx1++ = 0;
} }
/* clamp, sub and return */ /* clamp, sub and return */
mp_clamp(x); mp_clamp(x);
/* if x >= n then subtract and reduce again /* if x >= n then subtract and reduce again
* Each successive "recursion" makes the input smaller and smaller. * Each successive "recursion" makes the input smaller and smaller.
*/ */
if (mp_cmp_mag(x, n) != MP_LT) { if (mp_cmp_mag(x, n) != MP_LT) {
if ((err = s_mp_sub(x, n, x)) != MP_OKAY) { if ((err = s_mp_sub(x, n, x)) != MP_OKAY) {
return err; return err;
} }
goto top; goto top;
} }
return MP_OKAY; return MP_OKAY;
} }
#endif #endif

View File

@ -20,11 +20,11 @@
*/ */
void mp_exch(mp_int *a, mp_int *b) void mp_exch(mp_int *a, mp_int *b)
{ {
mp_int t; mp_int t;
t = *a; t = *a;
*a = *b; *a = *b;
*b = t; *b = t;
} }
#endif #endif

View File

@ -18,7 +18,7 @@
/* wrapper function for mp_expt_d_ex() */ /* wrapper function for mp_expt_d_ex() */
int mp_expt_d(mp_int *a, mp_digit b, mp_int *c) int mp_expt_d(mp_int *a, mp_digit b, mp_int *c)
{ {
return mp_expt_d_ex(a, b, c, 0); return mp_expt_d_ex(a, b, c, 0);
} }
#endif #endif

View File

@ -23,85 +23,85 @@
*/ */
int mp_exptmod(mp_int *G, mp_int *X, mp_int *P, mp_int *Y) int mp_exptmod(mp_int *G, mp_int *X, mp_int *P, mp_int *Y)
{ {
int dr; int dr;
/* modulus P must be positive */ /* modulus P must be positive */
if (P->sign == MP_NEG) { if (P->sign == MP_NEG) {
return MP_VAL; return MP_VAL;
} }
/* if exponent X is negative we have to recurse */ /* if exponent X is negative we have to recurse */
if (X->sign == MP_NEG) { if (X->sign == MP_NEG) {
#ifdef BN_MP_INVMOD_C #ifdef BN_MP_INVMOD_C
mp_int tmpG, tmpX; mp_int tmpG, tmpX;
int err; int err;
/* first compute 1/G mod P */ /* first compute 1/G mod P */
if ((err = mp_init(&tmpG)) != MP_OKAY) { if ((err = mp_init(&tmpG)) != MP_OKAY) {
return err; return err;
} }
if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) { if ((err = mp_invmod(G, P, &tmpG)) != MP_OKAY) {
mp_clear(&tmpG); mp_clear(&tmpG);
return err; return err;
} }
/* now get |X| */ /* now get |X| */
if ((err = mp_init(&tmpX)) != MP_OKAY) { if ((err = mp_init(&tmpX)) != MP_OKAY) {
mp_clear(&tmpG); mp_clear(&tmpG);
return err; return err;
} }
if ((err = mp_abs(X, &tmpX)) != MP_OKAY) { if ((err = mp_abs(X, &tmpX)) != MP_OKAY) {
mp_clear_multi(&tmpG, &tmpX, NULL); mp_clear_multi(&tmpG, &tmpX, NULL);
return err; return err;
} }
/* and now compute (1/G)**|X| instead of G**X [X < 0] */ /* and now compute (1/G)**|X| instead of G**X [X < 0] */
err = mp_exptmod(&tmpG, &tmpX, P, Y); err = mp_exptmod(&tmpG, &tmpX, P, Y);
mp_clear_multi(&tmpG, &tmpX, NULL); mp_clear_multi(&tmpG, &tmpX, NULL);
return err; return err;
#else #else
/* no invmod */ /* no invmod */
return MP_VAL; return MP_VAL;
#endif #endif
} }
/* modified diminished radix reduction */ /* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C) #if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
if (mp_reduce_is_2k_l(P) == MP_YES) { if (mp_reduce_is_2k_l(P) == MP_YES) {
return s_mp_exptmod(G, X, P, Y, 1); return s_mp_exptmod(G, X, P, Y, 1);
} }
#endif #endif
#ifdef BN_MP_DR_IS_MODULUS_C #ifdef BN_MP_DR_IS_MODULUS_C
/* is it a DR modulus? */ /* is it a DR modulus? */
dr = mp_dr_is_modulus(P); dr = mp_dr_is_modulus(P);
#else #else
/* default to no */ /* default to no */
dr = 0; dr = 0;
#endif #endif
#ifdef BN_MP_REDUCE_IS_2K_C #ifdef BN_MP_REDUCE_IS_2K_C
/* if not, is it a unrestricted DR modulus? */ /* if not, is it a unrestricted DR modulus? */
if (dr == 0) { if (dr == 0) {
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) == MP_YES) || (dr != 0)) { if ((mp_isodd(P) == MP_YES) || (dr != 0)) {
return mp_exptmod_fast(G, X, P, Y, dr); return mp_exptmod_fast(G, X, P, Y, dr);
} else { } else {
#endif #endif
#ifdef BN_S_MP_EXPTMOD_C #ifdef BN_S_MP_EXPTMOD_C
/* otherwise use the generic Barrett reduction technique */ /* otherwise use the generic Barrett reduction technique */
return s_mp_exptmod(G, X, P, Y, 0); return s_mp_exptmod(G, X, P, Y, 0);
#else #else
/* no exptmod for evens */ /* no exptmod for evens */
return MP_VAL; return MP_VAL;
#endif #endif
#ifdef BN_MP_EXPTMOD_FAST_C #ifdef BN_MP_EXPTMOD_FAST_C
} }
#endif #endif
} }

View File

@ -36,9 +36,9 @@ int mp_fread(mp_int *a, int radix, FILE *stream)
for (;;) { for (;;) {
/* find y in the radix map */ /* find y in the radix map */
for (y = 0; y < radix; y++) { for (y = 0; y < radix; y++) {
if (mp_s_rmap[y] == ch) { if (mp_s_rmap[y] == ch) {
break; break;
} }
} }
if (y == radix) { if (y == radix) {
break; break;

View File

@ -36,10 +36,10 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
} }
for (x = 0; x < len; x++) { for (x = 0; x < len; x++) {
if (fputc(buf[x], stream) == EOF) { if (fputc(buf[x], stream) == EOF) {
XFREE(buf); XFREE(buf);
return MP_VAL; return MP_VAL;
} }
} }
XFREE(buf); XFREE(buf);

View File

@ -18,87 +18,87 @@
/* Greatest Common Divisor using the binary method */ /* Greatest Common Divisor using the binary method */
int mp_gcd(mp_int *a, mp_int *b, mp_int *c) int mp_gcd(mp_int *a, mp_int *b, mp_int *c)
{ {
mp_int u, v; mp_int u, v;
int k, u_lsb, v_lsb, res; int k, u_lsb, v_lsb, res;
/* either zero than gcd is the largest */ /* either zero than gcd is the largest */
if (mp_iszero(a) == MP_YES) { if (mp_iszero(a) == MP_YES) {
return mp_abs(b, c); return mp_abs(b, c);
} }
if (mp_iszero(b) == MP_YES) { if (mp_iszero(b) == MP_YES) {
return mp_abs(a, c); return mp_abs(a, c);
} }
/* get copies of a and b we can modify */ /* get copies of a and b we can modify */
if ((res = mp_init_copy(&u, a)) != MP_OKAY) { if ((res = mp_init_copy(&u, a)) != MP_OKAY) {
return res; return res;
} }
if ((res = mp_init_copy(&v, b)) != MP_OKAY) { if ((res = mp_init_copy(&v, b)) != MP_OKAY) {
goto LBL_U; goto LBL_U;
} }
/* must be positive for the remainder of the algorithm */ /* must be positive for the remainder of the algorithm */
u.sign = v.sign = MP_ZPOS; u.sign = v.sign = MP_ZPOS;
/* B1. Find the common power of two for u and v */ /* B1. Find the common power of two for u and v */
u_lsb = mp_cnt_lsb(&u); u_lsb = mp_cnt_lsb(&u);
v_lsb = mp_cnt_lsb(&v); v_lsb = mp_cnt_lsb(&v);
k = MIN(u_lsb, v_lsb); k = MIN(u_lsb, v_lsb);
if (k > 0) { if (k > 0) {
/* divide the power of two out */ /* divide the power of two out */
if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) { if ((res = mp_div_2d(&u, k, &u, NULL)) != MP_OKAY) {
goto LBL_V; goto LBL_V;
} }
if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) { if ((res = mp_div_2d(&v, k, &v, NULL)) != MP_OKAY) {
goto LBL_V; goto LBL_V;
} }
} }
/* divide any remaining factors of two out */ /* divide any remaining factors of two out */
if (u_lsb != k) { if (u_lsb != k) {
if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) { if ((res = mp_div_2d(&u, u_lsb - k, &u, NULL)) != MP_OKAY) {
goto LBL_V; goto LBL_V;
} }
} }
if (v_lsb != k) { if (v_lsb != k) {
if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) { if ((res = mp_div_2d(&v, v_lsb - k, &v, NULL)) != MP_OKAY) {
goto LBL_V; goto LBL_V;
} }
} }
while (mp_iszero(&v) == MP_NO) { while (mp_iszero(&v) == MP_NO) {
/* make sure v is the largest */ /* make sure v is the largest */
if (mp_cmp_mag(&u, &v) == MP_GT) { if (mp_cmp_mag(&u, &v) == MP_GT) {
/* swap u and v to make sure v is >= u */ /* 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) {
goto LBL_V; goto LBL_V;
} }
c->sign = MP_ZPOS; c->sign = MP_ZPOS;
res = MP_OKAY; res = MP_OKAY;
LBL_V: LBL_V:
mp_clear(&u); mp_clear(&u);
LBL_U: LBL_U:
mp_clear(&v); mp_clear(&v);
return res; return res;
} }
#endif #endif

View File

@ -18,25 +18,25 @@
/* get the lower 32-bits of an mp_int */ /* get the lower 32-bits of an mp_int */
unsigned long mp_get_int(mp_int *a) unsigned long mp_get_int(mp_int *a)
{ {
int i; int i;
mp_min_u32 res; mp_min_u32 res;
if (a->used == 0) { if (a->used == 0) {
return 0; return 0;
} }
/* get number of digits of the lsb we have to read */ /* get number of digits of the lsb we have to read */
i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */ /* get most significant digit of result */
res = DIGIT(a, i); res = DIGIT(a, i);
while (--i >= 0) { while (--i >= 0) {
res = (res << DIGIT_BIT) | DIGIT(a, i); res = (res << DIGIT_BIT) | DIGIT(a, i);
} }
/* force result to 32-bits always so it is consistent on non 32-bit platforms */ /* force result to 32-bits always so it is consistent on non 32-bit platforms */
return res & 0xFFFFFFFFUL; return res & 0xFFFFFFFFUL;
} }
#endif #endif

View File

@ -18,24 +18,24 @@
/* get the lower unsigned long of an mp_int, platform dependent */ /* get the lower unsigned long of an mp_int, platform dependent */
unsigned long mp_get_long(mp_int *a) unsigned long mp_get_long(mp_int *a)
{ {
int i; int i;
unsigned long res; unsigned long res;
if (a->used == 0) { if (a->used == 0) {
return 0; return 0;
} }
/* get number of digits of the lsb we have to read */ /* get number of digits of the lsb we have to read */
i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; i = MIN(a->used, (int)(((sizeof(unsigned long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */ /* get most significant digit of result */
res = DIGIT(a, i); res = DIGIT(a, i);
#if (ULONG_MAX != 0xffffffffuL) || (DIGIT_BIT < 32) #if (ULONG_MAX != 0xffffffffuL) || (DIGIT_BIT < 32)
while (--i >= 0) { while (--i >= 0) {
res = (res << DIGIT_BIT) | DIGIT(a, i); res = (res << DIGIT_BIT) | DIGIT(a, i);
} }
#endif #endif
return res; return res;
} }
#endif #endif

View File

@ -18,24 +18,24 @@
/* get the lower unsigned long long of an mp_int, platform dependent */ /* get the lower unsigned long long of an mp_int, platform dependent */
unsigned long long mp_get_long_long(mp_int *a) unsigned long long mp_get_long_long(mp_int *a)
{ {
int i; int i;
unsigned long long res; unsigned long long res;
if (a->used == 0) { if (a->used == 0) {
return 0; return 0;
} }
/* get number of digits of the lsb we have to read */ /* get number of digits of the lsb we have to read */
i = MIN(a->used, (int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1; i = MIN(a->used, (int)(((sizeof(unsigned long long) * CHAR_BIT) + DIGIT_BIT - 1) / DIGIT_BIT)) - 1;
/* get most significant digit of result */ /* get most significant digit of result */
res = DIGIT(a, i); res = DIGIT(a, i);
#if DIGIT_BIT < 64 #if DIGIT_BIT < 64
while (--i >= 0) { while (--i >= 0) {
res = (res << DIGIT_BIT) | DIGIT(a, i); res = (res << DIGIT_BIT) | DIGIT(a, i);
} }
#endif #endif
return res; return res;
} }
#endif #endif

View File

@ -18,37 +18,37 @@
/* grow as required */ /* grow as required */
int mp_grow(mp_int *a, int size) int mp_grow(mp_int *a, int size)
{ {
int i; int i;
mp_digit *tmp; mp_digit *tmp;
/* if the alloc size is smaller alloc more ram */ /* if the alloc size is smaller alloc more ram */
if (a->alloc < size) { if (a->alloc < size) {
/* ensure there are always at least MP_PREC digits extra on top */ /* ensure there are always at least MP_PREC digits extra on top */
size += (MP_PREC * 2) - (size % MP_PREC); size += (MP_PREC * 2) - (size % MP_PREC);
/* reallocate the array a->dp /* reallocate the array a->dp
* *
* We store the return in a temporary variable * We store the return in a temporary variable
* in case the operation failed we don't want * in case the operation failed we don't want
* to overwrite the dp member of a. * to overwrite the dp member of a.
*/ */
tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof (mp_digit) * size); tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof(mp_digit) * size);
if (tmp == NULL) { if (tmp == NULL) {
/* reallocation failed but "a" is still valid [can be freed] */ /* reallocation failed but "a" is still valid [can be freed] */
return MP_MEM; return MP_MEM;
} }
/* reallocation succeeded so set a->dp */ /* reallocation succeeded so set a->dp */
a->dp = tmp; a->dp = tmp;
/* zero excess digits */ /* zero excess digits */
i = a->alloc; i = a->alloc;
a->alloc = size; a->alloc = size;
for (; i < a->alloc; i++) { for (; i < a->alloc; i++) {
a->dp[i] = 0; a->dp[i] = 0;
} }
} }
return MP_OKAY; return MP_OKAY;
} }
#endif #endif