[Gmp-commit] /var/hg/gmp: Update mini-gmp random seed logic to be idenpendent...
mercurial at gmplib.org
mercurial at gmplib.org
Thu Nov 24 18:34:19 UTC 2016
details: /var/hg/gmp/rev/fbc88b42a197
changeset: 17134:fbc88b42a197
user: Niels M?ller <nisse at lysator.liu.se>
date: Thu Nov 24 19:34:11 2016 +0100
description:
Update mini-gmp random seed logic to be idenpendent of word size.
diffstat:
mini-gmp/ChangeLog | 5 ++++
mini-gmp/tests/hex-random.c | 51 ++++++++++++++++++++++++++++----------------
2 files changed, 37 insertions(+), 19 deletions(-)
diffs (111 lines):
diff -r f0cd41dd4ebb -r fbc88b42a197 mini-gmp/ChangeLog
--- a/mini-gmp/ChangeLog Thu Nov 24 16:19:50 2016 +0100
+++ b/mini-gmp/ChangeLog Thu Nov 24 19:34:11 2016 +0100
@@ -1,5 +1,10 @@
2016-11-24 Niels Möller <nisse at lysator.liu.se>
+ * tests/hex-random.c (mkseed): Produce a 48-bit mpz_t value,
+ regardless of word size.
+ (hex_random_init): Use gmp_randseed instead of gmp_randseed_ui,
+ and support seeds exceeding an unsigned long.
+
* tests/hex-random.c (mkseed): New function, using /dev/urandom
for random seed when available.
(hex_random_init): Use it.
diff -r f0cd41dd4ebb -r fbc88b42a197 mini-gmp/tests/hex-random.c
--- a/mini-gmp/tests/hex-random.c Thu Nov 24 16:19:50 2016 +0100
+++ b/mini-gmp/tests/hex-random.c Thu Nov 24 19:34:11 2016 +0100
@@ -33,61 +33,74 @@
static gmp_randstate_t state;
-static unsigned long
-mkseed (void)
+static void
+mkseed (mpz_t seed)
{
FILE *f = fopen ("/dev/urandom", "rb");
if (f)
{
- unsigned long seed;
+ unsigned char buf[6];
size_t res;
setbuf (f, NULL);
- res = fread (&seed, sizeof(seed), 1, f);
+ res = fread (buf, sizeof(buf), 1, f);
fclose (f);
if (res == 1)
- return seed;
+ {
+ mpz_import (seed, sizeof(buf), 1, 1, 0, 0, buf);
+ return;
+ }
}
+
#ifdef __unix__
{
struct timeval tv;
+ mpz_t usec;
+ mpz_init (usec);
+
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);
+ mpz_set_ui (seed, tv.tv_sec);
+ mpz_set_ui (usec, tv.tv_usec);
+ /* usec fits in 20 bits, shift left to make it 48 bits. */
+ mpz_mul_2exp (usec, usec, 28);
+ mpz_xor (seed, seed, usec);
+
+ mpz_clear (usec);
}
#else
- return time (NULL);
+ mpz_set_ui (seed, time (NULL));
#endif
}
void
hex_random_init (void)
{
- unsigned long seed;
+ mpz_t seed;
char *env_seed;
+ mpz_init (seed);
+
env_seed = getenv ("GMP_CHECK_RANDOMIZE");
if (env_seed && env_seed[0])
{
- seed = strtoul (env_seed, NULL, 0);
- if (seed)
- printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%lu\n", seed);
+ mpz_set_str (seed, env_seed, 0);
+ if (mpz_cmp_ui (seed, 0) != 0)
+ gmp_printf ("Re-seeding with GMP_CHECK_RANDOMIZE=%Zd\n", seed);
else
{
- seed = mkseed ();
- printf ("Seed GMP_CHECK_RANDOMIZE=%lu (include this in bug reports)\n", seed);
+ mkseed (seed);
+ gmp_printf ("Seed GMP_CHECK_RANDOMIZE=%Zd (include this in bug reports)\n", seed);
}
fflush (stdout);
}
else
- seed = 4711;
+ mpz_set_ui (seed, 4711);
gmp_randinit_default (state);
- gmp_randseed_ui (state, seed);
+ gmp_randseed (state, seed);
+
+ mpz_clear (seed);
}
char *
More information about the gmp-commit
mailing list