mirror of
https://github.com/bitcoin/bitcoin.git
synced 2026-01-19 23:03:45 +01:00
random: refactor: move rand* utilities to RandomMixin
Rather than make all the useful types of randomness be exclusive to FastRandomContext, move it to a separate RandomMixin class where it can be reused by other RNGs. A Curiously Recurring Template Pattern (CRTP) is used for this, to provide the ability for individual RNG classes to override one or more randomness functions, without needing the runtime-cost of virtual classes. Specifically, RNGs are expected to only provide fillrand and rand64, while all others are derived from those: - randbits - randrange - randbytes - rand32 - rand256 - randbool - rand_uniform_delay - rand_uniform_duration - min(), max(), operator()(), to comply with C++ URBG concept.
This commit is contained in:
@@ -665,7 +665,7 @@ void FastRandomContext::fillrand(Span<std::byte> output) noexcept
|
||||
rng.Keystream(output);
|
||||
}
|
||||
|
||||
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), rng(MakeByteSpan(seed)), bitbuf_size(0) {}
|
||||
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), rng(MakeByteSpan(seed)) {}
|
||||
|
||||
bool Random_SanityCheck()
|
||||
{
|
||||
@@ -715,7 +715,7 @@ bool Random_SanityCheck()
|
||||
|
||||
static constexpr std::array<std::byte, ChaCha20::KEYLEN> ZERO_KEY{};
|
||||
|
||||
FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), rng(ZERO_KEY), bitbuf_size(0)
|
||||
FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), rng(ZERO_KEY)
|
||||
{
|
||||
// Note that despite always initializing with ZERO_KEY, requires_seed is set to true if not
|
||||
// fDeterministic. That means the rng will be reinitialized with a secure random key upon first
|
||||
@@ -726,10 +726,8 @@ FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexce
|
||||
{
|
||||
requires_seed = from.requires_seed;
|
||||
rng = from.rng;
|
||||
bitbuf = from.bitbuf;
|
||||
bitbuf_size = from.bitbuf_size;
|
||||
from.requires_seed = true;
|
||||
from.bitbuf_size = 0;
|
||||
static_cast<RandomMixin<FastRandomContext>&>(*this) = std::move(from);
|
||||
return *this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user