ark-curves-cherry-picked/curve-benches/src/macros/ec.rs
curve-benches/src/macros/ec.rsRaw
1#[macro_export]
2macro_rules! ec_bench {
3 ($projective:ty, $affine:ty) => {
4 fn rand(b: &mut $crate::bencher::Bencher) {
5 let mut rng = ark_std::test_rng();
6 b.iter(|| <$projective>::rand(&mut rng));
7 }
8
9 fn mul_assign(b: &mut $crate::bencher::Bencher) {
10 const SAMPLES: usize = 1000;
11
12 let mut rng = ark_std::test_rng();
13
14 let v: Vec<($projective, Fr)> = (0..SAMPLES)
15 .map(|_| (<$projective>::rand(&mut rng), Fr::rand(&mut rng)))
16 .collect();
17
18 let mut count = 0;
19 b.iter(|| {
20 let mut tmp = v[count].0;
21 tmp *= v[count].1;
22 count = (count + 1) % SAMPLES;
23 tmp
24 });
25 }
26
27 fn add_assign(b: &mut $crate::bencher::Bencher) {
28 const SAMPLES: usize = 1000;
29
30 let mut rng = ark_std::test_rng();
31
32 let v: Vec<($projective, $projective)> = (0..SAMPLES)
33 .map(|_| (<$projective>::rand(&mut rng), <$projective>::rand(&mut rng)))
34 .collect();
35
36 let mut count = 0;
37 b.iter(|| {
38 let mut tmp = v[count].0;
39 n_fold!(tmp, v, add_assign, count);
40 count = (count + 1) % SAMPLES;
41 tmp
42 });
43 }
44
45 fn sub_assign(b: &mut $crate::bencher::Bencher) {
46 const SAMPLES: usize = 1000;
47
48 let mut rng = ark_std::test_rng();
49
50 let v: Vec<($projective, $projective)> = (0..SAMPLES)
51 .map(|_| (<$projective>::rand(&mut rng), <$projective>::rand(&mut rng)))
52 .collect();
53
54 let mut count = 0;
55 b.iter(|| {
56 let mut tmp = v[count].0;
57 n_fold!(tmp, v, sub_assign, count);
58 count = (count + 1) % SAMPLES;
59 tmp
60 });
61 }
62
63 fn double(b: &mut $crate::bencher::Bencher) {
64 const SAMPLES: usize = 1000;
65
66 let mut rng = ark_std::test_rng();
67
68 let v: Vec<$projective> = (0..SAMPLES)
69 .map(|_| <$projective>::rand(&mut rng))
70 .collect();
71
72 let mut count = 0;
73 b.iter(|| {
74 let mut tmp = v[count];
75 n_fold!(tmp, double_in_place);
76 count = (count + 1) % SAMPLES;
77 tmp
78 });
79 }
80
81 fn add_assign_mixed(b: &mut $crate::bencher::Bencher) {
82 const SAMPLES: usize = 1000;
83
84 let mut rng = ark_std::test_rng();
85
86 let v: Vec<($projective, $affine)> = (0..SAMPLES)
87 .map(|_| {
88 (
89 <$projective>::rand(&mut rng),
90 <$projective>::rand(&mut rng).into(),
91 )
92 })
93 .collect();
94
95 let mut count = 0;
96 b.iter(|| {
97 let mut tmp = v[count].0;
98 n_fold!(tmp, v, add_assign_mixed, count);
99 count = (count + 1) % SAMPLES;
100 tmp
101 });
102 }
103
104 fn deser(b: &mut $crate::bencher::Bencher) {
105 use ark_ec::ProjectiveCurve;
106 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
107 const SAMPLES: usize = 1000;
108
109 let mut rng = ark_std::test_rng();
110
111 let mut num_bytes = 0;
112 let tmp = <$projective>::rand(&mut rng).into_affine();
113 let v: Vec<_> = (0..SAMPLES)
114 .flat_map(|_| {
115 let mut bytes = Vec::with_capacity(1000);
116 tmp.serialize(&mut bytes).unwrap();
117 num_bytes = bytes.len();
118 bytes
119 })
120 .collect();
121
122 let mut count = 0;
123 b.iter(|| {
124 count = (count + 1) % SAMPLES;
125 let index = count * num_bytes;
126 <$affine>::deserialize(&v[index..(index + num_bytes)]).unwrap()
127 });
128 }
129
130 fn ser(b: &mut $crate::bencher::Bencher) {
131 use ark_ec::ProjectiveCurve;
132 use ark_serialize::CanonicalSerialize;
133 const SAMPLES: usize = 1000;
134
135 let mut rng = ark_std::test_rng();
136
137 let mut v: Vec<_> = (0..SAMPLES)
138 .map(|_| <$projective>::rand(&mut rng))
139 .collect();
140 let v = <$projective>::batch_normalization_into_affine(v.as_mut_slice());
141 let mut bytes = Vec::with_capacity(1000);
142
143 let mut count = 0;
144 b.iter(|| {
145 let tmp = v[count];
146 count = (count + 1) % SAMPLES;
147 bytes.clear();
148 tmp.serialize(&mut bytes)
149 });
150 }
151
152 fn deser_unchecked(b: &mut $crate::bencher::Bencher) {
153 use ark_ec::ProjectiveCurve;
154 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
155 const SAMPLES: usize = 1000;
156
157 let mut rng = ark_std::test_rng();
158
159 let mut num_bytes = 0;
160 let tmp = <$projective>::rand(&mut rng).into_affine();
161 let v: Vec<_> = (0..SAMPLES)
162 .flat_map(|_| {
163 let mut bytes = Vec::with_capacity(1000);
164 tmp.serialize_unchecked(&mut bytes).unwrap();
165 num_bytes = bytes.len();
166 bytes
167 })
168 .collect();
169
170 let mut count = 0;
171 b.iter(|| {
172 count = (count + 1) % SAMPLES;
173 let index = count * num_bytes;
174 <$affine>::deserialize_unchecked(&v[index..(index + num_bytes)]).unwrap()
175 });
176 }
177
178 fn ser_unchecked(b: &mut $crate::bencher::Bencher) {
179 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
180 const SAMPLES: usize = 1000;
181
182 let mut rng = ark_std::test_rng();
183
184 let mut v: Vec<_> = (0..SAMPLES)
185 .map(|_| <$projective>::rand(&mut rng))
186 .collect();
187 let v = <$projective>::batch_normalization_into_affine(v.as_mut_slice());
188 let mut bytes = Vec::with_capacity(1000);
189
190 let mut count = 0;
191 b.iter(|| {
192 let tmp = v[count];
193 count = (count + 1) % SAMPLES;
194 bytes.clear();
195 tmp.serialize_unchecked(&mut bytes)
196 });
197 }
198
199 fn deser_uncompressed(b: &mut $crate::bencher::Bencher) {
200 use ark_ec::ProjectiveCurve;
201 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
202 const SAMPLES: usize = 1000;
203
204 let mut rng = ark_std::test_rng();
205
206 let mut num_bytes = 0;
207 let tmp = <$projective>::rand(&mut rng).into_affine();
208 let v: Vec<_> = (0..SAMPLES)
209 .flat_map(|_| {
210 let mut bytes = Vec::with_capacity(1000);
211 tmp.serialize_uncompressed(&mut bytes).unwrap();
212 num_bytes = bytes.len();
213 bytes
214 })
215 .collect();
216
217 let mut count = 0;
218 b.iter(|| {
219 count = (count + 1) % SAMPLES;
220 let index = count * num_bytes;
221 <$affine>::deserialize_uncompressed(&v[index..(index + num_bytes)]).unwrap()
222 });
223 }
224
225 fn msm_131072(b: &mut $crate::bencher::Bencher) {
226 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
227 const SAMPLES: usize = 131072;
228
229 let mut rng = ark_std::test_rng();
230
231 let g = <$projective>::rand(&mut rng).into_affine();
232 let v: Vec<_> = (0..SAMPLES).map(|_| g).collect();
233 let scalars: Vec<_> = (0..SAMPLES)
234 .map(|_| Fr::rand(&mut rng).into_repr())
235 .collect();
236 b.bench_n(1, |b| {
237 b.iter(|| ark_ec::msm::VariableBase::msm(&v, &scalars));
238 })
239 }
240
241 $crate::benchmark_group!(
242 group_ops,
243 rand,
244 mul_assign,
245 add_assign,
246 sub_assign,
247 add_assign_mixed,
248 double,
249 ser,
250 deser,
251 ser_unchecked,
252 deser_unchecked,
253 deser_uncompressed,
254 msm_131072,
255 );
256 };
257}