Use ChaCha20 caching in FastRandomContext

This commit is contained in:
Pieter Wuille
2022-09-21 17:31:54 -04:00
parent 38eaece67b
commit 5d16f75763
2 changed files with 8 additions and 26 deletions

View File

@@ -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. */