1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
use crate::traits::{AffineCurve, PairingCurve, ProjectiveCurve};
use snarkvm_fields::{Field, PrimeField, SquareRootField, ToConstraintField};
use core::{fmt::Debug, hash::Hash, iter};
pub trait PairingEngine: Sized + 'static + Copy + Debug + PartialEq + Eq + Hash + Sync + Send {
type Fr: PrimeField + SquareRootField + Into<<Self::Fr as PrimeField>::BigInteger>;
type G1Projective: ProjectiveCurve<BaseField = Self::Fq, ScalarField = Self::Fr, Affine = Self::G1Affine>
+ From<Self::G1Affine>;
type G1Affine: AffineCurve<BaseField = Self::Fq, ScalarField = Self::Fr, Projective = Self::G1Projective>
+ PairingCurve<PairWith = Self::G2Affine, PairingResult = Self::Fqk>
+ From<Self::G1Projective>
+ ToConstraintField<Self::Fq>;
type G2Projective: ProjectiveCurve<BaseField = Self::Fqe, ScalarField = Self::Fr, Affine = Self::G2Affine>
+ From<Self::G2Affine>;
type G2Affine: AffineCurve<BaseField = Self::Fqe, ScalarField = Self::Fr, Projective = Self::G2Projective>
+ PairingCurve<PairWith = Self::G1Affine, PairingResult = Self::Fqk>
+ From<Self::G2Projective>
+ ToConstraintField<Self::Fq>;
type Fq: PrimeField + SquareRootField;
type Fqe: SquareRootField;
type Fqk: Field;
#[must_use]
fn miller_loop<'a, I>(i: I) -> Self::Fqk
where
I: Iterator<
Item = (&'a <Self::G1Affine as PairingCurve>::Prepared, &'a <Self::G2Affine as PairingCurve>::Prepared),
>;
#[must_use]
fn final_exponentiation(_: &Self::Fqk) -> Option<Self::Fqk>;
#[must_use]
fn product_of_pairings<'a, I>(i: I) -> Self::Fqk
where
I: Iterator<
Item = (&'a <Self::G1Affine as PairingCurve>::Prepared, &'a <Self::G2Affine as PairingCurve>::Prepared),
>,
{
Self::final_exponentiation(&Self::miller_loop(i)).unwrap()
}
#[must_use]
fn pairing<G1, G2>(p: G1, q: G2) -> Self::Fqk
where
G1: Into<Self::G1Affine>,
G2: Into<Self::G2Affine>,
{
Self::final_exponentiation(&Self::miller_loop(iter::once((&p.into().prepare(), &q.into().prepare())))).unwrap()
}
}