Merge branch '20151112_lint' into develop

This closes #40
This commit is contained in:
Steffen Jaeckel 2015-12-02 22:51:48 +01:00
commit edef6ca191
51 changed files with 133 additions and 133 deletions

View File

@ -32,7 +32,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
olduse = x->used; olduse = x->used;
/* grow a as required */ /* grow a as required */
if (x->alloc < n->used + 1) { if (x->alloc < (n->used + 1)) {
if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) { if ((res = mp_grow (x, n->used + 1)) != MP_OKAY) {
return res; return res;
} }
@ -57,7 +57,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
} }
/* zero the high words of W[a->used..m->used*2] */ /* zero the high words of W[a->used..m->used*2] */
for (; ix < n->used * 2 + 1; ix++) { for (; ix < ((n->used * 2) + 1); ix++) {
*_W++ = 0; *_W++ = 0;
} }
} }
@ -126,7 +126,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
/* alias for next word, where the carry goes */ /* alias for next word, where the carry goes */
_W = W + ++ix; _W = W + ++ix;
for (; ix <= n->used * 2 + 1; ix++) { for (; ix <= ((n->used * 2) + 1); ix++) {
*_W++ += *_W1++ >> ((mp_word) DIGIT_BIT); *_W++ += *_W1++ >> ((mp_word) DIGIT_BIT);
} }
@ -143,7 +143,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
/* alias for shifted double precision result */ /* alias for shifted double precision result */
_W = W + n->used; _W = W + n->used;
for (ix = 0; ix < n->used + 1; ix++) { for (ix = 0; ix < (n->used + 1); ix++) {
*tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK)); *tmpx++ = (mp_digit)(*_W++ & ((mp_word) MP_MASK));
} }

View File

@ -87,7 +87,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
register mp_digit *tmpc; register 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];
} }

View File

@ -66,7 +66,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
* 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
*/ */
iy = MIN(iy, (ty-tx+1)>>1); iy = MIN(iy, ((ty-tx)+1)>>1);
/* execute loop */ /* execute loop */
for (iz = 0; iz < iy; iz++) { for (iz = 0; iz < iy; iz++) {

View File

@ -29,12 +29,12 @@ mp_2expt (mp_int * a, int b)
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);

View File

@ -23,14 +23,14 @@ mp_add_d (mp_int * a, mp_digit b, mp_int * c)
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;

View File

@ -28,7 +28,7 @@ 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);
} }

View File

@ -31,7 +31,7 @@ int mp_cnt_lsb(mp_int *a)
} }
/* scan lower digits until non-zero */ /* scan lower digits until non-zero */
for (x = 0; x < a->used && a->dp[x] == 0; x++) {} for (x = 0; (x < a->used) && (a->dp[x] == 0); x++) {}
q = a->dp[x]; q = a->dp[x];
x *= DIGIT_BIT; x *= DIGIT_BIT;

View File

