bc_rand/
lib.rs

1#![doc(html_root_url = "https://docs.rs/bc-rand/0.5.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    RandomNumberGenerator, rng_fill_random_data, rng_next_in_closed_range,
29    rng_next_in_range, rng_next_with_upper_bound, rng_random_array,
30    rng_random_bool, rng_random_data, rng_random_u32,
31};
32
33mod secure_random;
34pub use secure_random::{
35    SecureRandomNumberGenerator, fill_random_data, random_data,
36};
37
38mod seeded_random;
39pub use seeded_random::{
40    SeededRandomNumberGenerator, fake_random_data,
41    make_fake_random_number_generator,
42};
43
44impl RandomNumberGenerator for rand::rngs::ThreadRng {
45    fn random_data(&mut self, size: usize) -> Vec<u8> {
46        let mut data = vec![0u8; size];
47        self.fill_random_data(&mut data);
48        data
49    }
50
51    fn fill_random_data(&mut self, data: &mut [u8]) { self.fill_bytes(data); }
52}
53
54pub fn thread_rng() -> rand::rngs::ThreadRng { rand::rng() }
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}