Merge pull request #85 from fperrad/20170830_format
rebase formating code
This commit is contained in:
commit
fedc15b625
27
astylerc
Normal file
27
astylerc
Normal 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
|
@ -138,7 +138,8 @@ top:
|
||||
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
|
||||
|
@ -20,8 +20,7 @@
|
||||
* 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;
|
||||
|
||||
|
@ -19,8 +19,7 @@
|
||||
*
|
||||
* 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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
@ -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.
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* clear one (frees) */
|
||||
void
|
||||
mp_clear (mp_int * a)
|
||||
void mp_clear(mp_int *a)
|
||||
{
|
||||
int i;
|
||||
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
15
bn_mp_div.c
15
bn_mp_div.c
@ -278,11 +278,16 @@ int mp_div (mp_int * a, mp_int * b, mp_int * c, mp_int * 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;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
@ -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;
|
||||
|
@ -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
|
||||
|
@ -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;
|
||||
|
||||
|
@ -19,7 +19,8 @@
|
||||
* 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 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;
|
||||
|
@ -51,8 +51,7 @@ 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) {
|
||||
|
@ -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 */
|
||||
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++) {
|
||||
|
@ -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:
|
||||
|
@ -94,8 +94,10 @@ int mp_gcd (mp_int * a, mp_int * b, mp_int * c)
|
||||
}
|
||||
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
|
||||
|
@ -19,7 +19,8 @@
|
||||
* 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 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;
|
||||
}
|
||||
|
||||
|
@ -165,7 +165,8 @@ top:
|
||||
/* C is now the inverse */
|
||||
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
|
||||
|
@ -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;
|
||||
ERR:mp_clear(&t);
|
||||
ERR:
|
||||
mp_clear(&t);
|
||||
return res;
|
||||
}
|
||||
#endif
|
||||
|
@ -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
|
||||
|
@ -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 */
|
||||
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;
|
||||
}
|
||||
|
@ -105,12 +105,18 @@ int mp_karatsuba_sqr (mp_int * a, mp_int * b)
|
||||
|
||||
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;
|
||||
}
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -120,9 +120,12 @@ int mp_n_root_ex (mp_int * a, mp_digit b, mp_int * c, int fast)
|
||||
|
||||
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
|
||||
|
@ -52,7 +52,8 @@ int mp_prime_fermat (mp_int * a, mp_int * b, int *result)
|
||||
}
|
||||
|
||||
err = MP_OKAY;
|
||||
LBL_T:mp_clear (&t);
|
||||
LBL_T:
|
||||
mp_clear(&t);
|
||||
return err;
|
||||
}
|
||||
#endif
|
||||
|
@ -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
|
||||
|
@ -91,9 +91,12 @@ int mp_prime_miller_rabin (mp_int * a, mp_int * b, int *result)
|
||||
|
||||
/* 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
|
||||
|
@ -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) {
|
||||
|
@ -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;
|
||||
|
@ -41,8 +41,7 @@ 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;
|
||||
|
@ -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;
|
||||
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
@ -69,8 +69,10 @@ int mp_sqrt(mp_int *arg, mp_int *ret)
|
||||
|
||||
mp_exch(&t1, ret);
|
||||
|
||||
E1: mp_clear(&t2);
|
||||
E2: mp_clear(&t1);
|
||||
E1:
|
||||
mp_clear(&t2);
|
||||
E2:
|
||||
mp_clear(&t1);
|
||||
return res;
|
||||
}
|
||||
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* 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;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* squaring using Toom-Cook 3-way algorithm */
|
||||
int
|
||||
mp_toom_sqr(mp_int *a, mp_int *b)
|
||||
int mp_toom_sqr(mp_int *a, mp_int *b)
|
||||
{
|
||||
mp_int w0, w1, w2, w3, w4, tmp1, a0, a1, a2;
|
||||
int res, B;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* XOR two ints together */
|
||||
int
|
||||
mp_xor (mp_int * a, mp_int * b, mp_int * c)
|
||||
int mp_xor(mp_int *a, mp_int *b, mp_int *c)
|
||||
{
|
||||
int res, ix, px;
|
||||
mp_int t, *x;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* reverse an array, used for radix code */
|
||||
void
|
||||
bn_reverse (unsigned char *s, int len)
|
||||
void bn_reverse(unsigned char *s, int len)
|
||||
{
|
||||
int ix, iy;
|
||||
unsigned char t;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* low level addition, based on HAC pp.594, Algorithm 14.7 */
|
||||
int
|
||||
s_mp_add (mp_int * a, mp_int * b, mp_int * c)
|
||||
int s_mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
{
|
||||
mp_int *x;
|
||||
int olduse, res, min, max;
|
||||
|
@ -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);
|
||||
err = MP_OKAY;
|
||||
LBL_RES:mp_clear (&res);
|
||||
LBL_MU:mp_clear (&mu);
|
||||
LBL_RES:
|
||||
mp_clear(&res);
|
||||
LBL_MU:
|
||||
mp_clear(&mu);
|
||||
LBL_M:
|
||||
mp_clear(&M[1]);
|
||||
for (x = 1<<(winsize-1); x < (1 << winsize); x++) {
|
||||
|
@ -18,8 +18,7 @@
|
||||
/* multiplies |a| * |b| and does not compute the lower digs digits
|
||||
* [meant to get the higher part of the product]
|
||||
*/
|
||||
int
|
||||
s_mp_mul_high_digs (mp_int * a, mp_int * b, mp_int * c, int digs)
|
||||
int s_mp_mul_high_digs(mp_int *a, mp_int *b, mp_int *c, int digs)
|
||||
{
|
||||
mp_int t;
|
||||
int res, pa, pb, ix, iy;
|
||||
|
@ -16,8 +16,7 @@
|
||||
*/
|
||||
|
||||
/* low level subtraction (assumes |a| > |b|), HAC pp.595 Algorithm 14.9 */
|
||||
int
|
||||
s_mp_sub (mp_int * a, mp_int * b, mp_int * c)
|
||||
int s_mp_sub(mp_int *a, mp_int *b, mp_int *c)
|
||||
{
|
||||
int olduse, res, min, max;
|
||||
|
||||
|
57
dep.pl
57
dep.pl
@ -9,7 +9,18 @@ my %deplist;
|
||||
|
||||
#open class file and write preamble
|
||||
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') {
|
||||
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
|
||||
$define =~ tr/[a-z]/[A-Z]/;
|
||||
$define =~ tr/\./_/;
|
||||
print {$class} "#define $define\n";
|
||||
print {$class} << "EOS";
|
||||
# define $define
|
||||
EOS
|
||||
|
||||
# now copy text and apply #ifdef as required
|
||||
my $apply = 0;
|
||||
@ -31,7 +44,11 @@ foreach my $filename (glob 'bn*.c') {
|
||||
if ($line =~ /include/) {
|
||||
print {$out} $line;
|
||||
} else {
|
||||
print {$out} "#include <tommath.h>\n#ifdef $define\n$line";
|
||||
print {$out} << "EOS";
|
||||
#include <tommath.h>
|
||||
#ifdef $define
|
||||
$line
|
||||
EOS
|
||||
$apply = 1;
|
||||
}
|
||||
while (<$src>) {
|
||||
@ -40,7 +57,9 @@ foreach my $filename (glob 'bn*.c') {
|
||||
}
|
||||
}
|
||||
if ($apply == 1) {
|
||||
print {$out} "#endif\n";
|
||||
print {$out} << 'EOS';
|
||||
#endif
|
||||
EOS
|
||||
}
|
||||
close $src;
|
||||
close $out;
|
||||
@ -48,7 +67,10 @@ foreach my $filename (glob 'bn*.c') {
|
||||
unlink $filename;
|
||||
rename 'tmp', $filename;
|
||||
}
|
||||
print {$class} "#endif\n\n";
|
||||
print {$class} << 'EOS';
|
||||
#endif
|
||||
|
||||
EOS
|
||||
|
||||
# now do classes
|
||||
|
||||
@ -59,7 +81,9 @@ foreach my $filename (glob 'bn*.c') {
|
||||
$filename =~ tr/[a-z]/[A-Z]/;
|
||||
$filename =~ tr/\./_/;
|
||||
|
||||
print {$class} "#if defined($filename)\n";
|
||||
print {$class} << "EOS";
|
||||
#if defined($filename)
|
||||
EOS
|
||||
my $list = $filename;
|
||||
|
||||
# scan for mp_* and make classes
|
||||
@ -74,7 +98,9 @@ foreach my $filename (glob 'bn*.c') {
|
||||
$a =~ tr/[a-z]/[A-Z]/;
|
||||
$a = 'BN_' . $a . '_C';
|
||||
if (!($list =~ /$a/)) {
|
||||
print {$class} " #define $a\n";
|
||||
print {$class} << "EOS";
|
||||
# define $a
|
||||
EOS
|
||||
}
|
||||
$list = $list . ',' . $a;
|
||||
}
|
||||
@ -82,11 +108,24 @@ foreach my $filename (glob 'bn*.c') {
|
||||
}
|
||||
$deplist{$filename} = $list;
|
||||
|
||||
print {$class} "#endif\n\n";
|
||||
print {$class} << 'EOS';
|
||||
#endif
|
||||
|
||||
EOS
|
||||
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;
|
||||
|
||||
#now let's make a cool call graph...
|
||||
|
@ -1050,6 +1050,7 @@
|
||||
#ifdef LTM3
|
||||
# define LTM_LAST
|
||||
#endif
|
||||
|
||||
#include <tommath_superclass.h>
|
||||
#include <tommath_class.h>
|
||||
#else
|
||||
|
Loading…
x
Reference in New Issue
Block a user