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::PairingEngine;
 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: PairingEngine, ConstraintF: Field = <E as PairingEngine>::Fq> {
17    /// An variable representing an element of `G1`.
18    /// This is the R1CS equivalent of `E::G1Projective`.
19    type G1Var: CurveVar<E::G1Projective, ConstraintF>
20        + AllocVar<E::G1Projective, ConstraintF>
21        + AllocVar<E::G1Affine, ConstraintF>;
22
23    /// An variable representing an element of `G2`.
24    /// This is the R1CS equivalent of `E::G2Projective`.
25    type G2Var: CurveVar<E::G2Projective, ConstraintF>
26        + AllocVar<E::G2Projective, ConstraintF>
27        + AllocVar<E::G2Affine, ConstraintF>;
28
29    /// An variable representing an element of `GT`.
30    /// This is the R1CS equivalent of `E::GT`.
31    type GTVar: FieldVar<E::Fqk, ConstraintF>;
32
33    /// An variable representing cached precomputation  that can speed up
34    /// pairings computations. This is the R1CS equivalent of
35    /// `E::G1Prepared`.
36    type G1PreparedVar: ToBytesGadget<ConstraintF>
37        + AllocVar<E::G1Prepared, ConstraintF>
38        + Clone
39        + Debug;
40    /// An variable representing cached precomputation  that can speed up
41    /// pairings computations. This is the R1CS equivalent of
42    /// `E::G2Prepared`.
43    type G2PreparedVar: ToBytesGadget<ConstraintF>
44        + AllocVar<E::G2Prepared, ConstraintF>
45        + Clone
46        + Debug;
47
48    /// Computes a multi-miller loop between elements
49    /// of `p` and `q`.
50    fn miller_loop(
51        p: &[Self::G1PreparedVar],
52        q: &[Self::G2PreparedVar],
53    ) -> Result<Self::GTVar, SynthesisError>;
54
55    /// Computes a final exponentiation over `p`.
56    fn final_exponentiation(p: &Self::GTVar) -> Result<Self::GTVar, SynthesisError>;
57
58    /// Computes a pairing over `p` and `q`.
59    #[tracing::instrument(target = "r1cs")]
60    fn pairing(
61        p: Self::G1PreparedVar,
62        q: Self::G2PreparedVar,
63    ) -> Result<Self::GTVar, SynthesisError> {
64        let tmp = Self::miller_loop(&[p], &[q])?;
65        Self::final_exponentiation(&tmp)
66    }
67
68    /// Computes a product of pairings over the elements in `p` and `q`.
69    #[must_use]
70    #[tracing::instrument(target = "r1cs")]
71    fn product_of_pairings(
72        p: &[Self::G1PreparedVar],
73        q: &[Self::G2PreparedVar],
74    ) -> Result<Self::GTVar, SynthesisError> {
75        let miller_result = Self::miller_loop(p, q)?;
76        Self::final_exponentiation(&miller_result)
77    }
78
79    /// Performs the precomputation to generate `Self::G1PreparedVar`.
80    fn prepare_g1(q: &Self::G1Var) -> Result<Self::G1PreparedVar, SynthesisError>;
81
82    /// Performs the precomputation to generate `Self::G2PreparedVar`.
83    fn prepare_g2(q: &Self::G2Var) -> Result<Self::G2PreparedVar, SynthesisError>;
84}