osom_lib_prng/prngs/
chacha.rs

1use crate::{
2    block_prng::BlockPRNG,
3    block_streams::ChaChaStream,
4    traits::{CryptographicallySecure, PRNGenerator, Splittable}
5};
6
7/// The `ChaCha` based PRNG. It is a cryptographically secure PRNG.
8/// 
9/// # Notes
10/// 
11/// * It is significantly slower than non-cryptographically secure PRNGS,
12///   e.g. around ~10x slower than LCG.
13/// * The recommended `ROUNDS` value is 20.
14pub type ChaCha<const ROUNDS: u32> = BlockPRNG<ChaChaStream<ROUNDS>>;
15
16unsafe impl<const ROUNDS: u32> CryptographicallySecure for ChaCha<ROUNDS> { }
17
18impl<const ROUNDS: u32> Splittable for ChaCha<ROUNDS> {
19    fn split(&mut self) -> Self {
20        let new_key = self.generate::<[u8; 32]>();
21        let new_nonce = self.generate::<[u8; 12]>();
22        let new_chacha_stream = ChaChaStream::from_arrays(new_key, new_nonce);
23        BlockPRNG::new(new_chacha_stream)
24    }
25}
26
27/// The recommended [`ChaCha`] PRNG with 20 rounds.
28pub type DefaultChaCha = ChaCha<20>;