concrete_csprng/seeders/
mod.rs

1//! A module containing seeders objects.
2//!
3//! When initializing a generator, one needs to provide a [`Seed`], which is then used as key to the
4//! AES blockcipher. As a consequence, the quality of the outputs of the generator is directly
5//! conditioned by the quality of this seed. This module proposes different mechanisms to deliver
6//! seeds that can accommodate varying scenarios.
7
8/// A seed value, used to initialize a generator.
9#[derive(Debug, Copy, Clone, PartialEq, Eq)]
10pub struct Seed(pub u128);
11
12/// A trait representing a seeding strategy.
13pub trait Seeder {
14    /// Generates a new seed.
15    fn seed(&mut self) -> Seed;
16
17    /// Check whether the seeder can be used on the current machine. This function may check if some
18    /// required CPU features are available or if some OS features are available for example.
19    fn is_available() -> bool
20    where
21        Self: Sized;
22}
23
24mod implem;
25// This import statement can be empty if seeder features are disabled, rustc's behavior changed to
26// warn of empty modules, we know this can happen, so allow it.
27#[allow(unused_imports)]
28pub use implem::*;
29
30#[cfg(test)]
31mod generic_tests {
32    use crate::seeders::Seeder;
33
34    /// Naively verifies that two fixed-size sequences generated by repeatedly calling the seeder
35    /// are different.
36    #[allow(unused)] // to please clippy when tests are not activated
37    pub fn check_seeder_fixed_sequences_different<S: Seeder, F: Fn(u128) -> S>(
38        construct_seeder: F,
39    ) {
40        const SEQUENCE_SIZE: usize = 500;
41        const REPEATS: usize = 10_000;
42        for i in 0..REPEATS {
43            let mut seeder = construct_seeder(i as u128);
44            let orig_seed = seeder.seed();
45            for _ in 0..SEQUENCE_SIZE {
46                assert_ne!(seeder.seed(), orig_seed);
47            }
48        }
49    }
50}