aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick Mathewson <nickm@torproject.org>2019-01-17 16:10:02 -0500
committerNick Mathewson <nickm@torproject.org>2019-01-17 16:10:02 -0500
commitc08fc2e19e7695c5d3fd111fb8c1a46afc23b5e8 (patch)
tree996d9e796f60177d4f2e5c5d15a45ae60a90dc35
parent77712a5fa29f135e0a79ed9122f28c5dcb902e77 (diff)
downloadtor-c08fc2e19e7695c5d3fd111fb8c1a46afc23b5e8.tar.gz
tor-c08fc2e19e7695c5d3fd111fb8c1a46afc23b5e8.zip
Speed up the deterministic prng in test_prob_distr, by a lot.
Using a single xof object and squeezing it repeatedly should make everything MUCH faster here.
-rw-r--r--src/test/test_prob_distr.c47
1 files changed, 31 insertions, 16 deletions
diff --git a/src/test/test_prob_distr.c b/src/test/test_prob_distr.c
index aac9d23e46..114057feca 100644
--- a/src/test/test_prob_distr.c
+++ b/src/test/test_prob_distr.c
@@ -1115,13 +1115,30 @@ test_psi_dist_sample(const struct dist *dist)
}
/* This is the seed of the deterministic randomness */
-static uint32_t deterministic_rand_counter;
+static uint8_t rng_seed[16];
+static crypto_xof_t *rng_xof = NULL;
/** Initialize the seed of the deterministic randomness. */
static void
init_deterministic_rand(void)
{
- deterministic_rand_counter = crypto_rand_u32();
+ crypto_rand((char*)rng_seed, sizeof(rng_seed));
+ crypto_xof_free(rng_xof);
+ rng_xof = crypto_xof_new();
+ crypto_xof_add_bytes(rng_xof, rng_seed, sizeof(rng_seed));
+}
+
+static void
+teardown_deterministic_rand(void)
+{
+ crypto_xof_free(rng_xof);
+}
+
+static void
+dump_seed(void)
+{
+ printf("\nSeed: %s\n",
+ hex_str((const char*)rng_seed, sizeof(rng_seed)));
}
/** Produce deterministic randomness for the stochastic tests using the global
@@ -1134,15 +1151,8 @@ static void
crypto_rand_deterministic(char *out, size_t n)
{
/* Use a XOF to squeeze bytes out of that silly counter */
- crypto_xof_t *xof = crypto_xof_new();
- tor_assert(xof);
- crypto_xof_add_bytes(xof, (uint8_t*)&deterministic_rand_counter,
- sizeof(deterministic_rand_counter));
- crypto_xof_squeeze_bytes(xof, (uint8_t*)out, n);
- crypto_xof_free(xof);
-
- /* Increase counter for next run */
- deterministic_rand_counter++;
+ tor_assert(rng_xof);
+ crypto_xof_squeeze_bytes(rng_xof, (uint8_t*)out, n);
}
static void
@@ -1287,8 +1297,9 @@ test_stochastic_genpareto(void *arg)
done:
if (tests_failed) {
- printf("seed: %"PRIu32, deterministic_rand_counter);
+ dump_seed();
}
+ teardown_deterministic_rand();
UNMOCK(crypto_rand);
}
@@ -1316,8 +1327,9 @@ test_stochastic_geometric(void *arg)
done:
if (tests_failed) {
- printf("seed: %"PRIu32, deterministic_rand_counter);
+ dump_seed();
}
+ teardown_deterministic_rand();
UNMOCK(crypto_rand);
}
@@ -1344,8 +1356,9 @@ test_stochastic_logistic(void *arg)
done:
if (tests_failed) {
- printf("seed: %"PRIu32, deterministic_rand_counter);
+ dump_seed();
}
+ teardown_deterministic_rand();
UNMOCK(crypto_rand);
}
@@ -1372,8 +1385,9 @@ test_stochastic_log_logistic(void *arg)
done:
if (tests_failed) {
- printf("seed: %"PRIu32, deterministic_rand_counter);
+ dump_seed();
}
+ teardown_deterministic_rand();
UNMOCK(crypto_rand);
}
@@ -1402,8 +1416,9 @@ test_stochastic_weibull(void *arg)
done:
if (tests_failed) {
- printf("seed: %"PRIu32, deterministic_rand_counter);
+ dump_seed();
}
+ teardown_deterministic_rand();
UNMOCK(crypto_rand);
}