Merge pull request #85 from fperrad/20170830_format

rebase formating code
This commit is contained in:
Steffen Jaeckel 2017-09-15 12:46:38 +02:00 committed by GitHub
commit fedc15b625
129 changed files with 5706 additions and 5579 deletions

27
astylerc Normal file
View File

@ -0,0 +1,27 @@
# Artistic Style, see http://astyle.sourceforge.net/
# full documentation, see: http://astyle.sourceforge.net/astyle.html
#
# usage:
# astyle --options=astylerc *.[ch]
## Bracket Style Options
style=kr
## Tab Options
indent=spaces=3
## Bracket Modify Options
## Indentation Options
min-conditional-indent=0
## Padding Options
pad-header
unpad-paren
align-pointer=name
## Formatting Options
break-after-logical
max-code-length=120
convert-tabs
mode=c

View File

@ -21,13 +21,13 @@
* Based on slow invmod except this is optimized for the case where b is
* odd as per HAC Note 14.64 on pp. 610
*/
int fast_mp_invmod (mp_int * a, mp_int * b, mp_int * c)
int fast_mp_invmod(mp_int *a, mp_int *b, mp_int *c)
{
mp_int x, y, u, v, B, D;
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,16 @@ 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

View File

@ -23,7 +23,7 @@
*
* Based on Algorithm 14.32 on pp.601 of HAC.
*/
int fast_mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
int fast_mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho)
{
int ix, res, olduse;
mp_word W[MP_WARRAY];
@ -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;
}

View File

@ -31,7 +31,7 @@
* Based on Algorithm 14.12 on pp.595 of HAC.
*
*/
int fast_s_mp_mul_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
int fast_s_mp_mul_digs(mp_int *a, mp_int *b, mp_int *c, int digs)
{
int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY];
@ -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

View File

@ -24,7 +24,7 @@
*
* Based on Algorithm 14.12 on pp.595 of HAC.
*/
int fast_s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
int fast_s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs)
{
int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY];
@ -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

View File

@ -25,7 +25,7 @@
After that loop you do the squares and add them in.
*/
int fast_s_mp_sqr (mp_int * a, mp_int * b)
int fast_s_mp_sqr(mp_int *a, mp_int *b)
{
int olduse, res, pa, ix, iz;
mp_digit W[MP_WARRAY], *tmpx;
@ -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

View File

@ -20,16 +20,15 @@
* Simple algorithm which zeroes the int, grows it then just sets one bit
* as required.
*/
int
mp_2expt (mp_int * a, int b)
int 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;
}

View File

