Skip to content

Commit 5d16f75

Browse files
committed
Use ChaCha20 caching in FastRandomContext
1 parent 38eaece commit 5d16f75

File tree

2 files changed

+8
-26
lines changed

2 files changed

+8
-26
lines changed

src/random.cpp

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -605,12 +605,9 @@ void FastRandomContext::RandomSeed()
605605

606606
uint256 FastRandomContext::rand256() noexcept
607607
{
608-
if (bytebuf_size < 32) {
609-
FillByteBuffer();
610-
}
608+
if (requires_seed) RandomSeed();
611609
uint256 ret;
612-
memcpy(ret.begin(), bytebuf + 64 - bytebuf_size, 32);
613-
bytebuf_size -= 32;
610+
rng.Keystream(ret.data(), ret.size());
614611
return ret;
615612
}
616613

@@ -624,7 +621,7 @@ std::vector<unsigned char> FastRandomContext::randbytes(size_t len)
624621
return ret;
625622
}
626623

627-
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bytebuf_size(0), bitbuf_size(0)
624+
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bitbuf_size(0)
628625
{
629626
rng.SetKey(seed.begin(), 32);
630627
}
@@ -675,7 +672,7 @@ bool Random_SanityCheck()
675672
return true;
676673
}
677674

678-
FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), bytebuf_size(0), bitbuf_size(0)
675+
FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_seed(!fDeterministic), bitbuf_size(0)
679676
{
680677
if (!fDeterministic) {
681678
return;
@@ -688,12 +685,9 @@ FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexce
688685
{
689686
requires_seed = from.requires_seed;
690687
rng = from.rng;
691-
std::copy(std::begin(from.bytebuf), std::end(from.bytebuf), std::begin(bytebuf));
692-
bytebuf_size = from.bytebuf_size;
693688
bitbuf = from.bitbuf;
694689
bitbuf_size = from.bitbuf_size;
695690
from.requires_seed = true;
696-
from.bytebuf_size = 0;
697691
from.bitbuf_size = 0;
698692
return *this;
699693
}

src/random.h

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -145,23 +145,11 @@ class FastRandomContext
145145
bool requires_seed;
146146
ChaCha20 rng;
147147

148-
unsigned char bytebuf[64];
149-
int bytebuf_size;
150-
151148
uint64_t bitbuf;
152149
int bitbuf_size;
153150

154151
void RandomSeed();
155152

156-
void FillByteBuffer()
157-
{
158-
if (requires_seed) {
159-
RandomSeed();
160-
}
161-
rng.Keystream(bytebuf, sizeof(bytebuf));
162-
bytebuf_size = sizeof(bytebuf);
163-
}
164-
165153
void FillBitBuffer()
166154
{
167155
bitbuf = rand64();
@@ -185,10 +173,10 @@ class FastRandomContext
185173
/** Generate a random 64-bit integer. */
186174
uint64_t rand64() noexcept
187175
{
188-
if (bytebuf_size < 8) FillByteBuffer();
189-
uint64_t ret = ReadLE64(bytebuf + 64 - bytebuf_size);
190-
bytebuf_size -= 8;
191-
return ret;
176+
if (requires_seed) RandomSeed();
177+
unsigned char buf[8];
178+
rng.Keystream(buf, 8);
179+
return ReadLE64(buf);
192180
}
193181

194182
/** Generate a random (bits)-bit integer. */

0 commit comments

Comments
 (0)