hashx/
rand.rs

1//! Pseudorandom number utilities for HashX's program generator
2//!
3//! HashX uses pseudorandom numbers to make individual decisions in the program
4//! generation process. The program generator consumes u8 and u32 values that
5//! use a shared u64 generator, implemented using SipHash1,3.
6//!
7//! We use the [`RngCore`] trait for this underlying u64 generator,
8//! allowing substitute random number generators for testing or for special
9//! purposes that don't require compatibility with HashX proper.
10//!
11//! The stateful u8 and u32 layer comes from this module's [`RngBuffer`].
12//! It's important for the u8 and u32 queues to share a common generator.
13//! The order of dequeueing u8 items vs u32 items intentionally modifies the
14//! assignment of particular u64 [`RngCore`] values to the two queues.
15
16use crate::siphash::{siphash13_ctr, SipState};
17use arrayvec::ArrayVec;
18use rand_core::RngCore;
19
20/// Wrap a [`RngCore`] implementation for fast `u8` and `u32` output.
21///
22/// This maintains small queues for each data type: up to one `u32` and up to
23/// 7 bytes. The queueing behavior matches convenions required by HashX:
24/// The underlying `u64` values are always generated lazily, and component
25/// values are extracted in big endian order.
26#[derive(Debug)]
27pub(crate) struct RngBuffer<'a, T: RngCore> {
28    /// Inner [`RngCore`] implementation
29    inner: &'a mut T,
30    /// Buffer of remaining u8 values from breaking up a u64
31    u8_vec: ArrayVec<u8, 7>,
32    /// Up to one buffered u32 value
33    u32_opt: Option<u32>,
34}
35
36impl<'a, T: RngCore> RngBuffer<'a, T> {
37    /// Construct a new empty buffer around a [`RngCore`] implementation.
38    ///
39    /// No actual random numbers will be generated until the first call to
40    /// [`Self::next_u8`] or [`Self::next_u32`].
41    #[inline(always)]
42    pub(crate) fn new(rng: &'a mut T) -> Self {
43        Self {
44            inner: rng,
45            u8_vec: Default::default(),
46            u32_opt: None,
47        }
48    }
49
50    /// Request 32 bits from the buffered random number generator.
51    ///
52    /// If we have buffered data stored, returns that. If not,
53    /// requests 64 bits from the [`RngCore`] and saves half for later.
54    #[inline(always)]
55    pub(crate) fn next_u32(&mut self) -> u32 {
56        let previous = self.u32_opt;
57        match previous {
58            Some(value) => {
59                self.u32_opt = None;
60                value
61            }
62            None => {
63                let value = self.inner.next_u64();
64                self.u32_opt = Some(value as u32);
65                (value >> 32) as u32
66            }
67        }
68    }
69
70    /// Request 8 bits from the buffered random number generator.
71    ///
72    /// If we have buffered data stored, returns that. If not,
73    /// requests 64 bits from the [`RngCore`] and saves 7 bytes for later.
74    #[inline(always)]
75    pub(crate) fn next_u8(&mut self) -> u8 {
76        let value = self.u8_vec.pop();
77        match value {
78            Some(value) => value,
79            None => {
80                // Little endian (reversed) order here,
81                // because we dequeue items from the end of the Vec.
82                let bytes = self.inner.next_u64().to_le_bytes();
83                let (last, saved) = bytes.split_last().expect("u64 has nonzero length");
84                self.u8_vec
85                    .try_extend_from_slice(saved)
86                    .expect("slice length correct");
87                *last
88            }
89        }
90    }
91}
92
93/// HashX-style random number generator built on SipHash1,3
94///
95/// This is an implementation of [`RngCore`] using SipHash1,3 as
96/// the 64-bit PRNG layer needed by HashX's program generator.
97#[derive(Debug, Clone)]
98pub struct SipRand {
99    /// SipHash state vector used as input to SipHash1,3 in counter mode
100    key: SipState,
101    /// Next unused counter value
102    counter: u64,
103}
104
105impl SipRand {
106    /// Build a new SipHash random number generator.
107    ///
108    /// The internal SipHash1,3 generator is initialized to a supplied
109    /// internal state, and the counter is reset to zero.
110    #[inline(always)]
111    pub fn new(key: SipState) -> Self {
112        Self::new_with_counter(key, 0)
113    }
114
115    /// Build a new [`SipRand`] with a specific initial counter value.
116    #[inline(always)]
117    pub fn new_with_counter(key: SipState, counter: u64) -> Self {
118        Self { key, counter }
119    }
120}
121
122impl RngCore for SipRand {
123    /// Generate a full 64-bit random result using SipHash1,3.
124    fn next_u64(&mut self) -> u64 {
125        let value = siphash13_ctr(self.key, self.counter);
126        self.counter += 1;
127        value
128    }
129
130    /// Return a 32-bit value by discarding the upper half of a 64-bit result.
131    fn next_u32(&mut self) -> u32 {
132        self.next_u64() as u32
133    }
134
135    /// Fill `dest` with random data.
136    fn fill_bytes(&mut self, dest: &mut [u8]) {
137        rand_core::impls::fill_bytes_via_next(self, dest);
138    }
139}
140
141#[cfg(test)]
142mod test {
143    use super::{RngBuffer, SipRand, SipState};
144
145    #[test]
146    fn rng_vectors() {
147        // Check against pseudorandom number streams seen during tor unit tests
148
149        let (key0, _key1) = SipState::pair_from_seed(b"abc");
150        let mut rng_inner = SipRand::new(key0);
151        let mut rng = RngBuffer::new(&mut rng_inner);
152
153        #[derive(Debug, PartialEq)]
154        enum Value {
155            U32(u32),
156            U8(u8),
157        }
158
159        let expected = vec![
160            Value::U32(0xf695edd0),
161            Value::U32(0x2205449d),
162            Value::U32(0x51c1ac51),
163            Value::U32(0xcd19a7d1),
164            Value::U8(0xad),
165            Value::U32(0x79793a52),
166            Value::U32(0xd965083d),
167            Value::U8(0xf4),
168            Value::U32(0x915e9969),
169            Value::U32(0x7563b6e2),
170            Value::U32(0x4e5a9d8b),
171            Value::U32(0xef2bb9ce),
172            Value::U8(0xcb),
173            Value::U32(0xa4beee16),
174            Value::U32(0x78fa6e6f),
175            Value::U8(0x30),
176            Value::U32(0xc321cb9f),
177            Value::U32(0xbbf29635),
178            Value::U32(0x919450f4),
179            Value::U32(0xf3d8f358),
180            Value::U8(0x3b),
181            Value::U32(0x818a72e9),
182            Value::U32(0x58225fcf),
183            Value::U8(0x98),
184            Value::U32(0x3fcb5059),
185            Value::U32(0xaf5bcb70),
186            Value::U8(0x14),
187            Value::U32(0xd41e0326),
188            Value::U32(0xe79aebc6),
189            Value::U32(0xa348672c),
190            Value::U8(0xcf),
191            Value::U32(0x5d51b520),
192            Value::U32(0x73afc36f),
193            Value::U32(0x31348711),
194            Value::U32(0xca25b040),
195            Value::U32(0x3700c37b),
196            Value::U8(0x62),
197            Value::U32(0xf0d1d6a6),
198            Value::U32(0xc1edebf3),
199            Value::U8(0x9d),
200            Value::U32(0x9bb1f33f),
201            Value::U32(0xf1309c95),
202            Value::U32(0x0797718a),
203            Value::U32(0xa3bbcf7e),
204            Value::U8(0x80),
205            Value::U8(0x28),
206            Value::U8(0xe9),
207            Value::U8(0x2e),
208            Value::U32(0xf5506289),
209            Value::U32(0x97b46d7c),
210            Value::U8(0x64),
211            Value::U32(0xc99fe4ad),
212            Value::U32(0x6e756189),
213            Value::U8(0x54),
214            Value::U8(0xf7),
215            Value::U8(0x0f),
216            Value::U8(0x7d),
217            Value::U32(0x38c983eb),
218        ];
219
220        let mut actual = Vec::new();
221        for item in &expected {
222            match item {
223                Value::U8(_) => actual.push(Value::U8(rng.next_u8())),
224                Value::U32(_) => actual.push(Value::U32(rng.next_u32())),
225            }
226        }
227
228        assert_eq!(expected, actual);
229    }
230}