arnaucube
arnaucube/ark-r1cs-stdPublic Self-hosted repository
ark-r1cs-std/src/pairing/mod.rs
src/pairing/mod.rsRaw
 1use crate::prelude::*;
 2use ark_ec::{pairing::Pairing, CurveGroup};
 3use ark_ff::Field;
 4use ark_relations::r1cs::SynthesisError;
 5use core::fmt::Debug;
 6
 7/// This module implements pairings for BLS12 bilinear groups.
 8pub mod bls12;
 9/// This module implements pairings for MNT4 bilinear groups.
10pub mod mnt4;
11/// This module implements pairings for MNT6 bilinear groups.
12pub mod mnt6;
13
14/// Specifies the constraints for computing a pairing in the yybilinear group
15/// `E`.
16pub trait PairingVar<E: Pairing, ConstraintF: Field = <<E as Pairing>::G1 as CurveGroup>::BaseField>
17{
18    /// An variable representing an element of `G1`.
19    /// This is the R1CS equivalent of `E::G1Projective`.
20    type G1Var: CurveVar<E::G1, ConstraintF>
21        + AllocVar<E::G1, ConstraintF>
22        + AllocVar<E::G1Affine, ConstraintF>;
23
24    /// An variable representing an element of `G2`.
25    /// This is the R1CS equivalent of `E::G2Projective`.
26    type G2Var: CurveVar<E::G2, ConstraintF>
27        + AllocVar<E::G2, ConstraintF>
28        + AllocVar<E::G2Affine, ConstraintF>;
29
30    /// An variable representing an element of `GT`.
31    /// This is the R1CS equivalent of `E::GT`.
32    type GTVar: FieldVar<E::TargetField, ConstraintF>;
33
34    /// An variable representing cached precomputation  that can speed up
35    /// pairings computations. This is the R1CS equivalent of
36    /// `E::G1Prepared`.
37    type G1PreparedVar: ToBytesGadget<ConstraintF>
38        + AllocVar<E::G1Prepared, ConstraintF>
39        + Clone
40        + Debug;
41    /// An variable representing cached precomputation  that can speed up
42    /// pairings computations. This is the R1CS equivalent of
43    /// `E::G2Prepared`.
44    type G2PreparedVar: ToBytesGadget<ConstraintF>
45        + AllocVar<E::G2Prepared, ConstraintF>
46        + Clone
47        + Debug;
48
49    /// Computes a multi-miller loop between elements
50    /// of `p` and `q`.
51    fn miller_loop(
52        p: &[Self::G1PreparedVar],
53        q: &[Self::G2PreparedVar],
54    ) -> Result<Self::GTVar, SynthesisError>;
55
56    /// Computes a final exponentiation over `p`.
57    fn final_exponentiation(p: &Self::GTVar) -> Result<Self::GTVar, SynthesisError>;
58
59    /// Computes a pairing over `p` and `q`.
60    #[tracing::instrument(target = "r1cs")]
61    fn pairing(
62        p: Self::G1PreparedVar,
63        q: Self::G2PreparedVar,
64    ) -> Result<Self::GTVar, SynthesisError> {
65        let tmp = Self::miller_loop(&[p], &[q])?;
66        Self::final_exponentiation(&tmp)
67    }
68
69    /// Computes a product of pairings over the elements in `p` and `q`.
70    #[must_use]
71    #[tracing::instrument(target = "r1cs")]
72    fn product_of_pairings(
73        p: &[Self::G1PreparedVar],
74        q: &[Self::G2PreparedVar],
75    ) -> Result<Self::GTVar, SynthesisError> {
76        let miller_result = Self::miller_loop(p, q)?;
77        Self::final_exponentiation(&miller_result)
78    }
79
80    /// Performs the precomputation to generate `Self::G1PreparedVar`.
81    fn prepare_g1(q: &Self::G1Var) -> Result<Self::G1PreparedVar, SynthesisError>;
82
83    /// Performs the precomputation to generate `Self::G2PreparedVar`.
84    fn prepare_g2(q: &Self::G2Var) -> Result<Self::G2PreparedVar, SynthesisError>;
85}