Merge rust-bitcoin/rust-bitcoin#4440: Add support for de/serializing PSBT_{IN,OUT}_MUSIG2_PARTICIPANT_PUBKEYS

2481695b45 Add tests for BIP-373 PSBT_{IN,OUT}_MUSIG2_PARTICIPANT_PUBKEYS serialization and deserialization (Daniel Roberts)
3e8e6d9aa1 Add BIP-373 PSBT_{IN,OUT}_MUSIG2_PARTICIPANT_PUBKEYS serialization and deserialization (Daniel Roberts)

Pull request description:

  This change adds support for serializing and deserializing two PSBT keys from BIP-373: `PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS` and `PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS`

  This is a part of #4207 that can be implemented independently of the rest (which depends on https://github.com/rust-bitcoin/rust-secp256k1/pull/716). I believe this satisfactorily avoids changing things multiple times on end users, *however* it's not *completely* transparent to end users, since any code that currently accesses these fields through `unknown` will need to be updated. Later, when `PSBT_IN_MUSIG2_PUB_NONCE` and `PSBT_IN_MUSIG2_PARTIAL_SIG` are supported, code will need to be updated a second time to retrieve them from the correct place instead of `unknown`. I'm of the opinion that this imposes a very minor maintenance burden, only consisting of *removing* deserialization code.

  ### Notes/Requests for feedback

  - For the most part I used my judgement rather than `cargo fmt` since `cargo fmt` already had a lot of other complaints, but of course I'll update if I need to.
  - To satisfy the requirement that every commit pass tests, the commit updating the psbt serde regression test should probably be squashed into the first commit, but I just wanted to confirm that before I did it. I suppose similarly, the test commit could be squashed as well?
  - I waffled between `musig2_participants` and `musig2_participant_pubkeys`, but I've decided to go with `musig2_participant_pubkeys` because that is consistent with Bitcoin Core

ACKs for top commit:
  tcharding:
    ACK 2481695b45
  apoelstra:
    ACK 2481695b456bcccbb25c247c1fd39bbda24dbb30; successfully ran local tests

Tree-SHA512: af884923593c9cbb24ff3f1f08219458538592fabde85d5d65bc2d9bc7bf0b1a73dac38d2c56303b4f3162088db129ea7e879c3d4b324e965933c121ef939a07
This commit is contained in:
merge-script 2025-05-26 19:17:48 +00:00
commit 5e0b86d2b1
No known key found for this signature in database
GPG Key ID: C588D63CE41B97C1
4 changed files with 96 additions and 0 deletions

View File

