2014-06-26 14:41:53 +02:00
|
|
|
// Copyright (c) 2009-2010 Satoshi Nakamoto
|
2023-08-16 19:27:31 +02:00
|
|
|
// Copyright (c) 2009-2020 The Bitcoin Core developers
|
2014-12-13 05:09:33 +01:00
|
|
|
// Distributed under the MIT software license, see the accompanying
|
2014-06-26 14:41:53 +02:00
|
|
|
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
|
|
|
|
|
|
|
#ifndef BITCOIN_RANDOM_H
|
|
|
|
#define BITCOIN_RANDOM_H
|
|
|
|
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <crypto/chacha20.h>
|
|
|
|
#include <crypto/common.h>
|
2023-06-26 22:50:21 +02:00
|
|
|
#include <span.h>
|
2020-03-19 23:46:56 +01:00
|
|
|
#include <uint256.h>
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2022-07-26 16:02:13 +02:00
|
|
|
#include <cassert>
|
2020-04-08 14:27:07 +02:00
|
|
|
#include <chrono> // For std::chrono::microseconds
|
|
|
|
#include <cstdint>
|
2018-03-22 17:21:41 +01:00
|
|
|
#include <limits>
|
2023-02-06 19:56:32 +01:00
|
|
|
#include <vector>
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2021-09-11 22:52:36 +02:00
|
|
|
/**
|
|
|
|
* Overall design of the RNG and entropy sources.
|
|
|
|
*
|
|
|
|
* We maintain a single global 256-bit RNG state for all high-quality randomness.
|
|
|
|
* The following (classes of) functions interact with that state by mixing in new
|
|
|
|
* entropy, and optionally extracting random output from it:
|
|
|
|
*
|
|
|
|
* - The GetRand*() class of functions, as well as construction of FastRandomContext objects,
|
|
|
|
* perform 'fast' seeding, consisting of mixing in:
|
|
|
|
* - A stack pointer (indirectly committing to calling thread and call stack)
|
|
|
|
* - A high-precision timestamp (rdtsc when available, c++ high_resolution_clock otherwise)
|
2019-02-18 10:55:34 +01:00
|
|
|
* - 64 bits from the hardware RNG (rdrand) when available.
|
2021-09-11 22:52:36 +02:00
|
|
|
* These entropy sources are very fast, and only designed to protect against situations
|
|
|
|
* where a VM state restore/copy results in multiple systems with the same randomness.
|
|
|
|
* FastRandomContext on the other hand does not protect against this once created, but
|
|
|
|
* is even faster (and acceptable to use inside tight loops).
|
|
|
|
*
|
|
|
|
* - The GetStrongRand*() class of function perform 'slow' seeding, including everything
|
|
|
|
* that fast seeding includes, but additionally:
|
|
|
|
* - OS entropy (/dev/urandom, getrandom(), ...). The application will terminate if
|
|
|
|
* this entropy source fails.
|
|
|
|
* - Another high-precision timestamp (indirectly committing to a benchmark of all the
|
|
|
|
* previous sources).
|
|
|
|
* These entropy sources are slower, but designed to make sure the RNG state contains
|
|
|
|
* fresh data that is unpredictable to attackers.
|
|
|
|
*
|
2019-12-05 15:14:24 +01:00
|
|
|
* - RandAddPeriodic() seeds everything that fast seeding includes, but additionally:
|
|
|
|
* - A high-precision timestamp
|
|
|
|
* - Dynamic environment data (performance monitoring, ...)
|
|
|
|
* - Strengthen the entropy for 10 ms using repeated SHA512.
|
|
|
|
* This is run once every minute.
|
2021-09-11 22:52:36 +02:00
|
|
|
*
|
|
|
|
* On first use of the RNG (regardless of what function is called first), all entropy
|
|
|
|
* sources used in the 'slow' seeder are included, but also:
|
2019-02-18 10:55:34 +01:00
|
|
|
* - 256 bits from the hardware RNG (rdseed or rdrand) when available.
|
2019-12-05 15:14:24 +01:00
|
|
|
* - Dynamic environment data (performance monitoring, ...)
|
|
|
|
* - Static environment data
|
2019-05-18 10:01:21 +02:00
|
|
|
* - Strengthen the entropy for 100 ms using repeated SHA512.
|
2021-09-11 22:52:36 +02:00
|
|
|
*
|
|
|
|
* When mixing in new entropy, H = SHA512(entropy || old_rng_state) is computed, and
|
|
|
|
* (up to) the first 32 bytes of H are produced as output, while the last 32 bytes
|
|
|
|
* become the new RNG state.
|
|
|
|
*/
|
2014-06-26 14:41:53 +02:00
|
|
|
|
|
|
|
/**
|
2021-09-11 22:52:36 +02:00
|
|
|
* Generate random data via the internal PRNG.
|
|
|
|
*
|
|
|
|
* These functions are designed to be fast (sub microsecond), but do not necessarily
|
|
|
|
* meaningfully add entropy to the PRNG state.
|
|
|
|
*
|
|
|
|
* Thread-safe.
|
2014-06-26 14:41:53 +02:00
|
|
|
*/
|
2021-09-11 22:52:36 +02:00
|
|
|
void GetRandBytes(unsigned char* buf, int num) noexcept;
|
2020-05-15 14:58:42 +02:00
|
|
|
/** Generate a uniform random integer in the range [0..range). Precondition: range > 0 */
|
2021-09-11 22:52:36 +02:00
|
|
|
uint64_t GetRand(uint64_t nMax) noexcept;
|
2020-05-15 14:58:42 +02:00
|
|
|
/** Generate a uniform random duration in the range [0..max). Precondition: max.count() > 0 */
|
|
|
|
template <typename D>
|
|
|
|
D GetRandomDuration(typename std::common_type<D>::type max) noexcept
|
|
|
|
// Having the compiler infer the template argument from the function argument
|
|
|
|
// is dangerous, because the desired return value generally has a different
|
|
|
|
// type than the function argument. So std::common_type is used to force the
|
|
|
|
// call site to specify the type of the return value.
|
|
|
|
{
|
|
|
|
assert(max.count() > 0);
|
|
|
|
return D{GetRand(max.count())};
|
|
|
|
};
|
|
|
|
constexpr auto GetRandMicros = GetRandomDuration<std::chrono::microseconds>;
|
|
|
|
constexpr auto GetRandMillis = GetRandomDuration<std::chrono::milliseconds>;
|
2021-09-11 22:52:36 +02:00
|
|
|
int GetRandInt(int nMax) noexcept;
|
|
|
|
uint256 GetRandHash() noexcept;
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2019-01-09 07:30:19 +01:00
|
|
|
bool GetRandBool(double rate);
|
|
|
|
|
2017-05-23 17:53:00 +02:00
|
|
|
/**
|
2021-09-11 22:52:36 +02:00
|
|
|
* Gather entropy from various sources, feed it into the internal PRNG, and
|
|
|
|
* generate random data using it.
|
|
|
|
*
|
|
|
|
* This function will cause failure whenever the OS RNG fails.
|
|
|
|
*
|
|
|
|
* Thread-safe.
|
2017-05-23 17:53:00 +02:00
|
|
|
*/
|
2021-09-11 22:52:36 +02:00
|
|
|
void GetStrongRandBytes(unsigned char* buf, int num) noexcept;
|
2017-05-23 17:53:00 +02:00
|
|
|
|
2016-05-30 15:46:16 +02:00
|
|
|
/**
|
2022-04-25 11:59:51 +02:00
|
|
|
* Gather entropy from various expensive sources, and feed them to the PRNG state.
|
2021-09-11 22:52:36 +02:00
|
|
|
*
|
|
|
|
* Thread-safe.
|
2016-05-30 15:46:16 +02:00
|
|
|
*/
|
2019-12-05 15:14:24 +01:00
|
|
|
void RandAddPeriodic() noexcept;
|
2016-05-30 15:46:16 +02:00
|
|
|
|
2019-12-04 13:10:58 +01:00
|
|
|
/**
|
|
|
|
* Gathers entropy from the low bits of the time at which events occur. Should
|
|
|
|
* be called with a uint32_t describing the event at the time an event occurs.
|
|
|
|
*
|
|
|
|
* Thread-safe.
|
|
|
|
*/
|
2019-12-05 21:55:22 +01:00
|
|
|
void RandAddEvent(const uint32_t event_info) noexcept;
|
2019-12-04 13:10:58 +01:00
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
/**
|
2016-10-18 15:38:44 +02:00
|
|
|
* Fast randomness source. This is seeded once with secure random data, but
|
2021-09-11 22:52:36 +02:00
|
|
|
* is completely deterministic and does not gather more entropy after that.
|
|
|
|
*
|
2016-10-18 15:38:44 +02:00
|
|
|
* This class is not thread-safe.
|
2014-06-26 14:41:53 +02:00
|
|
|
*/
|
2022-06-13 20:29:08 +02:00
|
|
|
class FastRandomContext
|
|
|
|
{
|
2017-04-24 14:02:12 +02:00
|
|
|
private:
|
|
|
|
bool requires_seed;
|
|
|
|
ChaCha20 rng;
|
|
|
|
|
|
|
|
uint64_t bitbuf;
|
|
|
|
int bitbuf_size;
|
|
|
|
|
|
|
|
void RandomSeed();
|
|
|
|
|
|
|
|
void FillBitBuffer()
|
|
|
|
{
|
|
|
|
bitbuf = rand64();
|
|
|
|
bitbuf_size = 64;
|
|
|
|
}
|
|
|
|
|
2016-10-18 15:38:44 +02:00
|
|
|
public:
|
2021-09-11 22:52:36 +02:00
|
|
|
explicit FastRandomContext(bool fDeterministic = false) noexcept;
|
2017-04-24 14:02:12 +02:00
|
|
|
|
|
|
|
/** Initialize with explicit seed (only for testing) */
|
2021-09-11 22:52:36 +02:00
|
|
|
explicit FastRandomContext(const uint256& seed) noexcept;
|
2017-04-24 14:02:12 +02:00
|
|
|
|
2018-12-13 13:43:12 +01:00
|
|
|
// Do not permit copying a FastRandomContext (move it, or create a new one to get reseeded).
|
|
|
|
FastRandomContext(const FastRandomContext&) = delete;
|
|
|
|
FastRandomContext(FastRandomContext&&) = delete;
|
|
|
|
FastRandomContext& operator=(const FastRandomContext&) = delete;
|
|
|
|
|
|
|
|
/** Move a FastRandomContext. If the original one is used again, it will be reseeded. */
|
|
|
|
FastRandomContext& operator=(FastRandomContext&& from) noexcept;
|
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random 64-bit integer. */
|
2021-09-11 22:52:36 +02:00
|
|
|
uint64_t rand64() noexcept
|
2017-04-24 14:02:12 +02:00
|
|
|
{
|
Merge bitcoin/bitcoin#26153: Reduce wasted pseudorandom bytes in ChaCha20 + various improvements
511aa4f1c7508f15cab8d7e58007900ad6fd3d5d Add unit test for ChaCha20's new caching (Pieter Wuille)
fb243d25f754da8f01793b41e2d225b917f3e5d7 Improve test vectors for ChaCha20 (Pieter Wuille)
93aee8bbdad808b7009279b67470d496cc26b936 Inline ChaCha20 32-byte specific constants (Pieter Wuille)
62ec713961ade7b58e90c905395558a41e8a59f0 Only support 32-byte keys in ChaCha20{,Aligned} (Pieter Wuille)
f21994a02e1cc46d41995581b54222abc655be93 Use ChaCha20Aligned in MuHash3072 code (Pieter Wuille)
5d16f757639e2cc6e81db6e07bc1d5dd74abca6c Use ChaCha20 caching in FastRandomContext (Pieter Wuille)
38eaece67b1bc37b2f502348c5d7537480a34346 Add fuzz test for testing that ChaCha20 works as a stream (Pieter Wuille)
5f05b27841af0bed1b6e7de5f46ffe33e5919e4d Add xoroshiro128++ PRNG (Martin Leitner-Ankerl)
12ff72476ac0dbf8add736ad3fb5fad2eeab156c Make unrestricted ChaCha20 cipher not waste keystream bytes (Pieter Wuille)
6babf402130a8f3ef3058594750aeaa50b8f5044 Rename ChaCha20::Seek -> Seek64 to clarify multiple of 64 (Pieter Wuille)
e37bcaa0a6dbb334ab6e817efcb609ccee6edc39 Split 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 reACK 511aa4f1c7508f15cab8d7e58007900ad6fd3d5d
dhruv:
tACK crACK 511aa4f1c7
Tree-SHA512: 3aa80971322a93e780c75a8d35bd39da3a9ea570fbae4491eaf0c45242f5f670a24a592c50ad870d5fd09b9f88ec06e274e8aa3cefd9561d623c63f7198cf2c7
2023-02-15 15:51:38 +01:00
|
|
|
if (requires_seed) RandomSeed();
|
|
|
|
unsigned char buf[8];
|
|
|
|
rng.Keystream(buf, 8);
|
|
|
|
return ReadLE64(buf);
|
2017-04-24 14:02:12 +02:00
|
|
|
}
|
2014-06-26 14:41:53 +02:00
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random (bits)-bit integer. */
|
2022-06-13 20:29:08 +02:00
|
|
|
uint64_t randbits(int bits) noexcept
|
|
|
|
{
|
2017-04-24 14:02:12 +02:00
|
|
|
if (bits == 0) {
|
|
|
|
return 0;
|
|
|
|
} else if (bits > 32) {
|
|
|
|
return rand64() >> (64 - bits);
|
|
|
|
} else {
|
|
|
|
if (bitbuf_size < bits) FillBitBuffer();
|
|
|
|
uint64_t ret = bitbuf & (~(uint64_t)0 >> (64 - bits));
|
|
|
|
bitbuf >>= bits;
|
|
|
|
bitbuf_size -= bits;
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-06-13 20:29:08 +02:00
|
|
|
/** Generate a random integer in the range [0..range).
|
|
|
|
* Precondition: range > 0.
|
|
|
|
*/
|
2021-09-11 22:52:36 +02:00
|
|
|
uint64_t randrange(uint64_t range) noexcept
|
2017-04-24 14:02:12 +02:00
|
|
|
{
|
Merge #17293: Add assertion to randrange that input is not 0
a35b6824f3a0bdb68c5aef599c0f17562689970e Add assertion to randrange that input is not 0 (Jeremy Rubin)
Pull request description:
From the comment in randrange, their is an implicit argument that randrange cannot accept an argument of 0. If the argument is 0, then we have to return {}, which is not possible in a uint64_t.
The current code takes a very interesting approach, which is to return [0..std::numeric_limits<uint64_t>]. This can cause all sorts of fun problems, like allocating a lot of memory, accessing random memory (maybe with your private keys), and crashing the computer entirely.
This gives us three choices of how to make it "safe":
1) return Optional<uint64_t>
2) Change the return type to [0..range]
3) Return 0 if 0
4) Assert(range)
So which solution is best?
1) seems a bit overkill, as it makes any code using randrange worse.
2) Changing the return type as in 2 could be acceptable, but it imposes the potential overflow checking on the caller (which is what we want).
3) An interesting option -- effective makes the return type in {0} U [0..range]. But this is a bad choice, because it leads to code like `vec[randrange(vec.size())]`, which is incorrect for an empty vector. Null set should mean null set.
4) Assert(range) stands out as the best mitigation for now, with perhaps a future change to solution 2. It prevents the error from propagating at the earliest possible time, so the program crashes cleanly rather than by freezing the computer or accessing random memory.
ACKs for top commit:
instagibbs:
Seems reasonable for now, ACK https://github.com/bitcoin/bitcoin/pull/17293/commits/a35b6824f3a0bdb68c5aef599c0f17562689970e
laanwj:
ACK a35b6824f3a0bdb68c5aef599c0f17562689970e
promag:
ACK a35b6824f3a0bdb68c5aef599c0f17562689970e.
Tree-SHA512: 8fc626cde4b04b918100cb7af28753f25ec697bd077ce0e0c640be0357626322aeea233e3c8fd964ba1564b0fda830b7f5188310ebbb119c113513a4b89952dc
2019-11-02 11:40:40 +01:00
|
|
|
assert(range);
|
2017-04-24 14:02:12 +02:00
|
|
|
--range;
|
|
|
|
int bits = CountBits(range);
|
|
|
|
while (true) {
|
|
|
|
uint64_t ret = randbits(bits);
|
|
|
|
if (ret <= range) return ret;
|
|
|
|
}
|
2016-10-18 15:38:44 +02:00
|
|
|
}
|
|
|
|
|
2018-01-12 16:37:39 +01:00
|
|
|
uint32_t rand32(uint32_t nMax) {
|
|
|
|
return rand32() % nMax;
|
|
|
|
}
|
2016-11-20 07:52:45 +01:00
|
|
|
|
2018-01-12 16:37:39 +01:00
|
|
|
uint32_t operator()(uint32_t nMax) {
|
|
|
|
return rand32(nMax);
|
2016-11-20 07:52:45 +01:00
|
|
|
}
|
2018-01-12 16:37:39 +01:00
|
|
|
|
2017-06-07 23:59:41 +02:00
|
|
|
/** Generate random bytes. */
|
|
|
|
std::vector<unsigned char> randbytes(size_t len);
|
|
|
|
|
2023-06-26 22:50:21 +02:00
|
|
|
/** Fill a byte Span with random bytes. */
|
|
|
|
void fillrand(Span<std::byte> output);
|
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random 32-bit integer. */
|
2021-09-11 22:52:36 +02:00
|
|
|
uint32_t rand32() noexcept { return randbits(32); }
|
2017-04-24 14:02:12 +02:00
|
|
|
|
2017-06-07 23:59:41 +02:00
|
|
|
/** generate a random uint256. */
|
2021-09-11 22:52:36 +02:00
|
|
|
uint256 rand256() noexcept;
|
2017-06-07 23:59:41 +02:00
|
|
|
|
2017-04-24 14:02:12 +02:00
|
|
|
/** Generate a random boolean. */
|
2021-09-11 22:52:36 +02:00
|
|
|
bool randbool() noexcept { return randbits(1); }
|
2018-03-22 17:21:41 +01:00
|
|
|
|
2023-07-19 19:58:56 +02:00
|
|
|
/** Return the time point advanced by a uniform random duration. */
|
|
|
|
template <typename Tp>
|
|
|
|
Tp rand_uniform_delay(const Tp& time, typename Tp::duration range)
|
|
|
|
{
|
2022-07-26 16:02:13 +02:00
|
|
|
return time + rand_uniform_duration<Tp>(range);
|
2023-07-19 19:58:56 +02:00
|
|
|
}
|
|
|
|
|
2022-07-26 16:02:13 +02:00
|
|
|
/** Generate a uniform random duration in the range from 0 (inclusive) to range (exclusive). */
|
|
|
|
template <typename Chrono>
|
|
|
|
typename Chrono::duration rand_uniform_duration(typename Chrono::duration range) noexcept
|
|
|
|
{
|
|
|
|
using Dur = typename Chrono::duration;
|
|
|
|
return range.count() > 0 ? /* interval [0..range) */ Dur{randrange(range.count())} :
|
|
|
|
range.count() < 0 ? /* interval (range..0] */ -Dur{randrange(-range.count())} :
|
|
|
|
/* interval [0..0] */ Dur{0};
|
|
|
|
};
|
|
|
|
|
2018-03-22 17:21:41 +01:00
|
|
|
// Compatibility with the C++11 UniformRandomBitGenerator concept
|
|
|
|
typedef uint64_t result_type;
|
|
|
|
static constexpr uint64_t min() { return 0; }
|
|
|
|
static constexpr uint64_t max() { return std::numeric_limits<uint64_t>::max(); }
|
2021-09-11 22:52:36 +02:00
|
|
|
inline uint64_t operator()() noexcept { return rand64(); }
|
2016-11-20 07:52:45 +01:00
|
|
|
};
|
2016-10-09 13:46:46 +02:00
|
|
|
|
2021-01-14 20:46:16 +01:00
|
|
|
/** More efficient than using std::shuffle on a FastRandomContext.
|
|
|
|
*
|
|
|
|
* This is more efficient as std::shuffle will consume entropy in groups of
|
|
|
|
* 64 bits at the time and throw away most.
|
|
|
|
*
|
|
|
|
* This also works around a bug in libstdc++ std::shuffle that may cause
|
|
|
|
* type::operator=(type&&) to be invoked on itself, which the library's
|
|
|
|
* debug mode detects and panics on. This is a known issue, see
|
|
|
|
* https://stackoverflow.com/questions/22915325/avoiding-self-assignment-in-stdshuffle
|
|
|
|
*/
|
2022-06-13 20:29:08 +02:00
|
|
|
template <typename I, typename R>
|
2021-01-14 20:46:16 +01:00
|
|
|
void Shuffle(I first, I last, R&& rng)
|
|
|
|
{
|
|
|
|
while (first != last) {
|
|
|
|
size_t j = rng.randrange(last - first);
|
|
|
|
if (j) {
|
|
|
|
using std::swap;
|
|
|
|
swap(*first, *(first + j));
|
|
|
|
}
|
|
|
|
++first;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-01 12:40:06 +01:00
|
|
|
/* Number of random bytes returned by GetOSRand.
|
|
|
|
* When changing this constant make sure to change all call sites, and make
|
|
|
|
* sure that the underlying OS APIs for all platforms support the number.
|
|
|
|
* (many cap out at 256 bytes).
|
|
|
|
*/
|
2017-12-13 13:32:00 +01:00
|
|
|
static const int NUM_OS_RANDOM_BYTES = 32;
|
2017-03-01 12:40:06 +01:00
|
|
|
|
|
|
|
/** Get 32 bytes of system entropy. Do not use this in application code: use
|
|
|
|
* GetStrongRandBytes instead.
|
|
|
|
*/
|
2022-06-13 20:29:08 +02:00
|
|
|
void GetOSRand(unsigned char* ent32);
|
2017-03-01 12:40:06 +01:00
|
|
|
|
|
|
|
/** Check that OS randomness is available and returning the requested number
|
|
|
|
* of bytes.
|
|
|
|
*/
|
|
|
|
bool Random_SanityCheck();
|
|
|
|
|
2021-09-11 22:52:36 +02:00
|
|
|
/**
|
|
|
|
* Initialize global RNG state and log any CPU features that are used.
|
|
|
|
*
|
|
|
|
* Calling this function is optional. RNG state will be initialized when first
|
|
|
|
* needed if it is not called.
|
|
|
|
*/
|
2017-06-14 15:22:08 +02:00
|
|
|
void RandomInit();
|
|
|
|
|
2014-06-26 14:41:53 +02:00
|
|
|
#endif // BITCOIN_RANDOM_H
|