Skip to content

psbt: add new fields for MuSig2 - #2198

Open
guggero wants to merge 9 commits into
btcsuite:masterfrom
guggero:bip-musig2-psbt
Open

psbt: add new fields for MuSig2#2198
guggero wants to merge 9 commits into
btcsuite:masterfrom
guggero:bip-musig2-psbt

Conversation

@guggero

@guggero guggero commented Jun 14, 2024

Copy link
Copy Markdown
Collaborator

Implements the new input and output fields to the PSBT library according to BIP-0373.

@coveralls

coveralls commented Jun 14, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 9513655947

Details

  • 4 of 240 (1.67%) changed or added relevant lines in 3 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.2%) to 56.774%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 3 86.27%
Totals Coverage Status
Change from base Build 9348961734: -0.2%
Covered Lines: 29625
Relevant Lines: 52181

💛 - Coveralls

@coveralls

coveralls commented Jun 14, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 9519109524

Details

  • 4 of 381 (1.05%) changed or added relevant lines in 5 files are covered.
  • 8 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.4%) to 56.6%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/utils.go 0 22 0.0%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/finalizer.go 0 119 0.0%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 3 86.27%
txscript/taproot.go 5 95.31%
Totals Coverage Status
Change from base Build 9348961734: -0.4%
Covered Lines: 29614
Relevant Lines: 52322

💛 - Coveralls

@coveralls

coveralls commented Jun 15, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 9528732501

Details

  • 4 of 381 (1.05%) changed or added relevant lines in 5 files are covered.
  • 11 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.4%) to 56.603%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/utils.go 0 22 0.0%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/finalizer.go 0 119 0.0%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 2 87.32%
peer/peer.go 9 73.72%
Totals Coverage Status
Change from base Build 9348961734: -0.4%
Covered Lines: 29616
Relevant Lines: 52322

💛 - Coveralls

@coveralls

coveralls commented Jun 15, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 9528834149

Details

  • 4 of 381 (1.05%) changed or added relevant lines in 5 files are covered.
  • 3 unchanged lines in 1 file lost coverage.
  • Overall coverage decreased (-0.4%) to 56.626%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/utils.go 0 22 0.0%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/finalizer.go 0 119 0.0%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 3 86.97%
Totals Coverage Status
Change from base Build 9348961734: -0.4%
Covered Lines: 29628
Relevant Lines: 52322

💛 - Coveralls

@coveralls

coveralls commented Jun 15, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 9528865705

Details

  • 4 of 387 (1.03%) changed or added relevant lines in 5 files are covered.
  • 12 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.4%) to 56.607%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/utils.go 0 22 0.0%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/finalizer.go 0 125 0.0%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 3 86.27%
peer/peer.go 9 73.72%
Totals Coverage Status
Change from base Build 9348961734: -0.4%
Covered Lines: 29621
Relevant Lines: 52327

💛 - Coveralls

@coveralls

coveralls commented Jun 15, 2024

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 9528900206

Details

  • 6 of 389 (1.54%) changed or added relevant lines in 5 files are covered.
  • 12 unchanged lines in 2 files lost coverage.
  • Overall coverage decreased (-0.4%) to 56.587%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/utils.go 0 22 0.0%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/finalizer.go 2 127 1.57%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
connmgr/connmanager.go 3 86.27%
peer/peer.go 9 73.49%
Totals Coverage Status
Change from base Build 9348961734: -0.4%
Covered Lines: 29611
Relevant Lines: 52328

💛 - Coveralls

@coveralls

coveralls commented Jan 19, 2025

Copy link
Copy Markdown

Pull Request Test Coverage Report for Build 14964403415

Details

  • 6 of 389 (1.54%) changed or added relevant lines in 5 files are covered.
  • 30 unchanged lines in 5 files lost coverage.
  • Overall coverage decreased (-1.6%) to 55.082%

