From 768ad7256585e162a5643f5b8b9e7a8b27d8be3a Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Wed, 27 Dec 2023 21:56:02 -0500 Subject: [PATCH 01/10] Use correct constraint field for curve tests --- curves/curve-constraint-tests/src/lib.rs | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index b7d146ade..e67e39f45 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -525,6 +525,8 @@ pub mod pairing { use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand}; + type BasePrimeField

= <

::BaseField as Field>::BasePrimeField; + #[allow(dead_code)] pub fn bilinearity_test>() -> Result<(), SynthesisError> where @@ -538,7 +540,7 @@ pub mod pairing { AllocationMode::Constant, ]; for &mode in &modes { - let cs = ConstraintSystem::<::BaseField>::new_ref(); + let cs = ConstraintSystem::>::new_ref(); let mut rng = test_rng(); let a = E::G1::rand(&mut rng); From ce11b1b4cdb4d34a5dd40ad0c1aae353c81e3427 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Wed, 27 Dec 2023 22:03:56 -0500 Subject: [PATCH 02/10] Add mul by non-native field var test --- curves/curve-constraint-tests/src/lib.rs | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index e67e39f45..c0f31eca6 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -231,7 +231,7 @@ pub mod curves { use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand}; - use ark_r1cs_std::prelude::*; + use ark_r1cs_std::{prelude::*, fields::nonnative::NonNativeFieldVar}; pub fn group_test() -> Result<(), SynthesisError> where @@ -350,15 +350,22 @@ pub mod curves { let scalar_bits: Vec = BitIteratorLE::new(&scalar).collect(); input = Vec::new_witness(ark_relations::ns!(cs, "bits"), || Ok(scalar_bits)).unwrap(); + let scalar = NonNativeFieldVar::new_variable( + ark_relations::ns!(cs, "scalar"), + || Ok(scalar), + mode, + ).unwrap(); let result = a .scalar_mul_le(input.iter()) .expect(&format!("Mode: {:?}", mode)); + let mul_result = a * scalar; let result_val = result.value()?.into_affine(); assert_eq!( result_val, native_result, "gadget & native values are diff. after scalar mul {:?}", scalar, ); + assert_eq!(mul_result.value().unwrap(), native_result); assert!(cs.is_satisfied().unwrap()); } From d702ca22f7a6b0316772c455630e0ee6f939d167 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Wed, 27 Dec 2023 22:23:22 -0500 Subject: [PATCH 03/10] Tweak --- curves/curve-constraint-tests/src/lib.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index c0f31eca6..d9f42d97b 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -231,7 +231,7 @@ pub mod curves { use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand}; - use ark_r1cs_std::{prelude::*, fields::nonnative::NonNativeFieldVar}; + use ark_r1cs_std::{fields::nonnative::NonNativeFieldVar, prelude::*}; pub fn group_test() -> Result<(), SynthesisError> where @@ -354,7 +354,8 @@ pub mod curves { ark_relations::ns!(cs, "scalar"), || Ok(scalar), mode, - ).unwrap(); + ) + .unwrap(); let result = a .scalar_mul_le(input.iter()) .expect(&format!("Mode: {:?}", mode)); From 87826da1605befda5e296d6367acbe7948536518 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Wed, 27 Dec 2023 22:29:23 -0500 Subject: [PATCH 04/10] Fix --- curves/curve-constraint-tests/src/lib.rs | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index d9f42d97b..3f85ff772 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -350,23 +350,30 @@ pub mod curves { let scalar_bits: Vec = BitIteratorLE::new(&scalar).collect(); input = Vec::new_witness(ark_relations::ns!(cs, "bits"), || Ok(scalar_bits)).unwrap(); - let scalar = NonNativeFieldVar::new_variable( + let scalar_var = NonNativeFieldVar::new_variable( ark_relations::ns!(cs, "scalar"), - || Ok(scalar), + + || { + let scalar = scalar + .iter() + .flat_map(|b| b.to_le_bytes()) + .collect::>(); + Ok(C::ScalarField::from_le_bytes_mod_order(&scalar)) + }, mode, ) .unwrap(); let result = a .scalar_mul_le(input.iter()) .expect(&format!("Mode: {:?}", mode)); - let mul_result = a * scalar; + let mul_result = a.clone() * scalar_var; let result_val = result.value()?.into_affine(); assert_eq!( result_val, native_result, "gadget & native values are diff. after scalar mul {:?}", scalar, ); - assert_eq!(mul_result.value().unwrap(), native_result); + assert_eq!(mul_result.value().unwrap().into_affine(), native_result); assert!(cs.is_satisfied().unwrap()); } From 20d88dbc74e764f49fcfb3d1c2d5c4f0e7c60d85 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Wed, 27 Dec 2023 22:29:37 -0500 Subject: [PATCH 05/10] fmt --- curves/curve-constraint-tests/src/lib.rs | 1 - 1 file changed, 1 deletion(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index 3f85ff772..c09aef15d 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -352,7 +352,6 @@ pub mod curves { Vec::new_witness(ark_relations::ns!(cs, "bits"), || Ok(scalar_bits)).unwrap(); let scalar_var = NonNativeFieldVar::new_variable( ark_relations::ns!(cs, "scalar"), - || { let scalar = scalar .iter() From 1b9360ca77f04f1b0a9225b5d97ed4ba3e8b67aa Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Thu, 28 Dec 2023 13:02:23 -0500 Subject: [PATCH 06/10] Update after rename of `NonNativeFieldVar` --- curves/curve-constraint-tests/src/lib.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index c09aef15d..d3b8b7b01 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -231,7 +231,7 @@ pub mod curves { use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand}; - use ark_r1cs_std::{fields::nonnative::NonNativeFieldVar, prelude::*}; + use ark_r1cs_std::{fields::emulated_fp::EmulatedFpVar, prelude::*}; pub fn group_test() -> Result<(), SynthesisError> where @@ -350,7 +350,7 @@ pub mod curves { let scalar_bits: Vec = BitIteratorLE::new(&scalar).collect(); input = Vec::new_witness(ark_relations::ns!(cs, "bits"), || Ok(scalar_bits)).unwrap(); - let scalar_var = NonNativeFieldVar::new_variable( + let scalar_var = EmulatedFpVar::new_variable( ark_relations::ns!(cs, "scalar"), || { let scalar = scalar From c80da9b702eddf6679263b1743516c473caddf7d Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Thu, 28 Dec 2023 16:28:54 -0500 Subject: [PATCH 07/10] Tweak --- curves/curve-constraint-tests/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index d3b8b7b01..86ac8543b 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -195,7 +195,7 @@ pub mod fields { pub fn frobenius_tests(maxpower: usize) -> Result<(), SynthesisError> where F: Field, - ConstraintF: Field, + ConstraintF: PrimeField, AF: FieldVar, for<'a> &'a AF: FieldOpsBounds<'a, F, AF>, { From ebbca39f5314b49aff127874eb119450a166b267 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Thu, 28 Dec 2023 17:25:53 -0500 Subject: [PATCH 08/10] Tweak --- curves/curve-constraint-tests/src/lib.rs | 1 + 1 file changed, 1 insertion(+) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index 86ac8543b..13497c7a5 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -536,6 +536,7 @@ pub mod pairing { }; use ark_ff::{BitIteratorLE, Field, PrimeField}; use ark_r1cs_std::prelude::*; + use ark_r1cs_std::convert::ToBytesGadget use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand}; From fac89e0676c092069ece233ab19b048a779d99cf Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Thu, 28 Dec 2023 17:40:28 -0500 Subject: [PATCH 09/10] Tweak --- curves/curve-constraint-tests/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index 13497c7a5..6c73695d6 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -536,7 +536,7 @@ pub mod pairing { }; use ark_ff::{BitIteratorLE, Field, PrimeField}; use ark_r1cs_std::prelude::*; - use ark_r1cs_std::convert::ToBytesGadget + use ark_r1cs_std::convert::ToBytesGadget; use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand}; From 0d237b4be3bf183896e74579dfa7a4c578b1d855 Mon Sep 17 00:00:00 2001 From: Pratyush Mishra Date: Wed, 3 Jan 2024 08:07:24 -0500 Subject: [PATCH 10/10] Format --- curves/curve-constraint-tests/src/lib.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/curves/curve-constraint-tests/src/lib.rs b/curves/curve-constraint-tests/src/lib.rs index 6c73695d6..2607f8c85 100644 --- a/curves/curve-constraint-tests/src/lib.rs +++ b/curves/curve-constraint-tests/src/lib.rs @@ -535,8 +535,8 @@ pub mod pairing { AffineRepr, CurveGroup, }; use ark_ff::{BitIteratorLE, Field, PrimeField}; - use ark_r1cs_std::prelude::*; use ark_r1cs_std::convert::ToBytesGadget; + use ark_r1cs_std::prelude::*; use ark_relations::r1cs::{ConstraintSystem, SynthesisError}; use ark_std::{test_rng, vec::Vec, UniformRand};