From 5b06cce45622ce3e9c3fcf9a7e0579625a31147d Mon Sep 17 00:00:00 2001 From: duguorong <80258679+duguorong009@users.noreply.github.com> Date: Fri, 26 Jan 2024 00:17:09 +0800 Subject: [PATCH] feat: add the macro branch to test the "frobenius" --- src/bn256/fq12.rs | 41 ++++++++---------------------- src/bn256/fq2.rs | 38 ++++++++------------------- src/bn256/fq6.rs | 41 ++++++++---------------------- src/pluto_eris/fields/fp12.rs | 47 ++++++++++------------------------ src/pluto_eris/fields/fp2.rs | 44 ++++++++++---------------------- src/pluto_eris/fields/fp6.rs | 48 ++++++++++------------------------- src/tests/field.rs | 30 +++++++++++++++++++--- 7 files changed, 102 insertions(+), 187 deletions(-) diff --git a/src/bn256/fq12.rs b/src/bn256/fq12.rs index b44ba7ee..bef81af4 100644 --- a/src/bn256/fq12.rs +++ b/src/bn256/fq12.rs @@ -574,38 +574,19 @@ fn test_squaring() { } } -#[test] -fn test_frobenius() { - let mut rng = XorShiftRng::from_seed([ - 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, - 0xe5, - ]); - - for _ in 0..100 { - for i in 0..14 { - let mut a = Fq12::random(&mut rng); - let mut b = a; - - for _ in 0..i { - a = a.pow_vartime([ - 0x3c208c16d87cfd47, - 0x97816a916871ca8d, - 0xb85045b68181585d, - 0x30644e72e131a029, - ]); - } - b.frobenius_map(i); - - assert_eq!(a, b); - } - } -} - #[cfg(test)] mod test { use super::*; crate::field_testing_suite!(Fq12, "field_arithmetic"); - // crate::field_testing_suite!(Fq12, "conversion"); - // crate::field_testing_suite!(Fq12, "serialization"); - // crate::field_testing_suite!(Fq12, "quadratic_residue"); + // extension field-specific + crate::field_testing_suite!( + Fq12, + "frobenius", + [ + 0x3c208c16d87cfd47, + 0x97816a916871ca8d, + 0xb85045b68181585d, + 0x30644e72e131a029, + ] + ); } diff --git a/src/bn256/fq2.rs b/src/bn256/fq2.rs index 6eb5f3b4..9aff70c4 100644 --- a/src/bn256/fq2.rs +++ b/src/bn256/fq2.rs @@ -663,33 +663,6 @@ fn test_fq2_mul_nonresidue() { } } -#[test] -fn test_frobenius() { - let mut rng = XorShiftRng::from_seed([ - 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, - 0xe5, - ]); - - for _ in 0..100 { - for i in 0..14 { - let mut a = Fq2::random(&mut rng); - let mut b = a; - - for _ in 0..i { - a = a.pow([ - 0x3c208c16d87cfd47, - 0x97816a916871ca8d, - 0xb85045b68181585d, - 0x30644e72e131a029, - ]); - } - b.frobenius_map(i); - - assert_eq!(a, b); - } - } -} - #[cfg(test)] mod test { use super::*; @@ -699,4 +672,15 @@ mod test { 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, + "frobenius", + [ + 0x3c208c16d87cfd47, + 0x97816a916871ca8d, + 0xb85045b68181585d, + 0x30644e72e131a029, + ] + ); } diff --git a/src/bn256/fq6.rs b/src/bn256/fq6.rs index 9736d4af..3135606a 100644 --- a/src/bn256/fq6.rs +++ b/src/bn256/fq6.rs @@ -676,38 +676,19 @@ fn test_squaring() { } } -#[test] -fn test_frobenius() { - let mut rng = XorShiftRng::from_seed([ - 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, - 0xe5, - ]); - - for _ in 0..100 { - for i in 0..14 { - let mut a = Fq6::random(&mut rng); - let mut b = a; - - for _ in 0..i { - a = a.pow_vartime([ - 0x3c208c16d87cfd47, - 0x97816a916871ca8d, - 0xb85045b68181585d, - 0x30644e72e131a029, - ]); - } - b.frobenius_map(i); - - assert_eq!(a, b); - } - } -} - #[cfg(test)] mod test { use super::*; crate::field_testing_suite!(Fq6, "field_arithmetic"); - // crate::field_testing_suite!(Fq6, "conversion"); - // crate::field_testing_suite!(Fq6, "serialization"); - // crate::field_testing_suite!(Fq6, "quadratic_residue"); + // extension field-specific + crate::field_testing_suite!( + Fq6, + "frobenius", + [ + 0x3c208c16d87cfd47, + 0x97816a916871ca8d, + 0xb85045b68181585d, + 0x30644e72e131a029, + ] + ); } diff --git a/src/pluto_eris/fields/fp12.rs b/src/pluto_eris/fields/fp12.rs index ce4f208c..652ac4a2 100644 --- a/src/pluto_eris/fields/fp12.rs +++ b/src/pluto_eris/fields/fp12.rs @@ -630,41 +630,22 @@ fn test_squaring() { } } -#[test] -fn test_frobenius() { - let mut rng = XorShiftRng::from_seed([ - 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, - 0xe5, - ]); - - for _ in 0..50 { - for i in 0..13 { - let mut a = Fp12::random(&mut rng); - let mut b = a; - - for _ in 0..i { - a = a.pow_vartime([ - 0x9ffffcd300000001, - 0xa2a7e8c30006b945, - 0xe4a7a5fe8fadffd6, - 0x443f9a5cda8a6c7b, - 0xa803ca76f439266f, - 0x0130e0000d7f70e4, - 0x2400000000002400, - ]); - } - b.frobenius_map(i); - - assert_eq!(a, b); - } - } -} - #[cfg(test)] mod test { use super::*; crate::field_testing_suite!(Fp12, "field_arithmetic"); - // crate::field_testing_suite!(Fp12, "conversion"); - // crate::field_testing_suite!(Fp12, "serialization"); - // crate::field_testing_suite!(Fp12, "quadratic_residue"); + // extension field-specific + crate::field_testing_suite!( + Fp12, + "frobenius", + [ + 0x9ffffcd300000001, + 0xa2a7e8c30006b945, + 0xe4a7a5fe8fadffd6, + 0x443f9a5cda8a6c7b, + 0xa803ca76f439266f, + 0x0130e0000d7f70e4, + 0x2400000000002400, + ] + ); } diff --git a/src/pluto_eris/fields/fp2.rs b/src/pluto_eris/fields/fp2.rs index 5091eefe..8a984698 100644 --- a/src/pluto_eris/fields/fp2.rs +++ b/src/pluto_eris/fields/fp2.rs @@ -689,36 +689,6 @@ fn test_fp2_mul_nonresidue() { } } -#[test] -fn test_frobenius() { - let mut rng = XorShiftRng::from_seed([ - 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, - 0xe5, - ]); - - for _ in 0..50 { - for i in 0..8 { - let mut a = Fp2::random(&mut rng); - let mut b = a; - - for _ in 0..i { - a = a.pow_vartime([ - 0x9ffffcd300000001, - 0xa2a7e8c30006b945, - 0xe4a7a5fe8fadffd6, - 0x443f9a5cda8a6c7b, - 0xa803ca76f439266f, - 0x0130e0000d7f70e4, - 0x2400000000002400, - ]); - } - b.frobenius_map(i); - - assert_eq!(a, b); - } - } -} - #[cfg(test)] mod test { use super::*; @@ -728,4 +698,18 @@ mod test { 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, + "frobenius", + [ + 0x9ffffcd300000001, + 0xa2a7e8c30006b945, + 0xe4a7a5fe8fadffd6, + 0x443f9a5cda8a6c7b, + 0xa803ca76f439266f, + 0x0130e0000d7f70e4, + 0x2400000000002400, + ] + ); } diff --git a/src/pluto_eris/fields/fp6.rs b/src/pluto_eris/fields/fp6.rs index b117a317..2296e2f7 100644 --- a/src/pluto_eris/fields/fp6.rs +++ b/src/pluto_eris/fields/fp6.rs @@ -749,42 +749,22 @@ fn test_squaring() { } } -#[test] -fn test_frobenius() { - let mut rng = XorShiftRng::from_seed([ - 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, - 0xe5, - ]); - - for _ in 0..50 { - for i in 0..8 { - let mut a = Fp6::random(&mut rng); - let mut b = a; - - for _ in 0..i { - a = a.pow_vartime([ - // p - 0x9ffffcd300000001, - 0xa2a7e8c30006b945, - 0xe4a7a5fe8fadffd6, - 0x443f9a5cda8a6c7b, - 0xa803ca76f439266f, - 0x0130e0000d7f70e4, - 0x2400000000002400, - ]); - } - b.frobenius_map(i); - - assert_eq!(a, b); - } - } -} - #[cfg(test)] mod test { use super::*; crate::field_testing_suite!(Fp6, "field_arithmetic"); - // crate::field_testing_suite!(Fp6, "conversion"); - // crate::field_testing_suite!(Fp6, "serialization"); - // crate::field_testing_suite!(Fp6, "quadratic_residue"); + // extension field-specific + crate::field_testing_suite!( + Fp6, + "frobenius", + [ + 0x9ffffcd300000001, + 0xa2a7e8c30006b945, + 0xe4a7a5fe8fadffd6, + 0x443f9a5cda8a6c7b, + 0xa803ca76f439266f, + 0x0130e0000d7f70e4, + 0x2400000000002400, + ] + ); } diff --git a/src/tests/field.rs b/src/tests/field.rs index 2e488a84..9843958d 100644 --- a/src/tests/field.rs +++ b/src/tests/field.rs @@ -510,15 +510,39 @@ macro_rules! field_testing_suite { } }; - ($field: ident, "zeta" $(, $prime_field: ident)*) => { + ($field: ident, "zeta" $(, $base_field: ident)*) => { #[test] fn test_zeta() { assert_eq!($field::ZETA * $field::ZETA * $field::ZETA, $field::ONE); assert_ne!($field::ZETA * $field::ZETA, $field::ONE); $( - let zeta = $field::new($prime_field::ZETA.square(), $prime_field::zero()); + let zeta = $field::new($base_field::ZETA.square(), $base_field::zero()); assert_eq!(zeta, $field::ZETA); )* } - } + }; + + ($ext_field: ident, "frobenius", $frobenius_param: expr) => { + #[test] + fn test_frobenius() { + let mut rng = XorShiftRng::from_seed([ + 0x59, 0x62, 0xbe, 0x5d, 0x76, 0x3d, 0x31, 0x8d, 0x17, 0xdb, 0x37, 0x32, 0x54, 0x06, 0xbc, + 0xe5, + ]); + + for _ in 0..50 { + for i in 0..8 { + let mut a = $ext_field::random(&mut rng); + let mut b = a; + + for _ in 0..i { + a = a.pow($frobenius_param); + } + b.frobenius_map(i); + + assert_eq!(a, b); + } + } + } + }; }