Bitcoin ABC 0.26.3
P2P Digital Currency
|
#include <crypto/chacha20.h>
#include <crypto/common.h>
#include <span.h>
#include <uint256.h>
#include <chrono>
#include <cstdint>
#include <limits>
Go to the source code of this file.
Classes | |
class | FastRandomContext |
Fast randomness source. More... | |
Functions | |
void | GetRandBytes (Span< uint8_t > bytes) noexcept |
Overall design of the RNG and entropy sources. | |
uint64_t | GetRandInternal (uint64_t nMax) noexcept |
Generate a uniform random integer in the range [0..range). | |
template<typename T > | |
T | GetRand (T nMax=std::numeric_limits< T >::max()) noexcept |
Generate a uniform random integer of type T in the range [0..nMax) nMax defaults to std::numeric_limits<T>::max() Precondition: nMax > 0, T is an integral type, no larger than uint64_t. | |
template<typename D > | |
D | GetRandomDuration (typename std::common_type< D >::type max) noexcept |
Generate a uniform random duration in the range [0..max). | |
std::chrono::microseconds | GetExponentialRand (std::chrono::microseconds now, std::chrono::seconds average_interval) |
Return a timestamp in the future sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution). | |
uint256 | GetRandHash () noexcept |
void | GetStrongRandBytes (Span< uint8_t > bytes) noexcept |
Gather entropy from various sources, feed it into the internal PRNG, and generate random data using it. | |
void | RandAddPeriodic () noexcept |
Gather entropy from various expensive sources, and feed them to the PRNG state. | |
void | RandAddEvent (const uint32_t event_info) noexcept |
Gathers entropy from the low bits of the time at which events occur. | |
template<typename I , typename R > | |
void | Shuffle (I first, I last, R &&rng) |
More efficient than using std::shuffle on a FastRandomContext. | |
void | GetOSRand (uint8_t *ent32) |
Get 32 bytes of system entropy. | |
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. | |
Variables | |
constexpr auto | GetRandMicros = GetRandomDuration<std::chrono::microseconds> |
constexpr auto | GetRandMillis = GetRandomDuration<std::chrono::milliseconds> |
static const int | NUM_OS_RANDOM_BYTES = 32 |
Number of random bytes returned by GetOSRand. | |
std::chrono::microseconds GetExponentialRand | ( | std::chrono::microseconds | now, |
std::chrono::seconds | average_interval | ||
) |
Return a timestamp in the future sampled from an exponential distribution (https://en.wikipedia.org/wiki/Exponential_distribution).
This distribution is memoryless and should be used for repeated network events (e.g. sending a certain type of message) to minimize leaking information to observers.
The probability of an event occuring before time x is 1 - e^-(x/a) where a is the average interval between events.
Definition at line 794 of file random.cpp.
Get 32 bytes of system entropy.
Do not use this in application code: use GetStrongRandBytes instead.
Fall back to /dev/urandom if there is no specific method implemented to get system entropy for this OS.
Definition at line 319 of file random.cpp.
|
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:
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.
Definition at line 639 of file random.cpp.
|
noexcept |
Definition at line 659 of file random.cpp.
Generate a uniform random integer in the range [0..range).
Precondition: range > 0
Definition at line 655 of file random.cpp.
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.
Definition at line 642 of file random.cpp.
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 649 of file random.cpp.
|
noexcept |
Gather entropy from various expensive sources, and feed them to the PRNG state.
Thread-safe.
Definition at line 645 of file random.cpp.
bool Random_SanityCheck | ( | ) |
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 706 of file random.cpp.
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.
Definition at line 786 of file random.cpp.
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 291 of file random.h.
|
constexpr |
|
constexpr |
|
static |