Skip to content

Commit

Permalink
Get groups to compile
Browse files Browse the repository at this point in the history
  • Loading branch information
Pratyush committed Aug 11, 2021
1 parent 195d74f commit 66e9073
Show file tree
Hide file tree
Showing 7 changed files with 472 additions and 292 deletions.
70 changes: 49 additions & 21 deletions src/groups/curves/short_weierstrass/bls12/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,44 @@ use ark_ff::{BitIteratorBE, Field, One};
use ark_relations::r1cs::{Namespace, SynthesisError};

use crate::{
fields::{fp::FpVar, fp2::Fp2Var, FieldVar},
fields::{fp2::Fp2Var, FieldVar},
groups::curves::short_weierstrass::*,
Vec,
};
use core::fmt::Debug;

type FpVar<P> = <<P as Bls12Parameters>::Fp as FieldWithVar>::Var;

/// Represents a projective point in G1.
pub type G1Var<P> =
ProjectiveVar<<P as Bls12Parameters>::G1Parameters, FpVar<<P as Bls12Parameters>::Fp>>;
pub type G1Var<P> = ProjectiveVar<<P as Bls12Parameters>::G1Parameters>;

/// Represents an affine point on G1. Should be used only for comparison and
/// when a canonical representation of a point is required, and not for
/// arithmetic.
pub type G1AffineVar<P> =
AffineVar<<P as Bls12Parameters>::G1Parameters, FpVar<<P as Bls12Parameters>::Fp>>;
pub type G1AffineVar<P> = AffineVar<<P as Bls12Parameters>::G1Parameters>;

/// Represents a projective point in G2.
pub type G2Var<P> = ProjectiveVar<<P as Bls12Parameters>::G2Parameters, Fp2G<P>>;
pub type G2Var<P> = ProjectiveVar<<P as Bls12Parameters>::G2Parameters>;
/// Represents an affine point on G2. Should be used only for comparison and
/// when a canonical representation of a point is required, and not for
/// arithmetic.
pub type G2AffineVar<P> = AffineVar<<P as Bls12Parameters>::G2Parameters, Fp2G<P>>;
pub type G2AffineVar<P> = AffineVar<<P as Bls12Parameters>::G2Parameters>;

/// Represents the cached precomputation that can be performed on a G1 element
/// which enables speeding up pairing computation.
#[derive(Derivative)]
#[derivative(Clone(bound = "G1Var<P>: Clone"), Debug(bound = "G1Var<P>: Debug"))]
pub struct G1PreparedVar<P: Bls12Parameters>(pub AffineVar<P::G1Parameters, FpVar<P::Fp>>);
#[derivative(
Clone(bound = "P: Bls12Parameters, P::Fp: FieldWithVar"),
Debug(bound = "P: Bls12Parameters, P::Fp: FieldWithVar")
)]
pub struct G1PreparedVar<P: Bls12Parameters>(pub G1AffineVar<P>)
where
P::Fp: FieldWithVar;

