|
|
@@ -33,6 +33,7 @@
|
|
|
#include "lib/math/prob_distr.h"
|
|
|
#include "lib/math/fp.h"
|
|
|
#include "lib/crypt_ops/crypto_rand.h"
|
|
|
+#include "test/rng_test_helpers.h"
|
|
|
|
|
|
#include <float.h>
|
|
|
#include <math.h>
|
|
|
@@ -1117,49 +1118,14 @@ test_psi_dist_sample(const struct dist *dist)
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-/* This is the seed of the deterministic randomness */
|
|
|
-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)
|
|
|
-{
|
|
|
- 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("\n"
|
|
|
"NOTE: This is a stochastic test, and we expect it to fail from\n"
|
|
|
"time to time, with some low probability. If you see it fail more\n"
|
|
|
- "than one trial in 100, though, please tell us.\n\n"
|
|
|
- "Seed: %s\n",
|
|
|
- hex_str((const char*)rng_seed, sizeof(rng_seed)));
|
|
|
-}
|
|
|
-
|
|
|
-/** Produce deterministic randomness for the stochastic tests using the global
|
|
|
- * deterministic_rand_counter seed
|
|
|
- *
|
|
|
- * This function produces deterministic data over multiple calls iff it's
|
|
|
- * called in the same call order with the same 'n' parameter (which is the
|
|
|
- * case for the psi test). If not, outputs will deviate. */
|
|
|
-static void
|
|
|
-crypto_rand_deterministic(char *out, size_t n)
|
|
|
-{
|
|
|
- /* Use a XOF to squeeze bytes out of that silly counter */
|
|
|
- tor_assert(rng_xof);
|
|
|
- crypto_xof_squeeze_bytes(rng_xof, (uint8_t*)out, n);
|
|
|
+ "than one trial in 100, though, please tell us.\n\n");
|
|
|
+ testing_dump_reproducible_rng_seed();
|
|
|
}
|
|
|
|
|
|
static void
|
|
|
@@ -1199,8 +1165,7 @@ test_stochastic_uniform(void *arg)
|
|
|
};
|
|
|
bool ok = true, tests_failed = true;
|
|
|
|
|
|
- init_deterministic_rand();
|
|
|
- MOCK(crypto_rand, crypto_rand_deterministic);
|
|
|
+ testing_enable_reproducible_rng();
|
|
|
|
|
|
ok &= test_psi_dist_sample(&uniform01.base);
|
|
|
ok &= test_psi_dist_sample(&uniform_pos.base);
|
|
|
@@ -1217,8 +1182,7 @@ test_stochastic_uniform(void *arg)
|
|
|
if (tests_failed) {
|
|
|
dump_seed();
|
|
|
}
|
|
|
- teardown_deterministic_rand();
|
|
|
- UNMOCK(crypto_rand);
|
|
|
+ testing_disable_reproducible_rng();
|
|
|
}
|
|
|
|
|
|
static bool
|
|
|
@@ -1288,8 +1252,7 @@ test_stochastic_genpareto(void *arg)
|
|
|
bool tests_failed = true;
|
|
|
(void) arg;
|
|
|
|
|
|
- init_deterministic_rand();
|
|
|
- MOCK(crypto_rand, crypto_rand_deterministic);
|
|
|
+ testing_enable_reproducible_rng();
|
|
|
|
|
|
ok = test_stochastic_genpareto_impl(0, 1, -0.25);
|
|
|
tt_assert(ok);
|
|
|
@@ -1312,8 +1275,7 @@ test_stochastic_genpareto(void *arg)
|
|
|
if (tests_failed) {
|
|
|
dump_seed();
|
|
|
}
|
|
|
- teardown_deterministic_rand();
|
|
|
- UNMOCK(crypto_rand);
|
|
|
+ testing_disable_reproducible_rng();
|
|
|
}
|
|
|
|
|
|
static void
|
|
|
@@ -1324,8 +1286,7 @@ test_stochastic_geometric(void *arg)
|
|
|
|
|
|
(void) arg;
|
|
|
|
|
|
- init_deterministic_rand();
|
|
|
- MOCK(crypto_rand, crypto_rand_deterministic);
|
|
|
+ testing_enable_reproducible_rng();
|
|
|
|
|
|
ok = test_stochastic_geometric_impl(0.1);
|
|
|
tt_assert(ok);
|
|
|
@@ -1342,8 +1303,7 @@ test_stochastic_geometric(void *arg)
|
|
|
if (tests_failed) {
|
|
|
dump_seed();
|
|
|
}
|
|
|
- teardown_deterministic_rand();
|
|
|
- UNMOCK(crypto_rand);
|
|
|
+ testing_disable_reproducible_rng();
|
|
|
}
|
|
|
|
|
|
static void
|
|
|
@@ -1353,8 +1313,7 @@ test_stochastic_logistic(void *arg)
|
|
|
bool tests_failed = true;
|
|
|
(void) arg;
|
|
|
|
|
|
- init_deterministic_rand();
|
|
|
- MOCK(crypto_rand, crypto_rand_deterministic);
|
|
|
+ testing_enable_reproducible_rng();
|
|
|
|
|
|
ok = test_stochastic_logistic_impl(0, 1);
|
|
|
tt_assert(ok);
|
|
|
@@ -1371,8 +1330,7 @@ test_stochastic_logistic(void *arg)
|
|
|
if (tests_failed) {
|
|
|
dump_seed();
|
|
|
}
|
|
|
- teardown_deterministic_rand();
|
|
|
- UNMOCK(crypto_rand);
|
|
|
+ testing_disable_reproducible_rng();
|
|
|
}
|
|
|
|
|
|
static void
|
|
|
@@ -1382,8 +1340,7 @@ test_stochastic_log_logistic(void *arg)
|
|
|
bool tests_failed = true;
|
|
|
(void) arg;
|
|
|
|
|
|
- init_deterministic_rand();
|
|
|
- MOCK(crypto_rand, crypto_rand_deterministic);
|
|
|
+ testing_enable_reproducible_rng();
|
|
|
|
|
|
ok = test_stochastic_log_logistic_impl(1, 1);
|
|
|
tt_assert(ok);
|
|
|
@@ -1400,8 +1357,7 @@ test_stochastic_log_logistic(void *arg)
|
|
|
if (tests_failed) {
|
|
|
dump_seed();
|
|
|
}
|
|
|
- teardown_deterministic_rand();
|
|
|
- UNMOCK(crypto_rand);
|
|
|
+ testing_disable_reproducible_rng();
|
|
|
}
|
|
|
|
|
|
static void
|
|
|
@@ -1411,8 +1367,7 @@ test_stochastic_weibull(void *arg)
|
|
|
bool tests_failed = true;
|
|
|
(void) arg;
|
|
|
|
|
|
- init_deterministic_rand();
|
|
|
- MOCK(crypto_rand, crypto_rand_deterministic);
|
|
|
+ testing_enable_reproducible_rng();
|
|
|
|
|
|
ok = test_stochastic_weibull_impl(1, 0.5);
|
|
|
tt_assert(ok);
|
|
|
@@ -1431,7 +1386,7 @@ test_stochastic_weibull(void *arg)
|
|
|
if (tests_failed) {
|
|
|
dump_seed();
|
|
|
}
|
|
|
- teardown_deterministic_rand();
|
|
|
+ testing_disable_reproducible_rng();
|
|
|
UNMOCK(crypto_rand);
|
|
|
}
|
|
|
|