Trusted. Random
(Cure Standard Library v0.38.0)
View Source
Cryptographically strong randomness, for the trusted (unproved) library.
Why this module exists
Std.Gen provides a seeded, reproducible PRNG for property testing:
:rand is fast, deterministic given a seed, and exactly wrong for
anything security-shaped. A token, a nonce, a salt, or a session id needs
entropy the OS guarantees, and :crypto.strong_rand_bytes/1 is where the
BEAM gets it.
The distinction is worth stating plainly because getting it wrong is a security bug, not a style issue:
Std.Gen.int_in/2— reproducible, seedable, NOT for secrets.Trusted.Random.bytes/1— OS entropy, for secrets.
The trust contract
This module satisfies the four rules every Trusted.* module must meet
(see docs/TRUSTED.md):
- Total surface. Every function returns a concrete type; the only failure mode is an invalid size, which is rejected in the type rather than at runtime.
- Declared boundary. Every
@externcarries a## Trust:block. - No proof claims. Nothing here is a proof.
- Reviewable axiom set.
cure audit trust Trusted.Randomsnapshots the reachable externs.
Examples
mod RandomBytesExample
use Std.Binary
use Trusted.Random
fn token() -> Binary = bytes(32)mod RandomIntExample
use Trusted.Random
fn die_roll() -> Int = int_in(1, 6)
Summary
Functions
Fold width bytes into an integer.
The number of distinct values width bytes can express.
The number of bytes needed to represent range distinct values.
n bytes of OS entropy.
Clamp a requested size at zero.
One rejection-sampling round; recurses when the draw is out of range.
Draw an integer from width random bytes, most significant first.
Draw a value in lo..hi by rejection sampling.
A uniformly distributed integer in lo..hi, inclusive.
256^width, built iteratively.
The number of values in lo..hi, inclusive.
Trust: :binary.at/2 is 0-based and raises badarg out of range. Only
ever called with an index below raw_byte_size/1.
Trust: :erlang.byte_size/1 is a pure total function on a Binary.
Trust: :crypto.strong_rand_bytes/1 answers a Binary of exactly the
requested length, drawn from the OS CSPRNG. OTP crypto reference. It
raises error(badarg) for a negative or non-integer size, which
bytes/1 prevents by checking the size before the call.
The largest multiple of range that does not exceed space.
Grow the byte width until space covers range.
Functions
fn accumulate_bytes(binary: Binary, index: Int, width: Int, acc: Int) -> Int
Fold width bytes into an integer.
fn byte_space(width: Int) -> Int
The number of distinct values width bytes can express.
fn byte_width(range: Int) -> Int
The number of bytes needed to represent range distinct values.
1 byte covers 256 values, 2 cover 65536, and so on. A range of 1 needs 1 byte (the draw is always 0, which is correct for a single-value range).
fn bytes(n: Int) -> Binary
n bytes of OS entropy.
The building block for tokens, nonces, salts, and ids. A negative n
answers an empty binary rather than raising: "give me no randomness" is a
degenerate request, not a programming error worth crashing on, and the
caller that computed n from user input should not be able to abort the
process with it.
Examples
mod BytesExample
use Std.Binary
use Trusted.Random
fn token_size() -> Int = Std.Binary.byte_size(bytes(32))
fn clamp_size(n: Int) -> Int
Clamp a requested size at zero.
:crypto.strong_rand_bytes/1 raises for a negative size, so the clamp is
what makes bytes/1 total. It is a local helper rather than an inline
branch so bytes/1's body stays a single call -- which is what T1's
carrier exception recognizes, and what makes the totality argument
legible: the only thing that can vary is the size, and it is clamped.
fn draw_attempt(lo: Int, range: Int, width: Int, limit: Int) -> Int
One rejection-sampling round; recurses when the draw is out of range.
fn draw_below(width: Int) -> Int
Draw an integer from width random bytes, most significant first.
fn draw_in(lo: Int, hi: Int, range: Int) -> Int
Draw a value in lo..hi by rejection sampling.
Each attempt reads byte_width/1 bytes into an integer. A draw below
limit (the largest multiple of the range that fits) is accepted; anything
above is discarded and redrawn, which is what removes the modulo bias.
The recursion terminates with probability 1 and, in practice, after about
one attempt: limit is at least half the byte space, so the rejection rate
is below 50% per round.
fn int_in(lo: Int, hi: Int) -> Int
A uniformly distributed integer in lo..hi, inclusive.
Uses rejection sampling over the byte stream rather than rem, so the
result is uniform even when the range does not divide the byte space
evenly — the modulo bias that makes a naive rand % range unsuitable for
security-shaped work.
A lo greater than hi answers lo: the caller asked for an empty range,
and returning one of the two bounds is the only total answer.
Examples
mod IntInExample
use Trusted.Random
fn die_roll() -> Int = int_in(1, 6)
fn power_of_256(width: Int, acc: Int) -> Int
256^width, built iteratively.
fn range_size(lo: Int, hi: Int) -> Int
The number of values in lo..hi, inclusive.
fn raw_byte_at(binary: Binary, index: Int) -> Int
Trust: :binary.at/2 is 0-based and raises badarg out of range. Only
ever called with an index below raw_byte_size/1.
fn raw_byte_size(binary: Binary) -> Int
Trust: :erlang.byte_size/1 is a pure total function on a Binary.
fn raw_strong_rand_bytes(size: Int) -> Binary
Trust: :crypto.strong_rand_bytes/1 answers a Binary of exactly the
requested length, drawn from the OS CSPRNG. OTP crypto reference. It
raises error(badarg) for a negative or non-integer size, which
bytes/1 prevents by checking the size before the call.
fn truncate_to_multiple(space: Int, range: Int) -> Int
The largest multiple of range that does not exceed space.
Draws below this bound map onto the range without bias; draws at or above it are rejected.
fn wide_byte_width(range: Int, width: Int, space: Int) -> Int
Grow the byte width until space covers range.