mirror of
https://github.com/arnaucube/ark-r1cs-std.git
synced 2026-01-10 16:01:28 +01:00
Add ToConstraintFieldGadget (#278)
This commit is contained in:
@@ -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 core::borrow::Borrow;
|
||||||
use r1cs_core::{lc, ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable};
|
use r1cs_core::{lc, ConstraintSystemRef, LinearCombination, Namespace, SynthesisError, Variable};
|
||||||
|
|
||||||
@@ -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> {
|
impl<F: Field> CondSelectGadget<F> for Boolean<F> {
|
||||||
#[tracing::instrument(target = "r1cs")]
|
#[tracing::instrument(target = "r1cs")]
|
||||||
fn conditionally_select(
|
fn conditionally_select(
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ use algebra::{
|
|||||||
use core::{borrow::Borrow, marker::PhantomData};
|
use core::{borrow::Borrow, marker::PhantomData};
|
||||||
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
|
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
|
||||||
|
|
||||||
|
use crate::fields::fp::FpVar;
|
||||||
use crate::{
|
use crate::{
|
||||||
fields::{FieldOpsBounds, FieldVar},
|
fields::{FieldOpsBounds, FieldVar},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
Assignment, Vec,
|
Assignment, ToConstraintFieldGadget, Vec,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Derivative)]
|
#[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>
|
impl<BF, P> CondSelectGadget<P::BasePrimeField> for CubicExtVar<BF, P>
|
||||||
where
|
where
|
||||||
BF: FieldVar<P::BaseField, P::BasePrimeField>,
|
BF: FieldVar<P::BaseField, P::BasePrimeField>,
|
||||||
|
|||||||
@@ -4,7 +4,7 @@ use r1cs_core::{lc, ConstraintSystemRef, LinearCombination, Namespace, Synthesis
|
|||||||
use core::borrow::Borrow;
|
use core::borrow::Borrow;
|
||||||
|
|
||||||
use crate::fields::{FieldOpsBounds, FieldVar};
|
use crate::fields::{FieldOpsBounds, FieldVar};
|
||||||
use crate::{prelude::*, Assignment, Vec};
|
use crate::{prelude::*, Assignment, ToConstraintFieldGadget, Vec};
|
||||||
|
|
||||||
pub mod cmp;
|
pub mod cmp;
|
||||||
|
|
||||||
@@ -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> {
|
impl<F: PrimeField> CondSelectGadget<F> for AllocatedFp<F> {
|
||||||
#[inline]
|
#[inline]
|
||||||
#[tracing::instrument(target = "r1cs")]
|
#[tracing::instrument(target = "r1cs")]
|
||||||
@@ -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> {
|
impl<F: PrimeField> CondSelectGadget<F> for FpVar<F> {
|
||||||
#[tracing::instrument(target = "r1cs")]
|
#[tracing::instrument(target = "r1cs")]
|
||||||
fn conditionally_select(
|
fn conditionally_select(
|
||||||
|
|||||||
@@ -5,10 +5,11 @@ use algebra::{
|
|||||||
use core::{borrow::Borrow, marker::PhantomData};
|
use core::{borrow::Borrow, marker::PhantomData};
|
||||||
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
|
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
|
||||||
|
|
||||||
|
use crate::fields::fp::FpVar;
|
||||||
use crate::{
|
use crate::{
|
||||||
fields::{FieldOpsBounds, FieldVar},
|
fields::{FieldOpsBounds, FieldVar},
|
||||||
prelude::*,
|
prelude::*,
|
||||||
Assignment, Vec,
|
Assignment, ToConstraintFieldGadget, Vec,
|
||||||
};
|
};
|
||||||
|
|
||||||
#[derive(Derivative)]
|
#[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>
|
impl<BF, P> CondSelectGadget<P::BasePrimeField> for QuadExtVar<BF, P>
|
||||||
where
|
where
|
||||||
BF: FieldVar<P::BaseField, P::BasePrimeField>,
|
BF: FieldVar<P::BaseField, P::BasePrimeField>,
|
||||||
|
|||||||
@@ -8,7 +8,8 @@ use algebra::{
|
|||||||
use core::{borrow::Borrow, marker::PhantomData};
|
use core::{borrow::Borrow, marker::PhantomData};
|
||||||
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
|
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 bls12;
|
||||||
pub mod mnt4;
|
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>
|
impl<P, F> R1CSVar<<P::BaseField as Field>::BasePrimeField> for ProjectiveVar<P, F>
|
||||||
where
|
where
|
||||||
P: SWModelParameters,
|
P: SWModelParameters,
|
||||||
|
|||||||
@@ -8,8 +8,9 @@ use algebra::{
|
|||||||
|
|
||||||
use r1cs_core::{ConstraintSystemRef, Namespace, SynthesisError};
|
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};
|
use core::{borrow::Borrow, marker::PhantomData};
|
||||||
|
|
||||||
#[derive(Derivative)]
|
#[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]
|
#[inline]
|
||||||
fn div2(limbs: &mut [u64]) {
|
fn div2(limbs: &mut [u64]) {
|
||||||
let mut t = 0;
|
let mut t = 0;
|
||||||
|
|||||||
@@ -154,3 +154,10 @@ impl<T> Assignment<T> for Option<T> {
|
|||||||
self.ok_or(r1cs_core::SynthesisError::AssignmentMissing)
|
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>;
|
||||||
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user