Skip to content

Commit

Permalink
refactor: cleaner test_frobenius macro
Browse files Browse the repository at this point in the history
  • Loading branch information
davidnevadoc committed Sep 12, 2024
1 parent 728f3e5 commit d087b08
Show file tree
Hide file tree
Showing 10 changed files with 19 additions and 100 deletions.
2 changes: 1 addition & 1 deletion src/bls12381/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -294,5 +294,5 @@ mod test {
setup_f12_test_funcs!(Fq12, Fq6, Fq2);
test_fq12!(f12_mul_by_014_, 500);
test_fq12!(f12_mul_by_034_, 500);
test_frobenius!(Fq12, 8, Fq::MODULUS_LIMBS);
test_frobenius!(Fq12, Fq, 8);
}
14 changes: 3 additions & 11 deletions src/bls12381/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl ExtField for Fq2 {
mod test {

use super::*;
use crate::{arith_test, legendre_test, serde_test, test};
use crate::{arith_test, f2_tests, legendre_test, serde_test, test, test_frobenius};
use rand_core::RngCore;

// constants_test!(Fq2);
Expand All @@ -77,16 +77,8 @@ mod test {
serde_test!(Fq2);
// test_uniform_bytes!(Fq2, 1000, L 96);

crate::f2_tests!(Fq2, Fq);
crate::test_frobenius!(
Fq2,
20,
// 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
);
f2_tests!(Fq2, Fq);
test_frobenius!(Fq2, Fq, 20);

#[test]
fn test_fq2_mul_nonresidue() {
Expand Down
2 changes: 1 addition & 1 deletion src/bls12381/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ mod test {
test_fq6!(f6_mul_nonresidue_, 1000);
test_fq6!(f6_mul_by_1_, 1000);
test_fq6!(f6_mul_by_01_, 1000);
test_frobenius!(Fq6, 10, Fq::MODULUS_LIMBS);
test_frobenius!(Fq6, Fq, 10);

#[test]
fn test_fq6_mul_nonresidue() {
Expand Down
11 changes: 1 addition & 10 deletions src/bn256/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,14 +222,5 @@ mod test {
setup_f12_test_funcs!(Fq12, Fq6, Fq2);
test_fq12!(f12_mul_by_014_, 500);
test_fq12!(f12_mul_by_034_, 500);
test_frobenius!(
Fq12,
8,
[
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
0x30644e72e131a029,
]
);
test_frobenius!(Fq12, Fq, 8);
}
15 changes: 3 additions & 12 deletions src/bn256/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl ExtField for Fq2 {
mod test {

use super::*;
use crate::{arith_test, legendre_test, serde_test, test};
use crate::{arith_test, f2_tests, legendre_test, serde_test, test, test_frobenius};
use rand_core::RngCore;

// constants_test!(Fq2);
Expand All @@ -80,17 +80,8 @@ mod test {
serde_test!(Fq2);
// test_uniform_bytes!(Fq2, 1000, L 96);

crate::f2_tests!(Fq2, Fq);
crate::test_frobenius!(
Fq2,
20,
[
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
0x30644e72e131a029,
]
);
f2_tests!(Fq2, Fq);
test_frobenius!(Fq2, Fq, 20);

#[test]
fn test_fq2_squaring() {
Expand Down
11 changes: 1 addition & 10 deletions src/bn256/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -226,15 +226,6 @@ mod test {
test_fq6!(f6_mul_nonresidue_, 1000);
test_fq6!(f6_mul_by_1_, 1000);
test_fq6!(f6_mul_by_01_, 1000);
test_frobenius!(
Fq6,
10,
[
0x3c208c16d87cfd47,
0x97816a916871ca8d,
0xb85045b68181585d,
0x30644e72e131a029
]
);
test_frobenius!(Fq6, Fq, 10);
// test_uniform_bytes!(Fq6, 1000, L 96);
}
17 changes: 1 addition & 16 deletions src/pluto_eris/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -280,20 +280,5 @@ mod test {
setup_f12_test_funcs!(Fp12, Fp6, Fp2);
test_fp12!(f12_mul_by_014_, 500);
test_fp12!(f12_mul_by_034_, 500);
test_frobenius!(
Fp12,
8,
// ϕ: E → E
// (x, y) ↦ (x^p, y^p)
// p: modulus of base field (Here, Fp::MODULUS)
[
0x9ffffcd300000001,
0xa2a7e8c30006b945,
0xe4a7a5fe8fadffd6,
0x443f9a5cda8a6c7b,
0xa803ca76f439266f,
0x0130e0000d7f70e4,
0x2400000000002400,
]
);
test_frobenius!(Fp12, Fp, 8);
}
18 changes: 3 additions & 15 deletions src/pluto_eris/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,19 +91,7 @@ mod test {
// test_uniform_bytes!(Fp2, 1000, L 96);

crate::f2_tests!(Fp2, Fp);
crate::test_frobenius!(
Fp2,
20,
[
0x9ffffcd300000001,
0xa2a7e8c30006b945,
0xe4a7a5fe8fadffd6,
0x443f9a5cda8a6c7b,
0xa803ca76f439266f,
0x0130e0000d7f70e4,
0x2400000000002400,
]
);
crate::test_frobenius!(Fp2, Fp, 20);

#[test]
fn test_fp2_squaring() {
Expand Down Expand Up @@ -148,9 +136,9 @@ mod test {
0xbc, 0xe5,
]);
for _ in 0..1000 {
let a = Fp2::random(&mut rng);
let mut a = Fp2::random(&mut rng);
let mut b = a;
a.mul_by_nonresidue();
a = a.mul_by_nonresidue();
b.mul_assign(&Fp2::NON_RESIDUE);

assert_eq!(a, b);
Expand Down
24 changes: 3 additions & 21 deletions src/pluto_eris/fp6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -273,31 +273,13 @@ mod test {
test_fp6!(f6_mul_nonresidue_, 1000);
test_fp6!(f6_mul_by_1_, 1000);
test_fp6!(f6_mul_by_01_, 1000);
test_frobenius!(
Fp6,
10,
[
0x9ffffcd300000001,
0xa2a7e8c30006b945,
0xe4a7a5fe8fadffd6,
0x443f9a5cda8a6c7b,
0xa803ca76f439266f,
0x0130e0000d7f70e4,
0x2400000000002400,
]
);
test_frobenius!(Fp6, Fp, 10);

#[test]
fn test_fq2_mul_nonresidue() {
let nqr = Fp6 {
c0: Fp2::ZERO,
c1: Fp2::ONE,
c2: Fp2::ZERO,
};

fn test_fp6_mul_nonresidue() {
let e = Fp6::random(rand_core::OsRng);
let a0 = e.mul_by_nonresidue();
let a1 = e * nqr;
let a1 = e * Fp6::NON_RESIDUE;

assert_eq!(a0, a1);
}
Expand Down
5 changes: 2 additions & 3 deletions src/tests/field/extensions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,6 @@ macro_rules! setup_f12_test_funcs {
c2: $base_field_2::zero(),
},
});

assert_eq!(a, b);
}
}
Expand All @@ -179,15 +178,15 @@ macro_rules! setup_f12_test_funcs {

#[macro_export]
macro_rules! test_frobenius {
($field:ident, $size: expr, $frobenius_param: expr) => {
($field:ident, $base: ident, $size: expr) => {
fn test_frobenius(mut rng: impl RngCore, n: usize) {
for _ in 0..n {
for i in 0..12 {
let mut a = $field::random(&mut rng);
let mut b = a;

for _ in 0..i {
a = a.pow($frobenius_param);
a = a.pow($base::MODULUS_LIMBS);
}
b.frobenius_map(i);

Expand Down

0 comments on commit d087b08

Please sign in to comment.