Skip to content

Commit f4c13b7

Browse files
committed
Merge #803: Make musig serialized length constants part of the API of secp256k1::musig
58f19da musig: Rename musig constants to use _SIZE suffix (Daniel Roberts) 90ba850 musig: move serialized data length constants (Daniel Roberts) 5f027ac musig: reword docs for serialized data length constants (Daniel Roberts) Pull request description: This PR: - (hopefully) disambiguates the documentation for these constants. Less is more I think, the serialized format is meant to be opaque afaik(?). - Moves the serialized size constants into the `musig` module (they aren't used in sys-secp256k1 module at all, and there's no precedent for such constants in there). - Drops the `MUSIG_` prefix from these constants under the assumption they are unambiguous in-context (can always refer to them with `musig::CONSTANT` anyway - Leaves the "private" size constants in sys-secp256k1 because they're used there, and even though there's no precedent, it seems reasonable - Renames the `_LEN` suffix to `_SIZE` to match other constants in rust-secp256k1 ACKs for top commit: apoelstra: ACK 58f19da; successfully ran local tests Tree-SHA512: f054c74df1924cc0eed04e7a9173cc3c15882cd41e0ad08fb84f59a3ed8c8bf4e9d04ca2a8c07faa4189b91fa03d4097458ff95c00abe6e7eb44e87bf3145fb8
2 parents 80e78a5 + 58f19da commit f4c13b7

File tree

2 files changed

+59
-66
lines changed

2 files changed

+59
-66
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,91 +1429,77 @@ impl<T: CPtr> CPtr for Option<T> {
14291429
}
14301430
}
14311431
}
1432-
/// Total length (in bytes) of the key aggregation context.
1432+
/// Total size (in bytes) of the key aggregation context.
14331433
/// This structure packs all metadata needed for aggregating individual public keys
14341434
/// into a single MuSig2 aggregated key (including coefficients and any extra metadata).
1435-
pub const MUSIG_KEYAGG_LEN: usize = 197;
1435+
pub const MUSIG_KEYAGG_SIZE: usize = 197;
14361436

1437-
/// Length (in bytes) of the secret nonce structure used in a MuSig2 session.
1437+
/// Size (in bytes) of the secret nonce structure used in a MuSig2 session.
14381438
/// It holds the secret (ephemeral) nonces used internally for nonce derivation
14391439
/// before the corresponding public nonces are computed.
1440-
pub const MUSIG_SECNONCE_LEN: usize = 132;
1440+
pub const MUSIG_SECNONCE_SIZE: usize = 132;
14411441

1442-
/// Length (in bytes) of the public nonce structure.
1442+
/// Size (in bytes) of the public nonce structure.
14431443
/// This is derived from the secret nonce and shared among participants to build
14441444
/// nonce commitments in the MuSig2 protocol.
1445-
pub const MUSIG_PUBNONCE_LEN: usize = 132;
1445+
pub const MUSIG_PUBNONCE_SIZE: usize = 132;
14461446

1447-
/// Length (in bytes) of the aggregated nonce structure.
1447+
/// Size (in bytes) of the aggregated nonce structure.
14481448
/// Represents the combined nonce obtained by aggregating the individual public nonces
14491449
/// from all participants for the final signature computation.
1450-
pub const MUSIG_AGGNONCE_LEN: usize = 132;
1450+
pub const MUSIG_AGGNONCE_SIZE: usize = 132;
14511451

1452-
/// Serialized length (in bytes) of the aggregated nonce.
1453-
/// This is the compact form (typically using a compressed representation) used for
1454-
/// transmitting or storing the aggregated nonce.
1455-
pub const MUSIG_AGGNONCE_SERIALIZED_LEN: usize = 66;
1456-
1457-
/// Serialized length (in bytes) of an individual public nonce.
1458-
/// This compact serialized form is what gets exchanged between signers.
1459-
pub const MUSIG_PUBNONCE_SERIALIZED_LEN: usize = 66;
1460-
1461-
/// Length (in bytes) of the session structure.
1452+
/// Size (in bytes) of the session structure.
14621453
/// The session object holds all state needed for a MuSig2 signing session (e.g. aggregated nonce,
14631454
/// key aggregation info, and other state necessary for computing partial signatures).
1464-
pub const MUSIG_SESSION_LEN: usize = 133;
1455+
pub const MUSIG_SESSION_SIZE: usize = 133;
14651456

1466-
/// Length (in bytes) of the internal representation of a partial signature.
1457+
/// Size (in bytes) of the internal representation of a partial signature.
14671458
/// This structure include magic bytes ([0xeb, 0xfb, 0x1a, 0x32]) alongside the actual signature scalar.
1468-
pub const MUSIG_PART_SIG_LEN: usize = 36;
1469-
1470-
/// Serialized length (in bytes) of a partial signature.
1471-
/// This is the compact form (typically just the 32-byte scalar) that is used when communicating
1472-
/// partial signatures to be combined into the final signature.
1473-
pub const MUSIG_PART_SIG_SERIALIZED_LEN: usize = 32;
1459+
pub const MUSIG_PART_SIG_SIZE: usize = 36;
14741460

