Skip to content

Commit 8b09ce8

Browse files
committed
provide a Random::try_random method
1 parent b892274 commit 8b09ce8

File tree

8 files changed

+40
-30
lines changed

8 files changed

+40
-30
lines changed

src/int/rand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
11
//! Random number generator support
22
3-
use rand_core::{RngCore, TryRngCore};
3+
use rand_core::TryRngCore;
44

55
use crate::{Int, Random, RandomBits, RandomBitsError};
66

77
use super::Uint;
88

99
impl<const LIMBS: usize> Random for Int<LIMBS> {
1010
/// Generate a cryptographically secure random [`Int`].
11-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
12-
Self(Uint::random(rng))
11+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
12+
Ok(Self(Uint::try_random(rng)?))
1313
}
1414
}
1515

src/limb/rand.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,17 @@
22
33
use super::Limb;
44
use crate::{Encoding, NonZero, Random, RandomMod};
5-
use rand_core::{RngCore, TryRngCore};
5+
use rand_core::TryRngCore;
66
use subtle::ConstantTimeLess;
77

88
impl Random for Limb {
9-
#[cfg(target_pointer_width = "32")]
10-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
11-
Self(rng.next_u32())
12-
}
9+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
10+
#[cfg(target_pointer_width = "32")]
11+
let val = rng.try_next_u32()?;
12+
#[cfg(target_pointer_width = "64")]
13+
let val = rng.try_next_u64()?;
1314

14-
#[cfg(target_pointer_width = "64")]
15-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
16-
Self(rng.next_u64())
15+
Ok(Self(val))
1716
}
1817
}
1918

src/modular/const_monty_form.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use core::{fmt::Debug, marker::PhantomData};
1515
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
1616

1717
#[cfg(feature = "rand_core")]
18-
use crate::{rand_core::RngCore, Random, RandomMod};
18+
use crate::{rand_core::TryRngCore, Random, RandomMod};
1919

2020
#[cfg(feature = "serde")]
2121
use {
@@ -207,8 +207,11 @@ where
207207
MOD: ConstMontyParams<LIMBS>,
208208
{
209209
#[inline]
210-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
211-
Self::new(&Uint::random_mod(rng, MOD::MODULUS.as_nz_ref()))
210+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
211+
Ok(Self::new(&Uint::try_random_mod(
212+
rng,
213+
MOD::MODULUS.as_nz_ref(),
214+
)?))
212215
}
213216
}
214217

src/non_zero.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
1212
use crate::{ArrayEncoding, ByteArray};
1313

1414
#[cfg(feature = "rand_core")]
15-
use {crate::Random, rand_core::RngCore};
15+
use {crate::Random, rand_core::TryRngCore};
1616

1717
#[cfg(feature = "serde")]
1818
use serdect::serde::{
@@ -246,10 +246,10 @@ where
246246
/// As a result, it runs in variable time. If the generator `rng` is
247247
/// cryptographically secure (for example, it implements `CryptoRng`),
248248
/// then this is guaranteed not to leak anything about the output value.
249-
fn random<R: RngCore + ?Sized>(mut rng: &mut R) -> Self {
249+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
250250
loop {
251-
if let Some(result) = Self::new(T::random(&mut rng)).into() {
252-
break result;
251+
if let Some(result) = Self::new(T::try_random(rng)?).into() {
252+
break Ok(result);
253253
}
254254
}
255255
}

src/odd.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
88
use crate::BoxedUint;
99

1010
#[cfg(feature = "rand_core")]
11-
use {crate::Random, rand_core::RngCore};
11+
use crate::{rand_core::TryRngCore, Random};
1212

1313
#[cfg(all(feature = "alloc", feature = "rand_core"))]
14-
use {crate::RandomBits, rand_core::TryRngCore};
14+
use crate::RandomBits;
1515

1616
#[cfg(feature = "serde")]
1717
use crate::Zero;
@@ -153,10 +153,10 @@ impl PartialOrd<Odd<BoxedUint>> for BoxedUint {
153153
#[cfg(feature = "rand_core")]
154154
impl<const LIMBS: usize> Random for Odd<Uint<LIMBS>> {
155155
/// Generate a random `Odd<Uint<T>>`.
156-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
157-
let mut ret = Uint::random(rng);
156+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
157+
let mut ret = Uint::try_random(rng)?;
158158
ret.limbs[0] |= Limb::ONE;
159-
Odd(ret)
159+
Ok(Odd(ret))
160160
}
161161
}
162162

src/traits.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -299,7 +299,15 @@ pub trait Random: Sized {
299299
/// Generate a random value.
300300
///
301301
/// If `rng` is a CSRNG, the generation is cryptographically secure as well.
302-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self;
302+
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
303+
let Ok(out) = Self::try_random(rng);
304+
out
305+
}
306+
307+
/// Generate a random value.
308+
///
309+
/// If `rng` is a CSRNG, the generation is cryptographically secure as well.
310+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error>;
303311
}
304312

305313
/// Possible errors of the methods in [`RandomBits`] trait.

src/uint/rand.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,14 @@ use rand_core::{RngCore, TryRngCore};
66
use subtle::ConstantTimeLess;
77

88
impl<const LIMBS: usize> Random for Uint<LIMBS> {
9-
fn random<R: RngCore + ?Sized>(mut rng: &mut R) -> Self {
9+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
1010
let mut limbs = [Limb::ZERO; LIMBS];
1111

1212
for limb in &mut limbs {
13-
*limb = Limb::random(&mut rng)
13+
*limb = Limb::try_random(rng)?
1414
}
1515

16-
limbs.into()
16+
Ok(limbs.into())
1717
}
1818
}
1919

src/wrapping.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use core::{
88
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq};
99

1010
#[cfg(feature = "rand_core")]
11-
use {crate::Random, rand_core::RngCore};
11+
use {crate::Random, rand_core::TryRngCore};
1212

1313
#[cfg(feature = "serde")]
1414
use serdect::serde::{Deserialize, Deserializer, Serialize, Serializer};
@@ -259,8 +259,8 @@ impl<T: fmt::UpperHex> fmt::UpperHex for Wrapping<T> {
259259

260260
#[cfg(feature = "rand_core")]
261261
impl<T: Random> Random for Wrapping<T> {
262-
fn random<R: RngCore + ?Sized>(rng: &mut R) -> Self {
263-
Wrapping(Random::random(rng))
262+
fn try_random<R: TryRngCore + ?Sized>(rng: &mut R) -> Result<Self, R::Error> {
263+
Ok(Wrapping(Random::try_random(rng)?))
264264
}
265265
}
266266

0 commit comments

Comments
 (0)