Changeset View
Changeset View
Standalone View
Standalone View
src/test/util_tests.cpp
Show First 20 Lines • Show All 811 Lines • ▼ Show 20 Lines | |||||
BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) { | BOOST_AUTO_TEST_CASE(util_seed_insecure_rand) { | ||||
SeedInsecureRand(true); | SeedInsecureRand(true); | ||||
for (int mod = 2; mod < 11; mod++) { | for (int mod = 2; mod < 11; mod++) { | ||||
int mask = 1; | int mask = 1; | ||||
// Really rough binomial confidence approximation. | // Really rough binomial confidence approximation. | ||||
int err = | int err = | ||||
30 * 10000. / mod * sqrt((1. / mod * (1 - 1. / mod)) / 10000.); | 30 * 10000. / mod * sqrt((1. / mod * (1 - 1. / mod)) / 10000.); | ||||
// mask is 2^ceil(log2(mod))-1 | // mask is 2^ceil(log2(mod))-1 | ||||
while (mask < mod - 1) | while (mask < mod - 1) { | ||||
mask = (mask << 1) + 1; | mask = (mask << 1) + 1; | ||||
} | |||||
int count = 0; | int count = 0; | ||||
// How often does it get a zero from the uniform range [0,mod)? | // How often does it get a zero from the uniform range [0,mod)? | ||||
for (int i = 0; i < 10000; i++) { | for (int i = 0; i < 10000; i++) { | ||||
uint32_t rval; | uint32_t rval; | ||||
do { | do { | ||||
rval = insecure_rand() & mask; | rval = InsecureRand32() & mask; | ||||
} while (rval >= (uint32_t)mod); | } while (rval >= uint32_t(mod)); | ||||
count += rval == 0; | count += rval == 0; | ||||
} | } | ||||
BOOST_CHECK(count <= 10000 / mod + err); | BOOST_CHECK(count <= 10000 / mod + err); | ||||
BOOST_CHECK(count >= 10000 / mod - err); | BOOST_CHECK(count >= 10000 / mod - err); | ||||
} | } | ||||
} | } | ||||
BOOST_AUTO_TEST_CASE(util_TimingResistantEqual) { | BOOST_AUTO_TEST_CASE(util_TimingResistantEqual) { | ||||
▲ Show 20 Lines • Show All 590 Lines • Show Last 20 Lines |