Bitcoin
|
#include <crypto/chacha20.h>
#include <crypto/common.h>
#include <uint256.h>
#include <stdint.h>
#include <limits>
Go to the source code of this file.
Classes | |
class | FastRandomContext |
Functions | |
void | GetRandBytes (unsigned char *buf, int num) noexcept |
uint64_t | GetRand (uint64_t nMax) noexcept |
int | GetRandInt (int nMax) noexcept |
uint256 | GetRandHash () noexcept |
void | GetStrongRandBytes (unsigned char *buf, int num) noexcept |
void | RandAddSeedSleep () |
template<typename I , typename R > | |
void | Shuffle (I first, I last, R &&rng) |
void | GetOSRand (unsigned char *ent32) |
bool | Random_SanityCheck () |
void | RandomInit () |
Variables | |
static const int | NUM_OS_RANDOM_BYTES = 32 |
void GetOSRand | ( | unsigned char * | ent32 | ) |
Get 32 bytes of system entropy. Do not use this in application code: use GetStrongRandBytes instead.
Get 32 bytes of system entropy.
|
noexcept |
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:
- Once every minute, strengthen the entropy for 10 ms using repeated SHA512. These just exploit the fact the system is idle to improve the quality of the RNG slightly.
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:
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.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.
|
noexcept |
|
noexcept |
|
noexcept |
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.
void RandAddSeedSleep | ( | ) |
Sleep for 1ms, gather entropy from various sources, and feed them to the PRNG state.
Thread-safe.
bool Random_SanityCheck | ( | ) |
Check that OS randomness is available and returning the requested number of bytes.
void RandomInit | ( | ) |
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.
void Shuffle | ( | I | first, |
I | last, | ||
R && | rng | ||
) |
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
|
static |