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

add fuzz tests about field operations. #1385

Closed
wants to merge 2 commits into from

Conversation

YafeiXie1
Copy link

Thanks for the previous review.
This PR:

  1. changes on the function names in the scalar operations tests.
  2. remove the randomness in fuzz_scalar_shift test, the value is computed from size.
  3. add the function that decide the test based on an environment variable.
  4. add fuzz tests about field operations.
    Could you give me some suggestions?

selected_fuzz_function = &fuzz_field_storage_cmov;
} else {
fprintf(stderr, "Unknown fuzz test selected using FUZZ environment variable: %s\n", test_name);
assert(false);
Copy link
Contributor

Choose a reason for hiding this comment

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

false doesn't exist as a keyword in C89 (which the libsecp256k1 source code tries to use with the least amount of extensions possible). You can use assert(0);.

if (secp256k1_fe_normalizes_to_zero(&a)) {
CHECK(secp256k1_fe_normalizes_to_zero(&r1));
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Coding style nit: } else { can go on one line.

if (secp256k1_scalar_is_zero(&a)) {
CHECK(secp256k1_scalar_is_zero(&r1));
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit: } else { on one line.

if (secp256k1_scalar_is_zero(&a)) {
CHECK(secp256k1_scalar_is_zero(&r1));
}
else {
Copy link
Contributor

Choose a reason for hiding this comment

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

Style nit: } else { on one line.

int bit, r1, r2;
secp256k1_scalar a;
secp256k1_scalar_set_b32(&a, data, NULL);
bit = 1 + (size % 15);
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't recommend using the fuzzer input's length as input itself; I don't think it's really designed to covney much information.

Suggestion: bit = 1 + (data[32] % 15), and if (size >= 33) {.

/*** Scalar Operation ***/
/* Test commutativity of scalar addition */
static void fuzz_scalar_add_commutativty(const uint8_t *data, size_t size) {
if (size > 63) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Tiny nit, but I think it's a bit more readable to use if (size >= 64) { (here and elsewhere), as the 64 is the actual size we need.

static void fuzz_field_equality(const uint8_t *data, size_t size) {
if (size > 31) {
secp256k1_fe fe;
secp256k1_fe_set_b32_mod(&fe, data);
Copy link
Contributor

Choose a reason for hiding this comment

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

This will only construct very "benign" field elements, which are perhaps not the most likely ones to trigger issues.

Going beyond this is non-trivial though. You'll probably want to write a helper function to construct a field element with specified representation directly (so the fe.d[0..4] elements, in particular) and then bail out if it turns out to be invalid. Then you'll need to set magnitude/normalization appropriately as well.

This will involve a number of changes, and you'll want to read up on the field element representation (see field_5x52.h, let's ignore the 26-bit field for now). It'll also mean some refactoring of secp256k1_fe_impl_verify.

@sipa
Copy link
Contributor

sipa commented Aug 21, 2023

Superseded by #1407

@sipa sipa closed this Aug 21, 2023
@YafeiXie1 YafeiXie1 deleted the fuzz branch August 29, 2023 13:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants