[Gmp-commit] /var/hg/gmp: Use /dev/urandom for seeding mini-gmp tests, when a...
mercurial at gmplib.org
mercurial at gmplib.org
Thu Nov 24 15:20:08 UTC 2016
details: /var/hg/gmp/rev/f0cd41dd4ebb
changeset: 17133:f0cd41dd4ebb
user: Niels M?ller <nisse at lysator.liu.se>
date: Thu Nov 24 16:19:50 2016 +0100
description:
Use /dev/urandom for seeding mini-gmp tests, when available.
diffstat:
mini-gmp/ChangeLog | 6 ++++++
mini-gmp/tests/hex-random.c | 43 +++++++++++++++++++++++++++++++++++++++----
2 files changed, 45 insertions(+), 4 deletions(-)
diffs (88 lines):
diff -r c76021c9e885 -r f0cd41dd4ebb mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog Wed Nov 23 21:32:15 2016 +0100
+++ b/mini-gmp/ChangeLog Thu Nov 24 16:19:50 2016 +0100
@@ -1,3 +1,9 @@
+2016-11-24 Niels Möller <nisse at lysator.liu.se>
+
+ * tests/hex-random.c (mkseed): New function, using /dev/urandom
+ for random seed when available.
+ (hex_random_init): Use it.
+
2016-11-23 Niels Möller <nisse at lysator.liu.se>
* mini-gmp.c (GMP_CMP): New macro.
diff -r c76021c9e885 -r f0cd41dd4ebb mini-gmp/tests/hex-random.c
--- a/mini-gmp/tests/hex-random.c Wed Nov 23 21:32:15 2016 +0100
+++ b/mini-gmp/tests/hex-random.c Thu Nov 24 16:19:50 2016 +0100
@@ -21,7 +21,11 @@
#include <stdlib.h>
#include <time.h>
-#include <unistd.h>
+
+#ifdef __unix__
+# include <unistd.h>
+# include <sys/time.h>
+#endif
#include "gmp.h"
@@ -29,13 +33,44 @@
static gmp_randstate_t state;
+static unsigned long
+mkseed (void)
+{
+ FILE *f = fopen ("/dev/urandom", "rb");
+ if (f)
+ {
+ unsigned long seed;
+ size_t res;
+
+ setbuf (f, NULL);
+ res = fread (&seed, sizeof(seed), 1, f);
+ fclose (f);
+
+ if (res == 1)
+ return seed;
+ }
+#ifdef __unix__
+ {
+ struct timeval tv;
+ gettimeofday (&tv, NULL);
+ /* Unsigned long may be only 32 bits, and then a plain microsecond
+ count would wrap around in only 71 minutes. So instead, xor
+ microseconds with the most significant second bits, which are
+ the least "random". */
+ return tv.tv_sec ^ (tv.tv_usec << 12);
+ }
+#else
+ return time (NULL);
+#endif
+}
+
void
hex_random_init (void)
{
unsigned long seed;
char *env_seed;
- env_seed = getenv("GMP_CHECK_RANDOMIZE");
+ env_seed = getenv ("GMP_CHECK_RANDOMIZE");
if (env_seed && env_seed[0])
{
seed = strtoul (env_seed, NULL, 0);
@@ -43,10 +78,10 @@
printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%lu\n", seed);
else
{
- seed = time(NULL) + getpid();
+ seed = mkseed ();
printf ("Seed GMP_CHECK_RANDOMIZE=%lu (include this in bug reports)\n", seed);
}
- fflush(stdout);
+ fflush (stdout);
}
else
seed = 4711;
More information about the gmp-commit
mailing list