@ -71,7 +71,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
/* now q == quotient and ta == remainder */ /* now q == quotient and ta == remainder */
n = a->sign; n = a->sign;
n2 = (a->sign == b->sign ? MP_ZPOS : MP_NEG); n2 = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
if (c != NULL) { if (c != NULL) {
mp_exch(c, &q); mp_exch(c, &q);
c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2; c->sign = (mp_iszero(c) == MP_YES) ? MP_ZPOS : n2;
@ -190,7 +190,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
/* step 3.1 if xi == yt then set q{i-t-1} to b-1, /* step 3.1 if xi == yt then set q{i-t-1} to b-1,
* otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */ * otherwise set q{i-t-1} to (xi*b + x{i-1})/yt */
if (x.dp[i] == y.dp[t]) { if (x.dp[i] == y.dp[t]) {
q.dp[i - t - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1); q.dp[(i - t) - 1] = ((((mp_digit)1) << DIGIT_BIT) - 1);
} else { } else {
mp_word tmp; mp_word tmp;
tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT); tmp = ((mp_word) x.dp[i]) << ((mp_word) DIGIT_BIT);
@ -199,7 +199,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
if (tmp > (mp_word) MP_MASK) { if (tmp > (mp_word) MP_MASK) {
tmp = MP_MASK; tmp = MP_MASK;
} }
q.dp[i - t - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK)); q.dp[(i - t) - 1] = (mp_digit) (tmp & (mp_word) (MP_MASK));
} }
/* while (q{i-t-1} * (yt * b + y{t-1})) > /* while (q{i-t-1} * (yt * b + y{t-1})) >
@ -207,32 +207,32 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
do q{i-t-1} -= 1; do q{i-t-1} -= 1;
*/ */
q.dp[i - t - 1] = (q.dp[i - t - 1] + 1) & MP_MASK; q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] + 1) & MP_MASK;
do { do {
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1) & MP_MASK; q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK;
/* find left hand */ /* find left hand */
mp_zero (&t1); mp_zero (&t1);
t1.dp[0] = (t - 1 < 0) ? 0 : y.dp[t - 1]; t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1];
t1.dp[1] = y.dp[t]; t1.dp[1] = y.dp[t];
t1.used = 2; t1.used = 2;
if ((res = mp_mul_d (&t1, q.dp[i - t - 1], &t1)) != MP_OKAY) { if ((res = mp_mul_d (&t1, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }
/* find right hand */ /* find right hand */
t2.dp[0] = (i - 2 < 0) ? 0 : x.dp[i - 2]; t2.dp[0] = ((i - 2) < 0) ? 0 : x.dp[i - 2];
t2.dp[1] = (i - 1 < 0) ? 0 : x.dp[i - 1]; t2.dp[1] = ((i - 1) < 0) ? 0 : x.dp[i - 1];
t2.dp[2] = x.dp[i]; t2.dp[2] = x.dp[i];
t2.used = 3; t2.used = 3;
} while (mp_cmp_mag(&t1, &t2) == MP_GT); } while (mp_cmp_mag(&t1, &t2) == MP_GT);
/* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */ /* step 3.3 x = x - q{i-t-1} * y * b**{i-t-1} */
if ((res = mp_mul_d (&y, q.dp[i - t - 1], &t1)) != MP_OKAY) { if ((res = mp_mul_d (&y, q.dp[(i - t) - 1], &t1)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }
@ -245,14 +245,14 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
if ((res = mp_copy (&y, &t1)) != MP_OKAY) { if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }
if ((res = mp_lshd (&t1, i - t - 1)) != MP_OKAY) { if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }
if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) { if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }
q.dp[i - t - 1] = (q.dp[i - t - 1] - 1UL) & MP_MASK; q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1UL) & MP_MASK;
} }
} }
@ -261,7 +261,7 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
*/ */
/* get sign before writing to c */ /* get sign before writing to c */
x.sign = x.used == 0 ? MP_ZPOS : a->sign; x.sign = (x.used == 0) ? MP_ZPOS : a->sign;
if (c != NULL) { if (c != NULL) {
mp_clamp (&q); mp_clamp (&q);

View File

@ -47,7 +47,7 @@ int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
} }
/* 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;
} }

View File

@ -40,7 +40,7 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
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;
} }
@ -62,7 +62,7 @@ top:
/* 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));
} }

View File

@ -37,7 +37,7 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
} lint; } lint;
lint.i = 0x01020304; lint.i = 0x01020304;
endian = (lint.c[0] == 4 ? -1 : 1); endian = (lint.c[0] == 4) ? -1 : 1;
} }
odd_nails = (nails % 8); odd_nails = (nails % 8);
@ -48,14 +48,14 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
nail_bytes = nails / 8; nail_bytes = nails / 8;
bits = mp_count_bits(&t); bits = mp_count_bits(&t);
count = bits / (size * 8 - nails) + ((bits % (size * 8 - nails) != 0) ? 1 : 0); count = (bits / ((size * 8) - nails)) + (((bits % ((size * 8) - nails)) != 0) ? 1 : 0);
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) { for (j = 0; j < size; ++j) {
unsigned char* byte = ( unsigned char* byte = (
(unsigned char*)rop + (unsigned char*)rop +
(order == -1 ? i : count - 1 - i) * size + (((order == -1) ? i : ((count - 1) - i)) * size) +
(endian == -1 ? j : size - 1 - j) ((endian == -1) ? j : ((size - 1) - j))
); );
if (j >= (size - nail_bytes)) { if (j >= (size - nail_bytes)) {
@ -63,9 +63,9 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
continue; continue;
} }
*byte = (unsigned char)(j == size - nail_bytes - 1 ? (t.dp[0] & odd_nail_mask) : t.dp[0] & 0xFF); *byte = (unsigned char)((j == ((size - nail_bytes) - 1)) ? (t.dp[0] & odd_nail_mask) : (t.dp[0] & 0xFF));
if ((result = mp_div_2d(&t, (j == size - nail_bytes - 1 ? 8 - odd_nails : 8), &t, NULL)) != MP_OKAY) { if ((result = mp_div_2d(&t, ((j == ((size - nail_bytes) - 1)) ? (8 - odd_nails) : 8), &t, NULL)) != MP_OKAY) {
mp_clear(&t); mp_clear(&t);
return result; return result;
} }

View File

@ -89,7 +89,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
/* 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

View File

@ -96,8 +96,8 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
/* automatically pick the comba one if available (saves quite a few calls/ifs) */ /* automatically pick the comba one if available (saves quite a few calls/ifs) */
#ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C #ifdef BN_FAST_MP_MONTGOMERY_REDUCE_C
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
@ -219,12 +219,12 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
* in the exponent. Technically this opt is not required but it * in the exponent. Technically this opt is not required but it
* does lower the # of trivial squaring/reductions used * does lower the # of trivial squaring/reductions used
*/ */
if (mode == 0 && y == 0) { if ((mode == 0) && (y == 0)) {
continue; continue;
} }
/* if the bit is zero and mode == 1 then we square */ /* if the bit is zero and mode == 1 then we square */
if (mode == 1 && y == 0) { if ((mode == 1) && (y == 0)) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
@ -266,7 +266,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
} }
/* if bits remain then square/multiply */ /* if bits remain then square/multiply */
if (mode == 2 && bitcpy > 0) { if ((mode == 2) && (bitcpy > 0)) {
/* square then multiply if the bit is set */ /* square then multiply if the bit is set */
for (x = 0; x < bitcpy; x++) { for (x = 0; x < bitcpy; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {

View File

@ -26,7 +26,7 @@ unsigned long mp_get_int(mp_int * a)
} }
/* 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);

View File

@ -26,12 +26,12 @@ unsigned long mp_get_long(mp_int * a)
} }
/* 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);
} }

View File

@ -26,7 +26,7 @@ unsigned long long mp_get_long_long (mp_int * a)
} }
/* 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);

View File

@ -33,7 +33,7 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
} lint; } lint;
lint.i = 0x01020304; lint.i = 0x01020304;
endian = (lint.c[0] == 4 ? -1 : 1); endian = (lint.c[0] == 4) ? -1 : 1;
} }
odd_nails = (nails % 8); odd_nails = (nails % 8);
@ -44,19 +44,19 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
nail_bytes = nails / 8; nail_bytes = nails / 8;
for (i = 0; i < count; ++i) { for (i = 0; i < count; ++i) {
for (j = 0; j < size - nail_bytes; ++j) { for (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *( unsigned char byte = *(
(unsigned char*)op + (unsigned char*)op +
(order == 1 ? i : count - 1 - i) * size + (((order == 1) ? i : ((count - 1) - i)) * size) +
(endian == 1 ? j + nail_bytes : size - 1 - j - nail_bytes) ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))
); );
if ( if (
(result = mp_mul_2d(rop, (j == 0 ? 8 - odd_nails : 8), rop)) != MP_OKAY) { (result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
return result; return result;
} }
rop->dp[0] |= (j == 0 ? (byte & odd_nail_mask) : byte); rop->dp[0] |= (j == 0) ? (byte & odd_nail_mask) : byte;
rop->used += 1; rop->used += 1;
} }
} }

View File

@ -19,7 +19,7 @@
int mp_invmod (mp_int * a, mp_int * b, mp_int * c) int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
{ {
/* b cannot be negative */ /* b cannot be negative */
if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) { if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -22,7 +22,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
int res; int res;
/* b cannot be negative */ /* b cannot be negative */
if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) { if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
return MP_VAL; return MP_VAL;
} }
@ -41,7 +41,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
} }
/* 2. [modified] if x,y are both even then return an error! */ /* 2. [modified] if x,y are both even then return an error! */
if (mp_iseven (&x) == MP_YES && mp_iseven (&y) == MP_YES) { if ((mp_iseven (&x) == MP_YES) && (mp_iseven (&y) == MP_YES)) {
res = MP_VAL; res = MP_VAL;
goto LBL_ERR; goto LBL_ERR;
} }
@ -64,7 +64,7 @@ top:
goto LBL_ERR; goto LBL_ERR;
} }
/* 4.2 if A or B is odd then */ /* 4.2 if A or B is odd then */
if (mp_isodd (&A) == MP_YES || mp_isodd (&B) == MP_YES) { if ((mp_isodd (&A) == MP_YES) || (mp_isodd (&B) == MP_YES)) {
/* A = (A+y)/2, B = (B-x)/2 */ /* A = (A+y)/2, B = (B-x)/2 */
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) { if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
goto LBL_ERR; goto LBL_ERR;
@ -89,7 +89,7 @@ top:
goto LBL_ERR; goto LBL_ERR;
} }
/* 5.2 if C or D is odd then */ /* 5.2 if C or D is odd then */
if (mp_isodd (&C) == MP_YES || mp_isodd (&D) == MP_YES) { if ((mp_isodd (&C) == MP_YES) || (mp_isodd (&D) == MP_YES)) {
/* C = (C+y)/2, D = (D-x)/2 */ /* C = (C+y)/2, D = (D-x)/2 */
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) { if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
goto LBL_ERR; goto LBL_ERR;

View File

@ -73,9 +73,9 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
/* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */ /* else set s=1 if p = 1/7 (mod 8) or s=-1 if p = 3/5 (mod 8) */
residue = n->dp[0] & 7; residue = n->dp[0] & 7;
if (residue == 1 || residue == 7) { if ((residue == 1) || (residue == 7)) {
s = 1; s = 1;
} else if (residue == 3 || residue == 5) { } else if ((residue == 3) || (residue == 5)) {
s = -1; s = -1;
} }
} }

