2003-02-28 11:02:06 -05:00
|
|
|
#include <time.h>
|
|
|
|
|
2003-08-29 10:06:56 -04:00
|
|
|
#ifdef IOWNANATHLON
|
|
|
|
#include <unistd.h>
|
|
|
|
#define SLEEP sleep(4)
|
|
|
|
#else
|
|
|
|
#define SLEEP
|
|
|
|
#endif
|
|
|
|
|
2014-02-14 05:48:34 -05:00
|
|
|
/*
|
|
|
|
* Configuration
|
|
|
|
*/
|
|
|
|
#ifndef LTM_DEMO_TEST_VS_MTEST
|
|
|
|
#define LTM_DEMO_TEST_VS_MTEST 1
|
|
|
|
#endif
|
|
|
|
|
2014-02-14 06:58:49 -05:00
|
|
|
#ifndef LTM_DEMO_TEST_REDUCE_2K_L
|
|
|
|
/* This test takes a moment so we disable it by default, but it can be:
|
|
|
|
* 0 to disable testing
|
|
|
|
* 1 to make the test with P = 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF
|
|
|
|
* 2 to make the test with P = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F
|
|
|
|
*/
|
|
|
|
#define LTM_DEMO_TEST_REDUCE_2K_L 0
|
|
|
|
#endif
|
|
|
|
|
2014-02-14 05:48:34 -05:00
|
|
|
#ifdef LTM_DEMO_REAL_RAND
|
|
|
|
#define LTM_DEMO_RAND_SEED time(NULL)
|
|
|
|
#else
|
|
|
|
#define LTM_DEMO_RAND_SEED 23
|
|
|
|
#endif
|
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
#include "tommath.h"
|
2003-02-28 11:05:26 -05:00
|
|
|
|
2014-02-14 06:57:52 -05:00
|
|
|
#if LTM_DEMO_TEST_VS_MTEST
|
2005-02-12 03:40:15 -05:00
|
|
|
void ndraw(mp_int * a, char *name)
|
2003-02-28 11:02:06 -05:00
|
|
|
{
|
2004-12-22 21:40:37 -05:00
|
|
|
char buf[16000];
|
2005-02-12 03:40:15 -05:00
|
|
|
|
2003-02-28 11:04:18 -05:00
|
|
|
printf("%s: ", name);
|
2004-12-22 21:40:37 -05:00
|
|
|
mp_toradix(a, buf, 10);
|
2003-02-28 11:04:18 -05:00
|
|
|
printf("%s\n", buf);
|
2003-02-28 11:02:06 -05:00
|
|
|
}
|
|
|
|
|
2005-02-12 03:40:15 -05:00
|
|
|
static void draw(mp_int * a)
|
2003-02-28 11:04:18 -05:00
|
|
|
{
|
|
|
|
ndraw(a, "");
|
|
|
|
}
|
2014-02-14 06:57:52 -05:00
|
|
|
#endif
|
2003-02-28 11:04:18 -05:00
|
|
|
|
|
|
|
|
2003-02-28 11:03:48 -05:00
|
|
|
unsigned long lfsr = 0xAAAAAAAAUL;
|
|
|
|
|
|
|
|
int lbit(void)
|
|
|
|
{
|
|
|
|
if (lfsr & 0x80000000UL) {
|
|
|
|
lfsr = ((lfsr << 1) ^ 0x8000001BUL) & 0xFFFFFFFFUL;
|
|
|
|
return 1;
|
|
|
|
} else {
|
|
|
|
lfsr <<= 1;
|
|
|
|
return 0;
|
|
|
|
}
|
2003-02-28 11:04:18 -05:00
|
|
|
}
|
2003-03-12 21:11:11 -05:00
|
|
|
|
2004-04-11 16:46:22 -04:00
|
|
|
int myrng(unsigned char *dst, int len, void *dat)
|
|
|
|
{
|
|
|
|
int x;
|
2014-02-14 06:57:52 -05:00
|
|
|
(void)dat;
|
2005-02-12 03:40:15 -05:00
|
|
|
for (x = 0; x < len; x++)
|
|
|
|
dst[x] = rand() & 0xFF;
|
2004-04-11 16:46:22 -04:00
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-02-14 06:59:04 -05:00
|
|
|
mp_int a, b, c, d, e, f;
|
2003-05-17 08:33:54 -04:00
|
|
|
|
2014-02-14 06:59:04 -05:00
|
|
|
static void _cleanup(void)
|
|
|
|
{
|
|
|
|
mp_clear_multi(&a, &b, &c, &d, &e, &f, NULL);
|
|
|
|
}
|
2003-02-28 11:03:48 -05:00
|
|
|
|
2005-02-12 03:40:15 -05:00
|
|
|
char cmd[4096], buf[4096];
|
2003-02-28 11:02:06 -05:00
|
|
|
int main(void)
|
|
|
|
{
|
2014-02-14 06:57:52 -05:00
|
|
|
unsigned long t;
|
2003-02-28 11:09:08 -05:00
|
|
|
unsigned rr;
|
2014-02-14 06:57:52 -05:00
|
|
|
int i, n, err, cnt, ix;
|
2005-03-12 06:55:11 -05:00
|
|
|
mp_digit mp;
|
2014-02-14 06:57:52 -05:00
|
|
|
#if LTM_DEMO_TEST_VS_MTEST
|
|
|
|
unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n,
|
|
|
|
gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n;
|
|
|
|
int old_kara_m, old_kara_s;
|
|
|
|
#endif
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2014-02-14 06:57:52 -05:00
|
|
|
mp_init_multi(&a, &b, &c, &d, &e, &f, NULL);
|
2003-02-28 11:08:34 -05:00
|
|
|
|
2014-02-14 06:57:52 -05:00
|
|
|
atexit(_cleanup);
|
2003-09-19 18:43:07 -04:00
|
|
|
|
2014-02-14 05:48:34 -05:00
|
|
|
srand(LTM_DEMO_RAND_SEED);
|
2003-07-02 11:39:39 -04:00
|
|
|
|
2014-02-14 05:48:34 -05:00
|
|
|
#if LTM_DEMO_TEST_VS_MTEST == 0
|
2014-02-14 05:30:15 -05:00
|
|
|
// test montgomery
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("Testing: montgomery...\n");
|
2005-03-12 06:55:11 -05:00
|
|
|
for (i = 1; i < 10; i++) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf(" digit size: %2d\r", i);
|
|
|
|
fflush(stdout);
|
2005-03-12 06:55:11 -05:00
|
|
|
for (n = 0; n < 1000; n++) {
|
|
|
|
mp_rand(&a, i);
|
|
|
|
a.dp[0] |= 1;
|
|
|
|
|
|
|
|
// let's see if R is right
|
|
|
|
mp_montgomery_calc_normalization(&b, &a);
|
|
|
|
mp_montgomery_setup(&a, &mp);
|
|
|
|
|
2014-02-14 05:30:15 -05:00
|
|
|
// now test a random reduction
|
2005-03-12 06:55:11 -05:00
|
|
|
for (ix = 0; ix < 100; ix++) {
|
|
|
|
mp_rand(&c, 1 + abs(rand()) % (2*i));
|
|
|
|
mp_copy(&c, &d);
|
|
|
|
mp_copy(&c, &e);
|
|
|
|
|
|
|
|
mp_mod(&d, &a, &d);
|
|
|
|
mp_montgomery_reduce(&c, &a, mp);
|
|
|
|
mp_mulmod(&c, &b, &a, &c);
|
|
|
|
|
2014-02-14 05:30:15 -05:00
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
2005-03-12 06:55:11 -05:00
|
|
|
printf("d = e mod a, c = e MOD a\n");
|
|
|
|
mp_todecimal(&a, buf); printf("a = %s\n", buf);
|
|
|
|
mp_todecimal(&e, buf); printf("e = %s\n", buf);
|
|
|
|
mp_todecimal(&d, buf); printf("d = %s\n", buf);
|
|
|
|
mp_todecimal(&c, buf); printf("c = %s\n", buf);
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("compare no compare!\n"); return EXIT_FAILURE; }
|
2005-03-12 06:55:11 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2005-02-12 03:40:15 -05:00
|
|
|
// test mp_get_int
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_get_int");
|
2005-02-12 03:40:15 -05:00
|
|
|
for (i = 0; i < 1000; ++i) {
|
|
|
|
t = ((unsigned long) rand() * rand() + 1) & 0xFFFFFFFF;
|
|
|
|
mp_set_int(&a, t);
|
|
|
|
if (t != mp_get_int(&a)) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nmp_get_int() bad result!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
mp_set_int(&a, 0);
|
|
|
|
if (mp_get_int(&a) != 0) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nmp_get_int() bad result!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
mp_set_int(&a, 0xffffffff);
|
|
|
|
if (mp_get_int(&a) != 0xffffffff) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nmp_get_int() bad result!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
// test mp_sqrt
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_sqrt\n");
|
2005-02-12 03:40:15 -05:00
|
|
|
for (i = 0; i < 1000; ++i) {
|
|
|
|
printf("%6d\r", i);
|
|
|
|
fflush(stdout);
|
|
|
|
n = (rand() & 15) + 1;
|
|
|
|
mp_rand(&a, n);
|
|
|
|
if (mp_sqrt(&a, &b) != MP_OKAY) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nmp_sqrt() error!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
mp_n_root(&a, 2, &a);
|
|
|
|
if (mp_cmp_mag(&b, &a) != MP_EQ) {
|
|
|
|
printf("mp_sqrt() bad result!\n");
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
2004-04-11 16:46:22 -04:00
|
|
|
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_is_square\n");
|
2005-02-12 03:40:15 -05:00
|
|
|
for (i = 0; i < 1000; ++i) {
|
|
|
|
printf("%6d\r", i);
|
|
|
|
fflush(stdout);
|
|
|
|
|
|
|
|
/* test mp_is_square false negatives */
|
|
|
|
n = (rand() & 7) + 1;
|
|
|
|
mp_rand(&a, n);
|
|
|
|
mp_sqr(&a, &a);
|
|
|
|
if (mp_is_square(&a, &n) != MP_OKAY) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nfn:mp_is_square() error!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
if (n == 0) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nfn:mp_is_square() bad result!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test for false positives */
|
|
|
|
mp_add_d(&a, 1, &a);
|
|
|
|
if (mp_is_square(&a, &n) != MP_OKAY) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nfp:mp_is_square() error!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
if (n == 1) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nfp:mp_is_square() bad result!");
|
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
2004-04-11 16:46:22 -04:00
|
|
|
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
printf("\n\n");
|
2004-04-11 16:46:22 -04:00
|
|
|
|
|
|
|
/* test for size */
|
2005-03-12 06:55:11 -05:00
|
|
|
for (ix = 10; ix < 128; ix++) {
|
2005-02-12 03:40:15 -05:00
|
|
|
printf("Testing (not safe-prime): %9d bits \r", ix);
|
|
|
|
fflush(stdout);
|
|
|
|
err =
|
|
|
|
mp_prime_random_ex(&a, 8, ix,
|
2011-03-23 18:25:15 -04:00
|
|
|
(rand() & 1) ? 0 : LTM_PRIME_2MSB_ON,
|
|
|
|
myrng, NULL);
|
2005-02-12 03:40:15 -05:00
|
|
|
if (err != MP_OKAY) {
|
|
|
|
printf("failed with err code %d\n", err);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (mp_count_bits(&a) != ix) {
|
|
|
|
printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2004-04-11 16:46:22 -04:00
|
|
|
}
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n");
|
2004-04-11 16:46:22 -04:00
|
|
|
|
2005-03-12 06:55:11 -05:00
|
|
|
for (ix = 16; ix < 128; ix++) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("Testing ( safe-prime): %9d bits \r", ix);
|
2005-02-12 03:40:15 -05:00
|
|
|
fflush(stdout);
|
|
|
|
err =
|
|
|
|
mp_prime_random_ex(&a, 8, ix,
|
2011-03-23 18:25:15 -04:00
|
|
|
((rand() & 1) ? 0 : LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE,
|
|
|
|
myrng, NULL);
|
2005-02-12 03:40:15 -05:00
|
|
|
if (err != MP_OKAY) {
|
|
|
|
printf("failed with err code %d\n", err);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (mp_count_bits(&a) != ix) {
|
|
|
|
printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
/* let's see if it's really a safe prime */
|
|
|
|
mp_sub_d(&a, 1, &a);
|
|
|
|
mp_div_2(&a, &a);
|
|
|
|
mp_prime_is_prime(&a, 8, &cnt);
|
|
|
|
if (cnt != MP_YES) {
|
|
|
|
printf("sub is not prime!\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
2004-04-11 16:46:22 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
printf("\n\n");
|
|
|
|
|
|
|
|
mp_read_radix(&a, "123456", 10);
|
|
|
|
mp_toradix_n(&a, buf, 10, 3);
|
|
|
|
printf("a == %s\n", buf);
|
|
|
|
mp_toradix_n(&a, buf, 10, 4);
|
|
|
|
printf("a == %s\n", buf);
|
|
|
|
mp_toradix_n(&a, buf, 10, 30);
|
|
|
|
printf("a == %s\n", buf);
|
|
|
|
|
|
|
|
|
2003-07-12 10:31:43 -04:00
|
|
|
#if 0
|
|
|
|
for (;;) {
|
|
|
|
fgets(buf, sizeof(buf), stdin);
|
|
|
|
mp_read_radix(&a, buf, 10);
|
|
|
|
mp_prime_next_prime(&a, 5, 1);
|
|
|
|
mp_toradix(&a, buf, 10);
|
|
|
|
printf("%s, %lu\n", buf, a.dp[0] & 3);
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2003-07-02 11:39:39 -04:00
|
|
|
/* test mp_cnt_lsb */
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_cnt_lsb");
|
2003-07-02 11:39:39 -04:00
|
|
|
mp_set(&a, 1);
|
2004-04-11 16:46:22 -04:00
|
|
|
for (ix = 0; ix < 1024; ix++) {
|
2005-02-12 03:40:15 -05:00
|
|
|
if (mp_cnt_lsb(&a) != ix) {
|
|
|
|
printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a));
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
mp_mul_2(&a, &a);
|
2003-07-02 11:39:39 -04:00
|
|
|
}
|
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* test mp_reduce_2k */
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_reduce_2k\n");
|
2004-08-09 18:15:59 -04:00
|
|
|
for (cnt = 3; cnt <= 128; ++cnt) {
|
2005-02-12 03:40:15 -05:00
|
|
|
mp_digit tmp;
|
|
|
|
|
|
|
|
mp_2expt(&a, cnt);
|
|
|
|
mp_sub_d(&a, 2, &a); /* a = 2**cnt - 2 */
|
|
|
|
|
|
|
|
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\r %4d bits", cnt);
|
2005-02-12 03:40:15 -05:00
|
|
|
printf("(%d)", mp_reduce_is_2k(&a));
|
|
|
|
mp_reduce_2k_setup(&a, &tmp);
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("(%lu)", (unsigned long)tmp);
|
2005-02-12 03:40:15 -05:00
|
|
|
for (ix = 0; ix < 1000; ix++) {
|
|
|
|
if (!(ix & 127)) {
|
|
|
|
printf(".");
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
mp_rand(&b, (cnt / DIGIT_BIT + 1) * 2);
|
|
|
|
mp_copy(&c, &b);
|
|
|
|
mp_mod(&c, &a, &c);
|
2005-03-12 06:55:11 -05:00
|
|
|
mp_reduce_2k(&b, &a, 2);
|
2005-02-12 03:40:15 -05:00
|
|
|
if (mp_cmp(&c, &b)) {
|
|
|
|
printf("FAILED\n");
|
|
|
|
exit(0);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2003-09-19 18:43:07 -04:00
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* test mp_div_3 */
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_div_3...\n");
|
2004-01-25 12:40:21 -05:00
|
|
|
mp_set(&d, 3);
|
2005-02-12 03:40:15 -05:00
|
|
|
for (cnt = 0; cnt < 10000;) {
|
2014-02-14 06:57:52 -05:00
|
|
|
mp_digit r2;
|
2003-09-19 18:43:07 -04:00
|
|
|
|
2005-02-12 03:40:15 -05:00
|
|
|
if (!(++cnt & 127))
|
2014-02-14 06:57:52 -05:00
|
|
|
{
|
|
|
|
printf("%9d\r", cnt);
|
|
|
|
fflush(stdout);
|
|
|
|
}
|
2003-12-24 13:59:22 -05:00
|
|
|
mp_rand(&a, abs(rand()) % 128 + 1);
|
2004-01-25 12:40:21 -05:00
|
|
|
mp_div(&a, &d, &b, &e);
|
2003-05-29 09:35:26 -04:00
|
|
|
mp_div_3(&a, &c, &r2);
|
2003-09-19 18:43:07 -04:00
|
|
|
|
2004-01-25 12:40:21 -05:00
|
|
|
if (mp_cmp(&b, &c) || mp_cmp_d(&e, r2)) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nmp_div_3 => Failure\n");
|
2003-05-29 09:35:26 -04:00
|
|
|
}
|
|
|
|
}
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\nPassed div_3 testing");
|
2003-05-17 08:33:54 -04:00
|
|
|
|
2003-03-22 10:10:20 -05:00
|
|
|
/* test the DR reduction */
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\nTesting: mp_dr_reduce...\n");
|
2004-08-09 18:15:59 -04:00
|
|
|
for (cnt = 2; cnt < 32; cnt++) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\r%d digit modulus", cnt);
|
2005-02-12 03:40:15 -05:00
|
|
|
mp_grow(&a, cnt);
|
|
|
|
mp_zero(&a);
|
|
|
|
for (ix = 1; ix < cnt; ix++) {
|
|
|
|
a.dp[ix] = MP_MASK;
|
|
|
|
}
|
|
|
|
a.used = cnt;
|
|
|
|
a.dp[0] = 3;
|
|
|
|
|
|
|
|
mp_rand(&b, cnt - 1);
|
|
|
|
mp_copy(&b, &c);
|
2003-05-17 08:33:54 -04:00
|
|
|
|
2003-03-22 10:10:20 -05:00
|
|
|
rr = 0;
|
|
|
|
do {
|
2005-02-12 03:40:15 -05:00
|
|
|
if (!(rr & 127)) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf(".");
|
2005-02-12 03:40:15 -05:00
|
|
|
fflush(stdout);
|
|
|
|
}
|
|
|
|
mp_sqr(&b, &b);
|
|
|
|
mp_add_d(&b, 1, &b);
|
|
|
|
mp_copy(&b, &c);
|
|
|
|
|
|
|
|
mp_mod(&b, &a, &b);
|
|
|
|
mp_dr_reduce(&c, &a, (((mp_digit) 1) << DIGIT_BIT) - a.dp[0]);
|
|
|
|
|
|
|
|
if (mp_cmp(&b, &c) != MP_EQ) {
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("Failed on trial %u\n", rr);
|
2005-02-12 03:40:15 -05:00
|
|
|
exit(-1);
|
|
|
|
|
|
|
|
}
|
2004-08-09 18:15:59 -04:00
|
|
|
} while (++rr < 500);
|
2014-02-14 06:57:52 -05:00
|
|
|
printf(" passed");
|
|
|
|
fflush(stdout);
|
2003-03-22 10:10:20 -05:00
|
|
|
}
|
2003-05-17 08:33:54 -04:00
|
|
|
|
2014-02-14 06:58:49 -05:00
|
|
|
#if LTM_DEMO_TEST_REDUCE_2K_L
|
2005-02-12 03:40:15 -05:00
|
|
|
/* test the mp_reduce_2k_l code */
|
2014-02-14 06:58:49 -05:00
|
|
|
#if LTM_DEMO_TEST_REDUCE_2K_L == 1
|
2005-02-12 03:40:15 -05:00
|
|
|
/* first load P with 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF */
|
|
|
|
mp_2expt(&a, 1024);
|
|
|
|
mp_read_radix(&b, "2A434B9FDEC95D8F9D550FFFFFFFFFFFFFFFF", 16);
|
|
|
|
mp_sub(&a, &b, &a);
|
2014-02-14 06:58:49 -05:00
|
|
|
#elif LTM_DEMO_TEST_REDUCE_2K_L == 2
|
2005-02-12 03:40:15 -05:00
|
|
|
/* p = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F */
|
|
|
|
mp_2expt(&a, 2048);
|
|
|
|
mp_read_radix(&b,
|
|
|
|
"1000000000000000000000000000000004945DDBF8EA2A91D5776399BB83E188F",
|
|
|
|
16);
|
|
|
|
mp_sub(&a, &b, &a);
|
2014-02-14 06:58:49 -05:00
|
|
|
#else
|
|
|
|
#error oops
|
2005-02-12 03:40:15 -05:00
|
|
|
#endif
|
|
|
|
|
|
|
|
mp_todecimal(&a, buf);
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("\n\np==%s\n", buf);
|
2005-02-12 03:40:15 -05:00
|
|
|
/* now mp_reduce_is_2k_l() should return */
|
|
|
|
if (mp_reduce_is_2k_l(&a) != 1) {
|
|
|
|
printf("mp_reduce_is_2k_l() return 0, should be 1\n");
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
mp_reduce_2k_setup_l(&a, &d);
|
|
|
|
/* now do a million square+1 to see if it varies */
|
|
|
|
mp_rand(&b, 64);
|
|
|
|
mp_mod(&b, &a, &b);
|
|
|
|
mp_copy(&b, &c);
|
2014-02-14 06:57:52 -05:00
|
|
|
printf("Testing: mp_reduce_2k_l...");
|
2005-02-12 03:40:15 -05:00
|
|
|
fflush(stdout);
|
|
|
|
for (cnt = 0; cnt < (1UL << 20); cnt++) {
|
|
|
|
mp_sqr(&b, &b);
|
|
|
|
mp_add_d(&b, 1, &b);
|
|
|
|
mp_reduce_2k_l(&b, &a, &d);
|
|
|
|
mp_sqr(&c, &c);
|
|
|
|
mp_add_d(&c, 1, &c);
|
|
|
|
mp_mod(&c, &a, &c);
|
|
|
|
if (mp_cmp(&b, &c) != MP_EQ) {
|
|
|
|
printf("mp_reduce_2k_l() failed at step %lu\n", cnt);
|
|
|
|
mp_tohex(&b, buf);
|
|
|
|
printf("b == %s\n", buf);
|
|
|
|
mp_tohex(&c, buf);
|
|
|
|
printf("c == %s\n", buf);
|
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
printf("...Passed\n");
|
2014-02-14 06:58:49 -05:00
|
|
|
#endif /* LTM_DEMO_TEST_REDUCE_2K_L */
|
2014-02-14 05:48:34 -05:00
|
|
|
|
|
|
|
#else
|
2005-02-12 03:40:15 -05:00
|
|
|
|
2003-05-17 08:33:54 -04:00
|
|
|
div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n =
|
2005-02-12 03:40:15 -05:00
|
|
|
sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = cnt = add_d_n =
|
|
|
|
sub_d_n = 0;
|
2003-09-19 18:43:07 -04:00
|
|
|
|
2003-05-29 09:35:26 -04:00
|
|
|
/* force KARA and TOOM to enable despite cutoffs */
|
2005-08-01 12:37:28 -04:00
|
|
|
KARATSUBA_SQR_CUTOFF = KARATSUBA_MUL_CUTOFF = 8;
|
|
|
|
TOOM_SQR_CUTOFF = TOOM_MUL_CUTOFF = 16;
|
2003-05-17 08:33:54 -04:00
|
|
|
|
2003-02-28 11:02:06 -05:00
|
|
|
for (;;) {
|
2005-02-12 03:40:15 -05:00
|
|
|
/* randomly clear and re-init one variable, this has the affect of triming the alloc space */
|
|
|
|
switch (abs(rand()) % 7) {
|
|
|
|
case 0:
|
|
|
|
mp_clear(&a);
|
|
|
|
mp_init(&a);
|
|
|
|
break;
|
|
|
|
case 1:
|
|
|
|
mp_clear(&b);
|
|
|
|
mp_init(&b);
|
|
|
|
break;
|
|
|
|
case 2:
|
|
|
|
mp_clear(&c);
|
|
|
|
mp_init(&c);
|
|
|
|
break;
|
|
|
|
case 3:
|
|
|
|
mp_clear(&d);
|
|
|
|
mp_init(&d);
|
|
|
|
break;
|
|
|
|
case 4:
|
|
|
|
mp_clear(&e);
|
|
|
|
mp_init(&e);
|
|
|
|
break;
|
|
|
|
case 5:
|
|
|
|
mp_clear(&f);
|
|
|
|
mp_init(&f);
|
|
|
|
break;
|
|
|
|
case 6:
|
|
|
|
break; /* don't clear any */
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
printf
|
|
|
|
("%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu/%4lu ",
|
|
|
|
add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n, gcd_n, lcm_n,
|
|
|
|
expt_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n);
|
|
|
|
fgets(cmd, 4095, stdin);
|
|
|
|
cmd[strlen(cmd) - 1] = 0;
|
|
|
|
printf("%s ]\r", cmd);
|
|
|
|
fflush(stdout);
|
|
|
|
if (!strcmp(cmd, "mul2d")) {
|
|
|
|
++mul2d_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
sscanf(buf, "%d", &rr);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
|
|
|
|
mp_mul_2d(&a, rr, &a);
|
|
|
|
a.sign = b.sign;
|
|
|
|
if (mp_cmp(&a, &b) != MP_EQ) {
|
|
|
|
printf("mul2d failed, rr == %d\n", rr);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "div2d")) {
|
|
|
|
++div2d_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
sscanf(buf, "%d", &rr);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
|
|
|
|
mp_div_2d(&a, rr, &a, &e);
|
|
|
|
a.sign = b.sign;
|
|
|
|
if (a.used == b.used && a.used == 0) {
|
|
|
|
a.sign = b.sign = MP_ZPOS;
|
|
|
|
}
|
|
|
|
if (mp_cmp(&a, &b) != MP_EQ) {
|
|
|
|
printf("div2d failed, rr == %d\n", rr);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "add")) {
|
|
|
|
++add_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
mp_copy(&a, &d);
|
|
|
|
mp_add(&d, &b, &d);
|
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
|
|
|
printf("add %lu failure!\n", add_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/* test the sign/unsigned storage functions */
|
|
|
|
|
|
|
|
rr = mp_signed_bin_size(&c);
|
|
|
|
mp_to_signed_bin(&c, (unsigned char *) cmd);
|
|
|
|
memset(cmd + rr, rand() & 255, sizeof(cmd) - rr);
|
|
|
|
mp_read_signed_bin(&d, (unsigned char *) cmd, rr);
|
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
|
|
|
printf("mp_signed_bin failure!\n");
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
rr = mp_unsigned_bin_size(&c);
|
|
|
|
mp_to_unsigned_bin(&c, (unsigned char *) cmd);
|
|
|
|
memset(cmd + rr, rand() & 255, sizeof(cmd) - rr);
|
|
|
|
mp_read_unsigned_bin(&d, (unsigned char *) cmd, rr);
|
|
|
|
if (mp_cmp_mag(&c, &d) != MP_EQ) {
|
|
|
|
printf("mp_unsigned_bin failure!\n");
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!strcmp(cmd, "sub")) {
|
|
|
|
++sub_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
mp_copy(&a, &d);
|
|
|
|
mp_sub(&d, &b, &d);
|
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
|
|
|
printf("sub %lu failure!\n", sub_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "mul")) {
|
|
|
|
++mul_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
mp_copy(&a, &d);
|
|
|
|
mp_mul(&d, &b, &d);
|
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
|
|
|
printf("mul %lu failure!\n", mul_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "div")) {
|
|
|
|
++div_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&d, buf, 64);
|
|
|
|
|
|
|
|
mp_div(&a, &b, &e, &f);
|
|
|
|
if (mp_cmp(&c, &e) != MP_EQ || mp_cmp(&d, &f) != MP_EQ) {
|
|
|
|
printf("div %lu %d, %d, failure!\n", div_n, mp_cmp(&c, &e),
|
|
|
|
mp_cmp(&d, &f));
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
|
|
|
draw(&e);
|
|
|
|
draw(&f);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!strcmp(cmd, "sqr")) {
|
|
|
|
++sqr_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
mp_copy(&a, &c);
|
|
|
|
mp_sqr(&c, &c);
|
|
|
|
if (mp_cmp(&b, &c) != MP_EQ) {
|
|
|
|
printf("sqr %lu failure!\n", sqr_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "gcd")) {
|
|
|
|
++gcd_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
mp_copy(&a, &d);
|
|
|
|
mp_gcd(&d, &b, &d);
|
|
|
|
d.sign = c.sign;
|
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
|
|
|
printf("gcd %lu failure!\n", gcd_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "lcm")) {
|
|
|
|
++lcm_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
mp_copy(&a, &d);
|
|
|
|
mp_lcm(&d, &b, &d);
|
|
|
|
d.sign = c.sign;
|
|
|
|
if (mp_cmp(&c, &d) != MP_EQ) {
|
|
|
|
printf("lcm %lu failure!\n", lcm_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "expt")) {
|
|
|
|
++expt_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&d, buf, 64);
|
|
|
|
mp_copy(&a, &e);
|
|
|
|
mp_exptmod(&e, &b, &c, &e);
|
|
|
|
if (mp_cmp(&d, &e) != MP_EQ) {
|
|
|
|
printf("expt %lu failure!\n", expt_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
|
|
|
draw(&e);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "invmod")) {
|
|
|
|
++inv_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&c, buf, 64);
|
|
|
|
mp_invmod(&a, &b, &d);
|
|
|
|
mp_mulmod(&d, &a, &b, &e);
|
|
|
|
if (mp_cmp_d(&e, 1) != MP_EQ) {
|
|
|
|
printf("inv [wrong value from MPI?!] failure\n");
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
draw(&d);
|
|
|
|
mp_gcd(&a, &b, &e);
|
|
|
|
draw(&e);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
} else if (!strcmp(cmd, "div2")) {
|
|
|
|
++div2_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
mp_div_2(&a, &c);
|
|
|
|
if (mp_cmp(&c, &b) != MP_EQ) {
|
|
|
|
printf("div_2 %lu failure\n", div2_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "mul2")) {
|
|
|
|
++mul2_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
mp_mul_2(&a, &c);
|
|
|
|
if (mp_cmp(&c, &b) != MP_EQ) {
|
|
|
|
printf("mul_2 %lu failure\n", mul2_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "add_d")) {
|
|
|
|
++add_d_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
sscanf(buf, "%d", &ix);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
mp_add_d(&a, ix, &c);
|
|
|
|
if (mp_cmp(&b, &c) != MP_EQ) {
|
|
|
|
printf("add_d %lu failure\n", add_d_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
printf("d == %d\n", ix);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
} else if (!strcmp(cmd, "sub_d")) {
|
|
|
|
++sub_d_n;
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&a, buf, 64);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
sscanf(buf, "%d", &ix);
|
|
|
|
fgets(buf, 4095, stdin);
|
|
|
|
mp_read_radix(&b, buf, 64);
|
|
|
|
mp_sub_d(&a, ix, &c);
|
|
|
|
if (mp_cmp(&b, &c) != MP_EQ) {
|
|
|
|
printf("sub_d %lu failure\n", sub_d_n);
|
|
|
|
draw(&a);
|
|
|
|
draw(&b);
|
|
|
|
draw(&c);
|
|
|
|
printf("d == %d\n", ix);
|
2014-02-14 06:57:52 -05:00
|
|
|
return EXIT_FAILURE;
|
2005-02-12 03:40:15 -05:00
|
|
|
}
|
|
|
|
}
|
2003-02-28 11:02:06 -05:00
|
|
|
}
|
2014-02-14 05:48:34 -05:00
|
|
|
#endif
|
2003-05-17 08:33:54 -04:00
|
|
|
return 0;
|
2003-02-28 11:02:06 -05:00
|
|
|
}
|
2005-08-01 12:37:28 -04:00
|
|
|
|
|
|
|
/* $Source$ */
|
|
|
|
/* $Revision$ */
|
|
|
|
/* $Date$ */
|