Commit 2a2e2716c28ffc9103759eb84d7646f52300ae84

Daniel Mendler 2019-05-12T11:33:17

move jenkins prng to bn_s_mp_rand_jenkins.c

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
diff --git a/.travis.yml b/.travis.yml
index d1ad089..a6a097d 100644
--- a/.travis.yml
+++ b/.travis.yml
@@ -153,13 +153,13 @@ matrix:
 
     # GCC for the x86-64 architecture testing against a different Bigint-implementation
     # with 333333 different inputs.
-    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
-    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
+    - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --with-valgrind'
+    - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --with-valgrind'
 
     # clang for the x86-64 architecture testing against a different Bigint-implementation
     # with a better random source.
-    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
-    - env: SANITIZER=1 BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
+    - env: BUILDOPTIONS='--with-cc=gcc-5 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
+    - env: BUILDOPTIONS='--with-cc=clang-7 --test-vs-mtest=333333 --mtest-real-rand --with-valgrind'
 
 
 # Notifications go to
diff --git a/bn_mp_rand.c b/bn_mp_rand.c
index 29bca68..db0ff48 100644
--- a/bn_mp_rand.c
+++ b/bn_mp_rand.c
@@ -3,11 +3,11 @@
 /* LibTomMath, multiple-precision integer library -- Tom St Denis */
 /* SPDX-License-Identifier: Unlicense */
 
-int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_source_platform;
+int (*s_mp_rand_source)(void *, size_t) = s_mp_rand_platform;
 
 void mp_rand_source(int (*source)(void *out, size_t size))
 {
-   s_mp_rand_source = (source == NULL) ? s_mp_rand_source_platform : source;
+   s_mp_rand_source = (source == NULL) ? s_mp_rand_platform : source;
 }
 
 /* makes a pseudo-random int of a given size */
