diff --git a/src/pairing/bls12/mod.rs b/src/pairing/bls12/mod.rs index 33454c41..d9f22352 100644 --- a/src/pairing/bls12/mod.rs +++ b/src/pairing/bls12/mod.rs @@ -1,9 +1,9 @@ use ark_relations::r1cs::SynthesisError; -use super::PairingVar as PG; +use super::{PairingGadget as PG, PairingWithGadget}; use crate::{ - fields::{fp::FpVar, fp12::Fp12Var, fp2::Fp2Var, FieldVar}, + fields::{fp::FpVar, fp12::Fp12Var, fp2::Fp2Var, FieldVar, FieldWithVar}, groups::bls12::{G1AffineVar, G1PreparedVar, G1Var, G2PreparedVar, G2Var}, }; use ark_ec::bls12::{Bls12, Bls12Parameters, TwistType}; @@ -11,11 +11,14 @@ use ark_ff::fields::BitIteratorBE; use core::marker::PhantomData; /// Specifies the constraints for computing a pairing in a BLS12 bilinear group. -pub struct PairingVar(PhantomData

); +pub struct Bls12Gadget(PhantomData

); type Fp2V

= Fp2Var<

::Fp2Params>; -impl PairingVar

{ +impl Bls12Gadget

+where + P::Fp: FieldWithVar>, +{ // Evaluate the line function at point p. #[tracing::instrument(target = "r1cs")] fn ell( @@ -59,7 +62,17 @@ impl PairingVar

{ } } -impl PG, P::Fp> for PairingVar

{ +impl PairingWithGadget for Bls12

+where + P::Fp: FieldWithVar>, +{ + type Gadget = Bls12Gadget

; +} + +impl PG> for Bls12Gadget

+where + P::Fp: FieldWithVar>, +{ type G1Var = G1Var

; type G2Var = G2Var

; type G1PreparedVar = G1PreparedVar

; diff --git a/src/pairing/mnt4/mod.rs b/src/pairing/mnt4/mod.rs index 0ddbec59..f9024058 100644 --- a/src/pairing/mnt4/mod.rs +++ b/src/pairing/mnt4/mod.rs @@ -1,9 +1,9 @@ use ark_relations::r1cs::SynthesisError; -use super::PairingVar as PG; +use super::{PairingGadget as PG, PairingWithGadget}; use crate::{ - fields::{fp::FpVar, fp2::Fp2Var, fp4::Fp4Var, FieldVar}, + fields::{fp::FpVar, fp2::Fp2Var, fp4::Fp4Var, FieldVar, FieldWithVar}, groups::mnt4::{ AteAdditionCoefficientsVar, AteDoubleCoefficientsVar, G1PreparedVar, G1Var, G2PreparedVar, G2ProjectiveExtendedVar, G2Var, @@ -15,14 +15,17 @@ use ark_ff::BitIteratorBE; use core::marker::PhantomData; /// Specifies the constraints for computing a pairing in a MNT4 bilinear group. -pub struct PairingVar(PhantomData

); +pub struct MNT4Gadget(PhantomData

); type Fp2G

= Fp2Var<

::Fp2Params>; type Fp4G

= Fp4Var<

::Fp4Params>; /// A variable corresponding to `ark_ec::mnt4::GT`. pub type GTVar

= Fp4G

; -impl PairingVar

{ +impl MNT4Gadget

+where + P::Fp: FieldWithVar>, +{ #[tracing::instrument(target = "r1cs", skip(r))] pub(crate) fn doubling_step_for_flipped_miller_loop( r: &G2ProjectiveExtendedVar

, @@ -186,7 +189,17 @@ impl PairingVar

{ } } -impl PG, P::Fp> for PairingVar

{ +impl PairingWithGadget for MNT4

+where + P::Fp: FieldWithVar>, +{ + type Gadget = MNT4Gadget

; +} + +impl PG> for MNT4Gadget

+where + P::Fp: FieldWithVar>, +{ type G1Var = G1Var

; type G2Var = G2Var

; type G1PreparedVar = G1PreparedVar

; diff --git a/src/pairing/mnt6/mod.rs b/src/pairing/mnt6/mod.rs index bb849b29..4711d304 100644 --- a/src/pairing/mnt6/mod.rs +++ b/src/pairing/mnt6/mod.rs @@ -1,9 +1,9 @@ use ark_relations::r1cs::SynthesisError; -use super::PairingVar as PG; +use super::{PairingGadget as PG, PairingWithGadget}; use crate::{ - fields::{fp::FpVar, fp3::Fp3Var, fp6_2over3::Fp6Var, FieldVar}, + fields::{fp::FpVar, fp3::Fp3Var, fp6_2over3::Fp6Var, FieldVar, FieldWithVar}, groups::mnt6::{ AteAdditionCoefficientsVar, AteDoubleCoefficientsVar, G1PreparedVar, G1Var, G2PreparedVar, G2ProjectiveExtendedVar, G2Var, @@ -14,14 +14,17 @@ use ark_ff::fields::BitIteratorBE; use core::marker::PhantomData; /// Specifies the constraints for computing a pairing in a MNT6 bilinear group. -pub struct PairingVar(PhantomData

); +pub struct MNT6Gadget(PhantomData

); type Fp3G

= Fp3Var<

::Fp3Params>; type Fp6G

= Fp6Var<

::Fp6Params>; /// A variable corresponding to `ark_ec::mnt6::GT`. pub type GTVar

= Fp6G

; -impl PairingVar

{ +impl MNT6Gadget

+where + P::Fp: FieldWithVar>, +{ #[tracing::instrument(target = "r1cs", skip(r))] pub(crate) fn doubling_step_for_flipped_miller_loop( r: &G2ProjectiveExtendedVar

, @@ -181,7 +184,17 @@ impl PairingVar

{ } } -impl PG, P::Fp> for PairingVar

{ +impl PairingWithGadget for MNT6

+where + P::Fp: FieldWithVar>, +{ + type Gadget = MNT6Gadget

; +} + +impl PG> for MNT6Gadget

+where + P::Fp: FieldWithVar>, +{ type G1Var = G1Var

; type G2Var = G2Var

; type G1PreparedVar = G1PreparedVar

; diff --git a/src/pairing/mod.rs b/src/pairing/mod.rs index 157f26d8..7ac4d797 100644 --- a/src/pairing/mod.rs +++ b/src/pairing/mod.rs @@ -1,6 +1,5 @@ use crate::prelude::*; use ark_ec::PairingEngine; -use ark_ff::Field; use ark_relations::r1cs::SynthesisError; use core::fmt::Debug; @@ -11,39 +10,37 @@ pub mod mnt4; /// This module implements pairings for MNT6 bilinear groups. pub mod mnt6; +pub trait PairingWithGadget: PairingEngine { + type Gadget: PairingGadget; +} + /// Specifies the constraints for computing a pairing in the yybilinear group /// `E`. -pub trait PairingVar::Fq> { +pub trait PairingGadget { /// An variable representing an element of `G1`. /// This is the R1CS equivalent of `E::G1Projective`. - type G1Var: CurveVar - + AllocVar - + AllocVar; + type G1Var: CurveVar + + AllocVar + + AllocVar; /// An variable representing an element of `G2`. /// This is the R1CS equivalent of `E::G2Projective`. - type G2Var: CurveVar - + AllocVar - + AllocVar; + type G2Var: CurveVar + + AllocVar + + AllocVar; /// An variable representing an element of `GT`. /// This is the R1CS equivalent of `E::GT`. - type GTVar: FieldVar; + type GTVar: FieldVar; /// An variable representing cached precomputation that can speed up /// pairings computations. This is the R1CS equivalent of /// `E::G1Prepared`. - type G1PreparedVar: ToBytesGadget - + AllocVar - + Clone - + Debug; + type G1PreparedVar: ToBytesGadget + AllocVar + Clone + Debug; /// An variable representing cached precomputation that can speed up /// pairings computations. This is the R1CS equivalent of /// `E::G2Prepared`. - type G2PreparedVar: ToBytesGadget - + AllocVar - + Clone - + Debug; + type G2PreparedVar: ToBytesGadget + AllocVar + Clone + Debug; /// Computes a multi-miller loop between elements /// of `p` and `q`.