Skip to content

Commit

Permalink
Use correct constraint field for curve tests and add tests for multip…
Browse files Browse the repository at this point in the history
…lication by `NonNativeFieldVar` (#742)

* Use correct constraint field for curve tests

* Add mul by non-native field var test

* Tweak

* Fix

* fmt

* Update after rename of `NonNativeFieldVar`

* Tweak

* Tweak

* Tweak

* Format
  • Loading branch information
Pratyush authored Jan 3, 2024
1 parent 4c994eb commit c3018ec
Showing 1 changed file with 20 additions and 3 deletions.
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

0 comments on commit c3018ec

Please sign in to comment.