aboutsummaryrefslogtreecommitdiff
path: root/src/random.cpp
diff options
context:
space:
mode:
authorPieter Wuille <pieter@wuille.net>2023-07-18 10:11:49 -0400
committerPieter Wuille <pieter@wuille.net>2023-08-17 15:26:34 -0400
commit3da636e08b781fa2f7c1c23bb937015185732a75 (patch)
tree11908cad2b43f2995362e497a96cb694910b7573 /src/random.cpp
parent6ce5e8f4758e6fccf59b3980af417ec6c75e383e (diff)
crypto: refactor ChaCha20 classes to use Span<std::byte> interface
Diffstat (limited to 'src/random.cpp')
-rw-r--r--src/random.cpp15
1 files changed, 8 insertions, 7 deletions
diff --git a/src/random.cpp b/src/random.cpp
index 5ff6f573b8..ec7ade8ff6 100644
--- a/src/random.cpp
+++ b/src/random.cpp
@@ -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