Skip to content

Commit 1533243

Browse files
committed
Add tests for MuSig2 de/serialization
1 parent b20e250 commit 1533243

File tree

2 files changed

+79
-0
lines changed

2 files changed

+79
-0
lines changed

src/musig.rs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,3 +1338,24 @@ impl Session {
13381338
/// Get a mut pointer to the inner Session
13391339
pub fn as_mut_ptr(&mut self) -> *mut ffi::MusigSession { &mut self.0 }
13401340
}
1341+
1342+
#[cfg(test)]
1343+
mod test {
1344+
use super::*;
1345+
1346+
#[test]
1347+
fn test_de_serialization() {
1348+
const MUSIG_PUBLIC_NONCE_HEX: &str = "03f4a361abd3d50535be08421dbc73b0a8f595654ae3238afcaf2599f94e25204c036ba174214433e21f5cd0fcb14b038eb40b05b7e7c820dd21aa568fdb0a9de4d7";
1349+
let pubnonce: PublicNonce = MUSIG_PUBLIC_NONCE_HEX.parse().unwrap();
1350+
1351+
assert_eq!(pubnonce.to_string(), MUSIG_PUBLIC_NONCE_HEX);
1352+
1353+
const MUSIG_AGGREGATED_NONCE_HEX: &str = "0218c30fe0f567a4a9c05eb4835e2735419cf30f834c9ce2fe3430f021ba4eacd503112e97bcf6a022d236d71a9357824a2b19515f980131b3970b087cadf94cc4a7";
1354+
let aggregated_nonce: AggregatedNonce = MUSIG_AGGREGATED_NONCE_HEX.parse().unwrap();
1355+
assert_eq!(aggregated_nonce.to_string(), MUSIG_AGGREGATED_NONCE_HEX);
1356+
1357+
const MUSIG_PARTIAL_SIGNATURE_HEX: &str = "289eeb2f5efc314aa6d87bf58125043c96d15a007db4b6aaaac7d18086f49a99";
1358+
let partial_signature: PartialSignature = MUSIG_PARTIAL_SIGNATURE_HEX.parse().unwrap();
1359+
assert_eq!(partial_signature.to_string(), MUSIG_PARTIAL_SIGNATURE_HEX);
1360+
}
1361+
}

tests/serde.rs

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ extern crate bincode;
44
extern crate secp256k1;
55
extern crate serde_cbor;
66

7+
use secp256k1::musig;
78
#[cfg(feature = "global-context")]
89
use secp256k1::{Keypair, Secp256k1};
910
use secp256k1::{PublicKey, SecretKey, XOnlyPublicKey};
@@ -35,6 +36,43 @@ static XONLY_PK_BYTES: [u8; 32] = [
3536
0x4a, 0xc8, 0x87, 0xfe, 0x91, 0xdd, 0xd1, 0x66,
3637
];
3738

39+
#[rustfmt::skip]
40+
static MUSIG_PUBLIC_NONCE_BYTES: [u8; 74] = [
41+
0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
42+
0x03, 0xf4, 0xa3, 0x61, 0xab, 0xd3, 0xd5, 0x05,
43+
0x35, 0xbe, 0x08, 0x42, 0x1d, 0xbc, 0x73, 0xb0,
44+
0xa8, 0xf5, 0x95, 0x65, 0x4a, 0xe3, 0x23, 0x8a,
45+
0xfc, 0xaf, 0x25, 0x99, 0xf9, 0x4e, 0x25, 0x20,
46+
0x4c, 0x03, 0x6b, 0xa1, 0x74, 0x21, 0x44, 0x33,
47+
0xe2, 0x1f, 0x5c, 0xd0, 0xfc, 0xb1, 0x4b, 0x03,
48+
0x8e, 0xb4, 0x0b, 0x05, 0xb7, 0xe7, 0xc8, 0x20,
49+
0xdd, 0x21, 0xaa, 0x56, 0x8f, 0xdb, 0x0a, 0x9d,
50+
0xe4, 0xd7,
51+
];
52+
53+
#[rustfmt::skip]
54+
static MUSIG_AGGREGATED_NONCE_BYTES: [u8; 74] = [
55+
0x42, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56+
0x02, 0x18, 0xc3, 0x0f, 0xe0, 0xf5, 0x67, 0xa4,
57+
0xa9, 0xc0, 0x5e, 0xb4, 0x83, 0x5e, 0x27, 0x35,
58+
0x41, 0x9c, 0xf3, 0x0f, 0x83, 0x4c, 0x9c, 0xe2,
59+
0xfe, 0x34, 0x30, 0xf0, 0x21, 0xba, 0x4e, 0xac,
60+
0xd5, 0x03, 0x11, 0x2e, 0x97, 0xbc, 0xf6, 0xa0,
61+
0x22, 0xd2, 0x36, 0xd7, 0x1a, 0x93, 0x57, 0x82,
62+
0x4a, 0x2b, 0x19, 0x51, 0x5f, 0x98, 0x01, 0x31,
63+
0xb3, 0x97, 0x0b, 0x08, 0x7c, 0xad, 0xf9, 0x4c,
64+
0xc4, 0xa7,
65+
];
66+
67+
#[rustfmt::skip]
68+
static MUSIG_PARTIAL_SIG_BYTES: [u8; 40] = [
69+
0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
70+
0x28, 0x9e, 0xeb, 0x2f, 0x5e, 0xfc, 0x31, 0x4a,
71+
0xa6, 0xd8, 0x7b, 0xf5, 0x81, 0x25, 0x04, 0x3c,
72+
0x96, 0xd1, 0x5a, 0x00, 0x7d, 0xb4, 0xb6, 0xaa,
73+
0xaa, 0xc7, 0xd1, 0x80, 0x86, 0xf4, 0x9a, 0x99,
74+
];
75+
3876
fn secret_key() -> SecretKey {
3977
SecretKey::from_slice(&SK_BYTES).expect("failed to create sk from slice")
4078
}
@@ -85,3 +123,23 @@ fn cbor() {
85123
// It also adds a 1-byte length prefix and a byte of metadata for the whole vector.
86124
assert_eq!(e.len(), 54);
87125
}
126+
127+
#[test]
128+
fn musig() {
129+
let public_nonce: musig::PublicNonce = bincode::deserialize(&MUSIG_PUBLIC_NONCE_BYTES).unwrap();
130+
let ser = bincode::serialize(&public_nonce).unwrap();
131+
132+
assert_eq!(ser, MUSIG_PUBLIC_NONCE_BYTES);
133+
134+
let aggregated_nonce: musig::AggregatedNonce =
135+
bincode::deserialize(&MUSIG_AGGREGATED_NONCE_BYTES).unwrap();
136+
let ser = bincode::serialize(&aggregated_nonce).unwrap();
137+
138+
assert_eq!(ser, MUSIG_AGGREGATED_NONCE_BYTES);
139+
140+
let partial_sig: musig::PartialSignature =
141+
bincode::deserialize(&MUSIG_PARTIAL_SIG_BYTES).unwrap();
142+
let ser = bincode::serialize(&partial_sig).unwrap();
143+
144+
assert_eq!(ser, MUSIG_PARTIAL_SIG_BYTES);
145+
}

0 commit comments

Comments
 (0)