impl<P: Bls12Parameters> G1PreparedVar<P> {
impl<P: Bls12Parameters> G1PreparedVar<P>
where
P::Fp: FieldWithVar,
{
/// Returns the value assigned to `self` in the underlying constraint
/// system.
pub fn value(&self) -> Result<G1Prepared<P>, SynthesisError> {
Expand All @@ -53,7 +61,10 @@ impl<P: Bls12Parameters> G1PreparedVar<P> {
}
}

impl<P: Bls12Parameters> AllocVar<G1Prepared<P>, P::Fp> for G1PreparedVar<P> {
impl<P: Bls12Parameters> AllocVar<G1Prepared<P>, P::Fp> for G1PreparedVar<P>
where
P::Fp: FieldWithVar,
{
fn new_variable<T: Borrow<G1Prepared<P>>>(
cs: impl Into<Namespace<P::Fp>>,
f: impl FnOnce() -> Result<T, SynthesisError>,
Expand All @@ -63,19 +74,24 @@ impl<P: Bls12Parameters> AllocVar<G1Prepared<P>, P::Fp> for G1PreparedVar<P> {
let cs = ns.cs();
let g1_prep = f().map(|b| b.borrow().0);

let x = FpVar::new_variable(ark_relations::ns!(cs, "x"), || g1_prep.map(|g| g.x), mode)?;
let y = FpVar::new_variable(ark_relations::ns!(cs, "y"), || g1_prep.map(|g| g.y), mode)?;
let x =
FpVar::<P>::new_variable(ark_relations::ns!(cs, "x"), || g1_prep.map(|g| g.x), mode)?;
let y =
FpVar::<P>::new_variable(ark_relations::ns!(cs, "y"), || g1_prep.map(|g| g.y), mode)?;
let infinity = Boolean::new_variable(
ark_relations::ns!(cs, "inf"),
|| g1_prep.map(|g| g.infinity),
mode,
)?;
let g = AffineVar::new(x, y, infinity);
let g = G1AffineVar::<P>::new(x, y, infinity);
Ok(Self(g))
}
}

impl<P: Bls12Parameters> ToBytesGadget<P::Fp> for G1PreparedVar<P> {
impl<P: Bls12Parameters> ToBytesGadget<P::Fp> for G1PreparedVar<P>
where
P::Fp: FieldWithVar,
{
#[inline]
#[tracing::instrument(target = "r1cs")]
fn to_bytes(&self) -> Result<Vec<UInt8<P::Fp>>, SynthesisError> {
Expand Down Expand Up @@ -104,15 +120,21 @@ type LCoeff<P> = (Fp2G<P>, Fp2G<P>);
/// which enables speeding up pairing computation.
#[derive(Derivative)]
#[derivative(
Clone(bound = "Fp2Var<P::Fp2Params>: Clone"),
Debug(bound = "Fp2Var<P::Fp2Params>: Debug")
Clone(bound = "P::Fp: FieldWithVar"),
Debug(bound = "P::Fp: FieldWithVar")
)]
pub struct G2PreparedVar<P: Bls12Parameters> {
pub struct G2PreparedVar<P: Bls12Parameters>
where
P::Fp: FieldWithVar,
{
#[doc(hidden)]
pub ell_coeffs: Vec<LCoeff<P>>,
}

impl<P: Bls12Parameters> AllocVar<G2Prepared<P>, P::Fp> for G2PreparedVar<P> {
impl<P: Bls12Parameters> AllocVar<G2Prepared<P>, P::Fp> for G2PreparedVar<P>
where
P::Fp: FieldWithVar,
{
#[tracing::instrument(target = "r1cs", skip(cs, f, mode))]
fn new_variable<T: Borrow<G2Prepared<P>>>(
cs: impl Into<Namespace<P::Fp>>,
Expand Down Expand Up @@ -170,7 +192,10 @@ impl<P: Bls12Parameters> AllocVar<G2Prepared<P>, P::Fp> for G2PreparedVar<P> {
}
}

impl<P: Bls12Parameters> ToBytesGadget<P::Fp> for G2PreparedVar<P> {
impl<P: Bls12Parameters> ToBytesGadget<P::Fp> for G2PreparedVar<P>
where
P::Fp: FieldWithVar,
{
#[inline]
#[tracing::instrument(target = "r1cs")]
fn to_bytes(&self) -> Result<Vec<UInt8<P::Fp>>, SynthesisError> {
Expand All @@ -193,7 +218,10 @@ impl<P: Bls12Parameters> ToBytesGadget<P::Fp> for G2PreparedVar<P> {
}
}

impl<P: Bls12Parameters> G2PreparedVar<P> {
impl<P: Bls12Parameters> G2PreparedVar<P>
where
P::Fp: FieldWithVar,
{
/// Constructs `Self` from a `G2Var`.
#[tracing::instrument(target = "r1cs")]
pub fn from_group_var(q: &G2Var<P>) -> Result<Self, SynthesisError> {
Expand Down
Loading

0 comments on commit 66e9073

Please sign in to comment.