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