Skip to content

Commit 6080ce8

Browse files
committed
Introduce MuSig2-related types for Taproot channels.
1 parent 0e28bcb commit 6080ce8

File tree

3 files changed

+58
-2
lines changed

3 files changed

+58
-2
lines changed

lightning/Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,3 +56,6 @@ regex = "1.5.6"
5656
version = "0.29.0"
5757
default-features = false
5858
features = ["bitcoinconsensus", "secp-recovery"]
59+
60+
[target.'cfg(taproot)'.dependencies]
61+
musig2 = { git = "https://github.com/arik-so/rust-musig2", rev = "27797d7" }

lightning/src/ln/msgs.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,11 @@ use crate::routing::gossip::NodeId;
5151
/// 21 million * 10^8 * 1000
5252
pub(crate) const MAX_VALUE_MSAT: u64 = 21_000_000_0000_0000_000;
5353

54+
#[cfg(taproot)]
55+
/// A partial signature that also contains the Musig2 nonce its signer used
56+
#[derive(Clone, Debug, PartialEq, Eq)]
57+
pub struct PartialSignatureWithNonce(pub musig2::types::PartialSignature, pub musig2::types::PublicNonce);
58+
5459
/// An error in decoding a message or struct.
5560
#[derive(Clone, Debug, PartialEq, Eq)]
5661
pub enum DecodeError {

lightning/src/util/ser.rs

Lines changed: 50 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ use bitcoin::hash_types::{Txid, BlockHash};
3838
use core::marker::Sized;
3939
use core::time::Duration;
4040
use crate::ln::msgs::DecodeError;
41+
#[cfg(taproot)]
42+
use crate::ln::msgs::PartialSignatureWithNonce;
4143
use crate::ln::{PaymentPreimage, PaymentHash, PaymentSecret};
4244

4345
use crate::util::byte_utils::{be48_to_array, slice_to_be48};
@@ -574,6 +576,7 @@ impl_array!(16); // for IPv6
574576
impl_array!(32); // for channel id & hmac
575577
impl_array!(PUBLIC_KEY_SIZE); // for PublicKey
576578
impl_array!(64); // for ecdsa::Signature and schnorr::Signature
579+
impl_array!(66); // for MuSig2 nonces
577580
impl_array!(1300); // for OnionPacket.hop_data
578581

579582
impl Writeable for [u16; 8] {
@@ -861,6 +864,39 @@ impl Readable for SecretKey {
861864
}
862865
}
863866

867+
#[cfg(taproot)]
868+
impl Writeable for musig2::types::PublicNonce {
869+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
870+
self.serialize().write(w)
871+
}
872+
}
873+
874+
#[cfg(taproot)]
875+
impl Readable for musig2::types::PublicNonce {
876+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
877+
let buf: [u8; PUBLIC_KEY_SIZE * 2] = Readable::read(r)?;
878+
musig2::types::PublicNonce::from_slice(&buf).map_err(|_| DecodeError::InvalidValue)
879+
}
880+
}
881+
882+
#[cfg(taproot)]
883+
impl Writeable for PartialSignatureWithNonce {
884+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
885+
self.0.serialize().write(w)?;
886+
self.1.write(w)
887+
}
888+
}
889+
890+
#[cfg(taproot)]
891+
impl Readable for PartialSignatureWithNonce {
892+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
893+
let partial_signature_buf: [u8; SECRET_KEY_SIZE] = Readable::read(r)?;
894+
let partial_signature = musig2::types::PartialSignature::from_slice(&partial_signature_buf).map_err(|_| DecodeError::InvalidValue)?;
895+
let public_nonce: musig2::types::PublicNonce = Readable::read(r)?;
896+
Ok(PartialSignatureWithNonce(partial_signature, public_nonce))
897+
}
898+
}
899+
864900
impl Writeable for Sha256dHash {
865901
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
866902
w.write_all(&self[..])
@@ -1251,6 +1287,7 @@ impl Readable for Duration {
12511287
#[cfg(test)]
12521288
mod tests {
12531289
use core::convert::TryFrom;
1290+
use bitcoin::secp256k1::ecdsa;
12541291
use crate::util::ser::{Readable, Hostname, Writeable};
12551292

12561293
#[test]
@@ -1273,11 +1310,22 @@ mod tests {
12731310
assert_eq!(Hostname::read(&mut buf.as_slice()).unwrap().as_str(), "test");
12741311
}
12751312

1313+
#[test]
1314+
/// Taproot will likely fill legacy signature fields with all 0s.
1315+
/// This test ensures that doing so won't break serialization.
1316+
fn null_signature_codec() {
1317+
let buffer = vec![0u8; 64];
1318+
let mut cursor = crate::io::Cursor::new(buffer.clone());
1319+
let signature = ecdsa::Signature::read(&mut cursor).unwrap();
1320+
let serialization = signature.serialize_compact();
1321+
assert_eq!(buffer, serialization.to_vec())
1322+
}
1323+
12761324
#[test]
12771325
fn bigsize_encoding_decoding() {
12781326
let values = vec![0, 252, 253, 65535, 65536, 4294967295, 4294967296, 18446744073709551615];
12791327
let bytes = vec![
1280-
"00",
1328+
"00",
12811329
"fc",
12821330
"fd00fd",
12831331
"fdffff",
@@ -1286,7 +1334,7 @@ mod tests {
12861334
"ff0000000100000000",
12871335
"ffffffffffffffffff"
12881336
];
1289-
for i in 0..=7 {
1337+
for i in 0..=7 {
12901338
let mut stream = crate::io::Cursor::new(::hex::decode(bytes[i]).unwrap());
12911339
assert_eq!(super::BigSize::read(&mut stream).unwrap().0, values[i]);
12921340
let mut stream = super::VecWriter(Vec::new());

0 commit comments

Comments
 (0)