Skip to content

Commit

Permalink
refactor: field tests
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnevadoc committed Jun 25, 2024
1 parent 63d44ee commit 1eb857a
Show file tree
Hide file tree
Showing 22 changed files with 893 additions and 882 deletions.
22 changes: 11 additions & 11 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,15 @@ impl_from_u64!(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);
use super::Fq;
use crate::{arith_test, constants_test, legendre_test, serde_test, test, test_uniform_bytes};

constants_test!(Fq);

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

serde_test!(Fq PrimeFieldBits);
test_uniform_bytes!(Fq, 1000, L 64, L 48);
}
38 changes: 29 additions & 9 deletions src/bn256/fq12.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use super::fq::Fq;
use super::fq2::Fq2;
use super::fq6::Fq6;

use crate::impl_tower2_common;
use core::ops::{Add, Neg, Sub};
use subtle::{Choice, ConditionallySelectable, ConstantTimeEq, CtOption};
Expand Down Expand Up @@ -357,17 +358,36 @@ 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, "f12_tests", Fq6, Fq2);
crate::field_testing_suite!(
use crate::{arith_test, setup_f12_test_funcs, test, test_frobenius};
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);
test_frobenius!(
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)
8,
[
0x3c208c16d87cfd47,
0x97816a916871ca8d,
Expand Down
30 changes: 16 additions & 14 deletions src/bn256/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,21 +181,21 @@ impl FromUniformBytes<96> for Fq2 {
#[cfg(test)]
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!(
use crate::{arith_test, legendre_test, serde_test, test};

// constants_test!(Fq2);

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

serde_test!(Fq2);
// test_uniform_bytes!(Fq2, 1000, L 96);

crate::f2_tests!(Fq2, Fq);
crate::test_frobenius!(
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)
20,
[
0x3c208c16d87cfd47,
0x97816a916871ca8d,
Expand Down Expand Up @@ -232,6 +232,8 @@ mod test {
}
}); // -1
}
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;

#[test]
fn test_fq2_mul_nonresidue() {
Expand Down
35 changes: 25 additions & 10 deletions src/bn256/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -415,22 +415,37 @@ pub const FROBENIUS_COEFF_FQ6_C2: [Fq2; 6] = [

#[cfg(test)]
mod test {

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);
}
}
};
}
use super::*;
crate::field_testing_suite!(Fq6, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fq6, "f6_tests", Fq2);
crate::field_testing_suite!(
use crate::{arith_test, setup_f6_test_funcs, test, test_frobenius};

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);
test_frobenius!(
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)
10,
[
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
0x30644e72e131a029,
0x30644e72e131a029
]
);
// test_uniform_bytes!(Fq6, 1000, L 96);
}
21 changes: 10 additions & 11 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,16 +51,15 @@ mod table_tests;

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

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);
test_uniform_bytes!(Fr, 1000, L 64, L 48);
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub mod secq256k1;
#[macro_use]
mod derive;

// Re-export to simplify down stream dependencies
// Re-export to simplify downstream dependencies.
pub use ff;
pub use group;
pub use pairing;
Expand Down
21 changes: 10 additions & 11 deletions src/pluto_eris/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ crate::impl_from_u64!(Fp);

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

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

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

serde_test!(Fp PrimeFieldBits);
test_uniform_bytes!(Fp, 1000, L 64, L 72, L 112);
}
33 changes: 27 additions & 6 deletions src/pluto_eris/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -421,14 +421,35 @@ pub const FROBENIUS_COEFF_FP12_C1: [Fp2; 12] = [

#[cfg(test)]
mod test {
macro_rules! test_fp12 {
($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::pluto_eris::fp12::test::$test(&mut rng, $size);
}
}
};
}
use super::*;
crate::field_testing_suite!(Fp12, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fp12, "f12_tests", Fp6, Fp2);
crate::field_testing_suite!(
use crate::{arith_test, setup_f12_test_funcs, test, test_frobenius};
use ff::Field;
use rand::RngCore;

arith_test!(Fp12);
// TODO Compile problems with derive_serde feature
// serde_test!(fp12);

// F12 specific
setup_f12_test_funcs!(Fp12, Fp6, Fp2);
test_fp12!(f12_mul_by_014_, 500);
test_fp12!(f12_mul_by_034_, 500);
test_frobenius!(
Fp12,
"frobenius",
// Frobenius endomorphism power parameter for extension field
8,
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fp::MODULUS)
Expand Down
30 changes: 16 additions & 14 deletions src/pluto_eris/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,21 +188,21 @@ impl Fp2 {
#[cfg(test)]
mod test {
use super::*;
crate::field_testing_suite!(Fp2, "field_arithmetic");
crate::field_testing_suite!(Fp2, "conversion");
crate::field_testing_suite!(Fp2, "serialization");
crate::field_testing_suite!(Fp2, "quadratic_residue");
crate::field_testing_suite!(Fp2, "sqrt");
crate::field_testing_suite!(Fp2, "zeta", Fp);
// extension field-specific
crate::field_testing_suite!(Fp2, "f2_tests", Fp);
crate::field_testing_suite!(
use crate::{arith_test, legendre_test, serde_test, test};

// constants_test!(Fp2);

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

serde_test!(Fp2);
// test_uniform_bytes!(Fp2, 1000, L 96);

crate::f2_tests!(Fp2, Fp);
crate::test_frobenius!(
Fp2,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fp::MODULUS)
20,
[
0x9ffffcd300000001,
0xa2a7e8c30006b945,
Expand Down Expand Up @@ -250,6 +250,8 @@ mod test {

#[test]
fn test_fp2_mul_nonresidue() {
use rand::SeedableRng;
use rand_xorshift::XorShiftRng;
let mut rng = XorShiftRng::from_seed([
0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06,
0xbc, 0xe5,
Expand Down
31 changes: 22 additions & 9 deletions src/pluto_eris/fp6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,17 +486,30 @@ pub(crate) const FROBENIUS_COEFF_FP6_C2: [Fp2; 6] = [

#[cfg(test)]
mod test {
macro_rules! test_fp6 {
($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::pluto_eris::fp6::test::$test(&mut rng, $size);
}
}
};
}
use super::*;
crate::field_testing_suite!(Fp6, "field_arithmetic");
// extension field-specific
crate::field_testing_suite!(Fp6, "f6_tests", Fp2);
crate::field_testing_suite!(
use crate::{arith_test, setup_f6_test_funcs, test, test_frobenius};

arith_test!(Fp6);
setup_f6_test_funcs!(Fp6, Fp2);
test_fp6!(f6_mul_nonresidue_, 1000);
test_fp6!(f6_mul_by_1_, 1000);
test_fp6!(f6_mul_by_01_, 1000);
test_frobenius!(
Fp6,
"frobenius",
// Frobenius endomorphism power parameter for extension field
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fp::MODULUS)
10,
[
0x9ffffcd300000001,
0xa2a7e8c30006b945,
Expand Down
21 changes: 10 additions & 11 deletions src/pluto_eris/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,15 @@ crate::impl_from_u64!(Fq);

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

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, 72, 112);
constants_test!(Fq);

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

serde_test!(Fq PrimeFieldBits);
test_uniform_bytes!(Fq, 1000, L 64, L 72, L 112);
}
Loading

0 comments on commit 1eb857a

Please sign in to comment.