View File

@ -26,7 +26,7 @@ int mp_lshd (mp_int * a, int b)
} }
/* grow to fit the new digits */ /* grow to fit the new digits */
if (a->alloc < a->used + b) { if (a->alloc < (a->used + b)) {
if ((res = mp_grow (a, a->used + b)) != MP_OKAY) { if ((res = mp_grow (a, a->used + b)) != MP_OKAY) {
return res; return res;
} }
@ -42,7 +42,7 @@ int mp_lshd (mp_int * a, int b)
top = a->dp + a->used - 1; top = a->dp + a->used - 1;
/* base */ /* base */
bottom = a->dp + a->used - 1 - b; bottom = (a->dp + a->used - 1) - b;
/* much like mp_rshd this is implemented using a sliding window /* much like mp_rshd this is implemented using a sliding window
* except the window goes the otherway around. Copying from * except the window goes the otherway around. Copying from

View File

@ -31,7 +31,7 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
return res; return res;
} }
if (mp_iszero(&t) != MP_NO || t.sign == b->sign) { if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
res = MP_OKAY; res = MP_OKAY;
mp_exch (&t, c); mp_exch (&t, c);
} else { } else {

View File

@ -39,7 +39,7 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
} }
/* zero digits above the last digit of the modulus */ /* zero digits above the last digit of the modulus */
for (x = (b / DIGIT_BIT) + ((b % DIGIT_BIT) == 0 ? 0 : 1); x < c->used; x++) { for (x = (b / DIGIT_BIT) + (((b % DIGIT_BIT) == 0) ? 0 : 1); x < c->used; x++) {
c->dp[x] = 0; c->dp[x] = 0;
} }
/* clear the digit that is not completely outside/inside the modulus */ /* clear the digit that is not completely outside/inside the modulus */

View File

@ -29,7 +29,7 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
bits = mp_count_bits (b) % DIGIT_BIT; bits = mp_count_bits (b) % DIGIT_BIT;
if (b->used > 1) { if (b->used > 1) {
if ((res = mp_2expt (a, (b->used - 1) * DIGIT_BIT + bits - 1)) != MP_OKAY) { if ((res = mp_2expt (a, ((b->used - 1) * DIGIT_BIT) + bits - 1)) != MP_OKAY) {
return res; return res;
} }
} else { } else {

View File

@ -28,10 +28,10 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* than the available columns [255 per default] since carries * than the available columns [255 per default] since carries
* are fixed up in the inner loop. * are fixed up in the inner loop.
*/ */
digs = n->used * 2 + 1; digs = (n->used * 2) + 1;
if ((digs < MP_WARRAY) && if ((digs < MP_WARRAY) &&
n->used < (n->used <
(1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
return fast_mp_montgomery_reduce (x, n, rho); return fast_mp_montgomery_reduce (x, n, rho);
} }
@ -52,7 +52,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* following inner loop to reduce the * following inner loop to reduce the
* input one digit at a time * input one digit at a time
*/ */
mu = (mp_digit) (((mp_word)x->dp[ix]) * ((mp_word)rho) & MP_MASK); mu = (mp_digit) (((mp_word)x->dp[ix] * (mp_word)rho) & MP_MASK);
/* a = a + mu * m * b**i */ /* a = a + mu * m * b**i */
{ {
@ -72,8 +72,8 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
/* Multiply and add in place */ /* Multiply and add in place */
for (iy = 0; iy < n->used; iy++) { for (iy = 0; iy < n->used; iy++) {
/* compute product and sum */ /* compute product and sum */
r = ((mp_word)mu) * ((mp_word)*tmpn++) + r = ((mp_word)mu * (mp_word)*tmpn++) +
((mp_word) u) + ((mp_word) * tmpx); (mp_word) u + (mp_word) *tmpx;
/* get carry */ /* get carry */
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT)); u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));

View File

@ -36,15 +36,15 @@ mp_montgomery_setup (mp_int * n, mp_digit * rho)
} }
x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */ x = (((b + 2) & 4) << 1) + b; /* here x*a==1 mod 2**4 */
x *= 2 - b * x; /* here x*a==1 mod 2**8 */ x *= 2 - (b * x); /* here x*a==1 mod 2**8 */
#if !defined(MP_8BIT) #if !defined(MP_8BIT)
x *= 2 - b * x; /* here x*a==1 mod 2**16 */ x *= 2 - (b * x); /* here x*a==1 mod 2**16 */
#endif #endif
#if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT)) #if defined(MP_64BIT) || !(defined(MP_8BIT) || defined(MP_16BIT))
x *= 2 - b * x; /* here x*a==1 mod 2**32 */ x *= 2 - (b * x); /* here x*a==1 mod 2**32 */
#endif #endif
#ifdef MP_64BIT #ifdef MP_64BIT
x *= 2 - b * x; /* here x*a==1 mod 2**64 */ x *= 2 - (b * x); /* here x*a==1 mod 2**64 */
#endif #endif
/* rho = -1/m mod b */ /* rho = -1/m mod b */

View File

@ -44,8 +44,8 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
#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

View File

@ -21,7 +21,7 @@ int mp_mul_2(mp_int * a, mp_int * b)
int x, res, oldused; int x, res, oldused;
/* grow to accomodate result */ /* grow to accomodate result */
if (b->alloc < a->used + 1) { if (b->alloc < (a->used + 1)) {
if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) { if ((res = mp_grow (b, a->used + 1)) != MP_OKAY) {
return res; return res;
} }

View File

@ -28,8 +28,8 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
} }
} }
if (c->alloc < (int)(c->used + b/DIGIT_BIT + 1)) { if (c->alloc < (int)(c->used + (b / DIGIT_BIT) + 1)) {
if ((res = mp_grow (c, c->used + b / DIGIT_BIT + 1)) != MP_OKAY) { if ((res = mp_grow (c, c->used + (b / DIGIT_BIT) + 1)) != MP_OKAY) {
return res; return res;
} }
} }

View File

@ -24,7 +24,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
int ix, res, olduse; int ix, res, olduse;
/* make sure c is big enough to hold a*b */ /* make sure c is big enough to hold a*b */
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;
} }
@ -48,7 +48,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
/* compute columns */ /* compute columns */
for (ix = 0; ix < a->used; ix++) { for (ix = 0; ix < a->used; ix++) {
/* compute product and carry sum for this term */ /* compute product and carry sum for this term */
r = ((mp_word) u) + ((mp_word)*tmpa++) * ((mp_word)b); r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
/* mask off higher bits to get a single digit */ /* mask off higher bits to get a single digit */
*tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK)); *tmpc++ = (mp_digit) (r & ((mp_word) MP_MASK));

