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.
Installation
[]
= "0.9.0"
The examples using Protected also require vitaminc-protected = "0.9.0"
under [dependencies].
WebAssembly
For a JavaScript host, use the wasm32-unknown-unknown target:
This crate enables getrandom 0.4's wasm_js feature on wasm32. That feature
selects the JavaScript entropy backend automatically on wasm32-unknown-unknown;
consumers need no extra feature, direct getrandom dependency, or
getrandom_backend="wasm_js" rustflag. The explicit flag was required by
getrandom 0.3.0–0.3.3; dependencies still using those older versions may have
their own configuration requirements.
At runtime, the backend calls crypto.getRandomValues through wasm-bindgen.
Package the application with wasm-bindgen or wasm-pack and load its generated
JavaScript glue in a browser, worker, or Node.js 19 or later. A compilation check
does not generate the glue or verify the host's entropy source. See
getrandom's WebAssembly support.
For WASI hosts, use a WASI target such as wasm32-wasip1. On that target,
getrandom uses the host's WASI randomness interface rather than JavaScript,
even though the wasm_js feature is enabled. No JavaScript glue is needed.
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 SafeRand;
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 SafeRand;
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 SafeRand;
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).
Cargo features
This crate defines no Cargo feature flags.
Compatibility
Requires Rust 1.89.0 and the Rust standard library (std).
MSRV increases require a minor release; patch releases retain the existing
floor. See the workspace compatibility policy.
CipherStash
Vitamin C is brought to you by the team at CipherStash.
License: MIT