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        fn miller_loop(b: &mut $crate::bencher::Bencher) {
 5            const SAMPLES: usize = 1000;
 6
 7            let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
 8
 9            let g1s = (0..SAMPLES).map(|_| G1::rand(&mut rng)).collect::<Vec<_>>();
10            let g2s = (0..SAMPLES).map(|_| G2::rand(&mut rng)).collect::<Vec<_>>();
11            let g1s = G1::batch_normalization_into_affine(&g1s);
12            let g2s = G2::batch_normalization_into_affine(&g2s);
13            let prepared = g1s
14                .into_iter()
15                .zip(g2s)
16                .map(|(g1, g2)| (g1.into(), g2.into()))
17                .collect::<Vec<(
18                    <$curve as PairingEngine>::G1Prepared,
19                    <$curve as PairingEngine>::G2Prepared,
20                )>>();
21            let mut count = 0;
22            b.iter(|| {
23                let tmp =
24                    $curve::miller_loop(&[(prepared[count].0.clone(), prepared[count].1.clone())]);
25                count = (count + 1) % SAMPLES;
26                tmp
27            });
28        }
29
30        fn final_exponentiation(b: &mut $crate::bencher::Bencher) {
31            const SAMPLES: usize = 1000;
32
33            let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
34
35            let v: Vec<_> = (0..SAMPLES)
36                .map(|_| {
37                    (
38                        G1Affine::from(G1::rand(&mut rng)).into(),
39                        G2Affine::from(G2::rand(&mut rng)).into(),
40                    )
41                })
42                .map(|(p, q)| $curve::miller_loop(&[(p, q)]))
43                .collect();
44
45            let mut count = 0;
46            b.iter(|| {
47                let tmp = $curve::final_exponentiation(&v[count]);
48                count = (count + 1) % SAMPLES;
49                tmp
50            });
51        }
52
53        fn full_pairing(b: &mut $crate::bencher::Bencher) {
54            const SAMPLES: usize = 1000;
55
56            let mut rng = XorShiftRng::seed_from_u64(1231275789u64);
57
58            let v: Vec<(G1, G2)> = (0..SAMPLES)
59                .map(|_| (G1::rand(&mut rng), G2::rand(&mut rng)))
60                .collect();
61
62            let mut count = 0;
63            b.iter(|| {
64                let tmp = $curve::pairing(v[count].0, v[count].1);
65                count = (count + 1) % SAMPLES;
66                tmp
67            });
68        }
69
70        $crate::benchmark_group!(pairing, miller_loop, final_exponentiation, full_pairing,);
71    };
72}