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:

The trust contract

This module satisfies the four rules every Trusted.* module must meet (see docs/TRUSTED.md):

  1. 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.
  2. Declared boundary. Every @extern carries a ## Trust: block.
  3. No proof claims. Nothing here is a proof.
  4. Reviewable axiom set. cure audit trust Trusted.Random snapshots 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

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

accumulate_bytes(binary: Binary, index: Int, width: Int, acc: Int) -> Int

 fn accumulate_bytes(binary: Binary, index: Int, width: Int, acc: Int) -> Int

Fold width bytes into an integer.

byte_space(width: Int) -> Int

 fn byte_space(width: Int) -> Int

The number of distinct values width bytes can express.

byte_width(range: Int) -> Int

 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).

bytes(n: Int) -> Binary

 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))

clamp_size(n: Int) -> Int

 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.

draw_attempt(lo: Int, range: Int, width: Int, limit: Int) -> Int

 fn draw_attempt(lo: Int, range: Int, width: Int, limit: Int) -> Int

One rejection-sampling round; recurses when the draw is out of range.

draw_below(width: Int) -> Int

 fn draw_below(width: Int) -> Int

Draw an integer from width random bytes, most significant first.

draw_in(lo: Int, hi: Int, range: Int) -> Int

 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.

int_in(lo: Int, hi: Int) -> Int

 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)

power_of_256(width: Int, acc: Int) -> Int

 fn power_of_256(width: Int, acc: Int) -> Int

256^width, built iteratively.

range_size(lo: Int, hi: Int) -> Int

 fn range_size(lo: Int, hi: Int) -> Int

The number of values in lo..hi, inclusive.

raw_byte_at(binary: Binary, index: Int) -> Int

 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.

raw_byte_size(binary: Binary) -> Int

 fn raw_byte_size(binary: Binary) -> Int

Trust: :erlang.byte_size/1 is a pure total function on a Binary.

raw_strong_rand_bytes(size: Int) -> 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.

truncate_to_multiple(space: Int, range: Int) -> Int

 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.

wide_byte_width(range: Int, width: Int, space: Int) -> Int

 fn wide_byte_width(range: Int, width: Int, space: Int) -> Int

Grow the byte width until space covers range.