1//! Random number generator support
23use super::Uint;
4use crate::{Encoding, Limb, NonZero, Random, RandomMod};
5use rand_core::CryptoRngCore;
6use subtle::ConstantTimeLess;
78impl<const LIMBS: usize> Random for Uint<LIMBS> {
9/// Generate a cryptographically secure random [`Uint`].
10fn random(mut rng: &mut impl CryptoRngCore) -> Self {
11let mut limbs = [Limb::ZERO; LIMBS];
1213for limb in &mut limbs {
14*limb = Limb::random(&mut rng)
15 }
1617 limbs.into()
18 }
19}
2021impl<const LIMBS: usize> RandomMod for Uint<LIMBS> {
22/// Generate a cryptographically secure random [`Uint`] which is less than
23 /// a given `modulus`.
24 ///
25 /// This function uses rejection sampling, a method which produces an
26 /// unbiased distribution of in-range values provided the underlying
27 /// CSRNG is unbiased, but runs in variable-time.
28 ///
29 /// The variable-time nature of the algorithm should not pose a security
30 /// issue so long as the underlying random number generator is truly a
31 /// CSRNG, where previous outputs are unrelated to subsequent
32 /// outputs and do not reveal information about the RNG's internal state.
33fn random_mod(rng: &mut impl CryptoRngCore, modulus: &NonZero<Self>) -> Self {
34let mut n = Self::ZERO;
3536let n_bits = modulus.as_ref().bits_vartime();
37let n_bytes = (n_bits + 7) / 8;
38let n_limbs = (n_bits + Limb::BITS - 1) / Limb::BITS;
39let hi_bytes = n_bytes - (n_limbs - 1) * Limb::BYTES;
4041let mut bytes = Limb::ZERO.to_le_bytes();
4243loop {
44for i in 0..n_limbs - 1 {
45 rng.fill_bytes(bytes.as_mut());
46// Need to deserialize from little-endian to make sure that two 32-bit limbs
47 // deserialized sequentially are equal to one 64-bit limb produced from the same
48 // byte stream.
49n.limbs[i] = Limb::from_le_bytes(bytes);
50 }
5152// Generate the high limb which may need to only be filled partially.
53bytes.as_mut().fill(0);
54 rng.fill_bytes(&mut (bytes.as_mut()[0..hi_bytes]));
55 n.limbs[n_limbs - 1] = Limb::from_le_bytes(bytes);
5657if n.ct_lt(modulus).into() {
58return n;
59 }
60 }
61 }
62}
6364#[cfg(test)]
65mod tests {
66use crate::{NonZero, RandomMod, U256};
67use rand_core::SeedableRng;
6869#[test]
70fn random_mod() {
71let mut rng = rand_chacha::ChaCha8Rng::seed_from_u64(1);
7273// Ensure `random_mod` runs in a reasonable amount of time
74let modulus = NonZero::new(U256::from(42u8)).unwrap();
75let res = U256::random_mod(&mut rng, &modulus);
7677// Check that the value is in range
78assert!(res >= U256::ZERO);
79assert!(res < U256::from(42u8));
8081// Ensure `random_mod` runs in a reasonable amount of time
82 // when the modulus is larger than 1 limb
83let modulus = NonZero::new(U256::from(0x10000000000000001u128)).unwrap();
84let res = U256::random_mod(&mut rng, &modulus);
8586// Check that the value is in range
87assert!(res >= U256::ZERO);
88assert!(res < U256::from(0x10000000000000001u128));
89 }
90}