mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-06-02 01:04:43 +02:00
random: add RandomMixin::randbits with compile-known bits
In many cases, it is known at compile time how many bits are requested from randbits. Provide a variant of randbits that accepts this number as a template, to make sure the compiler can make use of this knowledge. This is used immediately in rand32() and randbool(), and a few further call sites.
This commit is contained in:
@@ -1195,7 +1195,7 @@ BOOST_AUTO_TEST_CASE(muhash_tests)
|
||||
uint256 res;
|
||||
int table[4];
|
||||
for (int i = 0; i < 4; ++i) {
|
||||
table[i] = g_insecure_rand_ctx.randbits(3);
|
||||
table[i] = g_insecure_rand_ctx.randbits<3>();
|
||||
}
|
||||
for (int order = 0; order < 4; ++order) {
|
||||
MuHash3072 acc;
|
||||
@@ -1215,8 +1215,8 @@ BOOST_AUTO_TEST_CASE(muhash_tests)
|
||||
}
|
||||
}
|
||||
|
||||
MuHash3072 x = FromInt(g_insecure_rand_ctx.randbits(4)); // x=X
|
||||
MuHash3072 y = FromInt(g_insecure_rand_ctx.randbits(4)); // x=X, y=Y
|
||||
MuHash3072 x = FromInt(g_insecure_rand_ctx.randbits<4>()); // x=X
|
||||
MuHash3072 y = FromInt(g_insecure_rand_ctx.randbits<4>()); // x=X, y=Y
|
||||
MuHash3072 z; // x=X, y=Y, z=1
|
||||
z *= x; // x=X, y=Y, z=X
|
||||
z *= y; // x=X, y=Y, z=X*Y
|
||||
|
||||
@@ -107,7 +107,7 @@ BOOST_AUTO_TEST_CASE(fastrandom_randbits)
|
||||
BOOST_AUTO_TEST_CASE(randbits_test)
|
||||
{
|
||||
FastRandomContext ctx_lens; //!< RNG for producing the lengths requested from ctx_test.
|
||||
FastRandomContext ctx_test; //!< The RNG being tested.
|
||||
FastRandomContext ctx_test1(true), ctx_test2(true); //!< The RNGs being tested.
|
||||
int ctx_test_bitsleft{0}; //!< (Assumed value of) ctx_test::bitbuf_len
|
||||
|
||||
// Run the entire test 5 times.
|
||||
@@ -122,7 +122,25 @@ BOOST_AUTO_TEST_CASE(randbits_test)
|
||||
// Decide on a number of bits to request (0 through 64, inclusive; don't use randbits/randrange).
|
||||
int bits = ctx_lens.rand64() % 65;
|
||||
// Generate that many bits.
|
||||
uint64_t gen = ctx_test.randbits(bits);
|
||||
uint64_t gen = ctx_test1.randbits(bits);
|
||||
// For certain bits counts, also test randbits<Bits> and compare.
|
||||
uint64_t gen2;
|
||||
if (bits == 0) {
|
||||
gen2 = ctx_test2.randbits<0>();
|
||||
} else if (bits == 1) {
|
||||
gen2 = ctx_test2.randbits<1>();
|
||||
} else if (bits == 7) {
|
||||
gen2 = ctx_test2.randbits<7>();
|
||||
} else if (bits == 32) {
|
||||
gen2 = ctx_test2.randbits<32>();
|
||||
} else if (bits == 51) {
|
||||
gen2 = ctx_test2.randbits<51>();
|
||||
} else if (bits == 64) {
|
||||
gen2 = ctx_test2.randbits<64>();
|
||||
} else {
|
||||
gen2 = ctx_test2.randbits(bits);
|
||||
}
|
||||
BOOST_CHECK_EQUAL(gen, gen2);
|
||||
// Make sure the result is in range.
|
||||
if (bits < 64) BOOST_CHECK_EQUAL(gen >> bits, 0);
|
||||
// Mark all the seen bits in the output.
|
||||
|
||||
Reference in New Issue
Block a user