tests: add ECDSA verify case where r + n overflows p #1948

pull ViniciusCestarii wants to merge 1 commits into bitcoin-core:master from ViniciusCestarii:test-ecdsa-verify-r-plus-n-overflow changing 1 files +28 −0
  1. ViniciusCestarii commented at 12:15 AM on September 29, 2026: contributor

    Currently no test checks that ECDSA verify rejects a signature where r + n ≥ p and (r + n) mod p equals x(R), so the following mutant lives:

    diff --git a/src/ecdsa_impl.h b/src/ecdsa_impl.h
    index 5963877..5301eaa 100644
    --- a/src/ecdsa_impl.h
    +++ b/src/ecdsa_impl.h
    @@ -258,10 +258,6 @@ static int secp256k1_ecdsa_sig_verify(const secp256k1_scalar *sigr, const secp25
             /* xr * pr.z^2 mod p == pr.x, so the signature is valid. */
             return 1;
         }
    -    if (secp256k1_fe_cmp_var(&xr, &secp256k1_ecdsa_const_p_minus_order) >= 0) {
    -        /* xr + n >= p, so we can skip testing the second case. */
    -        return 0;
    -    }
         secp256k1_fe_add(&xr, &secp256k1_ecdsa_const_order_as_fe);
         if (secp256k1_gej_eq_x_var(&xr, &pr)) {
             /* (xr + n) * pr.z^2 mod p == pr.x, so the signature is valid. */
    

    This adds a case with r = p - n + 1, so that (r + n) mod p = 1, and a pubkey chosen so that x(R) = 1, covering this gap.

  2. real-or-random added the label assurance on Sep 29, 2026
  3. real-or-random added the label tweak/refactor on Sep 29, 2026
  4. real-or-random commented at 7:42 AM on September 29, 2026: contributor

    ACK mod nit

    This adds a case with r = p - n + 1, so that (r + n) mod p = 1, and a pubkey chosen so that x(R) = 1, covering this gap.

    I think it would be nice to add comments for csr and pubkey that explain how they were chosen.

  5. real-or-random commented at 7:43 AM on September 29, 2026: contributor

    You may also want to take a look at #1949 if you're interested.

  6. real-or-random commented at 7:47 AM on September 29, 2026: contributor

    And one more thing: If Such a test case would also be a great addition to the Wycheproof test vector database (which is also used by other projects) if you're willing to contribute there.

    See https://github.com/C2SP/wycheproof/blob/main/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json for the variant of ECDSA that we use (and we run these vectors as part of our tests), but I think a r + n > p case is a generic for ECDSA.

  7. ViniciusCestarii force-pushed on Sep 29, 2026
  8. ViniciusCestarii commented at 1:44 PM on September 29, 2026: contributor

    Thanks @real-or-random for the review! Forced push b125005e5f42a18f381d8dca6cc0dc7cddb1953a explaining how pubkey and csr values were chosen.

    You may also want to take a look at #1949 if you're interested.

    Cool! I'll give it a look.

    And one more thing: If Such a test case would also be a great addition to the Wycheproof test vector database (which is also used by other projects) if you're willing to contribute there.

    See https://github.com/C2SP/wycheproof/blob/main/testvectors_v1/ecdsa_secp256k1_sha256_bitcoin_test.json for the variant of ECDSA that we use (and we run these vectors as part of our tests), but I think a r + n > p case is a generic for ECDSA.

    The more coverage the better :), I'll open a PR there with an r + n > p case.

  9. theStack approved
  10. theStack commented at 8:47 PM on September 29, 2026: contributor

    ACK b125005e5f42a18f381d8dca6cc0dc7cddb1953a

  11. real-or-random commented at 7:23 AM on September 30, 2026: contributor

    Sorry for the additional nit: Can you also swap the declarations of pubkey and csr. Otherwise the reader has still clue what's going on when encountering the definition of pubkey first. Also, consider adding something like "scalar r as chars" to csr to make clear that this is the value r.

  12. tests: add ECDSA verify case where r + n overflows p fc88acc3e2
  13. ViniciusCestarii force-pushed on Sep 30, 2026
  14. ViniciusCestarii commented at 12:23 PM on September 30, 2026: contributor

    Thanks for the reviews! Forced push fc88acc3e2e2cbba7ee202634eb73d5d21694126 addressing #1948 (comment)

  15. real-or-random approved
  16. real-or-random commented at 1:45 PM on September 30, 2026: contributor

    utACK fc88acc3e2e2cbba7ee202634eb73d5d21694126

  17. theStack approved
  18. theStack commented at 2:03 PM on September 30, 2026: contributor

    re-ACK fc88acc3e2e2cbba7ee202634eb73d5d21694126

    Thanks!

  19. theStack merged this on Sep 30, 2026
  20. theStack closed this on Sep 30, 2026

  21. theStack referenced this in commit 4a264a935a on Sep 30, 2026

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/secp256k1. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-10-03 03:15 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me