Skip to content

Commit 2a2e271

Browse files
minadsjaeckel
authored andcommitted
move jenkins prng to bn_s_mp_rand_jenkins.c
1 parent c8cc365 commit 2a2e271

18 files changed

+155
-112
lines changed

.travis.yml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -153,13 +153,13 @@ matrix:
153153

154154
# GCC for the x86-64 architecture testing against a different Bigint-implementation
155155
# with 333333 different inputs.
156-
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
157-
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
156+
- env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
157+
- env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
158158

159159
# clang for the x86-64 architecture testing against a different Bigint-implementation
160160
# with a better random source.
161-
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
162-
- env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
161+
- env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
162+
- env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
163163

164164

165165
# Notifications go to

bn_mp_rand.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

6-
int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_source_platform;
6+
int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_platform;
77

88
void mp_rand_source(int (*source)(void *out, size_t size))
99
{
10-
s_mp_rand_source = (source == NULL) ? s_mp_rand_source_platform : source;
10+
s_mp_rand_source = (source == NULL) ? s_mp_rand_platform : source;
1111
}
1212

1313
/* makes a pseudo-random int of a given size */

bn_s_mp_rand_jenkins.c

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
#include "tommath_private.h"
2+
#ifdef BN_S_MP_RAND_JENKINS_C
3+
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
4+
/* SPDX-License-Identifier: Unlicense */
5+
6+
/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
7+
/* Chosen for speed and a good "mix" */
8+
typedef struct ranctx {
9+
uint64_t a;
10+
uint64_t b;
11+
uint64_t c;
12+
uint64_t d;
13+
} ranctx;
14+
15+
static ranctx jenkins_x;
16+
17+
#define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
18+
static uint64_t s_rand_jenkins_val(void)
19+
{
20+
uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7);
21+
jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13);
22+
jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37);
23+
jenkins_x.c = jenkins_x.d + e;
24+
jenkins_x.d = e + jenkins_x.a;
25+
return jenkins_x.d;
26+
}
27+
28+
void s_mp_rand_jenkins_init(uint64_t seed)
29+
{
30+
uint64_t i;
31+
jenkins_x.a = 0xf1ea5eed;
32+
jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
33+
for (i = 0; i < 20; ++i) {
34+
(void)s_rand_jenkins_val();
35+
}
36+
}
37+
38+
int s_mp_rand_jenkins(void *p, size_t n)
39+
{
40+
char *q = (char *)p;
41+
while (n > 0) {
42+
int i;
43+
uint64_t x = s_rand_jenkins_val();
44+
for (i = 0; i < 8 && n > 0; ++i, --n) {
45+
*q++ = (char)(x & 0xFF);
46+
x >>= 8;
47+
}
48+
}
49+
return MP_OKAY;
50+
}
51+
52+
#endif
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include "tommath_private.h"
2-
#ifdef BN_S_MP_RAND_SOURCE_PLATFORM_C
2+
#ifdef BN_S_MP_RAND_PLATFORM_C
33
/* LibTomMath, multiple-precision integer library -- Tom St Denis */
44
/* SPDX-License-Identifier: Unlicense */
55

@@ -128,7 +128,7 @@ static int s_read_ltm_rng(void *p, size_t n)
128128
}
129129
#endif
130130

