Skip to content

Commit

Permalink
Refactor field tests (#166)
Browse files Browse the repository at this point in the history
* refactor: field tests

* [WIP]: disable bls to fix tests

* fix: adapt tests to extension field refactor

* fix: bls12381 tests

* feat: add must_use for mul_nonresidue

* refactor: cleaner test_frobenius macro

* fix: add must_use

* fix: Change PrimeField consts for tower2

* chore: enable fp2 missing tests

* chore: cleanup

* fix: address review comments

* Update src/pluto_eris/fp2.rs

Co-authored-by: adria0.eth <[email protected]>

---------

Co-authored-by: adria0.eth <[email protected]>
  • Loading branch information
davidnevadoc and adria0 authored Oct 1, 2024
1 parent eb9812c commit 8771fe5
Show file tree
Hide file tree
Showing 30 changed files with 1,024 additions and 930 deletions.
23 changes: 13 additions & 10 deletions src/bls12381/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,16 +37,19 @@ impl ExtField for Fq {
#[cfg(test)]
mod test {
use super::*;
crate::field_testing_suite!(Fq, "field_arithmetic");
crate::field_testing_suite!(Fq, "conversion");
crate::field_testing_suite!(Fq, "serialization");
crate::field_testing_suite!(Fq, "quadratic_residue");
crate::field_testing_suite!(Fq, "bits");
crate::field_testing_suite!(Fq, "serialization_check");
crate::field_testing_suite!(Fq, "constants");
crate::field_testing_suite!(Fq, "sqrt");
crate::field_testing_suite!(Fq, "zeta");
crate::field_testing_suite!(Fq, "from_uniform_bytes", 64, 96);
use crate::{
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
};

constants_test!(Fq);

arith_test!(Fq);
legendre_test!(Fq);
test!(arith, Fq, sqrt_test, 1000);

serde_test!(Fq PrimeFieldBits);
from_uniform_bytes_test!(Fq, 1000, L 64, L 96);

#[test]
fn test_fq_mul_nonresidue() {
let e = Fq::random(rand_core::OsRng);
Expand Down
38 changes: 26 additions & 12 deletions src/bls12381/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -268,17 +268,31 @@ pub const FROBENIUS_COEFF_FQ12_C1: [Fq2; 12] = [

#[cfg(test)]
mod test {
macro_rules! test_fq12 {
($test:ident, $size: expr) => {
paste::paste! {
#[test]
fn [< $test test >]() {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
crate::bls12381::fq12::test::$test(&mut rng, $size);
}
}
};
}
use super::*;
crate::field_testing_suite!(Fq12, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fq12, "quadratic_sparse_mul", Fq6, Fq2);
crate::field_testing_suite!(
Fq12,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fq::MODULUS)
Fq::MODULUS_LIMBS
);
use crate::{arith_test, frobenius_test, setup_f12_test_funcs, test};
use ff::Field;
use rand::RngCore;

arith_test!(Fq12);
// TODO Compile problems with derive_serde feature
// serde_test!(fq12);

// F12 specific
setup_f12_test_funcs!(Fq12, Fq6, Fq2);
test_fq12!(f12_mul_by_014_, 500);
test_fq12!(f12_mul_by_034_, 500);
frobenius_test!(Fq12, Fq, 8);
}
32 changes: 15 additions & 17 deletions src/bls12381/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,23 +65,21 @@ impl ExtField for Fq2 {
mod test {

use super::*;
crate::field_testing_suite!(Fq2, "field_arithmetic");
crate::field_testing_suite!(Fq2, "conversion");
crate::field_testing_suite!(Fq2, "serialization");
crate::field_testing_suite!(Fq2, "quadratic_residue");
crate::field_testing_suite!(Fq2, "sqrt");
crate::field_testing_suite!(Fq2, "zeta", Fq);
// extension field-specific
crate::field_testing_suite!(Fq2, "f2_tests", Fq);
crate::field_testing_suite!(
Fq2,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fq::MODULUS)
Fq::MODULUS_LIMBS
);
use crate::{
arith_test, constants_test, f2_test, frobenius_test, legendre_test, serde_test, test,
};
use rand_core::RngCore;

constants_test!(Fq2);

arith_test!(Fq2);
legendre_test!(Fq2);
test!(arith, Fq2, sqrt_test, 1000);

serde_test!(Fq2);

f2_test!(Fq2, Fq);
frobenius_test!(Fq2, Fq, 20);

#[test]
fn test_fq2_mul_nonresidue() {
Expand Down
36 changes: 23 additions & 13 deletions src/bls12381/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -277,22 +277,32 @@ pub const FROBENIUS_COEFF_FQ6_C2: [Fq2; 6] = [
#[cfg(test)]
mod test {
use super::*;
crate::field_testing_suite!(Fq6, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fq6, "cubic_sparse_mul", Fq2);
crate::field_testing_suite!(
Fq6,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fq::MODULUS)
Fq::MODULUS_LIMBS
);
use crate::{arith_test, frobenius_test, setup_f6_test_funcs, test};
use rand_core::RngCore;

macro_rules! test_fq6 {
($test:ident, $size: expr) => {
paste::paste! {
#[test]
fn [< $test test >]() {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
crate::bls12381::fq6::test::$test(&mut rng, $size);
}
}
};
}

arith_test!(Fq6);
setup_f6_test_funcs!(Fq6, Fq2);
test_fq6!(f6_mul_nonresidue_, 1000);
test_fq6!(f6_mul_by_1_, 1000);
test_fq6!(f6_mul_by_01_, 1000);
frobenius_test!(Fq6, Fq, 10);

#[test]
fn test_fq6_mul_nonresidue() {
use ff::Field;
let e = Fq6::random(rand_core::OsRng);
let a0 = e.mul_by_nonresidue();
let a1 = e * Fq6::NON_RESIDUE;
Expand Down
24 changes: 13 additions & 11 deletions src/bls12381/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,15 +24,17 @@ crate::impl_from_bool!(Fr);

#[cfg(test)]
mod test {
use super::*;
crate::field_testing_suite!(Fr, "field_arithmetic");
crate::field_testing_suite!(Fr, "conversion");
crate::field_testing_suite!(Fr, "serialization");
crate::field_testing_suite!(Fr, "quadratic_residue");
crate::field_testing_suite!(Fr, "bits");
crate::field_testing_suite!(Fr, "serialization_check");
crate::field_testing_suite!(Fr, "constants");
crate::field_testing_suite!(Fr, "sqrt");
crate::field_testing_suite!(Fr, "zeta");
crate::field_testing_suite!(Fr, "from_uniform_bytes", 64);
use super::Fr;
use crate::{
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
};

constants_test!(Fr);

arith_test!(Fr);
legendre_test!(Fr);
test!(arith, Fr, sqrt_test, 1000);

serde_test!(Fr PrimeFieldBits);
from_uniform_bytes_test!(Fr, 1000, L 64);
}
31 changes: 13 additions & 18 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,22 +35,17 @@ impl ExtField for Fq {

#[cfg(test)]
mod test {
use super::*;
crate::field_testing_suite!(Fq, "field_arithmetic");
crate::field_testing_suite!(Fq, "conversion");
crate::field_testing_suite!(Fq, "serialization");
crate::field_testing_suite!(Fq, "quadratic_residue");
crate::field_testing_suite!(Fq, "bits");
crate::field_testing_suite!(Fq, "serialization_check");
crate::field_testing_suite!(Fq, "constants");
crate::field_testing_suite!(Fq, "sqrt");
crate::field_testing_suite!(Fq, "zeta");
crate::field_testing_suite!(Fq, "from_uniform_bytes", 64, 48);
#[test]
fn test_fq_mul_nonresidue() {
let e = Fq::random(rand_core::OsRng);
let a0 = e.mul_by_nonresidue();
let a1 = e * Fq::NON_RESIDUE;
assert_eq!(a0, a1);
}
use super::Fq;
use crate::{
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
};

constants_test!(Fq);

arith_test!(Fq);
legendre_test!(Fq);
test!(arith, Fq, sqrt_test, 1000);

serde_test!(Fq PrimeFieldBits);
from_uniform_bytes_test!(Fq, 1000, L 64, L 48);
}
37 changes: 25 additions & 12 deletions src/bn256/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -195,17 +195,30 @@ pub const FROBENIUS_COEFF_FQ12_C1: [Fq2; 12] = [

#[cfg(test)]
mod test {

macro_rules! test_fq12 {
($test:ident, $size: expr) => {
paste::paste! {
#[test]
fn [< $test test >]() {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
crate::bn256::fq12::test::$test(&mut rng, $size);
}
}
};
}
use super::*;
crate::field_testing_suite!(Fq12, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fq12, "quadratic_sparse_mul", Fq6, Fq2);
crate::field_testing_suite!(
Fq12,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fq::MODULUS)
Fq::MODULUS_LIMBS
);
use crate::{arith_test, frobenius_test, setup_f12_test_funcs, test};
use ff::Field;
use rand::RngCore;

arith_test!(Fq12);

// F12 specific
setup_f12_test_funcs!(Fq12, Fq6, Fq2);
test_fq12!(f12_mul_by_014_, 500);
test_fq12!(f12_mul_by_034_, 500);
frobenius_test!(Fq12, Fq, 8);
}
31 changes: 14 additions & 17 deletions src/bn256/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,23 +68,20 @@ impl ExtField for Fq2 {
mod test {

use super::*;
crate::field_testing_suite!(Fq2, "field_arithmetic");
crate::field_testing_suite!(Fq2, "conversion");
crate::field_testing_suite!(Fq2, "serialization");
crate::field_testing_suite!(Fq2, "quadratic_residue");
crate::field_testing_suite!(Fq2, "sqrt");
crate::field_testing_suite!(Fq2, "zeta", Fq);
// extension field-specific
crate::field_testing_suite!(Fq2, "f2_tests", Fq);
crate::field_testing_suite!(
Fq2,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fq::MODULUS)
Fq::MODULUS_LIMBS
);
use crate::{
arith_test, constants_test, f2_test, frobenius_test, legendre_test, serde_test, test,
};
use rand_core::RngCore;

constants_test!(Fq2);
arith_test!(Fq2);
legendre_test!(Fq2);
test!(arith, Fq2, sqrt_test, 1000);

serde_test!(Fq2);

f2_test!(Fq2, Fq);
frobenius_test!(Fq2, Fq, 20);

#[test]
fn test_fq2_mul_nonresidue() {
Expand Down
40 changes: 21 additions & 19 deletions src/bn256/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,27 @@ pub const FROBENIUS_COEFF_FQ6_C2: [Fq2; 6] = [
#[cfg(test)]
mod test {
use super::*;
crate::field_testing_suite!(Fq6, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fq6, "cubic_sparse_mul", Fq2);
crate::field_testing_suite!(
Fq6,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fq::MODULUS)
Fq::MODULUS_LIMBS
);
use crate::{arith_test, frobenius_test, setup_f6_test_funcs, test};
use rand_core::RngCore;

#[test]
fn test_fq6_mul_nonresidue() {
use ff::Field;
let e = Fq6::random(rand_core::OsRng);
let a0 = e.mul_by_nonresidue();
let a1 = e * Fq6::NON_RESIDUE;
assert_eq!(a0, a1);
macro_rules! test_fq6 {
($test:ident, $size: expr) => {
paste::paste! {
#[test]
fn [< $test test >]() {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
crate::bn256::fq6::test::$test(&mut rng, $size);
}
}
};
}

arith_test!(Fq6);
setup_f6_test_funcs!(Fq6, Fq2);
test_fq6!(f6_mul_nonresidue_, 1000);
test_fq6!(f6_mul_by_1_, 1000);
test_fq6!(f6_mul_by_01_, 1000);
frobenius_test!(Fq6, Fq, 10);
}
23 changes: 12 additions & 11 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,17 @@ mod table_tests;

#[cfg(test)]
mod test {
use super::Fr;
use crate::{
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
};

use super::*;
crate::field_testing_suite!(Fr, "field_arithmetic");
crate::field_testing_suite!(Fr, "conversion");
crate::field_testing_suite!(Fr, "serialization");
crate::field_testing_suite!(Fr, "quadratic_residue");
crate::field_testing_suite!(Fr, "bits");
crate::field_testing_suite!(Fr, "serialization_check");
crate::field_testing_suite!(Fr, "constants");
crate::field_testing_suite!(Fr, "sqrt");
crate::field_testing_suite!(Fr, "zeta");
crate::field_testing_suite!(Fr, "from_uniform_bytes", 64);
constants_test!(Fr);

arith_test!(Fr);
legendre_test!(Fr);
test!(arith, Fr, sqrt_test, 1000);

serde_test!(Fr PrimeFieldBits);
from_uniform_bytes_test!(Fr, 1000, L 64, L 48);
}
Loading

0 comments on commit 8771fe5

Please sign in to comment.