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

Conversation

duguorong009
Copy link
Contributor

@duguorong009 duguorong009 commented Jan 18, 2024

Description

  • Create some macros to generate the big testing suites for all of curves in the lib

Related issues

Changes

  • Add the curve_testing_suite macro for tests of curves
  • Create new macros for testing
  • Replace the functions used for testing, with new macros

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should also be testing the PrimeFiedBits remember! Also we need to see if for BN is necessary to add further testing for lookup-based ops or small scalars.
Also, each curve & Field should have tests for their associated constants to check they're correct.
Make sure all of the constants are indeed checked! :)

Aside from that, the rest looks really nice! I like the direction this is going towards!
Just left some comments :)

src/tests/curve.rs Outdated Show resolved Hide resolved
src/tests/curve.rs Outdated Show resolved Hide resolved
src/tests/curve.rs Outdated Show resolved Hide resolved
@CPerezz
Copy link
Member

CPerezz commented Jan 19, 2024

Changes look much better @duguorong009 !

I like them a lot! Let's try to extend it to the Field tests and see how the solution looks like!

@duguorong009
Copy link
Contributor Author

@CPerezz
Please check the current status of the PR when you are free.
I've implemented all of macros for curve testing.
I'd like to first complete the curve testing part before moving to field testing.

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of having a #[cfg(test)] on each test fn,
can't you have a module which is under the test flag and have all the macros without it inside.

I talk about thinks like this:

#[cfg(test)]
crate::curve_testing_suite!(G1, G2);
#[cfg(test)]
crate::curve_testing_suite!(G1, "hash_to_curve");
#[cfg(test)]
crate::curve_testing_suite!(G1, "endo_consistency");
#[cfg(test)]
crate::curve_testing_suite!(
G1,
"endo",
[
0x8b17ea66b99c90dd,
0x5bfc41088d8daaa7,
0xb3c4d79d41a91758,
0x00,
]
);
#[cfg(test)]
crate::curve_testing_suite!(
G1,
"svdw_map_to_curve",
(
// Precomputed constants taken from https://github.com/ConsenSys/gnark-crypto/blob/441dc0ffe639294b8d09e394f24ba7575577229c/internal/generator/config/bn254.go#L26-L32.
[
"4",
"10944121435919637611123202872628637544348155578648911831344518947322613104291",
"8815841940592487685674414971303048083897117035520822607866",
"7296080957279758407415468581752425029565437052432607887563012631548408736189",
],
// List of (u, (Q.x, Q.y)) taken from https://github.com/ConsenSys/gnark-crypto/blob/441dc0ffe639294b8d09e394f24ba7575577229c/ecc/bn254/hash_vectors_test.go#L4-L28
[
(
"0xcb81538a98a2e3580076eed495256611813f6dae9e16d3d4f8de7af0e9833e1",
(
"0x1bb8810e2ceaf04786d4efd216fc2820ddd9363712efc736ada11049d8af5925",
"0x1efbf8d54c60d865cce08437668ea30f5bf90d287dbd9b5af31da852915e8f11",
),
),
(
"0xba35e127276e9000b33011860904ddee28f1d48ddd3577e2a797ef4a5e62319",
(
"0xda4a96147df1f35b0f820bd35c6fac3b80e8e320de7c536b1e054667b22c332",
"0x189bd3fbffe4c8740d6543754d95c790e44cd2d162858e3b733d2b8387983bb7",
),
),
(
"0x11852286660cd970e9d7f46f99c7cca2b75554245e91b9b19d537aa6147c28fc",
(
"0x2ff727cfaaadb3acab713fa22d91f5fddab3ed77948f3ef6233d7ea9b03f4da1",
"0x304080768fd2f87a852155b727f97db84b191e41970506f0326ed4046d1141aa",
),
),
(
"0x174d1c85d8a690a876cc1deba0166d30569fafdb49cb3ed28405bd1c5357a1cc",
(
"0x11a2eaa8e3e89de056d1b3a288a7f733c8a1282efa41d28e71af065ab245df9b",
"0x60f37c447ac29fd97b9bb83be98ddccf15e34831a9cdf5493b7fede0777ae06",
),
),
(
"0x73b81432b4cf3a8a9076201500d1b94159539f052a6e0928db7f2df74bff672",
(
"0x27409dccc6ee4ce90e24744fda8d72c0bc64e79766f778da0c1c0ef1c186ea84",
"0x1ac201a542feca15e77f30370da183514dc99d8a0b2c136d64ede35cd0b51dc0",
),
),
]
)
);