diff --git a/bn_s_mp_rand_jenkins.c b/bn_s_mp_rand_jenkins.c
new file mode 100644
index 0000000..d6946f8
--- /dev/null
+++ b/bn_s_mp_rand_jenkins.c
@@ -0,0 +1,52 @@
+#include "tommath_private.h"
+#ifdef BN_S_MP_RAND_JENKINS_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
+/* Chosen for speed and a good "mix" */
+typedef struct ranctx {
+   uint64_t a;
+   uint64_t b;
+   uint64_t c;
+   uint64_t d;
+} ranctx;
+
+static ranctx jenkins_x;
+
+#define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
+static uint64_t s_rand_jenkins_val(void)
+{
+   uint64_t e = jenkins_x.a - rot(jenkins_x.b, 7);
+   jenkins_x.a = jenkins_x.b ^ rot(jenkins_x.c, 13);
+   jenkins_x.b = jenkins_x.c + rot(jenkins_x.d, 37);
+   jenkins_x.c = jenkins_x.d + e;
+   jenkins_x.d = e + jenkins_x.a;
+   return jenkins_x.d;
+}
+
+void s_mp_rand_jenkins_init(uint64_t seed)
+{
+   uint64_t i;
+   jenkins_x.a = 0xf1ea5eed;
+   jenkins_x.b = jenkins_x.c = jenkins_x.d = seed;
+   for (i = 0; i < 20; ++i) {
+      (void)s_rand_jenkins_val();
+   }
+}
+
+int s_mp_rand_jenkins(void *p, size_t n)
+{
+   char *q = (char *)p;
+   while (n > 0) {
+      int i;
+      uint64_t x = s_rand_jenkins_val();
+      for (i = 0; i < 8 && n > 0; ++i, --n) {
+         *q++ = (char)(x & 0xFF);
+         x >>= 8;
+      }
+   }
+   return MP_OKAY;
+}
+
+#endif
diff --git a/bn_s_mp_rand_platform.c b/bn_s_mp_rand_platform.c
new file mode 100644
index 0000000..ae0b968
--- /dev/null
+++ b/bn_s_mp_rand_platform.c
@@ -0,0 +1,164 @@
+#include "tommath_private.h"
+#ifdef BN_S_MP_RAND_PLATFORM_C
+/* LibTomMath, multiple-precision integer library -- Tom St Denis */
+/* SPDX-License-Identifier: Unlicense */
+
+/* First the OS-specific special cases
+ * - *BSD
+ * - Windows
+ */
+#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
+#  define MP_ARC4RANDOM
+#endif
+
+#if defined(_WIN32) || defined(_WIN32_WCE)
+#define MP_WIN_CSP
+
+#ifndef _WIN32_WINNT
+#define _WIN32_WINNT 0x0400
+#endif
+#ifdef _WIN32_WCE
+#define UNDER_CE
+#define ARM
+#endif
+
+#define WIN32_LEAN_AND_MEAN
+#include <windows.h>
+#include <wincrypt.h>
+
+static HCRYPTPROV hProv = 0;
+
+static void s_cleanup_win_csp(void)
+{
+   CryptReleaseContext(hProv, 0);
+   hProv = 0;
+}
+
+static int s_read_win_csp(void *p, size_t n)
+{
+   if (hProv == 0) {
+      if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
+                               (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
+          !CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
+                               CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
+         hProv = 0;
+         return MP_ERR;
+      }
+      atexit(s_cleanup_win_csp);
+   }
+   return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
+}
+#endif /* WIN32 */
+
+#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
+#if __GLIBC_PREREQ(2, 25)
+#define MP_GETRANDOM
+#include <sys/random.h>
+#include <errno.h>
+
+static int s_read_getrandom(void *p, size_t n)
+{
+   char *q = (char *)p;
+   while (n > 0) {
+      ssize_t ret = getrandom(q, n, 0);
+      if (ret < 0) {
+         if (errno == EINTR) {
+            continue;
+         }
+         return MP_ERR;
+      }
+      q += ret;
+      n -= (size_t)ret;
+   }
+   return MP_OKAY;
+}
+#endif
+#endif
+
+/* We assume all platforms besides windows provide "/dev/urandom".
+ * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
+ */
+#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
+#ifndef MP_DEV_URANDOM
+#define MP_DEV_URANDOM "/dev/urandom"
+#endif
+#include <fcntl.h>
+#include <errno.h>
+#include <unistd.h>
+
+static int s_read_dev_urandom(void *p, size_t n)
+{
+   int fd;
+   char *q = (char *)p;
+
+   do {
+      fd = open(MP_DEV_URANDOM, O_RDONLY);
+   } while ((fd == -1) && (errno == EINTR));
+   if (fd == -1) return MP_ERR;
+
+   while (n > 0) {
+      ssize_t ret = read(fd, p, n);
+      if (ret < 0) {
+         if (errno == EINTR) {
+            continue;
+         }
+         close(fd);
+         return MP_ERR;
+      }
+      q += ret;
+      n -= (size_t)ret;
+   }
+
+   close(fd);
+   return MP_OKAY;
+}
+#endif
+
+#if defined(MP_PRNG_ENABLE_LTM_RNG)
+unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
+void (*ltm_rng_callback)(void);
+
+static int s_read_ltm_rng(void *p, size_t n)
+{
+   unsigned long ret;
+   if (ltm_rng == NULL) return MP_ERR;
+   ret = ltm_rng(p, n, ltm_rng_callback);
+   if (ret != n) return MP_ERR;
+   return MP_OKAY;
+}
+#endif
+
+int s_mp_rand_platform(void *p, size_t n)
+{
+#if defined(MP_ARC4RANDOM)
+   arc4random_buf(p, n);
+   return MP_OKAY;
+#else
+
+   int ret = MP_ERR;
+
+#if defined(MP_WIN_CSP)
+   ret = s_read_win_csp(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+#if defined(MP_GETRANDOM)
+   ret = s_read_getrandom(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+#if defined(MP_DEV_URANDOM)
+   ret = s_read_dev_urandom(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+#if defined(MP_PRNG_ENABLE_LTM_RNG)
+   ret = s_read_ltm_rng(p, n);
+   if (ret == MP_OKAY) return ret;
+#endif
+
+   return ret;
+#endif
+}
+
+#endif
diff --git a/bn_s_mp_rand_source_platform.c b/bn_s_mp_rand_source_platform.c
deleted file mode 100644
index 7121f14..0000000
--- a/bn_s_mp_rand_source_platform.c
+++ /dev/null
@@ -1,164 +0,0 @@
-#include "tommath_private.h"
-#ifdef BN_S_MP_RAND_SOURCE_PLATFORM_C
-/* LibTomMath, multiple-precision integer library -- Tom St Denis */
-/* SPDX-License-Identifier: Unlicense */
-
-/* First the OS-specific special cases
- * - *BSD
- * - Windows
- */
-#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) || defined(__DragonFly__)
-#  define MP_ARC4RANDOM
-#endif
-
-#if defined(_WIN32) || defined(_WIN32_WCE)
-#define MP_WIN_CSP
-
-#ifndef _WIN32_WINNT
-#define _WIN32_WINNT 0x0400
-#endif
-#ifdef _WIN32_WCE
-#define UNDER_CE
-#define ARM
-#endif
-
-#define WIN32_LEAN_AND_MEAN
-#include <windows.h>
-#include <wincrypt.h>
-
-static HCRYPTPROV hProv = 0;
-
-static void s_cleanup_win_csp(void)
-{
-   CryptReleaseContext(hProv, 0);
-   hProv = 0;
-}
-
-static int s_read_win_csp(void *p, size_t n)
-{
-   if (hProv == 0) {
-      if (!CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
-                               (CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET)) &&
-          !CryptAcquireContext(&hProv, NULL, MS_DEF_PROV, PROV_RSA_FULL,
-                               CRYPT_VERIFYCONTEXT | CRYPT_MACHINE_KEYSET | CRYPT_NEWKEYSET)) {
-         hProv = 0;
-         return MP_ERR;
-      }
-      atexit(s_cleanup_win_csp);
-   }
-   return CryptGenRandom(hProv, (DWORD)n, (BYTE *)p) == TRUE ? MP_OKAY : MP_ERR;
-}
-#endif /* WIN32 */
-
-#if !defined(MP_WIN_CSP) && defined(__linux__) && defined(__GLIBC_PREREQ)
-#if __GLIBC_PREREQ(2, 25)
-#define MP_GETRANDOM
-#include <sys/random.h>
-#include <errno.h>
-
-static int s_read_getrandom(void *p, size_t n)
-{
-   char *q = (char *)p;
-   while (n > 0) {
-      ssize_t ret = getrandom(q, n, 0);
-      if (ret < 0) {
-         if (errno == EINTR) {
-            continue;
-         }
-         return MP_ERR;
-      }
-      q += ret;
-      n -= (size_t)ret;
-   }
-   return MP_OKAY;
-}
-#endif
-#endif
-
-/* We assume all platforms besides windows provide "/dev/urandom".
- * In case yours doesn't, define MP_NO_DEV_URANDOM at compile-time.
- */
-#if !defined(MP_WIN_CSP) && !defined(MP_NO_DEV_URANDOM)
-#ifndef MP_DEV_URANDOM
-#define MP_DEV_URANDOM "/dev/urandom"
-#endif
-#include <fcntl.h>
-#include <errno.h>
-#include <unistd.h>
-
-static int s_read_dev_urandom(void *p, size_t n)
-{
-   int fd;
-   char *q = (char *)p;
-
-   do {
-      fd = open(MP_DEV_URANDOM, O_RDONLY);
-   } while ((fd == -1) && (errno == EINTR));
-   if (fd == -1) return MP_ERR;
-
-   while (n > 0) {
-      ssize_t ret = read(fd, p, n);
-      if (ret < 0) {
-         if (errno == EINTR) {
-            continue;
-         }
-         close(fd);
-         return MP_ERR;
-      }
-      q += ret;
-      n -= (size_t)ret;
-   }
-
-   close(fd);
-   return MP_OKAY;
-}
-#endif
-
-#if defined(MP_PRNG_ENABLE_LTM_RNG)
-unsigned long (*ltm_rng)(unsigned char *out, unsigned long outlen, void (*callback)(void));
-void (*ltm_rng_callback)(void);
-
-static int s_read_ltm_rng(void *p, size_t n)
-{
-   unsigned long ret;
-   if (ltm_rng == NULL) return MP_ERR;
-   ret = ltm_rng(p, n, ltm_rng_callback);
-   if (ret != n) return MP_ERR;
-   return MP_OKAY;
-}
-#endif
-
-int s_mp_rand_source_platform(void *p, size_t n)
-{
-#if defined(MP_ARC4RANDOM)
-   arc4random_buf(p, n);
-   return MP_OKAY;
-#else
-
-   int ret = MP_ERR;
-
-#if defined(MP_WIN_CSP)
-   ret = s_read_win_csp(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-#if defined(MP_GETRANDOM)
-   ret = s_read_getrandom(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-#if defined(MP_DEV_URANDOM)
-   ret = s_read_dev_urandom(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-#if defined(MP_PRNG_ENABLE_LTM_RNG)
-   ret = s_read_ltm_rng(p, n);
-   if (ret == MP_OKAY) return ret;
-#endif
-
-   return ret;
-#endif
-}
-
-#endif
diff --git a/callgraph.txt b/callgraph.txt
index ea615f9..be0ec9a 100644
--- a/callgraph.txt
+++ b/callgraph.txt
@@ -15360,7 +15360,10 @@ BN_S_MP_MUL_HIGH_DIGS_FAST_C
 +--->BN_MP_CLAMP_C
 
 
-BN_S_MP_RAND_SOURCE_PLATFORM_C
+BN_S_MP_RAND_JENKINS_C
+
+
+BN_S_MP_RAND_PLATFORM_C
 
 
 BN_S_MP_REVERSE_C
diff --git a/demo/main.c b/demo/main.c
index aa58c20..068e31a 100644
--- a/demo/main.c
+++ b/demo/main.c
@@ -26,8 +26,6 @@ void ndraw(mp_int *a, const char *name)
 
 int main(int argc, char **argv)
 {
-   srand(LTM_DEMO_RAND_SEED);
-
 #ifdef MP_8BIT
    printf("Digit size 8 Bit \n");
 #endif
diff --git a/demo/opponent.c b/demo/opponent.c
index 7f02252..69c1c85 100644
--- a/demo/opponent.c
+++ b/demo/opponent.c
@@ -1,5 +1,11 @@
 #include "shared.h"
 
+#ifdef LTM_MTEST_REAL_RAND
+#define LTM_MTEST_RAND_SEED  time(NULL)
+#else
+#define LTM_MTEST_RAND_SEED  23
+#endif
+
 static void draw(mp_int *a)
 {
    ndraw(a, "");
@@ -21,6 +27,8 @@ int mtest_opponent(void)
    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;
 
+   srand(LTM_MTEST_RAND_SEED);
+
    if (mp_init_multi(&a, &b, &c, &d, &e, &f, NULL)!= MP_OKAY)
       return EXIT_FAILURE;
 
diff --git a/demo/shared.h b/demo/shared.h
index 006f4cf..b32def7 100644
--- a/demo/shared.h
+++ b/demo/shared.h
@@ -18,12 +18,6 @@
 #define LTM_DEMO_TEST_REDUCE_2K_L 0
 #endif
 
-#ifdef LTM_DEMO_REAL_RAND
-#define LTM_DEMO_RAND_SEED  time(NULL)
-#else
-#define LTM_DEMO_RAND_SEED  23
-#endif
-
 #define MP_WUR /* TODO: result checks disabled for now */
 #include "tommath.h"
 
diff --git a/demo/test.c b/demo/test.c
index 00716e0..d036e1d 100644
--- a/demo/test.c
+++ b/demo/test.c
@@ -1,4 +1,25 @@
 #include "shared.h"
+#include "tommath_private.h"
+
+static long rand_long(void)
+{
+   long x;
+   if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) {
+      fprintf(stderr, "s_mp_rand_source failed\n");
+      exit(EXIT_FAILURE);
+   }
+   return x;
+}
+
+static int rand_int(void)
+{
+   int x;
+   if (s_mp_rand_source(&x, sizeof(x)) != MP_OKAY) {
+      fprintf(stderr, "s_mp_rand_source failed\n");
+      exit(EXIT_FAILURE);
+   }
+   return x;
+}
 
 static int test_trivial_stuff(void)
 {
@@ -260,7 +281,7 @@ static int test_mp_complement(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      long l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      long l = rand_long();
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
@@ -297,12 +318,12 @@ static int test_mp_tc_div_2d(void)
       long l;
       int em;
 
-      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = rand_long();
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = rand() % 32;
+      em = abs(rand_int()) % 32;
 
       mp_set_long(&d, (unsigned long)labs(l >> em));
       if ((l >> em) < 0)
@@ -333,14 +354,14 @@ static int test_mp_tc_xor(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      int l, em;
+      long l, em;
 
-      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = rand_long();
       mp_set_int(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      em = rand_long();
       mp_set_int(&b, (unsigned long)labs(em));
       if (em < 0)
          mp_neg(&b, &b);
@@ -376,12 +397,12 @@ static int test_mp_tc_or(void)
    for (i = 0; i < 1000; ++i) {
       long l, em;
 
-      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = rand_long();
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      em = rand_long();
       mp_set_long(&b, (unsigned long)labs(em));
       if (em < 0)
          mp_neg(&b, &b);
@@ -416,12 +437,12 @@ static int test_mp_tc_and(void)
    for (i = 0; i < 1000; ++i) {
       long l, em;
 
-      l = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      l = rand_long();
       mp_set_long(&a, (unsigned long)labs(l));
       if (l < 0)
          mp_neg(&a, &a);
 
-      em = ((long)rand() * rand() + 1) * (rand() % 1 ? -1 : 1);
+      em = rand_long();
       mp_set_long(&b, (unsigned long)labs(em));
       if (em < 0)
          mp_neg(&b, &b);
@@ -518,8 +539,8 @@ static int test_mp_set_double(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      int tmp = rand();
-      double dbl = (double)tmp * rand() + 1;
+      int tmp = rand_int();
+      double dbl = (double)tmp * rand_int() + 1;
       if (mp_set_double(&a, dbl) != MP_OKAY) {
          printf("\nmp_set_double() failed");
          goto LBL_ERR;
@@ -558,7 +579,7 @@ static int test_mp_get_int(void)
    }
 
    for (i = 0; i < 1000; ++i) {
-      t = ((unsigned long)rand() * (unsigned long)rand() + 1uL) & 0xFFFFFFFFuL;
+      t = (unsigned long)rand_long() & 0xFFFFFFFFuL;
       mp_set_int(&a, t);
       if (t != mp_get_int(&a)) {
          printf("\nmp_get_int() bad result!");
@@ -662,7 +683,7 @@ static int test_mp_sqrt(void)
    for (i = 0; i < 1000; ++i) {
       printf("%6d\r", i);
       fflush(stdout);
-      n = (rand() & 15) + 1;
+      n = (rand_int() & 15) + 1;
       mp_rand(&a, n);
       if (mp_sqrt(&a, &b) != MP_OKAY) {
          printf("\nmp_sqrt() error!");
@@ -701,7 +722,7 @@ static int test_mp_is_square(void)
       fflush(stdout);
 
       /* test mp_is_square false negatives */
-      n = (rand() & 7) + 1;
+      n = (rand_int() & 7) + 1;
       mp_rand(&a, n);
       mp_sqr(&a, &a);
       if (mp_is_square(&a, &n) != MP_OKAY) {
@@ -789,7 +810,7 @@ static int test_mp_prime_rand(void)
    for (ix = 10; ix < 128; ix++) {
       printf("Testing (not safe-prime): %9d bits    \r", ix);
       fflush(stdout);
-      err = mp_prime_rand(&a, 8, ix, (rand() & 1) ? 0 : MP_PRIME_2MSB_ON);
+      err = mp_prime_rand(&a, 8, ix, (rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON);
       if (err != MP_OKAY) {
          printf("\nfailed with error: %s\n", mp_error_to_string(err));
          goto LBL_ERR;
@@ -850,7 +871,7 @@ static int test_mp_prime_is_prime(void)
    for (ix = 16; ix < 128; ix++) {
       printf("Testing (    safe-prime): %9d bits    \r", ix);
       fflush(stdout);
-      err = mp_prime_rand(&a, 8, ix, ((rand() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE);
+      err = mp_prime_rand(&a, 8, ix, ((rand_int() & 1) ? 0 : MP_PRIME_2MSB_ON) | MP_PRIME_SAFE);
       if (err != MP_OKAY) {
          printf("\nfailed with error: %s\n", mp_error_to_string(err));
          goto LBL_ERR;
@@ -941,7 +962,7 @@ static int test_mp_montgomery_reduce(void)
 
          /* now test a random reduction */
          for (ix = 0; ix < 100; ix++) {
-            mp_rand(&c, 1 + abs(rand()) % (2*i));
+            mp_rand(&c, 1 + abs(rand_int()) % (2*i));
             mp_copy(&c, &d);
             mp_copy(&c, &e);
 
@@ -1096,7 +1117,7 @@ static int test_mp_div_3(void)
          printf("%9d\r", cnt);
          fflush(stdout);
       }
-      mp_rand(&a, abs(rand()) % 128 + 1);
+      mp_rand(&a, abs(rand_int()) % 128 + 1);
       mp_div(&a, &d, &b, &e);
       mp_div_3(&a, &c, &r2);
 
@@ -1853,6 +1874,9 @@ int unit_tests(int argc, char **argv)
    unsigned long i;
    int res = EXIT_SUCCESS, j;
 
+   s_mp_rand_jenkins_init((uint64_t)time(NULL));
+   mp_rand_source(s_mp_rand_jenkins);
+
    for (i = 0; i < sizeof(test) / sizeof(test[0]); ++i) {
       if (argc > 1) {
          for (j = 1; j < argc; ++j) {
diff --git a/etc/tune.c b/etc/tune.c
index 399ad3d..06fb0d6 100644
--- a/etc/tune.c
+++ b/etc/tune.c
@@ -10,15 +10,6 @@
 #include <limits.h>
 #include <errno.h>
 
-static uint64_t s_ranval(void);
-static void s_raninit(uint64_t seed);
-static uint64_t s_timer_function(void);
-static void s_timer_start(void);
-static uint64_t s_timer_stop(void);
-static uint64_t s_time_mul(int size);
-static uint64_t s_time_sqr(int size);
-static void s_usage(char *s);
-
 /*
    Please take in mind that both multiplicands are of the same size. The balancing
    mechanism in mp_balance works well but has some overhead itself. You can test
@@ -26,56 +17,12 @@ static void s_usage(char *s);
    to generate ratios of the form 1:x.
 */
 
-/* Bob Jenkins' http://burtleburtle.net/bob/rand/smallprng.html */
-/* Chosen for speed and a good "mix" */
-typedef struct ranctx {
-   uint64_t a;
-   uint64_t b;
-   uint64_t c;
-   uint64_t d;
-} ranctx;
-
-static ranctx burtle_x;
-
-#   define rot(x,k) (((x)<<(k))|((x)>>(64-(k))))
-static uint64_t s_ranval(void)
-{
-   uint64_t e = burtle_x.a - rot(burtle_x.b, 7);
-   burtle_x.a = burtle_x.b ^ rot(burtle_x.c, 13);
-   burtle_x.b = burtle_x.c + rot(burtle_x.d, 37);
-   burtle_x.c = burtle_x.d + e;
-   burtle_x.d = e + burtle_x.a;
-   return burtle_x.d;
-}
-
-static void s_raninit(uint64_t seed)
-{
-   uint64_t i;
-   burtle_x.a = 0xf1ea5eed;
-   burtle_x.b = burtle_x.c = burtle_x.d = seed;
-   for (i = 0; i < 20; ++i) {
-      (void) s_ranval();
-   }
-}
-
-/*
-   The original used LTM's mp_rand which uses the cryptographically secure
-   source of the OS for its purpose. That is too expensive, too slow and
-   most important for a benchmark: it is not repeatable.
-*/
-static int s_ranbuf(void *p, size_t n)
-{
-   char *q = (char *)p;
-   while (n > 0) {
-      int i;
-      uint64_t x = s_ranval();
-      for (i = 0; i < 8 && n > 0; ++i, --n) {
-         *q++ = (char)(x & 0xFF);
-         x >>= 8;
-      }
-   }
-   return MP_OKAY;
-}
+static uint64_t s_timer_function(void);
+static void s_timer_start(void);
+static uint64_t s_timer_stop(void);
+static uint64_t s_time_mul(int size);
+static uint64_t s_time_sqr(int size);
+static void s_usage(char *s);
 
 static uint64_t s_timer_function(void)
 {
@@ -263,8 +210,6 @@ int main(int argc, char **argv)
    s_number_of_test_loops = 64;
    s_stabilization_extra = 3;
 
-   mp_rand_source(s_ranbuf);
-
    /* Very simple option parser, please treat it nicely. */
    if (argc != 1) {
       for (opt = 1; (opt < argc) && (argv[opt][0] == '-'); opt++) {
@@ -504,7 +449,13 @@ int main(int argc, char **argv)
       }
    }
 
-   s_raninit(seed);
+   /*
+     mp_rand uses the cryptographically secure
+     source of the OS by default. That is too expensive, too slow and
+     most important for a benchmark: it is not repeatable.
+   */
+   s_mp_rand_jenkins_init(seed);
+   mp_rand_source(s_mp_rand_jenkins);
 
    ksm  = KARATSUBA_MUL_CUTOFF;
    kss  = KARATSUBA_SQR_CUTOFF;
diff --git a/libtommath_VS2008.vcproj b/libtommath_VS2008.vcproj
index b630ce8..6e4fb35 100644
--- a/libtommath_VS2008.vcproj
+++ b/libtommath_VS2008.vcproj
@@ -873,7 +873,11 @@
 			>
 		</File>
 		<File
-			RelativePath="bn_s_mp_rand_source_platform.c"
+			RelativePath="bn_s_mp_rand_jenkins.c"
+			>
+		</File>
+		<File
+			RelativePath="bn_s_mp_rand_platform.c"
 			>
 		</File>
 		<File
diff --git a/makefile b/makefile
index 8b6f9af..a77373f 100644
--- a/makefile
+++ b/makefile
@@ -52,8 +52,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
-bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \
+bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.mingw b/makefile.mingw
index dd67ff8..a1f72bc 100644
--- a/makefile.mingw
+++ b/makefile.mingw
@@ -55,8 +55,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
-bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \
+bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h tommath_class.h tommath_superclass.h
 
diff --git a/makefile.msvc b/makefile.msvc
index 00706e0..6e5ee71 100644
--- a/makefile.msvc
+++ b/makefile.msvc
@@ -47,8 +47,8 @@ bn_mp_toradix.obj bn_mp_toradix_n.obj bn_mp_unsigned_bin_size.obj bn_mp_xor.obj 
 bn_s_mp_add.obj bn_s_mp_balance_mul.obj bn_s_mp_exptmod.obj bn_s_mp_exptmod_fast.obj bn_s_mp_invmod_fast.obj \
 bn_s_mp_invmod_slow.obj bn_s_mp_karatsuba_mul.obj bn_s_mp_karatsuba_sqr.obj bn_s_mp_montgomery_reduce_fast.obj \
 bn_s_mp_mul_digs.obj bn_s_mp_mul_digs_fast.obj bn_s_mp_mul_high_digs.obj bn_s_mp_mul_high_digs_fast.obj \
-bn_s_mp_rand_source_platform.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj bn_s_mp_sub.obj \
-bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
+bn_s_mp_rand_jenkins.obj bn_s_mp_rand_platform.obj bn_s_mp_reverse.obj bn_s_mp_sqr.obj bn_s_mp_sqr_fast.obj \
+bn_s_mp_sub.obj bn_s_mp_toom_mul.obj bn_s_mp_toom_sqr.obj
 
 HEADERS_PUB=tommath.h tommath_class.h tommath_superclass.h
 
diff --git a/makefile.shared b/makefile.shared
index 7b0cffd..1479793 100644
--- a/makefile.shared
+++ b/makefile.shared
@@ -49,8 +49,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
-bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \
+bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 #END_INS
 
diff --git a/makefile.unix b/makefile.unix
index 9aed117..e43745a 100644
--- a/makefile.unix
+++ b/makefile.unix
@@ -56,8 +56,8 @@ bn_mp_toradix.o bn_mp_toradix_n.o bn_mp_unsigned_bin_size.o bn_mp_xor.o bn_mp_ze
 bn_s_mp_add.o bn_s_mp_balance_mul.o bn_s_mp_exptmod.o bn_s_mp_exptmod_fast.o bn_s_mp_invmod_fast.o \
 bn_s_mp_invmod_slow.o bn_s_mp_karatsuba_mul.o bn_s_mp_karatsuba_sqr.o bn_s_mp_montgomery_reduce_fast.o \
 bn_s_mp_mul_digs.o bn_s_mp_mul_digs_fast.o bn_s_mp_mul_high_digs.o bn_s_mp_mul_high_digs_fast.o \
-bn_s_mp_rand_source_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o bn_s_mp_sub.o \
-bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
+bn_s_mp_rand_jenkins.o bn_s_mp_rand_platform.o bn_s_mp_reverse.o bn_s_mp_sqr.o bn_s_mp_sqr_fast.o \
+bn_s_mp_sub.o bn_s_mp_toom_mul.o bn_s_mp_toom_sqr.o
 
 HEADERS_PUB=tommath.h tommath_class.h tommath_superclass.h
 
diff --git a/tommath_class.h b/tommath_class.h
index 7738a3c..2e62a64 100644
--- a/tommath_class.h
+++ b/tommath_class.h
@@ -150,7 +150,8 @@
 #   define BN_S_MP_MUL_DIGS_FAST_C
 #   define BN_S_MP_MUL_HIGH_DIGS_C
 #   define BN_S_MP_MUL_HIGH_DIGS_FAST_C
-#   define BN_S_MP_RAND_SOURCE_PLATFORM_C
+#   define BN_S_MP_RAND_JENKINS_C
+#   define BN_S_MP_RAND_PLATFORM_C
 #   define BN_S_MP_REVERSE_C
 #   define BN_S_MP_SQR_C
 #   define BN_S_MP_SQR_FAST_C
@@ -1173,7 +1174,11 @@
 #   define BN_MP_CLAMP_C
 #endif
 
-#if defined(BN_S_MP_RAND_SOURCE_PLATFORM_C)
+#if defined(BN_S_MP_RAND_JENKINS_C)
+#   define BN_S_MP_RAND_JENKINS_INIT_C
+#endif
+
+#if defined(BN_S_MP_RAND_PLATFORM_C)
 #endif
 
 #if defined(BN_S_MP_REVERSE_C)
diff --git a/tommath_private.h b/tommath_private.h
index 0cf5d61..2fa3e0e 100644
--- a/tommath_private.h
+++ b/tommath_private.h
@@ -130,9 +130,13 @@ MP_WUR int s_mp_invmod_slow(const mp_int *a, const mp_int *b, mp_int *c);
 MP_WUR int s_mp_montgomery_reduce_fast(mp_int *x, const mp_int *n, mp_digit rho);
 MP_WUR int s_mp_exptmod_fast(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
 MP_WUR int s_mp_exptmod(const mp_int *G, const mp_int *X, const mp_int *P, mp_int *Y, int redmode);
-MP_WUR int s_mp_rand_source_platform(void *p, size_t n);
+MP_WUR int s_mp_rand_platform(void *p, size_t n);
 void s_mp_reverse(unsigned char *s, int len);
 
+/* TODO: jenkins prng is not thread safe as of now */
+MP_WUR int s_mp_rand_jenkins(void *p, size_t n);
+void s_mp_rand_jenkins_init(uint64_t);
+
 extern const char *const mp_s_rmap;
 extern const uint8_t mp_s_rmap_reverse[];
 extern const size_t mp_s_rmap_reverse_sz;