Skip to content

Commit

Permalink
feat: add the macro branch for testing constants
Browse files Browse the repository at this point in the history
  • Loading branch information
duguorong009 committed Jan 25, 2024
1 parent 8fa2c06 commit 4dfc897
Show file tree
Hide file tree
Showing 10 changed files with 31 additions and 118 deletions.
1 change: 1 addition & 0 deletions src/bn256/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ mod test {
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", MODULUS_STR);

use crate::ff_ext::Legendre;
use rand_core::OsRng;
Expand Down
1 change: 1 addition & 0 deletions src/bn256/fq2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -418,6 +418,7 @@ impl From<u64> for Fq2 {
}
}

// This trait is only implemented to satisfy the requirement of CurveExt
impl PrimeField for Fq2 {
type Repr = Fq2Bytes;

Expand Down
7 changes: 1 addition & 6 deletions src/bn256/fr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -339,6 +339,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand All @@ -360,12 +361,6 @@ mod test {
}
}

#[test]
fn test_delta() {
assert_eq!(Fr::DELTA, GENERATOR.pow([1u64 << Fr::S]));
assert_eq!(Fr::DELTA, Fr::MULTIPLICATIVE_GENERATOR.pow([1u64 << Fr::S]));
}

#[test]
fn test_from_u512() {
assert_eq!(
Expand Down
7 changes: 1 addition & 6 deletions src/pluto_eris/fields/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand All @@ -429,12 +430,6 @@ mod test {
}
}

#[test]
fn test_delta() {
assert_eq!(Fp::DELTA, GENERATOR.pow([1u64 << Fp::S]));
assert_eq!(Fp::DELTA, Fp::MULTIPLICATIVE_GENERATOR.pow([1u64 << Fp::S]));
}

#[test]
fn test_zeta() {
assert_eq!(Fp::ZETA * Fp::ZETA * Fp::ZETA, Fp::ONE);
Expand Down
7 changes: 1 addition & 6 deletions src/pluto_eris/fields/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand Down Expand Up @@ -426,12 +427,6 @@ mod test {
assert_ne!(Fq::ZETA * Fq::ZETA, Fq::ONE);
}

#[test]
fn test_delta() {
assert_eq!(Fq::DELTA, GENERATOR.pow([1u64 << Fq::S]));
assert_eq!(Fq::DELTA, Fq::MULTIPLICATIVE_GENERATOR.pow([1u64 << Fq::S]));
}

#[test]
fn test_from_u512() {
const N_VECS: usize = 10;
Expand Down
26 changes: 1 addition & 25 deletions src/secp256k1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand All @@ -320,29 +321,4 @@ mod test {
assert!(a == b || a == negb);
}
}

#[test]
fn test_constants() {
assert_eq!(
Fp::MODULUS,
"0xfffffffffffffffffffffffffffffffffffffffffffffffffffffffefffffc2f",
);

assert_eq!(Fp::from(2) * Fp::TWO_INV, Fp::ONE);
}

#[test]
fn test_delta() {
assert_eq!(Fp::DELTA, MULTIPLICATIVE_GENERATOR.pow([1u64 << Fp::S]));
}

#[test]
fn test_root_of_unity() {
assert_eq!(Fp::ROOT_OF_UNITY.pow_vartime([1 << Fp::S]), Fp::one());
}

#[test]
fn test_inv_root_of_unity() {
assert_eq!(Fp::ROOT_OF_UNITY_INV, Fp::ROOT_OF_UNITY.invert().unwrap());
}
}
26 changes: 1 addition & 25 deletions src/secp256k1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand All @@ -327,29 +328,4 @@ mod test {
assert!(a == b || a == negb);
}
}

#[test]
fn test_constants() {
assert_eq!(
Fq::MODULUS,
"0xfffffffffffffffffffffffffffffffebaaedce6af48a03bbfd25e8cd0364141",
);

assert_eq!(Fq::from(2) * Fq::TWO_INV, Fq::ONE);
}

#[test]
fn test_delta() {
assert_eq!(Fq::DELTA, Fq::MULTIPLICATIVE_GENERATOR.pow([1u64 << Fq::S]));
}

#[test]
fn test_root_of_unity() {
assert_eq!(Fq::ROOT_OF_UNITY.pow_vartime([1 << Fq::S]), Fq::one());
}

#[test]
fn test_inv_root_of_unity() {
assert_eq!(Fq::ROOT_OF_UNITY_INV, Fq::ROOT_OF_UNITY.invert().unwrap());
}
}
26 changes: 1 addition & 25 deletions src/secp256r1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,6 +317,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand All @@ -338,29 +339,4 @@ mod test {
assert!(a == b || a == negb);
}
}

#[test]
fn test_constants() {
assert_eq!(
Fp::MODULUS,
"0xffffffff00000001000000000000000000000000ffffffffffffffffffffffff",
);

assert_eq!(Fp::from(2) * Fp::TWO_INV, Fp::ONE);
}

#[test]
fn test_delta() {
assert_eq!(Fp::DELTA, MULTIPLICATIVE_GENERATOR.pow([1u64 << Fp::S]));
}

#[test]
fn test_root_of_unity() {
assert_eq!(Fp::ROOT_OF_UNITY.pow_vartime([1 << Fp::S]), Fp::one());
}

#[test]
fn test_inv_root_of_unity() {
assert_eq!(Fp::ROOT_OF_UNITY_INV, Fp::ROOT_OF_UNITY.invert().unwrap());
}
}
26 changes: 1 addition & 25 deletions src/secp256r1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,6 +306,7 @@ mod test {
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", MODULUS_STR);

use rand_core::OsRng;

Expand Down Expand Up @@ -333,29 +334,4 @@ mod test {
assert!(a == b || a == negb);
}
}

#[test]
fn test_constants() {
assert_eq!(
Fq::MODULUS,
"0xffffffff00000000ffffffffffffffffbce6faada7179e84f3b9cac2fc632551",
);

assert_eq!(Fq::from(2) * Fq::TWO_INV, Fq::ONE);
}

#[test]
fn test_delta() {
assert_eq!(Fq::DELTA, Fq::MULTIPLICATIVE_GENERATOR.pow([1u64 << Fq::S]));
}

#[test]
fn test_root_of_unity() {
assert_eq!(Fq::ROOT_OF_UNITY.pow_vartime([1 << Fq::S]), Fq::one());
}

#[test]
fn test_inv_root_of_unity() {
assert_eq!(Fq::ROOT_OF_UNITY_INV, Fq::ROOT_OF_UNITY.invert().unwrap());
}
}
22 changes: 22 additions & 0 deletions src/tests/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -439,4 +439,26 @@ macro_rules! field_testing_suite {
end_timer!(start);
}
};

($field: ident, "constants", $modulus_str: expr) => {
#[test]
fn test_primefield_constants() {
assert_eq!($field::MODULUS, $modulus_str);
assert_eq!(
$field::ROOT_OF_UNITY_INV,
$field::ROOT_OF_UNITY.invert().unwrap()
);
assert_eq!($field::from(2) * $field::TWO_INV, $field::ONE);
if $field::S != 0 {
assert_eq!(
$field::ROOT_OF_UNITY.pow_vartime([1 << $field::S]),
$field::one()
);
assert_eq!(
$field::DELTA,
$field::MULTIPLICATIVE_GENERATOR.pow([1u64 << $field::S])
);
}
}
};
}

0 comments on commit 4dfc897

Please sign in to comment.