14751461
#[repr(C)]
14761462
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1477-
pub struct MusigKeyAggCache([c_uchar; MUSIG_KEYAGG_LEN]);
1478-
impl_array_newtype!(MusigKeyAggCache, c_uchar, MUSIG_KEYAGG_LEN);
1463+
pub struct MusigKeyAggCache([c_uchar; MUSIG_KEYAGG_SIZE]);
1464+
impl_array_newtype!(MusigKeyAggCache, c_uchar, MUSIG_KEYAGG_SIZE);
14791465
impl_raw_debug!(MusigKeyAggCache);
14801466

14811467
#[repr(C)]
14821468
#[derive(Copy, Clone, PartialEq, Eq)]
1483-
pub struct MusigSecNonce([c_uchar; MUSIG_SECNONCE_LEN]);
1484-
impl_array_newtype!(MusigSecNonce, c_uchar, MUSIG_SECNONCE_LEN);
1469+
pub struct MusigSecNonce([c_uchar; MUSIG_SECNONCE_SIZE]);
1470+
impl_array_newtype!(MusigSecNonce, c_uchar, MUSIG_SECNONCE_SIZE);
14851471
impl_raw_debug!(MusigSecNonce);
14861472

14871473
impl MusigSecNonce {
1488-
pub fn dangerous_from_bytes(bytes: [c_uchar; MUSIG_SECNONCE_LEN]) -> Self {
1474+
pub fn dangerous_from_bytes(bytes: [c_uchar; MUSIG_SECNONCE_SIZE]) -> Self {
14891475
MusigSecNonce(bytes)
14901476
}
14911477

1492-
pub fn dangerous_into_bytes(self) -> [c_uchar; MUSIG_SECNONCE_LEN] { self.0 }
1478+
pub fn dangerous_into_bytes(self) -> [c_uchar; MUSIG_SECNONCE_SIZE] { self.0 }
14931479
}
14941480

14951481
#[repr(C)]
14961482
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1497-
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_LEN]);
1498-
impl_array_newtype!(MusigPubNonce, c_uchar, MUSIG_PUBNONCE_LEN);
1483+
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_SIZE]);
1484+
impl_array_newtype!(MusigPubNonce, c_uchar, MUSIG_PUBNONCE_SIZE);
14991485
impl_raw_debug!(MusigPubNonce);
15001486

15011487
#[repr(C)]
15021488
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1503-
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_LEN]);
1504-
impl_array_newtype!(MusigAggNonce, c_uchar, MUSIG_AGGNONCE_LEN);
1489+
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_SIZE]);
1490+
impl_array_newtype!(MusigAggNonce, c_uchar, MUSIG_AGGNONCE_SIZE);
15051491
impl_raw_debug!(MusigAggNonce);
15061492

15071493
#[repr(C)]
15081494
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1509-
pub struct MusigSession([c_uchar; MUSIG_SESSION_LEN]);
1510-
impl_array_newtype!(MusigSession, c_uchar, MUSIG_SESSION_LEN);
1495+
pub struct MusigSession([c_uchar; MUSIG_SESSION_SIZE]);
1496+
impl_array_newtype!(MusigSession, c_uchar, MUSIG_SESSION_SIZE);
15111497
impl_raw_debug!(MusigSession);
15121498

15131499
#[repr(C)]
15141500
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1515-
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_LEN]);
1516-
impl_array_newtype!(MusigPartialSignature, c_uchar, MUSIG_PART_SIG_LEN);
1501+
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_SIZE]);
1502+
impl_array_newtype!(MusigPartialSignature, c_uchar, MUSIG_PART_SIG_SIZE);
15171503
impl_raw_debug!(MusigPartialSignature);
15181504

15191505
#[cfg(secp256k1_fuzz)]

src/musig.rs

Lines changed: 33 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,19 @@ use crate::{
1717
Verification, XOnlyPublicKey,
1818
};
1919

