![]() |
PIVX Core
5.6.99
P2P Digital Currency
|
#include "random.h"#include "compat/cpuid.h"#include "crypto/sha256.h"#include "crypto/sha512.h"#include "support/cleanse.h"#include "support/allocators/secure.h"#include "logging.h"#include "sync.h"#include "utiltime.h"#include <stdlib.h>#include <limits>#include <chrono>#include <thread>#include "randomenv.h"#include <fcntl.h>#include <sys/time.h>Go to the source code of this file.
Enumerations | |
| enum class | RNGLevel { FAST , SLOW , PERIODIC } |
Functions | |
| void | GetDevURandom (unsigned char *ent32) |
| Fallback: get 32 bytes of system entropy from /dev/urandom. More... | |
| void | GetOSRand (unsigned char *ent32) |
| Get 32 bytes of system entropy. More... | |
| void | GetRandBytes (unsigned char *buf, int num) noexcept |
| Overall design of the RNG and entropy sources. More... | |
| void | GetStrongRandBytes (unsigned char *buf, int num) noexcept |
| Gather entropy from various sources, feed it into the internal PRNG, and generate random data using it. 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... | |
| uint64_t | GetRand (uint64_t nMax) noexcept |
| int | GetRandInt (int nMax) noexcept |
| uint256 | GetRandHash () noexcept |
| bool | GetRandBool (double rate) noexcept |
| bool | Random_SanityCheck () |
| Check that OS randomness is available and returning the requested number of bytes. More... | |
| void | RandomInit () |
| Initialize global RNG state and log any CPU features that are used. More... | |
Variables | |
| bool | g_mock_deterministic_tests {false} |
| Flag to make GetRand in random.h return the same number. More... | |
|
strong |
| Enumerator | |
|---|---|
| FAST | Automatically called by GetRandBytes. |
| SLOW | Automatically called by GetStrongRandBytes. |
| PERIODIC | Called by RandAddPeriodic() |
Definition at line 544 of file random.cpp.
| void GetDevURandom | ( | unsigned char * | ent32 | ) |
Fallback: get 32 bytes of system entropy from /dev/urandom.
The most compatible way to get cryptographic randomness on UNIX-ish platforms.
Definition at line 258 of file random.cpp.
| void GetOSRand | ( | unsigned char * | ent32 | ) |
Get 32 bytes of system entropy.
Do not use this in application code: use GetStrongRandBytes instead.
Definition at line 278 of file random.cpp.
|
noexcept |
Definition at line 586 of file random.cpp.
|
noexcept |
Definition at line 603 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 579 of file random.cpp.
|
noexcept |
Definition at line 596 of file random.cpp.
|
noexcept |
Definition at line 591 of file random.cpp.
|
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.
Definition at line 580 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 582 of file random.cpp.
|
noexcept |
Gather entropy from various expensive sources, and feed them to the PRNG state.
Thread-safe.
Definition at line 581 of file random.cpp.
| bool Random_SanityCheck | ( | ) |
Check that OS randomness is available and returning the requested number of bytes.
Definition at line 647 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 716 of file random.cpp.
| bool g_mock_deterministic_tests {false} |
Flag to make GetRand in random.h return the same number.
Definition at line 584 of file random.cpp.