Skip to content

musig: clear pubnonce output for invalid seckey#1850

Open
l0rinc wants to merge 2 commits intobitcoin-core:masterfrom
l0rinc:l0rinc/musig-clear-invalid-seckey-pubnonce
Open

musig: clear pubnonce output for invalid seckey#1850
l0rinc wants to merge 2 commits intobitcoin-core:masterfrom
l0rinc:l0rinc/musig-clear-invalid-seckey-pubnonce

Conversation

@l0rinc
Copy link
Copy Markdown

@l0rinc l0rinc commented Apr 29, 2026

This is a follow-up to PR #1849.

Problem

secp256k1_musig_nonce_gen returns failure if its arguments are invalid, for example when a non-NULL seckey fails secret-key validation.

On that path, secp256k1_musig_nonce_gen_internal invalidates the secret nonce on failure, but still unconditionally saves the derived public nonce output before returning failure.

Fix

Clear pubnonce before returning the invalid-seckey failure, so a failed nonce-generation call does not leave a newly written pubnonce behind.

The implementation still saves the public nonce before clearing it on failure (instead of branching around the save), to preserve constant-time control flow with respect to the secret-key validation result.

The second commit only extends existing MuSig API failure-path coverage for paths where secp256k1_musig_nonce_gen_internal already clears pubnonce at function entry before any of its own validation steps.

Copy link
Copy Markdown
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

Concept ACK

AFAICT this pattern matches what we do in similar API functions (e.g. _schnorrsig_sign32, _keypair_create, _ellswift_create).

Comment thread src/modules/musig/tests_impl.h
@theStack
Copy link
Copy Markdown
Contributor

ACK 7ed2abc

l0rinc added 2 commits April 29, 2026 19:39
`secp256k1_musig_nonce_gen_internal` clears `pubnonce` at the top of the function, before the later validation steps covered here.

Extend the existing MuSig API failure-path checks for `nonce_gen` and `nonce_gen_counter` to assert that these paths leave the public nonce output cleared.
`secp256k1_musig_nonce_gen` returns failure for invalid arguments, including a non-NULL `seckey` that fails validation.

`secp256k1_musig_nonce_gen_internal` already invalidates `secnonce` on that failure, but it still saves derived nonce points into `pubnonce` before returning.

Clear `pubnonce` with a conditional clear after the save, so the cleanup stays branchless with respect to the secret-key validation result and reverting this cleanup leaves stale derived nonce data in the MuSig API test.
@l0rinc l0rinc force-pushed the l0rinc/musig-clear-invalid-seckey-pubnonce branch from 7ed2abc to d679d2c Compare April 29, 2026 17:40
Copy link
Copy Markdown
Contributor

@theStack theStack left a comment

Choose a reason for hiding this comment

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

re-ACK d679d2c

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.

2 participants