arnaucube
arnaucube/miden-cryptoPublic Self-hosted repository
miden-crypto/src/rand/rpx.rs
src/rand/rpx.rsRaw
  1use alloc::{string::ToString, vec::Vec};
  2
  3use rand_core::impls;
  4
  5use super::{Felt, FeltRng, FieldElement, RandomCoin, RandomCoinError, RngCore, Word, ZERO};
  6use crate::{
  7    hash::rpx::{Rpx256, RpxDigest},
  8    utils::{ByteReader, ByteWriter, Deserializable, DeserializationError, Serializable},
  9};
 10
 11// CONSTANTS
 12// ================================================================================================
 13
 14const STATE_WIDTH: usize = Rpx256::STATE_WIDTH;
 15const RATE_START: usize = Rpx256::RATE_RANGE.start;
 16const RATE_END: usize = Rpx256::RATE_RANGE.end;
 17const HALF_RATE_WIDTH: usize = (Rpx256::RATE_RANGE.end - Rpx256::RATE_RANGE.start) / 2;
 18
 19// RPX RANDOM COIN
 20// ================================================================================================
 21/// A simplified version of the `SPONGE_PRG` reseedable pseudo-random number generator algorithm
 22/// described in <https://eprint.iacr.org/2011/499.pdf>.
 23///
 24/// The simplification is related to the following facts:
 25/// 1. A call to the reseed method implies one and only one call to the permutation function. This
 26///    is possible because in our case we never reseed with more than 4 field elements.
 27/// 2. As a result of the previous point, we don't make use of an input buffer to accumulate seed
 28///    material.
 29#[derive(Debug, Clone, Copy, PartialEq, Eq)]
 30pub struct RpxRandomCoin {
 31    state: [Felt; STATE_WIDTH],
 32    current: usize,
 33}
 34
 35impl RpxRandomCoin {
 36    /// Returns a new [RpxRandomCoin] initialize with the specified seed.
 37    pub fn new(seed: Word) -> Self {
 38        let mut state = [ZERO; STATE_WIDTH];
 39
 40        for i in 0..HALF_RATE_WIDTH {
 41            state[RATE_START + i] += seed[i];
 42        }
 43
 44        // Absorb
 45        Rpx256::apply_permutation(&mut state);
 46
 47        RpxRandomCoin { state, current: RATE_START }
 48    }
 49
 50    /// Returns an [RpxRandomCoin] instantiated from the provided components.
 51    ///
 52    /// # Panics
 53    /// Panics if `current` is smaller than 4 or greater than or equal to 12.
 54    pub fn from_parts(state: [Felt; STATE_WIDTH], current: usize) -> Self {
 55        assert!(
 56            (RATE_START..RATE_END).contains(&current),
 57            "current value outside of valid range"
 58        );
 59        Self { state, current }
 60    }
 61
 62    /// Returns components of this random coin.
 63    pub fn into_parts(self) -> ([Felt; STATE_WIDTH], usize) {
 64        (self.state, self.current)
 65    }
 66
 67    /// Fills `dest` with random data.
 68    pub fn fill_bytes(&mut self, dest: &mut [u8]) {
 69        <Self as RngCore>::fill_bytes(self, dest)
 70    }
 71
 72    fn draw_basefield(&mut self) -> Felt {
 73        if self.current == RATE_END {
 74            Rpx256::apply_permutation(&mut self.state);
 75            self.current = RATE_START;
 76        }
 77
 78        self.current += 1;
 79        self.state[self.current - 1]
 80    }
 81}
 82
 83// RANDOM COIN IMPLEMENTATION
 84// ------------------------------------------------------------------------------------------------
 85
 86impl RandomCoin for RpxRandomCoin {
 87    type BaseField = Felt;
 88    type Hasher = Rpx256;
 89
 90    fn new(seed: &[Self::BaseField]) -> Self {
 91        let digest: Word = Rpx256::hash_elements(seed).into();
 92        Self::new(digest)
 93    }
 94
 95    fn reseed(&mut self, data: RpxDigest) {
 96        // Reset buffer
 97        self.current = RATE_START;
 98
 99        // Add the new seed material to the first half of the rate portion of the RPX state
100        let data: Word = data.into();
101
102        self.state[RATE_START] += data[0];
103        self.state[RATE_START + 1] += data[1];
104        self.state[RATE_START + 2] += data[2];
105        self.state[RATE_START + 3] += data[3];
106
107        // Absorb
108        Rpx256::apply_permutation(&mut self.state);
109    }
110
111    fn check_leading_zeros(&self, value: u64) -> u32 {
112        let value = Felt::new(value);
113        let mut state_tmp = self.state;
114
115        state_tmp[RATE_START] += value;
116
117        Rpx256::apply_permutation(&mut state_tmp);
118
119        let first_rate_element = state_tmp[RATE_START].as_int();
120        first_rate_element.trailing_zeros()
121    }
122
123    fn draw<E: FieldElement<BaseField = Felt>>(&mut self) -> Result<E, RandomCoinError> {
124        let ext_degree = E::EXTENSION_DEGREE;
125        let mut result = vec![ZERO; ext_degree];
126        for r in result.iter_mut().take(ext_degree) {
127            *r = self.draw_basefield();
128        }
129
130        let result = E::slice_from_base_elements(&result);
131        Ok(result[0])
132    }
133
134    fn draw_integers(
135        &mut self,
136        num_values: usize,
137        domain_size: usize,
138        nonce: u64,
139    ) -> Result<Vec<usize>, RandomCoinError> {
140        assert!(domain_size.is_power_of_two(), "domain size must be a power of two");
141        assert!(num_values < domain_size, "number of values must be smaller than domain size");
142
143        // absorb the nonce
144        let nonce = Felt::new(nonce);
145        self.state[RATE_START] += nonce;
146        Rpx256::apply_permutation(&mut self.state);
147
148        // reset the buffer
149        self.current = RATE_START;
150
151        // determine how many bits are needed to represent valid values in the domain
152        let v_mask = (domain_size - 1) as u64;
153
154        // draw values from PRNG until we get as many unique values as specified by num_queries
155        let mut values = Vec::new();
156        for _ in 0..1000 {
157            // get the next pseudo-random field element
158            let value = self.draw_basefield().as_int();
159
160            // use the mask to get a value within the range
161            let value = (value & v_mask) as usize;
162
163            values.push(value);
164            if values.len() == num_values {
165                break;
166            }
167        }
168
169        if values.len() < num_values {
170            return Err(RandomCoinError::FailedToDrawIntegers(num_values, values.len(), 1000));
171        }
172
173        Ok(values)
174    }
175
176    fn reseed_with_salt(
177        &mut self,
178        data: <Self::Hasher as winter_crypto::Hasher>::Digest,
179        salt: Option<<Self::Hasher as winter_crypto::Hasher>::Digest>,
180    ) {
181        // Reset buffer
182        self.current = RATE_START;
183
184        // Add the new seed material to the first half of the rate portion of the RPO state
185        let data: Word = data.into();
186
187        self.state[RATE_START] += data[0];
188        self.state[RATE_START + 1] += data[1];
189        self.state[RATE_START + 2] += data[2];
190        self.state[RATE_START + 3] += data[3];
191
192        if let Some(salt) = salt {
193            // Add the salt to the second half of the rate portion of the RPO state
194            let data: Word = salt.into();
195
196            self.state[RATE_START + 4] += data[0];
197            self.state[RATE_START + 5] += data[1];
198            self.state[RATE_START + 6] += data[2];
199            self.state[RATE_START + 7] += data[3];
200        }
201
202        // Absorb
203        Rpx256::apply_permutation(&mut self.state);
204    }
205}
206
207// FELT RNG IMPLEMENTATION
208// ------------------------------------------------------------------------------------------------
209
210impl FeltRng for RpxRandomCoin {
211    fn draw_element(&mut self) -> Felt {
212        self.draw_basefield()
213    }
214
215    fn draw_word(&mut self) -> Word {
216        let mut output = [ZERO; 4];
217        for o in output.iter_mut() {
218            *o = self.draw_basefield();
219        }
220        output
221    }
222}
223
224// RNGCORE IMPLEMENTATION
225// ------------------------------------------------------------------------------------------------
226
227impl RngCore for RpxRandomCoin {
228    fn next_u32(&mut self) -> u32 {
229        self.draw_basefield().as_int() as u32
230    }
231
232    fn next_u64(&mut self) -> u64 {
233        impls::next_u64_via_u32(self)
234    }
235
236    fn fill_bytes(&mut self, dest: &mut [u8]) {
237        impls::fill_bytes_via_next(self, dest)
238    }
239
240    fn try_fill_bytes(&mut self, dest: &mut [u8]) -> Result<(), rand::Error> {
241        self.fill_bytes(dest);
242        Ok(())
243    }
244}
245
246// SERIALIZATION
247// ------------------------------------------------------------------------------------------------
248
249impl Serializable for RpxRandomCoin {
250    fn write_into<W: ByteWriter>(&self, target: &mut W) {
251        self.state.iter().for_each(|v| v.write_into(target));
252        // casting to u8 is OK because `current` is always between 4 and 12.
253        target.write_u8(self.current as u8);
254    }
255}
256
257impl Deserializable for RpxRandomCoin {
258    fn read_from<R: ByteReader>(source: &mut R) -> Result<Self, DeserializationError> {
259        let state = [
260            Felt::read_from(source)?,
261            Felt::read_from(source)?,
262            Felt::read_from(source)?,
263            Felt::read_from(source)?,
264            Felt::read_from(source)?,
265            Felt::read_from(source)?,
266            Felt::read_from(source)?,
267            Felt::read_from(source)?,
268            Felt::read_from(source)?,
269            Felt::read_from(source)?,
270            Felt::read_from(source)?,
271            Felt::read_from(source)?,
272        ];
273        let current = source.read_u8()? as usize;
274        if !(RATE_START..RATE_END).contains(&current) {
275            return Err(DeserializationError::InvalidValue(
276                "current value outside of valid range".to_string(),
277            ));
278        }
279        Ok(Self { state, current })
280    }
281}
282
283// TESTS
284// ================================================================================================
285
286#[cfg(test)]
287mod tests {
288    use super::{Deserializable, FeltRng, RpxRandomCoin, Serializable, ZERO};
289    use crate::ONE;
290
291    #[test]
292    fn test_feltrng_felt() {
293        let mut rpxcoin = RpxRandomCoin::new([ZERO; 4]);
294        let output = rpxcoin.draw_element();
295
296        let mut rpxcoin = RpxRandomCoin::new([ZERO; 4]);
297        let expected = rpxcoin.draw_basefield();
298
299        assert_eq!(output, expected);
300    }
301
302    #[test]
303    fn test_feltrng_word() {
304        let mut rpxcoin = RpxRandomCoin::new([ZERO; 4]);
305        let output = rpxcoin.draw_word();
306
307        let mut rpocoin = RpxRandomCoin::new([ZERO; 4]);
308        let mut expected = [ZERO; 4];
309        for o in expected.iter_mut() {
310            *o = rpocoin.draw_basefield();
311        }
312
313        assert_eq!(output, expected);
314    }
315
316    #[test]
317    fn test_feltrng_serialization() {
318        let coin1 = RpxRandomCoin::from_parts([ONE; 12], 5);
319
320        let bytes = coin1.to_bytes();
321        let coin2 = RpxRandomCoin::read_from_bytes(&bytes).unwrap();
322        assert_eq!(coin1, coin2);
323    }
324}