bc_rand/
lib.rs

1#![doc(html_root_url = "https://docs.rs/bc-rand/0.4.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,
29    rng_next_in_closed_range,
30    rng_next_in_range,
31    rng_next_with_upper_bound,
32    rng_random_array,
33    rng_random_bool,
34    rng_random_data,
35    rng_random_u32,
36    RandomNumberGenerator,
37};
38
39mod secure_random;
40pub use secure_random::{ fill_random_data, random_data, SecureRandomNumberGenerator };
41
42mod seeded_random;
43pub use seeded_random::{
44    fake_random_data,
45    make_fake_random_number_generator,
46    SeededRandomNumberGenerator,
47};
48
49impl RandomNumberGenerator for rand::rngs::ThreadRng {
50    fn random_data(&mut self, size: usize) -> Vec<u8> {
51        let mut data = vec![0u8; size];
52        self.fill_random_data(&mut data);
53        data
54    }
55
56    fn fill_random_data(&mut self, data: &mut [u8]) {
57        self.fill_bytes(data);
58    }
59}
60
61pub fn thread_rng() -> rand::rngs::ThreadRng {
62    rand::thread_rng()
63}
64
65#[cfg(test)]
66mod tests {
67    #[test]
68    fn test_readme_deps() {
69        version_sync::assert_markdown_deps_updated!("README.md");
70    }
71
72    #[test]
73    fn test_html_root_url() {
74        version_sync::assert_html_root_url_updated!("src/lib.rs");
75    }
76}