hcrypto: import libtommath v1.2.0
This commit is contained in:
@@ -1,24 +1,24 @@
|
||||
const float s_logv_2[] = {
|
||||
0.000000000, 0.000000000, 1.000000000, 0.630929754, /* 0 1 2 3 */
|
||||
0.500000000, 0.430676558, 0.386852807, 0.356207187, /* 4 5 6 7 */
|
||||
0.333333333, 0.315464877, 0.301029996, 0.289064826, /* 8 9 10 11 */
|
||||
0.278942946, 0.270238154, 0.262649535, 0.255958025, /* 12 13 14 15 */
|
||||
0.250000000, 0.244650542, 0.239812467, 0.235408913, /* 16 17 18 19 */
|
||||
0.231378213, 0.227670249, 0.224243824, 0.221064729, /* 20 21 22 23 */
|
||||
0.218104292, 0.215338279, 0.212746054, 0.210309918, /* 24 25 26 27 */
|
||||
0.208014598, 0.205846832, 0.203795047, 0.201849087, /* 28 29 30 31 */
|
||||
0.200000000, 0.198239863, 0.196561632, 0.194959022, /* 32 33 34 35 */
|
||||
0.193426404, 0.191958720, 0.190551412, 0.189200360, /* 36 37 38 39 */
|
||||
0.187901825, 0.186652411, 0.185449023, 0.184288833, /* 40 41 42 43 */
|
||||
0.183169251, 0.182087900, 0.181042597, 0.180031327, /* 44 45 46 47 */
|
||||
0.179052232, 0.178103594, 0.177183820, 0.176291434, /* 48 49 50 51 */
|
||||
0.175425064, 0.174583430, 0.173765343, 0.172969690, /* 52 53 54 55 */
|
||||
0.172195434, 0.171441601, 0.170707280, 0.169991616, /* 56 57 58 59 */
|
||||
0.169293808, 0.168613099, 0.167948779, 0.167300179, /* 60 61 62 63 */
|
||||
0.000000000, 0.000000000, 1.000000000, 0.630929754, /* 0 1 2 3 */
|
||||
0.500000000, 0.430676558, 0.386852807, 0.356207187, /* 4 5 6 7 */
|
||||
0.333333333, 0.315464877, 0.301029996, 0.289064826, /* 8 9 10 11 */
|
||||
0.278942946, 0.270238154, 0.262649535, 0.255958025, /* 12 13 14 15 */
|
||||
0.250000000, 0.244650542, 0.239812467, 0.235408913, /* 16 17 18 19 */
|
||||
0.231378213, 0.227670249, 0.224243824, 0.221064729, /* 20 21 22 23 */
|
||||
0.218104292, 0.215338279, 0.212746054, 0.210309918, /* 24 25 26 27 */
|
||||
0.208014598, 0.205846832, 0.203795047, 0.201849087, /* 28 29 30 31 */
|
||||
0.200000000, 0.198239863, 0.196561632, 0.194959022, /* 32 33 34 35 */
|
||||
0.193426404, 0.191958720, 0.190551412, 0.189200360, /* 36 37 38 39 */
|
||||
0.187901825, 0.186652411, 0.185449023, 0.184288833, /* 40 41 42 43 */
|
||||
0.183169251, 0.182087900, 0.181042597, 0.180031327, /* 44 45 46 47 */
|
||||
0.179052232, 0.178103594, 0.177183820, 0.176291434, /* 48 49 50 51 */
|
||||
0.175425064, 0.174583430, 0.173765343, 0.172969690, /* 52 53 54 55 */
|
||||
0.172195434, 0.171441601, 0.170707280, 0.169991616, /* 56 57 58 59 */
|
||||
0.169293808, 0.168613099, 0.167948779, 0.167300179, /* 60 61 62 63 */
|
||||
0.166666667
|
||||
};
|
||||
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/mtest/logtab.h,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
/* $Date: 2005/05/05 14:38:47 $ */
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
@@ -1,5 +1,5 @@
|
||||
/* Default configuration for MPI library */
|
||||
/* $Id: mpi-config.h,v 1.2 2005/05/05 14:38:47 tom Exp $ */
|
||||
/* $Id$ */
|
||||
|
||||
#ifndef MPI_CONFIG_H_
|
||||
#define MPI_CONFIG_H_
|
||||
@@ -85,6 +85,6 @@
|
||||
|
||||
/* crc==3287762869, version==2, Sat Feb 02 06:43:53 2002 */
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/mtest/mpi-config.h,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
/* $Date: 2005/05/05 14:38:47 $ */
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
@@ -15,6 +15,6 @@ typedef int mp_err;
|
||||
#define RADIX (MP_DIGIT_MAX+1)
|
||||
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/mtest/mpi-types.h,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
/* $Date: 2005/05/05 14:38:47 $ */
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
@@ -2,11 +2,13 @@
|
||||
mpi.c
|
||||
|
||||
by Michael J. Fromberger <sting@linguist.dartmouth.edu>
|
||||
Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved
|
||||
Copyright (C) 1998 Michael J. Fromberger
|
||||
|
||||
Arbitrary precision integer arithmetic library
|
||||
|
||||
$Id: mpi.c,v 1.2 2005/05/05 14:38:47 tom Exp $
|
||||
SPDX-License-Identifier: Unlicense
|
||||
|
||||
$Id$
|
||||
*/
|
||||
|
||||
#include "mpi.h"
|
||||
@@ -176,7 +178,7 @@ mp_err s_mp_add_d(mp_int *mp, mp_digit d); /* unsigned digit addition */
|
||||
mp_err s_mp_sub_d(mp_int *mp, mp_digit d); /* unsigned digit subtract */
|
||||
mp_err s_mp_mul_d(mp_int *mp, mp_digit d); /* unsigned digit multiply */
|
||||
mp_err s_mp_div_d(mp_int *mp, mp_digit d, mp_digit *r);
|
||||
/* unsigned digit divide */
|
||||
/* unsigned digit divide */
|
||||
mp_err s_mp_reduce(mp_int *x, mp_int *m, mp_int *mu);
|
||||
/* Barrett reduction */
|
||||
mp_err s_mp_add(mp_int *a, mp_int *b); /* magnitude addition */
|
||||
@@ -358,15 +360,15 @@ mp_err mp_copy(mp_int *from, mp_int *to)
|
||||
|
||||
} else {
|
||||
if((tmp = s_mp_alloc(USED(from), sizeof(mp_digit))) == NULL)
|
||||
return MP_MEM;
|
||||
return MP_MEM;
|
||||
|
||||
s_mp_copy(DIGITS(from), tmp, USED(from));
|
||||
|
||||
if(DIGITS(to) != NULL) {
|
||||
#if MP_CRYPTO
|
||||
s_mp_setz(DIGITS(to), ALLOC(to));
|
||||
s_mp_setz(DIGITS(to), ALLOC(to));
|
||||
#endif
|
||||
s_mp_free(DIGITS(to));
|
||||
s_mp_free(DIGITS(to));
|
||||
}
|
||||
|
||||
DIGITS(to) = tmp;
|
||||
@@ -507,7 +509,7 @@ mp_err mp_set_int(mp_int *mp, long z)
|
||||
return res;
|
||||
|
||||
res = s_mp_add_d(mp,
|
||||
(mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
|
||||
(mp_digit)((v >> (ix * CHAR_BIT)) & UCHAR_MAX));
|
||||
if(res != MP_OKAY)
|
||||
return res;
|
||||
|
||||
@@ -772,7 +774,7 @@ mp_err mp_expt_d(mp_int *a, mp_digit d, mp_int *c)
|
||||
while(d != 0) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d >>= 1;
|
||||
@@ -875,13 +877,13 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
*/
|
||||
if(c == b) {
|
||||
if((res = s_mp_add(c, a)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
} else {
|
||||
if(c != a && (res = mp_copy(a, c)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
|
||||
if((res = s_mp_add(c, b)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
} else if((cmp = s_mp_cmp(a, b)) > 0) { /* different sign: a > b */
|
||||
@@ -894,10 +896,10 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
mp_int tmp;
|
||||
|
||||
if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
if((res = s_mp_sub(&tmp, b)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
s_mp_exch(&tmp, c);
|
||||
@@ -906,9 +908,9 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
} else {
|
||||
|
||||
if(c != a && (res = mp_copy(a, c)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
if((res = s_mp_sub(c, b)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
@@ -924,10 +926,10 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
mp_int tmp;
|
||||
|
||||
if((res = mp_init_copy(&tmp, b)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
if((res = s_mp_sub(&tmp, a)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
s_mp_exch(&tmp, c);
|
||||
@@ -936,9 +938,9 @@ mp_err mp_add(mp_int *a, mp_int *b, mp_int *c)
|
||||
} else {
|
||||
|
||||
if(c != b && (res = mp_copy(b, c)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
if((res = s_mp_sub(c, a)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
|
||||
}
|
||||
}
|
||||
@@ -970,12 +972,12 @@ mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c)
|
||||
if(SIGN(a) != SIGN(b)) {
|
||||
if(c == a) {
|
||||
if((res = s_mp_add(c, b)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
} else {
|
||||
if(c != b && ((res = mp_copy(b, c)) != MP_OKAY))
|
||||
return res;
|
||||
return res;
|
||||
if((res = s_mp_add(c, a)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
SIGN(c) = SIGN(a);
|
||||
}
|
||||
|
||||
@@ -984,20 +986,20 @@ mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c)
|
||||
mp_int tmp;
|
||||
|
||||
if((res = mp_init_copy(&tmp, a)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
if((res = s_mp_sub(&tmp, b)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
s_mp_exch(&tmp, c);
|
||||
mp_clear(&tmp);
|
||||
|
||||
} else {
|
||||
if(c != a && ((res = mp_copy(a, c)) != MP_OKAY))
|
||||
return res;
|
||||
return res;
|
||||
|
||||
if((res = s_mp_sub(c, b)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
} else if(cmp == 0) { /* Same sign, equal magnitude */
|
||||
@@ -1009,21 +1011,21 @@ mp_err mp_sub(mp_int *a, mp_int *b, mp_int *c)
|
||||
mp_int tmp;
|
||||
|
||||
if((res = mp_init_copy(&tmp, b)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
|
||||
if((res = s_mp_sub(&tmp, a)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
s_mp_exch(&tmp, c);
|
||||
mp_clear(&tmp);
|
||||
|
||||
} else {
|
||||
if(c != b && ((res = mp_copy(b, c)) != MP_OKAY))
|
||||
return res;
|
||||
return res;
|
||||
|
||||
if((res = s_mp_sub(c, a)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
SIGN(c) = !SIGN(b);
|
||||
@@ -1157,7 +1159,7 @@ mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r)
|
||||
if((cmp = s_mp_cmp(a, b)) < 0) {
|
||||
if(r) {
|
||||
if((res = mp_copy(a, r)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
if(q)
|
||||
@@ -1173,7 +1175,7 @@ mp_err mp_div(mp_int *a, mp_int *b, mp_int *q, mp_int *r)
|
||||
|
||||
mp_set(q, 1);
|
||||
if(qneg)
|
||||
SIGN(q) = MP_NEG;
|
||||
SIGN(q) = MP_NEG;
|
||||
}
|
||||
|
||||
if(r)
|
||||
@@ -1264,7 +1266,7 @@ mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
|
||||
mp_int s, x;
|
||||
mp_err res;
|
||||
mp_digit d;
|
||||
int dig, bit;
|
||||
unsigned int bit, dig;
|
||||
|
||||
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
|
||||
|
||||
@@ -1286,14 +1288,14 @@ mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
|
||||
/* Loop over bits of each non-maximal digit */
|
||||
for(bit = 0; bit < DIGIT_BIT; bit++) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d >>= 1;
|
||||
|
||||
if((res = s_mp_sqr(&x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1303,7 +1305,7 @@ mp_err mp_expt(mp_int *a, mp_int *b, mp_int *c)
|
||||
while(d) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d >>= 1;
|
||||
@@ -1379,7 +1381,7 @@ mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c)
|
||||
|
||||
if(SIGN(c) == MP_NEG) {
|
||||
if((res = mp_add(c, m, c)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
}
|
||||
|
||||
} else if(mag < 0) {
|
||||
@@ -1388,7 +1390,7 @@ mp_err mp_mod(mp_int *a, mp_int *m, mp_int *c)
|
||||
|
||||
if(mp_cmp_z(a) < 0) {
|
||||
if((res = mp_add(c, m, c)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
|
||||
}
|
||||
|
||||
@@ -1637,7 +1639,7 @@ mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
|
||||
mp_err res;
|
||||
mp_digit d, *db = DIGITS(b);
|
||||
mp_size ub = USED(b);
|
||||
int dig, bit;
|
||||
unsigned int bit, dig;
|
||||
|
||||
ARGCHK(a != NULL && b != NULL && c != NULL, MP_BADARG);
|
||||
|
||||
@@ -1667,18 +1669,18 @@ mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
|
||||
/* Loop over the bits of the lower-order digits */
|
||||
for(bit = 0; bit < DIGIT_BIT; bit++) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d >>= 1;
|
||||
|
||||
if((res = s_mp_sqr(&x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
if((res = s_mp_reduce(&x, m, &mu)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1688,9 +1690,9 @@ mp_err mp_exptmod(mp_int *a, mp_int *b, mp_int *m, mp_int *c)
|
||||
while(d) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
if((res = s_mp_reduce(&s, m, &mu)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d >>= 1;
|
||||
@@ -1735,8 +1737,8 @@ mp_err mp_exptmod_d(mp_int *a, mp_digit d, mp_int *m, mp_int *c)
|
||||
while(d != 0) {
|
||||
if(d & 1) {
|
||||
if((res = s_mp_mul(&s, &x)) != MP_OKAY ||
|
||||
(res = mp_mod(&s, m, &s)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
(res = mp_mod(&s, m, &s)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
}
|
||||
|
||||
d /= 2;
|
||||
@@ -1973,17 +1975,17 @@ mp_err mp_gcd(mp_int *a, mp_int *b, mp_int *c)
|
||||
|
||||
if(mp_cmp_z(&t) == MP_GT) {
|
||||
if((res = mp_copy(&t, &u)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
|
||||
} else {
|
||||
if((res = mp_copy(&t, &v)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
|
||||
/* v = -t */
|
||||
if(SIGN(&t) == MP_ZPOS)
|
||||
SIGN(&v) = MP_NEG;
|
||||
SIGN(&v) = MP_NEG;
|
||||
else
|
||||
SIGN(&v) = MP_ZPOS;
|
||||
SIGN(&v) = MP_ZPOS;
|
||||
}
|
||||
|
||||
if((res = mp_sub(&u, &v, &t)) != MP_OKAY)
|
||||
@@ -2111,12 +2113,12 @@ mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y)
|
||||
s_mp_div_2(&u);
|
||||
|
||||
if(mp_iseven(&A) && mp_iseven(&B)) {
|
||||
s_mp_div_2(&A); s_mp_div_2(&B);
|
||||
s_mp_div_2(&A); s_mp_div_2(&B);
|
||||
} else {
|
||||
if((res = mp_add(&A, &yc, &A)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&A);
|
||||
if((res = mp_sub(&B, &xc, &B)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&B);
|
||||
if((res = mp_add(&A, &yc, &A)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&A);
|
||||
if((res = mp_sub(&B, &xc, &B)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&B);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2124,12 +2126,12 @@ mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y)
|
||||
s_mp_div_2(&v);
|
||||
|
||||
if(mp_iseven(&C) && mp_iseven(&D)) {
|
||||
s_mp_div_2(&C); s_mp_div_2(&D);
|
||||
s_mp_div_2(&C); s_mp_div_2(&D);
|
||||
} else {
|
||||
if((res = mp_add(&C, &yc, &C)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&C);
|
||||
if((res = mp_sub(&D, &xc, &D)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&D);
|
||||
if((res = mp_add(&C, &yc, &C)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&C);
|
||||
if((res = mp_sub(&D, &xc, &D)) != MP_OKAY) goto CLEANUP;
|
||||
s_mp_div_2(&D);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -2148,13 +2150,13 @@ mp_err mp_xgcd(mp_int *a, mp_int *b, mp_int *g, mp_int *x, mp_int *y)
|
||||
/* If we're done, copy results to output */
|
||||
if(mp_cmp_z(&u) == 0) {
|
||||
if(x)
|
||||
if((res = mp_copy(&C, x)) != MP_OKAY) goto CLEANUP;
|
||||
if((res = mp_copy(&C, x)) != MP_OKAY) goto CLEANUP;
|
||||
|
||||
if(y)
|
||||
if((res = mp_copy(&D, y)) != MP_OKAY) goto CLEANUP;
|
||||
if((res = mp_copy(&D, y)) != MP_OKAY) goto CLEANUP;
|
||||
|
||||
if(g)
|
||||
if((res = mp_mul(&gx, &v, g)) != MP_OKAY) goto CLEANUP;
|
||||
if((res = mp_mul(&gx, &v, g)) != MP_OKAY) goto CLEANUP;
|
||||
|
||||
break;
|
||||
}
|
||||
@@ -2387,7 +2389,7 @@ mp_err mp_to_unsigned_bin(mp_int *mp, unsigned char *str)
|
||||
|
||||
/* Generate digits in reverse order */
|
||||
while(dp < end) {
|
||||
int ix;
|
||||
unsigned int ix;
|
||||
|
||||
d = *dp;
|
||||
for(ix = 0; ix < sizeof(mp_digit); ++ix) {
|
||||
@@ -2463,15 +2465,15 @@ mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix)
|
||||
mp_sign sig = MP_ZPOS;
|
||||
|
||||
ARGCHK(mp != NULL && str != NULL && radix >= 2 && radix <= MAX_RADIX,
|
||||
MP_BADARG);
|
||||
MP_BADARG);
|
||||
|
||||
mp_zero(mp);
|
||||
|
||||
/* Skip leading non-digit characters until a digit or '-' or '+' */
|
||||
while(str[ix] &&
|
||||
(s_mp_tovalue(str[ix], radix) < 0) &&
|
||||
str[ix] != '-' &&
|
||||
str[ix] != '+') {
|
||||
(s_mp_tovalue(str[ix], radix) < 0) &&
|
||||
str[ix] != '-' &&
|
||||
str[ix] != '+') {
|
||||
++ix;
|
||||
}
|
||||
|
||||
@@ -2541,7 +2543,7 @@ int mp_value_radix_size(int num, int qty, int radix)
|
||||
|
||||
/* {{{ mp_toradix(mp, str, radix) */
|
||||
|
||||
mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix)
|
||||
mp_err mp_toradix(mp_int *mp, char *str, int radix)
|
||||
{
|
||||
int ix, pos = 0;
|
||||
|
||||
@@ -2567,8 +2569,8 @@ mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix)
|
||||
/* Generate output digits in reverse order */
|
||||
while(mp_cmp_z(&tmp) != 0) {
|
||||
if((res = s_mp_div_d(&tmp, rdx, &rem)) != MP_OKAY) {
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
mp_clear(&tmp);
|
||||
return res;
|
||||
}
|
||||
|
||||
/* Generate digits, use capital letters */
|
||||
@@ -2587,10 +2589,10 @@ mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix)
|
||||
/* Reverse the digits and sign indicator */
|
||||
ix = 0;
|
||||
while(ix < pos) {
|
||||
char tmp = str[ix];
|
||||
char _tmp = str[ix];
|
||||
|
||||
str[ix] = str[pos];
|
||||
str[pos] = tmp;
|
||||
str[pos] = _tmp;
|
||||
++ix;
|
||||
--pos;
|
||||
}
|
||||
@@ -2817,7 +2819,7 @@ mp_err s_mp_lshd(mp_int *mp, mp_size p)
|
||||
mp_err res;
|
||||
mp_size pos;
|
||||
mp_digit *dp;
|
||||
int ix;
|
||||
int ix;
|
||||
|
||||
if(p == 0)
|
||||
return MP_OKAY;
|
||||
@@ -2833,7 +2835,7 @@ mp_err s_mp_lshd(mp_int *mp, mp_size p)
|
||||
dp[ix + p] = dp[ix];
|
||||
|
||||
/* Fill the bottom digits with zeroes */
|
||||
for(ix = 0; ix < p; ix++)
|
||||
for(ix = 0; (unsigned)ix < p; ix++)
|
||||
dp[ix] = 0;
|
||||
|
||||
return MP_OKAY;
|
||||
@@ -2898,7 +2900,7 @@ void s_mp_div_2(mp_int *mp)
|
||||
|
||||
mp_err s_mp_mul_2(mp_int *mp)
|
||||
{
|
||||
int ix;
|
||||
unsigned int ix;
|
||||
mp_digit kin = 0, kout, *dp = DIGITS(mp);
|
||||
mp_err res;
|
||||
|
||||
@@ -2914,7 +2916,7 @@ mp_err s_mp_mul_2(mp_int *mp)
|
||||
if(kin) {
|
||||
if(ix >= ALLOC(mp)) {
|
||||
if((res = s_mp_grow(mp, ALLOC(mp) + 1)) != MP_OKAY)
|
||||
return res;
|
||||
return res;
|
||||
dp = DIGITS(mp);
|
||||
}
|
||||
|
||||
@@ -2970,7 +2972,7 @@ mp_err s_mp_mul_2d(mp_int *mp, mp_digit d)
|
||||
mp_err res;
|
||||
mp_digit save, next, mask, *dp;
|
||||
mp_size used;
|
||||
int ix;
|
||||
unsigned int ix;
|
||||
|
||||
if((res = s_mp_lshd(mp, d / DIGIT_BIT)) != MP_OKAY)
|
||||
return res;
|
||||
@@ -3555,12 +3557,12 @@ mp_err s_mp_sqr(mp_int *a)
|
||||
w = *pa1 * *pa2;
|
||||
|
||||
/* If w is more than half MP_WORD_MAX, the doubling will
|
||||
overflow, and we need to record a carry out into the next
|
||||
word */
|
||||
overflow, and we need to record a carry out into the next
|
||||
word */
|
||||
u = (w >> (MP_WORD_BIT - 1)) & 1;
|
||||
|
||||
/* Double what we've got, overflow will be ignored as defined
|
||||
for C arithmetic (we've already noted if it is to occur)
|
||||
for C arithmetic (we've already noted if it is to occur)
|
||||
*/
|
||||
w *= 2;
|
||||
|
||||
@@ -3568,7 +3570,7 @@ mp_err s_mp_sqr(mp_int *a)
|
||||
v = *pt + k;
|
||||
|
||||
/* If we do not already have an overflow carry, check to see
|
||||
if the addition will cause one, and set the carry out if so
|
||||
if the addition will cause one, and set the carry out if so
|
||||
*/
|
||||
u |= ((MP_WORD_MAX - v) < w);
|
||||
|
||||
@@ -3579,7 +3581,7 @@ mp_err s_mp_sqr(mp_int *a)
|
||||
*pt = ACCUM(w);
|
||||
|
||||
/* Save carry information for the next iteration of the loop.
|
||||
This is why k must be an mp_word, instead of an mp_digit */
|
||||
This is why k must be an mp_word, instead of an mp_digit */
|
||||
k = CARRYOUT(w) | (u << DIGIT_BIT);
|
||||
|
||||
} /* for(jx ...) */
|
||||
@@ -3665,10 +3667,10 @@ mp_err s_mp_div(mp_int *a, mp_int *b)
|
||||
/* Find a partial substring of a which is at least b */
|
||||
while(s_mp_cmp(&rem, b) < 0 && ix >= 0) {
|
||||
if((res = s_mp_lshd(&rem, 1)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
|
||||
if((res = s_mp_lshd(", 1)) != MP_OKAY)
|
||||
goto CLEANUP;
|
||||
goto CLEANUP;
|
||||
|
||||
DIGIT(&rem, 0) = DIGIT(a, ix);
|
||||
s_mp_clamp(&rem);
|
||||
@@ -3790,9 +3792,9 @@ int s_mp_cmp(mp_int *a, mp_int *b)
|
||||
|
||||
while(ix >= 0) {
|
||||
if(*ap > *bp)
|
||||
return MP_GT;
|
||||
return MP_GT;
|
||||
else if(*ap < *bp)
|
||||
return MP_LT;
|
||||
return MP_LT;
|
||||
|
||||
--ap; --bp; --ix;
|
||||
}
|
||||
@@ -3851,7 +3853,7 @@ int s_mp_ispow2(mp_int *v)
|
||||
|
||||
while(ix >= 0) {
|
||||
if(*dp)
|
||||
return -1; /* not a power of two */
|
||||
return -1; /* not a power of two */
|
||||
|
||||
--dp; --ix;
|
||||
}
|
||||
@@ -3980,6 +3982,6 @@ int s_mp_outlen(int bits, int r)
|
||||
/* HERE THERE BE DRAGONS */
|
||||
/* crc==4242132123, version==2, Sat Feb 02 06:43:52 2002 */
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/mtest/mpi.c,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
/* $Date: 2005/05/05 14:38:47 $ */
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
@@ -2,11 +2,13 @@
|
||||
mpi.h
|
||||
|
||||
by Michael J. Fromberger <sting@linguist.dartmouth.edu>
|
||||
Copyright (C) 1998 Michael J. Fromberger, All Rights Reserved
|
||||
Copyright (C) 1998 Michael J. Fromberger
|
||||
|
||||
Arbitrary precision integer arithmetic library
|
||||
|
||||
$Id: mpi.h,v 1.2 2005/05/05 14:38:47 tom Exp $
|
||||
SPDX-License-Identifier: Unlicense
|
||||
|
||||
$Id$
|
||||
*/
|
||||
|
||||
#ifndef _H_MPI_
|
||||
@@ -210,7 +212,7 @@ int mp_count_bits(mp_int *mp);
|
||||
mp_err mp_read_radix(mp_int *mp, unsigned char *str, int radix);
|
||||
int mp_radix_size(mp_int *mp, int radix);
|
||||
int mp_value_radix_size(int num, int qty, int radix);
|
||||
mp_err mp_toradix(mp_int *mp, unsigned char *str, int radix);
|
||||
mp_err mp_toradix(mp_int *mp, char *str, int radix);
|
||||
|
||||
int mp_char2value(char ch, int r);
|
||||
|
||||
@@ -226,6 +228,6 @@ const char *mp_strerror(mp_err ec);
|
||||
|
||||
#endif /* end _H_MPI_ */
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/mtest/mpi.h,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
/* $Date: 2005/05/05 14:38:47 $ */
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
@@ -39,39 +39,71 @@ mulmod
|
||||
#include <time.h>
|
||||
#include "mpi.c"
|
||||
|
||||
#ifdef LTM_MTEST_REAL_RAND
|
||||
#define getRandChar() fgetc(rng)
|
||||
FILE *rng;
|
||||
#else
|
||||
#define getRandChar() (rand()&0xFF)
|
||||
#endif
|
||||
|
||||
void rand_num(mp_int *a)
|
||||
{
|
||||
int n, size;
|
||||
int size;
|
||||
unsigned char buf[2048];
|
||||
size_t sz;
|
||||
|
||||
size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
|
||||
buf[0] = (fgetc(rng)&1)?1:0;
|
||||
fread(buf+1, 1, size, rng);
|
||||
while (buf[1] == 0) buf[1] = fgetc(rng);
|
||||
size = 1 + ((getRandChar()<<8) + getRandChar()) % 101;
|
||||
buf[0] = (getRandChar()&1)?1:0;
|
||||
#ifdef LTM_MTEST_REAL_RAND
|
||||
sz = fread(buf+1, 1, size, rng);
|
||||
#else
|
||||
sz = 1;
|
||||
while (sz < (unsigned)size) {
|
||||
buf[sz] = getRandChar();
|
||||
++sz;
|
||||
}
|
||||
#endif
|
||||
if (sz != (unsigned)size) {
|
||||
fprintf(stderr, "\nWarning: fread failed\n\n");
|
||||
}
|
||||
while (buf[1] == 0) buf[1] = getRandChar();
|
||||
mp_read_raw(a, buf, 1+size);
|
||||
}
|
||||
|
||||
void rand_num2(mp_int *a)
|
||||
{
|
||||
int n, size;
|
||||
int size;
|
||||
unsigned char buf[2048];
|
||||
size_t sz;
|
||||
|
||||
size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101;
|
||||
buf[0] = (fgetc(rng)&1)?1:0;
|
||||
fread(buf+1, 1, size, rng);
|
||||
while (buf[1] == 0) buf[1] = fgetc(rng);
|
||||
size = 10 + ((getRandChar()<<8) + getRandChar()) % 101;
|
||||
buf[0] = (getRandChar()&1)?1:0;
|
||||
#ifdef LTM_MTEST_REAL_RAND
|
||||
sz = fread(buf+1, 1, size, rng);
|
||||
#else
|
||||
sz = 1;
|
||||
while (sz < (unsigned)size) {
|
||||
buf[sz] = getRandChar();
|
||||
++sz;
|
||||
}
|
||||
#endif
|
||||
if (sz != (unsigned)size) {
|
||||
fprintf(stderr, "\nWarning: fread failed\n\n");
|
||||
}
|
||||
while (buf[1] == 0) buf[1] = getRandChar();
|
||||
mp_read_raw(a, buf, 1+size);
|
||||
}
|
||||
|
||||
#define mp_to64(a, b) mp_toradix(a, b, 64)
|
||||
|
||||
int main(void)
|
||||
int main(int argc, char *argv[])
|
||||
{
|
||||
int n, tmp;
|
||||
long long max;
|
||||
mp_int a, b, c, d, e;
|
||||
#ifdef MTEST_NO_FULLSPEED
|
||||
clock_t t1;
|
||||
#endif
|
||||
char buf[4096];
|
||||
|
||||
mp_init(&a);
|
||||
@@ -80,229 +112,263 @@ int main(void)
|
||||
mp_init(&d);
|
||||
mp_init(&e);
|
||||
|
||||
if (argc > 1) {
|
||||
max = strtol(argv[1], NULL, 0);
|
||||
if (max < 0) {
|
||||
if (max > -64) {
|
||||
max = (1 << -(max)) + 1;
|
||||
} else {
|
||||
max = 1;
|
||||
}
|
||||
} else if (max == 0) {
|
||||
max = 1;
|
||||
}
|
||||
} else {
|
||||
max = 0;
|
||||
}
|
||||
|
||||
|
||||
/* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */
|
||||
/*
|
||||
mp_set(&a, 1);
|
||||
for (n = 1; n < 8192; n++) {
|
||||
mp_mul(&a, &a, &c);
|
||||
printf("mul\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n%s\n", buf, buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
/*
|
||||
mp_set(&a, 1);
|
||||
for (n = 1; n < 8192; n++) {
|
||||
mp_mul(&a, &a, &c);
|
||||
printf("mul\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n%s\n", buf, buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
|
||||
mp_add_d(&a, 1, &a);
|
||||
mp_mul_2(&a, &a);
|
||||
mp_sub_d(&a, 1, &a);
|
||||
}
|
||||
*/
|
||||
mp_add_d(&a, 1, &a);
|
||||
mp_mul_2(&a, &a);
|
||||
mp_sub_d(&a, 1, &a);
|
||||
}
|
||||
*/
|
||||
|
||||
#ifdef LTM_MTEST_REAL_RAND
|
||||
rng = fopen("/dev/urandom", "rb");
|
||||
if (rng == NULL) {
|
||||
rng = fopen("/dev/random", "rb");
|
||||
if (rng == NULL) {
|
||||
fprintf(stderr, "\nWarning: stdin used as random source\n\n");
|
||||
rng = stdin;
|
||||
fprintf(stderr, "\nWarning: no /dev/[u]random available\n\n");
|
||||
printf("exit\n");
|
||||
return 1;
|
||||
}
|
||||
}
|
||||
#else
|
||||
srand(23);
|
||||
#endif
|
||||
|
||||
#ifdef MTEST_NO_FULLSPEED
|
||||
t1 = clock();
|
||||
#endif
|
||||
for (;;) {
|
||||
#if 0
|
||||
#ifdef MTEST_NO_FULLSPEED
|
||||
if (clock() - t1 > CLOCKS_PER_SEC) {
|
||||
sleep(2);
|
||||
t1 = clock();
|
||||
}
|
||||
#endif
|
||||
n = fgetc(rng) % 15;
|
||||
n = getRandChar() % 15;
|
||||
|
||||
if (max != 0) {
|
||||
--max;
|
||||
if (max == 0)
|
||||
n = 255;
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
/* add tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_add(&a, &b, &c);
|
||||
printf("add\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 1) {
|
||||
/* sub tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_sub(&a, &b, &c);
|
||||
printf("sub\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 2) {
|
||||
/* mul tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_mul(&a, &b, &c);
|
||||
printf("mul\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 3) {
|
||||
/* div tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_div(&a, &b, &c, &d);
|
||||
printf("div\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&d, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 4) {
|
||||
/* sqr tests */
|
||||
rand_num(&a);
|
||||
mp_sqr(&a, &b);
|
||||
printf("sqr\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 5) {
|
||||
/* mul_2d test */
|
||||
rand_num(&a);
|
||||
mp_copy(&a, &b);
|
||||
n = getRandChar() & 63;
|
||||
mp_mul_2d(&b, n, &b);
|
||||
mp_to64(&a, buf);
|
||||
printf("mul2d\n");
|
||||
printf("%s\n", buf);
|
||||
printf("%d\n", n);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 6) {
|
||||
/* div_2d test */
|
||||
rand_num(&a);
|
||||
mp_copy(&a, &b);
|
||||
n = getRandChar() & 63;
|
||||
mp_div_2d(&b, n, &b, NULL);
|
||||
mp_to64(&a, buf);
|
||||
printf("div2d\n");
|
||||
printf("%s\n", buf);
|
||||
printf("%d\n", n);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 7) {
|
||||
/* gcd test */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
a.sign = MP_ZPOS;
|
||||
b.sign = MP_ZPOS;
|
||||
mp_gcd(&a, &b, &c);
|
||||
printf("gcd\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 8) {
|
||||
/* lcm test */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
a.sign = MP_ZPOS;
|
||||
b.sign = MP_ZPOS;
|
||||
mp_lcm(&a, &b, &c);
|
||||
printf("lcm\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 9) {
|
||||
/* exptmod test */
|
||||
rand_num2(&a);
|
||||
rand_num2(&b);
|
||||
rand_num2(&c);
|
||||
/* if (c.dp[0]&1) mp_add_d(&c, 1, &c); */
|
||||
a.sign = b.sign = c.sign = 0;
|
||||
mp_exptmod(&a, &b, &c, &d);
|
||||
printf("expt\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&d, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 10) {
|
||||
/* invmod test */
|
||||
do {
|
||||
rand_num2(&a);
|
||||
rand_num2(&b);
|
||||
b.sign = MP_ZPOS;
|
||||
a.sign = MP_ZPOS;
|
||||
mp_gcd(&a, &b, &c);
|
||||
} while (mp_cmp_d(&c, 1) != 0 || mp_cmp_d(&b, 1) == 0);
|
||||
mp_invmod(&a, &b, &c);
|
||||
printf("invmod\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 11) {
|
||||
rand_num(&a);
|
||||
mp_mul_2(&a, &a);
|
||||
mp_div_2(&a, &b);
|
||||
printf("div2\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 12) {
|
||||
rand_num2(&a);
|
||||
mp_mul_2(&a, &b);
|
||||
printf("mul2\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 13) {
|
||||
rand_num2(&a);
|
||||
tmp = abs(rand()) & THE_MASK;
|
||||
mp_add_d(&a, tmp, &b);
|
||||
printf("add_d\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n%d\n", buf, tmp);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 14) {
|
||||
rand_num2(&a);
|
||||
tmp = abs(rand()) & THE_MASK;
|
||||
mp_sub_d(&a, tmp, &b);
|
||||
printf("sub_d\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n%d\n", buf, tmp);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 255) {
|
||||
printf("exit\n");
|
||||
break;
|
||||
}
|
||||
|
||||
if (n == 0) {
|
||||
/* add tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_add(&a, &b, &c);
|
||||
printf("add\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 1) {
|
||||
/* sub tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_sub(&a, &b, &c);
|
||||
printf("sub\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 2) {
|
||||
/* mul tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_mul(&a, &b, &c);
|
||||
printf("mul\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 3) {
|
||||
/* div tests */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
mp_div(&a, &b, &c, &d);
|
||||
printf("div\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&d, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 4) {
|
||||
/* sqr tests */
|
||||
rand_num(&a);
|
||||
mp_sqr(&a, &b);
|
||||
printf("sqr\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 5) {
|
||||
/* mul_2d test */
|
||||
rand_num(&a);
|
||||
mp_copy(&a, &b);
|
||||
n = fgetc(rng) & 63;
|
||||
mp_mul_2d(&b, n, &b);
|
||||
mp_to64(&a, buf);
|
||||
printf("mul2d\n");
|
||||
printf("%s\n", buf);
|
||||
printf("%d\n", n);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 6) {
|
||||
/* div_2d test */
|
||||
rand_num(&a);
|
||||
mp_copy(&a, &b);
|
||||
n = fgetc(rng) & 63;
|
||||
mp_div_2d(&b, n, &b, NULL);
|
||||
mp_to64(&a, buf);
|
||||
printf("div2d\n");
|
||||
printf("%s\n", buf);
|
||||
printf("%d\n", n);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 7) {
|
||||
/* gcd test */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
a.sign = MP_ZPOS;
|
||||
b.sign = MP_ZPOS;
|
||||
mp_gcd(&a, &b, &c);
|
||||
printf("gcd\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 8) {
|
||||
/* lcm test */
|
||||
rand_num(&a);
|
||||
rand_num(&b);
|
||||
a.sign = MP_ZPOS;
|
||||
b.sign = MP_ZPOS;
|
||||
mp_lcm(&a, &b, &c);
|
||||
printf("lcm\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 9) {
|
||||
/* exptmod test */
|
||||
rand_num2(&a);
|
||||
rand_num2(&b);
|
||||
rand_num2(&c);
|
||||
// if (c.dp[0]&1) mp_add_d(&c, 1, &c);
|
||||
a.sign = b.sign = c.sign = 0;
|
||||
mp_exptmod(&a, &b, &c, &d);
|
||||
printf("expt\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&d, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 10) {
|
||||
/* invmod test */
|
||||
rand_num2(&a);
|
||||
rand_num2(&b);
|
||||
b.sign = MP_ZPOS;
|
||||
a.sign = MP_ZPOS;
|
||||
mp_gcd(&a, &b, &c);
|
||||
if (mp_cmp_d(&c, 1) != 0) continue;
|
||||
if (mp_cmp_d(&b, 1) == 0) continue;
|
||||
mp_invmod(&a, &b, &c);
|
||||
printf("invmod\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&c, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 11) {
|
||||
rand_num(&a);
|
||||
mp_mul_2(&a, &a);
|
||||
mp_div_2(&a, &b);
|
||||
printf("div2\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 12) {
|
||||
rand_num2(&a);
|
||||
mp_mul_2(&a, &b);
|
||||
printf("mul2\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n", buf);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 13) {
|
||||
rand_num2(&a);
|
||||
tmp = abs(rand()) & THE_MASK;
|
||||
mp_add_d(&a, tmp, &b);
|
||||
printf("add_d\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n%d\n", buf, tmp);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
} else if (n == 14) {
|
||||
rand_num2(&a);
|
||||
tmp = abs(rand()) & THE_MASK;
|
||||
mp_sub_d(&a, tmp, &b);
|
||||
printf("sub_d\n");
|
||||
mp_to64(&a, buf);
|
||||
printf("%s\n%d\n", buf, tmp);
|
||||
mp_to64(&b, buf);
|
||||
printf("%s\n", buf);
|
||||
}
|
||||
}
|
||||
#ifdef LTM_MTEST_REAL_RAND
|
||||
fclose(rng);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
/* $Source: /cvs/libtom/libtommath/mtest/mtest.c,v $ */
|
||||
/* $Revision: 1.2 $ */
|
||||
/* $Date: 2005/05/05 14:38:47 $ */
|
||||
/* $Source$ */
|
||||
/* $Revision$ */
|
||||
/* $Date$ */
|
||||
|
Reference in New Issue
Block a user