@@ -27,12 +27,13 @@ fn make_rng() -> ChaCha8Rng {
2727fn random_odd_uint < T : RandomBits + Integer > (
2828 rng : & mut impl CryptoRngCore ,
2929 bit_length : u32 ,
30+ bits_precision : u32 ,
3031) -> Odd < T > {
31- random_odd_integer :: < T > ( rng, NonZeroU32 :: new ( bit_length) . unwrap ( ) )
32+ random_odd_integer :: < T > ( rng, NonZeroU32 :: new ( bit_length) . unwrap ( ) , bits_precision )
3233}
3334
3435fn make_sieve < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Sieve < Uint < L > > {
35- let start = random_odd_uint :: < Uint < L > > ( rng, Uint :: < L > :: BITS ) ;
36+ let start = random_odd_uint :: < Uint < L > > ( rng, Uint :: < L > :: BITS , Uint :: < L > :: BITS ) ;
3637 Sieve :: new ( & start, NonZeroU32 :: new ( Uint :: < L > :: BITS ) . unwrap ( ) , false )
3738}
3839
@@ -45,12 +46,12 @@ fn bench_sieve(c: &mut Criterion) {
4546 let mut group = c. benchmark_group ( "Sieve" ) ;
4647
4748 group. bench_function ( "(U128) random start" , |b| {
48- b. iter ( || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) )
49+ b. iter ( || random_odd_uint :: < U128 > ( & mut OsRng , 128 , 128 ) )
4950 } ) ;
5051
5152 group. bench_function ( "(U128) creation" , |b| {
5253 b. iter_batched (
53- || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
54+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 , 128 ) ,
5455 |start| Sieve :: new ( start. as_ref ( ) , NonZeroU32 :: new ( 128 ) . unwrap ( ) , false ) ,
5556 BatchSize :: SmallInput ,
5657 )
@@ -66,12 +67,12 @@ fn bench_sieve(c: &mut Criterion) {
6667 } ) ;
6768
6869 group. bench_function ( "(U1024) random start" , |b| {
69- b. iter ( || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 ) )
70+ b. iter ( || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 , 1024 ) )
7071 } ) ;
7172
7273 group. bench_function ( "(U1024) creation" , |b| {
7374 b. iter_batched (
74- || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 ) ,
75+ || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 , 1024 ) ,
7576 |start| Sieve :: new ( start. as_ref ( ) , NonZeroU32 :: new ( 1024 ) . unwrap ( ) , false ) ,
7677 BatchSize :: SmallInput ,
7778 )
@@ -93,7 +94,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
9394
9495 group. bench_function ( "(U128) creation" , |b| {
9596 b. iter_batched (
96- || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
97+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 , 128 ) ,
9798 |n| MillerRabin :: new ( & n) ,
9899 BatchSize :: SmallInput ,
99100 )
@@ -109,7 +110,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
109110
110111 group. bench_function ( "(U1024) creation" , |b| {
111112 b. iter_batched (
112- || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 ) ,
113+ || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 , 1024 ) ,
113114 |n| MillerRabin :: new ( & n) ,
114115 BatchSize :: SmallInput ,
115116 )
@@ -202,39 +203,39 @@ fn bench_presets(c: &mut Criterion) {
202203
203204 group. bench_function ( "(U128) Prime test" , |b| {
204205 b. iter_batched (
205- || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
206+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 , 128 ) ,
206207 |num| is_prime_with_rng ( & mut OsRng , num. as_ref ( ) ) ,
207208 BatchSize :: SmallInput ,
208209 )
209210 } ) ;
210211
211212 group. bench_function ( "(U128) Safe prime test" , |b| {
212213 b. iter_batched (
213- || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
214+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 , 128 ) ,
214215 |num| is_safe_prime_with_rng ( & mut OsRng , num. as_ref ( ) ) ,
215216 BatchSize :: SmallInput ,
216217 )
217218 } ) ;
218219
219220 let mut rng = make_rng ( ) ;
220221 group. bench_function ( "(U128) Random prime" , |b| {
221- b. iter ( || generate_prime_with_rng :: < U128 > ( & mut rng, 128 ) )
222+ b. iter ( || generate_prime_with_rng :: < U128 > ( & mut rng, 128 , 128 ) )
222223 } ) ;
223224
224225 let mut rng = make_rng ( ) ;
225226 group. bench_function ( "(U1024) Random prime" , |b| {
226- b. iter ( || generate_prime_with_rng :: < U1024 > ( & mut rng, 1024 ) )
227+ b. iter ( || generate_prime_with_rng :: < U1024 > ( & mut rng, 1024 , 1024 ) )
227228 } ) ;
228229
229230 let mut rng = make_rng ( ) ;
230231 group. bench_function ( "(U128) Random safe prime" , |b| {
231- b. iter ( || generate_safe_prime_with_rng :: < U128 > ( & mut rng, 128 ) )
232+ b. iter ( || generate_safe_prime_with_rng :: < U128 > ( & mut rng, 128 , 128 ) )
232233 } ) ;
233234
234235 group. sample_size ( 20 ) ;
235236 let mut rng = make_rng ( ) ;
236237 group. bench_function ( "(U1024) Random safe prime" , |b| {
237- b. iter ( || generate_safe_prime_with_rng :: < U1024 > ( & mut rng, 1024 ) )
238+ b. iter ( || generate_safe_prime_with_rng :: < U1024 > ( & mut rng, 1024 , 1024 ) )
238239 } ) ;
239240
240241 group. finish ( ) ;
@@ -244,19 +245,19 @@ fn bench_presets(c: &mut Criterion) {
244245
245246 let mut rng = make_rng ( ) ;
246247 group. bench_function ( "(U128) Random safe prime" , |b| {
247- b. iter ( || generate_safe_prime_with_rng :: < U128 > ( & mut rng, 128 ) )
248+ b. iter ( || generate_safe_prime_with_rng :: < U128 > ( & mut rng, 128 , 128 ) )
248249 } ) ;
249250
250251 // The performance should scale with the prime size, not with the Uint size.
251252 // So we should strive for this test's result to be as close as possible
252253 // to that of the previous one and as far away as possible from the next one.
253254 group. bench_function ( "(U256) Random 128 bit safe prime" , |b| {
254- b. iter ( || generate_safe_prime_with_rng :: < U256 > ( & mut rng, 128 ) )
255+ b. iter ( || generate_safe_prime_with_rng :: < U256 > ( & mut rng, 128 , 256 ) )
255256 } ) ;
256257
257258 // The upper bound for the previous test.
258259 group. bench_function ( "(U256) Random 256 bit safe prime" , |b| {
259- b. iter ( || generate_safe_prime_with_rng :: < U256 > ( & mut rng, 256 ) )
260+ b. iter ( || generate_safe_prime_with_rng :: < U256 > ( & mut rng, 256 , 256 ) )
260261 } ) ;
261262
262263 group. finish ( ) ;
@@ -267,7 +268,7 @@ fn bench_gmp(c: &mut Criterion) {
267268 let mut group = c. benchmark_group ( "GMP" ) ;
268269
269270 fn random < const L : usize > ( rng : & mut impl CryptoRngCore ) -> GmpInteger {
270- let num = random_odd_uint :: < Uint < L > > ( rng, Uint :: < L > :: BITS ) . get ( ) ;
271+ let num = random_odd_uint :: < Uint < L > > ( rng, Uint :: < L > :: BITS , Uint :: < L > :: BITS ) . get ( ) ;
271272 GmpInteger :: from_digits ( num. as_words ( ) , Order :: Lsf )
272273 }
273274
0 commit comments