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

Use correct constraint field for curve tests and add tests for multiplication by NonNativeFieldVar #742

Merged
merged 10 commits into from
Jan 3, 2024
Merged
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
23 changes: 20 additions & 3 deletions curves/curve-constraint-tests/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,7 @@ pub mod fields {
pub fn frobenius_tests<F: Field, ConstraintF, AF>(maxpower: usize) -> Result<(), SynthesisError>
where
F: Field,
ConstraintF: Field,
ConstraintF: PrimeField,
AF: FieldVar<F, ConstraintF>,
for<'a> &'a AF: FieldOpsBounds<'a, F, AF>,
{
Expand Down Expand Up @@ -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::{fields::emulated_fp::EmulatedFpVar, prelude::*};

pub fn group_test<C, ConstraintF, GG>() -> Result<(), SynthesisError>
where
Expand Down Expand Up @@ -350,15 +350,29 @@ pub mod curves {
let scalar_bits: Vec<bool> = BitIteratorLE::new(&scalar).collect();
input =
Vec::new_witness(ark_relations::ns!(cs, "bits"), || Ok(scalar_bits)).unwrap();
let scalar_var = EmulatedFpVar::new_variable(
ark_relations::ns!(cs, "scalar"),
|| {
let scalar = scalar
.iter()
.flat_map(|b| b.to_le_bytes())
.collect::<Vec<_>>();
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.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().into_affine(), native_result);
assert!(cs.is_satisfied().unwrap());
}

Expand Down Expand Up @@ -521,10 +535,13 @@ pub mod pairing {
AffineRepr, CurveGroup,
};
use ark_ff::{BitIteratorLE, Field, PrimeField};
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};

type BasePrimeField<P> = <<P as Pairing>::BaseField as Field>::BasePrimeField;

#[allow(dead_code)]
pub fn bilinearity_test<E: Pairing, P: PairingVar<E>>() -> Result<(), SynthesisError>
where
Expand All @@ -538,7 +555,7 @@ pub mod pairing {
AllocationMode::Constant,
];
for &mode in &modes {
let cs = ConstraintSystem::<<E::G1 as CurveGroup>::BaseField>::new_ref();
let cs = ConstraintSystem::<BasePrimeField<E>>::new_ref();

let mut rng = test_rng();
let a = E::G1::rand(&mut rng);
Expand Down
Loading