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
use crate::templates::{
bls12::{Bls12Parameters, TwistType},
short_weierstrass::short_weierstrass_jacobian::{GroupAffine, GroupProjective},
};
use snarkvm_errors::serialization::SerializationError;
use snarkvm_models::curves::{AffineCurve, Field, Fp2, One, SWModelParameters, Zero};
use snarkvm_utilities::{bititerator::BitIteratorBE, bytes::ToBytes, serialize::*};
use std::io::{Result as IoResult, Write};
pub type G2Affine<P> = GroupAffine<<P as Bls12Parameters>::G2Parameters>;
pub type G2Projective<P> = GroupProjective<<P as Bls12Parameters>::G2Parameters>;
type CoeffTriplet<T> = (Fp2<T>, Fp2<T>, Fp2<T>);
#[derive(Derivative, CanonicalSerialize, CanonicalDeserialize)]
#[derivative(
Clone(bound = "P: Bls12Parameters"),
Debug(bound = "P: Bls12Parameters"),
PartialEq(bound = "P: Bls12Parameters"),
Eq(bound = "P: Bls12Parameters")
)]
pub struct G2Prepared<P: Bls12Parameters> {
pub ell_coeffs: Vec<CoeffTriplet<P::Fp2Params>>,
pub infinity: bool,
}
#[derive(Derivative)]
#[derivative(
Clone(bound = "P: Bls12Parameters"),
Copy(bound = "P: Bls12Parameters"),
Debug(bound = "P: Bls12Parameters")
)]
struct G2HomProjective<P: Bls12Parameters> {
x: Fp2<P::Fp2Params>,
y: Fp2<P::Fp2Params>,
z: Fp2<P::Fp2Params>,
}
impl<P: Bls12Parameters> Default for G2Prepared<P> {
fn default() -> Self {
Self::from_affine(G2Affine::<P>::prime_subgroup_generator())
}
}
impl<P: Bls12Parameters> ToBytes for G2Prepared<P> {
fn write<W: Write>(&self, mut writer: W) -> IoResult<()> {
for coeff in &self.ell_coeffs {
coeff.0.write(&mut writer)?;
coeff.1.write(&mut writer)?;
coeff.2.write(&mut writer)?;
}
self.infinity.write(writer)
}
}
impl<P: Bls12Parameters> G2Prepared<P> {
pub fn is_zero(&self) -> bool {
self.infinity
}
pub fn from_affine(q: G2Affine<P>) -> Self {
let two_inv = P::Fp::one().double().inverse().unwrap();
if q.is_zero() {
return Self {
ell_coeffs: vec![],
infinity: true,
};
}
let mut r = G2HomProjective {
x: q.x,
y: q.y,
z: Fp2::one(),
};
let bit_iterator = BitIteratorBE::new(P::X);
let mut ell_coeffs = Vec::with_capacity(bit_iterator.len());
for i in bit_iterator.skip(1) {
ell_coeffs.push(doubling_step::<P>(&mut r, &two_inv));
if i {
ell_coeffs.push(addition_step::<P>(&mut r, &q));
}
}
Self {
ell_coeffs,
infinity: false,
}
}
}
#[allow(clippy::many_single_char_names)]
fn doubling_step<B: Bls12Parameters>(r: &mut G2HomProjective<B>, two_inv: &B::Fp) -> CoeffTriplet<B::Fp2Params> {
let mut a = r.x * &r.y;
a.mul_by_fp(two_inv);
let b = r.y.square();
let c = r.z.square();
let e = B::G2Parameters::COEFF_B * &(c.double() + &c);
let f = e.double() + &e;
let mut g = b + &f;
g.mul_by_fp(two_inv);
let h = (r.y + &r.z).square() - &(b + &c);
let i = e - &b;
let j = r.x.square();
let e_square = e.square();
r.x = a * &(b - &f);
r.y = g.square() - &(e_square.double() + &e_square);
r.z = b * &h;
match B::TWIST_TYPE {
TwistType::M => (i, j.double() + &j, -h),
TwistType::D => (-h, j.double() + &j, i),
}
}
#[allow(clippy::many_single_char_names)]
fn addition_step<B: Bls12Parameters>(r: &mut G2HomProjective<B>, q: &G2Affine<B>) -> CoeffTriplet<B::Fp2Params> {
let theta = r.y - &(q.y * &r.z);
let lambda = r.x - &(q.x * &r.z);
let c = theta.square();
let d = lambda.square();
let e = lambda * &d;
let f = r.z * &c;
let g = r.x * &d;
let h = e + &f - &g.double();
r.x = lambda * &h;
r.y = theta * &(g - &h) - &(e * &r.y);
r.z *= &e;
let j = theta * &q.x - &(lambda * &q.y);
match B::TWIST_TYPE {
TwistType::M => (j, -theta, lambda),
TwistType::D => (lambda, -theta, j),
}
}