Skip to content

Commit

Permalink
relax sizing requirement on the TryCryptoRng
Browse files Browse the repository at this point in the history
  • Loading branch information
baloo committed Feb 18, 2025
1 parent 8b57f55 commit 0cbd34e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,3 @@ crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint.git" }
glass_pumpkin = { git = "https://github.com/baloo/agora-glass_pumpkin.git", branch = "baloo/rand-core/0.9" }
# https://github.com/rust-num/num-bigint/pull/317
num-bigint = { git = "https://github.com/bionicles/num-bigint.git" }

# https://github.com/rust-random/rand/pull/1593
rand_core = { git = "https://github.com/fjarri/rand.git", branch = "sized" }
17 changes: 10 additions & 7 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ use crypto_primes::{
SmallPrimesSieve,
},
is_prime_with_rng, is_safe_prime_with_rng,
rng::MaybeRng,
};
#[cfg(feature = "multicore")]
use crypto_primes::{par_generate_prime_with_rng, par_generate_safe_prime_with_rng};
Expand All @@ -32,7 +33,7 @@ fn make_random_rng() -> ChaCha8Rng {
}

fn random_odd_uint<T: RandomBits + Integer, R: CryptoRng + ?Sized>(rng: &mut R, bit_length: u32) -> Odd<T> {
random_odd_integer::<T, R>(rng, NonZero::new(bit_length).unwrap(), SetBits::Msb).unwrap()
random_odd_integer::<T, _>(&mut MaybeRng(rng), NonZero::new(bit_length).unwrap(), SetBits::Msb).unwrap()
}

fn make_sieve<const L: usize, R: CryptoRng + ?Sized>(rng: &mut R) -> SmallPrimesSieve<Uint<L>> {
Expand Down Expand Up @@ -444,9 +445,10 @@ fn bench_glass_pumpkin(c: &mut Criterion) {
// Mimics the sequence of checks `glass-pumpkin` does to find a prime.
fn prime_like_gp<R: CryptoRng + ?Sized>(bit_length: u32, rng: &mut R) -> BoxedUint {
loop {
let start = random_odd_integer::<BoxedUint, R>(rng, NonZero::new(bit_length).unwrap(), SetBits::Msb)
.unwrap()
.get();
let start =
random_odd_integer::<BoxedUint, _>(&mut MaybeRng(rng), NonZero::new(bit_length).unwrap(), SetBits::Msb)
.unwrap()
.get();
let sieve = SmallPrimesSieve::new(start, NonZero::new(bit_length).unwrap(), false);
for num in sieve {
let odd_num = Odd::new(num.clone()).unwrap();
Expand All @@ -470,9 +472,10 @@ fn bench_glass_pumpkin(c: &mut Criterion) {
// Mimics the sequence of checks `glass-pumpkin` does to find a safe prime.
fn safe_prime_like_gp<R: CryptoRng + ?Sized>(bit_length: u32, rng: &mut R) -> BoxedUint {
loop {
let start = random_odd_integer::<BoxedUint, _>(rng, NonZero::new(bit_length).unwrap(), SetBits::Msb)
.unwrap()
.get();
let start =
random_odd_integer::<BoxedUint, _>(&mut MaybeRng(rng), NonZero::new(bit_length).unwrap(), SetBits::Msb)
.unwrap()
.get();
let sieve = SmallPrimesSieve::new(start, NonZero::new(bit_length).unwrap(), true);
for num in sieve {
let odd_num = Odd::new(num.clone()).unwrap();
Expand Down
4 changes: 2 additions & 2 deletions src/hazmat/sieve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,8 @@ impl<T: Integer + RandomBits> SieveFactory for SmallPrimesSieveFactory<T> {
rng: &mut R,
_previous_sieve: Option<&Self::Sieve>,
) -> Option<Self::Sieve> {
let start =
random_odd_integer::<T, R>(rng, self.max_bit_length, self.set_bits).expect("random_odd_integer() failed");
let start = random_odd_integer::<T, _>(&mut crate::rng::MaybeRng(rng), self.max_bit_length, self.set_bits)
.expect("random_odd_integer() failed");
Some(SmallPrimesSieve::new(
start.get(),
self.max_bit_length,
Expand Down
3 changes: 3 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,9 @@ pub mod hazmat;
mod presets;
mod traits;

#[doc(hidden)]
pub mod rng;

pub use generic::{sieve_and_find, SieveIterator};
pub use presets::{generate_prime_with_rng, generate_safe_prime_with_rng, is_prime_with_rng, is_safe_prime_with_rng};
pub use traits::{RandomPrimeWithRng, SieveFactory};
Expand Down
34 changes: 34 additions & 0 deletions src/rng.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use rand_core::{CryptoRng, RngCore, TryCryptoRng, TryRngCore};

/// Adapter from [`CryptoRng`] to [`TryCryptoRng`]
///
/// This is pending the release of a fix availale in this PR:
/// <https://github.com/rust-random/rand/pull/1593>
#[doc(hidden)]
#[derive(Debug)]
pub struct MaybeRng<'r, R>(pub &'r mut R)
where
R: ?Sized;

impl<R> TryRngCore for MaybeRng<'_, R>
where
R: RngCore + ?Sized,
{
type Error = core::convert::Infallible;

#[inline]
fn try_next_u32(&mut self) -> Result<u32, Self::Error> {
Ok(self.0.next_u32())
}

Check warning on line 22 in src/rng.rs

View check run for this annotation

Codecov / codecov/patch

src/rng.rs#L20-L22

Added lines #L20 - L22 were not covered by tests
#[inline]
fn try_next_u64(&mut self) -> Result<u64, Self::Error> {
Ok(self.0.next_u64())
}

Check warning on line 26 in src/rng.rs

View check run for this annotation

Codecov / codecov/patch

src/rng.rs#L24-L26

Added lines #L24 - L26 were not covered by tests
#[inline]
fn try_fill_bytes(&mut self, dst: &mut [u8]) -> Result<(), Self::Error> {
self.0.fill_bytes(dst);
Ok(())
}
}

impl<R: CryptoRng + ?Sized> TryCryptoRng for MaybeRng<'_, R> {}

0 comments on commit 0cbd34e

Please # to comment.