arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
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    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
 41        let y = &f * (&e2 - &x) - &d_eight;
 42        let z = (&r.y + &r.z).square()? - &c - &r.z.square()?;
 43        let t = z.square()?;
 44
 45        let r2 = G2ProjectiveExtendedVar { x, y, z, t };
 46        let c_h = (&r2.z + &r.t).square()? - &r2.t - &a;
 47        let c_4c = c.double()?.double()?;
 48        let c_j = (&f + &r.t).square()? - &g - &a;
 49        let c_l = (&f + &r.x).square()? - &g - &b;
 50        let coeff = AteDoubleCoefficientsVar {
 51            c_h,
 52            c_4c,
 53            c_j,
 54            c_l,
 55        };
 56
 57        Ok((r2, coeff))
 58    }
 59
 60    pub(crate) fn mixed_addition_step_for_flipped_miller_loop(
 61        x: &Fp2G<P>,
 62        y: &Fp2G<P>,
 63        r: &G2ProjectiveExtendedVar<P>,
 64    ) -> Result<(G2ProjectiveExtendedVar<P>, AteAdditionCoefficientsVar<P>), SynthesisError> {
 65        let a = y.square()?;
 66        let b = &r.t * x;
 67        let d = ((&r.z + y).square()? - &a - &r.t) * &r.t;
 68        let h = &b - &r.x;
 69        let i = h.square()?;
 70        let e = i.double()?.double()?;
 71        let j = &h * &e;
 72        let v = &r.x * &e;
 73        let ry2 = r.y.double()?;
 74        let l1 = &d - &ry2;
 75
 76        let x = l1.square()? - &j - &v.double()?;
 77        let y = &l1 * &(&v - &x) - j * &ry2;
 78        let z = (&r.z + &h).square()? - &r.t - &i;
 79        let t = z.square()?;
 80
 81        let r2 = G2ProjectiveExtendedVar {
 82            x,
 83            y,
 84            z: z.clone(),
 85            t,
 86        };
 87        let coeff = AteAdditionCoefficientsVar { c_l1: l1, c_rz: z };
 88
 89        Ok((r2, coeff))
 90    }
 91
 92    pub fn ate_miller_loop(
 93        p: &G1PreparedVar<P>,
 94        q: &G2PreparedVar<P>,
 95    ) -> Result<Fp4G<P>, SynthesisError> {
 96        let l1_coeff = Fp2G::<P>::new(p.x.clone(), FpVar::<P::Fp>::zero()) - &q.x_over_twist;
 97
 98        let mut f = Fp4G::<P>::one();
 99
100        let mut dbl_idx: usize = 0;
101        let mut add_idx: usize = 0;
102
103        // code below gets executed for all bits (EXCEPT the MSB itself) of
104        // mnt6_param_p (skipping leading zeros) in MSB to LSB order
105        for bit in BitIteratorBE::without_leading_zeros(P::ATE_LOOP_COUNT).skip(1) {
106            let dc = &q.double_coefficients[dbl_idx];
107            dbl_idx += 1;
108
109            let g_rr_at_p = Fp4G::<P>::new(
110                &dc.c_l - &dc.c_4c - &dc.c_j * &p.x_twist,
111                &dc.c_h * &p.y_twist,
112            );
113
114            f = f.square()? * &g_rr_at_p;
115
116            if bit {
117                let ac = &q.addition_coefficients[add_idx];
118                add_idx += 1;
119
120                let g_rq_at_p = Fp4G::<P>::new(
121                    &ac.c_rz * &p.y_twist,
122                    (&q.y_over_twist * &ac.c_rz + &l1_coeff * &ac.c_l1).negate()?,
123                );
124                f *= &g_rq_at_p;
125            }
126        }
127
128        if P::ATE_IS_LOOP_COUNT_NEG {
129            let ac = &q.addition_coefficients[add_idx];
130
131            let g_rnegr_at_p = Fp4G::<P>::new(
132                &ac.c_rz * &p.y_twist,
133                (&q.y_over_twist * &ac.c_rz + &l1_coeff * &ac.c_l1).negate()?,
134            );
135            f = (&f * &g_rnegr_at_p).inverse()?;
136        }
137
138        Ok(f)
139    }
140
141    pub fn final_exponentiation(value: &Fp4G<P>) -> Result<GTVar<P>, SynthesisError> {
142        let value_inv = value.inverse()?;
143        let value_to_first_chunk = Self::final_exponentiation_first_chunk(value, &value_inv)?;
144        let value_inv_to_first_chunk = Self::final_exponentiation_first_chunk(&value_inv, value)?;
145        Self::final_exponentiation_last_chunk(&value_to_first_chunk, &value_inv_to_first_chunk)
146    }
147
148    fn final_exponentiation_first_chunk(
149        elt: &Fp4G<P>,
150        elt_inv: &Fp4G<P>,
151    ) -> Result<Fp4G<P>, SynthesisError> {
152        // (q^2-1)
153
154        // elt_q2 = elt^(q^2)
155        let elt_q2 = elt.unitary_inverse()?;
156        // elt_q2_over_elt = elt^(q^2-1)
157        Ok(elt_q2 * elt_inv)
158    }
159
160    fn final_exponentiation_last_chunk(
161        elt: &Fp4G<P>,
162        elt_inv: &Fp4G<P>,
163    ) -> Result<Fp4G<P>, SynthesisError> {
164        let elt_clone = elt.clone();
165        let elt_inv_clone = elt_inv.clone();
166
167        let mut elt_q = elt.clone();
168        elt_q.frobenius_map_in_place(1)?;
169
170        let w1_part = elt_q.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_1)?;
171        let w0_part = if P::FINAL_EXPONENT_LAST_CHUNK_W0_IS_NEG {
172            elt_inv_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0)?
173        } else {
174            elt_clone.cyclotomic_exp(&P::FINAL_EXPONENT_LAST_CHUNK_ABS_OF_W0)?
175        };
176
177        Ok(w1_part * &w0_part)
178    }
179}
180
181impl<P: MNT4Parameters> PG<MNT4<P>, P::Fp> for PairingVar<P> {
182    type G1Var = G1Var<P>;
183    type G2Var = G2Var<P>;
184    type G1PreparedVar = G1PreparedVar<P>;
185    type G2PreparedVar = G2PreparedVar<P>;
186    type GTVar = GTVar<P>;
187
188    fn miller_loop(
189        ps: &[Self::G1PreparedVar],
190        qs: &[Self::G2PreparedVar],
191    ) -> Result<Self::GTVar, SynthesisError> {
192        let mut result = Fp4G::<P>::one();
193        for (p, q) in ps.iter().zip(qs) {
194            result *= Self::ate_miller_loop(p, q)?;
195        }
196
197        Ok(result)
198    }
199
200    fn final_exponentiation(r: &Self::GTVar) -> Result<Self::GTVar, SynthesisError> {
201        Self::final_exponentiation(r)
202    }
203
204    fn prepare_g1(p: &Self::G1Var) -> Result<Self::G1PreparedVar, SynthesisError> {
205        Self::G1PreparedVar::from_group_var(p)
206    }
207
208    fn prepare_g2(q: &Self::G2Var) -> Result<Self::G2PreparedVar, SynthesisError> {
209        Self::G2PreparedVar::from_group_var(q)
210    }
211}