MCPcopy Create free account
hub / github.com/bitcoin/bitcoin / FastRandomContext

Class FastRandomContext

src/random.h:385–414  ·  view source on GitHub ↗

* 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. */

Source from the content-addressed store, hash-verified

383 * This class is not thread-safe.
384 */
385class FastRandomContext : public RandomMixin<FastRandomContext>
386{
387private:
388 bool requires_seed;
389 ChaCha20 rng;
390
391 void RandomSeed() noexcept;
392
393public:
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 *

Callers 15

HeadersSyncStateMethod · 0.70
SerializeFileDBFunction · 0.70
CServiceHashMethod · 0.70
checkMethod · 0.70
StartScheduledTasksMethod · 0.70
NewPoWValidBlockMethod · 0.70
MaybeSendPingMethod · 0.70
ShouldCompactChainstateFunction · 0.70
FlushStateToDiskMethod · 0.70
ShouldCheckBlockIndexMethod · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected