Changeset View
Changeset View
Standalone View
Standalone View
src/random.h
// Copyright (c) 2009-2010 Satoshi Nakamoto | // Copyright (c) 2009-2010 Satoshi Nakamoto | ||||
// Copyright (c) 2009-2016 The Bitcoin Core developers | // Copyright (c) 2009-2016 The Bitcoin Core developers | ||||
// Distributed under the MIT software license, see the accompanying | // Distributed under the MIT software license, see the accompanying | ||||
// file COPYING or http://www.opensource.org/licenses/mit-license.php. | // file COPYING or http://www.opensource.org/licenses/mit-license.php. | ||||
#ifndef BITCOIN_RANDOM_H | #ifndef BITCOIN_RANDOM_H | ||||
#define BITCOIN_RANDOM_H | #define BITCOIN_RANDOM_H | ||||
#include <crypto/chacha20.h> | #include <crypto/chacha20.h> | ||||
#include <crypto/common.h> | #include <crypto/common.h> | ||||
#include <uint256.h> | #include <uint256.h> | ||||
#include <chrono> // For std::chrono::microseconds | |||||
#include <cstdint> | #include <cstdint> | ||||
#include <limits> | #include <limits> | ||||
/** | /** | ||||
* Overall design of the RNG and entropy sources. | * Overall design of the RNG and entropy sources. | ||||
* | * | ||||
* We maintain a single global 256-bit RNG state for all high-quality | * We maintain a single global 256-bit RNG state for all high-quality | ||||
* randomness. The following (classes of) functions interact with that state by | * randomness. The following (classes of) functions interact with that state by | ||||
▲ Show 20 Lines • Show All 49 Lines • ▼ Show 20 Lines | |||||
* | * | ||||
* These functions are designed to be fast (sub microsecond), but do not | * These functions are designed to be fast (sub microsecond), but do not | ||||
* necessarily meaningfully add entropy to the PRNG state. | * necessarily meaningfully add entropy to the PRNG state. | ||||
* | * | ||||
* Thread-safe. | * Thread-safe. | ||||
*/ | */ | ||||
void GetRandBytes(uint8_t *buf, int num) noexcept; | void GetRandBytes(uint8_t *buf, int num) noexcept; | ||||
uint64_t GetRand(uint64_t nMax) noexcept; | uint64_t GetRand(uint64_t nMax) noexcept; | ||||
std::chrono::microseconds | |||||
GetRandMicros(std::chrono::microseconds duration_max) noexcept; | |||||
int GetRandInt(int nMax) noexcept; | int GetRandInt(int nMax) noexcept; | ||||
uint256 GetRandHash() noexcept; | uint256 GetRandHash() noexcept; | ||||
/** | /** | ||||
* Gather entropy from various sources, feed it into the internal PRNG, and | * Gather entropy from various sources, feed it into the internal PRNG, and | ||||
* generate random data using it. | * generate random data using it. | ||||
* | * | ||||
* This function will cause failure whenever the OS RNG fails. | * This function will cause failure whenever the OS RNG fails. | ||||
▲ Show 20 Lines • Show All 174 Lines • Show Last 20 Lines |