@@ -4,15 +4,64 @@ use criterion::{
4
4
} ;
5
5
use crypto_bigint:: {
6
6
modular:: { DynResidue , DynResidueParams } ,
7
- Random , U256 ,
7
+ Inverter , PrecomputeInverter , Random , U256 ,
8
8
} ;
9
9
use rand_core:: OsRng ;
10
10
11
11
#[ cfg( feature = "alloc" ) ]
12
12
use crypto_bigint:: MultiExponentiate ;
13
13
14
+ fn bench_montgomery_conversion < M : Measurement > ( group : & mut BenchmarkGroup < ' _ , M > ) {
15
+ group. bench_function ( "DynResidueParams creation" , |b| {
16
+ b. iter_batched (
17
+ || U256 :: random ( & mut OsRng ) | U256 :: ONE ,
18
+ |modulus| black_box ( DynResidueParams :: new ( & modulus) ) ,
19
+ BatchSize :: SmallInput ,
20
+ )
21
+ } ) ;
22
+
23
+ let params = DynResidueParams :: new ( & ( U256 :: random ( & mut OsRng ) | U256 :: ONE ) ) . unwrap ( ) ;
24
+ group. bench_function ( "DynResidue creation" , |b| {
25
+ b. iter_batched (
26
+ || U256 :: random ( & mut OsRng ) ,
27
+ |x| black_box ( DynResidue :: new ( & x, params) ) ,
28
+ BatchSize :: SmallInput ,
29
+ )
30
+ } ) ;
31
+
32
+ let params = DynResidueParams :: new ( & ( U256 :: random ( & mut OsRng ) | U256 :: ONE ) ) . unwrap ( ) ;
33
+ group. bench_function ( "DynResidue retrieve" , |b| {
34
+ b. iter_batched (
35
+ || DynResidue :: new ( & U256 :: random ( & mut OsRng ) , params) ,
36
+ |x| black_box ( x. retrieve ( ) ) ,
37
+ BatchSize :: SmallInput ,
38
+ )
39
+ } ) ;
40
+ }
41
+
14
42
fn bench_montgomery_ops < M : Measurement > ( group : & mut BenchmarkGroup < ' _ , M > ) {
15
43
let params = DynResidueParams :: new ( & ( U256 :: random ( & mut OsRng ) | U256 :: ONE ) ) . unwrap ( ) ;
44
+
45
+ group. bench_function ( "invert, U256" , |b| {
46
+ b. iter_batched (
47
+ || DynResidue :: new ( & U256 :: random ( & mut OsRng ) , params) ,
48
+ |x| black_box ( x) . invert ( ) ,
49
+ BatchSize :: SmallInput ,
50
+ )
51
+ } ) ;
52
+
53
+ group. bench_function ( "Bernstein-Yang invert, U256" , |b| {
54
+ b. iter_batched (
55
+ || {
56
+ let x = DynResidue :: new ( & U256 :: random ( & mut OsRng ) , params) ;
57
+ let inverter = x. params ( ) . precompute_inverter ( ) ;
58
+ ( x, inverter)
59
+ } ,
60
+ |( x, inverter) | inverter. invert ( & black_box ( x) ) ,
61
+ BatchSize :: SmallInput ,
62
+ )
63
+ } ) ;
64
+
16
65
group. bench_function ( "multiplication, U256*U256" , |b| {
17
66
b. iter_batched (
18
67
|| {
@@ -25,8 +74,6 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
25
74
)
26
75
} ) ;
27
76
28
- let m = U256 :: random ( & mut OsRng ) | U256 :: ONE ;
29
- let params = DynResidueParams :: new ( & m) . unwrap ( ) ;
30
77
group. bench_function ( "modpow, U256^U256" , |b| {
31
78
b. iter_batched (
32
79
|| {
@@ -70,34 +117,6 @@ fn bench_montgomery_ops<M: Measurement>(group: &mut BenchmarkGroup<'_, M>) {
70
117
}
71
118
}
72
119
73
- fn bench_montgomery_conversion < M : Measurement > ( group : & mut BenchmarkGroup < ' _ , M > ) {
74
- group. bench_function ( "DynResidueParams creation" , |b| {
75
- b. iter_batched (
76
- || U256 :: random ( & mut OsRng ) | U256 :: ONE ,
77
- |modulus| black_box ( DynResidueParams :: new ( & modulus) ) ,
78
- BatchSize :: SmallInput ,
79
- )
80
- } ) ;
81
-
82
- let params = DynResidueParams :: new ( & ( U256 :: random ( & mut OsRng ) | U256 :: ONE ) ) . unwrap ( ) ;
83
- group. bench_function ( "DynResidue creation" , |b| {
84
- b. iter_batched (
85
- || U256 :: random ( & mut OsRng ) ,
86
- |x| black_box ( DynResidue :: new ( & x, params) ) ,
87
- BatchSize :: SmallInput ,
88
- )
89
- } ) ;
90
-
91
- let params = DynResidueParams :: new ( & ( U256 :: random ( & mut OsRng ) | U256 :: ONE ) ) . unwrap ( ) ;
92
- group. bench_function ( "DynResidue retrieve" , |b| {
93
- b. iter_batched (
94
- || DynResidue :: new ( & U256 :: random ( & mut OsRng ) , params) ,
95
- |x| black_box ( x. retrieve ( ) ) ,
96
- BatchSize :: SmallInput ,
97
- )
98
- } ) ;
99
- }
100
-
101
120
fn bench_montgomery ( c : & mut Criterion ) {
102
121
let mut group = c. benchmark_group ( "Montgomery arithmetic" ) ;
103
122
bench_montgomery_conversion ( & mut group) ;
0 commit comments