wallet: bound pending MuSig2 sessions #36641

pull fametrano wants to merge 1 commits into bitcoin:master from fametrano:wallet-musig-session-bound changing 11 files +190 −20
  1. fametrano commented at 6:02 AM on October 24, 2026: none

    The wallet keeps the secret nonce of every unfinished MuSig2 session in locked memory for as long as the wallet is loaded. A nonce is removed only when its session makes a partial signature, so each session that stops before that leaves 144 bytes of locked memory behind, with no bound on how many.

    I keep at most 32768 sessions per wallet and drop the oldest first. The nonces move from each descriptor to the wallet, because every descriptor that can sign for our key in a musig() starts sessions, and each one kept its own. A dropped nonce is never reused. If no participant has added a partial signature for that MuSig2 key yet, removing our pubnonce and running round 1 again restarts the session; otherwise all participants restart the round with new nonces. A release note says so. A standard transaction has at most 1738 signed Taproot inputs, so the limit allows 18 sessions per input, enough for any tree of up to 6 signers with a MuSig2 key path and one MuSig2 leaf per k-of-n subset. At the limit a wallet's nonces use 4.5 MiB of locked memory. Unit tests fill the limit and start one more session through the same descriptor and through another one of the same wallet; they fail without the change.

    This is one of several PRs from a large audit of Bitcoin Core that I did with the help of LLM tools. The audit produced its evidence all at once, which is why the PRs arrive together. I understand each change fully and will answer review questions myself.

  2. DrahtBot added the label Wallet on Oct 24, 2026
  3. DrahtBot commented at 6:02 AM on October 24, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process.

    Type Reviewers
    Concept NACK vicjuma

    If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36697 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36697.svg"></sub> (wallet: Add exportaddresskey RPC for descriptor wallets by jaromil)
    • #36451 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36451.svg"></sub> (refactor: Use NotNull for CreateNewBlock and pimpls by maflcko)
    • #35444 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35444.svg"></sub> (wallet: make descriptor SPKM mutex non-recursive by w0xlt)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

    <!--5faf32d7da4f0f540f40219e4f7537a3-->

    LLM Linter (✨ experimental)

    Possible typos and grammar issues:

    • src/musig.h: and from new nonces of all participants otherwise → otherwise, it restarts with new nonces from all participants [the original construction is grammatically broken]

    <sup>2026-11-01 02:10:48</sup>

  4. DrahtBot added the label CI failed on Oct 24, 2026
  5. fametrano force-pushed on Oct 24, 2026
  6. DrahtBot removed the label CI failed on Oct 24, 2026
  7. in src/musig.h:65 in 5c6a090b9d outdated
      61 | @@ -57,6 +62,34 @@ class MuSig2SecNonce
      62 |      bool IsValid();
      63 |  };
      64 |  
      65 | +/**
    


    vicjuma commented at 11:52 PM on October 26, 2026:

    src/script/sign.cpp:192: provider.DeleteMuSig2Session(session_id); currently lgtm tbh. I mean, on top of the current secnonces being memory-based, it is also deleted after it's done. Unless there is a practical scenario warranting this, I do not see the need

  8. in src/musig.cpp:128 in 5c6a090b9d outdated
     124 | @@ -122,6 +125,30 @@ bool MuSig2SecNonce::IsValid()
     125 |      return m_impl->IsValid();
     126 |  }
     127 |  
     128 | +bool MuSig2SecNonces::Add(const uint256& session_id, MuSig2SecNonce&& nonce)
    


    vicjuma commented at 11:56 PM on October 26, 2026:

    Any justifications for adding these?

  9. in src/wallet/test/scriptpubkeyman_tests.cpp:79 in 5c6a090b9d
      74 | +    BOOST_REQUIRE(other_provider != nullptr);
      75 | +    BOOST_CHECK(!other_provider->GetMuSig2SecNonce(session_ids.front()).has_value());
      76 | +    BOOST_CHECK(other_provider->GetMuSig2SecNonce(session_ids.back()).has_value());
      77 | +    size_t kept{0};
      78 | +    for (const uint256& id : session_ids) kept += other_provider->GetMuSig2SecNonce(id).has_value();
      79 | +    BOOST_CHECK_LT(kept, size_t{num_sessions});
    


    vicjuma commented at 12:03 AM on October 27, 2026:

    Even 1 will pass here, which is not the intention of your PR

  10. vicjuma commented at 12:10 AM on October 27, 2026: contributor

    Concept NACK I do not see the practical implications of what you are trying to solve. If there is any justification, point it out.

  11. wallet: bound pending MuSig2 sessions
    The wallet keeps the secret nonce of every unfinished MuSig2 session in
    locked memory for as long as the wallet is loaded. A nonce is removed
    only when its session makes a partial signature. A round that stops
    before that, or round 1 run again on the same PSBT, leaves its nonces
    behind, 144 bytes of locked memory per session.
    
    I keep at most 32768 sessions per wallet and drop the oldest first. A
    dropped nonce is never reused. If no participant has added a partial
    signature for that MuSig2 key yet, removing our pubnonce and running
    round 1 again restarts the session; otherwise all participants restart
    the round with new nonces. The release note says so. Each input starts
    one session for each MuSig2 key with our key in its key path and script
    leaves. A standard transaction has at most 1738 signed Taproot inputs,
    so the limit allows 18 sessions per input, enough for any tree of up to
    6 signers with a MuSig2 key path and one MuSig2 leaf per k-of-n subset.
    At the limit a wallet's nonces use 4.5 MiB of locked memory.
    
    The nonces move from each DescriptorScriptPubKeyMan to the CWallet, so
    every descriptor that can sign for our key shares the one limit. Signing
    providers get them under cs_wallet, which every signing path holds.
    
    I tested this with unit tests that fill the limit and then start one
    more session, through the same descriptor and through another
    descriptor of the same wallet. They fail without the change, and when
    the limit keeps one session more or one less.
    626d302858
  12. fametrano force-pushed on Nov 1, 2026
  13. fametrano commented at 6:03 PM on November 1, 2026: none

    Closing. @vicjuma, you asked for a practical case and I could not find one. An unfinished session costs 144 bytes of locked memory, reloading the wallet clears it, and it takes about 58000 of them to pass an 8 MiB mlock limit. Measured: https://gist.github.com/fametrano/9e706cbd47f0b084c51330f55fc151b7/041e33ad871f8b55ebf42fc6b29f26703938be58. I am sorry for the noise. Thanks for the review. The finding and its fix are in fametrano/bitcoin#11.

  14. fametrano closed this on Nov 1, 2026

Labels

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-11-20 15:51 UTC

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