Merge pull request #431 from libtom/rework-tests

clean-up test.c
This commit is contained in:
Steffen Jaeckel 2019-10-29 15:38:31 +01:00 committed by GitHub
commit 57ccd9f0c3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 439 additions and 726 deletions

View File

@ -46,27 +46,27 @@ static int mtest_opponent(void)
switch (abs(rand()) % 7) {
case 0:
mp_clear(&a);
mp_init(&a);
DO(mp_init(&a));
break;
case 1:
mp_clear(&b);
mp_init(&b);
DO(mp_init(&b));
break;
case 2:
mp_clear(&c);
mp_init(&c);
DO(mp_init(&c));
break;
case 3:
mp_clear(&d);
mp_init(&d);
DO(mp_init(&d));
break;
case 4:
mp_clear(&e);
mp_init(&e);
DO(mp_init(&e));
break;
case 5:
mp_clear(&f);
mp_init(&f);
DO(mp_init(&f));
break;
case 6:
break; /* don't clear any */
@ -83,13 +83,13 @@ static int mtest_opponent(void)
if (strcmp(cmd, "mul2d") == 0) {
++mul2d_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
sscanf(buf, "%u", &rr);
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(mp_read_radix(&b, buf, 64));
mp_mul_2d(&a, (int)rr, &a);
DO(mp_mul_2d(&a, (int)rr, &a));
a.sign = b.sign;
if (mp_cmp(&a, &b) != MP_EQ) {
printf("mul2d failed, rr == %u\n", rr);
@ -100,13 +100,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "div2d") == 0) {
++div2d_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
sscanf(buf, "%u", &rr);
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(mp_read_radix(&b, buf, 64));
mp_div_2d(&a, (int)rr, &a, &e);
DO(mp_div_2d(&a, (int)rr, &a, &e));
a.sign = b.sign;
if ((a.used == b.used) && (a.used == 0)) {
a.sign = b.sign = MP_ZPOS;
@ -120,13 +120,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "add") == 0) {
++add_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(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);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_add(&d, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("add %lu failure!\n", add_n);
draw(&a);
@ -139,9 +139,9 @@ static int mtest_opponent(void)
/* test the sign/unsigned storage functions */
rr = (unsigned)mp_sbin_size(&c);
mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
DO(mp_to_sbin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr);
DO(mp_from_sbin(&d, (unsigned char *) cmd, (size_t)rr));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("mp_signed_bin failure!\n");
draw(&c);
@ -150,9 +150,9 @@ static int mtest_opponent(void)
}
rr = (unsigned)mp_ubin_size(&c);
mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL);
DO(mp_to_ubin(&c, (unsigned char *) cmd, (size_t)rr, NULL));
memset(cmd + rr, rand() & 0xFF, sizeof(cmd) - rr);
mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr);
DO(mp_from_ubin(&d, (unsigned char *) cmd, (size_t)rr));
if (mp_cmp_mag(&c, &d) != MP_EQ) {
printf("mp_unsigned_bin failure!\n");
draw(&c);
@ -163,13 +163,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "sub") == 0) {
++sub_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(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);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_sub(&d, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("sub %lu failure!\n", sub_n);
draw(&a);
@ -181,13 +181,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "mul") == 0) {
++mul_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(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);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_mul(&d, &b, &d));
if (mp_cmp(&c, &d) != MP_EQ) {
printf("mul %lu failure!\n", mul_n);
draw(&a);
@ -199,15 +199,15 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "div") == 0) {
++div_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(mp_read_radix(&b, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&c, buf, 64);
DO(mp_read_radix(&c, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&d, buf, 64);
DO(mp_read_radix(&d, buf, 64));
mp_div(&a, &b, &e, &f);
DO(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));
@ -223,11 +223,11 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "sqr") == 0) {
++sqr_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
mp_copy(&a, &c);
mp_sqr(&c, &c);
DO(mp_read_radix(&b, buf, 64));
DO(mp_copy(&a, &c));
DO(mp_sqr(&c, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
printf("sqr %lu failure!\n", sqr_n);
draw(&a);
@ -238,13 +238,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "gcd") == 0) {
++gcd_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(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);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_gcd(&d, &b, &d));
d.sign = c.sign;
if (mp_cmp(&c, &d) != MP_EQ) {
printf("gcd %lu failure!\n", gcd_n);
@ -257,13 +257,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "lcm") == 0) {
++lcm_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(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);
DO(mp_read_radix(&c, buf, 64));
DO(mp_copy(&a, &d));
DO(mp_lcm(&d, &b, &d));
d.sign = c.sign;
if (mp_cmp(&c, &d) != MP_EQ) {
printf("lcm %lu failure!\n", lcm_n);
@ -276,15 +276,15 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "expt") == 0) {
++expt_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(mp_read_radix(&b, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&c, buf, 64);
DO(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);
DO(mp_read_radix(&d, buf, 64));
DO(mp_copy(&a, &e));
DO(mp_exptmod(&e, &b, &c, &e));
if (mp_cmp(&d, &e) != MP_EQ) {
printf("expt %lu failure!\n", expt_n);
draw(&a);
@ -297,13 +297,13 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "invmod") == 0) {
++inv_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
DO(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);
DO(mp_read_radix(&c, buf, 64));
DO(mp_invmod(&a, &b, &d));
DO(mp_mulmod(&d, &a, &b, &e));
if (mp_cmp_d(&e, 1uL) != MP_EQ) {
printf("inv [wrong value from MPI?!] failure\n");
draw(&a);
@ -311,7 +311,7 @@ static int mtest_opponent(void)
draw(&c);
draw(&d);
draw(&e);
mp_gcd(&a, &b, &e);
DO(mp_gcd(&a, &b, &e));
draw(&e);
goto LBL_ERR;
}
@ -319,10 +319,10 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "div2") == 0) {
++div2_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
mp_div_2(&a, &c);
DO(mp_read_radix(&b, buf, 64));
DO(mp_div_2(&a, &c));
if (mp_cmp(&c, &b) != MP_EQ) {
printf("div_2 %lu failure\n", div2_n);
draw(&a);
@ -333,10 +333,10 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "mul2") == 0) {
++mul2_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(mp_read_radix(&a, buf, 64));
FGETS(buf, 4095, stdin);
mp_read_radix(&b, buf, 64);
mp_mul_2(&a, &c);
DO(mp_read_radix(&b, buf, 64));
DO(mp_mul_2(&a, &c));
if (mp_cmp(&c, &b) != MP_EQ) {
printf("mul_2 %lu failure\n", mul2_n);
draw(&a);
@ -347,12 +347,12 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "add_d") == 0) {
++add_d_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(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, (mp_digit)ix, &c);
DO(mp_read_radix(&b, buf, 64));
DO(mp_add_d(&a, (mp_digit)ix, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
printf("add_d %lu failure\n", add_d_n);
draw(&a);
@ -364,12 +364,12 @@ static int mtest_opponent(void)
} else if (strcmp(cmd, "sub_d") == 0) {
++sub_d_n;
FGETS(buf, 4095, stdin);
mp_read_radix(&a, buf, 64);
DO(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, (mp_digit)ix, &c);
DO(mp_read_radix(&b, buf, 64));
DO(mp_sub_d(&a, (mp_digit)ix, &c));
if (mp_cmp(&b, &c) != MP_EQ) {
printf("sub_d %lu failure\n", sub_d_n);
draw(&a);

View File

@ -4,8 +4,12 @@ void ndraw(const mp_int *a, const char *name)
{
char *buf;
size_t size;
mp_err err;
mp_radix_size(a, 10, &size);
if ((err = mp_radix_size(a, 10, &size)) != MP_OKAY) {
fprintf(stderr, "\nndraw: mp_radix_size(a, 10, %zu) failed - %s\n", size, mp_error_to_string(err));
exit(EXIT_FAILURE);
}
buf = (char *)malloc(size);
if (buf == NULL) {
fprintf(stderr, "\nndraw: malloc(%zu) failed\n", size);
@ -13,9 +17,15 @@ void ndraw(const mp_int *a, const char *name)
}
printf("%s: ", name);
mp_to_decimal(a, buf, size);
if ((err = mp_to_decimal(a, buf, size)) != MP_OKAY) {
fprintf(stderr, "\nndraw: mp_to_decimal(a, buf, %zu) failed - %s\n", size, mp_error_to_string(err));
exit(EXIT_FAILURE);
}
printf("%s\n", buf);
mp_to_hex(a, buf, size);
if ((err = mp_to_hex(a, buf, size)) != MP_OKAY) {
fprintf(stderr, "\nndraw: mp_to_hex(a, buf, %zu) failed - %s\n", size, mp_error_to_string(err));
exit(EXIT_FAILURE);
}
printf("0x%s\n", buf);
free(buf);

View File

@ -14,8 +14,14 @@
#define LTM_DEMO_TEST_REDUCE_2K_L 0
#endif
#define MP_WUR /* TODO: result checks disabled for now */
#include "tommath_private.h"
#define EXPECT(a) do { if (!(a)) { fprintf(stderr, "%d: EXPECT(%s) failed\n", __LINE__, #a); goto LBL_ERR; } } while(0)
#define DO_WHAT(a, what) do { mp_err err; if ((err = (a)) != MP_OKAY) { fprintf(stderr, "%d: DO(%s) failed: %s\n", __LINE__, #a, mp_error_to_string(err)); what; } } while(0)
#define DO(a) DO_WHAT(a, goto LBL_ERR)
#define DOR(a) DO_WHAT(a, return EXIT_FAILURE)
extern void ndraw(const mp_int* a, const char* name);
extern void print_header(void);

File diff suppressed because it is too large Load Diff

View File

@ -204,7 +204,7 @@ mp_err mp_init_size(mp_int *a, int size) MP_WUR;
/* ---> Basic Manipulations <--- */
#define mp_iszero(a) ((a)->used == 0)
#define mp_isneg(a) ((a)->sign != MP_ZPOS)
#define mp_isneg(a) ((a)->sign == MP_NEG)
#define mp_iseven(a) (((a)->used == 0) || (((a)->dp[0] & 1u) == 0u))
#define mp_isodd(a) (!mp_iseven(a))