Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve: add some macros to generate big testing suite of curves #129

Merged
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
27 commits
Select commit Hold shift + click to select a range
89848d4
refactor: move tests into "tests" module
duguorong009 Jan 15, 2024
80d136a
improve: add edge cases(ONE and ZERO tests) to field testing suite
duguorong009 Jan 16, 2024
61cdc56
feat: add some macros for curve tests (0)
duguorong009 Jan 17, 2024
f965325
feat: add the "curve_testing_suite" macro
duguorong009 Jan 17, 2024
6b5406b
feat: add "test_endo_consistency" to "curve_testing_suite" macro
duguorong009 Jan 18, 2024
655f9de
refactor: small refactoring in "secp256k1" curve tests
duguorong009 Jan 18, 2024
928da66
chore: roll back useless refactorings
duguorong009 Jan 18, 2024
455f0f4
feat: add basic "field_testing_suite" macro
duguorong009 Jan 18, 2024
f58f42b
fix: use the field testing macro for "secp256k1" & "secp256r1"
duguorong009 Jan 18, 2024
7c8501d
fix: replace curve test functions with macros
duguorong009 Jan 19, 2024
42fb4f1
fix: use the exported macro for curve testing
duguorong009 Jan 19, 2024
2f42753
fix: add missing attr to "random_serde_test" macro
duguorong009 Jan 19, 2024
51b49ae
fix: fix the small error in "field_testing_suite" macro
duguorong009 Jan 19, 2024
c64921d
feat: add "endo_consistency" macro branch
duguorong009 Jan 19, 2024
7fcbbc3
feat: add the "endo" macro branch
duguorong009 Jan 19, 2024
64ab44c
feat: add the "ecdsa_example" macro branch
duguorong009 Jan 19, 2024
3238f4d
fix: add type casting in "endo" macro branch
duguorong009 Jan 19, 2024
c5c1d41
feat: add the "map_to_curve" macro branch
duguorong009 Jan 19, 2024
5c7e860
feat: add the "hash_to_curve" macro branch
duguorong009 Jan 19, 2024
762a329
chore: roll back the field testing refactorings
duguorong009 Jan 22, 2024
7db46ab
Merge branch 'main' into gr@refactor-tests
duguorong009 Jan 22, 2024
71069a7
chore: use better naming for macro
duguorong009 Jan 22, 2024
f7f5aa8
fix: update the "endo" testing macro
duguorong009 Jan 22, 2024
d205675
chore: remove the unnecessary "#[cfg(test)]" attrs
duguorong009 Jan 22, 2024
9ee6301
chore: remove duplicates & add comments
duguorong009 Jan 23, 2024
e1c92b5
feat: add the macro to test the curve constants/params
duguorong009 Jan 23, 2024
d13e9dd
chore: remove the leftover(println)
duguorong009 Jan 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 7 additions & 28 deletions src/bn256/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,20 +198,20 @@ impl G1 {
const SVDW_Z: Fq = Fq::ONE;
}

