Skip to content

Commit ce80942

Browse files
committed
random: replace construct/assign with explicit Reseed()
1 parent 2ae392d commit ce80942

File tree

7 files changed

+31
-46
lines changed

7 files changed

+31
-46
lines changed

src/random.cpp

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -704,6 +704,13 @@ void FastRandomContext::fillrand(Span<std::byte> output) noexcept
704704

705705
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), rng(MakeByteSpan(seed)) {}
706706

707+
void FastRandomContext::Reseed(const uint256& seed) noexcept
708+
{
709+
FlushCache();
710+
requires_seed = false;
711+
rng = {MakeByteSpan(seed)};
712+
}
713+
707714
bool Random_SanityCheck()
708715
{
709716
uint64_t start = GetPerformanceCounter();
@@ -759,15 +766,6 @@ FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_se
759766
// use.
760767
}
761768

762-
FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexcept
763-
{
764-
requires_seed = from.requires_seed;
765-
rng = from.rng;
766-
from.requires_seed = true;
767-
static_cast<RandomMixin<FastRandomContext>&>(*this) = std::move(from);
768-
return *this;
769-
}
770-
771769
void RandomInit()
772770
{
773771
// Invoke RNG code to trigger initialization (if not already performed)

src/random.h

Lines changed: 19 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -184,27 +184,21 @@ class RandomMixin
184184
*/
185185
RandomNumberGenerator auto& Impl() noexcept { return static_cast<T&>(*this); }
186186

187+
protected:
188+
constexpr void FlushCache() noexcept
189+
{
190+
bitbuf = 0;
191+
bitbuf_size = 0;
192+
}
193+
187194
public:
188-
RandomMixin() noexcept = default;
195+
constexpr RandomMixin() noexcept = default;
189196

190-
// Do not permit copying an RNG.
197+
// Do not permit copying or moving an RNG.
191198
RandomMixin(const RandomMixin&) = delete;
192199
RandomMixin& operator=(const RandomMixin&) = delete;
193-
194-
RandomMixin(RandomMixin&& other) noexcept : bitbuf(other.bitbuf), bitbuf_size(other.bitbuf_size)
195-
{
196-
other.bitbuf = 0;
197-
other.bitbuf_size = 0;
198-
}
199-
200-
RandomMixin& operator=(RandomMixin&& other) noexcept
201-
{
202-
bitbuf = other.bitbuf;
203-
bitbuf_size = other.bitbuf_size;
204-
other.bitbuf = 0;
205-
other.bitbuf_size = 0;
206-
return *this;
207-
}
200+
RandomMixin(RandomMixin&&) = delete;
201+
RandomMixin& operator=(RandomMixin&&) = delete;
208202

209203
/** Generate a random (bits)-bit integer. */
210204
uint64_t randbits(int bits) noexcept
@@ -394,13 +388,8 @@ class FastRandomContext : public RandomMixin<FastRandomContext>
394388
/** Initialize with explicit seed (only for testing) */
395389
explicit FastRandomContext(const uint256& seed) noexcept;
396390

397-
// Do not permit copying a FastRandomContext (move it, or create a new one to get reseeded).
398-
FastRandomContext(const FastRandomContext&) = delete;
399-
FastRandomContext(FastRandomContext&&) = delete;
400-
FastRandomContext& operator=(const FastRandomContext&) = delete;
401-
402-
/** Move a FastRandomContext. If the original one is used again, it will be reseeded. */
403-
FastRandomContext& operator=(FastRandomContext&& from) noexcept;
391+
/** Reseed with explicit seed (only for testing). */
392+
void Reseed(const uint256& seed) noexcept;
404393

405394
/** Generate a random 64-bit integer. */
406395
uint64_t rand64() noexcept
@@ -440,14 +429,12 @@ class InsecureRandomContext : public RandomMixin<InsecureRandomContext>
440429
constexpr explicit InsecureRandomContext(uint64_t seedval) noexcept
441430
: m_s0(SplitMix64(seedval)), m_s1(SplitMix64(seedval)) {}
442431

443-
// no copy - that is dangerous, we don't want accidentally copy the RNG and then have two streams
444-
// with exactly the same results.
445-
InsecureRandomContext(const InsecureRandomContext&) = delete;
446-
InsecureRandomContext& operator=(const InsecureRandomContext&) = delete;
447-
448-
// allow moves
449-
InsecureRandomContext(InsecureRandomContext&&) = default;
450-
InsecureRandomContext& operator=(InsecureRandomContext&&) = default;
432+
constexpr void Reseed(uint64_t seedval) noexcept
433+
{
434+
FlushCache();
435+
m_s0 = SplitMix64(seedval);
436+
m_s1 = SplitMix64(seedval);
437+
}
451438

452439
constexpr uint64_t rand64() noexcept
453440
{

src/test/fuzz/addrman.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ class AddrManDeterministic : public AddrMan
124124
explicit AddrManDeterministic(const NetGroupManager& netgroupman, FuzzedDataProvider& fuzzed_data_provider)
125125
: AddrMan(netgroupman, /*deterministic=*/true, GetCheckRatio())
126126
{
127-
WITH_LOCK(m_impl->cs, m_impl->insecure_rand = FastRandomContext{ConsumeUInt256(fuzzed_data_provider)});
127+
WITH_LOCK(m_impl->cs, m_impl->insecure_rand.Reseed(ConsumeUInt256(fuzzed_data_provider)));
128128
}
129129

130130
/**

src/test/orphanage_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ BOOST_AUTO_TEST_CASE(DoS_mapOrphans)
106106
// ecdsa_signature_parse_der_lax are executed during this test.
107107
// Specifically branches that run only when an ECDSA
108108
// signature's R and S values have leading zeros.
109-
g_insecure_rand_ctx = FastRandomContext{uint256{33}};
109+
g_insecure_rand_ctx.Reseed(uint256{33});
110110

111111
TxOrphanageTest orphanage;
112112
CKey key;

src/test/prevector_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@ class prevector_tester {
212212
prevector_tester() {
213213
SeedRandomForTest();
214214
rand_seed = InsecureRand256();
215-
rand_cache = FastRandomContext(rand_seed);
215+
rand_cache.Reseed(rand_seed);
216216
}
217217
};
218218

src/test/random_tests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,7 @@ BOOST_AUTO_TEST_CASE(xoroshiro128plusplus_reference_values)
253253
BOOST_TEST(0x6ea7c59f89bbfc75 == rng());
254254

255255
// seed with a random number
256-
rng = InsecureRandomContext(0x1a26f3fa8546b47a);
256+
rng.Reseed(0x1a26f3fa8546b47a);
257257
BOOST_TEST(0xc8dc5e08d844ac7d == rng());
258258
BOOST_TEST(0x5b5f1f6d499dad1b == rng());
259259
BOOST_TEST(0xbeb0031f93313d6f == rng());

src/test/util/random.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,5 +34,5 @@ void SeedRandomForTest(SeedRand seedtype)
3434
const uint256& seed{seedtype == SeedRand::SEED ? ctx_seed : uint256::ZERO};
3535
LogPrintf("%s: Setting random seed for current tests to %s=%s\n", __func__, RANDOM_CTX_SEED, seed.GetHex());
3636
MakeRandDeterministicDANGEROUS(seed);
37-
g_insecure_rand_ctx = FastRandomContext(GetRandHash());
37+
g_insecure_rand_ctx.Reseed(GetRandHash());
3838
}

0 commit comments

Comments
 (0)