test/mtest/stest: improve a bit

fix compiler warnings
make internal buffer size dependant on its possible size
fix return values
This commit is contained in:
Steffen Jaeckel 2014-08-14 21:11:31 +02:00
parent 0b1ad14d7b
commit 56438df4cf
5 changed files with 118 additions and 71 deletions

View File

@ -11,8 +11,29 @@
#define DISPLAY_P(...) printf(__VA_ARGS__)
#else
#define DISPLAY_P(...) (void)0
#define fp_dump(n,p) do{}while(0)
#endif
#ifndef fp_dump
void fp_dump(const char* n, fp_int* p)
{
int sz;
if (fp_radix_size(p, 2, &sz) != FP_OKAY)
return;
char* str = malloc(sz);
if (!str)
return;
#ifdef STEST_VERBOSE
fp_toradix(p, str, 2);
DISPLAY_P("%s = 0b%s\n", n, str);
fp_toradix(p, str, 16);
DISPLAY_P("%s = 0x%s\n", n, str);
#endif
fp_toradix(p, str, 10);
DISPLAY_P("%s = %s\n", n, str);
free(str);
}
#endif
#ifdef GBA_MODE
int c_main(void)
@ -45,7 +66,7 @@ int main(void)
fp_mul(&a, &b, &d);
if (fp_cmp(&c, &d)) {
DISPLAY("mul failed\n");
return 0;
return -1;
} else {
DISPLAY("mul passed\n");
}
@ -57,7 +78,7 @@ int main(void)
fp_mul(&a, &b, &d);
if (fp_cmp(&c, &d)) {
DISPLAY("mul failed\n");
return 0;
return -1;
} else {
DISPLAY("mul passed\n");
}
@ -69,7 +90,7 @@ int main(void)
fp_mul(&a, &b, &d);
if (fp_cmp(&c, &d)) {
DISPLAY("mul failed\n");
return 0;
return -1;
} else {
DISPLAY("mul passed\n");
}
@ -80,7 +101,7 @@ int main(void)
fp_sqr(&a, &c);
if (fp_cmp(&c, &b)) {
DISPLAY("sqr failed\n");
return 0;
return -1;
} else {
DISPLAY("sqr passed\n");
}
@ -90,7 +111,7 @@ int main(void)
fp_sqr(&a, &c);
if (fp_cmp(&c, &b)) {
DISPLAY("sqr failed\n");
return 0;
return -1;
} else {
DISPLAY("sqr passed\n");
}
@ -100,7 +121,7 @@ int main(void)
fp_sqr(&a, &c);
if (fp_cmp(&c, &b)) {
DISPLAY("sqr failed\n");
return 0;
return -1;
} else {
DISPLAY("sqr passed\n");
}
@ -109,12 +130,19 @@ int main(void)
/* montgomery reductions */
fp_read_radix(&a, "234892374892374893489123428937892781237863278637826327367637836278362783627836783678363", 10);
fp_read_radix(&b, "4447823492749823749234123489273987393983289319382762756425425425642727352327452374521", 10);
#ifdef FP_64BIT
fp_read_radix(&c, "942974496560863503657226741422301598807235487941674147660989764036913926327577165648", 10);
#else
fp_read_radix(&c, "2396271882990732698083317035605836523697277786556053771759862552557086442129695099100", 10);
fp_montgomery_setup(&b, &dp);
#endif
if (fp_montgomery_setup(&b, &dp) != FP_OKAY)
DISPLAY("mont setup failed\n");
fp_montgomery_reduce(&a, &b, dp);
if (fp_cmp(&a, &c)) {
DISPLAY("mont failed\n");
return 0;
fp_dump("a (is )", &a);
fp_dump("c (should)", &c);
return -1;
} else {
DISPLAY("mont passed\n");
}
@ -122,11 +150,14 @@ int main(void)
fp_read_radix(&a, "2348923748923748934891234456645654645645684576353428937892781237863278637826327367637836278362783627836783678363", 10);
fp_read_radix(&b, "444782349274982374923412348927398739398328931938276275642542542564272735232745237452123424324324444121111119", 10);
fp_read_radix(&c, "45642613844554582908652603086180267403823312390990082328515008314514368668691233331246183943400359349283420", 10);
fp_montgomery_setup(&b, &dp);
if (fp_montgomery_setup(&b, &dp) != FP_OKAY)
DISPLAY("mont setup failed\n");
fp_montgomery_reduce(&a, &b, dp);
if (fp_cmp(&a, &c)) {
DISPLAY("mont failed\n");
return 0;
fp_dump("a (is )", &a);
fp_dump("c (should)", &c);
return -1;
} else {
DISPLAY("mont passed\n");
}
@ -134,11 +165,14 @@ int main(void)
fp_read_radix(&a, "234823424242342923748923748934891234456645654645645684576353424972378234762378623891236834132352375235378462378489378927812378632786378263273676378362783627555555555539568389052478124618461834763837685723645827529034853490580134568947341278498542893481762349723907847892983627836783678363", 10);
fp_read_radix(&b, "44478234927456563455982374923412348927398739398328931938276275642485623481638279025465891276312903262837562349056234783648712314678120389173890128905425242424239784256427", 10);
fp_read_radix(&c, "33160865265453361650564031464519042126185632333462754084489985719613480783282357410514898819797738034600484519472656152351777186694609218202276509271061460265488348645081", 10);
fp_montgomery_setup(&b, &dp);
if (fp_montgomery_setup(&b, &dp) != FP_OKAY)
DISPLAY("mont setup failed\n");
fp_montgomery_reduce(&a, &b, dp);
if (fp_cmp(&a, &c)) {
DISPLAY("mont failed\n");
return 0;
fp_dump("a (is )", &a);
fp_dump("c (should)", &c);
return -1;
} else {
DISPLAY("mont passed\n");
}

