Skip to content

Commit d42c214

Browse files
committed
bump rand_core to 0.10.0-rc.2
1 parent 8b478bd commit d42c214

File tree

10 files changed

+106
-85
lines changed

10 files changed

+106
-85
lines changed

Cargo.toml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ rust-version = "1.85"
1212
[dependencies]
1313
crypto-bigint = { version = "0.7.0-pre.5", default-features = false, features = ["rand_core"] }
1414
libm = { version = "0.2.13", default-features = false, features = ["arch"] }
15-
rand_core = { version = "0.9.2", default-features = false }
15+
rand_core = { version = "0.10.0-rc.2", default-features = false }
1616
rayon = { version = "1", optional = true, default-features = false }
1717

1818
# Optional dependencies used in tests and benchmarks
@@ -21,10 +21,11 @@ rug = { version = "1.26", optional = true, default-features = false, features =
2121
glass_pumpkin = { version = "1", optional = true }
2222

2323
[dev-dependencies]
24-
rand_core = { version = "0.9.2", default-features = false, features = ["os_rng"] }
24+
rand = "0.10.0-rc.1"
25+
rand_core = { version = "0.10.0-rc.2", default-features = false }
2526
# need `crypto-bigint` with `alloc` to test `BoxedUint`
2627
crypto-bigint = { version = "0.7.0-pre.5", default-features = false, features = ["alloc"] }
27-
rand_chacha = "0.9"
28+
rand_chacha = "0.10.0-rc.1"
2829
criterion = { version = "0.5", features = ["html_reports"] }
2930
num-modular = { version = "0.5", features = ["num-bigint"] }
3031
num-bigint = "0.4"
@@ -62,3 +63,6 @@ harness = false
6263
[[bench]]
6364
name = "cctv"
6465
harness = false
66+
67+
[patch.crates-io]
68+
crypto-bigint = { git = "https://github.com/RustCrypto/crypto-bigint", branch = "rand_core/v0.10.0-rc-2" }

README.md

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -28,21 +28,19 @@ Find a 196 bit prime returned in a 256-bit long `crypto_bigint::U256`:
2828

2929
```rust
3030
use crypto_bigint::U256;
31-
use rand_core::{OsRng, TryRngCore};
3231
use crypto_primes::{Flavor, is_prime, random_prime};
3332

34-
let prime = random_prime::<U256, _>(&mut OsRng.unwrap_mut(), Flavor::Any, 196);
33+
let prime = random_prime::<U256, _>(&mut rand::rng(), Flavor::Any, 196);
3534
assert!(is_prime(Flavor::Any, &prime));
3635
```
3736

3837
Find a 64 bit safe prime returned in a `crypto_bigint::U1024`:
3938

4039
```rust
4140
use crypto_bigint::U1024;
42-
use rand_core::{OsRng, TryRngCore};
4341
use crypto_primes::{Flavor, is_prime, random_prime};
4442

45-
let prime = random_prime::<U1024, _>(&mut OsRng.unwrap_mut(), Flavor::Safe, 64);
43+
let prime = random_prime::<U1024, _>(&mut rand::rng(), Flavor::Safe, 64);
4644
assert!(is_prime(Flavor::Safe, &prime));
4745
```
4846

@@ -56,12 +54,11 @@ use crypto_primes::{
5654
is_prime, random_prime, sieve_and_find,
5755
};
5856
use crypto_bigint::U256;
59-
use rand_core::{OsRng, TryRngCore};
6057

6158
let flavor = Flavor::Any;
6259
let factory = SmallFactorsSieveFactory::<U256>::new(flavor, 256, SetBits::TwoMsb).unwrap();
6360
let prime = sieve_and_find(
64-
&mut OsRng.unwrap_mut(),
61+
&mut rand::rng(),
6562
factory,
6663
|_rng, candidate| is_prime(flavor, candidate)
6764
).unwrap().unwrap();

benches/bench.rs

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use core::num::NonZero;
33
use criterion::{BatchSize, Criterion, criterion_group, criterion_main};
44
use crypto_bigint::{BoxedUint, Odd, RandomBits, U128, U256, U1024, Uint, Unsigned, nlimbs};
55
use rand_chacha::ChaCha8Rng;
6-
use rand_core::{CryptoRng, OsRng, SeedableRng, TryRngCore};
6+
use rand_core::{CryptoRng, SeedableRng};
77

88
#[cfg(feature = "tests-gmp")]
99
use rug::{Integer as GmpInteger, integer::Order};
@@ -29,7 +29,7 @@ fn make_rng() -> ChaCha8Rng {
2929

3030
#[cfg(feature = "multicore")]
3131
fn make_random_rng() -> ChaCha8Rng {
32-
ChaCha8Rng::from_os_rng()
32+
ChaCha8Rng::from_rng(&mut rand::rng())
3333
}
3434

3535
fn random_odd_uint<T: RandomBits + Unsigned, R: CryptoRng + ?Sized>(rng: &mut R, bit_length: u32) -> Odd<T> {
@@ -48,14 +48,15 @@ fn make_presieved_num<const L: usize, R: CryptoRng + ?Sized>(rng: &mut R) -> Odd
4848

4949
fn bench_sieve(c: &mut Criterion) {
5050
let mut group = c.benchmark_group("Sieve");
51+
let mut rng = rand::rng();
5152

5253
group.bench_function("(U128) random start", |b| {
53-
b.iter(|| random_odd_uint::<U128, _>(&mut OsRng.unwrap_mut(), 128))
54+
b.iter(|| random_odd_uint::<U128, _>(&mut rng, 128))
5455
});
5556

5657
group.bench_function("(U128) creation", |b| {
5758
b.iter_batched(
58-
|| random_odd_uint::<U128, _>(&mut OsRng.unwrap_mut(), 128),
59+
|| random_odd_uint::<U128, _>(&mut rng, 128),
5960
|start| SmallFactorsSieve::new(start.get(), NonZero::new(128).unwrap(), false),
6061
BatchSize::SmallInput,
6162
)
@@ -64,19 +65,19 @@ fn bench_sieve(c: &mut Criterion) {
6465
// 5 is the average number of pre-sieved samples we need to take before we encounter a prime
6566
group.bench_function("(U128) average sieve samples for a prime (5)", |b| {
6667
b.iter_batched(
67-
|| make_sieve::<{ nlimbs!(128) }, _>(&mut OsRng.unwrap_mut()),
68+
|| make_sieve::<{ nlimbs!(128) }, _>(&mut rng),
6869
|sieve| sieve.take(5).for_each(drop),
6970
BatchSize::SmallInput,
7071
)
7172
});
7273

7374
group.bench_function("(U1024) random start", |b| {
74-
b.iter(|| random_odd_uint::<U1024, _>(&mut OsRng.unwrap_mut(), 1024))
75+
b.iter(|| random_odd_uint::<U1024, _>(&mut rng, 1024))
7576
});
7677

7778
group.bench_function("(U1024) creation", |b| {
7879
b.iter_batched(
79-
|| random_odd_uint::<U1024, _>(&mut OsRng.unwrap_mut(), 1024),
80+
|| random_odd_uint::<U1024, _>(&mut rng, 1024),
8081
|start| SmallFactorsSieve::new(start.get(), NonZero::new(1024).unwrap(), false),
8182
BatchSize::SmallInput,
8283
)
@@ -85,7 +86,7 @@ fn bench_sieve(c: &mut Criterion) {
8586
// 42 is the average number of pre-sieved samples we need to take before we encounter a prime
8687
group.bench_function("(U1024) average sieve samples for a prime (42)", |b| {
8788
b.iter_batched(
88-
|| make_sieve::<{ nlimbs!(1024) }, _>(&mut OsRng.unwrap_mut()),
89+
|| make_sieve::<{ nlimbs!(1024) }, _>(&mut rng),
8990
|sieve| sieve.take(42).for_each(drop),
9091
BatchSize::SmallInput,
9192
)
@@ -95,7 +96,7 @@ fn bench_sieve(c: &mut Criterion) {
9596
// before we encounter a safe prime
9697
group.bench_function("(U1024) average sieve samples for a safe prime (42^2)", |b| {
9798
b.iter_batched(
98-
|| make_sieve::<{ nlimbs!(1024) }, _>(&mut OsRng.unwrap_mut()),
99+
|| make_sieve::<{ nlimbs!(1024) }, _>(&mut rng),
99100
|sieve| sieve.take(42 * 42).for_each(drop),
100101
BatchSize::SmallInput,
101102
)
@@ -106,35 +107,36 @@ fn bench_sieve(c: &mut Criterion) {
106107

107108
fn bench_miller_rabin(c: &mut Criterion) {
108109
let mut group = c.benchmark_group("Miller-Rabin");
110+
let mut rng = rand::rng();
109111

110112
group.bench_function("(U128) creation", |b| {
111113
b.iter_batched(
112-
|| random_odd_uint::<U128, _>(&mut OsRng.unwrap_mut(), 128),
114+
|| random_odd_uint::<U128, _>(&mut rng, 128),
113115
MillerRabin::<U128>::new,
114116
BatchSize::SmallInput,
115117
)
116118
});
117119

118120
group.bench_function("(U128) random base test (pre-sieved)", |b| {
119121
b.iter_batched(
120-
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(128) }, _>(&mut OsRng.unwrap_mut())),
121-
|mr| mr.test_random_base(&mut OsRng.unwrap_mut()),
122+
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(128) }, _>(&mut rng.clone())),
123+
|mr| mr.test_random_base(&mut rng.clone()),
122124
BatchSize::SmallInput,
123125
)
124126
});
125127

126128
group.bench_function("(U1024) creation", |b| {
127129
b.iter_batched(
128-
|| random_odd_uint::<U1024, _>(&mut OsRng.unwrap_mut(), 1024),
130+
|| random_odd_uint::<U1024, _>(&mut rng, 1024),
129131
MillerRabin::<U1024>::new,
130132
BatchSize::SmallInput,
131133
)
132134
});
133135

134136
group.bench_function("(U1024) random base test (pre-sieved)", |b| {
135137
b.iter_batched(
136-
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(1024) }, _>(&mut OsRng.unwrap_mut())),
137-
|mr| mr.test_random_base(&mut OsRng.unwrap_mut()),
138+
|| MillerRabin::new(make_presieved_num::<{ nlimbs!(1024) }, _>(&mut rng.clone())),
139+
|mr| mr.test_random_base(&mut rng.clone()),
138140
BatchSize::SmallInput,
139141
)
140142
});
@@ -212,18 +214,19 @@ fn bench_lucas(c: &mut Criterion) {
212214

213215
fn bench_presets(c: &mut Criterion) {
214216
let mut group = c.benchmark_group("Presets");
217+
let mut rng = rand::rng();
215218

216219
group.bench_function("(U128) Prime test", |b| {
217220
b.iter_batched(
218-
|| random_odd_uint::<U128, _>(&mut OsRng.unwrap_mut(), 128),
221+
|| random_odd_uint::<U128, _>(&mut rng, 128),
219222
|num| is_prime(Flavor::Any, num.as_ref()),
220223
BatchSize::SmallInput,
221224
)
222225
});
223226

224227
group.bench_function("(U1024) Prime test", |b| {
225228
b.iter_batched(
226-
|| random_odd_uint::<U1024, _>(&mut OsRng.unwrap_mut(), 1024),
229+
|| random_odd_uint::<U1024, _>(&mut rng, 1024),
227230
|num| is_prime(Flavor::Any, num.as_ref()),
228231
BatchSize::SmallInput,
229232
)
@@ -233,15 +236,15 @@ fn bench_presets(c: &mut Criterion) {
233236

234237
group.bench_function("(U1024) Prime test (FIPS, 1/2^128 failure bound)", |b| {
235238
b.iter_batched(
236-
|| random_odd_uint::<U1024, _>(&mut OsRng.unwrap_mut(), 1024),
237-
|num| fips::is_prime(&mut OsRng.unwrap_mut(), Flavor::Any, num.as_ref(), iters, false),
239+
|| random_odd_uint::<U1024, _>(&mut rng.clone(), 1024),
240+
|num| fips::is_prime(&mut rng.clone(), Flavor::Any, num.as_ref(), iters, false),
238241
BatchSize::SmallInput,
239242
)
240243
});
241244

242245
group.bench_function("(U128) Safe prime test", |b| {
243246
b.iter_batched(
244-
|| random_odd_uint::<U128, _>(&mut OsRng.unwrap_mut(), 128),
247+
|| random_odd_uint::<U128, _>(&mut rng, 128),
245248
|num| is_prime(Flavor::Safe, num.as_ref()),
246249
BatchSize::SmallInput,
247250
)
@@ -365,6 +368,7 @@ fn bench_multicore_presets(_c: &mut Criterion) {}
365368
#[cfg(feature = "tests-gmp")]
366369
fn bench_gmp(c: &mut Criterion) {
367370
let mut group = c.benchmark_group("GMP");
371+
let mut rng = rand::rng();
368372

369373
fn random<const L: usize, R: CryptoRng + ?Sized>(rng: &mut R) -> GmpInteger {
370374
let num = random_odd_uint::<Uint<L>, R>(rng, Uint::<L>::BITS).get();
@@ -373,15 +377,15 @@ fn bench_gmp(c: &mut Criterion) {
373377

374378
group.bench_function("(U128) Random prime", |b| {
375379
b.iter_batched(
376-
|| random::<{ nlimbs!(128) }, _>(&mut OsRng.unwrap_mut()),
380+
|| random::<{ nlimbs!(128) }, _>(&mut rng),
377381
|n| n.next_prime(),
378382
BatchSize::SmallInput,
379383
)
380384
});
381385

382386
group.bench_function("(U1024) Random prime", |b| {
383387
b.iter_batched(
384-
|| random::<{ nlimbs!(1024) }, _>(&mut OsRng.unwrap_mut()),
388+
|| random::<{ nlimbs!(1024) }, _>(&mut rng),
385389
|n| n.next_prime(),
386390
BatchSize::SmallInput,
387391
)

src/generic.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ where
3737

3838
#[cfg(test)]
3939
mod tests {
40-
use rand_core::{CryptoRng, OsRng, TryRngCore};
40+
use rand_core::CryptoRng;
4141

4242
use super::sieve_and_find;
4343
use crate::{Error, hazmat::SieveFactory};
@@ -67,12 +67,14 @@ mod tests {
6767
}
6868
}
6969

70+
let mut rng = rand::rng();
71+
7072
let factory = TestSieveFactory { count: 0 };
71-
let result = sieve_and_find(&mut OsRng.unwrap_mut(), factory, |_rng, num| *num == 11);
73+
let result = sieve_and_find(&mut rng, factory, |_rng, num| *num == 11);
7274
assert!(result.unwrap().is_some());
7375

7476
let factory = TestSieveFactory { count: 0 };
75-
let result = sieve_and_find(&mut OsRng.unwrap_mut(), factory, |_rng, num| *num == 20);
77+
let result = sieve_and_find(&mut rng, factory, |_rng, num| *num == 20);
7678
assert!(result.unwrap().is_none());
7779
}
7880
}

src/hazmat/miller_rabin.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ mod tests {
235235

236236
use crypto_bigint::{Odd, RandomMod, U64, U128, U1024, U1536, Uint, Unsigned};
237237
use rand_chacha::ChaCha8Rng;
238-
use rand_core::{CryptoRng, OsRng, SeedableRng, TryRngCore};
238+
use rand_core::{CryptoRng, SeedableRng};
239239

240240
#[cfg(feature = "tests-exhaustive")]
241241
use num_prime::nt_funcs::is_prime64;
@@ -252,11 +252,13 @@ mod tests {
252252

253253
#[test]
254254
fn random_base_corner_cases() {
255+
let mut rng = rand::rng();
256+
255257
let mr = MillerRabin::new(Odd::new(U64::from(1u32)).unwrap());
256-
assert!(mr.test_random_base(&mut OsRng.unwrap_mut()) == Primality::Composite);
258+
assert!(mr.test_random_base(&mut rng) == Primality::Composite);
257259

258260
let mr = MillerRabin::new(Odd::new(U64::from(3u32)).unwrap());
259-
assert!(mr.test_random_base(&mut OsRng.unwrap_mut()) == Primality::Prime);
261+
assert!(mr.test_random_base(&mut rng) == Primality::Prime);
260262
}
261263

262264
fn is_spsp(num: u32) -> bool {

src/hazmat/precomputed.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,19 +143,19 @@ pub(crate) static RECIPROCALS: [Reciprocal; SMALL_PRIMES.len()] = create_recipro
143143
#[cfg(test)]
144144
mod tests {
145145
use crypto_bigint::{NonZero, Random, U256};
146-
use rand_core::{OsRng, TryRngCore};
147146

148147
use super::{SMALL_PRIMES, create_reciprocals};
149148

150149
#[test]
151150
fn correctness() {
151+
let mut rng = rand::rng();
152152
let reciprocals = create_reciprocals();
153153

154154
assert_eq!(reciprocals.len(), SMALL_PRIMES.len());
155155

156156
for (reciprocal, prime) in reciprocals.iter().zip(SMALL_PRIMES.iter()) {
157157
for _ in 0..10 {
158-
let x = U256::random(&mut OsRng.unwrap_mut());
158+
let x = U256::random(&mut rng);
159159
let r_ref = (x % NonZero::new(U256::from(*prime)).unwrap()).as_limbs()[0];
160160
let r_test = x.rem_limb_with_reciprocal(reciprocal);
161161
assert_eq!(r_ref, r_test);

0 commit comments

Comments
 (0)