Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ToConstraintFieldGadget #278

Merged
merged 6 commits into from
Sep 15, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions r1cs-std/src/bits/boolean.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use algebra::{BitIteratorBE, Field};
use algebra::{BitIteratorBE, Field, PrimeField};

use crate::{prelude::*, Assignment, Vec};
use crate::{fields::fp::FpVar, prelude::*, Assignment, ToConstraintFieldGadget, Vec};
use core::borrow::Borrow;
use r1cs_core::{lc, ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable};

Expand Down Expand Up @@ -597,6 +597,14 @@ impl<F: Field> ToBytesGadget<F> for Boolean<F> {
}
}

impl<F: PrimeField> ToConstraintFieldGadget<F> for Boolean<F> {
#[tracing::instrument(target = "r1cs")]
fn to_constraint_field(&self) -> Result<Vec<FpVar<F>>, SynthesisError> {
let var = From::from(self.clone());
Ok(vec![var])
}
}

impl<F: Field> CondSelectGadget<F> for Boolean<F> {
#[tracing::instrument(target = "r1cs")]
fn conditionally_select(
Expand Down
22 changes: 21 additions & 1 deletion r1cs-std/src/fields/cubic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use algebra::{
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};

use crate::fields::fp::FpVar;
use crate::{
fields::{FieldOpsBounds, FieldVar},
prelude::*,
Assignment, Vec,
Assignment, ToConstraintFieldGadget, Vec,
};

#[derive(Derivative)]
Expand Down Expand Up @@ -440,6 +441,25 @@ where
}
}

impl<BF, P> ToConstraintFieldGadget<P::BasePrimeField> for CubicExtVar<BF, P>
where
BF: FieldVar<P::BaseField, P::BasePrimeField>,
for<'a> &'a BF: FieldOpsBounds<'a, P::BaseField, BF>,
P: CubicExtVarParams<BF>,
BF: ToConstraintFieldGadget<P::BasePrimeField>,
{
#[tracing::instrument(target = "r1cs")]
fn to_constraint_field(&self) -> Result<Vec<FpVar<P::BasePrimeField>>, SynthesisError> {
let mut res = Vec::new();

res.extend_from_slice(&self.c0.to_constraint_field()?);
res.extend_from_slice(&self.c1.to_constraint_field()?);
res.extend_from_slice(&self.c2.to_constraint_field()?);

Ok(res)
}
}

impl<BF, P> CondSelectGadget<P::BasePrimeField> for CubicExtVar<BF, P>
where
BF: FieldVar<P::BaseField, P::BasePrimeField>,
Expand Down
16 changes: 15 additions & 1 deletion r1cs-std/src/fields/fp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use r1cs_core::{lc, ConstraintSystemRef, LinearCombination, Namespace, Synthesis
use core::borrow::Borrow;

use crate::fields::{FieldOpsBounds, FieldVar};
use crate::{prelude::*, Assignment, Vec};
use crate::{prelude::*, Assignment, ToConstraintFieldGadget, Vec};

pub mod cmp;

Expand Down Expand Up @@ -432,6 +432,13 @@ impl<F: PrimeField> ToBytesGadget<F> for AllocatedFp<F> {
}
}

impl<F: PrimeField> ToConstraintFieldGadget<F> for AllocatedFp<F> {
#[tracing::instrument(target = "r1cs")]
fn to_constraint_field(&self) -> Result<Vec<FpVar<F>>, SynthesisError> {
Ok(vec![self.clone().into()])
}
}

impl<F: PrimeField> CondSelectGadget<F> for AllocatedFp<F> {
#[inline]
#[tracing::instrument(target = "r1cs")]
Expand Down Expand Up @@ -845,6 +852,13 @@ impl<F: PrimeField> ToBytesGadget<F> for FpVar<F> {
}
}

impl<F: PrimeField> ToConstraintFieldGadget<F> for FpVar<F> {
#[tracing::instrument(target = "r1cs")]
fn to_constraint_field(&self) -> Result<Vec<FpVar<F>>, SynthesisError> {
Ok(vec![self.clone()])
}
}

