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