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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
use crate::{
templates::{
bls12::{
g1::{G1Affine, G1Prepared, G1Projective},
g2::{G2Affine, G2Prepared, G2Projective},
},
short_weierstrass_jacobian,
},
traits::{ModelParameters, PairingCurve, PairingEngine, ShortWeierstrassParameters},
AffineCurve,
};
use snarkvm_fields::{
fp6_3over2::Fp6Parameters,
Field,
Fp12,
Fp12Parameters,
Fp2,
Fp2Parameters,
One,
PrimeField,
SquareRootField,
Zero,
};
use snarkvm_utilities::bititerator::BitIteratorBE;
use core::{fmt::Debug, hash::Hash, marker::PhantomData};
use serde::{Deserialize, Serialize};
pub enum TwistType {
M,
D,
}
pub trait Bls12Parameters: 'static + Copy + Clone + Debug + PartialEq + Eq + Hash + Send + Sync + Sized {
const X: &'static [u64];
const X_IS_NEGATIVE: bool;
const TWIST_TYPE: TwistType;
type Fp: PrimeField + SquareRootField + Into<<Self::Fp as PrimeField>::BigInteger>;
type Fp2Params: Fp2Parameters<Fp = Self::Fp>;
type Fp6Params: Fp6Parameters<Fp2Params = Self::Fp2Params>;
type Fp12Params: Fp12Parameters<Fp6Params = Self::Fp6Params>;
type G1Parameters: ShortWeierstrassParameters<BaseField = Self::Fp>;
type G2Parameters: ShortWeierstrassParameters<
BaseField = Fp2<Self::Fp2Params>,
ScalarField = <Self::G1Parameters as ModelParameters>::ScalarField,
>;
fn g1_is_in_correct_subgroup(p: &short_weierstrass_jacobian::Affine<Self::G1Parameters>) -> bool {
p.mul_bits(BitIteratorBE::new(<Self::G1Parameters as ModelParameters>::ScalarField::characteristic())).is_zero()
}
fn g2_is_in_correct_subgroup(p: &short_weierstrass_jacobian::Affine<Self::G1Parameters>) -> bool {
p.mul_bits(BitIteratorBE::new(<Self::G1Parameters as ModelParameters>::ScalarField::characteristic())).is_zero()
}
}
#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct Bls12<P: Bls12Parameters>(PhantomData<P>);
type CoeffTriplet<T> = (Fp2<T>, Fp2<T>, Fp2<T>);
impl<P: Bls12Parameters> Bls12<P> {
fn ell(f: &mut Fp12<P::Fp12Params>, coeffs: &CoeffTriplet<P::Fp2Params>, p: &G1Affine<P>) {
let mut c0 = coeffs.0;
let mut c1 = coeffs.1;
let mut c2 = coeffs.2;
match P::TWIST_TYPE {
TwistType::M => {
c2.mul_by_fp(&p.y);
c1.mul_by_fp(&p.x);
f.mul_by_014(&c0, &c1, &c2);
}
TwistType::D => {
c0.mul_by_fp(&p.y);
c1.mul_by_fp(&p.x);
f.mul_by_034(&c0, &c1, &c2);
}
}
}
fn exp_by_x(mut f: Fp12<P::Fp12Params>) -> Fp12<P::Fp12Params> {
f = f.cyclotomic_exp(P::X);
if P::X_IS_NEGATIVE {
f.conjugate();
}
f
}
}
impl<P: Bls12Parameters> PairingEngine for Bls12<P>
where
G1Affine<P>: PairingCurve<
BaseField = <P::G1Parameters as ModelParameters>::BaseField,
ScalarField = <P::G1Parameters as ModelParameters>::ScalarField,
Projective = G1Projective<P>,
PairWith = G2Affine<P>,
Prepared = G1Prepared<P>,
PairingResult = Fp12<P::Fp12Params>,
>,
G2Affine<P>: PairingCurve<
BaseField = <P::G2Parameters as ModelParameters>::BaseField,
ScalarField = <P::G1Parameters as ModelParameters>::ScalarField,
Projective = G2Projective<P>,
PairWith = G1Affine<P>,
Prepared = G2Prepared<P>,
PairingResult = Fp12<P::Fp12Params>,
>,
{
type Fq = P::Fp;
type Fqe = Fp2<P::Fp2Params>;
type Fqk = Fp12<P::Fp12Params>;
type Fr = <P::G1Parameters as ModelParameters>::ScalarField;
type G1Affine = G1Affine<P>;
type G1Projective = G1Projective<P>;
type G2Affine = G2Affine<P>;
type G2Projective = G2Projective<P>;
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),
>,
{
let mut pairs = vec![];
for (p, q) in i {
if !p.is_zero() && !q.is_zero() {
pairs.push((p, q.ell_coeffs.iter()));
}
}
let mut f = Self::Fqk::one();
for i in BitIteratorBE::new(P::X).skip(1) {
f.square_in_place();
for &mut (p, ref mut coeffs) in &mut pairs {
Self::ell(&mut f, coeffs.next().unwrap(), &p.0);
}
if i {
for &mut (p, ref mut coeffs) in &mut pairs {
Self::ell(&mut f, coeffs.next().unwrap(), &p.0);
}
}
}
if P::X_IS_NEGATIVE {
f.conjugate();
}
f
}
fn final_exponentiation(f: &Self::Fqk) -> Option<Self::Fqk> {
let mut f1 = *f;
f1.conjugate();
match f.inverse() {
Some(mut f2) => {
let mut r = f1 * f2;
f2 = r;
r.frobenius_map(2);
r *= &f2;
let mut y0 = r.cyclotomic_square();
y0.conjugate();
let mut y5 = Self::exp_by_x(r);
let mut y1 = y5.cyclotomic_square();
let mut y3 = y0 * y5;
y0 = Self::exp_by_x(y3);
let y2 = Self::exp_by_x(y0);
let mut y4 = Self::exp_by_x(y2);
y4 *= &y1;
y1 = Self::exp_by_x(y4);
y3.conjugate();
y1 *= &y3;
y1 *= &r;
y3 = r;
y3.conjugate();
y0 *= &r;
y0.frobenius_map(3);
y4 *= &y3;
y4.frobenius_map(1);
y5 *= &y2;
y5.frobenius_map(2);
y5 *= &y0;
y5 *= &y4;
y5 *= &y1;
Some(y5)
}
None => None,
}
}
}