Skip to content

Commit 17bdf1d

Browse files
committed
Introduce MuSig2-related types for Taproot channels.
1 parent 31e78ff commit 17bdf1d

File tree

3 files changed

+55
-2
lines changed

3 files changed

+55
-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 = "a3edc36" }

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+
/// A partial signature that also contains the Musig2 nonce its signer used
55+
#[cfg(taproot)]
56+
#[derive(Clone, Debug, PartialEq, Eq)]
57+
pub struct PartialSignatureWithNonce(pub(crate) musig2::types::PartialSignature, pub(crate) 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: 47 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,48 @@ 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().to_vec().write(w)
871+
}
872+
873+
fn serialized_length(&self) -> usize {
874+
PUBLIC_KEY_SIZE * 2
875+
}
876+
}
877+
878+
#[cfg(taproot)]
879+
impl Readable for musig2::types::PublicNonce {
880+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
881+
let buf: [u8; PUBLIC_KEY_SIZE * 2] = Readable::read(r)?;
882+
let nonce = musig2::types::PublicNonce::from_slice(&buf).unwrap();
883+
Ok(nonce)
884+
}
885+
}
886+
887+
#[cfg(taproot)]
888+
impl Writeable for PartialSignatureWithNonce {
889+
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
890+
self.0.serialize().write(w)?;
891+
self.1.write(w)
892+
}
893+
894+
fn serialized_length(&self) -> usize {
895+
SECRET_KEY_SIZE + self.1.serialized_length()
896+
}
897+
}
898+
899+
#[cfg(taproot)]
900+
impl Readable for PartialSignatureWithNonce {
901+
fn read<R: Read>(r: &mut R) -> Result<Self, DecodeError> {
902+
let partial_signature_buf: [u8; SECRET_KEY_SIZE] = Readable::read(r)?;
903+
let partial_signature: musig2::types::PartialSignature = musig2::types::PartialSignature::from_slice(&partial_signature_buf).unwrap();
904+
let public_nonce: musig2::types::PublicNonce = Readable::read(r)?;
905+
Ok(PartialSignatureWithNonce(partial_signature, public_nonce))
906+
}
907+
}
908+
864909
impl Writeable for Sha256dHash {
865910
fn write<W: Writer>(&self, w: &mut W) -> Result<(), io::Error> {
866911
w.write_all(&self[..])
@@ -1277,7 +1322,7 @@ mod tests {
12771322
fn bigsize_encoding_decoding() {
12781323
let values = vec![0, 252, 253, 65535, 65536, 4294967295, 4294967296, 18446744073709551615];
12791324
let bytes = vec![
1280-
"00",
1325+
"00",
12811326
"fc",
12821327
"fd00fd",
12831328
"fdffff",
@@ -1286,7 +1331,7 @@ mod tests {
12861331
"ff0000000100000000",
12871332
"ffffffffffffffffff"
12881333
];
1289-
for i in 0..=7 {
1334+
for i in 0..=7 {
12901335
let mut stream = crate::io::Cursor::new(::hex::decode(bytes[i]).unwrap());
12911336
assert_eq!(super::BigSize::read(&mut stream).unwrap().0, values[i]);
12921337
let mut stream = super::VecWriter(Vec::new());

0 commit comments

Comments
 (0)