diff options
Diffstat (limited to 'src/random.h')
-rw-r--r-- | src/random.h | 27 |
1 files changed, 8 insertions, 19 deletions
diff --git a/src/random.h b/src/random.h index 5fe20c5f76..49c0dff5bf 100644 --- a/src/random.h +++ b/src/random.h @@ -1,5 +1,5 @@ // Copyright (c) 2009-2010 Satoshi Nakamoto -// Copyright (c) 2009-2020 The Bitcoin Core developers +// Copyright (c) 2009-2022 The Bitcoin Core developers // Distributed under the MIT software license, see the accompanying // file COPYING or http://www.opensource.org/licenses/mit-license.php. @@ -15,6 +15,7 @@ #include <chrono> #include <cstdint> #include <limits> +#include <vector> /** * Overall design of the RNG and entropy sources. @@ -145,23 +146,11 @@ private: bool requires_seed; ChaCha20 rng; - unsigned char bytebuf[64]; - int bytebuf_size; - uint64_t bitbuf; int bitbuf_size; void RandomSeed(); - void FillByteBuffer() - { - if (requires_seed) { - RandomSeed(); - } - rng.Keystream(bytebuf, sizeof(bytebuf)); - bytebuf_size = sizeof(bytebuf); - } - void FillBitBuffer() { bitbuf = rand64(); @@ -185,10 +174,10 @@ public: /** Generate a random 64-bit integer. */ uint64_t rand64() noexcept { - if (bytebuf_size < 8) FillByteBuffer(); - uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size); - bytebuf_size -= 8; - return ret; + if (requires_seed) RandomSeed(); + unsigned char buf[8]; + rng.Keystream(buf, 8); + return ReadLE64(buf); } /** Generate a random (bits)-bit integer. */ @@ -200,7 +189,7 @@ public: return rand64() >> (64 - bits); } else { if (bitbuf_size < bits) FillBitBuffer(); - uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits)); + uint64_t ret = bitbuf & (~uint64_t{0} >> (64 - bits)); bitbuf >>= bits; bitbuf_size -= bits; return ret; @@ -250,7 +239,7 @@ public: /* interval [0..0] */ Dur{0}; }; - // Compatibility with the C++11 UniformRandomBitGenerator concept + // Compatibility with the UniformRandomBitGenerator concept typedef uint64_t result_type; static constexpr uint64_t min() { return 0; } static constexpr uint64_t max() { return std::numeric_limits<uint64_t>::max(); } |