@ -59,6 +59,8 @@ const PSBT_IN_TAP_BIP32_DERIVATION: u64 = 0x16;
const PSBT_IN_TAP_INTERNAL_KEY: u64 = 0x17; const PSBT_IN_TAP_INTERNAL_KEY: u64 = 0x17;
/// Type: Taproot Merkle Root PSBT_IN_TAP_MERKLE_ROOT = 0x18 /// Type: Taproot Merkle Root PSBT_IN_TAP_MERKLE_ROOT = 0x18
const PSBT_IN_TAP_MERKLE_ROOT: u64 = 0x18; const PSBT_IN_TAP_MERKLE_ROOT: u64 = 0x18;
/// Type: MuSig2 Public Keys Participating in Aggregate Input PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS = 0x1a
const PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS: u64 = 0x1a;
/// Type: Proprietary Use Type PSBT_IN_PROPRIETARY = 0xFC /// Type: Proprietary Use Type PSBT_IN_PROPRIETARY = 0xFC
const PSBT_IN_PROPRIETARY: u64 = 0xFC; const PSBT_IN_PROPRIETARY: u64 = 0xFC;
@ -113,6 +115,8 @@ pub struct Input {
pub tap_internal_key: Option<XOnlyPublicKey>, pub tap_internal_key: Option<XOnlyPublicKey>,
/// Taproot Merkle root. /// Taproot Merkle root.
pub tap_merkle_root: Option<TapNodeHash>, pub tap_merkle_root: Option<TapNodeHash>,
/// Mapping from MuSig2 aggregate keys to the participant keys from which they were aggregated.
pub musig2_participant_pubkeys: BTreeMap<secp256k1::PublicKey, Vec<secp256k1::PublicKey>>,
/// Proprietary key-value pairs for this input. /// Proprietary key-value pairs for this input.
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>, pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
/// Unknown key-value pairs for this input. /// Unknown key-value pairs for this input.
@ -352,6 +356,11 @@ impl Input {
self.tap_merkle_root <= <raw_key: _>|< raw_value: TapNodeHash> self.tap_merkle_root <= <raw_key: _>|< raw_value: TapNodeHash>
} }
} }
PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS => {
impl_psbt_insert_pair! {
self.musig2_participant_pubkeys <= <raw_key: secp256k1::PublicKey>|< raw_value: Vec<secp256k1::PublicKey> >
}
}
PSBT_IN_PROPRIETARY => { PSBT_IN_PROPRIETARY => {
let key = raw::ProprietaryKey::try_from(raw_key.clone())?; let key = raw::ProprietaryKey::try_from(raw_key.clone())?;
match self.proprietary.entry(key) { match self.proprietary.entry(key) {
@ -390,6 +399,7 @@ impl Input {
self.tap_script_sigs.extend(other.tap_script_sigs); self.tap_script_sigs.extend(other.tap_script_sigs);
self.tap_scripts.extend(other.tap_scripts); self.tap_scripts.extend(other.tap_scripts);
self.tap_key_origins.extend(other.tap_key_origins); self.tap_key_origins.extend(other.tap_key_origins);
self.musig2_participant_pubkeys.extend(other.musig2_participant_pubkeys);
self.proprietary.extend(other.proprietary); self.proprietary.extend(other.proprietary);
self.unknown.extend(other.unknown); self.unknown.extend(other.unknown);
@ -482,6 +492,11 @@ impl Map for Input {
impl_psbt_get_pair! { impl_psbt_get_pair! {
rv.push(self.tap_merkle_root, PSBT_IN_TAP_MERKLE_ROOT) rv.push(self.tap_merkle_root, PSBT_IN_TAP_MERKLE_ROOT)
} }
impl_psbt_get_pair! {
rv.push_map(self.musig2_participant_pubkeys, PSBT_IN_MUSIG2_PARTICIPANT_PUBKEYS)
}
for (key, value) in self.proprietary.iter() { for (key, value) in self.proprietary.iter() {
rv.push(raw::Pair { key: key.to_key(), value: value.clone() }); rv.push(raw::Pair { key: key.to_key(), value: value.clone() });
} }

View File

@ -20,6 +20,8 @@ const PSBT_OUT_TAP_INTERNAL_KEY: u64 = 0x05;
const PSBT_OUT_TAP_TREE: u64 = 0x06; const PSBT_OUT_TAP_TREE: u64 = 0x06;
/// Type: Taproot Key BIP 32 Derivation Path PSBT_OUT_TAP_BIP32_DERIVATION = 0x07 /// Type: Taproot Key BIP 32 Derivation Path PSBT_OUT_TAP_BIP32_DERIVATION = 0x07
const PSBT_OUT_TAP_BIP32_DERIVATION: u64 = 0x07; const PSBT_OUT_TAP_BIP32_DERIVATION: u64 = 0x07;
/// Type: MuSig2 Public Keys Participating in Aggregate Output PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS = 0x08
const PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS: u64 = 0x08;
/// Type: Proprietary Use Type PSBT_IN_PROPRIETARY = 0xFC /// Type: Proprietary Use Type PSBT_IN_PROPRIETARY = 0xFC
const PSBT_OUT_PROPRIETARY: u64 = 0xFC; const PSBT_OUT_PROPRIETARY: u64 = 0xFC;
@ -40,6 +42,8 @@ pub struct Output {
pub tap_tree: Option<TapTree>, pub tap_tree: Option<TapTree>,
/// Map of tap root x only keys to origin info and leaf hashes contained in it. /// Map of tap root x only keys to origin info and leaf hashes contained in it.
pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>, pub tap_key_origins: BTreeMap<XOnlyPublicKey, (Vec<TapLeafHash>, KeySource)>,
/// Mapping from MuSig2 aggregate keys to the participant keys from which they were aggregated.
pub musig2_participant_pubkeys: BTreeMap<secp256k1::PublicKey, Vec<secp256k1::PublicKey>>,
/// Proprietary key-value pairs for this output. /// Proprietary key-value pairs for this output.
pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>, pub proprietary: BTreeMap<raw::ProprietaryKey, Vec<u8>>,
/// Unknown key-value pairs for this output. /// Unknown key-value pairs for this output.
@ -90,6 +94,11 @@ impl Output {
self.tap_key_origins <= <raw_key: XOnlyPublicKey>|< raw_value: (Vec<TapLeafHash>, KeySource)> self.tap_key_origins <= <raw_key: XOnlyPublicKey>|< raw_value: (Vec<TapLeafHash>, KeySource)>
} }
} }
PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS => {
impl_psbt_insert_pair! {
self.musig2_participant_pubkeys <= <raw_key: secp256k1::PublicKey>|< raw_value: Vec<secp256k1::PublicKey> >
}
}
_ => match self.unknown.entry(raw_key) { _ => match self.unknown.entry(raw_key) {
btree_map::Entry::Vacant(empty_key) => { btree_map::Entry::Vacant(empty_key) => {
empty_key.insert(raw_value); empty_key.insert(raw_value);
@ -107,6 +116,7 @@ impl Output {
self.proprietary.extend(other.proprietary); self.proprietary.extend(other.proprietary);
self.unknown.extend(other.unknown); self.unknown.extend(other.unknown);
self.tap_key_origins.extend(other.tap_key_origins); self.tap_key_origins.extend(other.tap_key_origins);
self.musig2_participant_pubkeys.extend(other.musig2_participant_pubkeys);
combine!(redeem_script, self, other); combine!(redeem_script, self, other);
combine!(witness_script, self, other); combine!(witness_script, self, other);
@ -143,6 +153,10 @@ impl Map for Output {
rv.push_map(self.tap_key_origins, PSBT_OUT_TAP_BIP32_DERIVATION) rv.push_map(self.tap_key_origins, PSBT_OUT_TAP_BIP32_DERIVATION)
} }
impl_psbt_get_pair! {
rv.push_map(self.musig2_participant_pubkeys, PSBT_OUT_MUSIG2_PARTICIPANT_PUBKEYS)
}
for (key, value) in self.proprietary.iter() { for (key, value) in self.proprietary.iter() {
rv.push(raw::Pair { key: key.to_key(), value: value.clone() }); rv.push(raw::Pair { key: key.to_key(), value: value.clone() });
} }

View File

@ -1339,6 +1339,8 @@ mod tests {
secp256k1::{All, SecretKey}, secp256k1::{All, SecretKey},
}; };
use std::str::FromStr;
use super::*; use super::*;
use crate::address::script_pubkey::ScriptExt as _; use crate::address::script_pubkey::ScriptExt as _;
use crate::bip32::ChildNumber; use crate::bip32::ChildNumber;
@ -2233,6 +2235,49 @@ mod tests {
assert!(!rtt.proprietary.is_empty()); assert!(!rtt.proprietary.is_empty());
} }
// Deserialize MuSig2 PSBT participant keys according to BIP-373
#[test]
fn serialize_and_deserialize_musig2_participants() {
// XXX: Does not cover PSBT_IN_MUSIG2_PUB_NONCE, PSBT_IN_MUSIG2_PARTIAL_SIG (yet)
let expected_in_agg_pk = secp256k1::PublicKey::from_str("021401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e00").unwrap();
let expected_in_pubkeys = vec![
secp256k1::PublicKey::from_str("02bebd7a1cef20283444b96e9ce78137e951ce48705390933896311a9abc75736a").unwrap(),
secp256k1::PublicKey::from_str("0355212dff7b3d7e8126687a62fd0435a3fb4de56d9af9ae23a1c9ca05b349c8e2").unwrap(),
];
let expected_out_agg_pk = secp256k1::PublicKey::from_str("0364934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba5").unwrap();
let expected_out_pubkeys = vec![
secp256k1::PublicKey::from_str("02841d69a8b80ae23a8090e6f3765540ea5efd8c287b1307c983a6e2a3a171b525").unwrap(),
secp256k1::PublicKey::from_str("02bad833849a98cdfb0a0749609ddccab16ad54485ecc67f828df4bdc4f2b90d4c").unwrap(),
];
const PSBT_HEX: &str = "70736274ff01005e02000000017b42be5ea467afe0d0571dc4a91bef97ff9605a590c0b8d5892323946414d1810000000000ffffffff01f0b9f50500000000225120bc7e18f55e2c7a28d78cadac1bc72c248372375d269bafe6b315bc40505d07e5000000000001012b00e1f50500000000225120de564ebf8ff7bd9bb41bd88264c04b1713ebb9dc8df36319091d2eabb16cda6221161401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e000500eb4cbe62211655212dff7b3d7e8126687a62fd0435a3fb4de56d9af9ae23a1c9ca05b349c8e20500755abbf92116bebd7a1cef20283444b96e9ce78137e951ce48705390933896311a9abc75736a05002a33dfd90117201401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e00221a021401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e004202bebd7a1cef20283444b96e9ce78137e951ce48705390933896311a9abc75736a0355212dff7b3d7e8126687a62fd0435a3fb4de56d9af9ae23a1c9ca05b349c8e20001052064934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba5210764934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba50500fa4c6afa22080364934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba54202841d69a8b80ae23a8090e6f3765540ea5efd8c287b1307c983a6e2a3a171b52502bad833849a98cdfb0a0749609ddccab16ad54485ecc67f828df4bdc4f2b90d4c00";
let psbt = hex_psbt(PSBT_HEX).unwrap();
assert_eq!(psbt.inputs[0].musig2_participant_pubkeys.len(), 1);
assert_eq!(
psbt.inputs[0].musig2_participant_pubkeys.iter().next().unwrap(),
(&expected_in_agg_pk, &expected_in_pubkeys)
);
assert_eq!(psbt.outputs[0].musig2_participant_pubkeys.len(), 1);
assert_eq!(
psbt.outputs[0].musig2_participant_pubkeys.iter().next().unwrap(),
(&expected_out_agg_pk, &expected_out_pubkeys)
);
// Check round trip de/serialization
assert_eq!(psbt.serialize_hex(), PSBT_HEX);
const PSBT_TRUNCATED_MUSIG_PARTICIPANTS_HEX: &str = "70736274ff01005e0200000001f034711ce319b1db76ce73440f2cb64a7e3a02e75c936b8d8a4958a024ea8d870000000000ffffffff01f0b9f50500000000225120bc7e18f55e2c7a28d78cadac1bc72c248372375d269bafe6b315bc40505d07e5000000000001012b00e1f50500000000225120de564ebf8ff7bd9bb41bd88264c04b1713ebb9dc8df36319091d2eabb16cda6221161401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e000500eb4cbe62211655212dff7b3d7e8126687a62fd0435a3fb4de56d9af9ae23a1c9ca05b349c8e20500755abbf92116bebd7a1cef20283444b96e9ce78137e951ce48705390933896311a9abc75736a05002a33dfd90117201401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e00221a021401301810a46a4e3f39e4603ec228ed301d9f2079767fda758dee7224b32e002a02bebd7a1cef20283444b96e9ce78137e951ce48705390933896311a9abc75736a0355212dff7b3d7e810001052064934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba5210764934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba50500fa4c6afa22080364934a64831bd917a2667b886671650846f021e1c025e4b2bb65e49ab3e7cba52a02841d69a8b80ae23a8090e6f3765540ea5efd8c287b1307c983a6e2a3a171b52502bad833849a98cdfb00";
hex_psbt(PSBT_TRUNCATED_MUSIG_PARTICIPANTS_HEX)
.expect_err("Deserializing PSBT with truncated musig participants should error");
}
// PSBTs taken from BIP 174 test vectors. // PSBTs taken from BIP 174 test vectors.
#[test] #[test]
fn combine_psbts() { fn combine_psbts() {

View File

@ -172,6 +172,28 @@ impl Deserialize for secp256k1::PublicKey {
} }
} }
impl Serialize for Vec<secp256k1::PublicKey> {
fn serialize(&self) -> Vec<u8> {
let mut result: Vec<u8> = Vec::with_capacity(secp256k1::constants::PUBLIC_KEY_SIZE * self.len());
for pubkey in self.iter() {
result.extend(Serialize::serialize(pubkey));
}
result
}
}
impl Deserialize for Vec<secp256k1::PublicKey> {
fn deserialize(bytes: &[u8]) -> Result<Self, Error> {
bytes.chunks(secp256k1::constants::PUBLIC_KEY_SIZE)
.map(|pubkey_bytes| {
secp256k1::PublicKey::deserialize(pubkey_bytes)
})
.collect()
}
}
impl Serialize for ecdsa::Signature { impl Serialize for ecdsa::Signature {
fn serialize(&self) -> Vec<u8> { self.to_vec() } fn serialize(&self) -> Vec<u8> { self.to_vec() }
} }