Skip to content

Commit

Permalink
Get pairings to compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Aug 11, 2021
1 parent 66e9073 commit d71692b
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 32 deletions.
23 changes: 18 additions & 5 deletions src/pairing/bls12/mod.rs
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
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};
use ark_ff::fields::BitIteratorBE;
use core::marker::PhantomData;

/// Specifies the constraints for computing a pairing in a BLS12 bilinear group.
pub struct PairingVar<P: Bls12Parameters>(PhantomData<P>);
pub struct Bls12Gadget<P: Bls12Parameters>(PhantomData<P>);

type Fp2V<P> = Fp2Var<<P as Bls12Parameters>::Fp2Params>;

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

impl<P: Bls12Parameters> PG<Bls12<P>, P::Fp> for PairingVar<P> {
impl<P: Bls12Parameters> PairingWithGadget for Bls12<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
type Gadget = Bls12Gadget<P>;
}

impl<P: Bls12Parameters> PG<Bls12<P>> for Bls12Gadget<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
type G1Var = G1Var<P>;
type G2Var = G2Var<P>;
type G1PreparedVar = G1PreparedVar<P>;
Expand Down
23 changes: 18 additions & 5 deletions src/pairing/mnt4/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<P: MNT4Parameters>(PhantomData<P>);
pub struct MNT4Gadget<P: MNT4Parameters>(PhantomData<P>);

type Fp2G<P> = Fp2Var<<P as MNT4Parameters>::Fp2Params>;
type Fp4G<P> = Fp4Var<<P as MNT4Parameters>::Fp4Params>;
/// A variable corresponding to `ark_ec::mnt4::GT`.
pub type GTVar<P> = Fp4G<P>;

impl<P: MNT4Parameters> PairingVar<P> {
impl<P: MNT4Parameters> MNT4Gadget<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
#[tracing::instrument(target = "r1cs", skip(r))]
pub(crate) fn doubling_step_for_flipped_miller_loop(
r: &G2ProjectiveExtendedVar<P>,
Expand Down Expand Up @@ -186,7 +189,17 @@ impl<P: MNT4Parameters> PairingVar<P> {
}
}

impl<P: MNT4Parameters> PG<MNT4<P>, P::Fp> for PairingVar<P> {
impl<P: MNT4Parameters> PairingWithGadget for MNT4<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
type Gadget = MNT4Gadget<P>;
}

impl<P: MNT4Parameters> PG<MNT4<P>> for MNT4Gadget<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
type G1Var = G1Var<P>;
type G2Var = G2Var<P>;
type G1PreparedVar = G1PreparedVar<P>;
Expand Down
23 changes: 18 additions & 5 deletions src/pairing/mnt6/mod.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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<P: MNT6Parameters>(PhantomData<P>);
pub struct MNT6Gadget<P: MNT6Parameters>(PhantomData<P>);

type Fp3G<P> = Fp3Var<<P as MNT6Parameters>::Fp3Params>;
type Fp6G<P> = Fp6Var<<P as MNT6Parameters>::Fp6Params>;
/// A variable corresponding to `ark_ec::mnt6::GT`.
pub type GTVar<P> = Fp6G<P>;

impl<P: MNT6Parameters> PairingVar<P> {
impl<P: MNT6Parameters> MNT6Gadget<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
#[tracing::instrument(target = "r1cs", skip(r))]
pub(crate) fn doubling_step_for_flipped_miller_loop(
r: &G2ProjectiveExtendedVar<P>,
Expand Down Expand Up @@ -181,7 +184,17 @@ impl<P: MNT6Parameters> PairingVar<P> {
}
}

impl<P: MNT6Parameters> PG<MNT6<P>, P::Fp> for PairingVar<P> {
impl<P: MNT6Parameters> PairingWithGadget for MNT6<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
type Gadget = MNT6Gadget<P>;
}

impl<P: MNT6Parameters> PG<MNT6<P>> for MNT6Gadget<P>
where
P::Fp: FieldWithVar<Var = FpVar<P::Fp>>,
{
type G1Var = G1Var<P>;
type G2Var = G2Var<P>;
type G1PreparedVar = G1PreparedVar<P>;
Expand Down
31 changes: 14 additions & 17 deletions src/pairing/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::prelude::*;
use ark_ec::PairingEngine;
use ark_ff::Field;
use ark_relations::r1cs::SynthesisError;
use core::fmt::Debug;

Expand All @@ -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<Self>;
}

/// Specifies the constraints for computing a pairing in the yybilinear group
/// `E`.
pub trait PairingVar<E: PairingEngine, ConstraintF: Field = <E as PairingEngine>::Fq> {
pub trait PairingGadget<E: PairingEngine> {
/// An variable representing an element of `G1`.
/// This is the R1CS equivalent of `E::G1Projective`.
type G1Var: CurveVar<E::G1Projective, ConstraintF>
+ AllocVar<E::G1Projective, ConstraintF>
+ AllocVar<E::G1Affine, ConstraintF>;
type G1Var: CurveVar<E::G1Projective, E::Fq>
+ AllocVar<E::G1Projective, E::Fq>
+ AllocVar<E::G1Affine, E::Fq>;

/// An variable representing an element of `G2`.
/// This is the R1CS equivalent of `E::G2Projective`.
type G2Var: CurveVar<E::G2Projective, ConstraintF>
+ AllocVar<E::G2Projective, ConstraintF>
+ AllocVar<E::G2Affine, ConstraintF>;
type G2Var: CurveVar<E::G2Projective, E::Fq>
+ AllocVar<E::G2Projective, E::Fq>
+ AllocVar<E::G2Affine, E::Fq>;

/// An variable representing an element of `GT`.
/// This is the R1CS equivalent of `E::GT`.
type GTVar: FieldVar<E::Fqk, ConstraintF>;
type GTVar: FieldVar<E::Fqk, E::Fq>;

/// An variable representing cached precomputation that can speed up
/// pairings computations. This is the R1CS equivalent of
/// `E::G1Prepared`.
type G1PreparedVar: ToBytesGadget<ConstraintF>
+ AllocVar<E::G1Prepared, ConstraintF>
+ Clone
+ Debug;
type G1PreparedVar: ToBytesGadget<E::Fq> + AllocVar<E::G1Prepared, E::Fq> + Clone + Debug;
/// An variable representing cached precomputation that can speed up
/// pairings computations. This is the R1CS equivalent of
/// `E::G2Prepared`.
type G2PreparedVar: ToBytesGadget<ConstraintF>
+ AllocVar<E::G2Prepared, ConstraintF>
+ Clone
+ Debug;
type G2PreparedVar: ToBytesGadget<E::Fq> + AllocVar<E::G2Prepared, E::Fq> + Clone + Debug;

/// Computes a multi-miller loop between elements
/// of `p` and `q`.
Expand Down

0 comments on commit d71692b

Please sign in to comment.