Skip to main content

qualia_core_db/net/qdnf/harness/
entropy.rs

1//! Seeded entropy for tests. Production paths must never select this.
2
3use crate::net::qdnf::errors::QdnfError;
4
5/// Which generator a caller asked for. `Test` exists only under `cfg(test)`.
6#[derive(Clone, Copy, Debug, PartialEq, Eq)]
7pub enum EntropySource {
8    Os,
9    #[cfg(test)]
10    Test,
11}
12
13/// Deterministic LCG used by harness tests. Product code must not call this.
14pub struct SeededEntropy {
15    state: u64,
16}
17
18impl SeededEntropy {
19    /// Creates a default `SeededEntropy`.
20    pub const fn new(seed: u64) -> Self {
21        Self { state: seed | 1 }
22    }
23
24    /// `SeededEntropy::fill`.
25    pub fn fill(&mut self, out: &mut [u8]) -> Result<(), QdnfError> {
26        for byte in out.iter_mut() {
27            self.state = self.state.wrapping_mul(6364136223846793005).wrapping_add(1);
28            *byte = (self.state >> 32) as u8;
29        }
30        Ok(())
31    }
32}
33
34/// Production entropy. Always OS bytes; never a seeded LCG.
35#[derive(Clone, Copy, Debug, Default)]
36pub struct ProductionEntropy;
37
38impl ProductionEntropy {
39    /// Creates `ProductionEntropy` with the given state.
40    pub const fn new() -> Self {
41        Self
42    }
43
44    /// Source reference.
45    pub const fn source(self) -> EntropySource {
46        EntropySource::Os
47    }
48
49    /// Fills `out` with OS entropy.
50    pub fn fill(&self, out: &mut [u8]) -> Result<(), QdnfError> {
51        fill_os(out)
52    }
53}
54
55/// Production entropy. Test code must not call this from SeededEntropy.
56pub fn fill_os(out: &mut [u8]) -> Result<(), QdnfError> {
57    getrandom::fill(out).map_err(|_| QdnfError::EntropyFailure)
58}
59
60#[cfg(test)]
61mod tests {
62    use super::*;
63
64    #[test]
65    fn seeded_entropy_is_not_used_as_os_entropy() {
66        let mut seeded = SeededEntropy::new(7);
67        let mut expected = [0u8; 16];
68        SeededEntropy::new(7).fill(&mut expected).unwrap();
69
70        let mut os_bytes = [0u8; 16];
71        ProductionEntropy.fill(&mut os_bytes).unwrap();
72
73        let mut from_seeded = [0u8; 16];
74        seeded.fill(&mut from_seeded).unwrap();
75
76        assert_eq!(from_seeded, expected);
77        assert_ne!(os_bytes, expected);
78        assert_eq!(ProductionEntropy.source(), EntropySource::Os);
79        assert_eq!(EntropySource::Test, EntropySource::Test);
80    }
81
82    #[test]
83    fn seeded_fill_is_repeatable() {
84        let mut a = [0u8; 8];
85        let mut b = [0u8; 8];
86        SeededEntropy::new(99).fill(&mut a).unwrap();
87        SeededEntropy::new(99).fill(&mut b).unwrap();
88        assert_eq!(a, b);
89    }
90
91    #[test]
92    fn fill_os_does_not_advance_seeded_state() {
93        let mut seeded = SeededEntropy::new(3);
94        let mut before = [0u8; 4];
95        SeededEntropy::new(3).fill(&mut before).unwrap();
96
97        let mut os = [0u8; 4];
98        fill_os(&mut os).unwrap();
99
100        let mut after = [0u8; 4];
101        seeded.fill(&mut after).unwrap();
102        assert_eq!(after, before);
103    }
104}