diff --git a/src/int/rand.rs b/src/int/rand.rs index 94f02e61..07a194b7 100644 --- a/src/int/rand.rs +++ b/src/int/rand.rs @@ -1,6 +1,6 @@ //! Random number generator support -use rand_core::{RngCore, TryRngCore}; +use rand_core::TryRngCore; use crate::{Int, Random, RandomBits, RandomBitsError}; @@ -8,8 +8,8 @@ use super::Uint; impl Random for Int { /// Generate a cryptographically secure random [`Int`]. - fn random(rng: &mut R) -> Self { - Self(Uint::random(rng)) + fn try_random(rng: &mut R) -> Result { + Ok(Self(Uint::try_random(rng)?)) } } diff --git a/src/limb/rand.rs b/src/limb/rand.rs index 2a5ba16c..22fd4218 100644 --- a/src/limb/rand.rs +++ b/src/limb/rand.rs @@ -2,18 +2,17 @@ use super::Limb; use crate::{Encoding, NonZero, Random, RandomMod}; -use rand_core::{RngCore, TryRngCore}; +use rand_core::TryRngCore; use subtle::ConstantTimeLess; impl Random for Limb { - #[cfg(target_pointer_width = "32")] - fn random(rng: &mut R) -> Self { - Self(rng.next_u32()) - } + fn try_random(rng: &mut R) -> Result { + #[cfg(target_pointer_width = "32")] + let val = rng.try_next_u32()?; + #[cfg(target_pointer_width = "64")] + let val = rng.try_next_u64()?; - #[cfg(target_pointer_width = "64")] - fn random(rng: &mut R) -> Self { - Self(rng.next_u64()) + Ok(Self(val)) } } diff --git a/src/modular/const_monty_form.rs b/src/modular/const_monty_form.rs index 8ff8dd80..cfd2511c 100644 --- a/src/modular/const_monty_form.rs +++ b/src/modular/const_monty_form.rs @@ -15,7 +15,7 @@ use core::{fmt::Debug, marker::PhantomData}; use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; #[cfg(feature = "rand_core")] -use crate::{Random, RandomMod, rand_core::RngCore}; +use crate::{Random, RandomMod, rand_core::TryRngCore}; #[cfg(feature = "serde")] use { @@ -204,8 +204,11 @@ where MOD: ConstMontyParams, { #[inline] - fn random(rng: &mut R) -> Self { - Self::new(&Uint::random_mod(rng, MOD::MODULUS.as_nz_ref())) + fn try_random(rng: &mut R) -> Result { + Ok(Self::new(&Uint::try_random_mod( + rng, + MOD::MODULUS.as_nz_ref(), + )?)) } } diff --git a/src/non_zero.rs b/src/non_zero.rs index 741c66e2..e76b5b97 100644 --- a/src/non_zero.rs +++ b/src/non_zero.rs @@ -12,7 +12,7 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; use crate::{ArrayEncoding, ByteArray}; #[cfg(feature = "rand_core")] -use {crate::Random, rand_core::RngCore}; +use {crate::Random, rand_core::TryRngCore}; #[cfg(feature = "serde")] use serdect::serde::{ @@ -246,10 +246,10 @@ where /// As a result, it runs in variable time. If the generator `rng` is /// cryptographically secure (for example, it implements `CryptoRng`), /// then this is guaranteed not to leak anything about the output value. - fn random(mut rng: &mut R) -> Self { + fn try_random(rng: &mut R) -> Result { loop { - if let Some(result) = Self::new(T::random(&mut rng)).into() { - break result; + if let Some(result) = Self::new(T::try_random(rng)?).into() { + break Ok(result); } } } diff --git a/src/odd.rs b/src/odd.rs index 4f076211..b442a632 100644 --- a/src/odd.rs +++ b/src/odd.rs @@ -8,10 +8,10 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption}; use crate::BoxedUint; #[cfg(feature = "rand_core")] -use {crate::Random, rand_core::RngCore}; +use crate::{Random, rand_core::TryRngCore}; #[cfg(all(feature = "alloc", feature = "rand_core"))] -use {crate::RandomBits, rand_core::TryRngCore}; +use crate::RandomBits; #[cfg(feature = "serde")] use crate::Zero; @@ -153,10 +153,10 @@ impl PartialOrd> for BoxedUint { #[cfg(feature = "rand_core")] impl Random for Odd> { /// Generate a random `Odd>`. - fn random(rng: &mut R) -> Self { - let mut ret = Uint::random(rng); + fn try_random(rng: &mut R) -> Result { + let mut ret = Uint::try_random(rng)?; ret.limbs[0] |= Limb::ONE; - Odd(ret) + Ok(Odd(ret)) } } diff --git a/src/traits.rs b/src/traits.rs index 86079823..962b6f52 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -299,7 +299,15 @@ pub trait Random: Sized { /// Generate a random value. /// /// If `rng` is a CSRNG, the generation is cryptographically secure as well. - fn random(rng: &mut R) -> Self; + fn random(rng: &mut R) -> Self { + let Ok(out) = Self::try_random(rng); + out + } + + /// Generate a random value. + /// + /// If `rng` is a CSRNG, the generation is cryptographically secure as well. + fn try_random(rng: &mut R) -> Result; } /// Possible errors of the methods in [`RandomBits`] trait. diff --git a/src/uint/rand.rs b/src/uint/rand.rs index 98d954ba..b37021c1 100644 --- a/src/uint/rand.rs +++ b/src/uint/rand.rs @@ -6,14 +6,14 @@ use rand_core::{RngCore, TryRngCore}; use subtle::ConstantTimeLess; impl Random for Uint { - fn random(mut rng: &mut R) -> Self { + fn try_random(rng: &mut R) -> Result { let mut limbs = [Limb::ZERO; LIMBS]; for limb in &mut limbs { - *limb = Limb::random(&mut rng) + *limb = Limb::try_random(rng)? } - limbs.into() + Ok(limbs.into()) } } diff --git a/src/wrapping.rs b/src/wrapping.rs index 414eaa22..3b2b969d 100644 --- a/src/wrapping.rs +++ b/src/wrapping.rs @@ -8,7 +8,7 @@ use core::{ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq}; #[cfg(feature = "rand_core")] -use {crate::Random, rand_core::RngCore}; +use {crate::Random, rand_core::TryRngCore}; #[cfg(feature = "serde")] use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer}; @@ -259,8 +259,8 @@ impl fmt::UpperHex for Wrapping { #[cfg(feature = "rand_core")] impl Random for Wrapping { - fn random(rng: &mut R) -> Self { - Wrapping(Random::random(rng)) + fn try_random(rng: &mut R) -> Result { + Ok(Wrapping(Random::try_random(rng)?)) } }