tommath/demo/demo.c

1292 lines
37 KiB
C
Raw Normal View History

2015-11-19 15:34:58 -05:00
#include <string.h>
2003-02-28 11:02:06 -05:00
#include <time.h>
/*
* Configuration
*/
#ifndef LTM_DEMO_TEST_VS_MTEST
2015-11-29 17:14:50 -05:00
#define LTM_DEMO_TEST_VS_MTEST 1
#endif
#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
#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
2017-12-09 15:08:54 -05:00
static void ndraw(mp_int *a, const 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);
mp_toradix(a, buf, 16);
printf("0x%s\n", buf);
2003-02-28 11:02:06 -05:00
}
#if LTM_DEMO_TEST_VS_MTEST != 0
2017-10-18 04:44:06 -04:00
static void draw(mp_int *a)
2003-02-28 11:04:18 -05:00
{
ndraw(a, "");
}
#endif
2003-02-28 11:04:18 -05:00
#if defined(LTM_DEMO_REAL_RAND) && !defined(_WIN32)
2017-10-18 04:44:06 -04:00
static FILE *fd_urandom;
#endif
2018-02-01 16:28:43 -05:00
#if LTM_DEMO_TEST_VS_MTEST == 0
static int myrng(unsigned char *dst, int len, void *dat)
2004-04-11 16:46:22 -04:00
{
int x;
(void)dat;
#if defined(LTM_DEMO_REAL_RAND)
if (!fd_urandom) {
2017-10-18 04:44:06 -04:00
# if !defined(_WIN32)
fprintf(stderr, "\nno /dev/urandom\n");
2017-10-18 04:44:06 -04:00
# endif
} else {
2018-02-13 13:04:25 -05:00
return fread(dst, 1uL, len, fd_urandom);
}
#endif
2017-10-18 04:44:06 -04:00
for (x = 0; x < len;) {
unsigned int r = (unsigned int)rand();
do {
2018-02-13 13:04:25 -05:00
dst[x++] = r & 0xFFu;
r >>= 8;
2018-02-13 13:04:25 -05:00
} while ((r != 0u) && (x < len));
}
2004-04-11 16:46:22 -04:00
return len;
}
2018-02-01 16:28:43 -05:00
#endif
2004-04-11 16:46:22 -04:00
#if LTM_DEMO_TEST_VS_MTEST != 0
static void _panic(int l)
{
2017-10-18 04:44:06 -04:00
fprintf(stderr, "\n%d: fgets failed\n", l);
exit(EXIT_FAILURE);
}
2017-10-18 05:39:14 -04:00
#define FGETS(str, size, stream) \
{ \
char *ret = fgets(str, size, stream); \
if (!ret) { _panic(__LINE__); } \
}
2018-12-27 03:24:25 -05:00
#endif
2017-10-18 05:39:14 -04:00
2018-02-01 16:28:43 -05:00
static 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)
{
2017-10-18 04:44:06 -04:00
mp_clear_multi(&a, &b, &c, &d, &e, &f, NULL);
printf("\n");
#ifdef LTM_DEMO_REAL_RAND
2017-10-18 04:44:06 -04:00
if (fd_urandom)
fclose(fd_urandom);
#endif
2014-02-14 06:59:04 -05:00
}
2018-02-01 16:28:43 -05:00
#if LTM_DEMO_TEST_VS_MTEST == 0
struct mp_sqrtmod_prime_st {
unsigned long p;
unsigned long n;
mp_digit r;
};
2018-02-01 16:28:43 -05:00
static struct mp_sqrtmod_prime_st sqrtmod_prime[] = {
2017-10-18 04:44:06 -04:00
{ 5, 14, 3 },
{ 7, 9, 4 },
{ 113, 2, 62 }
};
2015-11-29 16:43:52 -05:00
struct mp_jacobi_st {
unsigned long n;
int c[16];
};
2018-02-01 16:28:43 -05:00
static struct mp_jacobi_st jacobi[] = {
2017-10-18 04:44:06 -04:00
{ 3, { 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 } },
{ 5, { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0 } },
{ 7, { 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1 } },
{ 9, { -1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } },
2015-11-29 16:43:52 -05:00
};
struct mp_kronecker_st {
long n;
int c[21];
};
static struct mp_kronecker_st kronecker[] = {
/*-10, -9, -8, -7,-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10*/
{ -10, { 0, -1, 0, -1, 0, 0, 0, 1, 0, -1, 0, 1, 0, -1, 0, 0, 0, 1, 0, 1, 0 } },
{ -9, { -1, 0, -1, 1, 0, -1, -1, 0, -1, -1, 0, 1, 1, 0, 1, 1, 0, -1, 1, 0, 1 } },
{ -8, { 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0 } },
{ -7, { 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1, 1, -1, -1, 0, 1, 1, -1 } },
{ -6, { 0, 0, 0, -1, 0, -1, 0, 0, 0, -1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0 } },
{ -5, { 0, -1, 1, -1, 1, 0, -1, -1, 1, -1, 0, 1, -1, 1, 1, 0, -1, 1, -1, 1, 0 } },
{ -4, { 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0, -1, 0, 1, 0 } },
{ -3, { -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1, -1, 0, 1 } },
{ -2, { 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0 } },
{ -1, { -1, -1, -1, 1, 1, -1, -1, 1, -1, -1, 1, 1, 1, -1, 1, 1, -1, -1, 1, 1, 1 } },
{ 0, { 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0 } },
{ 1, { 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 } },
{ 2, { 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0 } },
{ 3, { 1, 0, -1, -1, 0, -1, 1, 0, -1, 1, 0, 1, -1, 0, 1, -1, 0, -1, -1, 0, 1 } },
{ 4, { 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0, 1, 0 } },
{ 5, { 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0, 1, -1, -1, 1, 0 } },
{ 6, { 0, 0, 0, -1, 0, 1, 0, 0, 0, 1, 0, 1, 0, 0, 0, 1, 0, -1, 0, 0, 0 } },
{ 7, { -1, 1, 1, 0, 1, -1, 1, 1, 1, 1, 0, 1, 1, 1, 1, -1, 1, 0, 1, 1, -1 } },
{ 8, { 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0, -1, 0, -1, 0, 1, 0, 1, 0 } },
{ 9, { 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1, 1, 0, 1 } },
{ 10, { 0, 1, 0, -1, 0, 0, 0, 1, 0, 1, 0, 1, 0, 1, 0, 0, 0, -1, 0, 1, 0 } }
};
2018-02-01 16:28:43 -05:00
#endif
2003-02-28 11:03:48 -05:00
2018-02-01 16:28:43 -05:00
#if LTM_DEMO_TEST_VS_MTEST != 0
static char cmd[4096];
#endif
static char buf[4096];
2003-02-28 11:02:06 -05:00
int main(void)
{
2003-02-28 11:09:08 -05:00
unsigned rr;
2018-12-27 03:10:09 -05:00
int ix;
#if LTM_DEMO_TEST_VS_MTEST != 0
unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n,
2017-10-18 04:44:06 -04:00
gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n;
#else
unsigned long s, t;
long k, m;
2014-12-10 12:59:42 -05:00
unsigned long long q, r;
mp_digit mp;
2018-12-27 03:10:09 -05:00
int i, n, err, should, cnt;
#endif
2003-02-28 11:08:34 -05:00
if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY)
2017-10-18 04:44:06 -04:00
return EXIT_FAILURE;
2003-02-28 11:08:34 -05:00
atexit(_cleanup);
2003-09-19 18:43:07 -04:00
#if defined(LTM_DEMO_REAL_RAND)
if (!fd_urandom) {
fd_urandom = fopen("/dev/urandom", "r");
if (!fd_urandom) {
2017-10-18 04:44:06 -04:00
# if !defined(_WIN32)
fprintf(stderr, "\ncould not open /dev/urandom\n");
2017-10-18 04:44:06 -04:00
# endif
}
}
#endif
srand(LTM_DEMO_RAND_SEED);
2003-07-02 11:39:39 -04:00
#ifdef MP_8BIT
printf("Digit size 8 Bit \n");
#endif
#ifdef MP_16BIT
printf("Digit size 16 Bit \n");
#endif
#ifdef MP_32BIT
printf("Digit size 32 Bit \n");
#endif
#ifdef MP_64BIT
printf("Digit size 64 Bit \n");
#endif
printf("Size of mp_digit: %u\n", (unsigned int)sizeof(mp_digit));
printf("Size of mp_word: %u\n", (unsigned int)sizeof(mp_word));
printf("DIGIT_BIT: %d\n", DIGIT_BIT);
printf("MP_PREC: %d\n", MP_PREC);
#if LTM_DEMO_TEST_VS_MTEST == 0
2018-11-26 06:32:05 -05:00
/* trivial stuff */
/* a: 0->5 */
2015-04-26 09:27:52 -04:00
mp_set_int(&a, 5);
2018-11-26 06:32:05 -05:00
/* a: 5-> b: -5 */
2015-04-26 09:27:52 -04:00
mp_neg(&a, &b);
if (mp_cmp(&a, &b) != MP_GT) {
return EXIT_FAILURE;
}
if (mp_cmp(&b, &a) != MP_LT) {
return EXIT_FAILURE;
}
2018-11-26 06:32:05 -05:00
/* a: 5-> a: -5 */
2015-04-26 09:27:52 -04:00
mp_neg(&a, &a);
if (mp_cmp(&b, &a) != MP_EQ) {
return EXIT_FAILURE;
}
2018-11-26 06:32:05 -05:00
/* a: -5-> b: 5 */
2015-04-26 09:27:52 -04:00
mp_abs(&a, &b);
if (mp_isneg(&b) != MP_NO) {
return EXIT_FAILURE;
}
2018-11-26 06:32:05 -05:00
/* a: -5-> b: -4 */
2018-02-13 13:04:25 -05:00
mp_add_d(&a, 1uL, &b);
2016-06-02 03:40:05 -04:00
if (mp_isneg(&b) != MP_YES) {
return EXIT_FAILURE;
}
if (mp_get_int(&b) != 4) {
return EXIT_FAILURE;
}
2018-11-26 06:32:05 -05:00
/* a: -5-> b: 1 */
2018-02-13 13:04:25 -05:00
mp_add_d(&a, 6uL, &b);
2016-06-02 03:40:05 -04:00
if (mp_get_int(&b) != 1) {
return EXIT_FAILURE;
}
2018-11-26 06:32:05 -05:00
/* a: -5-> a: 1 */
2018-02-13 13:04:25 -05:00
mp_add_d(&a, 6uL, &a);
2016-06-02 03:40:05 -04:00
if (mp_get_int(&a) != 1) {
return EXIT_FAILURE;
}
mp_zero(&a);
2018-11-26 06:32:05 -05:00
/* a: 0-> a: 6 */
2018-02-13 13:04:25 -05:00
mp_add_d(&a, 6uL, &a);
if (mp_get_int(&a) != 6) {
return EXIT_FAILURE;
}
2015-04-26 09:27:52 -04:00
2017-10-09 08:56:38 -04:00
mp_set_int(&a, 42);
mp_set_int(&b, 1);
mp_neg(&b, &b);
mp_set_int(&c, 1);
mp_exptmod(&a, &b, &c, &d);
mp_set_int(&c, 7);
mp_exptmod(&a, &b, &c, &d);
2015-11-29 16:43:52 -05:00
mp_set_int(&a, 0);
mp_set_int(&b, 1);
if ((err = mp_jacobi(&a, &b, &i)) != MP_OKAY) {
printf("Failed executing mp_jacobi(0 | 1) %s.\n", mp_error_to_string(err));
2015-11-29 16:43:52 -05:00
return EXIT_FAILURE;
}
if (i != 1) {
printf("Failed trivial mp_jacobi(0 | 1) %d != 1\n", i);
return EXIT_FAILURE;
}
for (cnt = 0; cnt < (int)(sizeof(jacobi)/sizeof(jacobi[0])); ++cnt) {
mp_set_int(&b, jacobi[cnt].n);
/* only test positive values of a */
for (n = -5; n <= 10; ++n) {
2015-11-29 16:43:52 -05:00
mp_set_int(&a, abs(n));
should = MP_OKAY;
if (n < 0) {
mp_neg(&a, &a);
/* Until #44 is fixed the negative a's must fail */
should = MP_VAL;
}
if ((err = mp_jacobi(&a, &b, &i)) != should) {
printf("Failed executing mp_jacobi(%d | %lu) %s.\n", n, jacobi[cnt].n, mp_error_to_string(err));
2015-11-29 16:43:52 -05:00
return EXIT_FAILURE;
}
2018-12-27 03:19:57 -05:00
if ((err == MP_OKAY) && (i != jacobi[cnt].c[n + 5])) {
2015-11-29 16:43:52 -05:00
printf("Failed trivial mp_jacobi(%d | %lu) %d != %d\n", n, jacobi[cnt].n, i, jacobi[cnt].c[n + 5]);
return EXIT_FAILURE;
}
}
}
mp_set_int(&a, 0);
mp_set_int(&b, 1u);
if ((err = mp_kronecker(&a, &b, &i)) != MP_OKAY) {
printf("Failed executing mp_kronecker(0 | 1) %s.\n", mp_error_to_string(err));
return EXIT_FAILURE;
}
if (i != 1) {
printf("Failed trivial mp_kronecker(0 | 1) %d != 1\n", i);
return EXIT_FAILURE;
}
for (cnt = 0; cnt < (int)(sizeof(kronecker)/sizeof(kronecker[0])); ++cnt) {
k = kronecker[cnt].n;
if (k < 0) {
mp_set_int(&a, (unsigned long)(-k));
mp_neg(&a, &a);
} else {
mp_set_int(&a, (unsigned long) k);
}
/* only test positive values of a */
for (m = -10; m <= 10; m++) {
if (m < 0) {
mp_set_int(&b,(unsigned long)(-m));
mp_neg(&b, &b);
} else {
mp_set_int(&b, (unsigned long) m);
}
if ((err = mp_kronecker(&a, &b, &i)) != MP_OKAY) {
printf("Failed executing mp_kronecker(%ld | %ld) %s.\n", kronecker[cnt].n, m, mp_error_to_string(err));
return EXIT_FAILURE;
}
2018-12-27 03:19:57 -05:00
if ((err == MP_OKAY) && (i != kronecker[cnt].c[m + 10])) {
printf("Failed trivial mp_kronecker(%ld | %ld) %d != %d\n", kronecker[cnt].n, m, i, kronecker[cnt].c[m + 10]);
return EXIT_FAILURE;
}
}
}
2018-11-26 06:32:05 -05:00
/* test mp_complement */
printf("\n\nTesting: mp_complement");
for (i = 0; i < 1000; ++i) {
int l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&a, labs(l));
if (l < 0)
mp_neg(&a, &a);
mp_complement(&a, &b);
l = ~l;
mp_set_int(&c, labs(l));
if (l < 0)
mp_neg(&c, &c);
if (mp_cmp(&b, &c) != MP_EQ) {
printf("\nmp_complement() bad result!");
return EXIT_FAILURE;
}
}
2018-11-26 06:32:05 -05:00
/* test mp_tc_div_2d */
printf("\n\nTesting: mp_tc_div_2d");
for (i = 0; i < 1000; ++i) {
int l, em;
l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&a, labs(l));
if (l < 0)
mp_neg(&a, &a);
em = rand() % 32;
mp_set_int(&d, labs(l >> em));
if ((l >> em) < 0)
mp_neg(&d, &d);
mp_tc_div_2d(&a, em, &b);
if (mp_cmp(&b, &d) != MP_EQ) {
printf("\nmp_tc_div_2d() bad result!");
return EXIT_FAILURE;
}
}
2018-11-26 06:32:05 -05:00
/* test mp_tc_xor */
printf("\n\nTesting: mp_tc_xor");
for (i = 0; i < 1000; ++i) {
int l, em;
l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&a, labs(l));
if (l < 0)
mp_neg(&a, &a);
em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&b, labs(em));
if (em < 0)
mp_neg(&b, &b);
mp_set_int(&d, labs(l ^ em));
if ((l ^ em) < 0)
mp_neg(&d, &d);
mp_tc_xor(&a, &b, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
printf("\nmp_tc_xor() bad result!");
return EXIT_FAILURE;
}
}
2018-11-26 06:32:05 -05:00
/* test mp_tc_or */
printf("\n\nTesting: mp_tc_or");
for (i = 0; i < 1000; ++i) {
int l, em;
l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&a, labs(l));
if (l < 0)
mp_neg(&a, &a);
em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&b, labs(em));
if (em < 0)
mp_neg(&b, &b);
mp_set_int(&d, labs(l | em));
if ((l | em) < 0)
mp_neg(&d, &d);
mp_tc_or(&a, &b, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
printf("\nmp_tc_or() bad result!");
return EXIT_FAILURE;
}
}
2018-11-26 06:32:05 -05:00
/* test mp_tc_and */
printf("\n\nTesting: mp_tc_and");
for (i = 0; i < 1000; ++i) {
int l, em;
l = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&a, labs(l));
if (l < 0)
mp_neg(&a, &a);
em = (rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
mp_set_int(&b, labs(em));
if (em < 0)
mp_neg(&b, &b);
mp_set_int(&d, labs(l & em));
if ((l & em) < 0)
mp_neg(&d, &d);
mp_tc_and(&a, &b, &c);
if (mp_cmp(&c, &d) != MP_EQ) {
printf("\nmp_tc_and() bad result!");
return EXIT_FAILURE;
}
}
2018-11-26 06:32:05 -05:00
/* mp_invmod corner-case of https://github.com/libtom/libtommath/issues/118 */
2018-09-22 10:36:56 -04:00
printf("\n\nTesting: mp_invmod");
{
const char *a_ = "47182BB8DF0FFE9F61B1F269BACC066B48BA145D35137D426328DC3F88A5EA44";
const char *b_ = "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF";
const char *should_ = "0521A82E10376F8E4FDEF9A32A427AC2A0FFF686E00290D39E3E4B5522409596";
if (mp_read_radix(&a, a_, 16) != MP_OKAY) {
printf("\nmp_read_radix(a) failed!");
return EXIT_FAILURE;
}
if (mp_read_radix(&b, b_, 16) != MP_OKAY) {
printf("\nmp_read_radix(b) failed!");
return EXIT_FAILURE;
}
if (mp_read_radix(&c, should_, 16) != MP_OKAY) {
printf("\nmp_read_radix(should) failed!");
return EXIT_FAILURE;
}
if (mp_invmod(&a, &b, &d) != MP_OKAY) {
printf("\nmp_invmod() failed!");
return EXIT_FAILURE;
}
if (mp_cmp(&c, &d) != MP_EQ) {
printf("\nmp_invmod() bad result!");
return EXIT_FAILURE;
}
}
2018-11-26 06:32:05 -05:00
/* test mp_get_double/mp_set_double */
2018-09-10 12:51:26 -04:00
#if defined(__STDC_IEC_559__) || defined(__GCC_IEC_559)
printf("\n\nTesting: mp_get_double");
if (mp_set_double(&a, +1.0/0.0) != MP_VAL) {
printf("\nmp_set_double should return MP_VAL for +inf");
return EXIT_FAILURE;
}
if (mp_set_double(&a, -1.0/0.0) != MP_VAL) {
printf("\nmp_set_double should return MP_VAL for -inf");
return EXIT_FAILURE;
}
if (mp_set_double(&a, +0.0/0.0) != MP_VAL) {
printf("\nmp_set_double should return MP_VAL for NaN");
return EXIT_FAILURE;
}
if (mp_set_double(&a, -0.0/0.0) != MP_VAL) {
printf("\nmp_set_double should return MP_VAL for NaN");
return EXIT_FAILURE;
}
for (i = 0; i < 1000; ++i) {
int tmp = rand();
double dbl = (double)tmp * rand() + 1;
if (mp_set_double(&a, dbl) != MP_OKAY) {
printf("\nmp_set_double() failed");
return EXIT_FAILURE;
}
if (dbl != mp_get_double(&a)) {
printf("\nmp_get_double() bad result!");
return EXIT_FAILURE;
}
if (mp_set_double(&a, -dbl) != MP_OKAY) {
printf("\nmp_set_double() failed");
return EXIT_FAILURE;
}
if (-dbl != mp_get_double(&a)) {
printf("\nmp_get_double() bad result!");
return EXIT_FAILURE;
}
}
#endif
2018-11-26 06:32:05 -05:00
/* test mp_get_int */
printf("\n\nTesting: mp_get_int");
2005-02-12 03:40:15 -05:00
for (i = 0; i < 1000; ++i) {
2018-02-05 14:22:17 -05:00
t = (unsigned long)(rand() * rand() + 1) & 0xFFFFFFFFuL;
2017-10-18 04:44:06 -04:00
mp_set_int(&a, t);
if (t != mp_get_int(&a)) {
printf("\nmp_get_int() bad result!");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
}
mp_set_int(&a, 0);
if (mp_get_int(&a) != 0) {
printf("\nmp_get_int() bad result!");
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2018-02-13 13:04:25 -05:00
mp_set_int(&a, 0xFFFFFFFFuL);
if (mp_get_int(&a) != 0xFFFFFFFFuL) {
printf("\nmp_get_int() bad result!");
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
printf("\n\nTesting: mp_get_long\n");
2018-12-27 03:19:57 -05:00
for (i = 0; i < ((int)(sizeof(unsigned long)*CHAR_BIT) - 1); ++i) {
t = (1ULL << (i+1)) - 1;
if (!t)
t = -1;
2014-12-10 12:27:03 -05:00
printf(" t = 0x%lx i = %d\r", t, i);
do {
if (mp_set_long(&a, t) != MP_OKAY) {
printf("\nmp_set_long() error!");
return EXIT_FAILURE;
}
s = mp_get_long(&a);
if (s != t) {
printf("\nmp_get_long() bad result! 0x%lx != 0x%lx", s, t);
return EXIT_FAILURE;
}
t <<= 1;
2017-10-18 04:44:06 -04:00
} while (t);
}
2014-12-10 12:59:42 -05:00
printf("\n\nTesting: mp_get_long_long\n");
2018-12-27 03:19:57 -05:00
for (i = 0; i < ((int)(sizeof(unsigned long long)*CHAR_BIT) - 1); ++i) {
2014-12-10 12:59:42 -05:00
r = (1ULL << (i+1)) - 1;
if (!r)
r = -1;
printf(" r = 0x%llx i = %d\r", r, i);
do {
if (mp_set_long_long(&a, r) != MP_OKAY) {
printf("\nmp_set_long_long() error!");
return EXIT_FAILURE;
}
q = mp_get_long_long(&a);
if (q != r) {
printf("\nmp_get_long_long() bad result! 0x%llx != 0x%llx", q, r);
return EXIT_FAILURE;
}
r <<= 1;
2017-10-18 04:44:06 -04:00
} while (r);
2014-12-10 12:59:42 -05:00
}
2018-11-26 06:32:05 -05:00
/* test mp_sqrt */
printf("\n\nTesting: mp_sqrt\n");
2005-02-12 03:40:15 -05:00
for (i = 0; i < 1000; ++i) {
2017-10-18 04:44:06 -04:00
printf("%6d\r", i);
fflush(stdout);
n = (rand() & 15) + 1;
mp_rand(&a, n);
if (mp_sqrt(&a, &b) != MP_OKAY) {
printf("\nmp_sqrt() error!");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2017-10-18 04:44:06 -04:00
mp_n_root_ex(&a, 2, &c, 0);
mp_n_root_ex(&a, 2, &d, 1);
if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("\nmp_n_root_ex() bad result!");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
}
2017-10-18 04:44:06 -04:00
if (mp_cmp_mag(&b, &c) != MP_EQ) {
printf("mp_sqrt() bad result!\n");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
}
2004-04-11 16:46:22 -04:00
printf("\n\nTesting: mp_is_square\n");
2005-02-12 03:40:15 -05:00
for (i = 0; i < 1000; ++i) {
2017-10-18 04:44:06 -04:00
printf("%6d\r", i);
fflush(stdout);
2005-02-12 03:40:15 -05:00
/* test mp_is_square false negatives */
2017-10-18 04:44:06 -04:00
n = (rand() & 7) + 1;
mp_rand(&a, n);
mp_sqr(&a, &a);
if (mp_is_square(&a, &n) != MP_OKAY) {
printf("\nfn:mp_is_square() error!");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
if (n == 0) {
2017-10-18 04:44:06 -04:00
printf("\nfn:mp_is_square() bad result!");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
/* test for false positives */
2018-02-13 13:04:25 -05:00
mp_add_d(&a, 1uL, &a);
2017-10-18 04:44:06 -04:00
if (mp_is_square(&a, &n) != MP_OKAY) {
printf("\nfp:mp_is_square() error!");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
if (n == 1) {
2017-10-18 04:44:06 -04:00
printf("\nfp:mp_is_square() bad result!");
2015-04-26 09:27:52 -04:00
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
2018-11-26 06:32:05 -05:00
/* r^2 = n (mod p) */
for (i = 0; i < (int)(sizeof(sqrtmod_prime)/sizeof(sqrtmod_prime[0])); ++i) {
mp_set_int(&a, sqrtmod_prime[i].p);
mp_set_int(&b, sqrtmod_prime[i].n);
if (mp_sqrtmod_prime(&b, &a, &c) != MP_OKAY) {
printf("Failed executing %d. mp_sqrtmod_prime\n", (i+1));
return EXIT_FAILURE;
}
if (mp_cmp_d(&c, sqrtmod_prime[i].r) != MP_EQ) {
printf("Failed %d. trivial mp_sqrtmod_prime\n", (i+1));
ndraw(&c, "r");
return EXIT_FAILURE;
}
}
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++) {
2017-10-18 04:44:06 -04:00
printf("Testing (not safe-prime): %9d bits \r", ix);
fflush(stdout);
err = mp_prime_random_ex(&a, 8, ix,
(rand() & 1) ? 0 : LTM_PRIME_2MSB_ON, myrng,
NULL);
2005-02-12 03:40:15 -05:00
if (err != MP_OKAY) {
2017-10-18 04:44:06 -04:00
printf("failed with err code %d\n", err);
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2017-10-18 04:44:06 -04:00
if (mp_count_bits(&a) != ix) {
printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2004-04-11 16:46:22 -04:00
}
printf("\n");
2004-04-11 16:46:22 -04:00
/* strong Miller-Rabin pseudoprime to the first 200 primes (F. Arnault) */
puts("Testing mp_prime_is_prime() with Arnault's pseudoprime 803...901 \n");
mp_read_radix(&a,
"91xLNF3roobhzgTzoFIG6P13ZqhOVYSN60Fa7Cj2jVR1g0k89zdahO9/kAiRprpfO1VAp1aBHucLFV/qLKLFb+zonV7R2Vxp1K13ClwUXStpV0oxTNQVjwybmFb5NBEHImZ6V7P6+udRJuH8VbMEnS0H8/pSqQrg82OoQQ2fPpAk6G1hkjqoCv5s/Yr",
64);
mp_prime_is_prime(&a, 8, &cnt);
if (cnt == MP_YES) {
printf("Arnault's pseudoprime is not prime but mp_prime_is_prime says it is.\n");
return EXIT_FAILURE;
}
/* About the same size as Arnault's pseudoprime */
puts("Testing mp_prime_is_prime() with certified prime 2^1119 + 53\n");
mp_set(&a,1u);
mp_mul_2d(&a,1119,&a);
mp_add_d(&a,53,&a);
mp_prime_is_prime(&a, 8, &cnt);
if (cnt == MP_NO) {
printf("A certified prime is a prime but mp_prime_is_prime says it not.\n");
return EXIT_FAILURE;
}
2005-03-12 06:55:11 -05:00
for (ix = 16; ix < 128; ix++) {
2017-10-18 04:44:06 -04:00
printf("Testing ( safe-prime): %9d bits \r", ix);
fflush(stdout);
err = mp_prime_random_ex(
&a, 8, ix, ((rand() & 1) ? 0 : LTM_PRIME_2MSB_ON) | LTM_PRIME_SAFE,
myrng, NULL);
2005-02-12 03:40:15 -05:00
if (err != MP_OKAY) {
2017-10-18 04:44:06 -04:00
printf("failed with err code %d\n", err);
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2017-10-18 04:44:06 -04:00
if (mp_count_bits(&a) != ix) {
printf("Prime is %d not %d bits!!!\n", mp_count_bits(&a), ix);
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
/* let's see if it's really a safe prime */
2018-02-13 13:04:25 -05:00
mp_sub_d(&a, 1uL, &a);
2017-10-18 04:44:06 -04:00
mp_div_2(&a, &a);
mp_prime_is_prime(&a, 8, &cnt);
2005-02-12 03:40:15 -05:00
if (cnt != MP_YES) {
2017-10-18 04:44:06 -04:00
printf("sub is not prime!\n");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2004-04-11 16:46:22 -04:00
}
printf("\n\n");
2018-11-26 06:32:05 -05:00
/* test montgomery */
printf("Testing: montgomery...\n");
for (i = 1; i <= 10; i++) {
if (i == 10)
i = 1000;
printf(" digit size: %2d\r", i);
fflush(stdout);
for (n = 0; n < 1000; n++) {
mp_rand(&a, i);
a.dp[0] |= 1;
2018-11-26 06:32:05 -05:00
/* let's see if R is right */
mp_montgomery_calc_normalization(&b, &a);
mp_montgomery_setup(&a, &mp);
2018-11-26 06:32:05 -05:00
/* now test a random reduction */
for (ix = 0; ix < 100; ix++) {
2017-10-18 04:44:06 -04:00
mp_rand(&c, 1 + abs(rand()) % (2*i));
mp_copy(&c, &d);
mp_copy(&c, &e);
2017-10-18 04:44:06 -04:00
mp_mod(&d, &a, &d);
mp_montgomery_reduce(&c, &a, mp);
mp_mulmod(&c, &b, &a, &c);
2017-10-18 04:44:06 -04:00
if (mp_cmp(&c, &d) != MP_EQ) {
/* *INDENT-OFF* */
2018-12-27 03:21:12 -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);
printf("compare no compare!\n"); return EXIT_FAILURE;
2017-10-18 04:44:06 -04:00
/* *INDENT-ON* */
}
/* only one big montgomery reduction */
if (i > 10) {
n = 1000;
ix = 100;
}
}
}
}
printf("\n\n");
2004-04-11 16:46:22 -04:00
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 */
printf("\n\nTesting: mp_cnt_lsb");
2018-02-13 13:04:25 -05:00
mp_set(&a, 1uL);
2004-04-11 16:46:22 -04:00
for (ix = 0; ix < 1024; ix++) {
2017-10-18 04:44:06 -04:00
if (mp_cnt_lsb(&a) != ix) {
printf("Failed at %d, %d\n", ix, mp_cnt_lsb(&a));
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
2017-10-18 04:44:06 -04:00
mp_mul_2(&a, &a);
2003-07-02 11:39:39 -04:00
}
2017-10-18 04:44:06 -04:00
/* test mp_reduce_2k */
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;
2017-10-18 04:44:06 -04:00
mp_2expt(&a, cnt);
2018-02-13 13:04:25 -05:00
mp_sub_d(&a, 2uL, &a); /* a = 2**cnt - 2 */
2005-02-12 03:40:15 -05:00
2017-10-18 04:44:06 -04:00
printf("\r %4d bits", cnt);
printf("(%d)", mp_reduce_is_2k(&a));
mp_reduce_2k_setup(&a, &tmp);
printf("(%lu)", (unsigned long) tmp);
2005-02-12 03:40:15 -05:00
for (ix = 0; ix < 1000; ix++) {
2015-04-26 09:27:52 -04:00
if (!(ix & 127)) {
2017-10-18 04:44:06 -04:00
printf(".");
fflush(stdout);
}
mp_rand(&b, (cnt / DIGIT_BIT + 1) * 2);
mp_copy(&c, &b);
mp_mod(&c, &a, &c);
2018-02-13 13:04:25 -05:00
mp_reduce_2k(&b, &a, 2uL);
2017-10-18 04:44:06 -04:00
if (mp_cmp(&c, &b)) {
printf("FAILED\n");
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
}
2005-02-12 03:40:15 -05:00
}
}
2003-09-19 18:43:07 -04:00
2017-10-18 04:44:06 -04:00
/* test mp_div_3 */
printf("\n\nTesting: mp_div_3...\n");
2018-02-13 13:04:25 -05:00
mp_set(&d, 3uL);
2005-02-12 03:40:15 -05:00
for (cnt = 0; cnt < 10000;) {
mp_digit r2;
2003-09-19 18:43:07 -04:00
2017-10-18 04:44:06 -04:00
if (!(++cnt & 127)) {
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)) {
2017-10-18 04:44:06 -04:00
printf("\nmp_div_3 => Failure\n");
2003-05-29 09:35:26 -04:00
}
}
printf("\nPassed div_3 testing");
2003-05-17 08:33:54 -04:00
2017-10-18 04:44:06 -04:00
/* test the DR reduction */
printf("\n\nTesting: mp_dr_reduce...\n");
2004-08-09 18:15:59 -04:00
for (cnt = 2; cnt < 32; cnt++) {
2017-10-18 04:44:06 -04:00
printf("\r%d digit modulus", cnt);
mp_grow(&a, cnt);
mp_zero(&a);
2005-02-12 03:40:15 -05:00
for (ix = 1; ix < cnt; ix++) {
2015-04-26 09:27:52 -04:00
a.dp[ix] = MP_MASK;
2005-02-12 03:40:15 -05:00
}
a.used = cnt;
a.dp[0] = 3;
2017-10-18 04:44:06 -04:00
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 {
2015-04-26 09:27:52 -04:00
if (!(rr & 127)) {
2017-10-18 04:44:06 -04:00
printf(".");
fflush(stdout);
2015-04-26 09:27:52 -04:00
}
2017-10-18 04:44:06 -04:00
mp_sqr(&b, &b);
2018-02-13 13:04:25 -05:00
mp_add_d(&b, 1uL, &b);
2017-10-18 04:44:06 -04:00
mp_copy(&b, &c);
2015-04-26 09:27:52 -04:00
2017-10-18 04:44:06 -04:00
mp_mod(&b, &a, &b);
2018-12-27 03:34:11 -05:00
mp_dr_setup(&a, &mp);
mp_dr_reduce(&c, &a, mp);
2015-04-26 09:27:52 -04:00
2017-10-18 04:44:06 -04:00
if (mp_cmp(&b, &c) != MP_EQ) {
printf("Failed on trial %u\n", rr);
2015-04-26 09:27:52 -04:00
return EXIT_FAILURE;
}
2004-08-09 18:15:59 -04:00
} while (++rr < 500);
2017-10-18 04:44:06 -04:00
printf(" passed");
fflush(stdout);
2003-03-22 10:10:20 -05:00
}
2003-05-17 08:33:54 -04:00
2017-10-18 04:44:06 -04:00
# if LTM_DEMO_TEST_REDUCE_2K_L
/* test the mp_reduce_2k_l code */
# if LTM_DEMO_TEST_REDUCE_2K_L == 1
/* first load P with 2^1024 - 0x2A434 B9FDEC95 D8F9D550 FFFFFFFF FFFFFFFF */
2005-02-12 03:40:15 -05:00
mp_2expt(&a, 1024);
mp_read_radix(&b, "2A434B9FDEC95D8F9D550FFFFFFFFFFFFFFFF", 16);
mp_sub(&a, &b, &a);
2017-10-18 04:44:06 -04:00
# elif LTM_DEMO_TEST_REDUCE_2K_L == 2
/* p = 2^2048 - 0x1 00000000 00000000 00000000 00000000 4945DDBF 8EA2A91D 5776399B B83E188F */
2005-02-12 03:40:15 -05:00
mp_2expt(&a, 2048);
mp_read_radix(&b,
2017-10-18 04:44:06 -04:00
"1000000000000000000000000000000004945DDBF8EA2A91D5776399BB83E188F",
16);
2005-02-12 03:40:15 -05:00
mp_sub(&a, &b, &a);
2017-10-18 04:44:06 -04:00
# else
# error oops
# endif
2005-02-12 03:40:15 -05:00
mp_todecimal(&a, buf);
printf("\n\np==%s\n", buf);
2017-10-18 04:44:06 -04:00
/* now mp_reduce_is_2k_l() should return */
2005-02-12 03:40:15 -05:00
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);
printf("Testing: mp_reduce_2k_l...");
2005-02-12 03:40:15 -05:00
fflush(stdout);
for (cnt = 0; cnt < (int)(1UL << 20); cnt++) {
2005-02-12 03:40:15 -05:00
mp_sqr(&b, &b);
2018-02-13 13:04:25 -05:00
mp_add_d(&b, 1uL, &b);
2005-02-12 03:40:15 -05:00
mp_reduce_2k_l(&b, &a, &d);
mp_sqr(&c, &c);
2018-02-13 13:04:25 -05:00
mp_add_d(&c, 1uL, &c);
2005-02-12 03:40:15 -05:00
mp_mod(&c, &a, &c);
if (mp_cmp(&b, &c) != MP_EQ) {
2017-10-18 04:44:06 -04:00
printf("mp_reduce_2k_l() failed at step %d\n", cnt);
mp_tohex(&b, buf);
printf("b == %s\n", buf);
mp_tohex(&c, buf);
printf("c == %s\n", buf);
return EXIT_FAILURE;
2005-02-12 03:40:15 -05:00
}
}
printf("...Passed\n");
2017-10-18 04:44:06 -04:00
# endif /* LTM_DEMO_TEST_REDUCE_2K_L */
#else
2003-05-17 08:33:54 -04:00
div2_n = mul2_n = inv_n = expt_n = lcm_n = gcd_n = add_n =
2018-12-27 03:02:31 -05:00
sub_n = mul_n = div_n = sqr_n = mul2d_n = div2d_n = 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:
2017-10-18 04:44:06 -04:00
mp_clear(&a);
mp_init(&a);
break;
2005-02-12 03:40:15 -05:00
case 1:
2017-10-18 04:44:06 -04:00
mp_clear(&b);
mp_init(&b);
break;
2005-02-12 03:40:15 -05:00
case 2:
2017-10-18 04:44:06 -04:00
mp_clear(&c);
mp_init(&c);
break;
2005-02-12 03:40:15 -05:00
case 3:
2017-10-18 04:44:06 -04:00
mp_clear(&d);
mp_init(&d);
break;
2005-02-12 03:40:15 -05:00
case 4:
2017-10-18 04:44:06 -04:00
mp_clear(&e);
mp_init(&e);
break;
2005-02-12 03:40:15 -05:00
case 5:
2017-10-18 04:44:06 -04:00
mp_clear(&f);
mp_init(&f);
break;
2005-02-12 03:40:15 -05:00
case 6:
2017-10-18 04:44:06 -04:00
break; /* don't clear any */
2005-02-12 03:40:15 -05:00
}
2017-10-18 04:44:06 -04:00
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);
2017-10-18 05:39:14 -04:00
FGETS(cmd, 4095, stdin);
2018-02-13 13:04:25 -05:00
cmd[strlen(cmd) - 1u] = '\0';
printf("%-6s ]\r", cmd);
2005-02-12 03:40:15 -05:00
fflush(stdout);
2018-02-02 11:44:40 -05:00
if (strcmp(cmd, "mul2d") == 0) {
2017-10-18 04:44:06 -04:00
++mul2d_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-12-10 03:45:29 -05:00
sscanf(buf, "%u", &rr);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
mp_mul_2d(&a, rr, &a);
a.sign = b.sign;
if (mp_cmp(&a, &b) != MP_EQ) {
2017-12-10 03:45:29 -05:00
printf("mul2d failed, rr == %u\n", rr);
2017-10-18 04:44:06 -04:00
draw(&a);
draw(&b);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "div2d") == 0) {
2017-10-18 04:44:06 -04:00
++div2d_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-12-10 03:45:29 -05:00
sscanf(buf, "%u", &rr);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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)) {
2017-10-18 04:44:06 -04:00
a.sign = b.sign = MP_ZPOS;
}
if (mp_cmp(&a, &b) != MP_EQ) {
2017-12-10 03:45:29 -05:00
printf("div2d failed, rr == %u\n", rr);
2017-10-18 04:44:06 -04:00
draw(&a);
draw(&b);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "add") == 0) {
2017-10-18 04:44:06 -04:00
++add_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
/* test the sign/unsigned storage functions */
rr = mp_signed_bin_size(&c);
mp_to_signed_bin(&c, (unsigned char *) cmd);
2018-02-13 13:04:25 -05:00
memset(cmd + rr, rand() & 0xFFu, sizeof(cmd) - rr);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
rr = mp_unsigned_bin_size(&c);
mp_to_unsigned_bin(&c, (unsigned char *) cmd);
2018-02-13 13:04:25 -05:00
memset(cmd + rr, rand() & 0xFFu, sizeof(cmd) - rr);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2005-02-12 03:40:15 -05:00
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "sub") == 0) {
2017-10-18 04:44:06 -04:00
++sub_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "mul") == 0) {
2017-10-18 04:44:06 -04:00
++mul_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "div") == 0) {
2017-10-18 04:44:06 -04:00
++div_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&c, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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)) {
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2005-02-12 03:40:15 -05:00
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "sqr") == 0) {
2017-10-18 04:44:06 -04:00
++sqr_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "gcd") == 0) {
2017-10-18 04:44:06 -04:00
++gcd_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "lcm") == 0) {
2017-10-18 04:44:06 -04:00
++lcm_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "expt") == 0) {
2017-10-18 04:44:06 -04:00
++expt_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&c, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "invmod") == 0) {
2017-10-18 04:44:06 -04:00
++inv_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&b, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&c, buf, 64);
mp_invmod(&a, &b, &d);
mp_mulmod(&d, &a, &b, &e);
2018-02-13 13:04:25 -05:00
if (mp_cmp_d(&e, 1uL) != MP_EQ) {
2017-10-18 04:44:06 -04:00
printf("inv [wrong value from MPI?!] failure\n");
draw(&a);
draw(&b);
draw(&c);
draw(&d);
draw(&e);
mp_gcd(&a, &b, &e);
draw(&e);
return EXIT_FAILURE;
}
2005-02-12 03:40:15 -05:00
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "div2") == 0) {
2017-10-18 04:44:06 -04:00
++div2_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "mul2") == 0) {
2017-10-18 04:44:06 -04:00
++mul2_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "add_d") == 0) {
2017-10-18 04:44:06 -04:00
++add_d_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
sscanf(buf, "%d", &ix);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "sub_d") == 0) {
2017-10-18 04:44:06 -04:00
++sub_d_n;
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
mp_read_radix(&a, buf, 64);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
sscanf(buf, "%d", &ix);
2017-10-18 05:39:14 -04:00
FGETS(buf, 4095, stdin);
2017-10-18 04:44:06 -04:00
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);
return EXIT_FAILURE;
}
2018-02-02 11:44:40 -05:00
} else if (strcmp(cmd, "exit") == 0) {
printf("\nokay, exiting now\n");
break;
2005-02-12 03:40:15 -05:00
}
2003-02-28 11:02:06 -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
2017-08-28 10:27:26 -04:00
/* ref: $Format:%D$ */
/* git commit: $Format:%H$ */
/* commit time: $Format:%ai$ */