20+
/// Serialized size (in bytes) of the aggregated nonce.
21+
/// The serialized form is used for transmitting or storing the aggregated nonce.
22+
pub const AGGNONCE_SERIALIZED_SIZE: usize = 66;
23+
24+
/// Serialized size (in bytes) of an individual public nonce.
25+
/// The serialized form is used for transmission between signers.
26+
pub const PUBNONCE_SERIALIZED_SIZE: usize = 66;
27+
28+
/// Serialized size (in bytes) of a partial signature.
29+
/// The serialized form is used for transmitting partial signatures to be
30+
/// aggregated into the final signature.
31+
pub const PART_SIG_SERIALIZED_SIZE: usize = 32;
32+
2033
/// Musig parsing errors
2134
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
2235
pub enum ParseError {
@@ -244,9 +257,9 @@ impl fmt::Display for PartialSignature {
244257
impl core::str::FromStr for PartialSignature {
245258
type Err = ParseError;
246259
fn from_str(s: &str) -> Result<Self, Self::Err> {
247-
let mut res = [0u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN];
260+
let mut res = [0u8; PART_SIG_SERIALIZED_SIZE];
248261
match from_hex(s, &mut res) {
249-
Ok(ffi::MUSIG_PART_SIG_SERIALIZED_LEN) => PartialSignature::from_byte_array(&res),
262+
Ok(PART_SIG_SERIALIZED_SIZE) => PartialSignature::from_byte_array(&res),
250263
_ => Err(ParseError::MalformedArg),
251264
}
252265
}
@@ -274,7 +287,7 @@ impl<'de> serde::Deserialize<'de> for PartialSignature {
274287
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
275288
"a raw MuSig2 partial signature",
276289
|slice| {
277-
let bytes: &[u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN] =
290+
let bytes: &[u8; PART_SIG_SERIALIZED_SIZE] =
278291
slice.try_into().map_err(|_| ParseError::MalformedArg)?;
279292

280293
Self::from_byte_array(bytes)
@@ -286,8 +299,8 @@ impl<'de> serde::Deserialize<'de> for PartialSignature {
286299

287300
impl PartialSignature {
288301
/// Serialize a PartialSignature as a byte array.
289-
pub fn serialize(&self) -> [u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN] {
290-
let mut data = MaybeUninit::<[u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN]>::uninit();
302+
pub fn serialize(&self) -> [u8; PART_SIG_SERIALIZED_SIZE] {
303+
let mut data = MaybeUninit::<[u8; PART_SIG_SERIALIZED_SIZE]>::uninit();
291304
unsafe {
292305
if ffi::secp256k1_musig_partial_sig_serialize(
293306
ffi::secp256k1_context_no_precomp,
@@ -308,9 +321,7 @@ impl PartialSignature {
308321
/// # Errors:
309322
///
310323
/// - MalformedArg: If the signature [`PartialSignature`] is out of curve order
311-
pub fn from_byte_array(
312-
data: &[u8; ffi::MUSIG_PART_SIG_SERIALIZED_LEN],
313-
) -> Result<Self, ParseError> {
324+
pub fn from_byte_array(data: &[u8; PART_SIG_SERIALIZED_SIZE]) -> Result<Self, ParseError> {
314325
let mut partial_sig = MaybeUninit::<ffi::MusigPartialSignature>::uninit();
315326
unsafe {
316327
if ffi::secp256k1_musig_partial_sig_parse(
@@ -673,14 +684,14 @@ impl SecretNonce {
673684
///
674685
/// See <https://blockstream.com/2019/02/18/musig-a-new-multisignature-standard/>
675686
/// for more details about these risks.
676-
pub fn dangerous_into_bytes(self) -> [u8; secp256k1_sys::MUSIG_SECNONCE_LEN] {
687+
pub fn dangerous_into_bytes(self) -> [u8; secp256k1_sys::MUSIG_SECNONCE_SIZE] {
677688
self.0.dangerous_into_bytes()
678689
}
679690

680691
/// Function to create a new [`SecretNonce`] from a 32 byte array.
681692
///
682693
/// Refer to the warning on [`SecretNonce::dangerous_into_bytes`] for more details.
683-
pub fn dangerous_from_bytes(array: [u8; secp256k1_sys::MUSIG_SECNONCE_LEN]) -> Self {
694+
pub fn dangerous_from_bytes(array: [u8; secp256k1_sys::MUSIG_SECNONCE_SIZE]) -> Self {
684695
SecretNonce(ffi::MusigSecNonce::dangerous_from_bytes(array))
685696
}
686697
}
@@ -714,9 +725,9 @@ impl fmt::Display for PublicNonce {
714725
impl core::str::FromStr for PublicNonce {
715726
type Err = ParseError;
716727
fn from_str(s: &str) -> Result<Self, Self::Err> {
717-
let mut res = [0u8; ffi::MUSIG_PUBNONCE_SERIALIZED_LEN];
728+
let mut res = [0u8; PUBNONCE_SERIALIZED_SIZE];
718729
match from_hex(s, &mut res) {
719-
Ok(ffi::MUSIG_PUBNONCE_SERIALIZED_LEN) => PublicNonce::from_byte_array(&res),
730+
Ok(PUBNONCE_SERIALIZED_SIZE) => PublicNonce::from_byte_array(&res),
720731
_ => Err(ParseError::MalformedArg),
721732
}
722733
}
@@ -744,7 +755,7 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
744755
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
745756
"a raw MuSig2 public nonce",
746757
|slice| {
747-
let bytes: &[u8; ffi::MUSIG_PUBNONCE_SERIALIZED_LEN] =
758+
let bytes: &[u8; PUBNONCE_SERIALIZED_SIZE] =
748759
slice.try_into().map_err(|_| ParseError::MalformedArg)?;
749760

750761
Self::from_byte_array(bytes)
@@ -756,8 +767,8 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
756767

757768
impl PublicNonce {
758769
/// Serialize a PublicNonce
759-
pub fn serialize(&self) -> [u8; ffi::MUSIG_PUBNONCE_SERIALIZED_LEN] {
760-
let mut data = [0; ffi::MUSIG_PUBNONCE_SERIALIZED_LEN];
770+
pub fn serialize(&self) -> [u8; PUBNONCE_SERIALIZED_SIZE] {
771+
let mut data = [0; PUBNONCE_SERIALIZED_SIZE];
761772
unsafe {
762773
if ffi::secp256k1_musig_pubnonce_serialize(
763774
ffi::secp256k1_context_no_precomp,
@@ -778,9 +789,7 @@ impl PublicNonce {
778789
/// # Errors:
779790
///
780791
/// - MalformedArg: If the [`PublicNonce`] is 132 bytes, but out of curve order
781-
pub fn from_byte_array(
782-
data: &[u8; ffi::MUSIG_PUBNONCE_SERIALIZED_LEN],
783-
) -> Result<Self, ParseError> {
792+
pub fn from_byte_array(data: &[u8; PUBNONCE_SERIALIZED_SIZE]) -> Result<Self, ParseError> {
784793
let mut pub_nonce = MaybeUninit::<ffi::MusigPubNonce>::uninit();
785794
unsafe {
786795
if ffi::secp256k1_musig_pubnonce_parse(
@@ -831,9 +840,9 @@ impl fmt::Display for AggregatedNonce {
831840
impl core::str::FromStr for AggregatedNonce {
832841
type Err = ParseError;
833842
fn from_str(s: &str) -> Result<Self, Self::Err> {
834-
let mut res = [0u8; ffi::MUSIG_AGGNONCE_SERIALIZED_LEN];
843+
let mut res = [0u8; AGGNONCE_SERIALIZED_SIZE];
835844
match from_hex(s, &mut res) {
836-
Ok(ffi::MUSIG_AGGNONCE_SERIALIZED_LEN) => AggregatedNonce::from_byte_array(&res),
845+
Ok(AGGNONCE_SERIALIZED_SIZE) => AggregatedNonce::from_byte_array(&res),
837846
_ => Err(ParseError::MalformedArg),
838847
}
839848
}
@@ -861,7 +870,7 @@ impl<'de> serde::Deserialize<'de> for AggregatedNonce {
861870
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
862871
"a raw MuSig2 aggregated nonce",
863872
|slice| {
864-
let bytes: &[u8; ffi::MUSIG_AGGNONCE_SERIALIZED_LEN] =
873+
let bytes: &[u8; AGGNONCE_SERIALIZED_SIZE] =
865874
slice.try_into().map_err(|_| ParseError::MalformedArg)?;
866875

867876
Self::from_byte_array(bytes)
@@ -941,8 +950,8 @@ impl AggregatedNonce {
941950
}
942951

943952
/// Serialize a AggregatedNonce into a 66 bytes array.
944-
pub fn serialize(&self) -> [u8; ffi::MUSIG_AGGNONCE_SERIALIZED_LEN] {
945-
let mut data = [0; ffi::MUSIG_AGGNONCE_SERIALIZED_LEN];
953+
pub fn serialize(&self) -> [u8; AGGNONCE_SERIALIZED_SIZE] {
954+
let mut data = [0; AGGNONCE_SERIALIZED_SIZE];
946955
unsafe {
947956
if ffi::secp256k1_musig_aggnonce_serialize(
948957
ffi::secp256k1_context_no_precomp,
@@ -963,9 +972,7 @@ impl AggregatedNonce {
963972
/// # Errors:
964973
///
965974
/// - MalformedArg: If the byte slice is 66 bytes, but the [`AggregatedNonce`] is invalid
966-
pub fn from_byte_array(
967-
data: &[u8; ffi::MUSIG_AGGNONCE_SERIALIZED_LEN],
968-
) -> Result<Self, ParseError> {
975+
pub fn from_byte_array(data: &[u8; AGGNONCE_SERIALIZED_SIZE]) -> Result<Self, ParseError> {
969976
let mut aggnonce = MaybeUninit::<ffi::MusigAggNonce>::uninit();
970977
unsafe {
971978
if ffi::secp256k1_musig_aggnonce_parse(

0 commit comments

Comments
 (0)