Which hold a lot of unnecessary #[cfg(test)].

Comment on lines 214 to 219
[
0x8b17ea66b99c90dd,
0x5bfc41088d8daaa7,
0xb3c4d79d41a91758,
0x00,
]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we import this from the EndoParams directly?
I see it risky to duplicate constants arround.
Also, please add some comments if there's no alternative to duplication in order to know what that is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um, @CPerezz
Can you help me here?
I wanted to add the comments related to z_other.
But, I failed to find the parameter or algorithm related to ZETA and z_other. 😢
(Just updated the "endo" testing macro to receive the z_other param as optional here)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can't we compute it by ourseleves here or something similar? I think it makes it complex if we indeed need to pass more parameters to the macro.

Maybe if it gets too complex we can leave it as is and adding a comment saying how the number was obtained.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the z_other in this test quite absurd.
It is checking that a variable that is declared in the test is the other 3-root of unity and nothing else.
It doesn't even check that is not the value being used as ZETA.
I would remove this variable altogether and remove it from the macro as well.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I missed the outdated tag, just saw that you did exactly this. Great! :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@CPerezz @davidnevadoc
Here, I tried to add the comments. 9ee6301

G1,
"endo",
[
0xe4bd44e5607cfd48,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ditto

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed z_other since it is ZETA, in essence. f7f5aa8

assert_eq!(affine_point, affine_point_rec);
}
}
macro_rules! projective_to_affine_affine_to_projective {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
macro_rules! projective_to_affine_affine_to_projective {
macro_rules! projective_affine_roundtrip {

Is shorter and means the same

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done 71069a7

@duguorong009
Copy link
Contributor Author

duguorong009 commented Jan 22, 2024

Thanks for the comments! @CPerezz
Will take care of them.

BTW, I think it is better to add only changes for curve testing, in this PR.
And we add the field testing macros in separate PR.
I'm afraid the PR would be unnecessarily HUGE at last.
What do you think?

@CPerezz
Copy link
Member

CPerezz commented Jan 22, 2024

Thanks for the comments! @CPerezz Will take care of them.

BTW, I think it is better to add only changes for curve testing, in this PR. And we add the field testing macros in separate PR. I'm afraid the PR would be unnecessarily HUGE at last. What do you think?

Sounds good to me!!!

Address all the stuff and set this to ready and I'll review ASAP! On the meantime, you can branch out from here and start the Field testing work if you want!

@duguorong009
Copy link
Contributor Author

Instead of having a #[cfg(test)] on each test fn, can't you have a module which is under the test flag and have all the macros without it inside.

I talk about thinks like this:

#[cfg(test)]
crate::curve_testing_suite!(G1, G2);
#[cfg(test)]
crate::curve_testing_suite!(G1, "hash_to_curve");
#[cfg(test)]
crate::curve_testing_suite!(G1, "endo_consistency");
#[cfg(test)]
crate::curve_testing_suite!(
G1,
"endo",
[
0x8b17ea66b99c90dd,
0x5bfc41088d8daaa7,
0xb3c4d79d41a91758,
0x00,
]
);
#[cfg(test)]
crate::curve_testing_suite!(
G1,
"svdw_map_to_curve",
(
// Precomputed constants taken from https://github.com/ConsenSys/gnark-crypto/blob/441dc0ffe639294b8d09e394f24ba7575577229c/internal/generator/config/bn254.go#L26-L32.
[
"4",
"10944121435919637611123202872628637544348155578648911831344518947322613104291",
"8815841940592487685674414971303048083897117035520822607866",
"7296080957279758407415468581752425029565437052432607887563012631548408736189",
],
// List of (u, (Q.x, Q.y)) taken from https://github.com/ConsenSys/gnark-crypto/blob/441dc0ffe639294b8d09e394f24ba7575577229c/ecc/bn254/hash_vectors_test.go#L4-L28
[
(
"0xcb81538a98a2e3580076eed495256611813f6dae9e16d3d4f8de7af0e9833e1",
(
"0x1bb8810e2ceaf04786d4efd216fc2820ddd9363712efc736ada11049d8af5925",
"0x1efbf8d54c60d865cce08437668ea30f5bf90d287dbd9b5af31da852915e8f11",
),
),
(
"0xba35e127276e9000b33011860904ddee28f1d48ddd3577e2a797ef4a5e62319",
(
"0xda4a96147df1f35b0f820bd35c6fac3b80e8e320de7c536b1e054667b22c332",
"0x189bd3fbffe4c8740d6543754d95c790e44cd2d162858e3b733d2b8387983bb7",
),
),
(
"0x11852286660cd970e9d7f46f99c7cca2b75554245e91b9b19d537aa6147c28fc",
(
"0x2ff727cfaaadb3acab713fa22d91f5fddab3ed77948f3ef6233d7ea9b03f4da1",
"0x304080768fd2f87a852155b727f97db84b191e41970506f0326ed4046d1141aa",
),
),
(
"0x174d1c85d8a690a876cc1deba0166d30569fafdb49cb3ed28405bd1c5357a1cc",
(
"0x11a2eaa8e3e89de056d1b3a288a7f733c8a1282efa41d28e71af065ab245df9b",
"0x60f37c447ac29fd97b9bb83be98ddccf15e34831a9cdf5493b7fede0777ae06",
),
),
(
"0x73b81432b4cf3a8a9076201500d1b94159539f052a6e0928db7f2df74bff672",
(
"0x27409dccc6ee4ce90e24744fda8d72c0bc64e79766f778da0c1c0ef1c186ea84",
"0x1ac201a542feca15e77f30370da183514dc99d8a0b2c136d64ede35cd0b51dc0",
),
),
]
)
);

Which hold a lot of unnecessary #[cfg(test)].

Try to remove the unnecessary attrs in d205675

@duguorong009 duguorong009 marked this pull request as ready for review January 22, 2024 14:37
@duguorong009 duguorong009 changed the title improve: add some macros to generate big testing suite of curves and fields improve: add some macros to generate big testing suite of curves Jan 22, 2024
@davidnevadoc davidnevadoc self-requested a review January 22, 2024 15:01
($c: ident) => {
assert!(bool::from($c::identity().is_on_curve()));
assert!(bool::from($c::generator().is_on_curve()));
assert!(bool::from($c::identity().is_on_curve()));
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are these checks duplicated?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for indication! @davidnevadoc
Here, I removed the duplicates. 9ee6301

Comment on lines 214 to 219
[
0x8b17ea66b99c90dd,
0x5bfc41088d8daaa7,
0xb3c4d79d41a91758,
0x00,
]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I find the z_other in this test quite absurd.
It is checking that a variable that is declared in the test is the other 3-root of unity and nothing else.
It doesn't even check that is not the value being used as ZETA.
I would remove this variable altogether and remove it from the macro as well.

@duguorong009
Copy link
Contributor Author

@CPerezz @davidnevadoc
Please review this commit e1c92b5.
This is for checking the curve constants/params.

Copy link
Member

@CPerezz CPerezz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!!

Great work!! 🥳

Comment on lines 254 to 258
println!(
"{:?} \n{:?}",
projective_repr.as_ref(),
affine_repr.as_ref()
);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, removing now. ( 😅 )
d13e9dd

@duguorong009 duguorong009 added this pull request to the merge queue Jan 23, 2024
Merged via the queue into privacy-scaling-explorations:main with commit 53dd906 Jan 23, 2024
11 checks passed
@CPerezz CPerezz mentioned this pull request Feb 8, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants