random: add a few noexcepts to FastRandomContext

This commit is contained in:
Pieter Wuille 2024-06-08 07:57:47 -04:00
parent b3b382dde2
commit 27cefc7fd6
2 changed files with 9 additions and 9 deletions

View file

@ -652,14 +652,14 @@ uint256 GetRandHash() noexcept
return hash; return hash;
} }
void FastRandomContext::RandomSeed() void FastRandomContext::RandomSeed() noexcept
{ {
uint256 seed = GetRandHash(); uint256 seed = GetRandHash();
rng.SetKey(MakeByteSpan(seed)); rng.SetKey(MakeByteSpan(seed));
requires_seed = false; requires_seed = false;
} }
void FastRandomContext::fillrand(Span<std::byte> output) void FastRandomContext::fillrand(Span<std::byte> output) noexcept
{ {
if (requires_seed) RandomSeed(); if (requires_seed) RandomSeed();
rng.Keystream(output); rng.Keystream(output);

View file

@ -150,9 +150,9 @@ private:
uint64_t bitbuf; uint64_t bitbuf;
int bitbuf_size; int bitbuf_size;
void RandomSeed(); void RandomSeed() noexcept;
void FillBitBuffer() void FillBitBuffer() noexcept
{ {
bitbuf = rand64(); bitbuf = rand64();
bitbuf_size = 64; bitbuf_size = 64;
@ -213,7 +213,7 @@ public:
/** Generate random bytes. */ /** Generate random bytes. */
template <typename B = unsigned char> template <typename B = unsigned char>
std::vector<B> randbytes(size_t len) std::vector<B> randbytes(size_t len) noexcept
{ {
std::vector<B> ret(len); std::vector<B> ret(len);
fillrand(MakeWritableByteSpan(ret)); fillrand(MakeWritableByteSpan(ret));
@ -221,7 +221,7 @@ public:
} }
/** Fill a byte Span with random bytes. */ /** Fill a byte Span with random bytes. */
void fillrand(Span<std::byte> output); void fillrand(Span<std::byte> output) noexcept;
/** Generate a random 32-bit integer. */ /** Generate a random 32-bit integer. */
uint32_t rand32() noexcept { return randbits(32); } uint32_t rand32() noexcept { return randbits(32); }
@ -239,7 +239,7 @@ public:
/** Return the time point advanced by a uniform random duration. */ /** Return the time point advanced by a uniform random duration. */
template <typename Tp> template <typename Tp>
Tp rand_uniform_delay(const Tp& time, typename Tp::duration range) Tp rand_uniform_delay(const Tp& time, typename Tp::duration range) noexcept
{ {
return time + rand_uniform_duration<Tp>(range); return time + rand_uniform_duration<Tp>(range);
} }
@ -256,8 +256,8 @@ public:
// Compatibility with the UniformRandomBitGenerator concept // Compatibility with the UniformRandomBitGenerator concept
typedef uint64_t result_type; typedef uint64_t result_type;
static constexpr uint64_t min() { return 0; } static constexpr uint64_t min() noexcept { return 0; }
static constexpr uint64_t max() { return std::numeric_limits<uint64_t>::max(); } static constexpr uint64_t max() noexcept { return std::numeric_limits<uint64_t>::max(); }
inline uint64_t operator()() noexcept { return rand64(); } inline uint64_t operator()() noexcept { return rand64(); }
}; };