mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-02-14 11:26:09 -05:00
random: replace construct/assign with explicit Reseed()
This commit is contained in:
parent
2ae392d561
commit
ce8094246e
7 changed files with 32 additions and 47 deletions
|
@ -704,6 +704,13 @@ void FastRandomContext::fillrand(Span<std::byte> output) noexcept
|
||||||
|
|
||||||
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), rng(MakeByteSpan(seed)) {}
|
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), rng(MakeByteSpan(seed)) {}
|
||||||
|
|
||||||
|
void FastRandomContext::Reseed(const uint256& seed) noexcept
|
||||||
|
{
|
||||||
|
FlushCache();
|
||||||
|
requires_seed = false;
|
||||||
|
rng = {MakeByteSpan(seed)};
|
||||||
|
}
|
||||||
|
|
||||||
bool Random_SanityCheck()
|
bool Random_SanityCheck()
|
||||||
{
|
{
|
||||||
uint64_t start = GetPerformanceCounter();
|
uint64_t start = GetPerformanceCounter();
|
||||||
|
@ -759,15 +766,6 @@ FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_se
|
||||||
// use.
|
// use.
|
||||||
}
|
}
|
||||||
|
|
||||||
FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexcept
|
|
||||||
{
|
|
||||||
requires_seed = from.requires_seed;
|
|
||||||
rng = from.rng;
|
|
||||||
from.requires_seed = true;
|
|
||||||
static_cast<RandomMixin<FastRandomContext>&>(*this) = std::move(from);
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
void RandomInit()
|
void RandomInit()
|
||||||
{
|
{
|
||||||
// Invoke RNG code to trigger initialization (if not already performed)
|
// Invoke RNG code to trigger initialization (if not already performed)
|
||||||
|
|
53
src/random.h
53
src/random.h
|
@ -184,27 +184,21 @@ private:
|
||||||
*/
|
*/
|
||||||
RandomNumberGenerator auto& Impl() noexcept { return static_cast<T&>(*this); }
|
RandomNumberGenerator auto& Impl() noexcept { return static_cast<T&>(*this); }
|
||||||
|
|
||||||
public:
|
protected:
|
||||||
RandomMixin() noexcept = default;
|
constexpr void FlushCache() noexcept
|
||||||
|
{
|
||||||
|
bitbuf = 0;
|
||||||
|
bitbuf_size = 0;
|
||||||
|
}
|
||||||
|
|
||||||
// Do not permit copying an RNG.
|
public:
|
||||||
|
constexpr RandomMixin() noexcept = default;
|
||||||
|
|
||||||
|
// Do not permit copying or moving an RNG.
|
||||||
RandomMixin(const RandomMixin&) = delete;
|
RandomMixin(const RandomMixin&) = delete;
|
||||||
RandomMixin& operator=(const RandomMixin&) = delete;
|
RandomMixin& operator=(const RandomMixin&) = delete;
|
||||||
|
RandomMixin(RandomMixin&&) = delete;
|
||||||
RandomMixin(RandomMixin&& other) noexcept : bitbuf(other.bitbuf), bitbuf_size(other.bitbuf_size)
|
RandomMixin& operator=(RandomMixin&&) = delete;
|
||||||
{
|
|
||||||
other.bitbuf = 0;
|
|
||||||
other.bitbuf_size = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
RandomMixin& operator=(RandomMixin&& other) noexcept
|
|
||||||
{
|
|
||||||
bitbuf = other.bitbuf;
|
|
||||||
bitbuf_size = other.bitbuf_size;
|
|
||||||
other.bitbuf = 0;
|
|
||||||
other.bitbuf_size = 0;
|
|
||||||
return *this;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Generate a random (bits)-bit integer. */
|
/** Generate a random (bits)-bit integer. */
|
||||||
uint64_t randbits(int bits) noexcept
|
uint64_t randbits(int bits) noexcept
|
||||||
|
@ -394,13 +388,8 @@ public:
|
||||||
/** Initialize with explicit seed (only for testing) */
|
/** Initialize with explicit seed (only for testing) */
|
||||||
explicit FastRandomContext(const uint256& seed) noexcept;
|
explicit FastRandomContext(const uint256& seed) noexcept;
|
||||||
|
|
||||||
// Do not permit copying a FastRandomContext (move it, or create a new one to get reseeded).
|
/** Reseed with explicit seed (only for testing). */
|
||||||
FastRandomContext(const FastRandomContext&) = delete;
|
void Reseed(const uint256& seed) noexcept;
|
||||||
FastRandomContext(FastRandomContext&&) = delete;
|
|
||||||
FastRandomContext& operator=(const FastRandomContext&) = delete;
|
|
||||||
|
|
||||||
/** Move a FastRandomContext. If the original one is used again, it will be reseeded. */
|
|
||||||
FastRandomContext& operator=(FastRandomContext&& from) noexcept;
|
|
||||||
|
|
||||||
/** Generate a random 64-bit integer. */
|
/** Generate a random 64-bit integer. */
|
||||||
uint64_t rand64() noexcept
|
uint64_t rand64() noexcept
|
||||||
|
@ -440,14 +429,12 @@ public:
|
||||||
constexpr explicit InsecureRandomContext(uint64_t seedval) noexcept
|
constexpr explicit InsecureRandomContext(uint64_t seedval) noexcept
|
||||||
: m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval)) {}
|
: m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval)) {}
|
||||||
|
|
||||||
// no copy - that is dangerous, we don't want accidentally copy the RNG and then have two streams
|
constexpr void Reseed(uint64_t seedval) noexcept
|
||||||
// with exactly the same results.
|
{
|
||||||
InsecureRandomContext(const InsecureRandomContext&) = delete;
|
FlushCache();
|
||||||
InsecureRandomContext& operator=(const InsecureRandomContext&) = delete;
|
m_s0 = SplitMix64(seedval);
|
||||||
|
m_s1 = SplitMix64(seedval);
|
||||||
// allow moves
|
}
|
||||||
InsecureRandomContext(InsecureRandomContext&&) = default;
|
|
||||||
InsecureRandomContext& operator=(InsecureRandomContext&&) = default;
|
|
||||||
|
|
||||||
constexpr uint64_t rand64() noexcept
|
constexpr uint64_t rand64() noexcept
|
||||||
{
|
{
|
||||||
|
|
|
@ -124,7 +124,7 @@ public:
|
||||||
explicit AddrManDeterministic(const NetGroupManager& netgroupman, FuzzedDataProvider& fuzzed_data_provider)
|
explicit AddrManDeterministic(const NetGroupManager& netgroupman, FuzzedDataProvider& fuzzed_data_provider)
|
||||||
: AddrMan(netgroupman, /*deterministic=*/true, GetCheckRatio())
|
: AddrMan(netgroupman, /*deterministic=*/true, GetCheckRatio())
|
||||||
{
|
{
|
||||||
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
|
WITH_LOCK(m_impl->cs, m_impl->insecure_rand.Reseed(ConsumeUInt256(fuzzed_data_provider)));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
|
||||||
// ecdsa_signature_parse_der_lax are executed during this test.
|
// ecdsa_signature_parse_der_lax are executed during this test.
|
||||||
// Specifically branches that run only when an ECDSA
|
// Specifically branches that run only when an ECDSA
|
||||||
// signature's R and S values have leading zeros.
|
// signature's R and S values have leading zeros.
|
||||||
g_insecure_rand_ctx = FastRandomContext{uint256{33}};
|
g_insecure_rand_ctx.Reseed(uint256{33});
|
||||||
|
|
||||||
TxOrphanageTest orphanage;
|
TxOrphanageTest orphanage;
|
||||||
CKey key;
|
CKey key;
|
||||||
|
|
|
@ -212,7 +212,7 @@ public:
|
||||||
prevector_tester() {
|
prevector_tester() {
|
||||||
SeedRandomForTest();
|
SeedRandomForTest();
|
||||||
rand_seed = InsecureRand256();
|
rand_seed = InsecureRand256();
|
||||||
rand_cache = FastRandomContext(rand_seed);
|
rand_cache.Reseed(rand_seed);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
|
@ -253,7 +253,7 @@ BOOST_AUTO_TEST_CASE(xoroshiro128plusplus_reference_values)
|
||||||
BOOST_TEST(0x6ea7c59f89bbfc75 == rng());
|
BOOST_TEST(0x6ea7c59f89bbfc75 == rng());
|
||||||
|
|
||||||
// seed with a random number
|
// seed with a random number
|
||||||
rng = InsecureRandomContext(0x1a26f3fa8546b47a);
|
rng.Reseed(0x1a26f3fa8546b47a);
|
||||||
BOOST_TEST(0xc8dc5e08d844ac7d == rng());
|
BOOST_TEST(0xc8dc5e08d844ac7d == rng());
|
||||||
BOOST_TEST(0x5b5f1f6d499dad1b == rng());
|
BOOST_TEST(0x5b5f1f6d499dad1b == rng());
|
||||||
BOOST_TEST(0xbeb0031f93313d6f == rng());
|
BOOST_TEST(0xbeb0031f93313d6f == rng());
|
||||||
|
|
|
@ -34,5 +34,5 @@ void SeedRandomForTest(SeedRand seedtype)
|
||||||
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
|
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
|
||||||
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
|
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
|
||||||
MakeRandDeterministicDANGEROUS(seed);
|
MakeRandDeterministicDANGEROUS(seed);
|
||||||
g_insecure_rand_ctx = FastRandomContext(GetRandHash());
|
g_insecure_rand_ctx.Reseed(GetRandHash());
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue