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

ToConstraintFieldGadget for SW ProjectiveVar #284

Closed
wants to merge 9 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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: 12 additions & 0 deletions algebra-core/src/to_field_vec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,16 @@ impl<F: PrimeField> ToConstraintField<F> for F {
}
}

impl<F: Field> ToConstraintField<F> for bool {
fn to_field_elements(&self) -> Result<Vec<F>, Error> {
if *self == true {
Ok(vec![F::one()])
} else {
Ok(vec![F::zero()])
}
}
}

// Impl for base field
impl<F: Field> ToConstraintField<F> for [F] {
#[inline]
Expand Down Expand Up @@ -69,7 +79,9 @@ where
fn to_field_elements(&self) -> Result<Vec<ConstraintF>, Error> {
let mut x_fe = self.x.to_field_elements()?;
let y_fe = self.y.to_field_elements()?;
let infinity_fe = self.infinity.to_field_elements()?;
x_fe.extend_from_slice(&y_fe);
x_fe.extend_from_slice(&infinity_fe);
Ok(x_fe)
}
}
Expand Down
3 changes: 2 additions & 1 deletion r1cs-std/src/fields/fp/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -872,7 +872,8 @@ impl<F: PrimeField> ToBitsGadget<F> for FpVar<F> {
fn to_non_unique_bits_le(&self) -> Result<Vec<Boolean<F>>, SynthesisError> {
use algebra::BitIteratorLE;
match self {
Self::Constant(c) => Ok(BitIteratorLE::without_trailing_zeros(&c.into_repr())
Self::Constant(c) => Ok(BitIteratorLE::new(&c.into_repr())
.take((F::Params::MODULUS_BITS) as usize)
.map(Boolean::constant)
.collect::<Vec<_>>()),
Self::Var(v) => v.to_non_unique_bits_le(),
Expand Down
15 changes: 15 additions & 0 deletions r1cs-std/src/groups/curves/short_weierstrass/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ where

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

Ok(res)
}
Expand Down Expand Up @@ -709,6 +710,20 @@ where
}
}

impl<P, F> ToConstraintFieldGadget<<P::BaseField as Field>::BasePrimeField> for ProjectiveVar<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> {
self.to_affine()?.to_constraint_field()
}
}

#[cfg(test)]
#[allow(dead_code)]
pub(crate) fn test<P, GG>() -> Result<(), SynthesisError>
Expand Down