A platform abstraction layer providing a cryptographic RNG, McRng
.
Example usage:
```rust use mc_rand::{McRng, RngCore}
pub fn myfunc() -> (u64, u64) { let mut rng = McRng::default(); let k0 = rng.nextu64(); let k1 = rng.next_u64(); (k0, k1) } ```
This project has evolved considerably as cargo has gotten more bug fixes and features.
Today, what it does is:
rdrand
, McRng
resolves to RdRandRng
, which uses
CPU intrinsics to call RDRAND
directly. This implementation was audited by NCC group.wasm_32
, McRng
resolves to OsRng
from rand crate.McRng
resolves to ThreadRng
.On targets without rdrand
, the feature rand/std
must be enabled, or the build will fail. In most non-embedded targets,
something else in your dependency tree will do this, so this generally isn't a big deal.
McRng
was created initially because MobileCoin builds SGX enclave software in
a strict no_std
environment. Enclaves are generally supposed to get randomness
from the CPU via RDRAND
and not from the OS, because the OS is untrusted in the
SGX security model.
This creates the following needs:
RDRAND
.resolver = 2
innovation, cargo would unify features across build-dependencies and target dependencies,
so if anything in your build.rs
pulled in rand
then you would get the standard library, which made common libraries like rand
toxic to our enclave builds.We wanted to have an RNG type that any of these users can consume, that will be secure and do the right thing on each platform without requiring explicit configuration or other toil from developers.
Because none of the existing RNG libraries quite provided this, we made mc-rand
.
It would be nice if we could improve this so that on targets without rdrand
, a conditional dependency on rand/std
is enabled,
but afaik this is still not possible due to outstanding issues in cargo. (Or maybe it is, and this is tech debt?)
Feel free to use mc-rand
knowing that it will usually do the right thing:
As other targets arise that are of interest, we are happy to improve support for them. McRng
fills a niche in terms of portability and performance that isn't quite filled
by OsRng
or ThreadRng
or other popular crates, and has been audited and battle-tested in production for years.