Mercurial > dropbear
diff libtommath/mtest/mtest.c @ 1439:8d24733026c5 coverity
merge
author | Matt Johnston <matt@ucc.asn.au> |
---|---|
date | Sat, 24 Jun 2017 23:33:16 +0800 |
parents | 60fc6476e044 |
children | 8bba51a55704 |
line wrap: on
line diff
--- a/libtommath/mtest/mtest.c Sat Jun 03 00:10:58 2017 +0800 +++ b/libtommath/mtest/mtest.c Sat Jun 24 23:33:16 2017 +0800 @@ -39,39 +39,71 @@ #include <time.h> #include "mpi.c" +#ifdef LTM_MTEST_REAL_RAND +#define getRandChar() fgetc(rng) FILE *rng; +#else +#define getRandChar() (rand()&0xFF) +#endif void rand_num(mp_int *a) { - int n, size; + int size; unsigned char buf[2048]; + size_t sz; - size = 1 + ((fgetc(rng)<<8) + fgetc(rng)) % 101; - buf[0] = (fgetc(rng)&1)?1:0; - fread(buf+1, 1, size, rng); - while (buf[1] == 0) buf[1] = fgetc(rng); + size = 1 + ((getRandChar()<<8) + getRandChar()) % 101; + buf[0] = (getRandChar()&1)?1:0; +#ifdef LTM_MTEST_REAL_RAND + sz = fread(buf+1, 1, size, rng); +#else + sz = 1; + while (sz < (unsigned)size) { + buf[sz] = getRandChar(); + ++sz; + } +#endif + if (sz != (unsigned)size) { + fprintf(stderr, "\nWarning: fread failed\n\n"); + } + while (buf[1] == 0) buf[1] = getRandChar(); mp_read_raw(a, buf, 1+size); } void rand_num2(mp_int *a) { - int n, size; + int size; unsigned char buf[2048]; + size_t sz; - size = 10 + ((fgetc(rng)<<8) + fgetc(rng)) % 101; - buf[0] = (fgetc(rng)&1)?1:0; - fread(buf+1, 1, size, rng); - while (buf[1] == 0) buf[1] = fgetc(rng); + size = 10 + ((getRandChar()<<8) + getRandChar()) % 101; + buf[0] = (getRandChar()&1)?1:0; +#ifdef LTM_MTEST_REAL_RAND + sz = fread(buf+1, 1, size, rng); +#else + sz = 1; + while (sz < (unsigned)size) { + buf[sz] = getRandChar(); + ++sz; + } +#endif + if (sz != (unsigned)size) { + fprintf(stderr, "\nWarning: fread failed\n\n"); + } + while (buf[1] == 0) buf[1] = getRandChar(); mp_read_raw(a, buf, 1+size); } #define mp_to64(a, b) mp_toradix(a, b, 64) -int main(void) +int main(int argc, char *argv[]) { int n, tmp; + long long max; mp_int a, b, c, d, e; +#ifdef MTEST_NO_FULLSPEED clock_t t1; +#endif char buf[4096]; mp_init(&a); @@ -80,6 +112,22 @@ mp_init(&d); mp_init(&e); + if (argc > 1) { + max = strtol(argv[1], NULL, 0); + if (max < 0) { + if (max > -64) { + max = (1 << -(max)) + 1; + } else { + max = 1; + } + } else if (max == 0) { + max = 1; + } + } + else { + max = 0; + } + /* initial (2^n - 1)^2 testing, makes sure the comba multiplier works [it has the new carry code] */ /* @@ -98,6 +146,7 @@ } */ +#ifdef LTM_MTEST_REAL_RAND rng = fopen("/dev/urandom", "rb"); if (rng == NULL) { rng = fopen("/dev/random", "rb"); @@ -106,16 +155,27 @@ rng = stdin; } } +#else + srand(23); +#endif +#ifdef MTEST_NO_FULLSPEED t1 = clock(); +#endif for (;;) { -#if 0 +#ifdef MTEST_NO_FULLSPEED if (clock() - t1 > CLOCKS_PER_SEC) { sleep(2); t1 = clock(); } #endif - n = fgetc(rng) % 15; + n = getRandChar() % 15; + + if (max != 0) { + --max; + if (max == 0) + n = 255; + } if (n == 0) { /* add tests */ @@ -180,7 +240,7 @@ /* mul_2d test */ rand_num(&a); mp_copy(&a, &b); - n = fgetc(rng) & 63; + n = getRandChar() & 63; mp_mul_2d(&b, n, &b); mp_to64(&a, buf); printf("mul2d\n"); @@ -192,7 +252,7 @@ /* div_2d test */ rand_num(&a); mp_copy(&a, &b); - n = fgetc(rng) & 63; + n = getRandChar() & 63; mp_div_2d(&b, n, &b, NULL); mp_to64(&a, buf); printf("div2d\n"); @@ -297,12 +357,18 @@ printf("%s\n%d\n", buf, tmp); mp_to64(&b, buf); printf("%s\n", buf); - } + } else if (n == 255) { + printf("exit\n"); + break; } + + } +#ifdef LTM_MTEST_REAL_RAND fclose(rng); +#endif return 0; } -/* $Source: /cvs/libtom/libtommath/mtest/mtest.c,v $ */ -/* $Revision: 1.2 $ */ -/* $Date: 2005/05/05 14:38:47 $ */ +/* $Source$ */ +/* $Revision$ */ +/* $Date$ */