|
1 | 1 | #include "shared.h" |
| 2 | +#include "tommath_private.h" |
| 3 | + |
| 4 | +static long rand_long(void) |
| 5 | +{ |
| 6 | + long x; |
| 7 | + if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) { |
| 8 | + fprintf(stderr, "s_mp_rand_source failed\n"); |
| 9 | + exit(EXIT_FAILURE); |
| 10 | + } |
| 11 | + return x; |
| 12 | +} |
| 13 | + |
| 14 | +static int rand_int(void) |
| 15 | +{ |
| 16 | + int x; |
| 17 | + if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) { |
| 18 | + fprintf(stderr, "s_mp_rand_source failed\n"); |
| 19 | + exit(EXIT_FAILURE); |
| 20 | + } |
| 21 | + return x; |
| 22 | +} |
2 | 23 |
|
3 | 24 | static int test_trivial_stuff(void) |
4 | 25 | { |
@@ -260,7 +281,7 @@ static int test_mp_complement(void) |
260 | 281 | } |
261 | 282 |
|
262 | 283 | for (i = 0; i < 1000; ++i) { |
263 | | - long l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 284 | + long l = rand_long(); |
264 | 285 | mp_set_long(&a, (unsigned long)labs(l)); |
265 | 286 | if (l < 0) |
266 | 287 | mp_neg(&a, &a); |
@@ -297,12 +318,12 @@ static int test_mp_tc_div_2d(void) |
297 | 318 | long l; |
298 | 319 | int em; |
299 | 320 |
|
300 | | - l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 321 | + l = rand_long(); |
301 | 322 | mp_set_long(&a, (unsigned long)labs(l)); |
302 | 323 | if (l < 0) |
303 | 324 | mp_neg(&a, &a); |
304 | 325 |
|
305 | | - em = rand() % 32; |
| 326 | + em = abs(rand_int()) % 32; |
306 | 327 |
|
307 | 328 | mp_set_long(&d, (unsigned long)labs(l >> em)); |
308 | 329 | if ((l >> em) < 0) |
@@ -333,14 +354,14 @@ static int test_mp_tc_xor(void) |
333 | 354 | } |
334 | 355 |
|
335 | 356 | for (i = 0; i < 1000; ++i) { |
336 | | - int l, em; |
| 357 | + long l, em; |
337 | 358 |
|
338 | | - l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 359 | + l = rand_long(); |
339 | 360 | mp_set_int(&a, (unsigned long)labs(l)); |
340 | 361 | if (l < 0) |
341 | 362 | mp_neg(&a, &a); |
342 | 363 |
|
343 | | - em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 364 | + em = rand_long(); |
344 | 365 | mp_set_int(&b, (unsigned long)labs(em)); |
345 | 366 | if (em < 0) |
346 | 367 | mp_neg(&b, &b); |
@@ -376,12 +397,12 @@ static int test_mp_tc_or(void) |
376 | 397 | for (i = 0; i < 1000; ++i) { |
377 | 398 | long l, em; |
378 | 399 |
|
379 | | - l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 400 | + l = rand_long(); |
380 | 401 | mp_set_long(&a, (unsigned long)labs(l)); |
381 | 402 | if (l < 0) |
382 | 403 | mp_neg(&a, &a); |
383 | 404 |
|
384 | | - em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 405 | + em = rand_long(); |
385 | 406 | mp_set_long(&b, (unsigned long)labs(em)); |
386 | 407 | if (em < 0) |
387 | 408 | mp_neg(&b, &b); |
@@ -416,12 +437,12 @@ static int test_mp_tc_and(void) |
416 | 437 | for (i = 0; i < 1000; ++i) { |
417 | 438 | long l, em; |
418 | 439 |
|
419 | | - l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 440 | + l = rand_long(); |
420 | 441 | mp_set_long(&a, (unsigned long)labs(l)); |
421 | 442 | if (l < 0) |
422 | 443 | mp_neg(&a, &a); |
423 | 444 |
|
424 | | - em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1); |
| 445 | + em = rand_long(); |
425 | 446 | mp_set_long(&b, (unsigned long)labs(em)); |
426 | 447 | if (em < 0) |
427 | 448 | mp_neg(&b, &b); |
@@ -518,8 +539,8 @@ static int test_mp_set_double(void) |
518 | 539 | } |
519 | 540 |
|
520 | 541 | for (i = 0; i < 1000; ++i) { |
521 | | - int tmp = rand(); |
522 | | - double dbl = (double)tmp * rand() + 1; |
| 542 | + int tmp = rand_int(); |
| 543 | + double dbl = (double)tmp * rand_int() + 1; |
523 | 544 | if (mp_set_double(&a, dbl) != MP_OKAY) { |
524 | 545 | printf("\nmp_set_double() failed"); |
525 | 546 | goto LBL_ERR; |
@@ -558,7 +579,7 @@ static int test_mp_get_int(void) |
558 | 579 | } |
559 | 580 |
|
560 | 581 | for (i = 0; i < 1000; ++i) { |
561 | | - t = ((unsigned long)rand() * (unsigned long)rand() + 1uL) & 0xFFFFFFFFuL; |
| 582 | + t = (unsigned long)rand_long() & 0xFFFFFFFFuL; |
562 | 583 | mp_set_int(&a, t); |
563 | 584 | if (t != mp_get_int(&a)) { |
564 | 585 | printf("\nmp_get_int() bad result!"); |
@@ -662,7 +683,7 @@ static int test_mp_sqrt(void) |
662 | 683 | for (i = 0; i < 1000; ++i) { |
663 | 684 | printf("%6d\r", i); |
664 | 685 | fflush(stdout); |
665 | | - n = (rand() & 15) + 1; |
| 686 | + n = (rand_int() & 15) + 1; |
666 | 687 | mp_rand(&a, n); |
667 | 688 | if (mp_sqrt(&a, &b) != MP_OKAY) { |
668 | 689 | printf("\nmp_sqrt() error!"); |
@@ -701,7 +722,7 @@ static int test_mp_is_square(void) |
701 | 722 | fflush(stdout); |
702 | 723 |
|
703 | 724 | /* test mp_is_square false negatives */ |
704 | | - n = (rand() & 7) + 1; |
| 725 | + n = (rand_int() & 7) + 1; |
705 | 726 | mp_rand(&a, n); |
706 | 727 | mp_sqr(&a, &a); |
707 | 728 | if (mp_is_square(&a, &n) != MP_OKAY) { |
@@ -789,7 +810,7 @@ static int test_mp_prime_rand(void) |
789 | 810 | for (ix = 10; ix < 128; ix++) { |
790 | 811 | printf("Testing (not safe-prime): %9d bits \r", ix); |
791 | 812 | fflush(stdout); |
792 | | - err = mp_prime_rand(&a, 8, ix, (rand() & 1) ? 0 : MP_PRIME_2MSB_ON); |
| 813 | + err = mp_prime_rand(&a, 8, ix, (rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON); |
793 | 814 | if (err != MP_OKAY) { |
794 | 815 | printf("\nfailed with error: %s\n", mp_error_to_string(err)); |
795 | 816 | goto LBL_ERR; |
@@ -850,7 +871,7 @@ static int test_mp_prime_is_prime(void) |
850 | 871 | for (ix = 16; ix < 128; ix++) { |
851 | 872 | printf("Testing ( safe-prime): %9d bits \r", ix); |
852 | 873 | fflush(stdout); |
853 | | - err = mp_prime_rand(&a, 8, ix, ((rand() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE); |
| 874 | + err = mp_prime_rand(&a, 8, ix, ((rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE); |
854 | 875 | if (err != MP_OKAY) { |
855 | 876 | printf("\nfailed with error: %s\n", mp_error_to_string(err)); |
856 | 877 | goto LBL_ERR; |
@@ -941,7 +962,7 @@ static int test_mp_montgomery_reduce(void) |
941 | 962 |
|
942 | 963 | /* now test a random reduction */ |
943 | 964 | for (ix = 0; ix < 100; ix++) { |
944 | | - mp_rand(&c, 1 + abs(rand()) % (2*i)); |
| 965 | + mp_rand(&c, 1 + abs(rand_int()) % (2*i)); |
945 | 966 | mp_copy(&c, &d); |
946 | 967 | mp_copy(&c, &e); |
947 | 968 |
|
@@ -1096,7 +1117,7 @@ static int test_mp_div_3(void) |
1096 | 1117 | printf("%9d\r", cnt); |
1097 | 1118 | fflush(stdout); |
1098 | 1119 | } |
1099 | | - mp_rand(&a, abs(rand()) % 128 + 1); |
| 1120 | + mp_rand(&a, abs(rand_int()) % 128 + 1); |
1100 | 1121 | mp_div(&a, &d, &b, &e); |
1101 | 1122 | mp_div_3(&a, &c, &r2); |
1102 | 1123 |
|
@@ -1853,6 +1874,9 @@ int unit_tests(int argc, char **argv) |
1853 | 1874 | unsigned long i; |
1854 | 1875 | int res = EXIT_SUCCESS, j; |
1855 | 1876 |
|
| 1877 | + s_mp_rand_jenkins_init((uint64_t)time(NULL)); |
| 1878 | + mp_rand_source(s_mp_rand_jenkins); |
| 1879 | + |
1856 | 1880 | for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) { |
1857 | 1881 | if (argc > 1) { |
1858 | 1882 | for (j = 1; j < argc; ++j) { |
|
0 commit comments