* Fast randomness source. This is seeded once with secure random data, but * is completely deterministic and does not gather more entropy after that. * * This class is not thread-safe. */
| 383 | * This class is not thread-safe. |
| 384 | */ |
| 385 | class FastRandomContext : public RandomMixin<FastRandomContext> |
| 386 | { |
| 387 | private: |
| 388 | bool requires_seed; |
| 389 | ChaCha20 rng; |
| 390 | |
| 391 | void RandomSeed() noexcept; |
| 392 | |
| 393 | public: |
| 394 | /** Construct a FastRandomContext with GetRandHash()-based entropy (or zero key if fDeterministic). */ |
| 395 | explicit FastRandomContext(bool fDeterministic = false) noexcept; |
| 396 | |
| 397 | /** Initialize with explicit seed (only for testing) */ |
| 398 | explicit FastRandomContext(const uint256& seed) noexcept; |
| 399 | |
| 400 | /** Reseed with explicit seed (only for testing). */ |
| 401 | void Reseed(const uint256& seed) noexcept; |
| 402 | |
| 403 | /** Generate a random 64-bit integer. */ |
| 404 | uint64_t rand64() noexcept |
| 405 | { |
| 406 | if (requires_seed) RandomSeed(); |
| 407 | std::array<std::byte, 8> buf; |
| 408 | rng.Keystream(buf); |
| 409 | return ReadLE64(buf.data()); |
| 410 | } |
| 411 | |
| 412 | /** Fill a byte span with random bytes. This overrides the RandomMixin version. */ |
| 413 | void fillrand(std::span<std::byte> output) noexcept; |
| 414 | }; |
| 415 | |
| 416 | /** xoroshiro128++ PRNG. Extremely fast, not appropriate for cryptographic purposes. |
| 417 | * |
no outgoing calls
no test coverage detected