View File

@ -31,7 +31,7 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
int res, neg; int res, neg;
/* input must be positive if b is even */ /* input must be positive if b is even */
if ((b & 1) == 0 && a->sign == MP_NEG) { if (((b & 1) == 0) && (a->sign == MP_NEG)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -31,7 +31,7 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
*result = MP_NO; *result = MP_NO;
/* valid value of t? */ /* valid value of t? */
if (t <= 0 || t > PRIME_SIZE) { if ((t <= 0) || (t > PRIME_SIZE)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -67,10 +67,10 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
} }
/* if y != 1 and y != n1 do */ /* if y != 1 and y != n1 do */
if (mp_cmp_d (&y, 1) != MP_EQ && mp_cmp (&y, &n1) != MP_EQ) { if ((mp_cmp_d (&y, 1) != MP_EQ) && (mp_cmp (&y, &n1) != MP_EQ)) {
j = 1; j = 1;
/* while j <= s-1 and y != n1 */ /* while j <= s-1 and y != n1 */
while ((j <= (s - 1)) && mp_cmp (&y, &n1) != MP_EQ) { while ((j <= (s - 1)) && (mp_cmp (&y, &n1) != MP_EQ)) {
if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) { if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
goto LBL_Y; goto LBL_Y;
} }

View File

@ -27,7 +27,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
mp_int b; mp_int b;
/* ensure t is valid */ /* ensure t is valid */
if (t <= 0 || t > PRIME_SIZE) { if ((t <= 0) || (t > PRIME_SIZE)) {
return MP_VAL; return MP_VAL;
} }
@ -129,7 +129,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
y = 1; y = 1;
} }
} }
} while (y == 1 && step < ((((mp_digit)1)<<DIGIT_BIT) - kstep)); } while ((y == 1) && (step < ((((mp_digit)1) << DIGIT_BIT) - kstep)));
/* add the step */ /* add the step */
if ((err = mp_add_d(a, step, a)) != MP_OKAY) { if ((err = mp_add_d(a, step, a)) != MP_OKAY) {
@ -137,7 +137,7 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
} }
/* if didn't pass sieve and step == MAX then skip test */ /* if didn't pass sieve and step == MAX then skip test */
if (y == 1 && step >= ((((mp_digit)1)<<DIGIT_BIT) - kstep)) { if ((y == 1) && (step >= ((((mp_digit)1) << DIGIT_BIT) - kstep))) {
continue; continue;
} }

