11use  criterion:: { criterion_group,  criterion_main,  BatchSize ,  Criterion } ; 
2- use  crypto_bigint:: { nlimbs,  Uint ,  U1024 } ; 
2+ use  crypto_bigint:: { nlimbs,  Odd ,   Uint ,  U1024 } ; 
33use  rand_chacha:: ChaCha8Rng ; 
44use  rand_core:: { CryptoRngCore ,  OsRng ,  SeedableRng } ; 
55
@@ -22,27 +22,27 @@ fn make_rng() -> ChaCha8Rng {
2222    ChaCha8Rng :: from_seed ( * b"01234567890123456789012345678901" ) 
2323} 
2424
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 ) ; 
2727    Sieve :: new ( & start,  Uint :: < L > :: BITS ,  false ) 
2828} 
2929
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 > >  { 
3131    let  mut  sieve = make_sieve ( rng) ; 
32-     sieve. next ( ) . unwrap ( ) 
32+     Odd :: new ( sieve. next ( ) . unwrap ( ) ) . unwrap ( ) 
3333} 
3434
3535fn  bench_sieve ( c :  & mut  Criterion )  { 
3636    let  mut  group = c. benchmark_group ( "Sieve" ) ; 
3737
3838    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 ) ) 
4040    } ) ; 
4141
4242    group. bench_function ( "(U128) creation" ,  |b| { 
4343        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 ) , 
4646            BatchSize :: SmallInput , 
4747        ) 
4848    } ) ; 
@@ -57,13 +57,13 @@ fn bench_sieve(c: &mut Criterion) {
5757    } ) ; 
5858
5959    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 ) ) 
6161    } ) ; 
6262
6363    group. bench_function ( "(U1024) creation" ,  |b| { 
6464        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 ) , 
6767            BatchSize :: SmallInput , 
6868        ) 
6969    } ) ; 
@@ -84,7 +84,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
8484
8585    group. bench_function ( "(U128) creation" ,  |b| { 
8686        b. iter_batched ( 
87-             || random_odd_uint :: < {  nlimbs ! ( 128 )  } > ( & mut  OsRng ,  128 ) , 
87+             || random_odd_uint :: < Uint < {  nlimbs ! ( 128 )  } > > ( & mut  OsRng ,  128 ) , 
8888            |start| MillerRabin :: new ( & start) , 
8989            BatchSize :: SmallInput , 
9090        ) 
@@ -100,7 +100,7 @@ fn bench_miller_rabin(c: &mut Criterion) {
100100
101101    group. bench_function ( "(U1024) creation" ,  |b| { 
102102        b. iter_batched ( 
103-             || random_odd_uint :: < {  nlimbs ! ( 1024 )  } > ( & mut  OsRng ,  1024 ) , 
103+             || random_odd_uint :: < Uint < {  nlimbs ! ( 1024 )  } > > ( & mut  OsRng ,  1024 ) , 
104104            |start| MillerRabin :: new ( & start) , 
105105            BatchSize :: SmallInput , 
106106        ) 
@@ -122,7 +122,7 @@ fn bench_lucas(c: &mut Criterion) {
122122    group. bench_function ( "(U128) Selfridge base, strong check (pre-sieved)" ,  |b| { 
123123        b. iter_batched ( 
124124            || 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 ) , 
126126            BatchSize :: SmallInput , 
127127        ) 
128128    } ) ; 
@@ -131,7 +131,7 @@ fn bench_lucas(c: &mut Criterion) {
131131    group. bench_function ( "(U1024) Selfridge base, strong check (pre-sieved)" ,  |b| { 
132132        b. iter_batched ( 
133133            || 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 ) , 
135135            BatchSize :: SmallInput , 
136136        ) 
137137    } ) ; 
@@ -140,7 +140,7 @@ fn bench_lucas(c: &mut Criterion) {
140140    group. bench_function ( "(U1024) A* base, Lucas-V check (pre-sieved)" ,  |b| { 
141141        b. iter_batched ( 
142142            || 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 ) , 
144144            BatchSize :: SmallInput , 
145145        ) 
146146    } ) ; 
@@ -151,7 +151,7 @@ fn bench_lucas(c: &mut Criterion) {
151151        |b| { 
152152            b. iter_batched ( 
153153                || 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 ) , 
155155                BatchSize :: SmallInput , 
156156            ) 
157157        } , 
@@ -161,7 +161,7 @@ fn bench_lucas(c: &mut Criterion) {
161161    group. bench_function ( "(U1024) brute force base, extra strong (pre-sieved)" ,  |b| { 
162162        b. iter_batched ( 
163163            || 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 ) , 
165165            BatchSize :: SmallInput , 
166166        ) 
167167    } ) ; 
@@ -192,39 +192,39 @@ fn bench_presets(c: &mut Criterion) {
192192
193193    group. bench_function ( "(U128) Prime test" ,  |b| { 
194194        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 ( ) ) , 
197197            BatchSize :: SmallInput , 
198198        ) 
199199    } ) ; 
200200
201201    group. bench_function ( "(U128) Safe prime test" ,  |b| { 
202202        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 ( ) ) , 
205205            BatchSize :: SmallInput , 
206206        ) 
207207    } ) ; 
208208
209209    let  mut  rng = make_rng ( ) ; 
210210    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 ) ) 
212212    } ) ; 
213213
214214    let  mut  rng = make_rng ( ) ; 
215215    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 ) ) 
217217    } ) ; 
218218
219219    let  mut  rng = make_rng ( ) ; 
220220    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 ) ) 
222222    } ) ; 
223223
224224    group. sample_size ( 20 ) ; 
225225    let  mut  rng = make_rng ( ) ; 
226226    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 ) ) 
228228    } ) ; 
229229
230230    group. finish ( ) ; 
@@ -234,19 +234,19 @@ fn bench_presets(c: &mut Criterion) {
234234
235235    let  mut  rng = make_rng ( ) ; 
236236    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 ) ) 
238238    } ) ; 
239239
240240    // The performance should scale with the prime size, not with the Uint size. 
241241    // So we should strive for this test's result to be as close as possible 
242242    // to that of the previous one and as far away as possible from the next one. 
243243    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 ) ) 
245245    } ) ; 
246246
247247    // The upper bound for the previous test. 
248248    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 ) ) 
250250    } ) ; 
251251
252252    group. finish ( ) ; 
@@ -257,7 +257,7 @@ fn bench_gmp(c: &mut Criterion) {
257257    let  mut  group = c. benchmark_group ( "GMP" ) ; 
258258
259259    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 ( ) ; 
261261        Integer :: from_digits ( num. as_words ( ) ,  Order :: Lsf ) 
262262    } 
263263
0 commit comments