Sampling bounded indices #
sampleBinaryNat width interprets exactly width fair bits as a natural number, giving the
uniform distribution below 2 ^ width. Its runtime certificate uses binary output.
sampleBoundedIndex bound uses log₂ bound + 1 fair bits. Each index strictly below bound
has the same probability; the remaining probability is assigned to the sentinel bound.
There is no unbounded rejection loop.
sampleModNat width modulus reduces such a value modulo modulus. Read in ZMod q, it is within
statistical distance q / 2 ^ width of uniform (dist_map_natCast_eval_sampleModNat_le): strict
PPT machines sample ZMod q only approximately, and enough extra bits make the error small.
sampleDyadicIndex bound instead samples exactly uniformly below the next power of two, whose
range is at most 2 * (bound + 1). sampleDyadicCoin bound numerator compares such an index with
numerator, sampling the exact capped fraction numerator / dyadicSize bound.
Main definitions #
sampleBinaryNat: exact uniform sampling from a specified bit width.sampleModNat: approximately uniform sampling of residues modulo a given modulus.sampleDyadicIndex: exact uniform sampling in the power-of-two range.sampleBoundedIndex: a capped uniform index with a rejection sentinel.sampleDyadicCoin: an exact capped dyadic acceptance probability.
Main results #
eval_sampleBinaryNat,mem_support_sampleBinaryNat_iff: the exact law and support.eval_sampleModNat,dist_map_natCast_eval_sampleModNat_le: the law of the residue sampler and its distance from uniform onZMod q.eval_sampleDyadicIndex,eval_sampleBoundedIndex: exact distribution laws.eval_sampleDyadicCoin_true: exact acceptance mass, including zero and one.
References #
Adapted from Languages/Probabilistic/UniformNat.lean on the cslib crypto branch at
5ff69ca93402e7c947208dc56396d6d86203ccd8.
Interpret exactly width independent fair bits as a binary natural number.
Instances For
With width zero the sampler returns 0 without sampling.
The sampled value is exactly uniform below 2 ^ width, including width zero.
Reduce width fair bits, read as a binary natural number, modulo modulus.
Instances For
The residue sampler reduces a uniform value below 2 ^ width.
Approximately uniform residues. Read in ZMod q, the residue of width fair bits is
within statistical distance q / 2 ^ width of the uniform distribution on ZMod q.
Every value below 2 ^ width can occur, and no other value can occur.
An exact uniform index in a power-of-two range, using a fixed number of fair bits.
Instances For
The dyadic sampler has no rejection or approximation error.
Move an independent source inside a uniform index choice, with a fixed fallback for padding indices. This exposes the finite average used by random-coordinate reductions.
An exact dyadic coin, saturated at probability one when the numerator exceeds the range.
Instances For
The dyadic coin's exact acceptance probability, including the endpoints zero and one.
The real-valued acceptance probability of the exact dyadic sampler.
Every positive dyadic acceptance probability is at least one over its sampling range.
A capped fair-bit index, with bound denoting rejection.