ark-r1cs-std/r1cs-std/src/pairing/mnt4/mod.rs
r1cs-std/src/pairing/mnt4/mod.rsRaw
1use r1cs_core::SynthesisError;
2
3use super::PairingVar as PG;
4
5use crate::{
6 fields::{fp::FpVar, fp2::Fp2Var, fp4::Fp4Var, FieldVar},
7 groups::mnt4::{
8 AteAdditionCoefficientsVar, AteDoubleCoefficientsVar, G1PreparedVar, G1Var, G2PreparedVar,
9 G2ProjectiveExtendedVar, G2Var,
10 },
11};
12use algebra::{
13 curves::mnt4::{MNT4Parameters, MNT4},
14 fields::BitIteratorBE,
15};
16use core::marker::PhantomData;
17
18pub struct PairingVar<P: MNT4Parameters>(PhantomData<P>);
19
20type Fp2G<P> = Fp2Var<<P as MNT4Parameters>::Fp2Params>;
21type Fp4G<P> = Fp4Var<<P as MNT4Parameters>::Fp4Params>;
22pub type GTVar<P> = Fp4G<P>;
23
24impl<P: MNT4Parameters> PairingVar<P> {
25 #[tracing::instrument(target = "r1cs", skip(r))]
26 pub(crate) fn doubling_step_for_flipped_miller_loop(
27 r: &G2ProjectiveExtendedVar<P>,
28 ) -> Result<(G2ProjectiveExtendedVar<P>, AteDoubleCoefficientsVar<P>), SynthesisError> {
29 let a = r.t.square()?;
30 let b = r.x.square()?;
31 let c = r.y.square()?;
32 let d = c.square()?;
33 let e = (&r.x + &c).square()? - &b - &d;
34 let f = (b.double()? + &b) + &a * P::TWIST_COEFF_A;
35 let g = f.square()?;
36
37 let d_eight = d.double()?.double()?.double()?;
38
39 let e2 = e.double()?;
40 let x = &g - &e2.double()?;
41
42 let y = &f * (&e2 - &x) - &d_eight;
43 let z = (&r.y + &r.z).square()? - &c - &r.z.square()?;
44 let t = z.square()?;
45
46 let r2 = G2ProjectiveExtendedVar { x, y, z, t };
47 let c_h = (&r2.z + &r.t).square()? - &r2.t - &a;
48 let c_4c = c.double()?.double()?;
49 let c_j = (&f + &r.t).square()? - &g - &a;
50 let c_l = (&f + &r.x).square()? - &g - &b;
51 let coeff = AteDoubleCoefficientsVar {
52 c_h,
53 c_4c,
54 c_j,
55 c_l,
56 };
57
58 Ok((r2, coeff))
59 }
60
61 #[tracing::instrument(target = "r1cs", skip(r))]
62 pub(crate) fn mixed_addition_step_for_flipped_miller_loop(
63 x: &Fp2G<P>,
64 y: &Fp2G<P>,
65 r: &G2ProjectiveExtendedVar<P>,
66 ) -> Result<(G2ProjectiveExtendedVar<P>, AteAdditionCoefficientsVar<P>), SynthesisError> {
67 let a = y.square()?;
68 let b = &r.t * x;
69 let d = ((&r.z + y).square()? - &a - &r.t) * &r.t;
70 let h = &b - &r.x;
71 let i = h.square()?;
72 let e = i.double()?.double()?;
73 let j = &h * &e;
74 let v = &r.x * &e;
75 let ry2 = r.y.double()?;
76 let l1 = &d - &ry2;
77
78 let x = l1.square()? - &j - &v.double()?;
79 let y = &l1 * &(&v - &x) - j * &ry2;
80 let z = (&r.z + &h).square()? - &r.t - &i;
81 let t = z.square()?;
82
83 let r2 = G2ProjectiveExtendedVar {
84 x,
85 y,
86 z: z.clone(),
87 t,
88 };
89 let coeff = AteAdditionCoefficientsVar { c_l1: l1, c_rz: z };
90
91 Ok((r2, coeff))
92 }
93
94 #[tracing::instrument(target = "r1cs", skip(p, q))]
95 pub fn ate_miller_loop(
96 p: &G1PreparedVar<P>,
97 q: &G2PreparedVar<P>,
98 ) -> Result<Fp4G<P>, SynthesisError> {
99 let l1_coeff = Fp2G::<P>::new(p.x.clone(), FpVar::<P::Fp>::zero()) - &q.x_over_twist;
100
101 let mut f = Fp4G::<P>::one();
102
103 let mut dbl_idx: usize = 0;
104 let mut add_idx: usize = 0;
105
106 // code below gets executed for all bits (EXCEPT the MSB itself) of
107 // mnt6_param_p (skipping leading zeros) in MSB to LSB order
108 for bit in BitIteratorBE::without_leading_zeros(P::ATE_LOOP_COUNT).skip(1) {
109 let dc = &q.double_coefficients[dbl_idx];
110 dbl_idx += 1;
111
112 let g_rr_at_p = Fp4G::<P>::new(
113 &dc.c_l - &dc.c_4c - &dc.c_j * &p.x_twist,
114 &dc.c_h * &p.y_twist,
115 );
116
117 f = f.square()? * &g_rr_at_p;
118
119 if bit {
120 let ac = &q.addition_coefficients[add_idx];
121 add_idx += 1;
122
123 let g_rq_at_p = Fp4G::<P>::new(
124 &ac.c_rz * &p.y_twist,
125 (&q.y_over_twist * &ac.c_rz + &l1_coeff * &ac.c_l1).negate()?,
126 );
127 f *= &g_rq_at_p;
128 }
129 }
130
131 if P::ATE_IS_LOOP_COUNT_NEG {
132 let ac = &q.addition_coefficients[add_idx];
133
134 let g_rnegr_at_p = Fp4G::<P>::new(
135 &ac.c_rz * &p.y_twist,
136 (&q.y_over_twist * &ac.c_rz + &l1_coeff * &ac.c_l1).negate()?,
137 );
138 f = (&f * &g_rnegr_at_p).inverse()?;
139 }
140
141 Ok(f)
142 }
143
144 #[tracing::instrument(target = "r1cs", skip(value))]
145 pub fn final_exponentiation(value: &Fp4G<P>) -> Result<GTVar<P>, SynthesisError> {
146 let value_inv = value.inverse()?;
147 let value_to_first_chunk = Self::final_exponentiation_first_chunk(value, &value_inv)?;
148 let value_inv_to_first_chunk = Self::final_exponentiation_first_chunk(&value_inv, value)?;
149 Self::final_exponentiation_last_chunk(&value_to_first_chunk, &value_inv_to_first_chunk)
150 }
151
152 #[tracing::instrument(target = "r1cs", skip(elt, elt_inv))]
153 fn final_exponentiation_first_chunk(
154 elt: &Fp4G<P>,
155 elt_inv: &Fp4G<P>,
156 ) -> Result<Fp4G<P>, SynthesisError> {
157 // (q^2-1)
158
159 // elt_q2 = elt^(q^2)
160 let elt_q2 = elt.unitary_inverse()?;
161 // elt_q2_over_elt = elt^(q^2-1)
162 Ok(elt_q2 * elt_inv)
163 }
164
165 #[tracing::instrument(target = "r1cs", skip(elt, elt_inv))]
166 fn final_exponentiation_last_chunk(
167 elt: &Fp4G<P>,
168 elt_inv: &Fp4G<P>,
169 ) -> Result<Fp4G<P>, SynthesisError> {
170 let elt_clone = elt.clone();
171 let elt_inv_clone = elt_inv.clone();
172
173 let mut elt_q = elt.clone();
174 elt_q.frobenius_map_in_place(1)?;
175
176 let w1_part = elt_q.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_1)?;
177 let w0_part = if P::FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG {
178 elt_inv_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0)?
179 } else {
180 elt_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0)?
181 };
182
183 Ok(w1_part * &w0_part)
184 }
185}
186
187impl<P: MNT4Parameters> PG<MNT4<P>, P::Fp> for PairingVar<P> {
188 type G1Var = G1Var<P>;
189 type G2Var = G2Var<P>;
190 type G1PreparedVar = G1PreparedVar<P>;
191 type G2PreparedVar = G2PreparedVar<P>;
192 type GTVar = GTVar<P>;
193
194 #[tracing::instrument(target = "r1cs")]
195 fn miller_loop(
196 ps: &[Self::G1PreparedVar],
197 qs: &[Self::G2PreparedVar],
198 ) -> Result<Self::GTVar, SynthesisError> {
199 let mut result = Fp4G::<P>::one();
200 for (p, q) in ps.iter().zip(qs) {
201 result *= Self::ate_miller_loop(p, q)?;
202 }
203
204 Ok(result)
205 }
206
207 #[tracing::instrument(target = "r1cs")]
208 fn final_exponentiation(r: &Self::GTVar) -> Result<Self::GTVar, SynthesisError> {
209 Self::final_exponentiation(r)
210 }
211
212 #[tracing::instrument(target = "r1cs")]
213 fn prepare_g1(p: &Self::G1Var) -> Result<Self::G1PreparedVar, SynthesisError> {
214 Self::G1PreparedVar::from_group_var(p)
215 }
216
217 #[tracing::instrument(target = "r1cs")]
218 fn prepare_g2(q: &Self::G2Var) -> Result<Self::G2PreparedVar, SynthesisError> {
219 Self::G2PreparedVar::from_group_var(q)
220 }
221}