arnaucube
arnaucube/ark-curves-cherry-pickedPublic Self-hosted repository
ark-curves-cherry-picked/curve-benches/src/macros/pairing.rs
curve-benches/src/macros/pairing.rsRaw
 1#[macro_export]
 2macro_rules! pairing_bench {
 3    ($curve:ident, $pairing_field:ident) => {
 4
 5        fn bench_pairing(c: &mut $crate::criterion::Criterion) {
 6            let mut group = c.benchmark_group("Pairing for ".to_string() + core::stringify!($curve));
 7            group.bench_function("Miller Loop", miller_loop);
 8            group.bench_function("Final Exponentiation", final_exponentiation);
 9            group.bench_function("Full Pairing", full_pairing);
10        }
11        
12        fn miller_loop(b: &mut $crate::criterion::Bencher) {
13            const SAMPLES: usize = 1000;
14
15            let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
16
17            let g1s = (0..SAMPLES).map(|_| G1::rand(&mut rng)).collect::<Vec<_>>();
18            let g2s = (0..SAMPLES).map(|_| G2::rand(&mut rng)).collect::<Vec<_>>();
19            let g1s = G1::batch_normalization_into_affine(&g1s);
20            let g2s = G2::batch_normalization_into_affine(&g2s);
21            let prepared = g1s
22                .into_iter()
23                .zip(g2s)
24                .map(|(g1, g2)| (g1.into(), g2.into()))
25                .collect::<Vec<(<$curve as PairingEngine>::G1Prepared, <$curve as PairingEngine>::G2Prepared)>>();
26            let mut count = 0;
27            b.iter(|| {
28                let tmp = $curve::miller_loop(&[(prepared[count].0.clone(), prepared[count].1.clone())]);
29                count = (count + 1) % SAMPLES;
30                tmp
31            });
32        }
33
34        
35        fn final_exponentiation(b: &mut $crate::criterion::Bencher) {
36            const SAMPLES: usize = 1000;
37
38            let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
39
40            let v: Vec<_> = (0..SAMPLES)
41                .map(|_| {
42                    (
43                        G1Affine::from(G1::rand(&mut rng)).into(),
44                        G2Affine::from(G2::rand(&mut rng)).into(),
45                    )
46                })
47                .map(|(p, q)| $curve::miller_loop(&[(p, q)]))
48                .collect();
49
50            let mut count = 0;
51            b.iter(|| {
52                let tmp = $curve::final_exponentiation(&v[count]);
53                count = (count + 1) % SAMPLES;
54                tmp
55            });
56        }
57
58        
59        fn full_pairing(b: &mut $crate::criterion::Bencher) {
60            const SAMPLES: usize = 1000;
61
62            let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
63
64            let v: Vec<(G1, G2)> = (0..SAMPLES)
65                .map(|_| (G1::rand(&mut rng), G2::rand(&mut rng)))
66                .collect();
67
68            let mut count = 0;
69            b.iter(|| {
70                let tmp = $curve::pairing(v[count].0, v[count].1);
71                count = (count + 1) % SAMPLES;
72                tmp
73            });
74        }
75
76        $crate::criterion::criterion_group!(
77            name = pairing;
78            config = $crate::criterion::Criterion::default()
79                .sample_size(10)
80                .warm_up_time(core::time::Duration::from_millis(500))
81                .measurement_time(core::time::Duration::from_secs(1));
82            targets = bench_pairing,
83        );
84    }
85}