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