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.h | |
parent | 38eaece67b1bc37b2f502348c5d7537480a34346 (diff) | |
download | bitcoin-5d16f757639e2cc6e81db6e07bc1d5dd74abca6c.tar.xz |
Use ChaCha20 caching in FastRandomContext
Diffstat (limited to 'src/random.h')
-rw-r--r-- | src/random.h | 20 |
1 files changed, 4 insertions, 16 deletions
diff --git a/src/random.h b/src/random.h index e890e909c7..63f28d3eb2 100644 --- a/src/random.h +++ b/src/random.h @@ -145,23 +145,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 +173,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. */ |