silk.system.insecure_random.
The InsecureRandom effect: a fast pseudorandom sequence that repeats for the same seed.
When to use#
Require &mut InsecureRandom for simulations, games, shuffles in tests, and other uses where
the same seed must give the same results. Provide InsecureRandom.seeded with a seed. Never
use it for keys, nonces, tokens, passwords, or protection against hash collisions. Use
silk.system.random for those.
Details#
A provider implements InsecureRandom.nextU64. InsecureRandom.nextBool,
InsecureRandom.below, and InsecureRandom.fillBytes use it in a fixed way, so the results
are the same on each target. InsecureRandom.seeded returns a Xoshiro256StarStar
generator. It expands the seed with SplitMix64.
Gotchas#
The output is predictable. An observer who sees a few values can compute the next values.
Examples#
Get the same first value from the same seed#
import silk.core.effect { Effect }
import silk.system.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 1
}
return 0
}Import as InsecureRandom with import silk.system.insecure_random { InsecureRandom }.
Public declarations: 2.
InsecureRandom#
pub service InsecureRandomThe effect for a fast pseudorandom sequence that is not secure and repeats for the same seed.
When to use#
Require &mut InsecureRandom for simulations, games, shuffles in tests, and other uses where
the same seed must give the same results. Provide InsecureRandom.seeded with a seed. Use
silk.system.random for secrets.
Details#
A provider implements InsecureRandom.nextU64. InsecureRandom.nextBool,
InsecureRandom.below, and InsecureRandom.fillBytes use it in a fixed way, so the results
are the same on each target. The provider owns its state. Each call moves only the provider of
the current effect forward.
Gotchas#
Do not use it for secrets, keys, nonces, tokens, passwords, or protection against hash collisions. The output is predictable: an observer who sees a few values can compute the next values.
Operation nextU64#
effect fn nextU64() -> u64 ? &mut InsecureRandomReturns the next u64 of the sequence.
Implementation InsecureRandom#
impl InsecureRandomAssociated function seeded#
pub fn seeded(seed: u64) -> Xoshiro256StarStarReturns a Xoshiro256StarStar generator that starts from seed.
Details#
The same seed gives the same sequence on each target. Each seed works, also 0, because the seed is expanded with four SplitMix64 steps.
Associated function nextBool#
pub effect fn nextBool() -> bool ? &mut InsecureRandomReturns true if the highest bit of the next u64 is set.
Associated function below#
pub effect fn below(upperExclusive: u64) -> Option<u64> ? &mut InsecureRandomReturns a value from 0 to upperExclusive - 1, with the same chance for each value. Returns
an absent option if upperExclusive is 0.
Details#
It drops u64 values that would make some results more likely than others, so it can read
more than one value.
Associated function fillBytes#
pub effect fn fillBytes(output: &mut [u8]) -> () ? &mut InsecureRandomFills output with bytes from the sequence. Each u64 gives 8 bytes, least significant byte
first.
Details#
An empty slice reads nothing. If the length is not a multiple of 8, the last u64 is read in
full and its extra bytes are dropped.
Xoshiro256StarStar#
pub struct Xoshiro256StarStarA xoshiro256** generator. Make one with InsecureRandom.seeded and provide it as
InsecureRandom.
Details#
The sequence is the same on each target. It is not secure: an observer who sees a few values can compute the next values.
Implementation InsecureRandom for Xoshiro256StarStar#
impl InsecureRandom for Xoshiro256StarStarOperation nextU64#
nextU64: Xoshiro256StarStar.xoshiroNextCategory: System.
Profiles: aarch64-apple-darwin, aarch64-unknown-linux-gnu, aarch64-unknown-linux-gnu-no-libc, x86_64-unknown-linux-gnu, x86_64-unknown-linux-gnu-no-libc, wasm32-unknown-unknown, wasm32-wasip1, wasm32-wasip2.