1
1
use criterion:: { criterion_group, criterion_main, BatchSize , Criterion } ;
2
- use crypto_bigint:: { nlimbs, Uint , U1024 } ;
2
+ use crypto_bigint:: { nlimbs, Odd , Uint , U1024 } ;
3
3
use rand_chacha:: ChaCha8Rng ;
4
4
use rand_core:: { CryptoRngCore , OsRng , SeedableRng } ;
5
5
@@ -22,27 +22,27 @@ fn make_rng() -> ChaCha8Rng {
22
22
ChaCha8Rng :: from_seed ( * b"01234567890123456789012345678901" )
23
23
}
24
24
25
- fn make_sieve < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Sieve < L > {
26
- let start: Uint < L > = random_odd_uint ( rng, Uint :: < L > :: BITS ) ;
25
+ fn make_sieve < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Sieve < Uint < L > > {
26
+ let start: Odd < Uint < L > > = random_odd_uint ( rng, Uint :: < L > :: BITS ) ;
27
27
Sieve :: new ( & start, Uint :: < L > :: BITS , false )
28
28
}
29
29
30
- fn make_presieved_num < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Uint < L > {
30
+ fn make_presieved_num < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Odd < Uint < L > > {
31
31
let mut sieve = make_sieve ( rng) ;
32
- sieve. next ( ) . unwrap ( )
32
+ Odd :: new ( sieve. next ( ) . unwrap ( ) ) . unwrap ( )
33
33
}
34
34
35
35
fn bench_sieve ( c : & mut Criterion ) {
36
36
let mut group = c. benchmark_group ( "Sieve" ) ;
37
37
38
38
group. bench_function ( "(U128) random start" , |b| {
39
- b. iter ( || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) )
39
+ b. iter ( || random_odd_uint :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut OsRng , 128 ) )
40
40
} ) ;
41
41
42
42
group. bench_function ( "(U128) creation" , |b| {
43
43
b. iter_batched (
44
- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
45
- |start| Sieve :: new ( & start, 128 , false ) ,
44
+ || random_odd_uint :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut OsRng , 128 ) ,
45
+ |start| Sieve :: new ( start. as_ref ( ) , 128 , false ) ,
46
46
BatchSize :: SmallInput ,
47
47
)
48
48
} ) ;
@@ -57,13 +57,13 @@ fn bench_sieve(c: &mut Criterion) {
57
57
} ) ;
58
58
59
59
group. bench_function ( "(U1024) random start" , |b| {
60
- b. iter ( || random_odd_uint :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng , 1024 ) )
60
+ b. iter ( || random_odd_uint :: < Uint < { nlimbs ! ( 1024 ) } > > ( & mut OsRng , 1024 ) )
61
61
} ) ;
62
62
63
63
group. bench_function ( "(U1024) creation" , |b| {
64
64
b. iter_batched (
65
- || random_odd_uint :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng , 1024 ) ,
66
- |start| Sieve :: new ( & start, 1024 , false ) ,
65
+ || random_odd_uint :: < Uint < { nlimbs ! ( 1024 ) } > > ( & mut OsRng , 1024 ) ,
66
+ |start| Sieve :: new ( start. as_ref ( ) , 1024 , false ) ,
67
67
BatchSize :: SmallInput ,
68
68
)
69
69
} ) ;
@@ -84,7 +84,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
84
84
85
85
group. bench_function ( "(U128) creation" , |b| {
86
86
b. iter_batched (
87
- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
87
+ || random_odd_uint :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut OsRng , 128 ) ,
88
88
|start| MillerRabin :: new ( & start) ,
89
89
BatchSize :: SmallInput ,
90
90
)
@@ -100,7 +100,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
100
100
101
101
group. bench_function ( "(U1024) creation" , |b| {
102
102
b. iter_batched (
103
- || random_odd_uint :: < { nlimbs ! ( 1024 ) } > ( & mut OsRng , 1024 ) ,
103
+ || random_odd_uint :: < Uint < { nlimbs ! ( 1024 ) } > > ( & mut OsRng , 1024 ) ,
104
104
|start| MillerRabin :: new ( & start) ,
105
105
BatchSize :: SmallInput ,
106
106
)
@@ -122,7 +122,7 @@ fn bench_lucas(c: &mut Criterion) {
122
122
group. bench_function ( "(U128) Selfridge base, strong check (pre-sieved)" , |b| {
123
123
b. iter_batched (
124
124
|| make_presieved_num :: < { nlimbs ! ( 128 ) } > ( & mut rng) ,
125
- |n| lucas_test ( & n , SelfridgeBase , LucasCheck :: Strong ) ,
125
+ |n| lucas_test ( n . as_ref ( ) , SelfridgeBase , LucasCheck :: Strong ) ,
126
126
BatchSize :: SmallInput ,
127
127
)
128
128
} ) ;
@@ -131,7 +131,7 @@ fn bench_lucas(c: &mut Criterion) {
131
131
group. bench_function ( "(U1024) Selfridge base, strong check (pre-sieved)" , |b| {
132
132
b. iter_batched (
133
133
|| make_presieved_num :: < { nlimbs ! ( 1024 ) } > ( & mut rng) ,
134
- |n| lucas_test ( & n , SelfridgeBase , LucasCheck :: Strong ) ,
134
+ |n| lucas_test ( n . as_ref ( ) , SelfridgeBase , LucasCheck :: Strong ) ,
135
135
BatchSize :: SmallInput ,
136
136
)
137
137
} ) ;
@@ -140,7 +140,7 @@ fn bench_lucas(c: &mut Criterion) {
140
140
group. bench_function ( "(U1024) A* base, Lucas-V check (pre-sieved)" , |b| {
141
141
b. iter_batched (
142
142
|| make_presieved_num :: < { nlimbs ! ( 1024 ) } > ( & mut rng) ,
143
- |n| lucas_test ( & n , AStarBase , LucasCheck :: LucasV ) ,
143
+ |n| lucas_test ( n . as_ref ( ) , AStarBase , LucasCheck :: LucasV ) ,
144
144
BatchSize :: SmallInput ,
145
145
)
146
146
} ) ;
@@ -151,7 +151,7 @@ fn bench_lucas(c: &mut Criterion) {
151
151
|b| {
152
152
b. iter_batched (
153
153
|| make_presieved_num :: < { nlimbs ! ( 1024 ) } > ( & mut rng) ,
154
- |n| lucas_test ( & n , BruteForceBase , LucasCheck :: AlmostExtraStrong ) ,
154
+ |n| lucas_test ( n . as_ref ( ) , BruteForceBase , LucasCheck :: AlmostExtraStrong ) ,
155
155
BatchSize :: SmallInput ,
156
156
)
157
157
} ,
@@ -161,7 +161,7 @@ fn bench_lucas(c: &mut Criterion) {
161
161
group. bench_function ( "(U1024) brute force base, extra strong (pre-sieved)" , |b| {
162
162
b. iter_batched (
163
163
|| make_presieved_num :: < { nlimbs ! ( 1024 ) } > ( & mut rng) ,
164
- |n| lucas_test ( & n , BruteForceBase , LucasCheck :: ExtraStrong ) ,
164
+ |n| lucas_test ( n . as_ref ( ) , BruteForceBase , LucasCheck :: ExtraStrong ) ,
165
165
BatchSize :: SmallInput ,
166
166
)
167
167
} ) ;
@@ -192,39 +192,39 @@ fn bench_presets(c: &mut Criterion) {
192
192
193
193
group. bench_function ( "(U128) Prime test" , |b| {
194
194
b. iter_batched (
195
- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
196
- |num| is_prime_with_rng ( & mut OsRng , & num) ,
195
+ || random_odd_uint :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut OsRng , 128 ) ,
196
+ |num| is_prime_with_rng ( & mut OsRng , num. as_ref ( ) ) ,
197
197
BatchSize :: SmallInput ,
198
198
)
199
199
} ) ;
200
200
201
201
group. bench_function ( "(U128) Safe prime test" , |b| {
202
202
b. iter_batched (
203
- || random_odd_uint :: < { nlimbs ! ( 128 ) } > ( & mut OsRng , 128 ) ,
204
- |num| is_safe_prime_with_rng ( & mut OsRng , & num) ,
203
+ || random_odd_uint :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut OsRng , 128 ) ,
204
+ |num| is_safe_prime_with_rng ( & mut OsRng , num. as_ref ( ) ) ,
205
205
BatchSize :: SmallInput ,
206
206
)
207
207
} ) ;
208
208
209
209
let mut rng = make_rng ( ) ;
210
210
group. bench_function ( "(U128) Random prime" , |b| {
211
- b. iter ( || generate_prime_with_rng :: < { nlimbs ! ( 128 ) } > ( & mut rng, None ) )
211
+ b. iter ( || generate_prime_with_rng :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut rng, 128 ) )
212
212
} ) ;
213
213
214
214
let mut rng = make_rng ( ) ;
215
215
group. bench_function ( "(U1024) Random prime" , |b| {
216
- b. iter ( || generate_prime_with_rng :: < { nlimbs ! ( 1024 ) } > ( & mut rng, None ) )
216
+ b. iter ( || generate_prime_with_rng :: < Uint < { nlimbs ! ( 1024 ) } > > ( & mut rng, 1024 ) )
217
217
} ) ;
218
218
219
219
let mut rng = make_rng ( ) ;
220
220
group. bench_function ( "(U128) Random safe prime" , |b| {
221
- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 128 ) } > ( & mut rng, None ) )
221
+ b. iter ( || generate_safe_prime_with_rng :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut rng, 128 ) )
222
222
} ) ;
223
223
224
224
group. sample_size ( 20 ) ;
225
225
let mut rng = make_rng ( ) ;
226
226
group. bench_function ( "(U1024) Random safe prime" , |b| {
227
- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 1024 ) } > ( & mut rng, None ) )
227
+ b. iter ( || generate_safe_prime_with_rng :: < Uint < { nlimbs ! ( 1024 ) } > > ( & mut rng, 1024 ) )
228
228
} ) ;
229
229
230
230
group. finish ( ) ;
@@ -234,19 +234,19 @@ fn bench_presets(c: &mut Criterion) {
234
234
235
235
let mut rng = make_rng ( ) ;
236
236
group. bench_function ( "(U128) Random safe prime" , |b| {
237
- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 128 ) } > ( & mut rng, None ) )
237
+ b. iter ( || generate_safe_prime_with_rng :: < Uint < { nlimbs ! ( 128 ) } > > ( & mut rng, 128 ) )
238
238
} ) ;
239
239
240
240
// The performance should scale with the prime size, not with the Uint size.
241
241
// So we should strive for this test's result to be as close as possible
242
242
// to that of the previous one and as far away as possible from the next one.
243
243
group. bench_function ( "(U256) Random 128 bit safe prime" , |b| {
244
- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 256 ) } > ( & mut rng, Some ( 128 ) ) )
244
+ b. iter ( || generate_safe_prime_with_rng :: < Uint < { nlimbs ! ( 256 ) } > > ( & mut rng, 128 ) )
245
245
} ) ;
246
246
247
247
// The upper bound for the previous test.
248
248
group. bench_function ( "(U256) Random 256 bit safe prime" , |b| {
249
- b. iter ( || generate_safe_prime_with_rng :: < { nlimbs ! ( 256 ) } > ( & mut rng, None ) )
249
+ b. iter ( || generate_safe_prime_with_rng :: < Uint < { nlimbs ! ( 256 ) } > > ( & mut rng, 256 ) )
250
250
} ) ;
251
251
252
252
group. finish ( ) ;
@@ -257,7 +257,7 @@ fn bench_gmp(c: &mut Criterion) {
257
257
let mut group = c. benchmark_group ( "GMP" ) ;
258
258
259
259
fn random < const L : usize > ( rng : & mut impl CryptoRngCore ) -> Integer {
260
- let num: Uint < L > = random_odd_uint ( rng, Uint :: < L > :: BITS ) ;
260
+ let num = random_odd_uint :: < Uint < L > > ( rng, Uint :: < L > :: BITS ) . get ( ) ;
261
261
Integer :: from_digits ( num. as_words ( ) , Order :: Lsf )
262
262
}
263
263
0 commit comments