nanorand/lib.rs
1#![cfg_attr(not(feature = "std"), no_std)]
2#![cfg_attr(docsrs, feature(doc_auto_cfg))]
3#![forbid(missing_docs)]
4#![warn(
5 clippy::perf,
6 clippy::complexity,
7 clippy::style,
8 clippy::correctness,
9 clippy::missing_const_for_fn
10)]
11
12//! A library meant for fast, random number generation with quick compile time, and minimal dependencies.
13//!
14//! # Examples
15//! ## Generating a number with an initialized RNG
16//! ```rust
17//! use nanorand::{Rng, WyRand};
18//!
19//! let mut rng = WyRand::new();
20//! println!("Random number: {}", rng.generate::<u64>());
21//! ```
22//! ## Generating a number with a thread-local RNG
23//! ```rust
24//! use nanorand::Rng;
25//!
26//! let mut rng = nanorand::tls_rng();
27//! println!("Random number: {}", rng.generate::<u64>());
28//! ```
29//! ## Generating a number in a range
30//! ```rust
31//! use nanorand::{Rng, WyRand};
32//!
33//! let mut rng = WyRand::new();
34//! println!("Random number between 1 and 100: {}", rng.generate_range(1_u64..=100));
35//! println!("Random number between -100 and 50: {}", rng.generate_range(-100_i64..=50));
36//! ```
37//! ### Buffering random bytes
38//! ```rust
39//! use nanorand::{Rng, BufferedRng, WyRand};
40//!
41//! let mut thingy = [0u8; 5];
42//! let mut rng = BufferedRng::new(WyRand::new());
43//! rng.fill(&mut thingy);
44//! // As WyRand generates 8 bytes of output, and our target is only 5 bytes,
45//! // 3 bytes will remain in the buffer.
46//! assert_eq!(rng.buffered(), 3);
47//! ```
48//! ## Shuffling a Vec
49//! ```rust
50//! use nanorand::{Rng, WyRand};
51//!
52//! let mut rng = WyRand::new();
53//! let mut items = vec![1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
54//! rng.shuffle(&mut items);
55//! ```
56//!
57//! ## Why should I use this over...
58//!
59//! * `rand` - The standard rand crate is a complex beast. It contains unsafe code in the core implementations, and while it has much more options than we do, that's kind of the point. We're straight to the point, while rand is everything and the kitchen sink.
60//! * `fastrand`, `oorandom`, `random-fast-rng`, or `randomize` - These are all minimal, zero-dep implementations of the PCG family of RNGs (Pcg32 and Pcg64). While these are decent, they are _much_ slower than wyrand (which beats the speed of these Pcg32 implementations while providing 64 random bits), and do not provide CSPRNGs.
61//! * `getrandom` - The getrandom crate just provides OS entropy sources. It is not meant for random number generation. In fact, we provide it as an optional entropy source.
62//!
63//! ## RNG Implementations
64//!
65//! **RNG**|**nanorand type**|**Output Size**|**Cryptographically Secure**|**Speed**<sup>1</sup>|**Notes**|**Original Implementation**
66//! :-----:|:-----:|:-----:|:-----:|:-----:|:-----:|:-----:
67//! wyrand|[`nanorand::WyRand`](rand/wyrand/struct.WyRand.html), [`nanorand::tls::TlsWyRand`](tls/fn.tls_rng.html)|64 bits (`u64`)|🚫|14 GB/s||[https://github.com/lemire/testingRNG/blob/master/source/wyrand.h](https://github.com/lemire/testingRNG/blob/master/source/wyrand.h)
68//! Pcg64|[`nanorand::Pcg64`](rand/pcg64/struct.Pcg64.html)|64 bits (`u64`)|🚫|1.6 GB/s||[https://github.com/rkern/pcg64](https://github.com/rkern/pcg64)
69//! ChaCha|[`nanorand::ChaCha`](rand/chacha/struct.ChaCha.html)|512 bits (`[u32; 16]`)|✅|980 MB/s (ChaCha8), 749 MB/s (ChaCha12), 505 MB/s (ChaCha20)||[https://cr.yp.to/chacha.html](https://cr.yp.to/chacha.html)
70//!
71//! <sup>1. Speed benchmarked on an M1 Macbook Air</sup>
72//!
73//! ## Entropy Sources
74//! _Listed in order of priority_
75//!
76//! * If the `getrandom` feature is enabled, then [`getrandom::getrandom`](https://docs.rs/getrandom/*/getrandom/fn.getrandom.html) will be called, and no other entropy sources will be used.
77//! * If the `rdseed` feature is enabled, and is running on an x86(-64) system with the [RDSEED](https://en.wikipedia.org/wiki/RDRAND) instruction, then
78//! we will attempt to source as much entropy as possible via our [`rdseed_entropy`](entropy::rdseed_entropy) function
79//! * Linux and Android will attempt to use the [`getrandom`](https://man7.org/linux/man-pages/man2/getrandom.2.html) syscall.
80//! * macOS and iOS (Darwin-based systems) will use Security.framework's [`SecRandomCopyBytes`](https://developer.apple.com/documentation/security/1399291-secrandomcopybytes).
81//! * OpenBSD will attempt to use the [`arc4random_buf`](https://man.openbsd.org/arc4random.3) function.
82//! * Windows
83//! * If we're targeting UWP, then the [`BCryptGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/bcrypt/nf-bcrypt-bcryptgenrandom) is used with system-preferred RNG (`BCRYPT_USE_SYSTEM_PREFERRED_RNG`).
84//! * Otherwise, we'll use [`RtlGenRandom`](https://docs.microsoft.com/en-us/windows/win32/api/ntsecapi/nf-ntsecapi-rtlgenrandom).
85//!
86//! ## Feature Flags
87//!
88//! * `alloc` (default) - Enables Rust `alloc` lib features, such as a buffering Rng wrapper.
89//! * `entropy` (default) - Allows sourcing entropy from the system. Implied by `getrandom`, too.
90//! * `std` (default) - Enables Rust `std` lib features, such as seeding from OS entropy sources. Requires `alloc` to be enabled.
91//! * `tls` (default) - Enables a thread-local [`WyRand`](rand/wyrand/struct.WyRand.html) RNG (see below). Requires `std` to be enabled.
92//! * `wyrand` (default) - Enable the [`WyRand`](rand/wyrand/struct.WyRand.html) RNG.
93//! * `pcg64` (default) - Enable the [`Pcg64`](rand/pcg64/struct.Pcg64.html) RNG.
94//! * `chacha` - Enable the [`ChaCha`](rand/chacha/struct.ChaCha.html) RNG. Requires Rust 1.47 or later.
95//! * `rdseed` - On x86 and x86-64 platforms, the `rdseed` intrinsic will be used when OS entropy isn't available.
96//! * `zeroize` - Implement the [Zeroize](https://crates.io/crates/zeroize) trait for all RNGs.
97//! * `getrandom` - Use the [`getrandom`](https://crates.io/crates/getrandom) crate as an entropy source. Works on most systems, optional due to the fact that it brings in more dependencies.
98//!
99//! ## MSRV
100//! The minimum supported Rust version for the latest version of nanorand is **Rust 1.56.0**, released October 21st, 2021.
101
102#[cfg(feature = "alloc")]
103extern crate alloc;
104
105#[cfg(feature = "alloc")]
106pub use buffer::BufferedRng;
107pub use gen::*;
108pub use rand::*;
109#[cfg(feature = "tls")]
110pub use tls::tls_rng;
111
112#[cfg(feature = "alloc")]
113/// Provides a buffered wrapper for RNGs, preventing bits from being wasted.
114pub mod buffer;
115/// Implementation of cryptography, for CSPRNGs.
116pub mod crypto;
117/// Sources for obtaining entropy.
118#[cfg(any(feature = "entropy", feature = "getrandom"))]
119pub mod entropy;
120/// Traits for generating types from an RNG.
121pub mod gen;
122/// RNG algorithms.
123pub mod rand;
124#[cfg(feature = "tls")]
125/// Provides a thread-local [`WyRand`] RNG.
126pub mod tls;