Skip to content

Commit

Permalink
feat: add the macro branch to test the "frobenius"
Browse files Browse the repository at this point in the history
  • Loading branch information
duguorong009 committed Jan 25, 2024
1 parent ddfbaee commit 5b06cce
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 187 deletions.
41 changes: 11 additions & 30 deletions src/bn256/fq12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
38 changes: 11 additions & 27 deletions src/bn256/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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,
]
);
}
41 changes: 11 additions & 30 deletions src/bn256/fq6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
47 changes: 14 additions & 33 deletions src/pluto_eris/fields/fp12.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
44 changes: 14 additions & 30 deletions src/pluto_eris/fields/fp2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::*;
Expand All @@ -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,
]
);
}
48 changes: 14 additions & 34 deletions src/pluto_eris/fields/fp6.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
]
);
}
30 changes: 27 additions & 3 deletions src/tests/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
}
};
}

0 comments on commit 5b06cce

Please sign in to comment.