View File

@ -8,7 +8,11 @@ void draw(fp_int *a)
int x;
printf("%d, %d, ", a->used, a->sign);
for (x = a->used - 1; x >= 0; x--) {
#if SIZEOF_FP_DIGIT == 4
printf("%08lx ", a->dp[x]);
#else
printf("%016llx ", a->dp[x]);
#endif
}
printf("\n");
}
@ -16,10 +20,12 @@ void draw(fp_int *a)
int myrng(unsigned char *dst, int len, void *dat)
{
int x;
(void)dat;
for (x = 0; x < len; x++) dst[x] = rand() & 0xFF;
return len;
}
#ifndef TESTING
/* RDTSC from Scott Duplichan */
static ulong64 TIMFUNC (void)
{
@ -68,22 +74,30 @@ static ulong64 TIMFUNC (void)
#error need rdtsc function for this build
#endif
}
#endif
char cmd[4096], buf[4096];
int main(void)
{
fp_int a,b,c,d,e,f;
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, mul_d_n, cnt, rr, ix;
#ifndef TESTING
unsigned long t;
fp_digit fp;
int n, err;
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, mul_d_n, t, cnt, rr, ix;
ulong64 t1, t2;
#endif
srand(time(NULL));
printf("TFM Ident string:\n%s\n\n", fp_ident());
fp_zero(&b); fp_zero(&c); fp_zero(&d); fp_zero(&e); fp_zero(&f);
fp_zero(&a); draw(&a);
fp_zero(&a);
#ifndef TESTING
draw(&a);
/* test set and simple shifts */
printf("Testing mul/div 2\n");
@ -217,17 +231,12 @@ int main(void)
}
printf("\n\n");
#ifdef TESTING
goto testing;
#endif
#if 1
t1 = TIMFUNC();
sleep(1);
printf("Ticks per second: %llu\n", TIMFUNC() - t1);
goto multtime;
/* do some timings... */
printf("Addition:\n");
for (t = 2; t <= FP_SIZE/2; t += 2) {
@ -252,7 +261,6 @@ goto multtime;
}
printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2);
}
multtime:
printf("Multiplication:\n");
for (t = 2; t < FP_SIZE/2; t += 2) {
fp_zero(&a);
@ -336,8 +344,7 @@ multtime:
}
printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2);
}
//#else
sqrtime:
printf("Squaring:\n");
for (t = 2; t < FP_SIZE/2; t += 2) {
fp_zero(&a);
@ -418,7 +425,7 @@ sqrtime:
}
printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2);
}
invmodtime:
printf("Invmod:\n");
for (t = 2; t < FP_SIZE/2; t += 2) {
fp_zero(&a);
@ -505,8 +512,7 @@ invmodtime:
}
printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2);
}
//#else
monttime:
printf("Montgomery:\n");
for (t = 2; t <= (FP_SIZE/2)-4; t += 2) {
// printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2);
@ -596,8 +602,7 @@ monttime:
}
printf("%5lu-bit: %9llu\n", t * DIGIT_BIT, t2);
}
//#else
expttime:
printf("Exptmod:\n");
for (t = 512/DIGIT_BIT; t <= (FP_SIZE/2)-2; t += 256/DIGIT_BIT) {
@ -629,7 +634,7 @@ expttime:
#endif
return 0;
testing:
#endif
fp_zero(&b); fp_zero(&c); fp_zero(&d); fp_zero(&e); fp_zero(&f); fp_zero(&a);
@ -650,7 +655,7 @@ testing:
fp_mul_2d(&a, rr, &a);
a.sign = b.sign;
if (fp_cmp(&a, &b) != FP_EQ) {
printf("mul2d failed, rr == %lu\n",rr);
printf("\nmul2d failed, rr == %lu\n",rr);
draw(&a);
draw(&b);
return 0;
@ -664,7 +669,7 @@ testing:
a.sign = b.sign;
if (a.used == b.used && a.used == 0) { a.sign = b.sign = FP_ZPOS; }
if (fp_cmp(&a, &b) != FP_EQ) {
printf("div2d failed, rr == %lu\n",rr);
printf("\ndiv2d failed, rr == %lu\n",rr);
draw(&a);
draw(&b);
return 0;
@ -676,7 +681,7 @@ testing:
fp_copy(&a, &d);
fp_add(&d, &b, &d);
if (fp_cmp(&c, &d) != FP_EQ) {
printf("add %lu failure!\n", add_n);
printf("\nadd %lu failure!\n", add_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
@ -688,7 +693,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
memset(cmd+rr, rand()&255, sizeof(cmd)-rr);
fp_read_signed_bin(&d, (unsigned char *)cmd, rr);
if (fp_cmp(&c, &d) != FP_EQ) {
printf("fp_signed_bin failure!\n");
printf("f\np_signed_bin failure!\n");
draw(&c);
draw(&d);
return 0;
@ -699,7 +704,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
memset(cmd+rr, rand()&255, sizeof(cmd)-rr);
fp_read_unsigned_bin(&d, (unsigned char *)cmd, rr);
if (fp_cmp_mag(&c, &d) != FP_EQ) {
printf("fp_unsigned_bin failure!\n");
printf("\nfp_unsigned_bin failure!\n");
draw(&c);
draw(&d);
return 0;
@ -712,98 +717,98 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fp_copy(&a, &d);
fp_sub(&d, &b, &d);
if (fp_cmp(&c, &d) != FP_EQ) {
printf("sub %lu failure!\n", sub_n);
printf("\nsub %lu failure!\n", sub_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
} else if (!strcmp(cmd, "mul")) {
} else if (!strcmp(cmd, "mul")) { ++mul_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&c, buf, 64);
//continue;
fp_copy(&a, &d);
fp_mul(&d, &b, &d); ++mul_n;
fp_mul(&d, &b, &d);
if (fp_cmp(&c, &d) != FP_EQ) {
printf("mul %lu failure!\n", mul_n);
printf("\nmul %lu failure!\n", mul_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
} else if (!strcmp(cmd, "div")) {
} else if (!strcmp(cmd, "div")) { ++div_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&c, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&d, buf, 64);
// continue;
fp_div(&a, &b, &e, &f); ++div_n;
fp_div(&a, &b, &e, &f);
if (fp_cmp(&c, &e) != FP_EQ || fp_cmp(&d, &f) != FP_EQ) {
printf("div %lu failure!\n", div_n);
printf("\ndiv %lu failure!\n", div_n);
draw(&a);draw(&b);draw(&c);draw(&d); draw(&e); draw(&f);
return 0;
}
} else if (!strcmp(cmd, "sqr")) {
} else if (!strcmp(cmd, "sqr")) { ++sqr_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
// continue;
fp_copy(&a, &c);
fp_sqr(&c, &c); ++sqr_n;
fp_sqr(&c, &c);
if (fp_cmp(&b, &c) != FP_EQ) {
printf("sqr %lu failure!\n", sqr_n);
printf("\nsqr %lu failure!\n", sqr_n);
draw(&a);draw(&b);draw(&c);
return 0;
}
} else if (!strcmp(cmd, "gcd")) {
} else if (!strcmp(cmd, "gcd")) { ++gcd_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&c, buf, 64);
// continue;
fp_copy(&a, &d);
fp_gcd(&d, &b, &d); ++gcd_n;
fp_gcd(&d, &b, &d);
d.sign = c.sign;
if (fp_cmp(&c, &d) != FP_EQ) {
printf("gcd %lu failure!\n", gcd_n);
printf("\ngcd %lu failure!\n", gcd_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
} else if (!strcmp(cmd, "lcm")) {
} else if (!strcmp(cmd, "lcm")) { ++lcm_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&c, buf, 64);
//continue;
fp_copy(&a, &d);
fp_lcm(&d, &b, &d); ++lcm_n;
fp_lcm(&d, &b, &d);
d.sign = c.sign;
if (fp_cmp(&c, &d) != FP_EQ) {
printf("lcm %lu failure!\n", lcm_n);
printf("\nlcm %lu failure!\n", lcm_n);
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
} else if (!strcmp(cmd, "expt")) {
} else if (!strcmp(cmd, "expt")) { ++expt_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&c, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&d, buf, 64);
// continue;
fp_copy(&a, &e);
fp_exptmod(&e, &b, &c, &e); ++expt_n;
fp_exptmod(&e, &b, &c, &e);
if (fp_cmp(&d, &e) != FP_EQ) {
printf("expt %lu failure!\n", expt_n);
printf("\nexpt %lu failure!\n", expt_n);
draw(&a);draw(&b);draw(&c);draw(&d); draw(&e);
return 0;
}
} else if (!strcmp(cmd, "invmod")) {
} else if (!strcmp(cmd, "invmod")) { ++inv_n;
fgets(buf, 4095, stdin); fp_read_radix(&a, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fgets(buf, 4095, stdin); fp_read_radix(&c, buf, 64);
//continue;
fp_invmod(&a, &b, &d);
#if 1
fp_mulmod(&d,&a,&b,&e); ++inv_n;
fp_mulmod(&d,&a,&b,&e);
if (fp_cmp_d(&e, 1) != FP_EQ) {
#else
if (fp_cmp(&d, &c) != FP_EQ) {
#endif
printf("inv [wrong value from MPI?!] failure\n");
printf("\ninv [wrong value from MPI?!] failure\n");
draw(&a);draw(&b);draw(&c);draw(&d);
return 0;
}
@ -813,7 +818,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fp_div_2(&a, &c);
if (fp_cmp(&c, &b) != FP_EQ) {
printf("div_2 %lu failure\n", div2_n);
printf("\ndiv_2 %lu failure\n", div2_n);
draw(&a);
draw(&b);
draw(&c);
@ -824,7 +829,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fp_mul_2(&a, &c);
if (fp_cmp(&c, &b) != FP_EQ) {
printf("mul_2 %lu failure\n", mul2_n);
printf("\nmul_2 %lu failure\n", mul2_n);
draw(&a);
draw(&b);
draw(&c);
@ -836,7 +841,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fp_add_d(&a, ix, &c);
if (fp_cmp(&b, &c) != FP_EQ) {
printf("add_d %lu failure\n", add_d_n);
printf("\nadd_d %lu failure\n", add_d_n);
draw(&a);
draw(&b);
draw(&c);
@ -849,7 +854,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fp_sub_d(&a, ix, &c);
if (fp_cmp(&b, &c) != FP_EQ) {
printf("sub_d %lu failure\n", sub_d_n);
printf("\nsub_d %lu failure\n", sub_d_n);
draw(&a);
draw(&b);
draw(&c);
@ -862,7 +867,7 @@ draw(&a);draw(&b);draw(&c);draw(&d);
fgets(buf, 4095, stdin); fp_read_radix(&b, buf, 64);
fp_mul_d(&a, ix, &c);
if (fp_cmp(&b, &c) != FP_EQ) {
printf("mul_d %lu failure\n", sub_d_n);
printf("\nmul_d %lu failure\n", mul_d_n);
draw(&a);
draw(&b);
draw(&c);

View File

@ -101,6 +101,8 @@ install: $(LIBNAME)
mtest: $(LIBNAME)
cd mtest; CC="$(CC)" CFLAGS="$(CFLAGS) -I../" MAKE=${MAKE} ${MAKE} mtest
demo/test.o: CFLAGS+=-Wno-unused-result
.PHONY: test
test: $(LIBNAME) demo/test.o
$(CC) $(CFLAGS) demo/test.o $(LIBNAME) $(PROF) -o test

View File

@ -1,9 +1,10 @@
CFLAGS += -Wall -W -O3
CFLAGS += -Wall -W -O3 -Wno-unused-result
default: mtest
.PHONY: mtest
mtest: mtest.o
$(CC) $(CFLAGS) mtest.o -ltommath -o mtest
clean:
rm -f *.o mtest *~
rm -f *.o mtest *~ mtest.exe

View File

@ -39,6 +39,7 @@ mulmod
#include <time.h>
#include <tommath.h>
#define CRYPT
#undef DIGIT_BIT
#include "../src/headers/tfm.h"
FILE *rng;
@ -46,8 +47,8 @@ FILE *rng;
/* 1-2048 bit numbers */
void rand_num(mp_int *a)
{
int n, size;
unsigned char buf[2048];
int size;
unsigned char buf[(FP_MAX_SIZE/16 - DIGIT_BIT/2) + 1];
size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % (FP_MAX_SIZE/16 - DIGIT_BIT/2);
buf[0] = (fgetc(rng)&1)?1:0;
@ -59,8 +60,8 @@ void rand_num(mp_int *a)
/* 1-256 bit numbers (to test things like exptmod) */
void rand_num2(mp_int *a)
{
int n, size;
unsigned char buf[2048];
int size;
unsigned char buf[(FP_MAX_SIZE/16 - DIGIT_BIT/2) + 1];
size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % (FP_MAX_SIZE/16 - DIGIT_BIT/2);
buf[0] = (fgetc(rng)&1)?1:0;
@ -69,13 +70,15 @@ void rand_num2(mp_int *a)
mp_read_raw(a, buf, 1+size);
}
#define mp_to64(a, b) mp_toradix(a, b, 64)
#define mp_to64(a, b) mp_toradix_n(a, b, 64, sizeof(b))
int main(void)
{
int n, tmp;
mp_int a, b, c, d, e;
#ifdef MTEST_NO_FULLSPEED
clock_t t1;
#endif
char buf[4096];
mp_init(&a);
@ -88,7 +91,7 @@ int main(void)
/* 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++) {
for (n = 1; n < ((FP_MAX_SIZE-(8*DIGIT_BIT))/2); n++) {
mp_mul(&a, &a, &c);
printf("mul\n");
mp_to64(&a, buf);
@ -111,9 +114,11 @@ int main(void)
}
}
#ifdef MTEST_NO_FULLSPEED
t1 = clock();
#endif
for (;;) {
#if 0
#ifdef MTEST_NO_FULLSPEED
if (clock() - t1 > CLOCKS_PER_SEC) {
sleep(2);
t1 = clock();