Changes Missing Coverage Covered Lines Changed/Added Lines %
btcutil/psbt/partial_output.go 1 22 4.55%
btcutil/psbt/utils.go 0 22 0.0%
btcutil/psbt/partial_input.go 3 63 4.76%
btcutil/psbt/finalizer.go 2 127 1.57%
btcutil/psbt/musig2.go 0 155 0.0%
Files with Coverage Reduction New Missed Lines %
btcutil/gcs/gcs.go 1 80.95%
mempool/mempool.go 1 66.56%
rpcclient/infrastructure.go 3 47.75%
btcec/v2/schnorr/signature.go 5 79.92%
btcec/v2/ecdsa/signature.go 20 69.7%
Totals Coverage Status
Change from base Build 14871723979: -1.6%
Covered Lines: 31088
Relevant Lines: 56440

💛 - Coveralls

@guggero
guggero force-pushed the bip-musig2-psbt branch from 8e879e1 to 35a2775 Compare May 12, 2025 05:18
@guggero
guggero force-pushed the bip-musig2-psbt branch 2 times, most recently from e4bb95c to 04ec8d7 Compare May 20, 2026 13:03
@guggero

guggero commented May 20, 2026

Copy link
Copy Markdown
Collaborator Author

The BIP-0373 PR was merged a while ago. I also rebased on latest master that has the Golang module fixes and addressed all TODOs. This is ready for review IMO.

@guggero
guggero marked this pull request as ready for review May 20, 2026 13:04
Comment thread psbt/musig2.go
Comment thread psbt/musig2.go Outdated
Comment thread psbt/musig2.go
Comment thread psbt/utils.go
Comment thread psbt/finalizer.go
Comment thread psbt/finalizer.go Outdated
@guggero

guggero commented Aug 12, 2026

Copy link
Copy Markdown
Collaborator Author

@TechLateef thank you very much for the thorough review! I've addressed all your comments.

@sputn1ck sputn1ck left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

BIP-373 implementation review. Inline findings below.

Comment thread psbt/finalizer.go Outdated
keyAggOpts []musig2.KeyAggOption,
combineOpts []musig2.CombineOption) (*schnorr.Signature, error) {

aggKey, _, _, err := musig2.AggregateKeys(set.keys, true, keyAggOpts...)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Preserve participant aggregation order

The signing set is reconstructed from nonce-map order, then AggregateKeys is always called with sorting enabled. BIP 373 stores participants in the required KeyAgg order and BIP 327 makes sorting optional. I reproduced this with a valid explicitly unsorted session; MaybeFinalizeAll rejects it. Please select the matching participant record, map nonces and signatures into its Keys order, and aggregate with sort=false.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed and added tests for!

Comment thread psbt/finalizer.go Outdated
if err != nil {
return nil, nil, err
}
if xpub == nil {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Implement the BIP 328 fallback

Derived aggregate finalization requires PSBT_GLOBAL_XPUB and explicitly rejects its absence, but BIP 373 says to assume BIP 328's fixed synthetic chain code when no matching global xpub exists. The official case 4c partial signatures therefore cannot be aggregated after removing the preaggregated key signature.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Addressed and unit tested.

Comment thread psbt/finalizer.go Outdated
var sigHashMsg [32]byte
copy(sigHashMsg[:], sigHash)

// No taproot tweak: the aggregate key is the key in the script and is

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Apply derivation tweaks to script spends

The tapscript finalizer always combines with no tweaks. BIP 373 permits the key inside a leaf to be derived from a parent MuSig2 aggregate, in which case the BIP32 derivation tweaks must also be applied here. Otherwise this method installs an invalid witness.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Fixed.

Comment thread psbt/finalizer.go Outdated
return nil, fmt.Errorf("no MuSig2 partial signatures on input")
}

if len(pInput.MuSig2PubNonces) != numSigs {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

[P1] Group independent signing contexts

This requires the total nonce and signature counts to match and later requires every record to use one aggregate and leaf. BIP 373 keys these records so several aggregates or spend paths can coexist. A complete key-path session plus one nonce for another leaf becomes ErrNotFinalizable. Please group by aggregate key and tapleaf hash, then select a complete context.

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

Makes sense, fixed.

Comment thread psbt/utils.go
Comment thread psbt/finalizer.go Outdated
Comment thread psbt/updater.go Outdated
Comment thread psbt/partial_input.go
@guggero
guggero requested a review from sputn1ck August 14, 2026 08:51
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.

4 participants