// MUST add `G2` later
// The reason is that "G2::hash_to_curve" is unimplemented.
// Hence, it causes the panic in "test_hash_to_curve".
#[cfg(test)]
mod tests {
crate::tests::curve::curve_testing_suite!(G1);

#[cfg(test)]
mod extra_tests {
use crate::arithmetic::CurveEndo;
use crate::bn256::{Fr, G1, G2};
use crate::CurveExt;
use ff::Field;
use ff::{PrimeField, WithSmallOrderMulGroup};
use rand_core::OsRng;

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<G1>();
}

#[test]
fn test_map_to_curve() {
crate::tests::curve::svdw_map_to_curve_test::<G1>(
Expand Down Expand Up @@ -264,12 +264,6 @@ mod tests {
);
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<G1>();
crate::tests::curve::curve_tests::<G2>();
}

#[test]
fn test_endo() {
let z_impl = Fr::ZETA;
Expand All @@ -282,10 +276,6 @@ mod tests {
assert_eq!(z_impl * z_impl + z_impl, -Fr::ONE);
assert_eq!(z_other * z_other + z_other, -Fr::ONE);

let g = G1::generator();
assert_eq!(g * Fr::ZETA, g.endo());
let g = G2::generator();
assert_eq!(g * Fr::ZETA, g.endo());
for _ in 0..100000 {
let k = Fr::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = G1::decompose_scalar(&k);
Expand All @@ -300,15 +290,4 @@ mod tests {
}
}
}

#[test]
fn test_serialization() {
crate::tests::curve::random_serialization_test::<G1>();
crate::tests::curve::random_serialization_test::<G2>();
#[cfg(feature = "derive_serde")]
{
crate::tests::curve::random_serde_test::<G1>();
crate::tests::curve::random_serde_test::<G2>();
}
}
}
26 changes: 4 additions & 22 deletions src/grumpkin/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,15 @@ impl G1 {
}

#[cfg(test)]
mod tests {
crate::tests::curve::curve_testing_suite!(G1);

#[cfg(test)]
mod extra_tests {
use crate::arithmetic::CurveEndo;
use crate::grumpkin::{Fr, G1};
use crate::CurveExt;
use ff::{Field, PrimeField, WithSmallOrderMulGroup};
use rand_core::OsRng;

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<G1>();
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<G1>();
}

#[test]
fn test_endo() {
let z_impl = Fr::ZETA;
Expand All @@ -120,9 +112,6 @@ mod tests {
assert_eq!(z_impl * z_impl + z_impl, -Fr::ONE);
assert_eq!(z_other * z_other + z_other, -Fr::ONE);

let g = G1::generator();
assert_eq!(g * Fr::ZETA, g.endo());

for _ in 0..100000 {
let k = Fr::random(OsRng);
let (k1, k1_neg, k2, k2_neg) = G1::decompose_scalar(&k);
Expand All @@ -137,11 +126,4 @@ mod tests {
}
}
}

#[test]
fn test_serialization() {
crate::tests::curve::random_serialization_test::<G1>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<G1>();
}
}
48 changes: 5 additions & 43 deletions src/pluto_eris/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -242,46 +242,8 @@ new_curve_impl!(
|_, _| unimplemented!(),
);

#[test]
fn test_curve_pluto() {
crate::tests::curve::curve_tests::<G1>();
}
#[test]
fn test_curve_eris() {
crate::tests::curve::curve_tests::<Eris>();
}
#[test]
fn test_curve_triton() {
crate::tests::curve::curve_tests::<G2>();
}

#[test]
fn test_serialization() {
crate::tests::curve::random_serialization_test::<G1>();
crate::tests::curve::random_serialization_test::<Eris>();
crate::tests::curve::random_serialization_test::<G2>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<G1>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<Eris>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<G2>();
}

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<G1>();
crate::tests::curve::hash_to_curve_test::<Eris>();
}

#[test]
fn test_endo_consistency() {
let g = Eris::generator();
assert_eq!(g * Fp::ZETA, g.endo());

let g = G1::generator();
assert_eq!(g * Fq::ZETA, g.endo());

let g = G2::generator();
assert_eq!(g * Fq::ZETA, g.endo());
}
// MUST add `G2` later
// The reason is that "G2::hash_to_curve" is unimplemented.
// Hence, it causes the panic in "test_hash_to_curve".
#[cfg(test)]
crate::tests::curve::curve_testing_suite!(G1, Eris);
35 changes: 6 additions & 29 deletions src/secp256k1/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -269,39 +269,16 @@ pub(crate) fn iso_map_secp256k1(rp: IsoSecp256k1) -> Secp256k1 {
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<Secp256k1>();
}

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<Secp256k1>();
}

#[test]
fn test_serialization() {
crate::tests::curve::random_serialization_test::<Secp256k1>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<Secp256k1>();
}
crate::tests::curve::curve_testing_suite!(Secp256k1);

#[test]
fn test_endo_consistency() {
let g = Secp256k1::generator();
assert_eq!(g * Fq::ZETA, g.endo());
}
#[cfg(test)]
mod extra_tests {
use super::*;
use ff::FromUniformBytes;
use rand_core::OsRng;

#[test]
fn ecdsa_example() {
use crate::group::Curve;
use crate::CurveAffine;
use ff::FromUniformBytes;
use rand_core::OsRng;

fn mod_n(x: Fp) -> Fq {
let mut x_repr = [0u8; 32];
x_repr.copy_from_slice(x.to_repr().as_ref());
Expand Down
22 changes: 4 additions & 18 deletions src/secp256k1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,10 @@ impl WithSmallOrderMulGroup<3> for Fp {
extend_field_legendre!(Fp);

#[cfg(test)]
mod test {
crate::tests::field::field_testing_suite!(Fp);

#[cfg(test)]
mod extra_tests {
use super::*;
use ff::Field;
use rand_core::OsRng;
Expand Down Expand Up @@ -340,29 +343,12 @@ mod test {
assert_eq!(Fp::ROOT_OF_UNITY_INV, Fp::ROOT_OF_UNITY.invert().unwrap());
}

#[test]
fn test_field() {
crate::tests::field::random_field_tests::<Fp>("secp256k1 base".to_string());
}

#[test]
fn test_conversion() {
crate::tests::field::random_conversion_tests::<Fp>("secp256k1 base".to_string());
}

#[test]
#[cfg(feature = "bits")]
fn test_bits() {
crate::tests::field::random_bits_tests::<Fp>("secp256k1 base".to_string());
}

#[test]
fn test_serialization() {
crate::tests::field::random_serialization_test::<Fp>("secp256k1 base".to_string());
#[cfg(feature = "derive_serde")]
crate::tests::field::random_serde_test::<Fp>("secp256k1 base".to_string());
}

#[test]
fn test_quadratic_residue() {
crate::tests::field::random_quadratic_residue_test::<Fp>();
Expand Down
21 changes: 4 additions & 17 deletions src/secp256k1/fq.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,10 @@ impl WithSmallOrderMulGroup<3> for Fq {
extend_field_legendre!(Fq);

#[cfg(test)]
mod test {
crate::tests::field::field_testing_suite!(Fq);

#[cfg(test)]
mod extra_tests {
use super::*;
use ff::Field;
use rand_core::OsRng;
Expand Down Expand Up @@ -347,28 +350,12 @@ mod test {
assert_eq!(Fq::ROOT_OF_UNITY_INV, Fq::ROOT_OF_UNITY.invert().unwrap());
}

#[test]
fn test_field() {
crate::tests::field::random_field_tests::<Fq>("secp256k1 scalar".to_string());
}

#[test]
fn test_conversion() {
crate::tests::field::random_conversion_tests::<Fq>("secp256k1 scalar".to_string());
}

#[test]
#[cfg(feature = "bits")]
fn test_bits() {
crate::tests::field::random_bits_tests::<Fq>("secp256k1 scalar".to_string());
}

#[test]
fn test_serialization() {
crate::tests::field::random_serialization_test::<Fq>("secp256k1 scalar".to_string());
#[cfg(feature = "derive_serde")]
crate::tests::field::random_serde_test::<Fq>("secp256k1 scalar".to_string());
}
#[test]
fn test_quadratic_residue() {
crate::tests::field::random_quadratic_residue_test::<Fq>();
Expand Down
22 changes: 4 additions & 18 deletions src/secp256r1/curve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,30 +92,16 @@ impl Secp256r1 {
}

#[cfg(test)]
mod tests {
crate::tests::curve::curve_testing_suite!(Secp256r1);

#[cfg(test)]
mod extra_tests {
use super::*;
use crate::group::Curve;
use crate::secp256r1::{Fp, Fq, Secp256r1};
use ff::FromUniformBytes;
use rand_core::OsRng;

#[test]
fn test_hash_to_curve() {
crate::tests::curve::hash_to_curve_test::<Secp256r1>();
}

#[test]
fn test_curve() {
crate::tests::curve::curve_tests::<Secp256r1>();
}

#[test]
fn test_serialization() {
crate::tests::curve::random_serialization_test::<Secp256r1>();
#[cfg(feature = "derive_serde")]
crate::tests::curve::random_serde_test::<Secp256r1>();
}

#[test]
fn ecdsa_example() {
fn mod_n(x: Fp) -> Fq {
Expand Down
22 changes: 4 additions & 18 deletions src/secp256r1/fp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,10 @@ impl WithSmallOrderMulGroup<3> for Fp {
extend_field_legendre!(Fp);

#[cfg(test)]
mod test {
crate::tests::field::field_testing_suite!(Fp);

#[cfg(test)]
mod extra_tests {
use super::*;
use ff::Field;
use rand_core::OsRng;
Expand Down Expand Up @@ -358,29 +361,12 @@ mod test {
assert_eq!(Fp::ROOT_OF_UNITY_INV, Fp::ROOT_OF_UNITY.invert().unwrap());
}

#[test]
fn test_field() {
crate::tests::field::random_field_tests::<Fp>("secp256r1 base".to_string());
}

#[test]
fn test_conversion() {
crate::tests::field::random_conversion_tests::<Fp>("secp256r1 base".to_string());
}

#[test]
#[cfg(feature = "bits")]
fn test_bits() {
crate::tests::field::random_bits_tests::<Fp>("secp256r1 base".to_string());
}

#[test]
fn test_serialization() {
crate::tests::field::random_serialization_test::<Fp>("secp256r1 base".to_string());
#[cfg(feature = "derive_serde")]
crate::tests::field::random_serde_test::<Fp>("secp256r1 base".to_string());
}

#[test]
fn test_quadratic_residue() {
crate::tests::field::random_quadratic_residue_test::<Fp>();
Expand Down
Loading