View File

@ -36,7 +36,7 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
int res, err, bsize, maskOR_msb_offset; int res, err, bsize, maskOR_msb_offset;
/* sanity check the input */ /* sanity check the input */
if (size <= 1 || t <= 0) { if ((size <= 1) || (t <= 0)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -25,7 +25,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
*size = 0; *size = 0;
/* make sure the radix is in range */ /* make sure the radix is in range */
if (radix < 2 || radix > 64) { if ((radix < 2) || (radix > 64)) {
return MP_VAL; return MP_VAL;
} }
@ -36,7 +36,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
/* special case for binary */ /* special case for binary */
if (radix == 2) { if (radix == 2) {
*size = mp_count_bits (a) + (a->sign == MP_NEG ? 1 : 0) + 1; *size = mp_count_bits (a) + ((a->sign == MP_NEG) ? 1 : 0) + 1;
return MP_OKAY; return MP_OKAY;
} }

View File

@ -25,7 +25,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
mp_zero(a); mp_zero(a);
/* make sure the radix is ok */ /* make sure the radix is ok */
if (radix < 2 || radix > 64) { if ((radix < 2) || (radix > 64)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -36,9 +36,9 @@ 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
#endif #endif

View File

@ -56,7 +56,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
/* Q = prime - 1 */ /* Q = prime - 1 */
mp_zero(&S); mp_zero(&S);
/* S = 0 */ /* S = 0 */
while (mp_iseven(&Q)) { while (mp_iseven(&Q) != MP_NO) {
if ((res = mp_div_2(&Q, &Q)) != MP_OKAY) goto cleanup; if ((res = mp_div_2(&Q, &Q)) != MP_OKAY) goto cleanup;
/* Q = Q / 2 */ /* Q = Q / 2 */
if ((res = mp_add_d(&S, 1, &S)) != MP_OKAY) goto cleanup; if ((res = mp_add_d(&S, 1, &S)) != MP_OKAY) goto cleanup;
@ -64,7 +64,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
} }
/* find a Z such that the Legendre symbol (Z|prime) == -1 */ /* find a Z such that the Legendre symbol (Z|prime) == -1 */
mp_set_int(&Z, 2); if ((res = mp_set_int(&Z, 2)) != MP_OKAY) goto cleanup;
/* Z = 2 */ /* Z = 2 */
while(1) { while(1) {
if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup; if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
@ -96,7 +96,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
i++; i++;
} }
if (i == 0) { if (i == 0) {
mp_copy(&R, ret); if ((res = mp_copy(&R, ret)) != MP_OKAY) goto cleanup;
res = MP_OKAY; res = MP_OKAY;
goto cleanup; goto cleanup;
} }

View File

@ -23,7 +23,7 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
int res, ix, oldused; int res, ix, oldused;
/* 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;
} }
@ -49,7 +49,7 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
tmpc = c->dp; tmpc = c->dp;
/* if a <= b simply fix the single digit */ /* if a <= b simply fix the single digit */
if ((a->used == 1 && a->dp[0] <= b) || a->used == 0) { if (((a->used == 1) && (a->dp[0] <= b)) || (a->used == 0)) {
if (a->used == 1) { if (a->used == 1) {
*tmpc++ = b - *tmpa; *tmpc++ = b - *tmpa;
} else { } else {
@ -67,13 +67,13 @@ mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
/* subtract first digit */ /* subtract first digit */
*tmpc = *tmpa++ - b; *tmpc = *tmpa++ - b;
mu = *tmpc >> (sizeof(mp_digit) * CHAR_BIT - 1); mu = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1);
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
/* handle rest of the digits */ /* 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 >> (sizeof(mp_digit) * CHAR_BIT - 1); mu = *tmpc >> ((sizeof(mp_digit) * CHAR_BIT) - 1);
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
} }
} }

View File

@ -24,7 +24,7 @@ int mp_toradix (mp_int * a, char *str, int radix)
char *_s = str; char *_s = str;
/* check range of the radix */ /* check range of the radix */
if (radix < 2 || radix > 64) { if ((radix < 2) || (radix > 64)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -27,7 +27,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
char *_s = str; char *_s = str;
/* check range of the maxlen, radix */ /* check range of the maxlen, radix */
if (maxlen < 2 || radix < 2 || radix > 64) { if ((maxlen < 2) || (radix < 2) || (radix > 64)) {
return MP_VAL; return MP_VAL;
} }

View File

@ -19,7 +19,7 @@
int mp_unsigned_bin_size (mp_int * a) int mp_unsigned_bin_size (mp_int * a)
{ {
int size = mp_count_bits (a); int size = mp_count_bits (a);
return (size / 8 + ((size & 7) != 0 ? 1 : 0)); return (size / 8) + (((size & 7) != 0) ? 1 : 0);
} }
#endif #endif

View File

@ -36,7 +36,7 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
} }
/* init result */ /* init result */
if (c->alloc < max + 1) { if (c->alloc < (max + 1)) {
if ((res = mp_grow (c, max + 1)) != MP_OKAY) { if ((res = mp_grow (c, max + 1)) != MP_OKAY) {
return res; return res;
} }

View File

@ -164,12 +164,12 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
* in the exponent. Technically this opt is not required but it * in the exponent. Technically this opt is not required but it
* does lower the # of trivial squaring/reductions used * does lower the # of trivial squaring/reductions used
*/ */
if (mode == 0 && y == 0) { if ((mode == 0) && (y == 0)) {
continue; continue;
} }
/* if the bit is zero and mode == 1 then we square */ /* if the bit is zero and mode == 1 then we square */
if (mode == 1 && y == 0) { if ((mode == 1) && (y == 0)) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
goto LBL_RES; goto LBL_RES;
} }
@ -211,7 +211,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
} }
/* if bits remain then square/multiply */ /* if bits remain then square/multiply */
if (mode == 2 && bitcpy > 0) { if ((mode == 2) && (bitcpy > 0)) {
/* square then multiply if the bit is set */ /* square then multiply if the bit is set */
for (x = 0; x < bitcpy; x++) { for (x = 0; x < bitcpy; x++) {
if ((err = mp_sqr (&res, &res)) != MP_OKAY) { if ((err = mp_sqr (&res, &res)) != MP_OKAY) {

View File

@ -29,8 +29,8 @@ 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);
} }
@ -61,9 +61,9 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
/* compute the columns of the output and propagate the carry */ /* compute the columns of the output and propagate the carry */
for (iy = 0; iy < pb; iy++) { for (iy = 0; iy < pb; iy++) {
/* compute the column as a mp_word */ /* compute the column as a mp_word */
r = ((mp_word)*tmpt) + r = (mp_word)*tmpt +
((mp_word)tmpx) * ((mp_word)*tmpy++) + ((mp_word)tmpx * (mp_word)*tmpy++) +
((mp_word) u); (mp_word)u;
/* the new column is the lower part of the result */ /* the new column is the lower part of the result */
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
@ -72,7 +72,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT)); u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
} }
/* set carry if it is placed below digs */ /* set carry if it is placed below digs */
if (ix + iy < digs) { if ((ix + iy) < digs) {
*tmpt = u; *tmpt = u;
} }
} }

View File

@ -30,7 +30,7 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
/* can we use the fast multiplier? */ /* can we use the fast multiplier? */
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C #ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
if (((a->used + b->used + 1) < MP_WARRAY) if (((a->used + b->used + 1) < MP_WARRAY)
&& MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof (mp_word)) - (2 * DIGIT_BIT)))) { && (MIN (a->used, b->used) < (1 << ((CHAR_BIT * sizeof(mp_word)) - (2 * DIGIT_BIT))))) {
return fast_s_mp_mul_high_digs (a, b, c, digs); return fast_s_mp_mul_high_digs (a, b, c, digs);
} }
#endif #endif
@ -57,9 +57,9 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
for (iy = digs - ix; iy < pb; iy++) { for (iy = digs - ix; iy < pb; iy++) {
/* calculate the double precision result */ /* calculate the double precision result */
r = ((mp_word)*tmpt) + r = (mp_word)*tmpt +
((mp_word)tmpx) * ((mp_word)*tmpy++) + ((mp_word)tmpx * (mp_word)*tmpy++) +
((mp_word) u); (mp_word)u;
/* get the lower part */ /* get the lower part */
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK)); *tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));

