Vitamin C Random
A carefully designed random number generator that is safe to use for cryptographic purposes.
This crate is part of the Vitamin C framework to make cryptography code healthy.
Generatable
Types implementing the [Generatable] trait can be generated randomly using [SafeRand].
use ;
let mut rng = from_entropy.expect;
let x: = random.unwrap;
You can implement Generatable for your own types using the derive macro:
use ;
// Create a random number generator and generate an instance of MyStruct.
let mut rng = from_entropy.expect;
let instance: MyStruct = random.unwrap;
println!;
Bounded Random Numbers
SafeRand::next_below(n) returns a value in 0..n, the bound every index-shaped use
wants. It makes exactly one 64-bit draw per call, reduced with Lemire's multiply-high
method, so the number of draws does not depend on the values drawn. The reduction's
statistical distance from uniform is at most n / 2⁶⁴; a protocol that needs exact
uniformity must account for that term.
use ;
let mut rng = from_entropy.expect;
let index = rng.next_below;
assert!;
The bound may also be a Protected<u32>, in which case the result is Protected too:
use ;
use ;
let mut rng = from_entropy.expect;
let index: = rng.next_below;
assert!;
A Protected<u32> bound of zero panics, and that panic is observable on the secret. When
a secret bound might be zero, check it once at construction and pass a
Protected<NonZeroU32>; that form never panics:
use NonZeroU32;
use ;
use ;
let mut rng = from_entropy.expect;
let bound = new;
let index: = rng.next_below;
assert!;
All three are the BoundedRng::next_below trait method. The older inclusive form,
next_bounded(max) for 0..=max, lives on the separate, deprecated BoundedRngInclusive
trait and on SafeRand::next_bounded_u32; both are deprecated in favour of
next_below(max + 1).
CipherStash
Vitamin C is brought to you by the team at CipherStash.
License: MIT