bc_rand/
lib.rs

1#![doc(html_root_url = "https://docs.rs/bc-rand/0.3.0")]
2#![warn(rust_2018_idioms)]
3
4//! # Blockchain Commons Random Number Utilities
5//!
6//!
7//! `bc-rand` exposes a uniform API for the random number primitives used
8//! in higher-level [Blockchain Commons](https://blockchaincommons.com)
9//! projects, including a cryptographically strong random number generator
10//! [`SecureRandomNumberGenerator`] and a deterministic random number generator
11//! [`SeededRandomNumberGenerator`].
12//!
13//! These primitive random number generators implement the
14//! [`RandomNumberGenerator`] trait to produce random numbers compatible with
15//! the `RandomNumberGenerator` Swift protocol used in MacOS and iOS, which is
16//! important when using the deterministic random number generator for
17//! cross-platform testing.
18//!
19//! The crate also includes several convenience functions for generating secure
20//! and deterministic random numbers.
21
22mod magnitude;
23mod widening;
24
25mod random_number_generator;
26use rand::RngCore;
27pub use random_number_generator::{
28    rng_fill_random_data, rng_next_in_closed_range, rng_next_in_range, rng_next_with_upper_bound,
29    rng_random_array, rng_random_bool, rng_random_data, rng_random_u32, RandomNumberGenerator,
30};
31
32mod secure_random;
33pub use secure_random::{fill_random_data, random_data, SecureRandomNumberGenerator};
34
35mod seeded_random;
36pub use seeded_random::{
37    fake_random_data, make_fake_random_number_generator, SeededRandomNumberGenerator,
38};
39
40impl RandomNumberGenerator for rand::rngs::ThreadRng {
41    fn random_data(&mut self, size: usize) -> Vec<u8> {
42        let mut data = vec![0u8; size];
43        self.fill_random_data(&mut data);
44        data
45    }
46
47    fn fill_random_data(&mut self, data: &mut [u8]) {
48        self.fill_bytes(data);
49    }
50}
51
52pub fn thread_rng() -> rand::rngs::ThreadRng {
53    rand::thread_rng()
54}
55
56#[cfg(test)]
57mod tests {
58    #[test]
59    fn test_readme_deps() {
60        version_sync::assert_markdown_deps_updated!("README.md");
61    }
62
63    #[test]
64    fn test_html_root_url() {
65        version_sync::assert_html_root_url_updated!("src/lib.rs");
66    }
67}