131-
int s_mp_rand_source_platform(void *p, size_t n)
131+
int s_mp_rand_platform(void *p, size_t n)
132132
{
133133
#if defined(MP_ARC4RANDOM)
134134
arc4random_buf(p, n);

callgraph.txt

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15360,7 +15360,10 @@ BN_S_MP_MUL_HIGH_DIGS_FAST_C
1536015360
+--->BN_MP_CLAMP_C
1536115361

1536215362

15363-
BN_S_MP_RAND_SOURCE_PLATFORM_C
15363+
BN_S_MP_RAND_JENKINS_C
15364+
15365+
15366+
BN_S_MP_RAND_PLATFORM_C
1536415367

1536515368

1536615369
BN_S_MP_REVERSE_C

demo/main.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@ void ndraw(mp_int *a, const char *name)
2626

2727
int main(int argc, char **argv)
2828
{
29-
srand(LTM_DEMO_RAND_SEED);
30-
3129
#ifdef MP_8BIT
3230
printf("Digit size 8 Bit \n");
3331
#endif

demo/opponent.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
#include "shared.h"
22

3+
#ifdef LTM_MTEST_REAL_RAND
4+
#define LTM_MTEST_RAND_SEED time(NULL)
5+
#else
6+
#define LTM_MTEST_RAND_SEED 23
7+
#endif
8+
39
static void draw(mp_int *a)
410
{
511
ndraw(a, "");
@@ -21,6 +27,8 @@ int mtest_opponent(void)
2127
unsigned long expt_n, add_n, sub_n, mul_n, div_n, sqr_n, mul2d_n, div2d_n,
2228
gcd_n, lcm_n, inv_n, div2_n, mul2_n, add_d_n, sub_d_n;
2329

30+
srand(LTM_MTEST_RAND_SEED);
31+
2432
if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY)
2533
return EXIT_FAILURE;
2634

demo/shared.h

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,6 @@
1818
#define LTM_DEMO_TEST_REDUCE_2K_L 0
1919
#endif
2020

21-
#ifdef LTM_DEMO_REAL_RAND
22-
#define LTM_DEMO_RAND_SEED time(NULL)
23-
#else
24-
#define LTM_DEMO_RAND_SEED 23
25-
#endif
26-
2721
#define MP_WUR /* TODO: result checks disabled for now */
2822
#include "tommath.h"
2923

demo/test.c

Lines changed: 43 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,25 @@
11
#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+
}
223

324
static int test_trivial_stuff(void)
425
{
@@ -260,7 +281,7 @@ static int test_mp_complement(void)
260281
}
261282

262283
for (i = 0; i < 1000; ++i) {
263-
long l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
284+
long l = rand_long();
264285
mp_set_long(&a, (unsigned long)labs(l));
265286
if (l < 0)
266287
mp_neg(&a, &a);
@@ -297,12 +318,12 @@ static int test_mp_tc_div_2d(void)
297318
long l;
298319
int em;
299320

300-
l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
321+
l = rand_long();
301322
mp_set_long(&a, (unsigned long)labs(l));
302323
if (l < 0)
303324
mp_neg(&a, &a);
304325

305-
em = rand() % 32;
326+
em = abs(rand_int()) % 32;
306327

307328
mp_set_long(&d, (unsigned long)labs(l >> em));
308329
if ((l >> em) < 0)
@@ -333,14 +354,14 @@ static int test_mp_tc_xor(void)
333354
}
334355

335356
for (i = 0; i < 1000; ++i) {
336-
int l, em;
357+
long l, em;
337358

338-
l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
359+
l = rand_long();
339360
mp_set_int(&a, (unsigned long)labs(l));
340361
if (l < 0)
341362
mp_neg(&a, &a);
342363

343-
em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
364+
em = rand_long();
344365
mp_set_int(&b, (unsigned long)labs(em));
345366
if (em < 0)
346367
mp_neg(&b, &b);
@@ -376,12 +397,12 @@ static int test_mp_tc_or(void)
376397
for (i = 0; i < 1000; ++i) {
377398
long l, em;
378399

379-
l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
400+
l = rand_long();
380401
mp_set_long(&a, (unsigned long)labs(l));
381402
if (l < 0)
382403
mp_neg(&a, &a);
383404

384-
em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
405+
em = rand_long();
385406
mp_set_long(&b, (unsigned long)labs(em));
386407
if (em < 0)
387408
mp_neg(&b, &b);
@@ -416,12 +437,12 @@ static int test_mp_tc_and(void)
416437
for (i = 0; i < 1000; ++i) {
417438
long l, em;
418439

419-
l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
440+
l = rand_long();
420441
mp_set_long(&a, (unsigned long)labs(l));
421442
if (l < 0)
422443
mp_neg(&a, &a);
423444

424-
em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
445+
em = rand_long();
425446
mp_set_long(&b, (unsigned long)labs(em));
426447
if (em < 0)
427448
mp_neg(&b, &b);
@@ -518,8 +539,8 @@ static int test_mp_set_double(void)
518539
}
519540

520541
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;
523544
if (mp_set_double(&a, dbl) != MP_OKAY) {
524545
printf("\nmp_set_double() failed");
525546
goto LBL_ERR;
@@ -558,7 +579,7 @@ static int test_mp_get_int(void)
558579
}
559580

560581
for (i = 0; i < 1000; ++i) {
561-
t = ((unsigned long)rand() * (unsigned long)rand() + 1uL) & 0xFFFFFFFFuL;
582+
t = (unsigned long)rand_long() & 0xFFFFFFFFuL;
562583
mp_set_int(&a, t);
563584
if (t != mp_get_int(&a)) {
564585
printf("\nmp_get_int() bad result!");
@@ -662,7 +683,7 @@ static int test_mp_sqrt(void)
662683
for (i = 0; i < 1000; ++i) {
663684
printf("%6d\r", i);
664685
fflush(stdout);
665-
n = (rand() & 15) + 1;
686+
n = (rand_int() & 15) + 1;
666687
mp_rand(&a, n);
667688
if (mp_sqrt(&a, &b) != MP_OKAY) {
668689
printf("\nmp_sqrt() error!");
@@ -701,7 +722,7 @@ static int test_mp_is_square(void)
701722
fflush(stdout);
702723

703724
/* test mp_is_square false negatives */
704-
n = (rand() & 7) + 1;
725+
n = (rand_int() & 7) + 1;
705726
mp_rand(&a, n);
706727
mp_sqr(&a, &a);
707728
if (mp_is_square(&a, &n) != MP_OKAY) {
@@ -789,7 +810,7 @@ static int test_mp_prime_rand(void)
789810
for (ix = 10; ix < 128; ix++) {
790811
printf("Testing (not safe-prime): %9d bits \r", ix);
791812
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);
793814
if (err != MP_OKAY) {
794815
printf("\nfailed with error: %s\n", mp_error_to_string(err));
795816
goto LBL_ERR;
@@ -850,7 +871,7 @@ static int test_mp_prime_is_prime(void)
850871
for (ix = 16; ix < 128; ix++) {
851872
printf("Testing ( safe-prime): %9d bits \r", ix);
852873
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);
854875
if (err != MP_OKAY) {
855876
printf("\nfailed with error: %s\n", mp_error_to_string(err));
856877
goto LBL_ERR;
@@ -941,7 +962,7 @@ static int test_mp_montgomery_reduce(void)
941962

942963
/* now test a random reduction */
943964
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));
945966
mp_copy(&c, &d);
946967
mp_copy(&c, &e);
947968

@@ -1096,7 +1117,7 @@ static int test_mp_div_3(void)
10961117
printf("%9d\r", cnt);
10971118
fflush(stdout);
10981119
}
1099-
mp_rand(&a, abs(rand()) % 128 + 1);
1120+
mp_rand(&a, abs(rand_int()) % 128 + 1);
11001121
mp_div(&a, &d, &b, &e);
11011122
mp_div_3(&a, &c, &r2);
11021123

@@ -1853,6 +1874,9 @@ int unit_tests(int argc, char **argv)
18531874
unsigned long i;
18541875
int res = EXIT_SUCCESS, j;
18551876

1877+
s_mp_rand_jenkins_init((uint64_t)time(NULL));
1878+
mp_rand_source(s_mp_rand_jenkins);
1879+
18561880
for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) {
18571881
if (argc > 1) {
18581882
for (j = 1; j < argc; ++j) {

0 commit comments

Comments
 (0)