crypto: refactor ChaCha20 classes to use Span<std::byte> interface

This commit is contained in:
Pieter Wuille
2023-07-18 10:11:49 -04:00
parent 6ce5e8f475
commit 3da636e08b
10 changed files with 200 additions and 178 deletions

View File

@@ -16,6 +16,7 @@
#include <sync.h>
#include <util/time.h>
#include <array>
#include <cmath>
#include <cstdlib>
#include <thread>
@@ -577,7 +578,7 @@ uint256 GetRandHash() noexcept
void FastRandomContext::RandomSeed()
{
uint256 seed = GetRandHash();
rng.SetKey32(seed.begin());
rng.SetKey(MakeByteSpan(seed));
requires_seed = false;
}
@@ -585,7 +586,7 @@ uint256 FastRandomContext::rand256() noexcept
{
if (requires_seed) RandomSeed();
uint256 ret;
rng.Keystream(ret.data(), ret.size());
rng.Keystream(MakeWritableByteSpan(ret));
return ret;
}
@@ -595,7 +596,7 @@ std::vector<B> FastRandomContext::randbytes(size_t len)
if (requires_seed) RandomSeed();
std::vector<B> ret(len);
if (len > 0) {
rng.Keystream(UCharCast(ret.data()), len);
rng.Keystream(MakeWritableByteSpan(ret));
}
return ret;
}
@@ -605,12 +606,12 @@ template std::vector<std::byte> FastRandomContext::randbytes(size_t);
void FastRandomContext::fillrand(Span<std::byte> output)
{
if (requires_seed) RandomSeed();
rng.Keystream(UCharCast(output.data()), output.size());
rng.Keystream(output);
}
FastRandomContext::FastRandomContext(const uint256& seed) noexcept : requires_seed(false), bitbuf_size(0)
{
rng.SetKey32(seed.begin());
rng.SetKey(MakeByteSpan(seed));
}
bool Random_SanityCheck()
@@ -664,8 +665,8 @@ FastRandomContext::FastRandomContext(bool fDeterministic) noexcept : requires_se
if (!fDeterministic) {
return;
}
uint256 seed;
rng.SetKey32(seed.begin());
static constexpr std::array<std::byte, ChaCha20::KEYLEN> ZERO{};
rng.SetKey(ZERO);
}
FastRandomContext& FastRandomContext::operator=(FastRandomContext&& from) noexcept