Skip to main content

miden_crypto/rand/
mod.rs

1//! Pseudo-random element generation.
2
3use p3_field::PrimeField64;
4use rand::RngCore;
5
6use crate::{Felt, Word};
7
8mod rpo;
9pub use rpo::RpoRandomCoin;
10
11mod rpx;
12pub use rpx::RpxRandomCoin;
13
14// Test utilities for generating random data (used in tests and benchmarks)
15#[cfg(any(test, feature = "std"))]
16pub mod test_utils;
17
18// RANDOMNESS (ported from Winterfell's winter-utils)
19// ================================================================================================
20
21/// Defines how `Self` can be read from a sequence of random bytes.
22pub trait Randomizable: Sized {
23    /// Size of `Self` in bytes.
24    ///
25    /// This is used to determine how many bytes should be passed to the
26    /// [from_random_bytes()](Self::from_random_bytes) function.
27    const VALUE_SIZE: usize;
28
29    /// Returns `Self` if the set of bytes forms a valid value, otherwise returns None.
30    fn from_random_bytes(source: &[u8]) -> Option<Self>;
31}
32
33impl Randomizable for u128 {
34    const VALUE_SIZE: usize = 16;
35
36    fn from_random_bytes(source: &[u8]) -> Option<Self> {
37        if let Ok(bytes) = source[..Self::VALUE_SIZE].try_into() {
38            Some(u128::from_le_bytes(bytes))
39        } else {
40            None
41        }
42    }
43}
44
45impl Randomizable for u64 {
46    const VALUE_SIZE: usize = 8;
47
48    fn from_random_bytes(source: &[u8]) -> Option<Self> {
49        if let Ok(bytes) = source[..Self::VALUE_SIZE].try_into() {
50            Some(u64::from_le_bytes(bytes))
51        } else {
52            None
53        }
54    }
55}
56
57impl Randomizable for u32 {
58    const VALUE_SIZE: usize = 4;
59
60    fn from_random_bytes(source: &[u8]) -> Option<Self> {
61        if let Ok(bytes) = source[..Self::VALUE_SIZE].try_into() {
62            Some(u32::from_le_bytes(bytes))
63        } else {
64            None
65        }
66    }
67}
68
69impl Randomizable for u16 {
70    const VALUE_SIZE: usize = 2;
71
72    fn from_random_bytes(source: &[u8]) -> Option<Self> {
73        if let Ok(bytes) = source[..Self::VALUE_SIZE].try_into() {
74            Some(u16::from_le_bytes(bytes))
75        } else {
76            None
77        }
78    }
79}
80
81impl Randomizable for u8 {
82    const VALUE_SIZE: usize = 1;
83
84    fn from_random_bytes(source: &[u8]) -> Option<Self> {
85        Some(source[0])
86    }
87}
88
89impl Randomizable for Felt {
90    const VALUE_SIZE: usize = 8;
91
92    fn from_random_bytes(source: &[u8]) -> Option<Self> {
93        if let Ok(bytes) = source[..Self::VALUE_SIZE].try_into() {
94            let value = u64::from_le_bytes(bytes);
95            // Ensure the value is within the field modulus
96            if value < Felt::ORDER_U64 {
97                Some(Felt::new(value))
98            } else {
99                None
100            }
101        } else {
102            None
103        }
104    }
105}
106
107impl<const N: usize> Randomizable for [u8; N] {
108    const VALUE_SIZE: usize = N;
109
110    fn from_random_bytes(source: &[u8]) -> Option<Self> {
111        let mut result = [Default::default(); N];
112        result.copy_from_slice(source);
113
114        Some(result)
115    }
116}
117
118/// Pseudo-random element generator.
119///
120/// An instance can be used to draw, uniformly at random, base field elements as well as [Word]s.
121pub trait FeltRng: RngCore {
122    /// Draw, uniformly at random, a base field element.
123    fn draw_element(&mut self) -> Felt;
124
125    /// Draw, uniformly at random, a [Word].
126    fn draw_word(&mut self) -> Word;
127}
128
129// RANDOM VALUE GENERATION FOR TESTING
130// ================================================================================================
131
132/// Generates a random field element for testing purposes.
133///
134/// This function is only available with the `std` feature.
135#[cfg(feature = "std")]
136pub fn random_felt() -> Felt {
137    use rand::Rng;
138    let mut rng = rand::rng();
139    // Goldilocks field order is 2^64 - 2^32 + 1
140    // Generate a random u64 and reduce modulo the field order
141    Felt::new(rng.random::<u64>())
142}
143
144/// Generates a random word (4 field elements) for testing purposes.
145///
146/// This function is only available with the `std` feature.
147#[cfg(feature = "std")]
148pub fn random_word() -> Word {
149    Word::new([random_felt(), random_felt(), random_felt(), random_felt()])
150}