musig: invalidate output if pubnonce/aggnonce parsing fails #1991

pull amartya1523 wants to merge 4 commits into bitcoin-core:master from amartya1523:musig-parse-invalidate-output changing 3 files +56 −6
  1. amartya1523 commented at 2:31 PM on November 6, 2026: contributor

    musig_pubnonce_parse and musig_aggnonce_parse return 0 on invalid input but leave the output object untouched. Every other parse function in the library (ec_pubkey_parse, xonly_pubkey_parse, the ECDSA signature parsers, silentpayments_recipient_label_parse and musig_partial_sig_parse) clears its output first, so a failed parse leaves an object that is rejected when used.

    Without that, if the object already holds a nonce (e.g. a coordinator parsing incoming nonces into a reused array, as the test vectors do), the old nonce survives a failed parse and is accepted by nonce_agg / nonce_process without any error. If the object was never initialized, a later load reads uninitialized memory.

    Commits:

    • clear the output right after the nonce != NULL check, as ec_pubkey_parse does, so it is also cleared when in66 is NULL
    • test that a failed parse (and a NULL in66) leaves a zeroed object that serialize and nonce_agg / nonce_process reject. An all-zero aggnonce is valid (infinity), so that test uses a bad prefix byte. These checks fail without the first commit.
    • partial_sig_parse cleared only after both ARG_CHECKs, so move its memset up the same way
    • document "If not, it's set to an invalid value" for the three musig parsers, matching xonly_pubkey_parse

    This is narrower than #1850: it doesn't propose a general clear-on-failure rule, it only makes the musig parsers behave like the other parsers.

  2. musig: invalidate nonce output if pubnonce/aggnonce parsing fails
    Every other parse function in the library (ec_pubkey_parse,
    xonly_pubkey_parse, the ECDSA signature parsers,
    silentpayments_recipient_label_parse and musig_partial_sig_parse)
    clears its output object before parsing, so that a failed parse leaves
    an object that is rejected when it is used later.
    
    musig_pubnonce_parse and musig_aggnonce_parse return 0 but leave the
    output untouched instead. If the object was not initialized, a later
    load reads uninitialized memory. If it holds an earlier nonce, for
    example when a coordinator parses incoming nonces into a reused array,
    that stale nonce stays valid and is accepted by nonce_agg and
    nonce_process without any error.
    
    Clear the output right after checking that it is non-NULL, as
    ec_pubkey_parse and xonly_pubkey_parse do, so that it is also cleared
    if in66 is NULL.
    a6a7f0a121
  3. musig: test that failed nonce parsing invalidates the output
    Parse a valid nonce into an object, then parse an invalid encoding into
    the same object, and check that the object is cleared and rejected by
    serialize and by nonce_agg (for a pubnonce) or nonce_process (for an
    aggnonce). Also check the case where in66 is NULL.
    
    An all-zero encoding is a valid aggnonce (both points at infinity), so
    the aggnonce test uses an invalid prefix byte instead.
    
    These checks fail without the previous commit.
    2b4660bb6f
  4. musig: clear partial sig output before checking in32
    partial_sig_parse cleared its output only after both ARG_CHECKs, so a
    NULL in32 left a previously parsed signature in place. Clear it right
    after checking sig, as the nonce parsers now do. Adjust the NULL in32
    test to use a temporary object and check that it is cleared.
    2287dd9c34
  5. doc: say that musig parse functions invalidate the output on failure
    xonly_pubkey_parse documents that its output is set to an invalid value
    if parsing fails. The musig parse functions now all behave this way, so
    document it for pubnonce_parse, aggnonce_parse and partial_sig_parse as
    well.
    d33f69730f
  6. Yudis-bit commented at 2:46 PM on November 9, 2026: contributor

    Zeroing the nonce buffer at function entry leaves an invalid magic header on bad inputs, matching xonly_pubkey_parse and preventing stale nonces from passing downstream checks.

    Unit tests pass locally under ctest, and ctime_tests is clean under Valgrind. ACK d33f697.

  7. theStack added the label user-documentation on Nov 11, 2026
  8. theStack added the label tweak/refactor on Nov 11, 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-11-12 07:15 UTC

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