diff options
author | Pieter Wuille <pieter@wuille.net> | 2022-09-21 17:31:54 -0400 |
---|---|---|
committer | Pieter Wuille <pieter@wuille.net> | 2023-01-30 18:12:21 -0500 |
commit | 5d16f757639e2cc6e81db6e07bc1d5dd74abca6c (patch) | |
tree | ed32b081d25bfd639b06aab1b0e0e58b8af5a31b /src/random.cpp | |
parent | 38eaece67b1bc37b2f502348c5d7537480a34346 (diff) | |
download | bitcoin-5d16f757639e2cc6e81db6e07bc1d5dd74abca6c.tar.xz |
Use ChaCha20 caching in FastRandomContext
Diffstat (limited to 'src/random.cpp')
-rw-r--r-- | src/random.cpp | 14 |
1 files changed, 4 insertions, 10 deletions
diff --git a/src/random.cpp b/src/random.cpp index 23ea9ba6b7..32deca9f70 100644 --- a/src/random.cpp +++ b/src/random.cpp @@ -605,12 +605,9 @@ void FastRandomContext::RandomSeed() uint256 FastRandomContext::rand256() noexcept { - if (bytebuf_size < 32) { - FillByteBuffer(); - } + if (requires_seed) RandomSeed(); uint256 ret; - memcpy(ret.begin(), bytebuf + 64 - bytebuf_size, 32); - bytebuf_size -= 32; + rng.Keystream(ret.data(), ret.size()); return ret; } @@ -624,7 +621,7 @@ std::vector<unsigned char> FastRandomContext::randbytes(size_t len) return ret; } -FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bytebuf_size(0), bitbuf_size(0) +FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bitbuf_size(0) { rng.SetKey(seed.begin(), 32); } @@ -675,7 +672,7 @@ bool Random_SanityCheck() return true; } -FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0) +FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), bitbuf_size(0) { if (!fDeterministic) { return; @@ -688,12 +685,9 @@ FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexce { requires_seed = from.requires_seed; rng = from.rng; - std::copy(std::begin(from.bytebuf), std::end(from.bytebuf), std::begin(bytebuf)); - bytebuf_size = from.bytebuf_size; bitbuf = from.bitbuf; bitbuf_size = from.bitbuf_size; from.requires_seed = true; - from.bytebuf_size = 0; from.bitbuf_size = 0; return *this; } |