Browse Source

Add `ToConstraintFieldGadget` (#278)

master
Weikeng Chen 4 years ago
committed by GitHub
parent
commit
c21d0b2796
7 changed files with 115 additions and 7 deletions
  1. +10
    -2
      r1cs-std/src/bits/boolean.rs
  2. +21
    -1
      r1cs-std/src/fields/cubic_extension.rs
  3. +15
    -1
      r1cs-std/src/fields/fp/mod.rs
  4. +20
    -1
      r1cs-std/src/fields/quadratic_extension.rs
  5. +21
    -1
      r1cs-std/src/groups/curves/short_weierstrass/mod.rs
  6. +21
    -1
      r1cs-std/src/groups/curves/twisted_edwards/mod.rs
  7. +7
    -0
      r1cs-std/src/lib.rs

+ 10
- 2
r1cs-std/src/bits/boolean.rs

@ -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};
@ -597,6 +597,14 @@ impl ToBytesGadget for Boolean {
}
}
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(

+ 21
- 1
r1cs-std/src/fields/cubic_extension.rs

@ -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)]
@ -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>,

+ 15
- 1
r1cs-std/src/fields/fp/mod.rs

@ -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;
@ -432,6 +432,13 @@ impl ToBytesGadget for AllocatedFp {
}
}
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")]
@ -845,6 +852,13 @@ impl ToBytesGadget for FpVar {
}
}
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(

+ 20
- 1
r1cs-std/src/fields/quadratic_extension.rs

@ -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)]
@ -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>,

+ 21
- 1
r1cs-std/src/groups/curves/short_weierstrass/mod.rs

@ -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;
@ -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,

+ 21
- 1
r1cs-std/src/groups/curves/twisted_edwards/mod.rs

@ -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)]
@ -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::new();
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;

+ 7
- 0
r1cs-std/src/lib.rs

@ -154,3 +154,10 @@ impl Assignment for Option {
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>;
}

Loading…
Cancel
Save