Skip to content

Commit 8771fe5

Browse files
davidnevadocadria0
andauthored
Refactor field tests (#166)
* 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]>
1 parent eb9812c commit 8771fe5

30 files changed

+1024
-930
lines changed

Diff for: src/bls12381/fq.rs

+13-10
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,19 @@ impl ExtField for Fq {
3737
#[cfg(test)]
3838
mod test {
3939
use super::*;
40-
crate::field_testing_suite!(Fq, "field_arithmetic");
41-
crate::field_testing_suite!(Fq, "conversion");
42-
crate::field_testing_suite!(Fq, "serialization");
43-
crate::field_testing_suite!(Fq, "quadratic_residue");
44-
crate::field_testing_suite!(Fq, "bits");
45-
crate::field_testing_suite!(Fq, "serialization_check");
46-
crate::field_testing_suite!(Fq, "constants");
47-
crate::field_testing_suite!(Fq, "sqrt");
48-
crate::field_testing_suite!(Fq, "zeta");
49-
crate::field_testing_suite!(Fq, "from_uniform_bytes", 64, 96);
40+
use crate::{
41+
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
42+
};
43+
44+
constants_test!(Fq);
45+
46+
arith_test!(Fq);
47+
legendre_test!(Fq);
48+
test!(arith, Fq, sqrt_test, 1000);
49+
50+
serde_test!(Fq PrimeFieldBits);
51+
from_uniform_bytes_test!(Fq, 1000, L 64, L 96);
52+
5053
#[test]
5154
fn test_fq_mul_nonresidue() {
5255
let e = Fq::random(rand_core::OsRng);

Diff for: src/bls12381/fq12.rs

+26-12
Original file line numberDiff line numberDiff line change
@@ -268,17 +268,31 @@ pub const FROBENIUS_COEFF_FQ12_C1: [Fq2; 12] = [
268268

269269
#[cfg(test)]
270270
mod test {
271+
macro_rules! test_fq12 {
272+
($test:ident, $size: expr) => {
273+
paste::paste! {
274+
#[test]
275+
fn [< $test test >]() {
276+
use rand::SeedableRng;
277+
use rand_xorshift::XorShiftRng;
278+
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
279+
crate::bls12381::fq12::test::$test(&mut rng, $size);
280+
}
281+
}
282+
};
283+
}
271284
use super::*;
272-
crate::field_testing_suite!(Fq12, "field_arithmetic");
273-
// extension field-specific
274-
crate::field_testing_suite!(Fq12, "quadratic_sparse_mul", Fq6, Fq2);
275-
crate::field_testing_suite!(
276-
Fq12,
277-
"frobenius",
278-
// Frobenius endomorphism power parameter for extension field
279-
// ϕ: E → E
280-
// (x, y) ↦ (x^p, y^p)
281-
// p: modulus of base field (Here, Fq::MODULUS)
282-
Fq::MODULUS_LIMBS
283-
);
285+
use crate::{arith_test, frobenius_test, setup_f12_test_funcs, test};
286+
use ff::Field;
287+
use rand::RngCore;
288+
289+
arith_test!(Fq12);
290+
// TODO Compile problems with derive_serde feature
291+
// serde_test!(fq12);
292+
293+
// F12 specific
294+
setup_f12_test_funcs!(Fq12, Fq6, Fq2);
295+
test_fq12!(f12_mul_by_014_, 500);
296+
test_fq12!(f12_mul_by_034_, 500);
297+
frobenius_test!(Fq12, Fq, 8);
284298
}

Diff for: src/bls12381/fq2.rs

+15-17
Original file line numberDiff line numberDiff line change
@@ -65,23 +65,21 @@ impl ExtField for Fq2 {
6565
mod test {
6666

6767
use super::*;
68-
crate::field_testing_suite!(Fq2, "field_arithmetic");
69-
crate::field_testing_suite!(Fq2, "conversion");
70-
crate::field_testing_suite!(Fq2, "serialization");
71-
crate::field_testing_suite!(Fq2, "quadratic_residue");
72-
crate::field_testing_suite!(Fq2, "sqrt");
73-
crate::field_testing_suite!(Fq2, "zeta", Fq);
74-
// extension field-specific
75-
crate::field_testing_suite!(Fq2, "f2_tests", Fq);
76-
crate::field_testing_suite!(
77-
Fq2,
78-
"frobenius",
79-
// Frobenius endomorphism power parameter for extension field
80-
// ϕ: E → E
81-
// (x, y) ↦ (x^p, y^p)
82-
// p: modulus of base field (Here, Fq::MODULUS)
83-
Fq::MODULUS_LIMBS
84-
);
68+
use crate::{
69+
arith_test, constants_test, f2_test, frobenius_test, legendre_test, serde_test, test,
70+
};
71+
use rand_core::RngCore;
72+
73+
constants_test!(Fq2);
74+
75+
arith_test!(Fq2);
76+
legendre_test!(Fq2);
77+
test!(arith, Fq2, sqrt_test, 1000);
78+
79+
serde_test!(Fq2);
80+
81+
f2_test!(Fq2, Fq);
82+
frobenius_test!(Fq2, Fq, 20);
8583

8684
#[test]
8785
fn test_fq2_mul_nonresidue() {

Diff for: src/bls12381/fq6.rs

+23-13
Original file line numberDiff line numberDiff line change
@@ -277,22 +277,32 @@ pub const FROBENIUS_COEFF_FQ6_C2: [Fq2; 6] = [
277277
#[cfg(test)]
278278
mod test {
279279
use super::*;
280-
crate::field_testing_suite!(Fq6, "field_arithmetic");
281-
// extension field-specific
282-
crate::field_testing_suite!(Fq6, "cubic_sparse_mul", Fq2);
283-
crate::field_testing_suite!(
284-
Fq6,
285-
"frobenius",
286-
// Frobenius endomorphism power parameter for extension field
287-
// ϕ: E → E
288-
// (x, y) ↦ (x^p, y^p)
289-
// p: modulus of base field (Here, Fq::MODULUS)
290-
Fq::MODULUS_LIMBS
291-
);
280+
use crate::{arith_test, frobenius_test, setup_f6_test_funcs, test};
281+
use rand_core::RngCore;
282+
283+
macro_rules! test_fq6 {
284+
($test:ident, $size: expr) => {
285+
paste::paste! {
286+
#[test]
287+
fn [< $test test >]() {
288+
use rand::SeedableRng;
289+
use rand_xorshift::XorShiftRng;
290+
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
291+
crate::bls12381::fq6::test::$test(&mut rng, $size);
292+
}
293+
}
294+
};
295+
}
296+
297+
arith_test!(Fq6);
298+
setup_f6_test_funcs!(Fq6, Fq2);
299+
test_fq6!(f6_mul_nonresidue_, 1000);
300+
test_fq6!(f6_mul_by_1_, 1000);
301+
test_fq6!(f6_mul_by_01_, 1000);
302+
frobenius_test!(Fq6, Fq, 10);
292303

293304
#[test]
294305
fn test_fq6_mul_nonresidue() {
295-
use ff::Field;
296306
let e = Fq6::random(rand_core::OsRng);
297307
let a0 = e.mul_by_nonresidue();
298308
let a1 = e * Fq6::NON_RESIDUE;

Diff for: src/bls12381/fr.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,17 @@ crate::impl_from_bool!(Fr);
2424

2525
#[cfg(test)]
2626
mod test {
27-
use super::*;
28-
crate::field_testing_suite!(Fr, "field_arithmetic");
29-
crate::field_testing_suite!(Fr, "conversion");
30-
crate::field_testing_suite!(Fr, "serialization");
31-
crate::field_testing_suite!(Fr, "quadratic_residue");
32-
crate::field_testing_suite!(Fr, "bits");
33-
crate::field_testing_suite!(Fr, "serialization_check");
34-
crate::field_testing_suite!(Fr, "constants");
35-
crate::field_testing_suite!(Fr, "sqrt");
36-
crate::field_testing_suite!(Fr, "zeta");
37-
crate::field_testing_suite!(Fr, "from_uniform_bytes", 64);
27+
use super::Fr;
28+
use crate::{
29+
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
30+
};
31+
32+
constants_test!(Fr);
33+
34+
arith_test!(Fr);
35+
legendre_test!(Fr);
36+
test!(arith, Fr, sqrt_test, 1000);
37+
38+
serde_test!(Fr PrimeFieldBits);
39+
from_uniform_bytes_test!(Fr, 1000, L 64);
3840
}

Diff for: src/bn256/fq.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -35,22 +35,17 @@ impl ExtField for Fq {
3535

3636
#[cfg(test)]
3737
mod test {
38-
use super::*;
39-
crate::field_testing_suite!(Fq, "field_arithmetic");
40-
crate::field_testing_suite!(Fq, "conversion");
41-
crate::field_testing_suite!(Fq, "serialization");
42-
crate::field_testing_suite!(Fq, "quadratic_residue");
43-
crate::field_testing_suite!(Fq, "bits");
44-
crate::field_testing_suite!(Fq, "serialization_check");
45-
crate::field_testing_suite!(Fq, "constants");
46-
crate::field_testing_suite!(Fq, "sqrt");
47-
crate::field_testing_suite!(Fq, "zeta");
48-
crate::field_testing_suite!(Fq, "from_uniform_bytes", 64, 48);
49-
#[test]
50-
fn test_fq_mul_nonresidue() {
51-
let e = Fq::random(rand_core::OsRng);
52-
let a0 = e.mul_by_nonresidue();
53-
let a1 = e * Fq::NON_RESIDUE;
54-
assert_eq!(a0, a1);
55-
}
38+
use super::Fq;
39+
use crate::{
40+
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
41+
};
42+
43+
constants_test!(Fq);
44+
45+
arith_test!(Fq);
46+
legendre_test!(Fq);
47+
test!(arith, Fq, sqrt_test, 1000);
48+
49+
serde_test!(Fq PrimeFieldBits);
50+
from_uniform_bytes_test!(Fq, 1000, L 64, L 48);
5651
}

Diff for: src/bn256/fq12.rs

+25-12
Original file line numberDiff line numberDiff line change
@@ -195,17 +195,30 @@ pub const FROBENIUS_COEFF_FQ12_C1: [Fq2; 12] = [
195195

196196
#[cfg(test)]
197197
mod test {
198+
199+
macro_rules! test_fq12 {
200+
($test:ident, $size: expr) => {
201+
paste::paste! {
202+
#[test]
203+
fn [< $test test >]() {
204+
use rand::SeedableRng;
205+
use rand_xorshift::XorShiftRng;
206+
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
207+
crate::bn256::fq12::test::$test(&mut rng, $size);
208+
}
209+
}
210+
};
211+
}
198212
use super::*;
199-
crate::field_testing_suite!(Fq12, "field_arithmetic");
200-
// extension field-specific
201-
crate::field_testing_suite!(Fq12, "quadratic_sparse_mul", Fq6, Fq2);
202-
crate::field_testing_suite!(
203-
Fq12,
204-
"frobenius",
205-
// Frobenius endomorphism power parameter for extension field
206-
// ϕ: E → E
207-
// (x, y) ↦ (x^p, y^p)
208-
// p: modulus of base field (Here, Fq::MODULUS)
209-
Fq::MODULUS_LIMBS
210-
);
213+
use crate::{arith_test, frobenius_test, setup_f12_test_funcs, test};
214+
use ff::Field;
215+
use rand::RngCore;
216+
217+
arith_test!(Fq12);
218+
219+
// F12 specific
220+
setup_f12_test_funcs!(Fq12, Fq6, Fq2);
221+
test_fq12!(f12_mul_by_014_, 500);
222+
test_fq12!(f12_mul_by_034_, 500);
223+
frobenius_test!(Fq12, Fq, 8);
211224
}

Diff for: src/bn256/fq2.rs

+14-17
Original file line numberDiff line numberDiff line change
@@ -68,23 +68,20 @@ impl ExtField for Fq2 {
6868
mod test {
6969

7070
use super::*;
71-
crate::field_testing_suite!(Fq2, "field_arithmetic");
72-
crate::field_testing_suite!(Fq2, "conversion");
73-
crate::field_testing_suite!(Fq2, "serialization");
74-
crate::field_testing_suite!(Fq2, "quadratic_residue");
75-
crate::field_testing_suite!(Fq2, "sqrt");
76-
crate::field_testing_suite!(Fq2, "zeta", Fq);
77-
// extension field-specific
78-
crate::field_testing_suite!(Fq2, "f2_tests", Fq);
79-
crate::field_testing_suite!(
80-
Fq2,
81-
"frobenius",
82-
// Frobenius endomorphism power parameter for extension field
83-
// ϕ: E → E
84-
// (x, y) ↦ (x^p, y^p)
85-
// p: modulus of base field (Here, Fq::MODULUS)
86-
Fq::MODULUS_LIMBS
87-
);
71+
use crate::{
72+
arith_test, constants_test, f2_test, frobenius_test, legendre_test, serde_test, test,
73+
};
74+
use rand_core::RngCore;
75+
76+
constants_test!(Fq2);
77+
arith_test!(Fq2);
78+
legendre_test!(Fq2);
79+
test!(arith, Fq2, sqrt_test, 1000);
80+
81+
serde_test!(Fq2);
82+
83+
f2_test!(Fq2, Fq);
84+
frobenius_test!(Fq2, Fq, 20);
8885

8986
#[test]
9087
fn test_fq2_mul_nonresidue() {

Diff for: src/bn256/fq6.rs

+21-19
Original file line numberDiff line numberDiff line change
@@ -204,25 +204,27 @@ pub const FROBENIUS_COEFF_FQ6_C2: [Fq2; 6] = [
204204
#[cfg(test)]
205205
mod test {
206206
use super::*;
207-
crate::field_testing_suite!(Fq6, "field_arithmetic");
208-
// extension field-specific
209-
crate::field_testing_suite!(Fq6, "cubic_sparse_mul", Fq2);
210-
crate::field_testing_suite!(
211-
Fq6,
212-
"frobenius",
213-
// Frobenius endomorphism power parameter for extension field
214-
// ϕ: E → E
215-
// (x, y) ↦ (x^p, y^p)
216-
// p: modulus of base field (Here, Fq::MODULUS)
217-
Fq::MODULUS_LIMBS
218-
);
207+
use crate::{arith_test, frobenius_test, setup_f6_test_funcs, test};
208+
use rand_core::RngCore;
219209

220-
#[test]
221-
fn test_fq6_mul_nonresidue() {
222-
use ff::Field;
223-
let e = Fq6::random(rand_core::OsRng);
224-
let a0 = e.mul_by_nonresidue();
225-
let a1 = e * Fq6::NON_RESIDUE;
226-
assert_eq!(a0, a1);
210+
macro_rules! test_fq6 {
211+
($test:ident, $size: expr) => {
212+
paste::paste! {
213+
#[test]
214+
fn [< $test test >]() {
215+
use rand::SeedableRng;
216+
use rand_xorshift::XorShiftRng;
217+
let mut rng = XorShiftRng::from_seed(crate::tests::SEED);
218+
crate::bn256::fq6::test::$test(&mut rng, $size);
219+
}
220+
}
221+
};
227222
}
223+
224+
arith_test!(Fq6);
225+
setup_f6_test_funcs!(Fq6, Fq2);
226+
test_fq6!(f6_mul_nonresidue_, 1000);
227+
test_fq6!(f6_mul_by_1_, 1000);
228+
test_fq6!(f6_mul_by_01_, 1000);
229+
frobenius_test!(Fq6, Fq, 10);
228230
}

Diff for: src/bn256/fr.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -45,16 +45,17 @@ mod table_tests;
4545

4646
#[cfg(test)]
4747
mod test {
48+
use super::Fr;
49+
use crate::{
50+
arith_test, constants_test, from_uniform_bytes_test, legendre_test, serde_test, test,
51+
};
4852

49-
use super::*;
50-
crate::field_testing_suite!(Fr, "field_arithmetic");
51-
crate::field_testing_suite!(Fr, "conversion");
52-
crate::field_testing_suite!(Fr, "serialization");
53-
crate::field_testing_suite!(Fr, "quadratic_residue");
54-
crate::field_testing_suite!(Fr, "bits");
55-
crate::field_testing_suite!(Fr, "serialization_check");
56-
crate::field_testing_suite!(Fr, "constants");
57-
crate::field_testing_suite!(Fr, "sqrt");
58-
crate::field_testing_suite!(Fr, "zeta");
59-
crate::field_testing_suite!(Fr, "from_uniform_bytes", 64);
53+
constants_test!(Fr);
54+
55+
arith_test!(Fr);
56+
legendre_test!(Fr);
57+
test!(arith, Fr, sqrt_test, 1000);
58+
59+
serde_test!(Fr PrimeFieldBits);
60+
from_uniform_bytes_test!(Fr, 1000, L 64, L 48);
6061
}

0 commit comments

Comments
 (0)