Secure random bytes and unbiased sampling for MoonBit; never a seeded generator.
test "draw" {
let key = @moonseed.bytes(32, @system.source) // 32 uniform bytes
let n = @moonseed.below(b"\x03\xe8", @system.source) // uniform in [0, 1000)
let d = @moonseed.scalar(b"\x05", @system.source) // uniform in [1, 4]
ignore((key, n, d))
}| Package | What it holds | Depends on |
|---|---|---|
| moonseed | Source, Unavailable, bytes, below, scalar, Replay | nothing |
| moonseed/system | source, the platform's entropy | core env |
| Source | Where the bytes come from |
|---|---|
| @system.source | crypto.getRandomValues on JavaScript, WASI random_get on wasm and wasm-gc, the runtime on native |
| Replay::new(bytes) | the bytes given, in order; exhausted after |
| your own | anything that implements Source |
struct Device {}
impl @moonseed.Source for Device with fn fill(_self, _buf) {
raise Failed("the device is unplugged")
}test "replay" {
let source = @moonseed.Replay::new(b"\x07\xff\x02")
assert_eq(@moonseed.below(b"\x0a", source), b"\x07")
// 0xff is 15 once the high bits are cleared, too big, so the next byte is drawn.
assert_eq(@moonseed.below(b"\x0a", source), b"\x02")
assert_eq(source.remaining(), 0)
let refused = try {
ignore(@moonseed.below(b"\x0a", source))
false
} catch {
Exhausted => true
_ => false
}
assert_true(refused)
}pub struct Replay {
// private fields
}Install
Download zipSecure random bytes and unbiased sampling for MoonBit; never a seeded generator.