@@ -3,7 +3,7 @@ use core::num::NonZero;
33use criterion:: { BatchSize , Criterion , criterion_group, criterion_main} ;
44use crypto_bigint:: { BoxedUint , Odd , RandomBits , U128 , U256 , U1024 , Uint , Unsigned , nlimbs} ;
55use rand_chacha:: ChaCha8Rng ;
6- use rand_core:: { CryptoRng , OsRng , SeedableRng , TryRngCore } ;
6+ use rand_core:: { CryptoRng , SeedableRng } ;
77
88#[ cfg( feature = "tests-gmp" ) ]
99use rug:: { Integer as GmpInteger , integer:: Order } ;
@@ -29,7 +29,7 @@ fn make_rng() -> ChaCha8Rng {
2929
3030#[ cfg( feature = "multicore" ) ]
3131fn make_random_rng ( ) -> ChaCha8Rng {
32- ChaCha8Rng :: from_os_rng ( )
32+ ChaCha8Rng :: from_rng ( & mut rand :: rng ( ) )
3333}
3434
3535fn 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
4949fn 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
107108fn 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
213215fn 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" ) ]
366369fn 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 )
0 commit comments