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