impl<F: PrimeField> CondSelectGadget<F> for FpVar<F> {
#[tracing::instrument(target = "r1cs")]
fn conditionally_select(
Expand Down
21 changes: 20 additions & 1 deletion r1cs-std/src/fields/quadratic_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,11 @@ use algebra::{
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};

use crate::fields::fp::FpVar;
use crate::{
fields::{FieldOpsBounds, FieldVar},
prelude::*,
Assignment, Vec,
Assignment, ToConstraintFieldGadget, Vec,
};

#[derive(Derivative)]
Expand Down Expand Up @@ -430,6 +431,24 @@ where
}
}

impl<BF, P> ToConstraintFieldGadget<P::BasePrimeField> for QuadExtVar<BF, P>
where
BF: FieldVar<P::BaseField, P::BasePrimeField>,
for<'a> &'a BF: FieldOpsBounds<'a, P::BaseField, BF>,
P: QuadExtVarParams<BF>,
BF: ToConstraintFieldGadget<P::BasePrimeField>,
{
#[tracing::instrument(target = "r1cs")]
fn to_constraint_field(&self) -> Result<Vec<FpVar<P::BasePrimeField>>, SynthesisError> {
let mut res = Vec::new();

res.extend_from_slice(&self.c0.to_constraint_field()?);
res.extend_from_slice(&self.c1.to_constraint_field()?);

Ok(res)
}
}

impl<BF, P> CondSelectGadget<P::BasePrimeField> for QuadExtVar<BF, P>
where
BF: FieldVar<P::BaseField, P::BasePrimeField>,
Expand Down
22 changes: 21 additions & 1 deletion r1cs-std/src/groups/curves/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ use algebra::{
use core::{borrow::Borrow, marker::PhantomData};
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};

use crate::{prelude::*, Vec};
use crate::fields::fp::FpVar;
use crate::{prelude::*, ToConstraintFieldGadget, Vec};

pub mod bls12;
pub mod mnt4;
Expand Down Expand Up @@ -80,6 +81,25 @@ where
}
}

impl<P, F> ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
where
P: SWModelParameters,
F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
F: ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField>,
{
fn to_constraint_field(
&self,
) -> Result<Vec<FpVar<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
let mut res = Vec::<FpVar<<P::BaseField as Field>::BasePrimeField>>::new();

res.extend_from_slice(&self.x.to_constraint_field()?);
res.extend_from_slice(&self.y.to_constraint_field()?);

Ok(res)
}
}

impl<P, F> R1CSVar<<P::BaseField as Field>::BasePrimeField> for ProjectiveVar<P, F>
where
P: SWModelParameters,
Expand Down
22 changes: 21 additions & 1 deletion r1cs-std/src/groups/curves/twisted_edwards/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,9 @@ use algebra::{

use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};

use crate::{prelude::*, Vec};
use crate::{prelude::*, ToConstraintFieldGadget, Vec};

use crate::fields::fp::FpVar;
use core::{borrow::Borrow, marker::PhantomData};

#[derive(Derivative)]
Expand Down Expand Up @@ -646,6 +647,25 @@ where
}
}

impl<P, F> ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField> for AffineVar<P, F>
where
P: TEModelParameters,
F: FieldVar<P::BaseField, <P::BaseField as Field>::BasePrimeField>,
for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>,
F: ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField>,
{
fn to_constraint_field(
&self,
) -> Result<Vec<FpVar<<P::BaseField as Field>::BasePrimeField>>, SynthesisError> {
let mut res = Vec::<FpVar<<P::BaseField as Field>::BasePrimeField>>::new();
weikengchen marked this conversation as resolved.
Show resolved Hide resolved

res.extend_from_slice(&self.x.to_constraint_field()?);
res.extend_from_slice(&self.y.to_constraint_field()?);

Ok(res)
}
}

#[inline]
fn div2(limbs: &mut [u64]) {
let mut t = 0;
Expand Down
7 changes: 7 additions & 0 deletions r1cs-std/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,3 +154,10 @@ impl<T> Assignment<T> for Option<T> {
self.ok_or(r1cs_core::SynthesisError::AssignmentMissing)
}
}

/// Obtains the field variables
pub trait ToConstraintFieldGadget<ConstraintF: algebra::PrimeField> {
fn to_constraint_field(
&self,
) -> Result<Vec<crate::fields::fp::FpVar<ConstraintF>>, r1cs_core::SynthesisError>;
}