vfhe.crypto.random

The randomness: Random, the source a scheme’s samplers draw from, and seeded_below_many, values another party recomputes.

Random() is the entropy stream, bytes no one can predict; Random(seed) is a stream the seed reproduces, for a reproducible scheme. seeded_below_many is the other kind of determinism: value i is a pure function of (context, seed) and i, so a verifier recomputes a prover’s draws from a transcript.

>>> from vfhe.crypto.random import Random, seeded_below_many
>>> len(Random().bytes(16))
16
>>> seeded_below_many(2, 1000, b"example", b"seed") == seeded_below_many(2, 1000, b"example", b"seed")
True
class vfhe.crypto.random.Random(seed: bytes | None = None)

A source of randomness: the entropy stream, or a stream a seed reproduces.

Without a seed, draws come from the entropy stream, seeded from the hardware and expanded by the engine’s cipher in C, and nobody can predict them. Thread-safe: every thread draws from a pool of its own, and a forked child discards the pool it inherits. A failing seed source exits the process; a predictable value is never returned.

With a seed, every draw is a function of the seed, the BLAKE3 XOF of it read in order, the same bytes on every engine: two sources from one seed give the same bytes in the same order, so a scheme built over one generates the same keys, masks and noise every time, and the seed is as secret as they are. A seeded source belongs to one thread at a time; its state is wiped when it is released.

>>> Random(b"seed").bytes(4) == Random(b"seed").bytes(4)
True
Parameters:

seed – Any length; None for the entropy stream.

obj

The C source, or NULL for the entropy stream; what a sampler is handed.

bytes(amount: int) → bytes

Draws bytes.

Parameters:

amount – Bytes to draw; 0 and above.

Returns:

amount bytes.

Raises:

ValueError – amount is negative.

below(bound: int) → int

Draws one value: below_many with a count of 1.

Parameters:

bound – The value is below it; 1 to 2**64 - 1.

Returns:

A value uniform in [0, bound).

Raises:

ValueError – bound is outside [1, 2**64).

below_many(count: int, bound: int) → list[int]

Draws values exactly uniform below a bound, by rejection.

The draw branches on how many words were rejected and on nothing else, so a timing reveals only the rejection count.

Parameters:
  • count – Values to draw; 0 and above.

  • bound – Every value is below it; 1 to 2**64 - 1.

Returns:

count values, each uniform in [0, bound).

Raises:

ValueError – bound is outside [1, 2**64), or count is negative.

vfhe.crypto.random.seeded_below_many(count: int, bound: int, context: bytes, seed: bytes) → list[int]

Computes values of the sequence (context, seed) names, uniform below a bound.

The sequence is defined by index: value i is word i of a BLAKE3 stream derived from context and seed, masked to the bits of bound - 1 and redrawn from further streams while it is not below bound. So value i depends on i alone and raising count extends the sequence; in C, seeded_below_from starts at any index. The values are exactly uniform and byte for byte the same on every engine, which is what lets a verifier recompute a prover’s draws. Thread-safe, no state. The running time depends on bound.

Parameters:
  • count – Values to compute; 0 and above.

  • bound – Every value is below it; 1 to 2**64 - 1.

  • context – The domain-separation tag: a fixed byte-string literal per call site, never caller data, so one seed under two tags gives two independent sequences. Holds no NUL byte.

  • seed – Any length; public when the values are.

Returns:

Values [0, count) of the sequence, each in [0, bound).

Raises:

ValueError – bound is outside [1, 2**64), count is negative, or context holds a NUL byte, which would end it early in C.