Silk

silk/insecure_random

Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc.

Provider-replaceable deterministic words, booleans, bounded values, and byte filling.

When to use

Require InsecureRandom when a reproducible pseudorandom stream is needed. Use Xoshiro256StarStar with seeded for one stable supported-target sequence.

Details

InsecureRandom.nextU64 obtains one word from the active provider. nextBool, below, and fillBytes define one stable mapping from provider words to their results. Seed expansion uses SplitMix64.

Gotchas

These APIs are deterministic and non-cryptographic. Do not use them for secrets, credentials, keys, nonces, tokens, or hash-flood protection.

Examples

Reproduce the first word from one seed

import silk.effect { Effect }
import silk.insecure_random { InsecureRandom }

pub fn main() -> i32 {
  let mut provider = InsecureRandom.seeded(0)
  let first = run InsecureRandom.nextU64()
    |> Effect.provideMut<InsecureRandom>(&mut provider)
  if first != 0x99ec5f36cb75f2b4 { return 0 }
  return 42
}

Import as InsecureRandom with import silk.insecure_random { InsecureRandom }.

Public declarations: 2.

InsecureRandom

pub service InsecureRandom

An exclusive source of deterministic non-cryptographic u64 words.

Details

A provider owns its state. Each call advances only the provider supplied for the lexical Effect.

Gotchas

This service is unsuitable for secrets, credentials, keys, nonces, tokens, and hash protection.

Operation nextU64

effect<'static> fn nextU64() -> u64 ? &mut InsecureRandom

Returns the next deterministic word and advances the active provider once.

Associated function InsecureRandom.seeded

pub fn seeded(seed: u64) -> Xoshiro256StarStar

Creates a reproducible Xoshiro256StarStar provider from one u64 seed.

Details

Every seed, including zero, expands through four successive SplitMix64 steps.

Associated function InsecureRandom.nextBool

pub effect<'static> fn nextBool() -> bool ? &mut InsecureRandom

Returns whether bit 63 of the next deterministic provider word is set.

Associated function InsecureRandom.below

pub effect<'static> fn below(upperExclusive: u64) -> silk/option.Option<u64> ? &mut InsecureRandom

Returns a deterministic value below upperExclusive, or None when the bound is zero.

Details

Positive bounds use complete-word rejection sampling. Progress requires eventual acceptance.

Associated function InsecureRandom.fillBytes

pub effect<'life0> fn fillBytes<'life0>(output: &'life0 mut [u8]) -> () ? &mut InsecureRandom

Fills output from deterministic provider words in least-significant-byte-first order.

Details

An empty slice consumes no word. A partial final group consumes one complete word.

Xoshiro256StarStar

pub struct Xoshiro256StarStar

A deterministic xoshiro256** provider with four private u64 state words.

Details

The sequence is stable on every Silk engine and is not cryptographically secure.

Implementation InsecureRandom for Xoshiro256StarStar

impl InsecureRandom for Xoshiro256StarStar

Operation nextU64

nextU64 = Xoshiro256StarStar.xoshiroNext

On this page