Bitcoin Core 31.1.0
P2P Digital Currency
Loading...
Searching...
No Matches
random.cpp File Reference
#include <bitcoin-build-config.h>
#include <random.h>
#include <compat/compat.h>
#include <compat/cpuid.h>
#include <crypto/chacha20.h>
#include <crypto/sha256.h>
#include <crypto/sha512.h>
#include <randomenv.h>
#include <span.h>
#include <support/allocators/secure.h>
#include <support/cleanse.h>
#include <sync.h>
#include <util/log.h>
#include <util/time.h>
#include <array>
#include <cmath>
#include <cstdlib>
#include <optional>
#include <thread>
#include <fcntl.h>
#include <sys/time.h>
Include dependency graph for random.cpp:

Go to the source code of this file.

Functions

void MakeRandDeterministicDANGEROUS (const uint256 &seed) noexcept
 Internal function to set g_determinstic_rng.
void GetRandBytes (std::span< unsigned char > bytes) noexcept
 Generate random data via the internal PRNG.
void GetStrongRandBytes (std::span< unsigned char > 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.
bool Random_SanityCheck ()
 Check that OS randomness is available and returning the requested number of bytes.
void RandomInit ()
double MakeExponentiallyDistributed (uint64_t uniform) noexcept
 Given a uniformly random uint64_t, return an exponentially distributed double with mean 1.

Variables

std::atomic< bool > g_used_g_prng {false}
static constexpr std::array< std::byte, ChaCha20::KEYLENZERO_KEY {}

Function Documentation

◆ GetRandBytes()

void GetRandBytes ( std::span< unsigned char > bytes)
noexcept

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 601 of file random.cpp.

Here is the caller graph for this function:

◆ GetStrongRandBytes()

void GetStrongRandBytes ( std::span< unsigned char > bytes)
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 607 of file random.cpp.

Here is the caller graph for this function:

◆ MakeExponentiallyDistributed()

double MakeExponentiallyDistributed ( uint64_t uniform)
noexcept

Given a uniformly random uint64_t, return an exponentially distributed double with mean 1.

Definition at line 704 of file random.cpp.

Here is the caller graph for this function:

◆ MakeRandDeterministicDANGEROUS()

void MakeRandDeterministicDANGEROUS ( const uint256 & seed)
noexcept

Internal function to set g_determinstic_rng.

Only accessed from tests.

Definition at line 595 of file random.cpp.

Here is the caller graph for this function:

◆ RandAddEvent()

void RandAddEvent ( uint32_t event_info)
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 617 of file random.cpp.

Here is the caller graph for this function:

◆ RandAddPeriodic()

void RandAddPeriodic ( )
noexcept

Gather entropy from various expensive sources, and feed them to the PRNG state.

Thread-safe.

Definition at line 612 of file random.cpp.

Here is the caller graph for this function:

◆ Random_SanityCheck()

bool Random_SanityCheck ( )

Check that OS randomness is available and returning the requested number of bytes.

Definition at line 641 of file random.cpp.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ RandomInit()

void RandomInit ( )

Definition at line 696 of file random.cpp.

Here is the caller graph for this function:

Variable Documentation

◆ g_used_g_prng

std::atomic<bool> g_used_g_prng {false}

Definition at line 599 of file random.cpp.

◆ ZERO_KEY

std::array<std::byte, ChaCha20::KEYLEN> ZERO_KEY {}
staticconstexpr

Definition at line 687 of file random.cpp.