remove space after function name and cast
This commit is contained in:
parent
378be117a3
commit
d6a9a58f64
@ -27,7 +27,7 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
int res, neg;
|
||||
|
||||
/* 2. [modified] b must be odd */
|
||||
if (mp_iseven (b) == MP_YES) {
|
||||
if (mp_iseven(b) == MP_YES) {
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
@ -37,92 +37,92 @@ int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
|
||||
/* x == modulus, y == value to invert */
|
||||
if ((res = mp_copy (b, &x)) != MP_OKAY) {
|
||||
if ((res = mp_copy(b, &x)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* we need y = |a| */
|
||||
if ((res = mp_mod (a, b, &y)) != MP_OKAY) {
|
||||
if ((res = mp_mod(a, b, &y)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
||||
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
||||
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
|
||||
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
mp_set (&D, 1);
|
||||
mp_set(&D, 1);
|
||||
|
||||
top:
|
||||
/* 4. while u is even do */
|
||||
while (mp_iseven (&u) == MP_YES) {
|
||||
while (mp_iseven(&u) == MP_YES) {
|
||||
/* 4.1 u = u/2 */
|
||||
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&u, &u)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
/* 4.2 if B is odd then */
|
||||
if (mp_isodd (&B) == MP_YES) {
|
||||
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
|
||||
if (mp_isodd(&B) == MP_YES) {
|
||||
if ((res = mp_sub(&B, &x, &B)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
/* B = B/2 */
|
||||
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&B, &B)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* 5. while v is even do */
|
||||
while (mp_iseven (&v) == MP_YES) {
|
||||
while (mp_iseven(&v) == MP_YES) {
|
||||
/* 5.1 v = v/2 */
|
||||
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&v, &v)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
/* 5.2 if D is odd then */
|
||||
if (mp_isodd (&D) == MP_YES) {
|
||||
if (mp_isodd(&D) == MP_YES) {
|
||||
/* D = (D-x)/2 */
|
||||
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&D, &x, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
/* D = D/2 */
|
||||
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&D, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* 6. if u >= v then */
|
||||
if (mp_cmp (&u, &v) != MP_LT) {
|
||||
if (mp_cmp(&u, &v) != MP_LT) {
|
||||
/* u = u - v, B = B - D */
|
||||
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&u, &v, &u)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&B, &D, &B)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
} else {
|
||||
/* v - v - u, D = D - B */
|
||||
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&v, &u, &v)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&D, &B, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* if not zero goto step 4 */
|
||||
if (mp_iszero (&u) == MP_NO) {
|
||||
if (mp_iszero(&u) == MP_NO) {
|
||||
goto top;
|
||||
}
|
||||
|
||||
/* now a = C, b = D, gcd == g*v */
|
||||
|
||||
/* if v != 1 then there is no inverse */
|
||||
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
||||
if (mp_cmp_d(&v, 1) != MP_EQ) {
|
||||
res = MP_VAL;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
@ -130,15 +130,15 @@ top:
|
||||
/* b is now the inverse */
|
||||
neg = a->sign;
|
||||
while (D.sign == MP_NEG) {
|
||||
if ((res = mp_add (&D, b, &D)) != MP_OKAY) {
|
||||
if ((res = mp_add(&D, b, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
mp_exch (&D, c);
|
||||
mp_exch(&D, c);
|
||||
c->sign = neg;
|
||||
res = MP_OKAY;
|
||||
|
||||
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &B, &D, NULL);
|
||||
LBL_ERR:mp_clear_multi(&x, &y, &u, &v, &B, &D, NULL);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -33,7 +33,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
|
||||
/* grow a as required */
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -73,7 +73,7 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
* that W[ix-1] have the carry cleared (see after the inner loop)
|
||||
*/
|
||||
mp_digit mu;
|
||||
mu = (mp_digit) (((W[ix] & MP_MASK) * rho) & MP_MASK);
|
||||
mu = (mp_digit)(((W[ix] & MP_MASK) * rho) & MP_MASK);
|
||||
|
||||
/* a = a + mu * m * b**i
|
||||
*
|
||||
@ -157,11 +157,11 @@ int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
|
||||
/* set the max used and clamp */
|
||||
x->used = n->used + 1;
|
||||
mp_clamp (x);
|
||||
mp_clamp(x);
|
||||
|
||||
/* if A >= m then A = A - m */
|
||||
if (mp_cmp_mag (x, n) != MP_LT) {
|
||||
return s_mp_sub (x, n, x);
|
||||
if (mp_cmp_mag(x, n) != MP_LT) {
|
||||
return s_mp_sub(x, n, x);
|
||||
}
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
@ -39,7 +39,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
|
||||
/* grow the destination as required */
|
||||
if (c->alloc < digs) {
|
||||
if ((res = mp_grow (c, digs)) != MP_OKAY) {
|
||||
if ((res = mp_grow(c, digs)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -97,7 +97,7 @@ int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
*tmpc++ = 0;
|
||||
}
|
||||
}
|
||||
mp_clamp (c);
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -33,7 +33,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* grow the destination as required */
|
||||
pa = a->used + b->used;
|
||||
if (c->alloc < pa) {
|
||||
if ((res = mp_grow (c, pa)) != MP_OKAY) {
|
||||
if ((res = mp_grow(c, pa)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -88,7 +88,7 @@ int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
*tmpc++ = 0;
|
||||
}
|
||||
}
|
||||
mp_clamp (c);
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -34,7 +34,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
/* grow the destination as required */
|
||||
pa = a->used + a->used;
|
||||
if (b->alloc < pa) {
|
||||
if ((res = mp_grow (b, pa)) != MP_OKAY) {
|
||||
if ((res = mp_grow(b, pa)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -104,7 +104,7 @@ int fast_s_mp_sqr (mp_int * a, mp_int * b)
|
||||
*tmpb++ = 0;
|
||||
}
|
||||
}
|
||||
mp_clamp (b);
|
||||
mp_clamp(b);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -26,10 +26,10 @@ mp_2expt (mp_int * a, int b)
|
||||
int res;
|
||||
|
||||
/* zero a as per default */
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
|
@ -26,7 +26,7 @@ mp_abs (mp_int * a, mp_int * b)
|
||||
|
||||
/* copy a to b */
|
||||
if (a != b) {
|
||||
if ((res = mp_copy (a, b)) != MP_OKAY) {
|
||||
if ((res = mp_copy(a, b)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@ -29,18 +29,18 @@ int mp_add (mp_int * a, mp_int * b, mp_int * c)
|
||||
/* both positive or both negative */
|
||||
/* add their magnitudes, copy the sign */
|
||||
c->sign = sa;
|
||||
res = s_mp_add (a, b, c);
|
||||
res = s_mp_add(a, b, c);
|
||||
} else {
|
||||
/* one positive, the other negative */
|
||||
/* subtract the one with the greater magnitude from */
|
||||
/* the one of the lesser magnitude. The result gets */
|
||||
/* the sign of the one with the greater magnitude. */
|
||||
if (mp_cmp_mag (a, b) == MP_LT) {
|
||||
if (mp_cmp_mag(a, b) == MP_LT) {
|
||||
c->sign = sb;
|
||||
res = s_mp_sub (b, a, c);
|
||||
res = s_mp_sub(b, a, c);
|
||||
} else {
|
||||
c->sign = sa;
|
||||
res = s_mp_sub (a, b, c);
|
||||
res = s_mp_sub(a, b, c);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
@ -22,16 +22,16 @@ mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
int res;
|
||||
mp_int t;
|
||||
|
||||
if ((res = mp_init (&t)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_add (a, b, &t)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_add(a, b, &t)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
res = mp_mod (&t, c, d);
|
||||
mp_clear (&t);
|
||||
res = mp_mod(&t, c, d);
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
10
bn_mp_and.c
10
bn_mp_and.c
@ -23,13 +23,13 @@ mp_and (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_int t, *x;
|
||||
|
||||
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;
|
||||
}
|
||||
px = b->used;
|
||||
x = b;
|
||||
} else {
|
||||
if ((res = mp_init_copy (&t, b)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
px = a->used;
|
||||
@ -45,9 +45,9 @@ mp_and (mp_int * a, mp_int * b, mp_int * c)
|
||||
t.dp[ix] = 0;
|
||||
}
|
||||
|
||||
mp_clamp (&t);
|
||||
mp_exch (c, &t);
|
||||
mp_clear (&t);
|
||||
mp_clamp(&t);
|
||||
mp_exch(c, &t);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -28,7 +28,7 @@ mp_copy (mp_int * a, mp_int * b)
|
||||
|
||||
/* grow dest */
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
74
bn_mp_div.c
74
bn_mp_div.c
@ -24,19 +24,19 @@ 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) == MP_YES) {
|
||||
if (mp_iszero(b) == MP_YES) {
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
/* if a < b then q=0, r = a */
|
||||
if (mp_cmp_mag (a, b) == MP_LT) {
|
||||
if (mp_cmp_mag(a, b) == MP_LT) {
|
||||
if (d != NULL) {
|
||||
res = mp_copy (a, d);
|
||||
res = mp_copy(a, d);
|
||||
} else {
|
||||
res = MP_OKAY;
|
||||
}
|
||||
if (c != NULL) {
|
||||
mp_zero (c);
|
||||
mp_zero(c);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
@ -106,41 +106,41 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
int res, n, t, i, norm, neg;
|
||||
|
||||
/* is divisor zero ? */
|
||||
if (mp_iszero (b) == MP_YES) {
|
||||
if (mp_iszero(b) == MP_YES) {
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
/* if a < b then q=0, r = a */
|
||||
if (mp_cmp_mag (a, b) == MP_LT) {
|
||||
if (mp_cmp_mag(a, b) == MP_LT) {
|
||||
if (d != NULL) {
|
||||
res = mp_copy (a, d);
|
||||
res = mp_copy(a, d);
|
||||
} else {
|
||||
res = MP_OKAY;
|
||||
}
|
||||
if (c != NULL) {
|
||||
mp_zero (c);
|
||||
mp_zero(c);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_init_size (&q, a->used + 2)) != MP_OKAY) {
|
||||
if ((res = mp_init_size(&q, a->used + 2)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
q.used = a->used + 2;
|
||||
|
||||
if ((res = mp_init (&t1)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t1)) != MP_OKAY) {
|
||||
goto LBL_Q;
|
||||
}
|
||||
|
||||
if ((res = mp_init (&t2)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t2)) != MP_OKAY) {
|
||||
goto LBL_T1;
|
||||
}
|
||||
|
||||
if ((res = mp_init_copy (&x, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&x, a)) != MP_OKAY) {
|
||||
goto LBL_T2;
|
||||
}
|
||||
|
||||
if ((res = mp_init_copy (&y, b)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&y, b)) != MP_OKAY) {
|
||||
goto LBL_X;
|
||||
}
|
||||
|
||||
@ -152,10 +152,10 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
norm = mp_count_bits(&y) % DIGIT_BIT;
|
||||
if (norm < (int)(DIGIT_BIT-1)) {
|
||||
norm = (DIGIT_BIT-1) - norm;
|
||||
if ((res = mp_mul_2d (&x, norm, &x)) != MP_OKAY) {
|
||||
if ((res = mp_mul_2d(&x, norm, &x)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
if ((res = mp_mul_2d (&y, norm, &y)) != MP_OKAY) {
|
||||
if ((res = mp_mul_2d(&y, norm, &y)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
} else {
|
||||
@ -167,19 +167,19 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
t = y.used - 1;
|
||||
|
||||
/* while (x >= y*b**n-t) do { q[n-t] += 1; x -= y*b**{n-t} } */
|
||||
if ((res = mp_lshd (&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
|
||||
if ((res = mp_lshd(&y, n - t)) != MP_OKAY) { /* y = y*b**{n-t} */
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
while (mp_cmp (&x, &y) != MP_LT) {
|
||||
while (mp_cmp(&x, &y) != MP_LT) {
|
||||
++(q.dp[n - t]);
|
||||
if ((res = mp_sub (&x, &y, &x)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&x, &y, &x)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
}
|
||||
|
||||
/* reset y by shifting it back down */
|
||||
mp_rshd (&y, n - t);
|
||||
mp_rshd(&y, n - t);
|
||||
|
||||
/* step 3. for i from n down to (t + 1) */
|
||||
for (i = n; i >= (t + 1); i--) {
|
||||
@ -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) {
|
||||
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})) >
|
||||
@ -212,11 +212,11 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
q.dp[(i - t) - 1] = (q.dp[(i - t) - 1] - 1) & MP_MASK;
|
||||
|
||||
/* find left hand */
|
||||
mp_zero (&t1);
|
||||
mp_zero(&t1);
|
||||
t1.dp[0] = ((t - 1) < 0) ? 0 : y.dp[t - 1];
|
||||
t1.dp[1] = y.dp[t];
|
||||
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;
|
||||
}
|
||||
|
||||
@ -228,27 +228,27 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
if ((res = mp_lshd (&t1, (i - t) - 1)) != MP_OKAY) {
|
||||
if ((res = mp_lshd(&t1, (i - t) - 1)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&x, &t1, &x)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&x, &t1, &x)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
/* if x < 0 then { x = x + y*b**{i-t-1}; q{i-t-1} -= 1; } */
|
||||
if (x.sign == MP_NEG) {
|
||||
if ((res = mp_copy (&y, &t1)) != MP_OKAY) {
|
||||
if ((res = mp_copy(&y, &t1)) != MP_OKAY) {
|
||||
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;
|
||||
}
|
||||
if ((res = mp_add (&x, &t1, &x)) != MP_OKAY) {
|
||||
if ((res = mp_add(&x, &t1, &x)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
@ -264,25 +264,25 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
x.sign = (x.used == 0) ? MP_ZPOS : a->sign;
|
||||
|
||||
if (c != NULL) {
|
||||
mp_clamp (&q);
|
||||
mp_exch (&q, c);
|
||||
mp_clamp(&q);
|
||||
mp_exch(&q, c);
|
||||
c->sign = neg;
|
||||
}
|
||||
|
||||
if (d != NULL) {
|
||||
if ((res = mp_div_2d (&x, norm, &x, NULL)) != MP_OKAY) {
|
||||
if ((res = mp_div_2d(&x, norm, &x, NULL)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
mp_exch (&x, d);
|
||||
mp_exch(&x, d);
|
||||
}
|
||||
|
||||
res = MP_OKAY;
|
||||
|
||||
LBL_Y:mp_clear (&y);
|
||||
LBL_X:mp_clear (&x);
|
||||
LBL_T2:mp_clear (&t2);
|
||||
LBL_T1:mp_clear (&t1);
|
||||
LBL_Q:mp_clear (&q);
|
||||
LBL_Y:mp_clear(&y);
|
||||
LBL_X:mp_clear(&x);
|
||||
LBL_T2:mp_clear(&t2);
|
||||
LBL_T1:mp_clear(&t1);
|
||||
LBL_Q:mp_clear(&q);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -22,7 +22,7 @@ int mp_div_2(mp_int * a, mp_int * b)
|
||||
|
||||
/* copy */
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -58,7 +58,7 @@ int mp_div_2(mp_int * a, mp_int * b)
|
||||
}
|
||||
}
|
||||
b->sign = a->sign;
|
||||
mp_clamp (b);
|
||||
mp_clamp(b);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -23,33 +23,33 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
|
||||
|
||||
/* if the shift count is <= 0 then we do no work */
|
||||
if (b <= 0) {
|
||||
res = mp_copy (a, c);
|
||||
res = mp_copy(a, c);
|
||||
if (d != NULL) {
|
||||
mp_zero (d);
|
||||
mp_zero(d);
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
/* copy */
|
||||
if ((res = mp_copy (a, c)) != MP_OKAY) {
|
||||
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
/* 'a' should not be used after here - it might be the same as d */
|
||||
|
||||
/* get the remainder */
|
||||
if (d != NULL) {
|
||||
if ((res = mp_mod_2d (a, b, d)) != MP_OKAY) {
|
||||
if ((res = mp_mod_2d(a, b, d)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* shift by as many digits in the bit count */
|
||||
if (b >= (int)DIGIT_BIT) {
|
||||
mp_rshd (c, b / DIGIT_BIT);
|
||||
mp_rshd(c, b / DIGIT_BIT);
|
||||
}
|
||||
|
||||
/* shift any bit count < DIGIT_BIT */
|
||||
D = (mp_digit) (b % DIGIT_BIT);
|
||||
D = (mp_digit)(b % DIGIT_BIT);
|
||||
if (D != 0) {
|
||||
mp_digit *tmpc, mask, shift;
|
||||
|
||||
@ -76,7 +76,7 @@ int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
|
||||
r = rr;
|
||||
}
|
||||
}
|
||||
mp_clamp (c);
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -41,7 +41,7 @@ mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
|
||||
|
||||
/* ensure that "x" has at least 2m digits */
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -76,12 +76,12 @@ top:
|
||||
}
|
||||
|
||||
/* clamp, sub and return */
|
||||
mp_clamp (x);
|
||||
mp_clamp(x);
|
||||
|
||||
/* if x >= n then subtract and reduce again
|
||||
* 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) {
|
||||
return err;
|
||||
}
|
||||
|
@ -23,27 +23,27 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
|
||||
mp_int g;
|
||||
|
||||
if ((res = mp_init_copy (&g, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&g, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* set initial result */
|
||||
mp_set (c, 1);
|
||||
mp_set(c, 1);
|
||||
|
||||
if (fast != 0) {
|
||||
while (b > 0) {
|
||||
/* if the bit is set multiply */
|
||||
if ((b & 1) != 0) {
|
||||
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
|
||||
mp_clear (&g);
|
||||
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
||||
mp_clear(&g);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* square */
|
||||
if (b > 1) {
|
||||
if ((res = mp_sqr (&g, &g)) != MP_OKAY) {
|
||||
mp_clear (&g);
|
||||
if ((res = mp_sqr(&g, &g)) != MP_OKAY) {
|
||||
mp_clear(&g);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -55,15 +55,15 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
else {
|
||||
for (x = 0; x < DIGIT_BIT; x++) {
|
||||
/* square */
|
||||
if ((res = mp_sqr (c, c)) != MP_OKAY) {
|
||||
mp_clear (&g);
|
||||
if ((res = mp_sqr(c, c)) != MP_OKAY) {
|
||||
mp_clear(&g);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* if the bit is set multiply */
|
||||
if ((b & (mp_digit) (((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
|
||||
if ((res = mp_mul (c, &g, c)) != MP_OKAY) {
|
||||
mp_clear (&g);
|
||||
if ((b & (mp_digit)(((mp_digit)1) << (DIGIT_BIT - 1))) != 0) {
|
||||
if ((res = mp_mul(c, &g, c)) != MP_OKAY) {
|
||||
mp_clear(&g);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -73,7 +73,7 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
}
|
||||
} /* if ... else */
|
||||
|
||||
mp_clear (&g);
|
||||
mp_clear(&g);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -89,13 +89,13 @@ 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) == MP_YES) || (dr != 0)) {
|
||||
return mp_exptmod_fast (G, X, P, Y, dr);
|
||||
if ((mp_isodd(P) == MP_YES) || (dr != 0)) {
|
||||
return mp_exptmod_fast(G, X, P, Y, dr);
|
||||
} else {
|
||||
#endif
|
||||
#ifdef BN_S_MP_EXPTMOD_C
|
||||
/* 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
|
||||
/* no exptmod for evens */
|
||||
return MP_VAL;
|
||||
|
@ -42,7 +42,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
int (*redux)(mp_int*,mp_int*,mp_digit);
|
||||
|
||||
/* find window size */
|
||||
x = mp_count_bits (X);
|
||||
x = mp_count_bits(X);
|
||||
if (x <= 7) {
|
||||
winsize = 2;
|
||||
} else if (x <= 36) {
|
||||
@ -75,7 +75,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||
if ((err = mp_init_size(&M[x], P->alloc)) != MP_OKAY) {
|
||||
for (y = 1<<(winsize-1); y < x; y++) {
|
||||
mp_clear (&M[y]);
|
||||
mp_clear(&M[y]);
|
||||
}
|
||||
mp_clear(&M[1]);
|
||||
return err;
|
||||
@ -86,7 +86,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
if (redmode == 0) {
|
||||
#ifdef BN_MP_MONTGOMERY_SETUP_C
|
||||
/* now setup montgomery */
|
||||
if ((err = mp_montgomery_setup (P, &mp)) != MP_OKAY) {
|
||||
if ((err = mp_montgomery_setup(P, &mp)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
}
|
||||
#else
|
||||
@ -133,7 +133,7 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
}
|
||||
|
||||
/* setup result */
|
||||
if ((err = mp_init_size (&res, P->alloc)) != MP_OKAY) {
|
||||
if ((err = mp_init_size(&res, P->alloc)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
}
|
||||
|
||||
@ -147,12 +147,12 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
if (redmode == 0) {
|
||||
#ifdef BN_MP_MONTGOMERY_CALC_NORMALIZATION_C
|
||||
/* now we need R mod m */
|
||||
if ((err = mp_montgomery_calc_normalization (&res, P)) != MP_OKAY) {
|
||||
if ((err = mp_montgomery_calc_normalization(&res, P)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
|
||||
/* now set M[1] to G * R mod m */
|
||||
if ((err = mp_mulmod (G, &res, P, &M[1])) != MP_OKAY) {
|
||||
if ((err = mp_mulmod(G, &res, P, &M[1])) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
#else
|
||||
@ -167,25 +167,25 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
}
|
||||
|
||||
/* compute the value at M[1<<(winsize-1)] by squaring M[1] (winsize-1) times */
|
||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
if ((err = mp_copy(&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
|
||||
for (x = 0; x < (winsize - 1); x++) {
|
||||
if ((err = mp_sqr (&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&M[1 << (winsize - 1)], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&M[1 << (winsize - 1)], P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
}
|
||||
|
||||
/* create upper table */
|
||||
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
||||
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
||||
if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&M[x], P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&M[x], P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
}
|
||||
@ -225,10 +225,10 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
|
||||
/* if the bit is zero and mode == 1 then we square */
|
||||
if ((mode == 1) && (y == 0)) {
|
||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&res, &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
continue;
|
||||
@ -242,19 +242,19 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
/* ok window is filled so square as required and multiply */
|
||||
/* square first */
|
||||
for (x = 0; x < winsize; x++) {
|
||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&res, &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
}
|
||||
|
||||
/* then multiply */
|
||||
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
||||
if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
|
||||
@ -269,10 +269,10 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
if ((mode == 2) && (bitcpy > 0)) {
|
||||
/* square then multiply if the bit is set */
|
||||
for (x = 0; x < bitcpy; x++) {
|
||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&res, &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
|
||||
@ -280,10 +280,10 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
bitbuf <<= 1;
|
||||
if ((bitbuf & (1 << winsize)) != 0) {
|
||||
/* then multiply */
|
||||
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
||||
if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, mp)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, mp)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
}
|
||||
@ -303,13 +303,13 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
|
||||
}
|
||||
|
||||
/* swap res with Y */
|
||||
mp_exch (&res, Y);
|
||||
mp_exch(&res, Y);
|
||||
err = MP_OKAY;
|
||||
LBL_RES:mp_clear (&res);
|
||||
LBL_RES:mp_clear(&res);
|
||||
LBL_M:
|
||||
mp_clear(&M[1]);
|
||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||
mp_clear (&M[x]);
|
||||
mp_clear(&M[x]);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
@ -25,24 +25,24 @@ int mp_fwrite(mp_int *a, int radix, FILE *stream)
|
||||
return err;
|
||||
}
|
||||
|
||||
buf = OPT_CAST(char) XMALLOC (len);
|
||||
buf = OPT_CAST(char) XMALLOC(len);
|
||||
if (buf == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
||||
if ((err = mp_toradix(a, buf, radix)) != MP_OKAY) {
|
||||
XFREE (buf);
|
||||
XFREE(buf);
|
||||
return err;
|
||||
}
|
||||
|
||||
for (x = 0; x < len; x++) {
|
||||
if (fputc(buf[x], stream) == EOF) {
|
||||
XFREE (buf);
|
||||
XFREE(buf);
|
||||
return MP_VAL;
|
||||
}
|
||||
}
|
||||
|
||||
XFREE (buf);
|
||||
XFREE(buf);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
18
bn_mp_gcd.c
18
bn_mp_gcd.c
@ -22,19 +22,19 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||
int k, u_lsb, v_lsb, res;
|
||||
|
||||
/* either zero than gcd is the largest */
|
||||
if (mp_iszero (a) == MP_YES) {
|
||||
return mp_abs (b, c);
|
||||
if (mp_iszero(a) == MP_YES) {
|
||||
return mp_abs(b, c);
|
||||
}
|
||||
if (mp_iszero (b) == MP_YES) {
|
||||
return mp_abs (a, c);
|
||||
if (mp_iszero(b) == MP_YES) {
|
||||
return mp_abs(a, c);
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
|
||||
if ((res = mp_init_copy (&v, b)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&v, b)) != MP_OKAY) {
|
||||
goto LBL_U;
|
||||
}
|
||||
|
||||
@ -89,13 +89,13 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
|
||||
/* 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;
|
||||
}
|
||||
c->sign = MP_ZPOS;
|
||||
res = MP_OKAY;
|
||||
LBL_V:mp_clear (&u);
|
||||
LBL_U:mp_clear (&v);
|
||||
LBL_V:mp_clear(&u);
|
||||
LBL_U:mp_clear(&v);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -32,7 +32,7 @@ int mp_grow (mp_int * a, int size)
|
||||
* in case the operation failed we don't want
|
||||
* 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) {
|
||||
/* reallocation failed but "a" is still valid [can be freed] */
|
||||
return MP_MEM;
|
||||
|
@ -21,7 +21,7 @@ int mp_init (mp_int * a)
|
||||
int i;
|
||||
|
||||
/* allocate memory required and clear it */
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * MP_PREC);
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof (mp_digit) * MP_PREC);
|
||||
if (a->dp == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
@ -20,11 +20,11 @@ int mp_init_copy (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = mp_init_size (a, b->used)) != MP_OKAY) {
|
||||
if ((res = mp_init_size(a, b->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if((res = mp_copy (b, a)) != MP_OKAY) {
|
||||
if((res = mp_copy(b, a)) != MP_OKAY) {
|
||||
mp_clear(a);
|
||||
}
|
||||
|
||||
|
@ -24,7 +24,7 @@ int mp_init_size (mp_int * a, int size)
|
||||
size += (MP_PREC * 2) - (size % MP_PREC);
|
||||
|
||||
/* alloc mem */
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC (sizeof (mp_digit) * size);
|
||||
a->dp = OPT_CAST(mp_digit) XMALLOC(sizeof (mp_digit) * size);
|
||||
if (a->dp == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
|
@ -26,7 +26,7 @@ int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
#ifdef BN_FAST_MP_INVMOD_C
|
||||
/* if the modulus is odd we can use a faster routine instead */
|
||||
if ((mp_isodd(b) == MP_YES) && (mp_cmp_d(b, 1) != MP_EQ)) {
|
||||
return fast_mp_invmod (a, b, c);
|
||||
return fast_mp_invmod(a, b, c);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -36,114 +36,114 @@ int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
|
||||
if ((res = mp_mod(a, b, &x)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_copy (b, &y)) != MP_OKAY) {
|
||||
if ((res = mp_copy(b, &y)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
/* 3. u=x, v=y, A=1, B=0, C=0,D=1 */
|
||||
if ((res = mp_copy (&x, &u)) != MP_OKAY) {
|
||||
if ((res = mp_copy(&x, &u)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_copy (&y, &v)) != MP_OKAY) {
|
||||
if ((res = mp_copy(&y, &v)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
mp_set (&A, 1);
|
||||
mp_set (&D, 1);
|
||||
mp_set(&A, 1);
|
||||
mp_set(&D, 1);
|
||||
|
||||
top:
|
||||
/* 4. while u is even do */
|
||||
while (mp_iseven (&u) == MP_YES) {
|
||||
while (mp_iseven(&u) == MP_YES) {
|
||||
/* 4.1 u = u/2 */
|
||||
if ((res = mp_div_2 (&u, &u)) != MP_OKAY) {
|
||||
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) == 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 */
|
||||
if ((res = mp_add (&A, &y, &A)) != MP_OKAY) {
|
||||
if ((res = mp_add(&A, &y, &A)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_sub (&B, &x, &B)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&B, &x, &B)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
/* A = A/2, B = B/2 */
|
||||
if ((res = mp_div_2 (&A, &A)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&A, &A)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_div_2 (&B, &B)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&B, &B)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* 5. while v is even do */
|
||||
while (mp_iseven (&v) == MP_YES) {
|
||||
while (mp_iseven(&v) == MP_YES) {
|
||||
/* 5.1 v = v/2 */
|
||||
if ((res = mp_div_2 (&v, &v)) != MP_OKAY) {
|
||||
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) == 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 */
|
||||
if ((res = mp_add (&C, &y, &C)) != MP_OKAY) {
|
||||
if ((res = mp_add(&C, &y, &C)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_sub (&D, &x, &D)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&D, &x, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
/* C = C/2, D = D/2 */
|
||||
if ((res = mp_div_2 (&C, &C)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&C, &C)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
if ((res = mp_div_2 (&D, &D)) != MP_OKAY) {
|
||||
if ((res = mp_div_2(&D, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* 6. if u >= v then */
|
||||
if (mp_cmp (&u, &v) != MP_LT) {
|
||||
if (mp_cmp(&u, &v) != MP_LT) {
|
||||
/* u = u - v, A = A - C, B = B - D */
|
||||
if ((res = mp_sub (&u, &v, &u)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&u, &v, &u)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&A, &C, &A)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&A, &C, &A)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&B, &D, &B)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&B, &D, &B)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
} else {
|
||||
/* v - v - u, C = C - A, D = D - B */
|
||||
if ((res = mp_sub (&v, &u, &v)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&v, &u, &v)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&C, &A, &C)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&C, &A, &C)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&D, &B, &D)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&D, &B, &D)) != MP_OKAY) {
|
||||
goto LBL_ERR;
|
||||
}
|
||||
}
|
||||
|
||||
/* if not zero goto step 4 */
|
||||
if (mp_iszero (&u) == MP_NO)
|
||||
if (mp_iszero(&u) == MP_NO)
|
||||
goto top;
|
||||
|
||||
/* now a = C, b = D, gcd == g*v */
|
||||
|
||||
/* if v != 1 then there is no inverse */
|
||||
if (mp_cmp_d (&v, 1) != MP_EQ) {
|
||||
if (mp_cmp_d(&v, 1) != MP_EQ) {
|
||||
res = MP_VAL;
|
||||
goto LBL_ERR;
|
||||
}
|
||||
@ -163,9 +163,9 @@ top:
|
||||
}
|
||||
|
||||
/* C is now the inverse */
|
||||
mp_exch (&C, c);
|
||||
mp_exch(&C, c);
|
||||
res = MP_OKAY;
|
||||
LBL_ERR:mp_clear_multi (&x, &y, &u, &v, &A, &B, &C, &D, NULL);
|
||||
LBL_ERR:mp_clear_multi(&x, &y, &u, &v, &A, &B, &C, &D, NULL);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -37,9 +37,9 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
|
||||
}
|
||||
|
||||
/* step 1. handle case of a == 0 */
|
||||
if (mp_iszero (a) == MP_YES) {
|
||||
if (mp_iszero(a) == MP_YES) {
|
||||
/* special case of a == 0 and n == 1 */
|
||||
if (mp_cmp_d (n, 1) == MP_EQ) {
|
||||
if (mp_cmp_d(n, 1) == MP_EQ) {
|
||||
*c = 1;
|
||||
} else {
|
||||
*c = 0;
|
||||
@ -48,7 +48,7 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
|
||||
}
|
||||
|
||||
/* step 2. if a == 1, return 1 */
|
||||
if (mp_cmp_d (a, 1) == MP_EQ) {
|
||||
if (mp_cmp_d(a, 1) == MP_EQ) {
|
||||
*c = 1;
|
||||
return MP_OKAY;
|
||||
}
|
||||
@ -57,11 +57,11 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
|
||||
s = 0;
|
||||
|
||||
/* step 3. write a = a1 * 2**k */
|
||||
if ((res = mp_init_copy (&a1, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&a1, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_init (&p1)) != MP_OKAY) {
|
||||
if ((res = mp_init(&p1)) != MP_OKAY) {
|
||||
goto LBL_A1;
|
||||
}
|
||||
|
||||
@ -91,14 +91,14 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
|
||||
}
|
||||
|
||||
/* if a1 == 1 we're done */
|
||||
if (mp_cmp_d (&a1, 1) == MP_EQ) {
|
||||
if (mp_cmp_d(&a1, 1) == MP_EQ) {
|
||||
*c = s;
|
||||
} else {
|
||||
/* n1 = n mod a1 */
|
||||
if ((res = mp_mod (n, &a1, &p1)) != MP_OKAY) {
|
||||
if ((res = mp_mod(n, &a1, &p1)) != MP_OKAY) {
|
||||
goto LBL_P1;
|
||||
}
|
||||
if ((res = mp_jacobi (&p1, &a1, &r)) != MP_OKAY) {
|
||||
if ((res = mp_jacobi(&p1, &a1, &r)) != MP_OKAY) {
|
||||
goto LBL_P1;
|
||||
}
|
||||
*c = s * r;
|
||||
@ -106,8 +106,8 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
|
||||
|
||||
/* done */
|
||||
res = MP_OKAY;
|
||||
LBL_P1:mp_clear (&p1);
|
||||
LBL_A1:mp_clear (&a1);
|
||||
LBL_P1:mp_clear(&p1);
|
||||
LBL_A1:mp_clear(&a1);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -53,27 +53,27 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
err = MP_MEM;
|
||||
|
||||
/* min # of digits */
|
||||
B = MIN (a->used, b->used);
|
||||
B = MIN(a->used, b->used);
|
||||
|
||||
/* now divide in two */
|
||||
B = B >> 1;
|
||||
|
||||
/* init copy all the temps */
|
||||
if (mp_init_size (&x0, B) != MP_OKAY)
|
||||
if (mp_init_size(&x0, B) != MP_OKAY)
|
||||
goto ERR;
|
||||
if (mp_init_size (&x1, a->used - B) != MP_OKAY)
|
||||
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
||||
goto X0;
|
||||
if (mp_init_size (&y0, B) != MP_OKAY)
|
||||
if (mp_init_size(&y0, B) != MP_OKAY)
|
||||
goto X1;
|
||||
if (mp_init_size (&y1, b->used - B) != MP_OKAY)
|
||||
if (mp_init_size(&y1, b->used - B) != MP_OKAY)
|
||||
goto Y0;
|
||||
|
||||
/* init temps */
|
||||
if (mp_init_size (&t1, B * 2) != MP_OKAY)
|
||||
if (mp_init_size(&t1, B * 2) != MP_OKAY)
|
||||
goto Y1;
|
||||
if (mp_init_size (&x0y0, B * 2) != MP_OKAY)
|
||||
if (mp_init_size(&x0y0, B * 2) != MP_OKAY)
|
||||
goto T1;
|
||||
if (mp_init_size (&x1y1, B * 2) != MP_OKAY)
|
||||
if (mp_init_size(&x1y1, B * 2) != MP_OKAY)
|
||||
goto X0Y0;
|
||||
|
||||
/* now shift the digits */
|
||||
@ -112,51 +112,51 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
/* only need to clamp the lower words since by definition the
|
||||
* upper words x1/y1 must have a known number of digits
|
||||
*/
|
||||
mp_clamp (&x0);
|
||||
mp_clamp (&y0);
|
||||
mp_clamp(&x0);
|
||||
mp_clamp(&y0);
|
||||
|
||||
/* now calc the products x0y0 and x1y1 */
|
||||
/* after this x0 is no longer required, free temp [x0==t2]! */
|
||||
if (mp_mul (&x0, &y0, &x0y0) != MP_OKAY)
|
||||
if (mp_mul(&x0, &y0, &x0y0) != MP_OKAY)
|
||||
goto X1Y1; /* x0y0 = x0*y0 */
|
||||
if (mp_mul (&x1, &y1, &x1y1) != MP_OKAY)
|
||||
if (mp_mul(&x1, &y1, &x1y1) != MP_OKAY)
|
||||
goto X1Y1; /* x1y1 = x1*y1 */
|
||||
|
||||
/* now calc x1+x0 and y1+y0 */
|
||||
if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
|
||||
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
|
||||
goto X1Y1; /* t1 = x1 - x0 */
|
||||
if (s_mp_add (&y1, &y0, &x0) != MP_OKAY)
|
||||
if (s_mp_add(&y1, &y0, &x0) != MP_OKAY)
|
||||
goto X1Y1; /* t2 = y1 - y0 */
|
||||
if (mp_mul (&t1, &x0, &t1) != MP_OKAY)
|
||||
if (mp_mul(&t1, &x0, &t1) != MP_OKAY)
|
||||
goto X1Y1; /* t1 = (x1 + x0) * (y1 + y0) */
|
||||
|
||||
/* add x0y0 */
|
||||
if (mp_add (&x0y0, &x1y1, &x0) != MP_OKAY)
|
||||
if (mp_add(&x0y0, &x1y1, &x0) != MP_OKAY)
|
||||
goto X1Y1; /* t2 = x0y0 + x1y1 */
|
||||
if (s_mp_sub (&t1, &x0, &t1) != MP_OKAY)
|
||||
if (s_mp_sub(&t1, &x0, &t1) != MP_OKAY)
|
||||
goto X1Y1; /* t1 = (x1+x0)*(y1+y0) - (x1y1 + x0y0) */
|
||||
|
||||
/* shift by B */
|
||||
if (mp_lshd (&t1, B) != MP_OKAY)
|
||||
if (mp_lshd(&t1, B) != MP_OKAY)
|
||||
goto X1Y1; /* t1 = (x0y0 + x1y1 - (x1-x0)*(y1-y0))<<B */
|
||||
if (mp_lshd (&x1y1, B * 2) != MP_OKAY)
|
||||
if (mp_lshd(&x1y1, B * 2) != MP_OKAY)
|
||||
goto X1Y1; /* x1y1 = x1y1 << 2*B */
|
||||
|
||||
if (mp_add (&x0y0, &t1, &t1) != MP_OKAY)
|
||||
if (mp_add(&x0y0, &t1, &t1) != MP_OKAY)
|
||||
goto X1Y1; /* t1 = x0y0 + t1 */
|
||||
if (mp_add (&t1, &x1y1, c) != MP_OKAY)
|
||||
if (mp_add(&t1, &x1y1, c) != MP_OKAY)
|
||||
goto X1Y1; /* t1 = x0y0 + t1 + x1y1 */
|
||||
|
||||
/* Algorithm succeeded set the return code to MP_OKAY */
|
||||
err = MP_OKAY;
|
||||
|
||||
X1Y1:mp_clear (&x1y1);
|
||||
X0Y0:mp_clear (&x0y0);
|
||||
T1:mp_clear (&t1);
|
||||
Y1:mp_clear (&y1);
|
||||
Y0:mp_clear (&y0);
|
||||
X1:mp_clear (&x1);
|
||||
X0:mp_clear (&x0);
|
||||
X1Y1:mp_clear(&x1y1);
|
||||
X0Y0:mp_clear(&x0y0);
|
||||
T1:mp_clear(&t1);
|
||||
Y1:mp_clear(&y1);
|
||||
Y0:mp_clear(&y0);
|
||||
X1:mp_clear(&x1);
|
||||
X0:mp_clear(&x0);
|
||||
ERR:
|
||||
return err;
|
||||
}
|
||||
|
@ -36,19 +36,19 @@ int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
||||
B = B >> 1;
|
||||
|
||||
/* init copy all the temps */
|
||||
if (mp_init_size (&x0, B) != MP_OKAY)
|
||||
if (mp_init_size(&x0, B) != MP_OKAY)
|
||||
goto ERR;
|
||||
if (mp_init_size (&x1, a->used - B) != MP_OKAY)
|
||||
if (mp_init_size(&x1, a->used - B) != MP_OKAY)
|
||||
goto X0;
|
||||
|
||||
/* init temps */
|
||||
if (mp_init_size (&t1, a->used * 2) != MP_OKAY)
|
||||
if (mp_init_size(&t1, a->used * 2) != MP_OKAY)
|
||||
goto X1;
|
||||
if (mp_init_size (&t2, a->used * 2) != MP_OKAY)
|
||||
if (mp_init_size(&t2, a->used * 2) != MP_OKAY)
|
||||
goto T1;
|
||||
if (mp_init_size (&x0x0, B * 2) != MP_OKAY)
|
||||
if (mp_init_size(&x0x0, B * 2) != MP_OKAY)
|
||||
goto T2;
|
||||
if (mp_init_size (&x1x1, (a->used - B) * 2) != MP_OKAY)
|
||||
if (mp_init_size(&x1x1, (a->used - B) * 2) != MP_OKAY)
|
||||
goto X0X0;
|
||||
|
||||
{
|
||||
@ -72,45 +72,45 @@ int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
||||
x0.used = B;
|
||||
x1.used = a->used - B;
|
||||
|
||||
mp_clamp (&x0);
|
||||
mp_clamp(&x0);
|
||||
|
||||
/* now calc the products x0*x0 and x1*x1 */
|
||||
if (mp_sqr (&x0, &x0x0) != MP_OKAY)
|
||||
if (mp_sqr(&x0, &x0x0) != MP_OKAY)
|
||||
goto X1X1; /* x0x0 = x0*x0 */
|
||||
if (mp_sqr (&x1, &x1x1) != MP_OKAY)
|
||||
if (mp_sqr(&x1, &x1x1) != MP_OKAY)
|
||||
goto X1X1; /* x1x1 = x1*x1 */
|
||||
|
||||
/* now calc (x1+x0)**2 */
|
||||
if (s_mp_add (&x1, &x0, &t1) != MP_OKAY)
|
||||
if (s_mp_add(&x1, &x0, &t1) != MP_OKAY)
|
||||
goto X1X1; /* t1 = x1 - x0 */
|
||||
if (mp_sqr (&t1, &t1) != MP_OKAY)
|
||||
if (mp_sqr(&t1, &t1) != MP_OKAY)
|
||||
goto X1X1; /* t1 = (x1 - x0) * (x1 - x0) */
|
||||
|
||||
/* add x0y0 */
|
||||
if (s_mp_add (&x0x0, &x1x1, &t2) != MP_OKAY)
|
||||
if (s_mp_add(&x0x0, &x1x1, &t2) != MP_OKAY)
|
||||
goto X1X1; /* t2 = x0x0 + x1x1 */
|
||||
if (s_mp_sub (&t1, &t2, &t1) != MP_OKAY)
|
||||
if (s_mp_sub(&t1, &t2, &t1) != MP_OKAY)
|
||||
goto X1X1; /* t1 = (x1+x0)**2 - (x0x0 + x1x1) */
|
||||
|
||||
/* shift by B */
|
||||
if (mp_lshd (&t1, B) != MP_OKAY)
|
||||
if (mp_lshd(&t1, B) != MP_OKAY)
|
||||
goto X1X1; /* t1 = (x0x0 + x1x1 - (x1-x0)*(x1-x0))<<B */
|
||||
if (mp_lshd (&x1x1, B * 2) != MP_OKAY)
|
||||
if (mp_lshd(&x1x1, B * 2) != MP_OKAY)
|
||||
goto X1X1; /* x1x1 = x1x1 << 2*B */
|
||||
|
||||
if (mp_add (&x0x0, &t1, &t1) != MP_OKAY)
|
||||
if (mp_add(&x0x0, &t1, &t1) != MP_OKAY)
|
||||
goto X1X1; /* t1 = x0x0 + t1 */
|
||||
if (mp_add (&t1, &x1x1, b) != MP_OKAY)
|
||||
if (mp_add(&t1, &x1x1, b) != MP_OKAY)
|
||||
goto X1X1; /* t1 = x0x0 + t1 + x1x1 */
|
||||
|
||||
err = MP_OKAY;
|
||||
|
||||
X1X1:mp_clear (&x1x1);
|
||||
X0X0:mp_clear (&x0x0);
|
||||
T2:mp_clear (&t2);
|
||||
T1:mp_clear (&t1);
|
||||
X1:mp_clear (&x1);
|
||||
X0:mp_clear (&x0);
|
||||
X1X1:mp_clear(&x1x1);
|
||||
X0X0:mp_clear(&x0x0);
|
||||
T2:mp_clear(&t2);
|
||||
T1:mp_clear(&t1);
|
||||
X1:mp_clear(&x1);
|
||||
X0:mp_clear(&x0);
|
||||
ERR:
|
||||
return err;
|
||||
}
|
||||
|
@ -22,12 +22,12 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_int t1, t2;
|
||||
|
||||
|
||||
if ((res = mp_init_multi (&t1, &t2, NULL)) != MP_OKAY) {
|
||||
if ((res = mp_init_multi(&t1, &t2, NULL)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* t1 = get the GCD of the two inputs */
|
||||
if ((res = mp_gcd (a, b, &t1)) != MP_OKAY) {
|
||||
if ((res = mp_gcd(a, b, &t1)) != MP_OKAY) {
|
||||
goto LBL_T;
|
||||
}
|
||||
|
||||
@ -50,7 +50,7 @@ int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
|
||||
c->sign = MP_ZPOS;
|
||||
|
||||
LBL_T:
|
||||
mp_clear_multi (&t1, &t2, NULL);
|
||||
mp_clear_multi(&t1, &t2, NULL);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -27,7 +27,7 @@ int mp_lshd (mp_int * a, int b)
|
||||
|
||||
/* grow to fit the new digits */
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
12
bn_mp_mod.c
12
bn_mp_mod.c
@ -22,23 +22,23 @@ mp_mod (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_int t;
|
||||
int res;
|
||||
|
||||
if ((res = mp_init_size (&t, b->used)) != MP_OKAY) {
|
||||
if ((res = mp_init_size(&t, b->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_div (a, b, NULL, &t)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_div(a, b, NULL, &t)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((mp_iszero(&t) != MP_NO) || (t.sign == b->sign)) {
|
||||
res = MP_OKAY;
|
||||
mp_exch (&t, c);
|
||||
mp_exch(&t, c);
|
||||
} else {
|
||||
res = mp_add (b, &t, c);
|
||||
res = mp_add(b, &t, c);
|
||||
}
|
||||
|
||||
mp_clear (&t);
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -23,18 +23,18 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
||||
|
||||
/* if b is <= 0 then zero the int */
|
||||
if (b <= 0) {
|
||||
mp_zero (c);
|
||||
mp_zero(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
/* if the modulus is larger than the value than return */
|
||||
if (b >= (int) (a->used * DIGIT_BIT)) {
|
||||
res = mp_copy (a, c);
|
||||
res = mp_copy(a, c);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* copy */
|
||||
if ((res = mp_copy (a, c)) != MP_OKAY) {
|
||||
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -44,8 +44,8 @@ mp_mod_2d (mp_int * a, int b, mp_int * c)
|
||||
}
|
||||
/* clear the digit that is not completely outside/inside the modulus */
|
||||
c->dp[b / DIGIT_BIT] &=
|
||||
(mp_digit) ((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
|
||||
mp_clamp (c);
|
||||
(mp_digit)((((mp_digit) 1) << (((mp_digit) b) % DIGIT_BIT)) - ((mp_digit) 1));
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -26,10 +26,10 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
||||
int x, bits, res;
|
||||
|
||||
/* how many bits of last digit does b use */
|
||||
bits = mp_count_bits (b) % DIGIT_BIT;
|
||||
bits = mp_count_bits(b) % DIGIT_BIT;
|
||||
|
||||
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;
|
||||
}
|
||||
} else {
|
||||
@ -40,11 +40,11 @@ int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
|
||||
|
||||
/* 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) {
|
||||
if ((res = mp_mul_2(a, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
if (mp_cmp_mag (a, b) != MP_LT) {
|
||||
if ((res = s_mp_sub (a, b, a)) != MP_OKAY) {
|
||||
if (mp_cmp_mag(a, b) != MP_LT) {
|
||||
if ((res = s_mp_sub(a, b, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@ -32,12 +32,12 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
if ((digs < MP_WARRAY) &&
|
||||
(n->used <
|
||||
(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);
|
||||
}
|
||||
|
||||
/* grow the input as required */
|
||||
if (x->alloc < digs) {
|
||||
if ((res = mp_grow (x, digs)) != MP_OKAY) {
|
||||
if ((res = mp_grow(x, digs)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -52,7 +52,7 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
* following inner loop to reduce the
|
||||
* 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 */
|
||||
{
|
||||
@ -102,11 +102,11 @@ mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
|
||||
|
||||
/* x = x/b**n.used */
|
||||
mp_clamp(x);
|
||||
mp_rshd (x, n->used);
|
||||
mp_rshd(x, n->used);
|
||||
|
||||
/* if x >= n then x = x - n */
|
||||
if (mp_cmp_mag (x, n) != MP_LT) {
|
||||
return s_mp_sub (x, n, x);
|
||||
if (mp_cmp_mag(x, n) != MP_LT) {
|
||||
return s_mp_sub(x, n, x);
|
||||
}
|
||||
|
||||
return MP_OKAY;
|
||||
|
10
bn_mp_mul.c
10
bn_mp_mul.c
@ -23,14 +23,14 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
||||
/* use Toom-Cook? */
|
||||
#ifdef BN_MP_TOOM_MUL_C
|
||||
if (MIN (a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||
if (MIN(a->used, b->used) >= TOOM_MUL_CUTOFF) {
|
||||
res = mp_toom_mul(a, b, c);
|
||||
} else
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_MUL_C
|
||||
/* use Karatsuba? */
|
||||
if (MIN (a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
||||
res = mp_karatsuba_mul (a, b, c);
|
||||
if (MIN(a->used, b->used) >= KARATSUBA_MUL_CUTOFF) {
|
||||
res = mp_karatsuba_mul(a, b, c);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
@ -46,12 +46,12 @@ int mp_mul (mp_int * a, mp_int * b, mp_int * c)
|
||||
if ((digs < MP_WARRAY) &&
|
||||
(MIN(a->used, b->used) <=
|
||||
(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
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_S_MP_MUL_DIGS_C
|
||||
res = s_mp_mul (a, b, c); /* uses s_mp_mul_digs */
|
||||
res = s_mp_mul(a, b, c); /* uses s_mp_mul_digs */
|
||||
#else
|
||||
res = MP_VAL;
|
||||
#endif
|
||||
|
@ -22,7 +22,7 @@ int mp_mul_2(mp_int * a, mp_int * b)
|
||||
|
||||
/* grow to accomodate result */
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
@ -23,26 +23,26 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
||||
|
||||
/* copy */
|
||||
if (a != c) {
|
||||
if ((res = mp_copy (a, c)) != MP_OKAY) {
|
||||
if ((res = mp_copy(a, c)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
/* shift by as many digits in the bit count */
|
||||
if (b >= (int)DIGIT_BIT) {
|
||||
if ((res = mp_lshd (c, b / DIGIT_BIT)) != MP_OKAY) {
|
||||
if ((res = mp_lshd(c, b / DIGIT_BIT)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
||||
/* shift any bit count < DIGIT_BIT */
|
||||
d = (mp_digit) (b % DIGIT_BIT);
|
||||
d = (mp_digit)(b % DIGIT_BIT);
|
||||
if (d != 0) {
|
||||
mp_digit *tmpc, shift, mask, r, rr;
|
||||
int x;
|
||||
@ -75,7 +75,7 @@ int mp_mul_2d (mp_int * a, int b, mp_int * c)
|
||||
c->dp[(c->used)++] = r;
|
||||
}
|
||||
}
|
||||
mp_clamp (c);
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -25,7 +25,7 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
|
||||
|
||||
/* make sure c is big enough to hold a*b */
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -51,10 +51,10 @@ mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
|
||||
r = (mp_word)u + ((mp_word)*tmpa++ * (mp_word)b);
|
||||
|
||||
/* 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));
|
||||
|
||||
/* send carry into next iteration */
|
||||
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
|
||||
u = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
||||
}
|
||||
|
||||
/* store final carry [if any] and increment ix offset */
|
||||
|
@ -21,16 +21,16 @@ int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
int res;
|
||||
mp_int t;
|
||||
|
||||
if ((res = mp_init_size (&t, c->used)) != MP_OKAY) {
|
||||
if ((res = mp_init_size(&t, c->used)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_mul (a, b, &t)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_mul(a, b, &t)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
res = mp_mod (&t, c, d);
|
||||
mp_clear (&t);
|
||||
res = mp_mod(&t, c, d);
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -35,15 +35,15 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
return MP_VAL;
|
||||
}
|
||||
|
||||
if ((res = mp_init (&t1)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t1)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_init (&t2)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t2)) != MP_OKAY) {
|
||||
goto LBL_T1;
|
||||
}
|
||||
|
||||
if ((res = mp_init (&t3)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t3)) != MP_OKAY) {
|
||||
goto LBL_T2;
|
||||
}
|
||||
|
||||
@ -52,56 +52,56 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
a->sign = MP_ZPOS;
|
||||
|
||||
/* t2 = 2 */
|
||||
mp_set (&t2, 2);
|
||||
mp_set(&t2, 2);
|
||||
|
||||
do {
|
||||
/* t1 = t2 */
|
||||
if ((res = mp_copy (&t2, &t1)) != MP_OKAY) {
|
||||
if ((res = mp_copy(&t2, &t1)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* t2 = t1 - ((t1**b - a) / (b * t1**(b-1))) */
|
||||
|
||||
/* t3 = t1**(b-1) */
|
||||
if ((res = mp_expt_d_ex (&t1, b - 1, &t3, fast)) != MP_OKAY) {
|
||||
if ((res = mp_expt_d_ex(&t1, b - 1, &t3, fast)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* numerator */
|
||||
/* t2 = t1**b */
|
||||
if ((res = mp_mul (&t3, &t1, &t2)) != MP_OKAY) {
|
||||
if ((res = mp_mul(&t3, &t1, &t2)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* t2 = t1**b - a */
|
||||
if ((res = mp_sub (&t2, a, &t2)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&t2, a, &t2)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* denominator */
|
||||
/* t3 = t1**(b-1) * b */
|
||||
if ((res = mp_mul_d (&t3, b, &t3)) != MP_OKAY) {
|
||||
if ((res = mp_mul_d(&t3, b, &t3)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
/* t3 = (t1**b - a)/(b * t1**(b-1)) */
|
||||
if ((res = mp_div (&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
||||
if ((res = mp_div(&t2, &t3, &t3, NULL)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (&t1, &t3, &t2)) != MP_OKAY) {
|
||||
if ((res = mp_sub(&t1, &t3, &t2)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
} while (mp_cmp (&t1, &t2) != MP_EQ);
|
||||
} while (mp_cmp(&t1, &t2) != MP_EQ);
|
||||
|
||||
/* result can be off by a few so check */
|
||||
for (;;) {
|
||||
if ((res = mp_expt_d_ex (&t1, b, &t2, fast)) != MP_OKAY) {
|
||||
if ((res = mp_expt_d_ex(&t1, b, &t2, fast)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
|
||||
if (mp_cmp (&t2, a) == MP_GT) {
|
||||
if ((res = mp_sub_d (&t1, 1, &t1)) != MP_OKAY) {
|
||||
if (mp_cmp(&t2, a) == MP_GT) {
|
||||
if ((res = mp_sub_d(&t1, 1, &t1)) != MP_OKAY) {
|
||||
goto LBL_T3;
|
||||
}
|
||||
} else {
|
||||
@ -113,16 +113,16 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
a->sign = neg;
|
||||
|
||||
/* set the result */
|
||||
mp_exch (&t1, c);
|
||||
mp_exch(&t1, c);
|
||||
|
||||
/* set the sign of the result */
|
||||
c->sign = neg;
|
||||
|
||||
res = MP_OKAY;
|
||||
|
||||
LBL_T3:mp_clear (&t3);
|
||||
LBL_T2:mp_clear (&t2);
|
||||
LBL_T1:mp_clear (&t1);
|
||||
LBL_T3:mp_clear(&t3);
|
||||
LBL_T2:mp_clear(&t2);
|
||||
LBL_T1:mp_clear(&t1);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -20,7 +20,7 @@ int mp_neg (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
if (a != b) {
|
||||
if ((res = mp_copy (a, b)) != MP_OKAY) {
|
||||
if ((res = mp_copy(a, b)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
10
bn_mp_or.c
10
bn_mp_or.c
@ -22,13 +22,13 @@ int mp_or (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_int t, *x;
|
||||
|
||||
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;
|
||||
}
|
||||
px = b->used;
|
||||
x = b;
|
||||
} else {
|
||||
if ((res = mp_init_copy (&t, b)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
px = a->used;
|
||||
@ -38,9 +38,9 @@ int mp_or (mp_int * a, mp_int * b, mp_int * c)
|
||||
for (ix = 0; ix < px; ix++) {
|
||||
t.dp[ix] |= x->dp[ix];
|
||||
}
|
||||
mp_clamp (&t);
|
||||
mp_exch (c, &t);
|
||||
mp_clear (&t);
|
||||
mp_clamp(&t);
|
||||
mp_exch(c, &t);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -37,22 +37,22 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
||||
}
|
||||
|
||||
/* init t */
|
||||
if ((err = mp_init (&t)) != MP_OKAY) {
|
||||
if ((err = mp_init(&t)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
/* compute t = b**a mod a */
|
||||
if ((err = mp_exptmod (b, a, a, &t)) != MP_OKAY) {
|
||||
if ((err = mp_exptmod(b, a, a, &t)) != MP_OKAY) {
|
||||
goto LBL_T;
|
||||
}
|
||||
|
||||
/* is it equal to b? */
|
||||
if (mp_cmp (&t, b) == MP_EQ) {
|
||||
if (mp_cmp(&t, b) == MP_EQ) {
|
||||
*result = MP_YES;
|
||||
}
|
||||
|
||||
err = MP_OKAY;
|
||||
LBL_T:mp_clear (&t);
|
||||
LBL_T:mp_clear(&t);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -30,7 +30,7 @@ int mp_prime_is_divisible (mp_int * a, int *result)
|
||||
|
||||
for (ix = 0; ix < PRIME_SIZE; ix++) {
|
||||
/* what is a mod LBL_prime_tab[ix] */
|
||||
if ((err = mp_mod_d (a, ltm_prime_tab[ix], &res)) != MP_OKAY) {
|
||||
if ((err = mp_mod_d(a, ltm_prime_tab[ix], &res)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
|
@ -44,7 +44,7 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
||||
}
|
||||
|
||||
/* first perform trial division */
|
||||
if ((err = mp_prime_is_divisible (a, &res)) != MP_OKAY) {
|
||||
if ((err = mp_prime_is_divisible(a, &res)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
@ -54,15 +54,15 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
||||
}
|
||||
|
||||
/* now perform the miller-rabin rounds */
|
||||
if ((err = mp_init (&b)) != MP_OKAY) {
|
||||
if ((err = mp_init(&b)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
|
||||
for (ix = 0; ix < t; ix++) {
|
||||
/* set the prime */
|
||||
mp_set (&b, ltm_prime_tab[ix]);
|
||||
mp_set(&b, ltm_prime_tab[ix]);
|
||||
|
||||
if ((err = mp_prime_miller_rabin (a, &b, &res)) != MP_OKAY) {
|
||||
if ((err = mp_prime_miller_rabin(a, &b, &res)) != MP_OKAY) {
|
||||
goto LBL_B;
|
||||
}
|
||||
|
||||
@ -73,7 +73,7 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
|
||||
|
||||
/* passed the test */
|
||||
*result = MP_YES;
|
||||
LBL_B:mp_clear (&b);
|
||||
LBL_B:mp_clear(&b);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -36,15 +36,15 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
}
|
||||
|
||||
/* get n1 = a - 1 */
|
||||
if ((err = mp_init_copy (&n1, a)) != MP_OKAY) {
|
||||
if ((err = mp_init_copy(&n1, a)) != MP_OKAY) {
|
||||
return err;
|
||||
}
|
||||
if ((err = mp_sub_d (&n1, 1, &n1)) != MP_OKAY) {
|
||||
if ((err = mp_sub_d(&n1, 1, &n1)) != MP_OKAY) {
|
||||
goto LBL_N1;
|
||||
}
|
||||
|
||||
/* set 2**s * r = n1 */
|
||||
if ((err = mp_init_copy (&r, &n1)) != MP_OKAY) {
|
||||
if ((err = mp_init_copy(&r, &n1)) != MP_OKAY) {
|
||||
goto LBL_N1;
|
||||
}
|
||||
|
||||
@ -54,29 +54,29 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
s = mp_cnt_lsb(&r);
|
||||
|
||||
/* now divide n - 1 by 2**s */
|
||||
if ((err = mp_div_2d (&r, s, &r, NULL)) != MP_OKAY) {
|
||||
if ((err = mp_div_2d(&r, s, &r, NULL)) != MP_OKAY) {
|
||||
goto LBL_R;
|
||||
}
|
||||
|
||||
/* compute y = b**r mod a */
|
||||
if ((err = mp_init (&y)) != MP_OKAY) {
|
||||
if ((err = mp_init(&y)) != MP_OKAY) {
|
||||
goto LBL_R;
|
||||
}
|
||||
if ((err = mp_exptmod (b, &r, a, &y)) != MP_OKAY) {
|
||||
if ((err = mp_exptmod(b, &r, a, &y)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
/* 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;
|
||||
/* while j <= s-1 and y != n1 */
|
||||
while ((j <= (s - 1)) && (mp_cmp (&y, &n1) != MP_EQ)) {
|
||||
if ((err = mp_sqrmod (&y, a, &y)) != MP_OKAY) {
|
||||
while ((j <= (s - 1)) && (mp_cmp(&y, &n1) != MP_EQ)) {
|
||||
if ((err = mp_sqrmod(&y, a, &y)) != MP_OKAY) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
/* if y == 1 then composite */
|
||||
if (mp_cmp_d (&y, 1) == MP_EQ) {
|
||||
if (mp_cmp_d(&y, 1) == MP_EQ) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
|
||||
@ -84,16 +84,16 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
}
|
||||
|
||||
/* if y != n1 then composite */
|
||||
if (mp_cmp (&y, &n1) != MP_EQ) {
|
||||
if (mp_cmp(&y, &n1) != MP_EQ) {
|
||||
goto LBL_Y;
|
||||
}
|
||||
}
|
||||
|
||||
/* probably prime now */
|
||||
*result = MP_YES;
|
||||
LBL_Y:mp_clear (&y);
|
||||
LBL_R:mp_clear (&r);
|
||||
LBL_N1:mp_clear (&n1);
|
||||
LBL_Y:mp_clear(&y);
|
||||
LBL_R:mp_clear(&r);
|
||||
LBL_N1:mp_clear(&n1);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -36,7 +36,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
|
||||
|
||||
/* special case for binary */
|
||||
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;
|
||||
}
|
||||
|
||||
@ -49,7 +49,7 @@ int mp_radix_size (mp_int * a, int radix, int *size)
|
||||
}
|
||||
|
||||
/* init a copy of the input */
|
||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -57,14 +57,14 @@ int mp_radix_size (mp_int * a, int radix, int *size)
|
||||
t.sign = MP_ZPOS;
|
||||
|
||||
/* fetch out all of the digits */
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
while (mp_iszero(&t) == MP_NO) {
|
||||
if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
++digs;
|
||||
}
|
||||
mp_clear (&t);
|
||||
mp_clear(&t);
|
||||
|
||||
/* return digs + 1, the 1 is for the NULL byte that would be required. */
|
||||
*size = digs + 1;
|
||||
|
@ -47,7 +47,7 @@ mp_rand (mp_int * a, int digits)
|
||||
int res;
|
||||
mp_digit d;
|
||||
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
if (digits <= 0) {
|
||||
return MP_OKAY;
|
||||
}
|
||||
@ -57,16 +57,16 @@ mp_rand (mp_int * a, int digits)
|
||||
d = s_gen_random();
|
||||
} while (d == 0);
|
||||
|
||||
if ((res = mp_add_d (a, d, a)) != MP_OKAY) {
|
||||
if ((res = mp_add_d(a, d, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
while (--digits > 0) {
|
||||
if ((res = mp_lshd (a, 1)) != MP_OKAY) {
|
||||
if ((res = mp_lshd(a, 1)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_add_d (a, s_gen_random(), a)) != MP_OKAY) {
|
||||
if ((res = mp_add_d(a, s_gen_random(), a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
|
@ -40,7 +40,7 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
}
|
||||
|
||||
/* set the integer to the default of zero */
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
|
||||
/* process each digit of the string */
|
||||
while (*str != '\0') {
|
||||
@ -60,10 +60,10 @@ int mp_read_radix (mp_int * a, const char *str, int radix)
|
||||
* to the number, otherwise exit the loop.
|
||||
*/
|
||||
if (y < radix) {
|
||||
if ((res = mp_mul_d (a, (mp_digit) radix, a)) != MP_OKAY) {
|
||||
if ((res = mp_mul_d(a, (mp_digit)radix, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
if ((res = mp_add_d (a, (mp_digit) y, a)) != MP_OKAY) {
|
||||
if ((res = mp_add_d(a, (mp_digit)y, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
} else {
|
||||
|
@ -21,7 +21,7 @@ int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c)
|
||||
int res;
|
||||
|
||||
/* read magnitude */
|
||||
if ((res = mp_read_unsigned_bin (a, b + 1, c - 1)) != MP_OKAY) {
|
||||
if ((res = mp_read_unsigned_bin(a, b + 1, c - 1)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -28,11 +28,11 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
|
||||
}
|
||||
|
||||
/* zero the int */
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
|
||||
/* read the bytes in */
|
||||
while (c-- > 0) {
|
||||
if ((res = mp_mul_2d (a, 8, a)) != MP_OKAY) {
|
||||
if ((res = mp_mul_2d(a, 8, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -45,7 +45,7 @@ int mp_read_unsigned_bin (mp_int * a, const unsigned char *b, int c)
|
||||
a->used += 2;
|
||||
#endif
|
||||
}
|
||||
mp_clamp (a);
|
||||
mp_clamp(a);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -25,25 +25,25 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
int res, um = m->used;
|
||||
|
||||
/* q = x */
|
||||
if ((res = mp_init_copy (&q, x)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&q, x)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
/* q1 = x / b**(k-1) */
|
||||
mp_rshd (&q, um - 1);
|
||||
mp_rshd(&q, um - 1);
|
||||
|
||||
/* according to HAC this optimization is ok */
|
||||
if (((mp_digit) um) > (((mp_digit)1) << (DIGIT_BIT - 1))) {
|
||||
if ((res = mp_mul (&q, mu, &q)) != MP_OKAY) {
|
||||
if ((res = mp_mul(&q, mu, &q)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
} else {
|
||||
#ifdef BN_S_MP_MUL_HIGH_DIGS_C
|
||||
if ((res = s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
|
||||
if ((res = s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
#elif defined(BN_FAST_S_MP_MUL_HIGH_DIGS_C)
|
||||
if ((res = fast_s_mp_mul_high_digs (&q, mu, &q, um)) != MP_OKAY) {
|
||||
if ((res = fast_s_mp_mul_high_digs(&q, mu, &q, um)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
#else
|
||||
@ -55,41 +55,41 @@ int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
|
||||
}
|
||||
|
||||
/* q3 = q2 / b**(k+1) */
|
||||
mp_rshd (&q, um + 1);
|
||||
mp_rshd(&q, um + 1);
|
||||
|
||||
/* x = x mod b**(k+1), quick (no division) */
|
||||
if ((res = mp_mod_2d (x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
|
||||
if ((res = mp_mod_2d(x, DIGIT_BIT * (um + 1), x)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
/* q = q * m mod b**(k+1), quick (no division) */
|
||||
if ((res = s_mp_mul_digs (&q, m, &q, um + 1)) != MP_OKAY) {
|
||||
if ((res = s_mp_mul_digs(&q, m, &q, um + 1)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
/* x = x - q */
|
||||
if ((res = mp_sub (x, &q, x)) != MP_OKAY) {
|
||||
if ((res = mp_sub(x, &q, x)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
/* If x < 0, add b**(k+1) to it */
|
||||
if (mp_cmp_d (x, 0) == MP_LT) {
|
||||
mp_set (&q, 1);
|
||||
if ((res = mp_lshd (&q, um + 1)) != MP_OKAY)
|
||||
if (mp_cmp_d(x, 0) == MP_LT) {
|
||||
mp_set(&q, 1);
|
||||
if ((res = mp_lshd(&q, um + 1)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
if ((res = mp_add (x, &q, x)) != MP_OKAY)
|
||||
if ((res = mp_add(x, &q, x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
/* Back off if it's too big */
|
||||
while (mp_cmp (x, m) != MP_LT) {
|
||||
if ((res = s_mp_sub (x, m, x)) != MP_OKAY) {
|
||||
while (mp_cmp(x, m) != MP_LT) {
|
||||
if ((res = s_mp_sub(x, m, x)) != MP_OKAY) {
|
||||
goto CLEANUP;
|
||||
}
|
||||
}
|
||||
|
||||
CLEANUP:
|
||||
mp_clear (&q);
|
||||
mp_clear(&q);
|
||||
|
||||
return res;
|
||||
}
|
||||
|
@ -22,10 +22,10 @@ int mp_reduce_setup (mp_int * a, mp_int * b)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = mp_2expt (a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
|
||||
if ((res = mp_2expt(a, b->used * 2 * DIGIT_BIT)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
return mp_div (a, b, a, NULL);
|
||||
return mp_div(a, b, a, NULL);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -27,7 +27,7 @@ void mp_rshd (mp_int * a, int b)
|
||||
|
||||
/* if b > used then simply zero it and return */
|
||||
if (a->used <= b) {
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
/* set to a digit */
|
||||
void mp_set (mp_int * a, mp_digit b)
|
||||
{
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
a->dp[0] = b & MP_MASK;
|
||||
a->used = (a->dp[0] != 0) ? 1 : 0;
|
||||
}
|
||||
|
@ -20,12 +20,12 @@ int mp_set_int (mp_int * a, unsigned long b)
|
||||
{
|
||||
int x, res;
|
||||
|
||||
mp_zero (a);
|
||||
mp_zero(a);
|
||||
|
||||
/* set four bits at a time */
|
||||
for (x = 0; x < 8; x++) {
|
||||
/* 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;
|
||||
}
|
||||
|
||||
@ -38,7 +38,7 @@ int mp_set_int (mp_int * a, unsigned long b)
|
||||
/* ensure that digits are not clamped off */
|
||||
a->used += 1;
|
||||
}
|
||||
mp_clamp (a);
|
||||
mp_clamp(a);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -26,7 +26,7 @@ int mp_shrink (mp_int * a)
|
||||
}
|
||||
|
||||
if (a->alloc != used) {
|
||||
if ((tmp = OPT_CAST(mp_digit) XREALLOC (a->dp, sizeof (mp_digit) * used)) == NULL) {
|
||||
if ((tmp = OPT_CAST(mp_digit) XREALLOC(a->dp, sizeof (mp_digit) * used)) == NULL) {
|
||||
return MP_MEM;
|
||||
}
|
||||
a->dp = tmp;
|
||||
|
@ -18,7 +18,7 @@
|
||||
/* get the size for an signed equivalent */
|
||||
int mp_signed_bin_size (mp_int * a)
|
||||
{
|
||||
return 1 + mp_unsigned_bin_size (a);
|
||||
return 1 + mp_unsigned_bin_size(a);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
@ -30,7 +30,7 @@ mp_sqr (mp_int * a, mp_int * b)
|
||||
#endif
|
||||
#ifdef BN_MP_KARATSUBA_SQR_C
|
||||
if (a->used >= KARATSUBA_SQR_CUTOFF) {
|
||||
res = mp_karatsuba_sqr (a, b);
|
||||
res = mp_karatsuba_sqr(a, b);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
@ -39,12 +39,12 @@ mp_sqr (mp_int * a, mp_int * b)
|
||||
if ((((a->used * 2) + 1) < MP_WARRAY) &&
|
||||
(a->used <
|
||||
(1 << (((sizeof(mp_word) * CHAR_BIT) - (2 * DIGIT_BIT)) - 1)))) {
|
||||
res = fast_s_mp_sqr (a, b);
|
||||
res = fast_s_mp_sqr(a, b);
|
||||
} else
|
||||
#endif
|
||||
{
|
||||
#ifdef BN_S_MP_SQR_C
|
||||
res = s_mp_sqr (a, b);
|
||||
res = s_mp_sqr(a, b);
|
||||
#else
|
||||
res = MP_VAL;
|
||||
#endif
|
||||
|
@ -22,16 +22,16 @@ mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
|
||||
int res;
|
||||
mp_int t;
|
||||
|
||||
if ((res = mp_init (&t)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_sqr (a, &t)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_sqr(a, &t)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
res = mp_mod (&t, b, c);
|
||||
mp_clear (&t);
|
||||
res = mp_mod(&t, b, c);
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -41,7 +41,7 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
}
|
||||
|
||||
/* First approx. (not very bad for large arg) */
|
||||
mp_rshd (&t1, t1.used/2);
|
||||
mp_rshd(&t1, t1.used/2);
|
||||
|
||||
/* t1 > 0 */
|
||||
if ((res = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
|
||||
|
@ -30,23 +30,23 @@ mp_sub (mp_int * a, mp_int * b, mp_int * c)
|
||||
/* In either case, ADD their magnitudes, */
|
||||
/* and use the sign of the first number. */
|
||||
c->sign = sa;
|
||||
res = s_mp_add (a, b, c);
|
||||
res = s_mp_add(a, b, c);
|
||||
} else {
|
||||
/* subtract a positive from a positive, OR */
|
||||
/* subtract a negative from a negative. */
|
||||
/* First, take the difference between their */
|
||||
/* magnitudes, then... */
|
||||
if (mp_cmp_mag (a, b) != MP_LT) {
|
||||
if (mp_cmp_mag(a, b) != MP_LT) {
|
||||
/* Copy the sign from the first */
|
||||
c->sign = sa;
|
||||
/* The first has a larger or equal magnitude */
|
||||
res = s_mp_sub (a, b, c);
|
||||
res = s_mp_sub(a, b, c);
|
||||
} else {
|
||||
/* The result has the *opposite* sign from */
|
||||
/* the first number. */
|
||||
c->sign = (sa == MP_ZPOS) ? MP_NEG : MP_ZPOS;
|
||||
/* The second has a larger magnitude */
|
||||
res = s_mp_sub (b, a, c);
|
||||
res = s_mp_sub(b, a, c);
|
||||
}
|
||||
}
|
||||
return res;
|
||||
|
@ -23,16 +23,16 @@ mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
|
||||
mp_int t;
|
||||
|
||||
|
||||
if ((res = mp_init (&t)) != MP_OKAY) {
|
||||
if ((res = mp_init(&t)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
if ((res = mp_sub (a, b, &t)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_sub(a, b, &t)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
res = mp_mod (&t, c, d);
|
||||
mp_clear (&t);
|
||||
res = mp_mod(&t, c, d);
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -20,7 +20,7 @@ int mp_to_signed_bin (mp_int * a, unsigned char *b)
|
||||
{
|
||||
int res;
|
||||
|
||||
if ((res = mp_to_unsigned_bin (a, b + 1)) != MP_OKAY) {
|
||||
if ((res = mp_to_unsigned_bin(a, b + 1)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
b[0] = (a->sign == MP_ZPOS) ? (unsigned char)0 : (unsigned char)1;
|
||||
|
@ -21,24 +21,24 @@ int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
|
||||
int x, res;
|
||||
mp_int t;
|
||||
|
||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
x = 0;
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
while (mp_iszero(&t) == MP_NO) {
|
||||
#ifndef MP_8BIT
|
||||
b[x++] = (unsigned char) (t.dp[0] & 255);
|
||||
b[x++] = (unsigned char)(t.dp[0] & 255);
|
||||
#else
|
||||
b[x++] = (unsigned char) (t.dp[0] | ((t.dp[1] & 0x01) << 7));
|
||||
b[x++] = (unsigned char)(t.dp[0] | ((t.dp[1] & 0x01) << 7));
|
||||
#endif
|
||||
if ((res = mp_div_2d (&t, 8, &t, NULL)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_div_2d(&t, 8, &t, NULL)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
}
|
||||
bn_reverse (b, x);
|
||||
mp_clear (&t);
|
||||
bn_reverse(b, x);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -35,7 +35,7 @@ int mp_toradix (mp_int * a, char *str, int radix)
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -47,9 +47,9 @@ int mp_toradix (mp_int * a, char *str, int radix)
|
||||
}
|
||||
|
||||
digs = 0;
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
while (mp_iszero(&t) == MP_NO) {
|
||||
if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
*str++ = mp_s_rmap[d];
|
||||
@ -59,12 +59,12 @@ int mp_toradix (mp_int * a, char *str, int radix)
|
||||
/* reverse the digits of the string. In this case _s points
|
||||
* to the first digit [exluding the sign] of the number]
|
||||
*/
|
||||
bn_reverse ((unsigned char *)_s, digs);
|
||||
bn_reverse((unsigned char *)_s, digs);
|
||||
|
||||
/* append a NULL so the string is properly terminated */
|
||||
*str = '\0';
|
||||
|
||||
mp_clear (&t);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
|
@ -38,7 +38,7 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
if ((res = mp_init_copy (&t, a)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, a)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
|
||||
@ -56,13 +56,13 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
}
|
||||
|
||||
digs = 0;
|
||||
while (mp_iszero (&t) == MP_NO) {
|
||||
while (mp_iszero(&t) == MP_NO) {
|
||||
if (--maxlen < 1) {
|
||||
/* no more room */
|
||||
break;
|
||||
}
|
||||
if ((res = mp_div_d (&t, (mp_digit) radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear (&t);
|
||||
if ((res = mp_div_d(&t, (mp_digit)radix, &t, &d)) != MP_OKAY) {
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
*str++ = mp_s_rmap[d];
|
||||
@ -72,12 +72,12 @@ int mp_toradix_n(mp_int * a, char *str, int radix, int maxlen)
|
||||
/* reverse the digits of the string. In this case _s points
|
||||
* to the first digit [exluding the sign] of the number
|
||||
*/
|
||||
bn_reverse ((unsigned char *)_s, digs);
|
||||
bn_reverse((unsigned char *)_s, digs);
|
||||
|
||||
/* append a NULL so the string is properly terminated */
|
||||
*str = '\0';
|
||||
|
||||
mp_clear (&t);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
|
@ -18,7 +18,7 @@
|
||||
/* get the size for an unsigned equivalent */
|
||||
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);
|
||||
}
|
||||
#endif
|
||||
|
10
bn_mp_xor.c
10
bn_mp_xor.c
@ -23,13 +23,13 @@ mp_xor (mp_int * a, mp_int * b, mp_int * c)
|
||||
mp_int t, *x;
|
||||
|
||||
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;
|
||||
}
|
||||
px = b->used;
|
||||
x = b;
|
||||
} else {
|
||||
if ((res = mp_init_copy (&t, b)) != MP_OKAY) {
|
||||
if ((res = mp_init_copy(&t, b)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
px = a->used;
|
||||
@ -39,9 +39,9 @@ mp_xor (mp_int * a, mp_int * b, mp_int * c)
|
||||
for (ix = 0; ix < px; ix++) {
|
||||
t.dp[ix] ^= x->dp[ix];
|
||||
}
|
||||
mp_clamp (&t);
|
||||
mp_exch (c, &t);
|
||||
mp_clear (&t);
|
||||
mp_clamp(&t);
|
||||
mp_exch(c, &t);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -37,7 +37,7 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
||||
/* init result */
|
||||
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;
|
||||
}
|
||||
}
|
||||
@ -99,7 +99,7 @@ s_mp_add (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
}
|
||||
|
||||
mp_clamp (c);
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -28,7 +28,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
int (*redux)(mp_int*,mp_int*,mp_int*);
|
||||
|
||||
/* find window size */
|
||||
x = mp_count_bits (X);
|
||||
x = mp_count_bits(X);
|
||||
if (x <= 7) {
|
||||
winsize = 2;
|
||||
} else if (x <= 36) {
|
||||
@ -61,7 +61,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||
if ((err = mp_init(&M[x])) != MP_OKAY) {
|
||||
for (y = 1<<(winsize-1); y < x; y++) {
|
||||
mp_clear (&M[y]);
|
||||
mp_clear(&M[y]);
|
||||
}
|
||||
mp_clear(&M[1]);
|
||||
return err;
|
||||
@ -69,17 +69,17 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
}
|
||||
|
||||
/* create mu, used for Barrett reduction */
|
||||
if ((err = mp_init (&mu)) != MP_OKAY) {
|
||||
if ((err = mp_init(&mu)) != MP_OKAY) {
|
||||
goto LBL_M;
|
||||
}
|
||||
|
||||
if (redmode == 0) {
|
||||
if ((err = mp_reduce_setup (&mu, P)) != MP_OKAY) {
|
||||
if ((err = mp_reduce_setup(&mu, P)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
redux = mp_reduce;
|
||||
} else {
|
||||
if ((err = mp_reduce_2k_setup_l (P, &mu)) != MP_OKAY) {
|
||||
if ((err = mp_reduce_2k_setup_l(P, &mu)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
redux = mp_reduce_2k_l;
|
||||
@ -93,26 +93,26 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
* The first half of the table is not
|
||||
* computed though accept for M[0] and M[1]
|
||||
*/
|
||||
if ((err = mp_mod (G, P, &M[1])) != MP_OKAY) {
|
||||
if ((err = mp_mod(G, P, &M[1])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
||||
/* compute the value at M[1<<(winsize-1)] by squaring
|
||||
* M[1] (winsize-1) times
|
||||
*/
|
||||
if ((err = mp_copy (&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
if ((err = mp_copy(&M[1], &M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
||||
for (x = 0; x < (winsize - 1); x++) {
|
||||
/* square it */
|
||||
if ((err = mp_sqr (&M[1 << (winsize - 1)],
|
||||
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&M[1 << (winsize - 1)],
|
||||
&M[1 << (winsize - 1)])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
|
||||
/* reduce modulo P */
|
||||
if ((err = redux (&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&M[1 << (winsize - 1)], P, &mu)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
}
|
||||
@ -121,19 +121,19 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
* for x = (2**(winsize - 1) + 1) to (2**winsize - 1)
|
||||
*/
|
||||
for (x = (1 << (winsize - 1)) + 1; x < (1 << winsize); x++) {
|
||||
if ((err = mp_mul (&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
||||
if ((err = mp_mul(&M[x - 1], &M[1], &M[x])) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
if ((err = redux (&M[x], P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&M[x], P, &mu)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
}
|
||||
|
||||
/* setup result */
|
||||
if ((err = mp_init (&res)) != MP_OKAY) {
|
||||
if ((err = mp_init(&res)) != MP_OKAY) {
|
||||
goto LBL_MU;
|
||||
}
|
||||
mp_set (&res, 1);
|
||||
mp_set(&res, 1);
|
||||
|
||||
/* set initial mode and bit cnt */
|
||||
mode = 0;
|
||||
@ -152,7 +152,7 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
}
|
||||
/* read next digit and reset the bitcnt */
|
||||
buf = X->dp[digidx--];
|
||||
bitcnt = (int) DIGIT_BIT;
|
||||
bitcnt = (int)DIGIT_BIT;
|
||||
}
|
||||
|
||||
/* grab the next msb from the exponent */
|
||||
@ -170,10 +170,10 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
|
||||
/* if the bit is zero and mode == 1 then we square */
|
||||
if ((mode == 1) && (y == 0)) {
|
||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&res, &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, &mu)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
continue;
|
||||
@ -187,19 +187,19 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
/* ok window is filled so square as required and multiply */
|
||||
/* square first */
|
||||
for (x = 0; x < winsize; x++) {
|
||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&res, &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, &mu)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
}
|
||||
|
||||
/* then multiply */
|
||||
if ((err = mp_mul (&res, &M[bitbuf], &res)) != MP_OKAY) {
|
||||
if ((err = mp_mul(&res, &M[bitbuf], &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, &mu)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
|
||||
@ -214,34 +214,34 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
|
||||
if ((mode == 2) && (bitcpy > 0)) {
|
||||
/* square then multiply if the bit is set */
|
||||
for (x = 0; x < bitcpy; x++) {
|
||||
if ((err = mp_sqr (&res, &res)) != MP_OKAY) {
|
||||
if ((err = mp_sqr(&res, &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, &mu)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
|
||||
bitbuf <<= 1;
|
||||
if ((bitbuf & (1 << winsize)) != 0) {
|
||||
/* then multiply */
|
||||
if ((err = mp_mul (&res, &M[1], &res)) != MP_OKAY) {
|
||||
if ((err = mp_mul(&res, &M[1], &res)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
if ((err = redux (&res, P, &mu)) != MP_OKAY) {
|
||||
if ((err = redux(&res, P, &mu)) != MP_OKAY) {
|
||||
goto LBL_RES;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
mp_exch (&res, Y);
|
||||
mp_exch(&res, Y);
|
||||
err = MP_OKAY;
|
||||
LBL_RES:mp_clear (&res);
|
||||
LBL_MU:mp_clear (&mu);
|
||||
LBL_RES:mp_clear(&res);
|
||||
LBL_MU:mp_clear(&mu);
|
||||
LBL_M:
|
||||
mp_clear(&M[1]);
|
||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||
mp_clear (&M[x]);
|
||||
mp_clear(&M[x]);
|
||||
}
|
||||
return err;
|
||||
}
|
||||
|
@ -31,10 +31,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
if (((digs) < MP_WARRAY) &&
|
||||
(MIN (a->used, b->used) <
|
||||
(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);
|
||||
}
|
||||
|
||||
if ((res = mp_init_size (&t, digs)) != MP_OKAY) {
|
||||
if ((res = mp_init_size(&t, digs)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
t.used = digs;
|
||||
@ -46,7 +46,7 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
u = 0;
|
||||
|
||||
/* limit ourselves to making digs digits of output */
|
||||
pb = MIN (b->used, digs - ix);
|
||||
pb = MIN(b->used, digs - ix);
|
||||
|
||||
/* setup some aliases */
|
||||
/* copy of the digit from a used within the nested loop */
|
||||
@ -77,10 +77,10 @@ int s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
}
|
||||
}
|
||||
|
||||
mp_clamp (&t);
|
||||
mp_exch (&t, c);
|
||||
mp_clamp(&t);
|
||||
mp_exch(&t, c);
|
||||
|
||||
mp_clear (&t);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -30,12 +30,12 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
/* can we use the fast multiplier? */
|
||||
#ifdef BN_FAST_S_MP_MUL_HIGH_DIGS_C
|
||||
if (((a->used + b->used + 1) < MP_WARRAY)
|
||||
&& (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);
|
||||
&& (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);
|
||||
}
|
||||
#endif
|
||||
|
||||
if ((res = mp_init_size (&t, a->used + b->used + 1)) != MP_OKAY) {
|
||||
if ((res = mp_init_size(&t, a->used + b->used + 1)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
t.used = a->used + b->used + 1;
|
||||
@ -62,16 +62,16 @@ s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
(mp_word)u;
|
||||
|
||||
/* get the lower part */
|
||||
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
|
||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
||||
|
||||
/* carry the carry */
|
||||
u = (mp_digit) (r >> ((mp_word) DIGIT_BIT));
|
||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
||||
}
|
||||
*tmpt = u;
|
||||
}
|
||||
mp_clamp (&t);
|
||||
mp_exch (&t, c);
|
||||
mp_clear (&t);
|
||||
mp_clamp(&t);
|
||||
mp_exch(&t, c);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -24,7 +24,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
mp_digit u, tmpx, *tmpt;
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@ -38,10 +38,10 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
((mp_word)a->dp[ix] * (mp_word)a->dp[ix]);
|
||||
|
||||
/* 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));
|
||||
|
||||
/* get the carry */
|
||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
||||
u = (mp_digit)(r >> ((mp_word)DIGIT_BIT));
|
||||
|
||||
/* left hand side of A[ix] * A[iy] */
|
||||
tmpx = a->dp[ix];
|
||||
@ -59,7 +59,7 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
r = ((mp_word) *tmpt) + r + r + ((mp_word) u);
|
||||
|
||||
/* store lower part */
|
||||
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
|
||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
||||
|
||||
/* get carry */
|
||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
||||
@ -67,14 +67,14 @@ int s_mp_sqr (mp_int * a, mp_int * b)
|
||||
/* propagate upwards */
|
||||
while (u != ((mp_digit) 0)) {
|
||||
r = ((mp_word) *tmpt) + ((mp_word) u);
|
||||
*tmpt++ = (mp_digit) (r & ((mp_word) MP_MASK));
|
||||
*tmpt++ = (mp_digit)(r & ((mp_word) MP_MASK));
|
||||
u = (mp_digit)(r >> ((mp_word) DIGIT_BIT));
|
||||
}
|
||||
}
|
||||
|
||||
mp_clamp (&t);
|
||||
mp_exch (&t, b);
|
||||
mp_clear (&t);
|
||||
mp_clamp(&t);
|
||||
mp_exch(&t, b);
|
||||
mp_clear(&t);
|
||||
return MP_OKAY;
|
||||
}
|
||||
#endif
|
||||
|
@ -27,7 +27,7 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
|
||||
|
||||
/* init result */
|
||||
if (c->alloc < max) {
|
||||
if ((res = mp_grow (c, max)) != MP_OKAY) {
|
||||
if ((res = mp_grow(c, max)) != MP_OKAY) {
|
||||
return res;
|
||||
}
|
||||
}
|
||||
@ -78,7 +78,7 @@ s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
}
|
||||
|
||||
mp_clamp (c);
|
||||
mp_clamp(c);
|
||||
return MP_OKAY;
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user