mirror of
https://github.com/bitcoin/bitcoin.git
synced 2025-11-10 22:18:54 +01:00
Merge bitcoin/bitcoin#26153: Reduce wasted pseudorandom bytes in ChaCha20 + various improvements
511aa4f1c7Add unit test for ChaCha20's new caching (Pieter Wuille)fb243d25f7Improve test vectors for ChaCha20 (Pieter Wuille)93aee8bbdaInline ChaCha20 32-byte specific constants (Pieter Wuille)62ec713961Only support 32-byte keys in ChaCha20{,Aligned} (Pieter Wuille)f21994a02eUse ChaCha20Aligned in MuHash3072 code (Pieter Wuille)5d16f75763Use ChaCha20 caching in FastRandomContext (Pieter Wuille)38eaece67bAdd fuzz test for testing that ChaCha20 works as a stream (Pieter Wuille)5f05b27841Add xoroshiro128++ PRNG (Martin Leitner-Ankerl)12ff72476aMake unrestricted ChaCha20 cipher not waste keystream bytes (Pieter Wuille)6babf40213Rename ChaCha20::Seek -> Seek64 to clarify multiple of 64 (Pieter Wuille)e37bcaa0a6Split ChaCha20 into aligned/unaligned variants (Pieter Wuille) Pull request description: This is an alternative to #25354 (by my benchmarking, somewhat faster), subsumes #25712, and adds additional test vectors. It separates the multiple-of-64-bytes-only "core" logic (which becomes simpler) from a layer around which performs caching/slicing to support arbitrary byte amounts. Both have their uses (in particular, the MuHash3072 code can benefit from multiple-of-64-bytes assumptions), plus the separation results in more readable code. Also, since FastRandomContext effectively had its own (more naive) caching on top of ChaCha20, that can be dropped in favor of ChaCha20's new built-in caching. I thought about rebasing #25712 on top of this, but the changes before are fairly extensive, so redid it instead. ACKs for top commit: ajtowns: ut reACK511aa4f1c7dhruv: tACK crACK511aa4f1c7Tree-SHA512: 3aa80971322a93e780c75a8d35bd39da3a9ea570fbae4491eaf0c45242f5f670a24a592c50ad870d5fd09b9f88ec06e274e8aa3cefd9561d623c63f7198cf2c7
This commit is contained in:
20
src/random.h
20
src/random.h
@@ -146,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();
|
||||
@@ -186,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. */
|
||||
|
||||
Reference in New Issue
Block a user