![]() |
Bitcoin ABC 0.32.5
P2P Digital Currency
|
#include <crypto/chacha20.h>#include <crypto/common.h>#include <span.h>#include <uint256.h>#include <util/check.h>#include <bit>#include <cassert>#include <chrono>#include <concepts>#include <cstdint>#include <limits>#include <type_traits>#include <vector>Go to the source code of this file.
Classes | |
| class | RandomMixin< T > |
| ===================== RANDOM NUMBER GENERATION CLASSES ===================== More... | |
| class | FastRandomContext |
| Fast randomness source. More... | |
| class | InsecureRandomContext |
| xoroshiro128++ PRNG. More... | |
Concepts | |
| concept | RandomNumberGenerator |
| A concept for RandomMixin-based random number generators. | |
| concept | StdChronoDuration |
| A concept for C++ std::chrono durations. | |
Functions | |
| void | RandomInit () |
| Overall design of the RNG and entropy sources. More... | |
| void | RandAddPeriodic () noexcept |
| Gather entropy from various expensive sources, and feed them to the PRNG state. More... | |
| void | RandAddEvent (const uint32_t event_info) noexcept |
| Gathers entropy from the low bits of the time at which events occur. More... | |
| void | GetRandBytes (Span< uint8_t > bytes) noexcept |
| ================== BASE RANDOMNESS GENERATION FUNCTIONS ==================== More... | |
| void | GetStrongRandBytes (Span< uint8_t > bytes) noexcept |
| Gather entropy from various sources, feed it into the internal PRNG, and generate random data using it. More... | |
| double | MakeExponentiallyDistributed (uint64_t uniform) noexcept |
| Given a uniformly random uint64_t, return an exponentially distributed double with mean 1. More... | |
| uint256 | GetRandHash () noexcept |
| ========== CONVENIENCE FUNCTIONS FOR COMMONLY USED RANDOMNESS ========== More... | |
| template<typename I , RandomNumberGenerator R> | |
| void | Shuffle (I first, I last, R &&rng) |
| More efficient than using std::shuffle on a FastRandomContext. More... | |
| bool | Random_SanityCheck () |
| =============== MISCELLANEOUS TEST-ONLY FUNCTIONS ====================== More... | |
|
noexcept |
================== BASE RANDOMNESS GENERATION FUNCTIONS ====================
All produced randomness is eventually generated by one of these functions. 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.
In test mode (see SeedRandomForTest in src/test/util/random.h), the normal PRNG state is bypassed, and a deterministic, seeded, PRNG is used instead.
Thread-safe.
Definition at line 690 of file random.cpp.
|
inlinenoexcept |
|
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.
The normal PRNG is never bypassed here, even in test mode.
Thread-safe.
Definition at line 695 of file random.cpp.
|
noexcept |
Given a uniformly random uint64_t, return an exponentially distributed double with mean 1.
Definition at line 803 of file random.cpp.
|
noexcept |
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.
Definition at line 704 of file random.cpp.
|
noexcept |
Gather entropy from various expensive sources, and feed them to the PRNG state.
Thread-safe.
Definition at line 700 of file random.cpp.
| bool Random_SanityCheck | ( | ) |
=============== MISCELLANEOUS TEST-ONLY FUNCTIONS ======================
Check that OS randomness is available and returning the requested number of bytes.
This does not measure the quality of randomness, but it does test that GetOSRand() overwrites all 32 bytes of the output given a maximum number of tries.
Loop until all bytes have been overwritten at least once, or max number tries reached.
Definition at line 730 of file random.cpp.
| void RandomInit | ( | ) |
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:
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.
During tests, the RNG can be put into a special deterministic mode, in which the output of all RNG functions, with the exception of GetStrongRandBytes(), is replaced with the output of a deterministic RNG. This deterministic RNG does not gather entropy, and is unaffected by RandAddPeriodic() or RandAddEvent(). It produces pseudorandom data that only depends on the seed it was initialized with, possibly until it is reinitialized. ==================== INITIALIZATION AND ADDING ENTROPY ================= 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.
Definition at line 796 of file random.cpp.
| 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
Definition at line 512 of file random.h.