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

@ -138,7 +138,8 @@ top:
c->sign = neg; c->sign = neg;
res = MP_OKAY; 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; return res;
} }
#endif #endif

View File

@ -20,8 +20,7 @@
* Simple algorithm which zeroes the int, grows it then just sets one bit * Simple algorithm which zeroes the int, grows it then just sets one bit
* as required. * as required.
*/ */
int int mp_2expt(mp_int *a, int b)
mp_2expt (mp_int * a, int b)
{ {
int res; int res;

View File

@ -19,8 +19,7 @@
* *
* Simple function copies the input and fixes the sign to positive * Simple function copies the input and fixes the sign to positive
*/ */
int int mp_abs(mp_int *a, mp_int *b)
mp_abs (mp_int * a, mp_int * b)
{ {
int res; int res;

View File

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

View File

@ -16,8 +16,7 @@
*/ */
/* d = a + b (mod c) */ /* d = a + b (mod c) */
int int mp_addmod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
mp_addmod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{ {
int res; int res;
mp_int t; mp_int t;

View File

@ -16,8 +16,7 @@
*/ */
/* AND two ints together */ /* AND two ints together */
int int mp_and(mp_int *a, mp_int *b, mp_int *c)
mp_and (mp_int * a, mp_int * b, mp_int * c)
{ {
int res, ix, px; int res, ix, px;
mp_int t, *x; mp_int t, *x;

View File

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

View File

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

View File

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

View File

@ -16,8 +16,7 @@
*/ */
/* copy, b = a */ /* copy, b = a */
int int mp_copy(mp_int *a, mp_int *b)
mp_copy (mp_int * a, mp_int * b)
{ {
int res, n; int res, n;

View File

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

View File

@ -278,11 +278,16 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
res = MP_OKAY; res = MP_OKAY;
LBL_Y:mp_clear (&y); LBL_Y:
LBL_X:mp_clear (&x); mp_clear(&y);
LBL_T2:mp_clear (&t2); LBL_X:
LBL_T1:mp_clear (&t1); mp_clear(&x);
LBL_Q:mp_clear (&q); LBL_T2:
mp_clear(&t2);
LBL_T1:
mp_clear(&t1);
LBL_Q:
mp_clear(&q);
return res; return res;
} }

View File

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

View File

@ -29,8 +29,7 @@
* *
* Input x must be in the range 0 <= x <= (n-1)**2 * Input x must be in the range 0 <= x <= (n-1)**2
*/ */
int int mp_dr_reduce(mp_int *x, mp_int *n, mp_digit k)
mp_dr_reduce (mp_int * x, mp_int * n, mp_digit k)
{ {
int err, i, m; int err, i, m;
mp_word r; mp_word r;

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 casts are required if DIGIT_BIT is one less than
* the number of bits in a mp_digit [e.g. DIGIT_BIT==31] * the number of bits in a mp_digit [e.g. DIGIT_BIT==31]
*/ */
*d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - *d = (mp_digit)((((mp_word)1) << ((mp_word)DIGIT_BIT)) - ((mp_word)a->dp[0]));
((mp_word)a->dp[0]));
} }
#endif #endif

View File

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

View File

@ -19,7 +19,8 @@
* see http://gmplib.org/manual/Integer-Import-and-Export.html * see http://gmplib.org/manual/Integer-Import-and-Export.html
*/ */
int mp_export(void *rop, size_t *countp, int order, size_t size, int mp_export(void *rop, size_t *countp, int order, size_t size,
int endian, size_t nails, mp_int* op) { int endian, size_t nails, mp_int *op)
{
int result; int result;
size_t odd_nails, nail_bytes, i, j, bits, count; size_t odd_nails, nail_bytes, i, j, bits, count;
unsigned char odd_nail_mask; 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 (i = 0; i < count; ++i) {
for (j = 0; j < size; ++j) { for (j = 0; j < size; ++j) {
unsigned char* byte = ( unsigned char *byte = (unsigned char *)rop +
(unsigned char*)rop +
(((order == -1) ? i : ((count - 1) - i)) * size) + (((order == -1) ? i : ((count - 1) - i)) * size) +
((endian == -1) ? j : ((size - 1) - j)) ((endian == -1) ? j : ((size - 1) - j));
);
if (j >= (size - nail_bytes)) { if (j >= (size - nail_bytes)) {
*byte = 0; *byte = 0;

View File

@ -51,8 +51,7 @@ int mp_expt_d_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
/* shift to next bit */ /* shift to next bit */
b >>= 1; b >>= 1;
} }
} } else {
else {
for (x = 0; x < DIGIT_BIT; x++) { for (x = 0; x < DIGIT_BIT; x++) {
/* square */ /* square */
if ((res = mp_sqr(c, c)) != MP_OKAY) { if ((res = mp_sqr(c, c)) != MP_OKAY) {

View File

@ -305,7 +305,8 @@ int mp_exptmod_fast (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode
/* swap res with Y */ /* swap res with Y */
mp_exch(&res, Y); mp_exch(&res, Y);
err = MP_OKAY; err = MP_OKAY;
LBL_RES:mp_clear (&res); LBL_RES:
mp_clear(&res);
LBL_M: LBL_M:
mp_clear(&M[1]); mp_clear(&M[1]);
for (x = 1<<(winsize-1); x < (1 << winsize); x++) { for (x = 1<<(winsize-1); x < (1 << winsize); x++) {

View File

@ -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) */ /* initialize, (u1,u2,u3) = (1,0,a) */
mp_set(&u1, 1); 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) */ /* initialize, (v1,v2,v3) = (0,1,b) */
mp_set(&v2, 1); 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 */ /* loop while v3 != 0 */
while (mp_iszero(&v3) == MP_NO) { while (mp_iszero(&v3) == MP_NO) {
/* q = u3/v3 */ /* 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 */ /* (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_mul(&v1, &q, &tmp)) != MP_OKAY) {
if ((err = mp_sub(&u1, &tmp, &t1)) != MP_OKAY) { goto LBL_ERR; } 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_sub(&u1, &tmp, &t1)) != MP_OKAY) {
if ((err = mp_mul(&v3, &q, &tmp)) != MP_OKAY) { goto LBL_ERR; } goto LBL_ERR;
if ((err = mp_sub(&u3, &tmp, &t3)) != 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) */ /* (u1,u2,u3) = (v1,v2,v3) */
if ((err = mp_copy(&v1, &u1)) != MP_OKAY) { goto LBL_ERR; } if ((err = mp_copy(&v1, &u1)) != MP_OKAY) {
if ((err = mp_copy(&v2, &u2)) != MP_OKAY) { goto LBL_ERR; } goto LBL_ERR;
if ((err = mp_copy(&v3, &u3)) != 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) */ /* (v1,v2,v3) = (t1,t2,t3) */
if ((err = mp_copy(&t1, &v1)) != MP_OKAY) { goto LBL_ERR; } if ((err = mp_copy(&t1, &v1)) != MP_OKAY) {
if ((err = mp_copy(&t2, &v2)) != MP_OKAY) { goto LBL_ERR; } goto LBL_ERR;
if ((err = mp_copy(&t3, &v3)) != 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 */ /* make sure U3 >= 0 */
if (u3.sign == MP_NEG) { if (u3.sign == MP_NEG) {
if ((err = mp_neg(&u1, &u1)) != MP_OKAY) { goto LBL_ERR; } if ((err = mp_neg(&u1, &u1)) != MP_OKAY) {
if ((err = mp_neg(&u2, &u2)) != MP_OKAY) { goto LBL_ERR; } goto LBL_ERR;
if ((err = mp_neg(&u3, &u3)) != 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 */ /* copy result out */
if (U1 != NULL) { mp_exch(U1, &u1); } if (U1 != NULL) {
if (U2 != NULL) { mp_exch(U2, &u2); } mp_exch(U1, &u1);
if (U3 != NULL) { mp_exch(U3, &u3); } }
if (U2 != NULL) {
mp_exch(U2, &u2);
}
if (U3 != NULL) {
mp_exch(U3, &u3);
}
err = MP_OKAY; err = MP_OKAY;
LBL_ERR: LBL_ERR:

View File

@ -94,8 +94,10 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
} }
c->sign = MP_ZPOS; c->sign = MP_ZPOS;
res = MP_OKAY; res = MP_OKAY;
LBL_V:mp_clear (&u); LBL_V:
LBL_U:mp_clear (&v); mp_clear(&u);
LBL_U:
mp_clear(&v);
return res; return res;
} }
#endif #endif

View File

@ -19,7 +19,8 @@
* see http://gmplib.org/manual/Integer-Import-and-Export.html * 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 mp_import(mp_int *rop, size_t count, int order, size_t size,
int endian, size_t nails, const void* op) { int endian, size_t nails, const void *op)
{
int result; int result;
size_t odd_nails, nail_bytes, i, j; size_t odd_nails, nail_bytes, i, j;
unsigned char odd_nail_mask; 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 (i = 0; i < count; ++i) {
for (j = 0; j < (size - nail_bytes); ++j) { for (j = 0; j < (size - nail_bytes); ++j) {
unsigned char byte = *( unsigned char byte = *((unsigned char *)op +
(unsigned char*)op +
(((order == 1) ? i : ((count - 1) - i)) * size) + (((order == 1) ? i : ((count - 1) - i)) * size) +
((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)) ((endian == 1) ? (j + nail_bytes) : (((size - 1) - j) - nail_bytes)));
);
if ( if ((result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
(result = mp_mul_2d(rop, ((j == 0) ? (8 - odd_nails) : 8), rop)) != MP_OKAY) {
return result; return result;
} }

View File

@ -165,7 +165,8 @@ top:
/* C is now the inverse */ /* C is now the inverse */
mp_exch(&C, c); mp_exch(&C, c);
res = MP_OKAY; 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; return res;
} }
#endif #endif

View File

@ -99,7 +99,8 @@ int mp_is_square(mp_int *arg,int *ret)
} }
*ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO; *ret = (mp_cmp_mag(&t, arg) == MP_EQ) ? MP_YES : MP_NO;
ERR:mp_clear(&t); ERR:
mp_clear(&t);
return res; return res;
} }
#endif #endif

View File

@ -106,8 +106,10 @@ int mp_jacobi (mp_int * a, mp_int * n, int *c)
/* done */ /* done */
res = MP_OKAY; res = MP_OKAY;
LBL_P1:mp_clear (&p1); LBL_P1:
LBL_A1:mp_clear (&a1); mp_clear(&p1);
LBL_A1:
mp_clear(&a1);
return res; return res;
} }
#endif #endif

View File

@ -150,13 +150,20 @@ int mp_karatsuba_mul (mp_int * a, mp_int * b, mp_int * c)
/* Algorithm succeeded set the return code to MP_OKAY */ /* Algorithm succeeded set the return code to MP_OKAY */
err = MP_OKAY; err = MP_OKAY;
X1Y1:mp_clear (&x1y1); X1Y1:
X0Y0:mp_clear (&x0y0); mp_clear(&x1y1);
T1:mp_clear (&t1); X0Y0:
Y1:mp_clear (&y1); mp_clear(&x0y0);
Y0:mp_clear (&y0); T1:
X1:mp_clear (&x1); mp_clear(&t1);
X0:mp_clear (&x0); Y1:
mp_clear(&y1);
Y0:
mp_clear(&y0);
X1:
mp_clear(&x1);
X0:
mp_clear(&x0);
ERR: ERR:
return err; return err;
} }

View File

@ -105,12 +105,18 @@ int mp_karatsuba_sqr (mp_int * a, mp_int * b)
err = MP_OKAY; err = MP_OKAY;
X1X1:mp_clear (&x1x1); X1X1:
X0X0:mp_clear (&x0x0); mp_clear(&x1x1);
T2:mp_clear (&t2); X0X0:
T1:mp_clear (&t1); mp_clear(&x0x0);
X1:mp_clear (&x1); T2:
X0:mp_clear (&x0); mp_clear(&t2);
T1:
mp_clear(&t1);
X1:
mp_clear(&x1);
X0:
mp_clear(&x0);
ERR: ERR:
return err; return err;
} }

View File

@ -16,8 +16,7 @@
*/ */
/* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */ /* c = a mod b, 0 <= c < b if b > 0, b < c <= 0 if b < 0 */
int int mp_mod(mp_int *a, mp_int *b, mp_int *c)
mp_mod (mp_int * a, mp_int * b, mp_int * c)
{ {
mp_int t; mp_int t;
int res; int res;

View File

@ -16,8 +16,7 @@
*/ */
/* calc a value mod 2**b */ /* calc a value mod 2**b */
int int mp_mod_2d(mp_int *a, int b, mp_int *c)
mp_mod_2d (mp_int * a, int b, mp_int * c)
{ {
int x, res; int x, res;

View File

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

View File

@ -16,8 +16,7 @@
*/ */
/* computes xR**-1 == x (mod N) via Montgomery Reduction */ /* computes xR**-1 == x (mod N) via Montgomery Reduction */
int int mp_montgomery_reduce(mp_int *x, mp_int *n, mp_digit rho)
mp_montgomery_reduce (mp_int * x, mp_int * n, mp_digit rho)
{ {
int ix, res, digs; int ix, res, digs;
mp_digit mu; mp_digit mu;

View File

@ -16,8 +16,7 @@
*/ */
/* setups the montgomery reduction stuff */ /* setups the montgomery reduction stuff */
int int mp_montgomery_setup(mp_int *n, mp_digit *rho)
mp_montgomery_setup (mp_int * n, mp_digit * rho)
{ {
mp_digit x, b; mp_digit x, b;

View File

@ -16,8 +16,7 @@
*/ */
/* multiply by a digit */ /* multiply by a digit */
int int mp_mul_d(mp_int *a, mp_digit b, mp_int *c)
mp_mul_d (mp_int * a, mp_digit b, mp_int * c)
{ {
mp_digit u, *tmpa, *tmpc; mp_digit u, *tmpa, *tmpc;
mp_word r; mp_word r;

View File

@ -120,9 +120,12 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
res = MP_OKAY; res = MP_OKAY;
LBL_T3:mp_clear (&t3); LBL_T3:
LBL_T2:mp_clear (&t2); mp_clear(&t3);
LBL_T1:mp_clear (&t1); LBL_T2:
mp_clear(&t2);
LBL_T1:
mp_clear(&t1);
return res; return res;
} }
#endif #endif

View File

@ -52,7 +52,8 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
} }
err = MP_OKAY; err = MP_OKAY;
LBL_T:mp_clear (&t); LBL_T:
mp_clear(&t);
return err; return err;
} }
#endif #endif

View File

@ -73,7 +73,8 @@ int mp_prime_is_prime (mp_int * a, int t, int *result)
/* passed the test */ /* passed the test */
*result = MP_YES; *result = MP_YES;
LBL_B:mp_clear (&b); LBL_B:
mp_clear(&b);
return err; return err;
} }
#endif #endif

View File

@ -91,9 +91,12 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
/* probably prime now */ /* probably prime now */
*result = MP_YES; *result = MP_YES;
LBL_Y:mp_clear (&y); LBL_Y:
LBL_R:mp_clear (&r); mp_clear(&y);
LBL_N1:mp_clear (&n1); LBL_R:
mp_clear(&r);
LBL_N1:
mp_clear(&n1);
return err; return err;
} }
#endif #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 (bbs_style == 1) {
/* if a mod 4 != 3 subtract the correct value to make it so */ /* if a mod 4 != 3 subtract the correct value to make it so */
if ((a->dp[0] & 3) != 3) { 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 { } else {
if (mp_iseven(a) == MP_YES) { if (mp_iseven(a) == MP_YES) {

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; tmp[bsize-1] |= maskOR_lsb;
/* read it in */ /* 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? */ /* 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) { if (res == MP_NO) {
continue; continue;
} }
if ((flags & LTM_PRIME_SAFE) != 0) { if ((flags & LTM_PRIME_SAFE) != 0) {
/* see if (a-1)/2 is prime */ /* see if (a-1)/2 is prime */
if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) { goto error; } if ((err = mp_sub_d(a, 1, a)) != MP_OKAY) {
if ((err = mp_div_2(a, a)) != MP_OKAY) { goto error; } goto error;
}
if ((err = mp_div_2(a, a)) != MP_OKAY) {
goto error;
}
/* is it prime? */ /* 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); } while (res == MP_NO);
if ((flags & LTM_PRIME_SAFE) != 0) { if ((flags & LTM_PRIME_SAFE) != 0) {
/* restore a to the original value */ /* restore a to the original value */
if ((err = mp_mul_2(a, a)) != MP_OKAY) { goto error; } if ((err = mp_mul_2(a, a)) != MP_OKAY) {
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) { goto error; } goto error;
}
if ((err = mp_add_d(a, 1, a)) != MP_OKAY) {
goto error;
}
} }
err = MP_OKAY; err = MP_OKAY;

View File

@ -41,8 +41,7 @@ static mp_digit s_gen_random(void)
return d; return d;
} }
int int mp_rand(mp_int *a, int digits)
mp_rand (mp_int * a, int digits)
{ {
int res; int res;
mp_digit d; mp_digit d;

View File

@ -16,8 +16,7 @@
*/ */
/* computes b = a*a */ /* computes b = a*a */
int int mp_sqr(mp_int *a, mp_int *b)
mp_sqr (mp_int * a, mp_int * b)
{ {
int res; int res;

View File

@ -16,8 +16,7 @@
*/ */
/* c = a * a (mod b) */ /* c = a * a (mod b) */
int int mp_sqrmod(mp_int *a, mp_int *b, mp_int *c)
mp_sqrmod (mp_int * a, mp_int * b, mp_int * c)
{ {
int res; int res;
mp_int t; mp_int t;

View File

@ -69,8 +69,10 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
mp_exch(&t1, ret); mp_exch(&t1, ret);
E1: mp_clear(&t2); E1:
E2: mp_clear(&t1); mp_clear(&t2);
E2:
mp_clear(&t1);
return res; return res;
} }

View File

@ -16,8 +16,7 @@
*/ */
/* high level subtraction (handles signs) */ /* high level subtraction (handles signs) */
int int mp_sub(mp_int *a, mp_int *b, mp_int *c)
mp_sub (mp_int * a, mp_int * b, mp_int * c)
{ {
int sa, sb, res; int sa, sb, res;

View File

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

View File

@ -16,8 +16,7 @@
*/ */
/* d = a - b (mod c) */ /* d = a - b (mod c) */
int int mp_submod(mp_int *a, mp_int *b, mp_int *c, mp_int *d)
mp_submod (mp_int * a, mp_int * b, mp_int * c, mp_int * d)
{ {
int res; int res;
mp_int t; mp_int t;

View File

@ -16,8 +16,7 @@
*/ */
/* squaring using Toom-Cook 3-way algorithm */ /* squaring using Toom-Cook 3-way algorithm */
int int mp_toom_sqr(mp_int *a, mp_int *b)
mp_toom_sqr(mp_int *a, mp_int *b)
{ {
mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2; mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
int res, B; int res, B;

View File

@ -16,8 +16,7 @@
*/ */
/* XOR two ints together */ /* XOR two ints together */
int int mp_xor(mp_int *a, mp_int *b, mp_int *c)
mp_xor (mp_int * a, mp_int * b, mp_int * c)
{ {
int res, ix, px; int res, ix, px;
mp_int t, *x; mp_int t, *x;

View File

@ -16,8 +16,7 @@
*/ */
/* reverse an array, used for radix code */ /* reverse an array, used for radix code */
void void bn_reverse(unsigned char *s, int len)
bn_reverse (unsigned char *s, int len)
{ {
int ix, iy; int ix, iy;
unsigned char t; unsigned char t;

View File

@ -16,8 +16,7 @@
*/ */
/* low level addition, based on HAC pp.594, Algorithm 14.7 */ /* low level addition, based on HAC pp.594, Algorithm 14.7 */
int int s_mp_add(mp_int *a, mp_int *b, mp_int *c)
s_mp_add (mp_int * a, mp_int * b, mp_int * c)
{ {
mp_int *x; mp_int *x;
int olduse, res, min, max; int olduse, res, min, max;

View File

@ -236,8 +236,10 @@ int s_mp_exptmod (mp_int * G, mp_int * X, mp_int * P, mp_int * Y, int redmode)
mp_exch(&res, Y); mp_exch(&res, Y);
err = MP_OKAY; err = MP_OKAY;
LBL_RES:mp_clear (&res); LBL_RES:
LBL_MU:mp_clear (&mu); mp_clear(&res);
LBL_MU:
mp_clear(&mu);
LBL_M: LBL_M:
mp_clear(&M[1]); mp_clear(&M[1]);
for (x = 1<<(winsize-1); x < (1 << winsize); x++) { for (x = 1<<(winsize-1); x < (1 << winsize); x++) {

View File

@ -18,8 +18,7 @@
/* multiplies |a| * |b| and does not compute the lower digs digits /* multiplies |a| * |b| and does not compute the lower digs digits
* [meant to get the higher part of the product] * [meant to get the higher part of the product]
*/ */
int int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs)
s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
{ {
mp_int t; mp_int t;
int res, pa, pb, ix, iy; int res, pa, pb, ix, iy;

View File

@ -16,8 +16,7 @@
*/ */
/* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */ /* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
int int s_mp_sub(mp_int *a, mp_int *b, mp_int *c)
s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
{ {
int olduse, res, min, max; int olduse, res, min, max;

57
dep.pl
View File

@ -9,7 +9,18 @@ my %deplist;
#open class file and write preamble #open class file and write preamble
open(my $class, '>', 'tommath_class.h') or die "Couldn't open tommath_class.h for writing\n"; open(my $class, '>', 'tommath_class.h') or die "Couldn't open tommath_class.h for writing\n";
print {$class} "#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))\n#if defined(LTM2)\n#define LTM3\n#endif\n#if defined(LTM1)\n#define LTM2\n#endif\n#define LTM1\n\n#if defined(LTM_ALL)\n"; print {$class} << 'EOS';
#if !(defined(LTM1) && defined(LTM2) && defined(LTM3))
#if defined(LTM2)
# define LTM3
#endif
#if defined(LTM1)
# define LTM2
#endif
#define LTM1
#if defined(LTM_ALL)
EOS
foreach my $filename (glob 'bn*.c') { foreach my $filename (glob 'bn*.c') {
my $define = $filename; my $define = $filename;
@ -19,7 +30,9 @@ foreach my $filename (glob 'bn*.c') {
# convert filename to upper case so we can use it as a define # convert filename to upper case so we can use it as a define
$define =~ tr/[a-z]/[A-Z]/; $define =~ tr/[a-z]/[A-Z]/;
$define =~ tr/\./_/; $define =~ tr/\./_/;
print {$class} "#define $define\n"; print {$class} << "EOS";
# define $define
EOS
# now copy text and apply #ifdef as required # now copy text and apply #ifdef as required
my $apply = 0; my $apply = 0;
@ -31,7 +44,11 @@ foreach my $filename (glob 'bn*.c') {
if ($line =~ /include/) { if ($line =~ /include/) {
print {$out} $line; print {$out} $line;
} else { } else {
print {$out} "#include <tommath.h>\n#ifdef $define\n$line"; print {$out} << "EOS";
#include <tommath.h>
#ifdef $define
$line
EOS
$apply = 1; $apply = 1;
} }
while (<$src>) { while (<$src>) {
@ -40,7 +57,9 @@ foreach my $filename (glob 'bn*.c') {
} }
} }
if ($apply == 1) { if ($apply == 1) {
print {$out} "#endif\n"; print {$out} << 'EOS';
#endif
EOS
} }
close $src; close $src;
close $out; close $out;
@ -48,7 +67,10 @@ foreach my $filename (glob 'bn*.c') {
unlink $filename; unlink $filename;
rename 'tmp', $filename; rename 'tmp', $filename;
} }
print {$class} "#endif\n\n"; print {$class} << 'EOS';
#endif
EOS
# now do classes # now do classes
@ -59,7 +81,9 @@ foreach my $filename (glob 'bn*.c') {
$filename =~ tr/[a-z]/[A-Z]/; $filename =~ tr/[a-z]/[A-Z]/;
$filename =~ tr/\./_/; $filename =~ tr/\./_/;
print {$class} "#if defined($filename)\n"; print {$class} << "EOS";
#if defined($filename)
EOS
my $list = $filename; my $list = $filename;
# scan for mp_* and make classes # scan for mp_* and make classes
@ -74,7 +98,9 @@ foreach my $filename (glob 'bn*.c') {
$a =~ tr/[a-z]/[A-Z]/; $a =~ tr/[a-z]/[A-Z]/;
$a = 'BN_' . $a . '_C'; $a = 'BN_' . $a . '_C';
if (!($list =~ /$a/)) { if (!($list =~ /$a/)) {
print {$class} " #define $a\n"; print {$class} << "EOS";
# define $a
EOS
} }
$list = $list . ',' . $a; $list = $list . ',' . $a;
} }
@ -82,11 +108,24 @@ foreach my $filename (glob 'bn*.c') {
} }
$deplist{$filename} = $list; $deplist{$filename} = $list;
print {$class} "#endif\n\n"; print {$class} << 'EOS';
#endif
EOS
close $src; close $src;
} }
print {$class} "#ifdef LTM3\n#define LTM_LAST\n#endif\n#include <tommath_superclass.h>\n#include <tommath_class.h>\n#else\n#define LTM_LAST\n#endif\n"; print {$class} << 'EOS';
#ifdef LTM3
# define LTM_LAST
#endif
#include <tommath_superclass.h>
#include <tommath_class.h>
#else
# define LTM_LAST
#endif
EOS
close $class; close $class;
#now let's make a cool call graph... #now let's make a cool call graph...

View File

@ -1050,6 +1050,7 @@
#ifdef LTM3 #ifdef LTM3
# define LTM_LAST # define LTM_LAST
#endif #endif
#include <tommath_superclass.h> #include <tommath_superclass.h>
#include <tommath_class.h> #include <tommath_class.h>
#else #else