tests: recovery: check that recovery fails if R is not an x coordinate #2013

pull nniiovoo wants to merge 1 commits into bitcoin-core:master from nniiovoo:tests-recovery-invalid-r changing 1 files +17 −0
  1. nniiovoo commented at 6:25 PM on November 28, 2026: contributor

    secp256k1_ecdsa_recover has to fail when R (or R + n for recid 2 and 3) is not the x coordinate of a point on the curve. No recovery test checks that it does: test_ecdsa_recovery_end_to_end can produce such an R by flipping a random byte, but it accepts either a failure or a different key. If the return 0 after a failed secp256k1_ge_set_xo_var is changed to return 1, noverify_tests and exhaustive_tests still pass, and tests only fails by accident, depending on the optimization level, when VERIFY checks read the uninitialized point (see the comment below).

    This adds the signature (r,s) = (5,4) next to the existing (4,4) one. Neither 5 nor 5+n is an x coordinate, so recovery must fail for all four recids.

  2. tests: recovery: check that recovery fails if R is not an x coordinate
    secp256k1_ecdsa_recover must fail when neither R nor, for recid 2 and 3,
    R + n is the x coordinate of a point on the curve. No test used such an
    R, so the tests still pass if that check returns success instead.
    
    Add the signature (r,s) = (5,4), which cannot be recovered with any
    recid.
    4853d8728b
  3. theStack added the label assurance on Nov 30, 2026
  4. IsaqueFranklin commented at 8:43 PM on November 30, 2026: none

    I changed the return mentioned in the description from 0 to 1 to see if the current tests could catch the mutation. On my setup (macOS, AppleClang 21):

    • tests --target=recovery aborts/fails during test_ecdsa_recovery_end_to_end on either of the internal checks a->magnitude <= m or (a->normalized == 0) || (a->normalized == 1).
    • noverify_tests --target=recovery passes with the same seeds.
    • exhaustive_tests also passed.

    Since the mutation returns 1 early, q is never filled with a public key, and then when _ecdsa_recover calls secp256k1_pubkey_save with an uninitialized q, we'll eventually reach the secp256k1_ge_to_bytes function which calls SECP256K1_GE_VERIFY on q. This could explain the magnitude and normalized check errors. I could be wrong tho.

    I guess the current tests can catch this mutation but it's more of an "indirect" catch through some internal checks.

  5. nniiovoo commented at 12:31 AM on December 1, 2026: contributor

    Thanks for checking this. You're right, my description was too strong. I had only tried it at -O1, where the garbage point gets through without tripping anything. With -O2 or -O3, tests fails the same way you saw (the VERIFY check on the uninitialized q in secp256k1_pubkey_save), while noverify_tests and exhaustive_tests still pass. So the current tests only catch it by accident, through uninitialized memory. The new test checks the return value of secp256k1_ecdsa_recover directly, so it fails in both builds at every optimization level. I'll update the description.

Labels

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-12-02 10:15 UTC

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