random.h

The source a sampler draws from: the entropy stream, or a stream a seed reproduces.

Every sampler in vfhe takes a Random. NULL is the entropy stream: seeds come from RDRAND where the engine has it and from getentropy otherwise, and expand through AES-128 in counter mode on an AES-NI engine and through BLAKE3 elsewhere, so the stream differs between engines; requests under 512 bytes come from the calling thread’s pool, larger ones expand a fresh seed of their own, and a forked child discards the pool it inherits. When the seed source fails, the process prints the reason to stderr and exits with EXIT_FAILURE: a predictable value is never returned. A seeded source is a BLAKE3 XOF of its seed, the same bytes on every engine, read in order; it belongs to one thread at a time, and two sources from one seed give the same bytes in the same order.

Note

Thread-safe for NULL; a seeded source is not shared between threads.

Typedefs

typedef struct Random *Random

A source of randomness; NULL stands for the entropy stream.

Functions

Random random_seeded(const uint8_t *seed, uint64_t seed_len)

Creates a source every draw of which is a function of seed.

Warning

Exits the process with EXIT_FAILURE if the allocation is refused.

Parameters:
  • seed – [in] The seed; a secret, when the draws are.

  • seed_len – [in] Its length in bytes.

Returns:

A source to release with random_free().

void random_free(Random source)

Releases a seeded source, wiping its state.

Parameters:

source – [in] From random_seeded(), or NULL, which does nothing.

void random_bytes(Random source, uint8_t *out, uint64_t amount)

Writes bytes from the source.

Warning

Exits the process if the entropy seed source fails.

Parameters:
  • source – [in] A source, or NULL for the entropy stream.

  • out – [out] Receives amount bytes.

  • amount – [in] Bytes to write.

void random_below(Random source, uint64_t *out, uint64_t count, uint64_t bound)

Writes values exactly uniform in [0, bound), by rejection.

Parameters:
  • source – [in] A source, or NULL for the entropy stream.

  • out – [out] Receives count values.

  • count – [in] Values to write.

  • bound – [in] At least 1.