ark-curves-cherry-picked/curve-benches/src/macros/field.rs
curve-benches/src/macros/field.rsRaw
1#[macro_export]
2macro_rules! f_bench {
3 // Use this for base fields
4 ($f:ident, $f_type:ty, $f_repr:ident, $f_repr_type:ty, $modname:ident) => {
5 pub mod $modname {
6 use super::*;
7 field_common!($f, $f_type);
8 sqrt!($f, $f_type);
9 prime_field!($f, $f_type, $f_repr, $f_repr_type);
10 $crate::benchmark_group!(
11 $modname,
12 // common stuff
13 add_assign,
14 sub_assign,
15 double,
16 negate,
17 mul_assign,
18 square,
19 inverse,
20 ser,
21 deser,
22 ser_unchecked,
23 deser_unchecked,
24 // sqrt field stuff
25 sqrt,
26 // prime field stuff
27 repr_add_nocarry,
28 repr_sub_noborrow,
29 repr_num_bits,
30 repr_mul2,
31 repr_div2,
32 into_repr,
33 from_repr,
34 );
35 }
36 use $modname::$modname;
37 };
38 // use this for intermediate fields
39 (extension, $f:ident, $f_type:ty, $modname:ident) => {
40 mod $modname {
41 use super::*;
42 field_common!($f, $f_type);
43 sqrt!($f, $f_type);
44 $crate::benchmark_group!(
45 $modname,
46 // common stuff
47 add_assign,
48 sub_assign,
49 double,
50 negate,
51 mul_assign,
52 square,
53 inverse,
54 ser,
55 deser,
56 ser_unchecked,
57 deser_unchecked,
58 // sqrt field stuff
59 sqrt,
60 );
61 }
62 use $modname::$modname;
63 };
64 // Use this for the full extension field Fqk
65 (target, $f:ident, $f_type:ty, $modname:ident) => {
66 mod $modname {
67 use super::*;
68 field_common!($f, $f_type);
69 $crate::benchmark_group!(
70 $modname,
71 // common stuff
72 add_assign,
73 sub_assign,
74 double,
75 negate,
76 mul_assign,
77 square,
78 inverse,
79 ser,
80 deser,
81 ser_unchecked,
82 deser_unchecked,
83 );
84 }
85 use $modname::$modname;
86 };
87}
88
89#[macro_export]
90macro_rules! field_common {
91 ($f:ident, $f_type:ty) => {
92 fn add_assign(b: &mut $crate::bencher::Bencher) {
93 const SAMPLES: usize = 1000;
94
95 let mut rng = ark_std::test_rng();
96
97 let v: Vec<_> = (0..SAMPLES)
98 .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng)))
99 .collect();
100
101 let mut count = 0;
102 b.iter(|| {
103 let mut tmp = v[count].0;
104 n_fold!(tmp, v, add_assign, count);
105 count = (count + 1) % SAMPLES;
106 tmp
107 });
108 }
109
110 fn sub_assign(b: &mut $crate::bencher::Bencher) {
111 const SAMPLES: usize = 1000;
112
113 let mut rng = ark_std::test_rng();
114
115 let v: Vec<_> = (0..SAMPLES)
116 .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng)))
117 .collect();
118
119 let mut count = 0;
120 b.iter(|| {
121 let mut tmp = v[count].0;
122 n_fold!(tmp, v, sub_assign, count);
123 count = (count + 1) % SAMPLES;
124 tmp
125 });
126 }
127
128 fn double(b: &mut $crate::bencher::Bencher) {
129 const SAMPLES: usize = 1000;
130
131 let mut rng = ark_std::test_rng();
132
133 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
134
135 let mut count = 0;
136 b.iter(|| {
137 let mut tmp = v[count];
138 n_fold!(tmp, double_in_place);
139 count = (count + 1) % SAMPLES;
140 tmp
141 });
142 }
143
144 fn negate(b: &mut $crate::bencher::Bencher) {
145 const SAMPLES: usize = 1000;
146
147 let mut rng = ark_std::test_rng();
148
149 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
150
151 let mut count = 0;
152 b.iter(|| {
153 let mut tmp = v[count];
154 tmp = -tmp;
155 count = (count + 1) % SAMPLES;
156 tmp
157 });
158 }
159
160 fn mul_assign(b: &mut $crate::bencher::Bencher) {
161 const SAMPLES: usize = 1000;
162
163 let mut rng = ark_std::test_rng();
164
165 let v: Vec<_> = (0..SAMPLES)
166 .map(|_| ($f::rand(&mut rng), $f::rand(&mut rng)))
167 .collect();
168
169 let mut count = 0;
170 b.iter(|| {
171 let mut tmp = v[count].0;
172 n_fold!(tmp, v, mul_assign, count);
173 count = (count + 1) % SAMPLES;
174 tmp
175 });
176 }
177
178 fn square(b: &mut $crate::bencher::Bencher) {
179 const SAMPLES: usize = 1000;
180
181 let mut rng = ark_std::test_rng();
182
183 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
184
185 let mut count = 0;
186 b.iter(|| {
187 let mut tmp = v[count];
188 n_fold!(tmp, square_in_place);
189 count = (count + 1) % SAMPLES;
190 tmp
191 });
192 }
193
194 fn inverse(b: &mut $crate::bencher::Bencher) {
195 const SAMPLES: usize = 1000;
196
197 let mut rng = ark_std::test_rng();
198
199 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
200
201 let mut count = 0;
202 b.iter(|| {
203 let tmp = v[count].inverse();
204 count = (count + 1) % SAMPLES;
205 tmp
206 });
207 }
208
209 fn deser(b: &mut $crate::bencher::Bencher) {
210 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
211 const SAMPLES: usize = 1000;
212
213 let mut rng = ark_std::test_rng();
214
215 let mut num_bytes = 0;
216 let v: Vec<_> = (0..SAMPLES)
217 .flat_map(|_| {
218 let mut bytes = Vec::with_capacity(1000);
219 let tmp = $f::rand(&mut rng);
220 tmp.serialize(&mut bytes).unwrap();
221 num_bytes = bytes.len();
222 bytes
223 })
224 .collect();
225
226 let mut count = 0;
227 b.iter(|| {
228 count = (count + 1) % SAMPLES;
229 let index = count * num_bytes;
230 <$f_type>::deserialize(&v[index..(index + num_bytes)]).unwrap()
231 });
232 }
233
234 fn ser(b: &mut $crate::bencher::Bencher) {
235 use ark_serialize::CanonicalSerialize;
236 const SAMPLES: usize = 1000;
237
238 let mut rng = ark_std::test_rng();
239
240 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
241 let mut bytes = Vec::with_capacity(1000);
242
243 let mut count = 0;
244 b.iter(|| {
245 let tmp = v[count];
246 count = (count + 1) % SAMPLES;
247 bytes.clear();
248 tmp.serialize(&mut bytes)
249 });
250 }
251
252 fn deser_unchecked(b: &mut $crate::bencher::Bencher) {
253 use ark_serialize::{CanonicalDeserialize, CanonicalSerialize};
254 const SAMPLES: usize = 1000;
255
256 let mut rng = ark_std::test_rng();
257
258 let mut num_bytes = 0;
259 let v: Vec<_> = (0..SAMPLES)
260 .flat_map(|_| {
261 let mut bytes = Vec::with_capacity(1000);
262 let tmp = $f::rand(&mut rng);
263 tmp.serialize_unchecked(&mut bytes).unwrap();
264 num_bytes = bytes.len();
265 bytes
266 })
267 .collect();
268
269 let mut count = 0;
270 b.iter(|| {
271 count = (count + 1) % SAMPLES;
272 let index = count * num_bytes;
273 <$f_type>::deserialize_unchecked(&v[index..(index + num_bytes)]).unwrap()
274 });
275 }
276
277 fn ser_unchecked(b: &mut $crate::bencher::Bencher) {
278 use ark_serialize::CanonicalSerialize;
279 const SAMPLES: usize = 1000;
280
281 let mut rng = ark_std::test_rng();
282
283 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
284 let mut bytes = Vec::with_capacity(1000);
285
286 let mut count = 0;
287 b.iter(|| {
288 let tmp = v[count];
289 count = (count + 1) % SAMPLES;
290 bytes.clear();
291 tmp.serialize_unchecked(&mut bytes)
292 });
293 }
294 };
295}
296
297#[macro_export]
298macro_rules! sqrt {
299 ($f:ident, $f_type:ty) => {
300 pub fn sqrt(b: &mut $crate::bencher::Bencher) {
301 const SAMPLES: usize = 1000;
302
303 let mut rng = ark_std::test_rng();
304
305 let v: Vec<$f_type> = (0..SAMPLES)
306 .map(|_| {
307 let mut tmp = $f::rand(&mut rng);
308 tmp.square_in_place();
309 tmp
310 })
311 .collect();
312
313 let mut count = 0;
314 b.iter(|| {
315 count = (count + 1) % SAMPLES;
316 v[count].sqrt()
317 });
318 }
319 };
320}
321
322#[macro_export]
323macro_rules! prime_field {
324 ($f:ident, $f_type:ty, $f_repr:ident, $f_repr_type:ty) => {
325 fn repr_add_nocarry(b: &mut $crate::bencher::Bencher) {
326 const SAMPLES: usize = 1000;
327
328 let mut rng = ark_std::test_rng();
329
330 let v: Vec<_> = (0..SAMPLES)
331 .map(|_| {
332 let mut tmp1 = $f_repr::rand(&mut rng);
333 let mut tmp2 = $f_repr::rand(&mut rng);
334 // Shave a few bits off to avoid overflow.
335 for _ in 0..3 {
336 tmp1.div2();
337 tmp2.div2();
338 }
339 (tmp1, tmp2)
340 })
341 .collect();
342
343 let mut count = 0;
344 b.iter(|| {
345 let mut tmp = v[count].0;
346 n_fold!(tmp, v, add_with_carry, count);
347 count = (count + 1) % SAMPLES;
348 tmp
349 });
350 }
351
352 fn repr_sub_noborrow(b: &mut $crate::bencher::Bencher) {
353 const SAMPLES: usize = 1000;
354
355 let mut rng = ark_std::test_rng();
356
357 let v: Vec<_> = (0..SAMPLES)
358 .map(|_| {
359 let tmp1 = $f_repr::rand(&mut rng);
360 let mut tmp2 = tmp1;
361 // Ensure tmp2 is smaller than tmp1.
362 for _ in 0..10 {
363 tmp2.div2();
364 }
365 (tmp1, tmp2)
366 })
367 .collect();
368
369 let mut count = 0;
370 b.iter(|| {
371 let mut tmp = v[count].0;
372 n_fold!(tmp, v, sub_with_borrow, count);
373 count = (count + 1) % SAMPLES;
374 tmp;
375 });
376 }
377
378 fn repr_num_bits(b: &mut $crate::bencher::Bencher) {
379 const SAMPLES: usize = 1000;
380
381 let mut rng = ark_std::test_rng();
382
383 let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect();
384
385 let mut count = 0;
386 b.iter(|| {
387 let tmp = v[count].num_bits();
388 count = (count + 1) % SAMPLES;
389 tmp;
390 });
391 }
392
393 fn repr_mul2(b: &mut $crate::bencher::Bencher) {
394 const SAMPLES: usize = 1000;
395
396 let mut rng = ark_std::test_rng();
397
398 let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect();
399
400 let mut count = 0;
401 b.iter(|| {
402 let mut tmp = v[count];
403 n_fold!(tmp, mul2);
404 count = (count + 1) % SAMPLES;
405 tmp;
406 });
407 }
408
409 fn repr_div2(b: &mut $crate::bencher::Bencher) {
410 const SAMPLES: usize = 1000;
411
412 let mut rng = ark_std::test_rng();
413
414 let v: Vec<$f_repr_type> = (0..SAMPLES).map(|_| $f_repr::rand(&mut rng)).collect();
415
416 let mut count = 0;
417 b.iter(|| {
418 let mut tmp = v[count];
419 n_fold!(tmp, div2);
420 count = (count + 1) % SAMPLES;
421 tmp;
422 });
423 }
424
425 fn into_repr(b: &mut $crate::bencher::Bencher) {
426 const SAMPLES: usize = 1000;
427
428 let mut rng = ark_std::test_rng();
429
430 let v: Vec<$f_type> = (0..SAMPLES).map(|_| $f::rand(&mut rng)).collect();
431
432 let mut count = 0;
433 b.iter(|| {
434 count = (count + 1) % SAMPLES;
435 v[count].into_bigint();
436 });
437 }
438
439 fn from_repr(b: &mut $crate::bencher::Bencher) {
440 const SAMPLES: usize = 1000;
441
442 let mut rng = ark_std::test_rng();
443
444 let v: Vec<$f_repr_type> = (0..SAMPLES)
445 .map(|_| $f::rand(&mut rng).into_bigint())
446 .collect();
447
448 let mut count = 0;
449 b.iter(|| {
450 count = (count + 1) % SAMPLES;
451 let _ = $f::from(v[count]);
452 });
453 }
454 };
455}