@ -19,14 +19,13 @@
*
* Simple function copies the input and fixes the sign to positive
*/
int
mp_abs (mp_int * a, mp_int * b)
int mp_abs(mp_int *a, mp_int *b)
{
int res;
/* 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;
}
}

View File

@ -16,7 +16,7 @@
*/
/* high level addition (handles signs) */
int mp_add (mp_int * a, mp_int * b, mp_int * c)
int mp_add(mp_int *a, mp_int *b, mp_int *c)
{
int sa, sb, 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;

View File

@ -16,8 +16,7 @@
*/
/* single digit addition */
int
mp_add_d (mp_int * a, mp_digit b, mp_int * c)
int mp_add_d(mp_int *a, mp_digit b, mp_int *c)
{
int res, ix, oldused;
mp_digit *tmpa, *tmpc, mu;

View File

@ -16,22 +16,21 @@
*/
/* d = a + b (mod c) */
int
mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
{
int res;
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

View File

@ -16,20 +16,19 @@
*/
/* AND two ints together */
int
mp_and (mp_int * a, mp_int * b, mp_int * c)
int mp_and(mp_int *a, mp_int *b, mp_int *c)
{
int res, ix, px;
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 +44,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

View File

@ -22,8 +22,7 @@
* Typically very fast. Also fixes the sign if there
* are no more leading digits
*/
void
mp_clamp (mp_int * a)
void mp_clamp(mp_int *a)
{
/* decrease used while the most significant digit is
* zero.

View File

@ -16,8 +16,7 @@
*/
/* clear one (frees) */
void
mp_clear (mp_int * a)
void mp_clear(mp_int *a)
{
int i;

View File

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

View File

@ -16,8 +16,7 @@
*/
/* compare two ints (signed)*/
int
mp_cmp (mp_int * a, mp_int * b)
int mp_cmp(mp_int *a, mp_int *b)
{
/* compare based on sign */
if (a->sign != b->sign) {

View File

@ -16,7 +16,7 @@
*/
/* compare a digit */
int mp_cmp_d(mp_int * a, mp_digit b)
int mp_cmp_d(mp_int *a, mp_digit b)
{
/* compare based on sign */
if (a->sign == MP_NEG) {

View File

@ -16,7 +16,7 @@
*/
/* compare maginitude of two ints (unsigned) */
int mp_cmp_mag (mp_int * a, mp_int * b)
int mp_cmp_mag(mp_int *a, mp_int *b)
{
int n;
mp_digit *tmpa, *tmpb;

View File

@ -16,8 +16,7 @@
*/
/* copy, b = a */
int
mp_copy (mp_int * a, mp_int * b)
int mp_copy(mp_int *a, mp_int *b)
{
int res, n;
@ -28,7 +27,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;
}
}

View File

@ -16,8 +16,7 @@
*/
/* returns the number of bits in an int */
int
mp_count_bits (mp_int * a)
int mp_count_bits(mp_int *a)
{
int r;
mp_digit q;

View File

@ -18,25 +18,25 @@
#ifdef BN_MP_DIV_SMALL
/* slower bit-bang division... also smaller */
int mp_div(mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
{
mp_int ta, tb, tq, q;
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;
}
@ -100,47 +100,47 @@ LBL_ERR:
* The overall algorithm is as described as
* 14.20 from HAC but fixed to treat these cases.
*/
int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int mp_div(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
{
mp_int q, x, y, t1, t2;
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,30 @@ 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;
}

View File

@ -16,13 +16,13 @@
*/
/* b = a/2 */
int mp_div_2(mp_int * a, mp_int * b)
int mp_div_2(mp_int *a, mp_int *b)
{
int x, res, oldused;
/* 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

View File

@ -16,40 +16,40 @@
*/
/* shift right by a certain bit count (store quotient in c, optional remainder in d) */
int mp_div_2d (mp_int * a, int b, mp_int * c, mp_int * d)
int mp_div_2d(mp_int *a, int b, mp_int *c, mp_int *d)
{
mp_digit D, r, rr;
int x, res;
/* 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

View File

@ -16,8 +16,7 @@
*/
/* divide by three (based on routine from MPI and the GMP manual) */
int
mp_div_3 (mp_int * a, mp_int *c, mp_digit * d)
int mp_div_3(mp_int *a, mp_int *c, mp_digit *d)
{
mp_int q;
mp_word w, t;

View File

@ -34,7 +34,7 @@ static int s_is_power_of_two(mp_digit b, int *p)
}
/* single digit division (based on routine from MPI) */
int mp_div_d (mp_int * a, mp_digit b, mp_int * c, mp_digit * d)
int mp_div_d(mp_int *a, mp_digit b, mp_int *c, mp_digit *d)
{
mp_int q;
mp_word w;

View File

@ -29,8 +29,7 @@
*
* Input x must be in the range 0 <= x <= (n-1)**2
*/
int
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
int mp_dr_reduce(mp_int *x, mp_int *n, mp_digit k)
{
int err, i, m;
mp_word r;
@ -41,12 +40,12 @@ 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;
}
}
/* top of loop, this is where the code resumes if
/* top of loop, this is where the code resumes if
* another reduction pass is required.
*/
top:
@ -76,12 +75,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;
}

View File

@ -21,8 +21,7 @@ void mp_dr_setup(mp_int *a, mp_digit *d)
/* the casts are required if DIGIT_BIT is one less than
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
*/
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) -
((mp_word)a->dp[0]));
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - ((mp_word)a->dp[0]));
}
#endif

View File

@ -18,8 +18,7 @@
/* swap the elements of two integers, for cases where you can't simply swap the
* mp_int pointers around
*/
void
mp_exch (mp_int * a, mp_int * b)
void mp_exch(mp_int *a, mp_int *b)
{
mp_int t;

View File

@ -18,8 +18,9 @@
/* based on gmp's mpz_export.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
int mp_export(void* rop, size_t* countp, int order, size_t size,
int endian, size_t nails, mp_int* op) {
int mp_export(void *rop, size_t *countp, int order, size_t size,
int endian, size_t nails, mp_int *op)
{
int result;
size_t odd_nails, nail_bytes, i, j, bits, count;
unsigned char odd_nail_mask;
@ -52,11 +53,9 @@ int mp_export(void* rop, size_t* countp, int order, size_t size,
for (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) {
unsigned char* byte = (
(unsigned char*)rop +
unsigned char *byte = (unsigned char *)rop +
(((order == -1) ? i : ((count - 1) - i)) * size) +
((endian == -1) ? j : ((size - 1) - j))
);
((endian == -1) ? j : ((size - 1) - j));
if (j >= (size - nail_bytes)) {
*byte = 0;

View File

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

View File

@ -16,34 +16,34 @@
*/
/* calculate c = a**b using a square-multiply algorithm */
int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
int mp_expt_d_ex(mp_int *a, mp_digit b, mp_int *c, int fast)
{
int res;
unsigned int x;
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;
}
}
@ -51,19 +51,18 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
/* shift to next bit */
b >>= 1;
}
}
else {
} 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 +72,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

View File

@ -21,7 +21,7 @@
* embedded in the normal function but that wasted alot of stack space
* for nothing (since 99% of the time the Montgomery code would be called)
*/
int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
int mp_exptmod(mp_int *G, mp_int *X, mp_int *P, mp_int *Y)
{
int dr;
@ -65,7 +65,7 @@ int mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y)
#endif
}
/* modified diminished radix reduction */
/* modified diminished radix reduction */
#if defined(BN_MP_REDUCE_IS_2K_L_C) && defined(BN_MP_REDUCE_2K_L_C) && defined(BN_S_MP_EXPTMOD_C)
if (mp_reduce_is_2k_l(P) == MP_YES) {
return s_mp_exptmod(G, X, P, Y, 1);
@ -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;

View File

@ -24,12 +24,12 @@
*/
#ifdef MP_LOW_MEM
#define TAB_SIZE 32
# define TAB_SIZE 32
#else
#define TAB_SIZE 256
# define TAB_SIZE 256
#endif
int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
int mp_exptmod_fast(mp_int *G, mp_int *X, mp_int *P, mp_int *Y, int redmode)
{
mp_int M[TAB_SIZE], res;
mp_digit buf, mp;
@ -39,10 +39,10 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
* one of many reduction algorithms without modding the guts of
* the code with if statements everywhere.
*/
int (*redux)(mp_int*,mp_int*,mp_digit);
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,14 @@ 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;
}

View File

@ -20,7 +20,7 @@
*/
int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
{
mp_int u1,u2,u3,v1,v2,v3,t1,t2,t3,q,tmp;
mp_int u1, u2, u3, v1, v2, v3, t1, t2, t3, q, tmp;
int err;
if ((err = mp_init_multi(&u1, &u2, &u3, &v1, &v2, &v3, &t1, &t2, &t3, &q, &tmp, NULL)) != MP_OKAY) {
@ -29,47 +29,89 @@ int mp_exteuclid(mp_int *a, mp_int *b, mp_int *U1, mp_int *U2, mp_int *U3)
/* initialize, (u1,u2,u3) = (1,0,a) */
mp_set(&u1, 1);
if ((err = mp_copy(a, &u3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(a, &u3)) != MP_OKAY) {
goto LBL_ERR;
}
/* initialize, (v1,v2,v3) = (0,1,b) */
mp_set(&v2, 1);
if ((err = mp_copy(b, &v3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(b, &v3)) != MP_OKAY) {
goto LBL_ERR;
}
/* loop while v3 != 0 */
while (mp_iszero(&v3) == MP_NO) {
/* q = u3/v3 */
if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_div(&u3, &v3, &q, NULL)) != MP_OKAY) {
goto LBL_ERR;
}
/* (t1,t2,t3) = (u1,u2,u3) - (v1,v2,v3)q */
if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_mul(&v1, &q, &tmp)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_mul(&v2, &q, &tmp)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_sub(&u2, &tmp, &t2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_sub(&u3, &tmp, &t3)) != MP_OKAY) {
goto LBL_ERR;
}
/* (u1,u2,u3) = (v1,v2,v3) */
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&v3, &u3)) != MP_OKAY) {
goto LBL_ERR;
}
/* (v1,v2,v3) = (t1,t2,t3) */
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_copy(&t3, &v3)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* make sure U3 >= 0 */
if (u3.sign == MP_NEG) {
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) { goto LBL_ERR; }
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) {
goto LBL_ERR;
}
if ((err = mp_neg(&u3, &u3)) != MP_OKAY) {
goto LBL_ERR;
}
}
/* copy result out */
if (U1 != NULL) { mp_exch(U1, &u1); }
if (U2 != NULL) { mp_exch(U2, &u2); }
if (U3 != NULL) { mp_exch(U3, &u3); }
if (U1 != NULL) {
mp_exch(U1, &u1);
}
if (U2 != NULL) {
mp_exch(U2, &u2);
}
if (U3 != NULL) {
mp_exch(U3, &u3);
}
err = MP_OKAY;
LBL_ERR:

View File

@ -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

View File

@ -16,25 +16,25 @@
*/
/* Greatest Common Divisor using the binary method */
int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
int mp_gcd(mp_int *a, mp_int *b, mp_int *c)
{
mp_int u, v;
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,15 @@ 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

View File

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

View File

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

View File

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

View File

@ -16,7 +16,7 @@
*/
/* grow as required */
int mp_grow (mp_int * a, int size)
int mp_grow(mp_int *a, int size)
{
int i;
mp_digit *tmp;
@ -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;

View File

@ -18,8 +18,9 @@
/* based on gmp's mpz_import.
* see http://gmplib.org/manual/Integer-Import-and-Export.html
*/
int mp_import(mp_int* rop, size_t count, int order, size_t size,
int endian, size_t nails, const void* op) {
int mp_import(mp_int *rop, size_t count, int order, size_t size,
int endian, size_t nails, const void *op)
{
int result;
size_t odd_nails, nail_bytes, i, j;
unsigned char odd_nail_mask;
@ -45,14 +46,11 @@ int mp_import(mp_int* rop, size_t count, int order, size_t size,
for (i = 0; i < count; ++i) {
for (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *(
(unsigned char*)op +
unsigned char byte = *((unsigned char *)op +
(((order == 1) ? i : ((count - 1) - i)) * size) +
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes))
);
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)));
if (
(result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
if ((result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
return result;
}

View File

@ -16,12 +16,12 @@
*/
/* init a new mp_int */
int mp_init (mp_int * a)
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;
}

View File

@ -16,15 +16,15 @@
*/
/* creates "a" then copies b into it */
int mp_init_copy (mp_int * a, mp_int * b)
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);
}

View File

@ -20,7 +20,7 @@ int mp_init_multi(mp_int *mp, ...)
{
mp_err res = MP_OKAY; /* Assume ok until proven otherwise */
int n = 0; /* Number of ok inits */
mp_int* cur_arg = mp;
mp_int *cur_arg = mp;
va_list args;
va_start(args, mp); /* init args to next argument from caller */
@ -36,14 +36,14 @@ int mp_init_multi(mp_int *mp, ...)
va_start(clean_args, mp);
while (n-- != 0) {
mp_clear(cur_arg);
cur_arg = va_arg(clean_args, mp_int*);
cur_arg = va_arg(clean_args, mp_int *);
}
va_end(clean_args);
res = MP_MEM;
break;
}
n++;
cur_arg = va_arg(args, mp_int*);
cur_arg = va_arg(args, mp_int *);
}
va_end(args);
return res; /* Assumed ok, if error flagged above. */

View File

@ -16,7 +16,7 @@
*/
/* initialize and set a digit */
int mp_init_set (mp_int * a, mp_digit b)
int mp_init_set(mp_int *a, mp_digit b)
{
int err;
if ((err = mp_init(a)) != MP_OKAY) {

View File

@ -16,7 +16,7 @@
*/
/* initialize and set a digit */
int mp_init_set_int (mp_int * a, unsigned long b)
int mp_init_set_int(mp_int *a, unsigned long b)
{
int err;
if ((err = mp_init(a)) != MP_OKAY) {

View File

@ -16,7 +16,7 @@
*/
/* init an mp_init for a given size */
int mp_init_size (mp_int * a, int size)
int mp_init_size(mp_int *a, int size)
{
int x;
@ -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;
}

View File

@ -16,7 +16,7 @@
*/
/* hac 14.61, pp608 */
int mp_invmod (mp_int * a, mp_int * b, mp_int * c)
int mp_invmod(mp_int *a, mp_int *b, mp_int *c)
{
/* b cannot be negative */
if ((b->sign == MP_NEG) || (mp_iszero(b) == MP_YES)) {
@ -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

View File

@ -16,7 +16,7 @@
*/
/* hac 14.61, pp608 */
int mp_invmod_slow (mp_int * a, mp_int * b, mp_int * c)
int mp_invmod_slow(mp_int *a, mp_int *b, mp_int *c)
{
mp_int x, y, u, v, A, B, C, D;
int res;
@ -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,10 @@ 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

View File

@ -38,7 +38,7 @@ static const char rem_105[105] = {
};
/* Store non-zero to ret if arg is square, and zero if not */
int mp_is_square(mp_int *arg,int *ret)
int mp_is_square(mp_int *arg, int *ret)
{
int res;
mp_digit c;
@ -58,12 +58,12 @@ int mp_is_square(mp_int *arg,int *ret)
}
/* First check mod 128 (suppose that DIGIT_BIT is at least 7) */
if (rem_128[127 & DIGIT(arg,0)] == 1) {
if (rem_128[127 & DIGIT(arg, 0)] == 1) {
return MP_OKAY;
}
/* Next check mod 105 (3*5*7) */
if ((res = mp_mod_d(arg,105,&c)) != MP_OKAY) {
if ((res = mp_mod_d(arg, 105, &c)) != MP_OKAY) {
return res;
}
if (rem_105[c] == 1) {
@ -71,10 +71,10 @@ int mp_is_square(mp_int *arg,int *ret)
}
if ((res = mp_init_set_int(&t,11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
if ((res = mp_init_set_int(&t, 11L*13L*17L*19L*23L*29L*31L)) != MP_OKAY) {
return res;
}
if ((res = mp_mod(arg,&t,&t)) != MP_OKAY) {
if ((res = mp_mod(arg, &t, &t)) != MP_OKAY) {
goto ERR;
}
r = mp_get_int(&t);
@ -91,15 +91,16 @@ int mp_is_square(mp_int *arg,int *ret)
if (((1L<<(r%31)) & 0x6DE2B848L) != 0L) goto ERR;
/* Final check - is sqr(sqrt(arg)) == arg ? */
if ((res = mp_sqrt(arg,&t)) != MP_OKAY) {
if ((res = mp_sqrt(arg, &t)) != MP_OKAY) {
goto ERR;
}
if ((res = mp_sqr(&t,&t)) != MP_OKAY) {
if ((res = mp_sqr(&t, &t)) != MP_OKAY) {
goto ERR;
}
*ret = (mp_cmp_mag(&t,arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:mp_clear(&t);
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:
mp_clear(&t);
return res;
}
#endif

View File

@ -20,7 +20,7 @@
* HAC is wrong here, as the special case of (0 | 1) is not
* handled correctly.
*/
int mp_jacobi (mp_int * a, mp_int * n, int *c)
int mp_jacobi(mp_int *a, mp_int *n, int *c)
{
mp_int a1, p1;
int k, s, r, res;
@ -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;
}
@ -86,19 +86,19 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
}
/* step 5. if p == 3 (mod 4) *and* a1 == 3 (mod 4) then s = -s */
if ( ((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
if (((n->dp[0] & 3) == 3) && ((a1.dp[0] & 3) == 3)) {
s = -s;
}
/* 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,10 @@ 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

View File

@ -44,7 +44,7 @@
* Generally though the overhead of this method doesn't pay off
* until a certain size (N ~ 80) is reached.
*/
int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
int mp_karatsuba_mul(mp_int *a, mp_int *b, mp_int *c)
{
mp_int x0, x1, y0, y1, t1, x0y0, x1y1;
int B, err;
@ -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,58 @@ 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;
}

View File

@ -22,7 +22,7 @@
* is essentially the same algorithm but merely
* tuned to perform recursive squarings.
*/
int mp_karatsuba_sqr (mp_int * a, mp_int * b)
int mp_karatsuba_sqr(mp_int *a, mp_int *b)
{
mp_int x0, x1, t1, t2, x0x0, x1x1;
int B, 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,51 @@ 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;
}

View File

@ -16,18 +16,18 @@
*/
/* computes least common multiple as |a*b|/(a, b) */
int mp_lcm (mp_int * a, mp_int * b, mp_int * c)
int mp_lcm(mp_int *a, mp_int *b, mp_int *c)
{
int res;
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

View File

@ -16,7 +16,7 @@
*/
/* shift left a certain amount of digits */
int mp_lshd (mp_int * a, int b)
int mp_lshd(mp_int *a, int b)
{
int x, res;
@ -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;
}
}

View File

@ -16,29 +16,28 @@
*/
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
int
mp_mod (mp_int * a, mp_int * b, mp_int * c)
int 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

View File

@ -16,25 +16,24 @@
*/
/* calc a value mod 2**b */
int
mp_mod_2d (mp_int * a, int b, mp_int * c)
int mp_mod_2d(mp_int *a, int b, mp_int *c)
{
int x, res;
/* 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);
if (b >= (int)(a->used * DIGIT_BIT)) {
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 +43,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

View File

@ -15,8 +15,7 @@
* Tom St Denis, tstdenis82@gmail.com, http://libtom.org
*/
int
mp_mod_d (mp_int * a, mp_digit b, mp_digit * c)
int mp_mod_d(mp_int *a, mp_digit b, mp_digit *c)
{
return mp_div_d(a, b, NULL, c);
}

View File

@ -21,15 +21,15 @@
* The method is slightly modified to shift B unconditionally upto just under
* the leading bit of b. This saves alot of multiple precision shifting.
*/
int mp_montgomery_calc_normalization (mp_int * a, mp_int * b)
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;
}
}

View File

@ -16,8 +16,7 @@
*/
/* computes xR**-1 == x (mod N) via Montgomery Reduction */
int
mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
int mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho)
{
int ix, res, digs;
mp_digit mu;
@ -32,12 +31,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 +51,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 +101,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;

View File

@ -16,12 +16,11 @@
*/
/* setups the montgomery reduction stuff */
int
mp_montgomery_setup (mp_int * n, mp_digit * rho)
int mp_montgomery_setup(mp_int *n, mp_digit *rho)
{
mp_digit x, b;
/* fast inversion mod 2**k
/* fast inversion mod 2**k
*
* Based on the fact that
*

View File

@ -16,21 +16,21 @@
*/
/* high level multiplication (handles sign) */
int mp_mul (mp_int * a, mp_int * b, mp_int * c)
int mp_mul(mp_int *a, mp_int *b, mp_int *c)
{
int res, neg;
neg = (a->sign == b->sign) ? MP_ZPOS : MP_NEG;
/* 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

View File

@ -16,13 +16,13 @@
*/
/* b = a*2 */
int mp_mul_2(mp_int * a, mp_int * b)
int mp_mul_2(mp_int *a, mp_int *b)
{
int x, res, oldused;
/* 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;
}
}

View File

@ -16,33 +16,33 @@
*/
/* shift left by a certain bit count */
int mp_mul_2d (mp_int * a, int b, mp_int * c)
int mp_mul_2d(mp_int *a, int b, mp_int *c)
{
mp_digit d;
int res;
/* 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

View File

@ -16,8 +16,7 @@
*/
/* multiply by a digit */
int
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
int mp_mul_d(mp_int *a, mp_digit b, mp_int *c)
{
mp_digit u, *tmpa, *tmpc;
mp_word r;
@ -25,7 +24,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 +50,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 */

View File

@ -16,21 +16,21 @@
*/
/* d = a * b (mod c) */
int mp_mulmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
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

View File

@ -18,7 +18,7 @@
/* wrapper function for mp_n_root_ex()
* computes c = (a)**(1/b) such that (c)**b <= a and (c+1)**b > a
*/
int mp_n_root (mp_int * a, mp_digit b, mp_int * c)
int mp_n_root(mp_int *a, mp_digit b, mp_int *c)
{
return mp_n_root_ex(a, b, c, 0);
}

View File

@ -25,7 +25,7 @@
* each step involves a fair bit. This is not meant to
* find huge roots [square and cube, etc].
*/
int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
int mp_n_root_ex(mp_int *a, mp_digit b, mp_int *c, int fast)
{
mp_int t1, t2, t3;
int res, neg;
@ -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,19 @@ 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

View File

@ -16,11 +16,11 @@
*/
/* b = -a */
int mp_neg (mp_int * a, mp_int * b)
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;
}
}

View File

@ -16,19 +16,19 @@
*/
/* OR two ints together */
int mp_or (mp_int * a, mp_int * b, mp_int * c)
int mp_or(mp_int *a, mp_int *b, mp_int *c)
{
int res, ix, px;
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

View File

@ -23,7 +23,7 @@
*
* Sets result to 1 if the congruence holds, or zero otherwise.
*/
int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
int mp_prime_fermat(mp_int *a, mp_int *b, int *result)
{
mp_int t;
int err;
@ -37,22 +37,23 @@ 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

View File

@ -20,7 +20,7 @@
*
* sets result to 0 if not, 1 if yes
*/
int mp_prime_is_divisible (mp_int * a, int *result)
int mp_prime_is_divisible(mp_int *a, int *result)
{
int err, ix;
mp_digit res;
@ -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;
}

View File

@ -22,7 +22,7 @@
*
* Sets result to 1 if probably prime, 0 otherwise
*/
int mp_prime_is_prime (mp_int * a, int t, int *result)
int mp_prime_is_prime(mp_int *a, int t, int *result)
{
mp_int b;
int ix, err, res;
@ -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,8 @@ 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

View File

@ -22,7 +22,7 @@
* Randomly the chance of error is no more than 1/4 and often
* very much lower.
*/
int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
int mp_prime_miller_rabin(mp_int *a, mp_int *b, int *result)
{
mp_int n1, y, r;
int s, j, err;
@ -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,19 @@ 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

View File

@ -81,7 +81,9 @@ int mp_prime_next_prime(mp_int *a, int t, int bbs_style)
if (bbs_style == 1) {
/* if a mod 4 != 3 subtract the correct value to make it so */
if ((a->dp[0] & 3) != 3) {
if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) { return err; };
if ((err = mp_sub_d(a, (a->dp[0] & 3) + 1, a)) != MP_OKAY) {
return err;
};
}
} else {
if (mp_iseven(a) == MP_YES) {

View File

@ -19,14 +19,14 @@
static const struct {
int k, t;
} sizes[] = {
{ 128, 28 },
{ 256, 16 },
{ 384, 10 },
{ 512, 7 },
{ 640, 6 },
{ 768, 5 },
{ 896, 4 },
{ 1024, 4 }
{ 128, 28 },
{ 256, 16 },
{ 384, 10 },
{ 512, 7 },
{ 640, 6 },
{ 768, 5 },
{ 896, 4 },
{ 1024, 4 }
};
/* returns # of RM trials required for a given bit size */

View File

@ -86,28 +86,42 @@ int mp_prime_random_ex(mp_int *a, int t, int size, int flags, ltm_prime_callback
tmp[bsize-1] |= maskOR_lsb;
/* read it in */
if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) { goto error; }
if ((err = mp_read_unsigned_bin(a, tmp, bsize)) != MP_OKAY) {
goto error;
}
/* is it prime? */
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
goto error;
}
if (res == MP_NO) {
continue;
}
if ((flags & LTM_PRIME_SAFE) != 0) {
/* see if (a-1)/2 is prime */
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; }
if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; }
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
goto error;
}
if ((err = mp_div_2(a, a)) != MP_OKAY) {
goto error;
}
/* is it prime? */
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) { goto error; }
if ((err = mp_prime_is_prime(a, t, &res)) != MP_OKAY) {
goto error;
}
}
} while (res == MP_NO);
if ((flags & LTM_PRIME_SAFE) != 0) {
/* restore a to the original value */
if ((err = mp_mul_2(a, a)) != MP_OKAY) { goto error; }
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) { goto error; }
if ((err = mp_mul_2(a, a)) != MP_OKAY) {
goto error;
}
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) {
goto error;
}
}
err = MP_OKAY;

View File

@ -16,7 +16,7 @@
*/
/* returns size of ASCII reprensentation */
int mp_radix_size (mp_int * a, int radix, int *size)
int mp_radix_size(mp_int *a, int radix, int *size)
{
int res, digs;
mp_int t;
@ -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;

View File

@ -16,13 +16,13 @@
*/
#if MP_GEN_RANDOM_MAX == 0xffffffff
#define MP_GEN_RANDOM_SHIFT 32
#define MP_GEN_RANDOM_SHIFT 32
#elif MP_GEN_RANDOM_MAX == 32767
/* SHRT_MAX */
#define MP_GEN_RANDOM_SHIFT 15
/* SHRT_MAX */
#define MP_GEN_RANDOM_SHIFT 15
#elif MP_GEN_RANDOM_MAX == 2147483647
/* INT_MAX */
#define MP_GEN_RANDOM_SHIFT 31
/* INT_MAX */
#define MP_GEN_RANDOM_SHIFT 31
#elif !defined(MP_GEN_RANDOM_SHIFT)
#error Thou shalt define their own valid MP_GEN_RANDOM_SHIFT
#endif
@ -41,13 +41,12 @@ static mp_digit s_gen_random(void)
return d;
}
int
mp_rand (mp_int * a, int digits)
int 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 +56,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;
}
}

View File

@ -16,7 +16,7 @@
*/
/* read a string [ASCII] in a given radix */
int mp_read_radix (mp_int * a, const char *str, int radix)
int mp_read_radix(mp_int *a, const char *str, int radix)
{
int y, res, neg;
char ch;
@ -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 {

View File

@ -16,12 +16,12 @@
*/
/* read signed bin, big endian, first byte is 0==positive or 1==negative */
int mp_read_signed_bin (mp_int * a, const unsigned char *b, int c)
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;
}

View File

@ -16,7 +16,7 @@
*/
/* 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)
int mp_read_unsigned_bin(mp_int *a, const unsigned char *b, int c)
{
int 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

View File

@ -19,31 +19,31 @@
* precomputed via mp_reduce_setup.
* From HAC pp.604 Algorithm 14.42
*/
int mp_reduce (mp_int * x, mp_int * m, mp_int * mu)
int mp_reduce(mp_int *x, mp_int *m, mp_int *mu)
{
mp_int q;
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;
}

View File

@ -18,14 +18,14 @@
/* pre-calculate the value required for Barrett reduction
* For a given modulus "b" it calulates the value required in "a"
*/
int mp_reduce_setup (mp_int * a, mp_int * b)
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

View File

@ -16,7 +16,7 @@
*/
/* shift right a certain amount of digits */
void mp_rshd (mp_int * a, int b)
void mp_rshd(mp_int *a, int b)
{
int x;
@ -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;
}

View File

@ -16,9 +16,9 @@
*/
/* set to a digit */
void mp_set (mp_int * a, mp_digit b)
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;
}

View File

@ -16,16 +16,16 @@
*/
/* set a 32-bit const */
int mp_set_int (mp_int * a, unsigned long b)
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

View File

@ -16,17 +16,17 @@
*/
/* shrink a bignum */
int mp_shrink (mp_int * a)
int mp_shrink(mp_int *a)
{
mp_digit *tmp;
int used = 1;
if(a->used > 0) {
if (a->used > 0) {
used = a->used;
}
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;

View File

@ -16,9 +16,9 @@
*/
/* get the size for an signed equivalent */
int mp_signed_bin_size (mp_int * a)
int mp_signed_bin_size(mp_int *a)
{
return 1 + mp_unsigned_bin_size (a);
return 1 + mp_unsigned_bin_size(a);
}
#endif

View File

@ -16,8 +16,7 @@
*/
/* computes b = a*a */
int
mp_sqr (mp_int * a, mp_int * b)
int mp_sqr(mp_int *a, mp_int *b)
{
int res;
@ -30,7 +29,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 +38,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

View File

@ -16,22 +16,21 @@
*/
/* c = a * a (mod b) */
int
mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
int 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

View File

@ -19,7 +19,7 @@
int mp_sqrt(mp_int *arg, mp_int *ret)
{
int res;
mp_int t1,t2;
mp_int t1, t2;
/* must be positive */
if (arg->sign == MP_NEG) {
@ -41,36 +41,38 @@ 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) {
if ((res = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
goto E1;
}
if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
if ((res = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
goto E1;
}
if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) {
goto E1;
}
/* And now t1 > sqrt(arg) */
do {
if ((res = mp_div(arg,&t1,&t2,NULL)) != MP_OKAY) {
if ((res = mp_div(arg, &t1, &t2, NULL)) != MP_OKAY) {
goto E1;
}
if ((res = mp_add(&t1,&t2,&t1)) != MP_OKAY) {
if ((res = mp_add(&t1, &t2, &t1)) != MP_OKAY) {
goto E1;
}
if ((res = mp_div_2(&t1,&t1)) != MP_OKAY) {
if ((res = mp_div_2(&t1, &t1)) != MP_OKAY) {
goto E1;
}
/* t1 >= sqrt(arg) >= t2 at this point */
} while (mp_cmp_mag(&t1,&t2) == MP_GT);
} while (mp_cmp_mag(&t1, &t2) == MP_GT);
mp_exch(&t1,ret);
mp_exch(&t1, ret);
E1: mp_clear(&t2);
E2: mp_clear(&t1);
E1:
mp_clear(&t2);
E2:
mp_clear(&t1);
return res;
}

View File

@ -66,7 +66,7 @@ int mp_sqrtmod_prime(mp_int *n, mp_int *prime, mp_int *ret)
/* find a Z such that the Legendre symbol (Z|prime) == -1 */
if ((res = mp_set_int(&Z, 2)) != MP_OKAY) goto cleanup;
/* Z = 2 */
while(1) {
while (1) {
if ((res = mp_jacobi(&Z, prime, &legendre)) != MP_OKAY) goto cleanup;
if (legendre == -1) break;
if ((res = mp_add_d(&Z, 1, &Z)) != MP_OKAY) goto cleanup;

View File

@ -16,8 +16,7 @@
*/
/* high level subtraction (handles signs) */
int
mp_sub (mp_int * a, mp_int * b, mp_int * c)
int mp_sub(mp_int *a, mp_int *b, mp_int *c)
{
int sa, sb, res;
@ -30,23 +29,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;

View File

@ -16,8 +16,7 @@
*/
/* single digit subtraction */
int
mp_sub_d (mp_int * a, mp_digit b, mp_int * c)
int mp_sub_d(mp_int *a, mp_digit b, mp_int *c)
{
mp_digit *tmpa, *tmpc, mu;
int res, ix, oldused;

View File

@ -16,23 +16,22 @@
*/
/* d = a - b (mod c) */
int
mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
int mp_submod(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_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

View File

@ -16,11 +16,11 @@
*/
/* store in signed [big endian] format */
int mp_to_signed_bin (mp_int * a, unsigned char *b)
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;

View File

@ -16,7 +16,7 @@
*/
/* store in signed [big endian] format */
int mp_to_signed_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
int mp_to_signed_bin_n(mp_int *a, unsigned char *b, unsigned long *outlen)
{
if (*outlen < (unsigned long)mp_signed_bin_size(a)) {
return MP_VAL;

View File

@ -16,29 +16,29 @@
*/
/* store in unsigned [big endian] format */
int mp_to_unsigned_bin (mp_int * a, unsigned char *b)
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

View File

@ -16,7 +16,7 @@
*/
/* store in unsigned [big endian] format */
int mp_to_unsigned_bin_n (mp_int * a, unsigned char *b, unsigned long *outlen)
int mp_to_unsigned_bin_n(mp_int *a, unsigned char *b, unsigned long *outlen)
{
if (*outlen < (unsigned long)mp_unsigned_bin_size(a)) {
return MP_VAL;

Some files were not shown because too many files have changed in this diff Show More