Math updates from libtom. Fixes in mp_read_radix for to include char 36 and clear the destination before checks. Fix to clear tmp value on fp_sub_d and fp_add_d. Fixes in assembly for x86-32 INNERMUL, PPC32 INNERMUL and PROPCARRY and x86-64 SQRADD. Added mp_isneg. Refactor of mp_iszero, mp_iseven, mp_isodd and mp_isneg to check using MP_YES or MP_NO. Changed fp_read_unsigned_bin "b" arg to const. Removal of the "register" on stack variables to let compiler determine best optimization.

This commit is contained in:
David Garske
2016-05-10 14:49:24 -07:00
parent 7a3cb23af9
commit 87c00eb3f3
6 changed files with 120 additions and 116 deletions

View File

@ -136,7 +136,7 @@ __asm__( \
"adcl $0,%%edx \n\t" \
"movl %%edx,%1 \n\t" \
:"=g"(_c[LO]), "=r"(cy) \
:"0"(_c[LO]), "1"(cy), "g"(mu), "g"(*tmpm++) \
:"0"(_c[LO]), "1"(cy), "r"(mu), "r"(*tmpm++) \
: "%eax", "%edx", "cc")
#define PROPCARRY \
@ -533,25 +533,21 @@ __asm__( \
mu = c[x] * mp
#define INNERMUL \
__asm__( \
__asm__( \
" mullw 16,%3,%4 \n\t" \
" mulhwu 17,%3,%4 \n\t" \
" addc 16,16,%0 \n\t" \
" addc 16,16,%2 \n\t" \
" addze 17,17 \n\t" \
" lwz 18,%1 \n\t" \
" addc 16,16,18 \n\t" \
" addc %1,16,%5 \n\t" \
" addze %0,17 \n\t" \
" stw 16,%1 \n\t" \
:"=r"(cy),"=m"(_c[0]):"0"(cy),"r"(mu),"r"(tmpm[0]),"1"(_c[0]):"16", "17", "18","cc"); ++tmpm;
:"=r"(cy),"=r"(_c[0]):"0"(cy),"r"(mu),"r"(tmpm[0]),"1"(_c[0]):"16", "17", "cc"); ++tmpm;
#define PROPCARRY \
__asm__( \
" lwz 16,%1 \n\t" \
" addc 16,16,%0 \n\t" \
" stw 16,%1 \n\t" \
" xor %0,%0,%0 \n\t" \
" addze %0,%0 \n\t" \
:"=r"(cy),"=m"(_c[0]):"0"(cy),"1"(_c[0]):"16","cc");
__asm__( \
" addc %1,%3,%2 \n\t" \
" xor %0,%2,%2 \n\t" \
" addze %0,%2 \n\t" \
:"=r"(cy),"=r"(_c[0]):"0"(cy),"1"(_c[0]):"cc");
#elif defined(TFM_PPC64)
@ -740,7 +736,7 @@ __asm__( \
"addq %%rax,%0 \n\t" \
"adcq %%rdx,%1 \n\t" \
"adcq $0,%2 \n\t" \
:"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "g"(i) :"%rax","%rdx","cc");
:"=r"(c0), "=r"(c1), "=r"(c2): "0"(c0), "1"(c1), "2"(c2), "x"(i) :"%rax","%rdx","cc");
#define SQRADD2(i, j) \
__asm__( \

View File

@ -252,7 +252,7 @@ int mp_leading_bit (mp_int * a)
if (mp_init_copy(&t, a) != MP_OKAY)
return 0;
while (mp_iszero(&t) == 0) {
while (mp_iszero(&t) == MP_NO) {
#ifndef MP_8BIT
bit = (t.dp[0] & 0x80) != 0;
#else
@ -277,7 +277,7 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
}
x = 0;
while (mp_iszero (&t) == 0) {
while (mp_iszero (&t) == MP_NO) {
#ifndef MP_8BIT
b[x++] = (unsigned char) (t.dp[0] & 255);
#else
@ -329,7 +329,7 @@ int mp_copy (mp_int * a, mp_int * b)
/* zero b and copy the parameters over */
{
register mp_digit *tmpa, *tmpb;
mp_digit *tmpa, *tmpb;
/* pointer aliases */
@ -507,7 +507,7 @@ void mp_exch (mp_int * a, mp_int * b)
/* shift right a certain number of bits */
void mp_rshb (mp_int *c, int x)
{
register mp_digit *tmpc, mask, shift;
mp_digit *tmpc, mask, shift;
mp_digit r, rr;
mp_digit D = x;
@ -553,7 +553,7 @@ void mp_rshd (mp_int * a, int b)
}
{
register mp_digit *bottom, *top;
mp_digit *bottom, *top;
/* shift the digits down */
@ -686,8 +686,8 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
/* shift any bit count < DIGIT_BIT */
d = (mp_digit) (b % DIGIT_BIT);
if (d != 0) {
register mp_digit *tmpc, shift, mask, r, rr;
register int x;
mp_digit *tmpc, shift, mask, r, rr;
int x;
/* bitmask for carries */
mask = (((mp_digit)1) << d) - 1;
@ -740,7 +740,7 @@ int mp_lshd (mp_int * a, int b)
}
{
register mp_digit *top, *bottom;
mp_digit *top, *bottom;
/* increment the used by the shift amount then copy upwards */
a->used += b;
@ -845,7 +845,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 */
#ifdef BN_MP_EXPTMOD_FAST_C
if (mp_isodd (P) == 1 || dr != 0) {
if (mp_isodd (P) == MP_YES || dr != 0) {
return mp_exptmod_fast (G, X, P, Y, dr);
} else {
#endif
@ -888,13 +888,13 @@ int mp_abs (mp_int * a, mp_int * b)
int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
{
/* b cannot be negative */
if (b->sign == MP_NEG || mp_iszero(b) == 1) {
if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) {
return MP_VAL;
}
#ifdef BN_FAST_MP_INVMOD_C
/* if the modulus is odd we can use a faster routine instead */
if (mp_isodd (b) == 1) {
if (mp_isodd (b) == MP_YES) {
return fast_mp_invmod (a, b, c);
}
#endif
@ -917,7 +917,7 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
int res, neg, loop_check = 0;
/* 2. [modified] b must be odd */
if (mp_iseven (b) == 1) {
if (mp_iseven (b) == MP_YES) {
return MP_VAL;
}
@ -947,13 +947,13 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
top:
/* 4. while u is even do */
while (mp_iseven (&u) == 1) {
while (mp_iseven (&u) == MP_YES) {
/* 4.1 u = u/2 */
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto LBL_ERR;
}
/* 4.2 if B is odd then */
if (mp_isodd (&B) == 1) {
if (mp_isodd (&B) == MP_YES) {
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
goto LBL_ERR;
}
@ -965,13 +965,13 @@ top:
}
/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
while (mp_iseven (&v) == MP_YES) {
/* 5.1 v = v/2 */
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
goto LBL_ERR;
}
/* 5.2 if D is odd then */
if (mp_isodd (&D) == 1) {
if (mp_isodd (&D) == MP_YES) {
/* D = (D-x)/2 */
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
goto LBL_ERR;
@ -1005,7 +1005,7 @@ top:
}
/* if not zero goto step 4 */
if (mp_iszero (&u) == 0) {
if (mp_iszero (&u) == MP_NO) {
if (++loop_check > 4096) {
res = MP_VAL;
goto LBL_ERR;
@ -1055,7 +1055,7 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
int res;
/* b cannot be negative */
if (b->sign == MP_NEG || mp_iszero(b) == 1) {
if (b->sign == MP_NEG || mp_iszero(b) == MP_YES) {
return MP_VAL;
}
@ -1079,7 +1079,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! */
if (mp_iseven (&x) == 1 && mp_iseven (&y) == 1) {
if (mp_iseven (&x) == MP_YES && mp_iseven (&y) == MP_YES) {
res = MP_VAL;
goto LBL_ERR;
}
@ -1096,13 +1096,13 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
top:
/* 4. while u is even do */
while (mp_iseven (&u) == 1) {
while (mp_iseven (&u) == MP_YES) {
/* 4.1 u = u/2 */
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
goto LBL_ERR;
}
/* 4.2 if A or B is odd then */
if (mp_isodd (&A) == 1 || mp_isodd (&B) == 1) {
if (mp_isodd (&A) == MP_YES || mp_isodd (&B) == MP_YES) {
/* A = (A+y)/2, B = (B-x)/2 */
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
goto LBL_ERR;
@ -1121,13 +1121,13 @@ top:
}
/* 5. while v is even do */
while (mp_iseven (&v) == 1) {
while (mp_iseven (&v) == MP_YES) {
/* 5.1 v = v/2 */
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
goto LBL_ERR;
}
/* 5.2 if C or D is odd then */
if (mp_isodd (&C) == 1 || mp_isodd (&D) == 1) {
if (mp_isodd (&C) == MP_YES || mp_isodd (&D) == MP_YES) {
/* C = (C+y)/2, D = (D-x)/2 */
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
goto LBL_ERR;
@ -1175,7 +1175,7 @@ top:
}
/* if not zero goto step 4 */
if (mp_iszero (&u) == 0)
if (mp_iszero (&u) == MP_NO)
goto top;
/* now a = C, b = D, gcd == g*v */
@ -1347,7 +1347,7 @@ int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int res, n, n2;
/* is divisor zero ? */
if (mp_iszero (b) == 1) {
if (mp_iszero (b) == MP_YES) {
return MP_VAL;
}
@ -1427,7 +1427,7 @@ int mp_div_2(mp_int * a, mp_int * b)
oldused = b->used;
b->used = a->used;
{
register mp_digit r, rr, *tmpa, *tmpb;
mp_digit r, rr, *tmpa, *tmpb;
/* source alias */
tmpa = a->dp + b->used - 1;
@ -1463,7 +1463,7 @@ int mp_div_2(mp_int * a, mp_int * b)
/* high level addition (handles signs) */
int mp_add (mp_int * a, mp_int * b, mp_int * c)
{
int sa, sb, res;
int sa, sb, res;
/* get sign of both inputs */
sa = a->sign;
@ -1523,8 +1523,8 @@ int s_mp_add (mp_int * a, mp_int * b, mp_int * c)
c->used = max + 1;
{
register mp_digit u, *tmpa, *tmpb, *tmpc;
register int i;
mp_digit u, *tmpa, *tmpb, *tmpc;
int i;
/* alias for digit pointers */
@ -1599,8 +1599,8 @@ int s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
c->used = max;
{
register mp_digit u, *tmpa, *tmpb, *tmpc;
register int i;
mp_digit u, *tmpa, *tmpb, *tmpc;
int i;
/* alias for digit pointers */
tmpa = a->dp;
@ -2165,8 +2165,8 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* an array of double precision words W[...]
*/
{
register mp_word *_W;
register mp_digit *tmpx;
mp_word *_W;
mp_digit *tmpx;
/* alias for the W[] array */
_W = W;
@ -2195,7 +2195,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* by casting the value down to a mp_digit. Note this requires
* that W[ix-1] have the carry cleared (see after the inner loop)
*/
register mp_digit mu;
mp_digit mu;
mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
/* a = a + mu * m * b**i
@ -2213,9 +2213,9 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* first m->used words of W[] have the carries fixed
*/
{
register int iy;
register mp_digit *tmpn;
register mp_word *_W;
int iy;
mp_digit *tmpn;
mp_word *_W;
/* alias for the digits of the modulus */
tmpn = n->dp;
@ -2238,8 +2238,8 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
* significant digits we zeroed].
*/
{
register mp_digit *tmpx;
register mp_word *_W, *_W1;
mp_digit *tmpx;
mp_word *_W, *_W1;
/* nox fix rest of carries */
@ -2334,9 +2334,9 @@ int mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
/* a = a + mu * m * b**i */
{
register int iy;
register mp_digit *tmpn, *tmpx, u;
register mp_word r;
int iy;
mp_digit *tmpn, *tmpx, u;
mp_word r;
/* alias for digits of the modulus */
tmpn = n->dp;
@ -2728,7 +2728,7 @@ int mp_mul_2(mp_int * a, mp_int * b)
b->used = a->used;
{
register mp_digit r, rr, *tmpa, *tmpb;
mp_digit r, rr, *tmpa, *tmpb;
/* alias for source */
tmpa = a->dp;
@ -2997,7 +2997,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
#else
mp_digit W[MP_WARRAY];
#endif
register mp_word _W;
mp_word _W;
/* grow the destination as required */
if (c->alloc < digs) {
@ -3055,7 +3055,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
c->used = pa;
{
register mp_digit *tmpc;
mp_digit *tmpc;
tmpc = c->dp;
for (ix = 0; ix < pa+1; ix++) {
/* now extract the previous digit [below the carry] */
@ -3234,7 +3234,6 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
bits = 1;
}
/* now compute C = A * B mod b */
for (x = bits - 1; x < (int)DIGIT_BIT; x++) {
if ((res = mp_mul_2 (a, a)) != MP_OKAY) {
@ -3777,7 +3776,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
c->used = pa;
{
register mp_digit *tmpc;
mp_digit *tmpc;
tmpc = c->dp + digs;
for (ix = digs; ix < pa; ix++) { /* TAO, <= could potentially overwrite */
@ -4034,12 +4033,12 @@ int mp_cnt_lsb(mp_int *a)
mp_digit q, qq;
/* easy out */
if (mp_iszero(a) == 1) {
if (mp_iszero(a) == MP_YES) {
return 0;
}
/* 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];
x *= DIGIT_BIT;
@ -4089,7 +4088,7 @@ static int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
}
/* quick outs */
if (b == 1 || mp_iszero(a) == 1) {
if (b == 1 || mp_iszero(a) == MP_YES) {
if (d != NULL) {
*d = 0;
}
@ -4539,7 +4538,7 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
}
}
while (mp_iszero(&v) == 0) {
while (mp_iszero(&v) == MP_NO) {
/* make sure v is the largest */
if (mp_cmp_mag(&u, &v) == MP_GT) {
/* swap u and v to make sure v is >= u */
@ -4608,11 +4607,11 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
/* process each digit of the string */
while (*str) {
/* if the radix < 36 the conversion is case insensitive
/* if the radix <= 36 the conversion is case insensitive
* this allows numbers like 1AB and 1ab to represent the same value
* [e.g. in hex]
*/
ch = (char) ((radix < 36) ? XTOUPPER((unsigned char)*str) : *str);
ch = (radix <= 36) ? (char)XTOUPPER((unsigned char)*str) : *str;
for (y = 0; y < 64; y++) {
if (ch == mp_s_rmap[y]) {
break;
@ -4637,7 +4636,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
}
/* set the sign only if a != 0 */
if (mp_iszero(a) != 1) {
if (mp_iszero(a) != MP_YES) {
a->sign = neg;
}
return MP_OKAY;
@ -4717,7 +4716,7 @@ int mp_toradix (mp_int *a, char *str, int radix)
}
/* quick out if its zero */
if (mp_iszero(a) == 1) {
if (mp_iszero(a) == MP_YES) {
*str++ = '0';
*str = '\0';
return MP_OKAY;
@ -4735,7 +4734,7 @@ int mp_toradix (mp_int *a, char *str, int radix)
}
digs = 0;
while (mp_iszero (&t) == 0) {
while (mp_iszero (&t) == MP_NO) {
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
mp_clear (&t);
return res;

View File

@ -428,7 +428,7 @@ int wc_SrpGetVerifier(Srp* srp, byte* verifier, word32* size)
if (!srp || !verifier || !size || srp->side != SRP_CLIENT_SIDE)
return BAD_FUNC_ARG;
if (mp_iszero(&srp->auth))
if (mp_iszero(&srp->auth) == MP_YES)
return SRP_CALL_ORDER_E;
r = mp_init(&v);
@ -462,7 +462,7 @@ int wc_SrpSetPrivate(Srp* srp, const byte* private, word32 size)
if (!srp || !private || !size)
return BAD_FUNC_ARG;
if (mp_iszero(&srp->auth))
if (mp_iszero(&srp->auth) == MP_YES)
return SRP_CALL_ORDER_E;
r = mp_init(&p);
@ -470,7 +470,7 @@ int wc_SrpSetPrivate(Srp* srp, const byte* private, word32 size)
return MP_INIT_E;
if (!r) r = mp_read_unsigned_bin(&p, private, size);
if (!r) r = mp_mod(&p, &srp->N, &srp->priv);
if (!r) r = mp_iszero(&srp->priv) ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_iszero(&srp->priv) == MP_YES ? SRP_BAD_KEY_E : 0;
mp_clear(&p);
@ -499,7 +499,7 @@ int wc_SrpGetPublic(Srp* srp, byte* pub, word32* size)
if (!srp || !pub || !size)
return BAD_FUNC_ARG;
if (mp_iszero(&srp->auth))
if (mp_iszero(&srp->auth) == MP_YES)
return SRP_CALL_ORDER_E;
modulusSz = mp_unsigned_bin_size(&srp->N);
@ -511,7 +511,7 @@ int wc_SrpGetPublic(Srp* srp, byte* pub, word32* size)
return MP_INIT_E;
/* priv = random() */
if (mp_iszero(&srp->priv))
if (mp_iszero(&srp->priv) == MP_YES)
r = wc_SrpGenPrivate(srp, pub, modulusSz);
/* client side: A = g ^ a % N */
@ -524,7 +524,7 @@ int wc_SrpGetPublic(Srp* srp, byte* pub, word32* size)
if (mp_init_multi(&i, &j, 0, 0, 0, 0) == MP_OKAY) {
if (!r) r = mp_read_unsigned_bin(&i, srp->k,SrpHashSize(srp->type));
if (!r) r = mp_iszero(&i) ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_iszero(&i) == MP_YES ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_exptmod(&srp->g, &srp->priv, &srp->N, &pubkey);
if (!r) r = mp_mulmod(&i, &srp->auth, &srp->N, &j);
if (!r) r = mp_add(&j, &pubkey, &i);
@ -601,7 +601,7 @@ int wc_SrpComputeKey(Srp* srp, byte* clientPubKey, word32 clientPubKeySz,
|| !serverPubKey || serverPubKeySz == 0)
return BAD_FUNC_ARG;
if (mp_iszero(&srp->priv))
if (mp_iszero(&srp->priv) == MP_YES)
return SRP_CALL_ORDER_E;
/* initializing variables */
@ -642,11 +642,11 @@ int wc_SrpComputeKey(Srp* srp, byte* clientPubKey, word32 clientPubKeySz,
/* temp1 = B - k * v; rejects k == 0, B == 0 and B >= N. */
r = mp_read_unsigned_bin(&temp1, srp->k, digestSz);
if (!r) r = mp_iszero(&temp1) ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_iszero(&temp1) == MP_YES ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_exptmod(&srp->g, &srp->auth, &srp->N, &temp2);
if (!r) r = mp_mulmod(&temp1, &temp2, &srp->N, &s);
if (!r) r = mp_read_unsigned_bin(&temp2, serverPubKey, serverPubKeySz);
if (!r) r = mp_iszero(&temp2) ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_iszero(&temp2) == MP_YES ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_cmp(&temp2, &srp->N) != MP_LT ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_sub(&temp2, &s, &temp1);
@ -663,7 +663,7 @@ int wc_SrpComputeKey(Srp* srp, byte* clientPubKey, word32 clientPubKeySz,
/* temp2 = A * temp1 % N; rejects A == 0, A >= N */
if (!r) r = mp_read_unsigned_bin(&s, clientPubKey, clientPubKeySz);
if (!r) r = mp_iszero(&s) ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_iszero(&s) == MP_YES ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_cmp(&s, &srp->N) != MP_LT ? SRP_BAD_KEY_E : 0;
if (!r) r = mp_mulmod(&s, &temp1, &srp->N, &temp2);

View File

@ -104,7 +104,7 @@ void fp_add(fp_int *a, fp_int *b, fp_int *c)
void s_fp_add(fp_int *a, fp_int *b, fp_int *c)
{
int x, y, oldused;
register fp_word t;
fp_word t;
y = MAX(a->used, b->used);
oldused = MIN(c->used, FP_SIZE); /* help static analysis w/ largest size */
@ -323,7 +323,7 @@ void fp_mul_2(fp_int * a, fp_int * b)
b->used = a->used;
{
register fp_digit r, rr, *tmpa, *tmpb;
fp_digit r, rr, *tmpa, *tmpb;
/* alias for source */
tmpa = a->dp;
@ -498,8 +498,9 @@ void fp_mul_comba(fp_int *A, fp_int *B, fp_int *C)
/* execute loop */
COMBA_FORWARD;
for (iz = 0; iz < iy; ++iz) {
/* TAO change COMBA_ADD back to MULADD */
MULADD(*tmpx++, *tmpy--);
fp_digit _tmpx = *tmpx++;
fp_digit _tmpy = *tmpy--;
MULADD(_tmpx, _tmpy);
}
/* store term */
@ -662,7 +663,7 @@ void fp_div_2(fp_int * a, fp_int * b)
oldused = b->used;
b->used = a->used;
{
register fp_digit r, rr, *tmpa, *tmpb;
fp_digit r, rr, *tmpa, *tmpb;
/* source alias */
tmpa = a->dp + b->used - 1;
@ -1056,7 +1057,7 @@ static int _fp_exptmod(fp_int * G, fp_int * X, fp_int * P, fp_int * Y)
return FP_OKAY;
}
#else
#else /* TFM_TIMING_RESISTANT */
/* y = g**x (mod b)
* Some restrictions... x must be positive and < b
@ -1750,13 +1751,13 @@ void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp)
_c = c + x;
tmpm = m->dp;
y = 0;
#if (defined(TFM_SSE2) || defined(TFM_X86_64))
#if defined(INNERMUL8)
for (; y < (pa & ~7); y += 8) {
INNERMUL8 ;
_c += 8;
tmpm += 8;
}
#endif
#endif
for (; y < pa; y++) {
INNERMUL;
++_c;
@ -1791,7 +1792,7 @@ void fp_montgomery_reduce(fp_int *a, fp_int *m, fp_digit mp)
}
}
void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c)
void fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c)
{
/* zero the int */
fp_zero (a);
@ -1978,7 +1979,7 @@ void fp_lshd(fp_int *a, int x)
/* right shift by bit count */
void fp_rshb(fp_int *c, int x)
{
register fp_digit *tmpc, mask, shift;
fp_digit *tmpc, mask, shift;
fp_digit r, rr;
fp_digit D = x;
@ -2057,6 +2058,7 @@ void fp_sub_d(fp_int *a, fp_digit b, fp_int *c)
fp_init(&tmp);
fp_set(&tmp, b);
fp_sub(a, &tmp, c);
fp_clear(&tmp);
}
@ -2196,7 +2198,7 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
/* reads a unsigned char array, assumes the msb is stored first [big endian] */
int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
{
fp_read_unsigned_bin(a, (unsigned char *)b, c);
fp_read_unsigned_bin(a, b, c);
return MP_OKAY;
}
@ -2263,6 +2265,10 @@ int mp_iszero(mp_int* a)
return fp_iszero(a);
}
int mp_isneg(mp_int* a)
{
return fp_isneg(a);
}
int mp_count_bits (mp_int* a)
{
@ -2778,7 +2784,8 @@ void fp_add_d(fp_int *a, fp_digit b, fp_int *c)
fp_int tmp;
fp_init(&tmp);
fp_set(&tmp, b);
fp_add(a,&tmp,c);
fp_add(a, &tmp, c);
fp_clear(&tmp);
}
/* external compatibility */
@ -2804,6 +2811,9 @@ static int fp_read_radix(fp_int *a, const char *str, int radix)
int y, neg;
char ch;
/* set the integer to the default of zero */
fp_zero (a);
/* make sure the radix is ok */
if (radix < 2 || radix > 64) {
return FP_VAL;
@ -2819,16 +2829,13 @@ static int fp_read_radix(fp_int *a, const char *str, int radix)
neg = FP_ZPOS;
}
/* set the integer to the default of zero */
fp_zero (a);
/* process each digit of the string */
while (*str) {
/* if the radix < 36 the conversion is case insensitive
/* if the radix <= 36 the conversion is case insensitive
* this allows numbers like 1AB and 1ab to represent the same value
* [e.g. in hex]
*/
ch = (char) ((radix < 36) ? XTOUPPER((unsigned char)*str) : *str);
ch = (char)((radix <= 36) ? XTOUPPER((unsigned char)*str) : *str);
for (y = 0; y < 64; y++) {
if (ch == fp_s_rmap[y]) {
break;

View File

@ -196,10 +196,10 @@ typedef int ltm_prime_callback(unsigned char *dst, int len, void *dat);
/* ---> Basic Manipulations <--- */
#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)
(((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)
(((a)->used > 0 && (((a)->dp[0] & 1u) == 1u)) ? MP_YES : MP_NO)
#define mp_isneg(a) (((a)->sign != MP_ZPOS) ? MP_YES : MP_NO)
/* number of primes */
#ifdef MP_8BIT

View File

@ -383,6 +383,7 @@ typedef struct {
#define fp_iszero(a) (((a)->used == 0) ? FP_YES : FP_NO)
#define fp_iseven(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 0)) ? FP_YES : FP_NO)
#define fp_isodd(a) (((a)->used > 0 && (((a)->dp[0] & 1) == 1)) ? FP_YES : FP_NO)
#define fp_isneg(a) (((a)->sign != 0) ? FP_YES : FP_NO)
/* set to a small digit */
void fp_set(fp_int *a, fp_digit b);
@ -527,16 +528,16 @@ int fp_exptmod(fp_int *a, fp_int *b, fp_int *c, fp_int *d);
/*int fp_prime_random_ex(fp_int *a, int t, int size, int flags, tfm_prime_callback cb, void *dat);*/
/* radix conersions */
/* radix conversions */
int fp_count_bits(fp_int *a);
int fp_leading_bit(fp_int *a);
int fp_unsigned_bin_size(fp_int *a);
void fp_read_unsigned_bin(fp_int *a, unsigned char *b, int c);
void fp_read_unsigned_bin(fp_int *a, const unsigned char *b, int c);
void fp_to_unsigned_bin(fp_int *a, unsigned char *b);
/*int fp_signed_bin_size(fp_int *a);*/
/*void fp_read_signed_bin(fp_int *a, unsigned char *b, int c);*/
/*void fp_read_signed_bin(fp_int *a, const unsigned char *b, int c);*/
/*void fp_to_signed_bin(fp_int *a, unsigned char *b);*/
/*int fp_read_radix(fp_int *a, char *str, int radix);*/
@ -591,20 +592,20 @@ void fp_sqr_comba64(fp_int *a, fp_int *b);
*/
/* Types */
typedef fp_digit mp_digit;
typedef fp_word mp_word;
typedef fp_int mp_int;
typedef fp_digit mp_digit;
typedef fp_word mp_word;
typedef fp_int mp_int;
/* Constants */
#define MP_LT FP_LT /* less than */
#define MP_EQ FP_EQ /* equal to */
#define MP_GT FP_GT /* greater than */
#define MP_VAL FP_VAL /* invalid */
#define MP_MEM FP_MEM /* memory error */
#define MP_NOT_INF FP_NOT_INF /* point not at infinity */
#define MP_OKAY FP_OKAY /* ok result */
#define MP_NO FP_NO /* yes/no result */
#define MP_YES FP_YES /* yes/no result */
#define MP_LT FP_LT /* less than */
#define MP_EQ FP_EQ /* equal to */
#define MP_GT FP_GT /* greater than */
#define MP_VAL FP_VAL /* invalid */
#define MP_MEM FP_MEM /* memory error */
#define MP_NOT_INF FP_NOT_INF /* point not at infinity */
#define MP_OKAY FP_OKAY /* ok result */
#define MP_NO FP_NO /* yes/no result */
#define MP_YES FP_YES /* yes/no result */
/* Prototypes */
#define mp_zero(a) fp_zero(a)
@ -637,6 +638,7 @@ int mp_sub_d(fp_int *a, fp_digit b, fp_int *c);
int mp_copy(fp_int* a, fp_int* b);
int mp_isodd(mp_int* a);
int mp_iszero(mp_int* a);
int mp_isneg(mp_int* a);
int mp_count_bits(mp_int *a);
int mp_leading_bit(mp_int *a);
int mp_set_int(mp_int *a, mp_digit b);