1+ use core:: num:: NonZeroU32 ;
2+
13use criterion:: { criterion_group, criterion_main, BatchSize , Criterion } ;
2- use crypto_bigint:: { nlimbs, Odd , Uint , U1024 } ;
4+ use crypto_bigint:: { nlimbs, Integer , Odd , RandomBits , Uint , U1024 , U128 , U256 } ;
35use rand_chacha:: ChaCha8Rng ;
46use rand_core:: { CryptoRngCore , OsRng , SeedableRng } ;
57
68#[ cfg( feature = "tests-gmp" ) ]
7- use rug:: { integer:: Order , Integer } ;
9+ use rug:: { integer:: Order , Integer as GmpInteger } ;
810
911#[ cfg( feature = "tests-openssl" ) ]
1012use openssl:: bn:: BigNum ;
1113
1214use crypto_primes:: {
1315 generate_prime_with_rng, generate_safe_prime_with_rng,
1416 hazmat:: {
15- lucas_test, random_odd_uint , AStarBase , BruteForceBase , LucasCheck , MillerRabin ,
17+ lucas_test, random_odd_integer , AStarBase , BruteForceBase , LucasCheck , MillerRabin ,
1618 SelfridgeBase , Sieve ,
1719 } ,
1820 is_prime_with_rng, is_safe_prime_with_rng,
@@ -22,9 +24,16 @@ fn make_rng() -> ChaCha8Rng {
2224 ChaCha8Rng :: from_seed ( * b"01234567890123456789012345678901" )
2325}
2426
25- fn make_sieve < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Sieve < L > {
26- let start = random_odd_uint :: < L > ( rng, Uint :: < L > :: BITS ) ;
27- Sieve :: new ( & start, Uint :: < L > :: BITS , false )
27+ fn random_odd_uint < T : RandomBits + Integer > (
28+ rng : & mut impl CryptoRngCore ,
29+ bit_length : u32 ,
30+ ) -> Odd < T > {
31+ random_odd_integer :: < T > ( rng, NonZeroU32 :: new ( bit_length) . unwrap ( ) )
32+ }
33+
34+ fn 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+ Sieve :: new ( & start, NonZeroU32 :: new ( Uint :: < L > :: BITS ) . unwrap ( ) , false )
2837}
2938
3039fn make_presieved_num < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Odd < Uint < L > > {
@@ -36,13 +45,13 @@ fn bench_sieve(c: &mut Criterion) {
3645 let mut group = c. benchmark_group ( "Sieve" ) ;
3746
3847 group. bench_function ( "(U128) random start" , |b| {
39- b. iter ( || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) )
48+ b. iter ( || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) )
4049 } ) ;
4150
4251 group. bench_function ( "(U128) creation" , |b| {
4352 b. iter_batched (
44- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
45- |start| Sieve :: new ( & start, 128 , false ) ,
53+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
54+ |start| Sieve :: new ( start. as_ref ( ) , NonZeroU32 :: new ( 128 ) . unwrap ( ) , false ) ,
4655 BatchSize :: SmallInput ,
4756 )
4857 } ) ;
@@ -57,13 +66,13 @@ fn bench_sieve(c: &mut Criterion) {
5766 } ) ;
5867
5968 group. bench_function ( "(U1024) random start" , |b| {
60- b. iter ( || random_odd_uint :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng , 1024 ) )
69+ b. iter ( || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 ) )
6170 } ) ;
6271
6372 group. bench_function ( "(U1024) creation" , |b| {
6473 b. iter_batched (
65- || random_odd_uint :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng , 1024 ) ,
66- |start| Sieve :: new ( & start, 1024 , false ) ,
74+ || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 ) ,
75+ |start| Sieve :: new ( start. as_ref ( ) , NonZeroU32 :: new ( 1024 ) . unwrap ( ) , false ) ,
6776 BatchSize :: SmallInput ,
6877 )
6978 } ) ;
@@ -84,31 +93,31 @@ fn bench_miller_rabin(c: &mut Criterion) {
8493
8594 group. bench_function ( "(U128) creation" , |b| {
8695 b. iter_batched (
87- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
88- MillerRabin :: new,
96+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
97+ |n| MillerRabin :: new ( & n ) ,
8998 BatchSize :: SmallInput ,
9099 )
91100 } ) ;
92101
93102 group. bench_function ( "(U128) random base test (pre-sieved)" , |b| {
94103 b. iter_batched (
95- || MillerRabin :: new ( make_presieved_num :: < { nlimbs ! ( 128 ) } > ( & mut OsRng ) ) ,
104+ || MillerRabin :: new ( & make_presieved_num :: < { nlimbs ! ( 128 ) } > ( & mut OsRng ) ) ,
96105 |mr| mr. test_random_base ( & mut OsRng ) ,
97106 BatchSize :: SmallInput ,
98107 )
99108 } ) ;
100109
101110 group. bench_function ( "(U1024) creation" , |b| {
102111 b. iter_batched (
103- || random_odd_uint :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng , 1024 ) ,
104- MillerRabin :: new,
112+ || random_odd_uint :: < U1024 > ( & mut OsRng , 1024 ) ,
113+ |n| MillerRabin :: new ( & n ) ,
105114 BatchSize :: SmallInput ,
106115 )
107116 } ) ;
108117
109118 group. bench_function ( "(U1024) random base test (pre-sieved)" , |b| {
110119 b. iter_batched (
111- || MillerRabin :: new ( make_presieved_num :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng ) ) ,
120+ || MillerRabin :: new ( & make_presieved_num :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng ) ) ,
112121 |mr| mr. test_random_base ( & mut OsRng ) ,
113122 BatchSize :: SmallInput ,
114123 )
@@ -193,39 +202,39 @@ fn bench_presets(c: &mut Criterion) {
193202
194203 group. bench_function ( "(U128) Prime test" , |b| {
195204 b. iter_batched (
196- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
197- |num| is_prime_with_rng ( & mut OsRng , & num) ,
205+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
206+ |num| is_prime_with_rng ( & mut OsRng , num. as_ref ( ) ) ,
198207 BatchSize :: SmallInput ,
199208 )
200209 } ) ;
201210
202211 group. bench_function ( "(U128) Safe prime test" , |b| {
203212 b. iter_batched (
204- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
205- |num| is_safe_prime_with_rng ( & mut OsRng , & num) ,
213+ || random_odd_uint :: < U128 > ( & mut OsRng , 128 ) ,
214+ |num| is_safe_prime_with_rng ( & mut OsRng , num. as_ref ( ) ) ,
206215 BatchSize :: SmallInput ,
207216 )
208217 } ) ;
209218
210219 let mut rng = make_rng ( ) ;
211220 group. bench_function ( "(U128) Random prime" , |b| {
212- b. iter ( || generate_prime_with_rng :: < { nlimbs ! ( 128 ) } > ( & mut rng, None ) )
221+ b. iter ( || generate_prime_with_rng :: < U128 > ( & mut rng, 128 ) )
213222 } ) ;
214223
215224 let mut rng = make_rng ( ) ;
216225 group. bench_function ( "(U1024) Random prime" , |b| {
217- b. iter ( || generate_prime_with_rng :: < { nlimbs ! ( 1024 ) } > ( & mut rng, None ) )
226+ b. iter ( || generate_prime_with_rng :: < U1024 > ( & mut rng, 1024 ) )
218227 } ) ;
219228
220229 let mut rng = make_rng ( ) ;
221230 group. bench_function ( "(U128) Random safe prime" , |b| {
222- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 128 ) } > ( & mut rng, None ) )
231+ b. iter ( || generate_safe_prime_with_rng :: < U128 > ( & mut rng, 128 ) )
223232 } ) ;
224233
225234 group. sample_size ( 20 ) ;
226235 let mut rng = make_rng ( ) ;
227236 group. bench_function ( "(U1024) Random safe prime" , |b| {
228- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 1024 ) } > ( & mut rng, None ) )
237+ b. iter ( || generate_safe_prime_with_rng :: < U1024 > ( & mut rng, 1024 ) )
229238 } ) ;
230239
231240 group. finish ( ) ;
@@ -235,19 +244,19 @@ fn bench_presets(c: &mut Criterion) {
235244
236245 let mut rng = make_rng ( ) ;
237246 group. bench_function ( "(U128) Random safe prime" , |b| {
238- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 128 ) } > ( & mut rng, None ) )
247+ b. iter ( || generate_safe_prime_with_rng :: < U128 > ( & mut rng, 128 ) )
239248 } ) ;
240249
241250 // The performance should scale with the prime size, not with the Uint size.
242251 // So we should strive for this test's result to be as close as possible
243252 // to that of the previous one and as far away as possible from the next one.
244253 group. bench_function ( "(U256) Random 128 bit safe prime" , |b| {
245- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 256 ) } > ( & mut rng, Some ( 128 ) ) )
254+ b. iter ( || generate_safe_prime_with_rng :: < U256 > ( & mut rng, 128 ) )
246255 } ) ;
247256
248257 // The upper bound for the previous test.
249258 group. bench_function ( "(U256) Random 256 bit safe prime" , |b| {
250- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 256 ) } > ( & mut rng, None ) )
259+ b. iter ( || generate_safe_prime_with_rng :: < U256 > ( & mut rng, 256 ) )
251260 } ) ;
252261
253262 group. finish ( ) ;
@@ -257,9 +266,9 @@ fn bench_presets(c: &mut Criterion) {
257266fn bench_gmp ( c : & mut Criterion ) {
258267 let mut group = c. benchmark_group ( "GMP" ) ;
259268
260- fn random < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Integer {
261- let num = random_odd_uint :: < L > ( rng, Uint :: < L > :: BITS ) ;
262- Integer :: from_digits ( num. as_words ( ) , Order :: Lsf )
269+ fn random < const L : usize > ( rng : & mut impl CryptoRngCore ) -> GmpInteger {
270+ let num = random_odd_uint :: < Uint < L > > ( rng, Uint :: < L > :: BITS ) . get ( ) ;
271+ GmpInteger :: from_digits ( num. as_words ( ) , Order :: Lsf )
263272 }
264273
265274 group. bench_function ( "(U128) Random prime" , |b| {
0 commit comments