View File

@ -24,18 +24,18 @@ int s_mp_sqr (mp_int * a, mp_int * b)
mp_digit u, tmpx, *tmpt; mp_digit u, tmpx, *tmpt;
pa = a->used; pa = a->used;
if ((res = mp_init_size (&t, 2*pa + 1)) != MP_OKAY) { if ((res = mp_init_size (&t, (2 * pa) + 1)) != MP_OKAY) {
return res; return res;
} }
/* default used is maximum possible size */ /* default used is maximum possible size */
t.used = 2*pa + 1; t.used = (2 * pa) + 1;
for (ix = 0; ix < pa; ix++) { for (ix = 0; ix < pa; ix++) {
/* first calculate the digit at 2*ix */ /* first calculate the digit at 2*ix */
/* calculate double precision result */ /* calculate double precision result */
r = ((mp_word) t.dp[2*ix]) + r = (mp_word)t.dp[2*ix] +
((mp_word)a->dp[ix])*((mp_word)a->dp[ix]); ((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
/* store lower part in result */ /* store lower part in result */
t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK)); t.dp[ix+ix] = (mp_digit) (r & ((mp_word) MP_MASK));
@ -47,7 +47,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
tmpx = a->dp[ix]; tmpx = a->dp[ix];
/* 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 */

View File

@ -47,14 +47,14 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
u = 0; u = 0;
for (i = 0; i < min; i++) { for (i = 0; i < min; i++) {
/* T[i] = A[i] - B[i] - U */ /* T[i] = A[i] - B[i] - U */
*tmpc = *tmpa++ - *tmpb++ - u; *tmpc = (*tmpa++ - *tmpb++) - u;
/* U = carry bit of T[i] /* U = carry bit of T[i]
* Note this saves performing an AND operation since * Note this saves performing an AND operation since
* if a carry does occur it will propagate all the way to the * if a carry does occur it will propagate all the way to the
* MSB. As a result a single shift is enough to get the carry * MSB. As a result a single shift is enough to get the carry
*/ */
u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
/* Clear carry from T[i] */ /* Clear carry from T[i] */
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;
@ -66,7 +66,7 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
*tmpc = *tmpa++ - u; *tmpc = *tmpa++ - u;
/* U = carry bit of T[i] */ /* U = carry bit of T[i] */
u = *tmpc >> ((mp_digit)(CHAR_BIT * sizeof (mp_digit) - 1)); u = *tmpc >> ((mp_digit)((CHAR_BIT * sizeof(mp_digit)) - 1));
/* Clear carry from T[i] */ /* Clear carry from T[i] */
*tmpc++ &= MP_MASK; *tmpc++ &= MP_MASK;

View File

@ -208,8 +208,8 @@ int mp_init_size(mp_int *a, int size);
/* ---> Basic Manipulations <--- */ /* ---> Basic Manipulations <--- */
#define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO) #define mp_iszero(a) (((a)->used == 0) ? MP_YES : MP_NO)
#define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 0)) ? MP_YES : MP_NO) #define mp_iseven(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 0u)) ? MP_YES : MP_NO)
#define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1) == 1)) ? MP_YES : MP_NO) #define mp_isodd(a) ((((a)->used > 0) && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO) #define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
/* set to zero */ /* set to zero */

View File

@ -66,9 +66,9 @@ int mp_karatsuba_sqr(mp_int *a, mp_int *b);
int mp_toom_sqr(mp_int *a, mp_int *b); int mp_toom_sqr(mp_int *a, mp_int *b);
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);
int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c); int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c);
int fast_mp_montgomery_reduce(mp_int *a, mp_int *m, mp_digit mp); int fast_mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho);
int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int mode); int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode);
int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int mode); int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode);
void bn_reverse(unsigned char *s, int len); void bn_reverse(unsigned char *s, int len);
extern const char *mp_s_rmap; extern const char *mp_s_rmap;
@ -88,14 +88,14 @@ int func_name (mp_int * a, type b) \
mp_zero (a); \ mp_zero (a); \
\ \
/* set four bits at a time */ \ /* set four bits at a time */ \
for (x = 0; x < (sizeof(type) * 2); x++) { \ for (x = 0; x < (sizeof(type) * 2u); x++) { \
/* shift the number up four bits */ \ /* shift the number up four bits */ \
if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \ if ((res = mp_mul_2d (a, 4, a)) != MP_OKAY) { \
return res; \ return res; \
} \ } \
\ \
/* OR in the top four bits of the source */ \ /* OR in the top four bits of the source */ \
a->dp[0] |= (b >> ((sizeof(type) * 8) - 4)) & 15; \ a->dp[0] |= (b >> ((sizeof(type) * 8u) - 4u)) & 15u; \
\ \
/* shift the source up to the next four bits */ \ /* shift the source up to the next four bits */ \
b <<= 4; \ b <<= 4; \