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::BitIteratorBE,
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 // code below gets executed for all bits (EXCEPT the MSB itself) of
100 // mnt6_param_p (skipping leading zeros) in MSB to LSB order
101 for bit in BitIteratorBE::without_leading_zeros(P::ATE_LOOP_COUNT).skip(1) {
102 let dc = &q.double_coefficients[dbl_idx];
103 dbl_idx += 1;
104
105 let g_rr_at_p = Fp6Var::new(
106 &dc.c_l - &dc.c_4c - &dc.c_j * &p.x_twist,
107 &dc.c_h * &p.y_twist,
108 );
109
110 f = f.square()? * &g_rr_at_p;
111
112 if bit {
113 let ac = &q.addition_coefficients[add_idx];
114 add_idx += 1;
115
116 let g_rq_at_p = Fp6Var::new(
117 &ac.c_rz * &p.y_twist,
118 (&q.y_over_twist * &ac.c_rz + &(&l1_coeff * &ac.c_l1)).negate()?,
119 );
120 f *= &g_rq_at_p;
121 }
122 }
123
124 if P::ATE_IS_LOOP_COUNT_NEG {
125 let ac = &q.addition_coefficients[add_idx];
126
127 let g_rnegr_at_p = Fp6Var::new(
128 &ac.c_rz * &p.y_twist,
129 (&q.y_over_twist * &ac.c_rz + &(l1_coeff * &ac.c_l1)).negate()?,
130 );
131 f = (f * &g_rnegr_at_p).inverse()?;
132 }
133
134 Ok(f)
135 }
136
137 pub fn final_exponentiation(value: &Fp6G<P>) -> Result<GTVar<P>, SynthesisError> {
138 let value_inv = value.inverse()?;
139 let value_to_first_chunk = Self::final_exponentiation_first_chunk(value, &value_inv)?;
140 let value_inv_to_first_chunk = Self::final_exponentiation_first_chunk(&value_inv, value)?;
141 Self::final_exponentiation_last_chunk(&value_to_first_chunk, &value_inv_to_first_chunk)
142 }
143
144 fn final_exponentiation_first_chunk(
145 elt: &Fp6G<P>,
146 elt_inv: &Fp6G<P>,
147 ) -> Result<Fp6G<P>, SynthesisError> {
148 // (q^3-1)*(q+1)
149
150 // elt_q3 = elt^(q^3)
151 let elt_q3 = elt.unitary_inverse()?;
152 // elt_q3_over_elt = elt^(q^3-1)
153 let elt_q3_over_elt = elt_q3 * elt_inv;
154 // alpha = elt^((q^3-1) * q)
155 let alpha = elt_q3_over_elt.frobenius_map(1)?;
156 // beta = elt^((q^3-1)*(q+1)
157 Ok(alpha * &elt_q3_over_elt)
158 }
159
160 fn final_exponentiation_last_chunk(
161 elt: &Fp6G<P>,
162 elt_inv: &Fp6G<P>,
163 ) -> Result<Fp6G<P>, SynthesisError> {
164 let elt_q = elt.frobenius_map(1)?;
165
166 let w1_part = elt_q.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_1)?;
167 let w0_part = if P::FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG {
168 elt_inv.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0)?
169 } else {
170 elt.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0)?
171 };
172
173 Ok(w1_part * &w0_part)
174 }
175}
176
177impl<P: MNT6Parameters> PG<MNT6<P>, P::Fp> for PairingVar<P> {
178 type G1Var = G1Var<P>;
179 type G2Var = G2Var<P>;
180 type G1PreparedVar = G1PreparedVar<P>;
181 type G2PreparedVar = G2PreparedVar<P>;
182 type GTVar = GTVar<P>;
183
184 fn miller_loop(
185 ps: &[Self::G1PreparedVar],
186 qs: &[Self::G2PreparedVar],
187 ) -> Result<Self::GTVar, SynthesisError> {
188 let mut result = Fp6G::<P>::one();
189 for (p, q) in ps.iter().zip(qs) {
190 result *= Self::ate_miller_loop(p, q)?;
191 }
192
193 Ok(result)
194 }
195
196 fn final_exponentiation(r: &Self::GTVar) -> Result<Self::GTVar, SynthesisError> {
197 Self::final_exponentiation(r)
198 }
199
200 fn prepare_g1(p: &Self::G1Var) -> Result<Self::G1PreparedVar, SynthesisError> {
201 Self::G1PreparedVar::from_group_var(p)
202 }
203
204 fn prepare_g2(q: &Self::G2Var) -> Result<Self::G2PreparedVar, SynthesisError> {
205 Self::G2PreparedVar::from_group_var(q)
206 }
207}