From 737f3b8c1ea2397df7b6555b01e685ef455af1b1 Mon Sep 17 00:00:00 2001 From: weikeng Date: Thu, 17 Sep 2020 16:31:53 -0700 Subject: [PATCH 1/9] add the case for projective curve --- .../groups/curves/short_weierstrass/mod.rs | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs index b73470e1e..202d771f6 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs @@ -709,6 +709,30 @@ where } } +impl ToConstraintFieldGadget<::BasePrimeField> for ProjectiveVar + where + P: SWModelParameters, + F: FieldVar::BasePrimeField>, + for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>, + F: ToConstraintFieldGadget<::BasePrimeField>, +{ + fn to_constraint_field( + &self, + ) -> Result::BasePrimeField>>, SynthesisError> { + let mut res = Vec::::BasePrimeField>>::new(); + + let z_inv = self.z.inverse()?; + + let x_div_by_z = &self.x * &z_inv; + let y_div_by_z = &self.x * &z_inv; + + res.extend_from_slice(&x_div_by_z.to_constraint_field()?); + res.extend_from_slice(&y_div_by_z.to_constraint_field()?); + + Ok(res) + } +} + #[cfg(test)] #[allow(dead_code)] pub(crate) fn test() -> Result<(), SynthesisError> From ae621bfffad34c8e020f3dd38014e6227028e449 Mon Sep 17 00:00:00 2001 From: weikeng Date: Thu, 17 Sep 2020 16:36:31 -0700 Subject: [PATCH 2/9] fmt --- r1cs-std/src/groups/curves/short_weierstrass/mod.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs index 202d771f6..2eb0c665e 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs @@ -710,11 +710,11 @@ where } impl ToConstraintFieldGadget<::BasePrimeField> for ProjectiveVar - where - P: SWModelParameters, - F: FieldVar::BasePrimeField>, - for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>, - F: ToConstraintFieldGadget<::BasePrimeField>, +where + P: SWModelParameters, + F: FieldVar::BasePrimeField>, + for<'a> &'a F: FieldOpsBounds<'a, P::BaseField, F>, + F: ToConstraintFieldGadget<::BasePrimeField>, { fn to_constraint_field( &self, From 6c3a6d60f63bf0453ddbabf806354331264732bd Mon Sep 17 00:00:00 2001 From: weikeng Date: Fri, 18 Sep 2020 16:20:24 -0700 Subject: [PATCH 3/9] serialize infinity; serialize proj by converting to affine --- r1cs-std/src/groups/curves/short_weierstrass/mod.rs | 13 ++----------- 1 file changed, 2 insertions(+), 11 deletions(-) diff --git a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs index 2eb0c665e..9ac33b29c 100644 --- a/r1cs-std/src/groups/curves/short_weierstrass/mod.rs +++ b/r1cs-std/src/groups/curves/short_weierstrass/mod.rs @@ -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) } @@ -719,17 +720,7 @@ where fn to_constraint_field( &self, ) -> Result::BasePrimeField>>, SynthesisError> { - let mut res = Vec::::BasePrimeField>>::new(); - - let z_inv = self.z.inverse()?; - - let x_div_by_z = &self.x * &z_inv; - let y_div_by_z = &self.x * &z_inv; - - res.extend_from_slice(&x_div_by_z.to_constraint_field()?); - res.extend_from_slice(&y_div_by_z.to_constraint_field()?); - - Ok(res) + self.to_affine()?.to_constraint_field() } } From d7fc1cef719e9b81b3e5ce5cf485e64762df0cd6 Mon Sep 17 00:00:00 2001 From: weikeng Date: Sun, 20 Sep 2020 15:56:21 -0700 Subject: [PATCH 4/9] change SWAffine to_constraint_field --- algebra-core/src/to_field_vec.rs | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/algebra-core/src/to_field_vec.rs b/algebra-core/src/to_field_vec.rs index 70dcb5ae1..4afa3e447 100644 --- a/algebra-core/src/to_field_vec.rs +++ b/algebra-core/src/to_field_vec.rs @@ -23,6 +23,16 @@ impl ToConstraintField for F { } } +impl ToConstraintField for bool { + fn to_field_elements(&self) -> Result, Error> { + if *self == true { + Ok(vec![F::one()]) + }else{ + Ok(vec![F::zero()]) + } + } +} + // Impl for base field impl ToConstraintField for [F] { #[inline] @@ -69,7 +79,9 @@ where fn to_field_elements(&self) -> Result, 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) } } From aba35343f9ca48511be450a7f00836c62eaff9bd Mon Sep 17 00:00:00 2001 From: weikeng Date: Sun, 20 Sep 2020 16:40:37 -0700 Subject: [PATCH 5/9] fmt --- algebra-core/src/to_field_vec.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/algebra-core/src/to_field_vec.rs b/algebra-core/src/to_field_vec.rs index 4afa3e447..b0bb84e42 100644 --- a/algebra-core/src/to_field_vec.rs +++ b/algebra-core/src/to_field_vec.rs @@ -27,7 +27,7 @@ impl ToConstraintField for bool { fn to_field_elements(&self) -> Result, Error> { if *self == true { Ok(vec![F::one()]) - }else{ + } else { Ok(vec![F::zero()]) } } From 4bf09733de077c8b7c726745c8669ffd61a7cd48 Mon Sep 17 00:00:00 2001 From: weikeng Date: Sun, 20 Sep 2020 18:09:56 -0700 Subject: [PATCH 6/9] [attempt] to_bits constant len --- r1cs-std/src/fields/fp/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r1cs-std/src/fields/fp/mod.rs b/r1cs-std/src/fields/fp/mod.rs index 1fa22662a..cd8ca75c8 100644 --- a/r1cs-std/src/fields/fp/mod.rs +++ b/r1cs-std/src/fields/fp/mod.rs @@ -872,7 +872,7 @@ impl ToBitsGadget for FpVar { fn to_non_unique_bits_le(&self) -> Result>, 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()) .map(Boolean::constant) .collect::>()), Self::Var(v) => v.to_non_unique_bits_le(), From 2b49bd7839a09af3164403ed365bdf29cb0245cb Mon Sep 17 00:00:00 2001 From: weikeng Date: Sun, 20 Sep 2020 18:14:55 -0700 Subject: [PATCH 7/9] [attempt] to_bits constant len --- r1cs-std/src/fields/fp/mod.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/r1cs-std/src/fields/fp/mod.rs b/r1cs-std/src/fields/fp/mod.rs index cd8ca75c8..e2f156ffb 100644 --- a/r1cs-std/src/fields/fp/mod.rs +++ b/r1cs-std/src/fields/fp/mod.rs @@ -873,6 +873,7 @@ impl ToBitsGadget for FpVar { use algebra::BitIteratorLE; match self { Self::Constant(c) => Ok(BitIteratorLE::new(&c.into_repr()) + .keep(F::Params::MODULUS_BITS - 1) .map(Boolean::constant) .collect::>()), Self::Var(v) => v.to_non_unique_bits_le(), From 19a01e6fd580cbf912387fad70c3928c39022c11 Mon Sep 17 00:00:00 2001 From: weikeng Date: Sun, 20 Sep 2020 18:17:23 -0700 Subject: [PATCH 8/9] [attempt] to_bits constant len --- r1cs-std/src/fields/fp/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r1cs-std/src/fields/fp/mod.rs b/r1cs-std/src/fields/fp/mod.rs index e2f156ffb..b5788c853 100644 --- a/r1cs-std/src/fields/fp/mod.rs +++ b/r1cs-std/src/fields/fp/mod.rs @@ -873,7 +873,7 @@ impl ToBitsGadget for FpVar { use algebra::BitIteratorLE; match self { Self::Constant(c) => Ok(BitIteratorLE::new(&c.into_repr()) - .keep(F::Params::MODULUS_BITS - 1) + .take((F::Params::MODULUS_BITS - 1) as usize) .map(Boolean::constant) .collect::>()), Self::Var(v) => v.to_non_unique_bits_le(), From b843f178585058f9e1534b08ed77b36cf8bc2118 Mon Sep 17 00:00:00 2001 From: weikeng Date: Sun, 20 Sep 2020 18:20:35 -0700 Subject: [PATCH 9/9] [attempt] to_bits constant len --- r1cs-std/src/fields/fp/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/r1cs-std/src/fields/fp/mod.rs b/r1cs-std/src/fields/fp/mod.rs index b5788c853..f9f781959 100644 --- a/r1cs-std/src/fields/fp/mod.rs +++ b/r1cs-std/src/fields/fp/mod.rs @@ -873,7 +873,7 @@ impl ToBitsGadget for FpVar { use algebra::BitIteratorLE; match self { Self::Constant(c) => Ok(BitIteratorLE::new(&c.into_repr()) - .take((F::Params::MODULUS_BITS - 1) as usize) + .take((F::Params::MODULUS_BITS) as usize) .map(Boolean::constant) .collect::>()), Self::Var(v) => v.to_non_unique_bits_le(),