Skip to content

Commit 58f19da

Browse files
committed
musig: Rename musig constants to use _SIZE suffix
Also fixes a formatting issue.
1 parent 90ba850 commit 58f19da

File tree

2 files changed

+52
-58
lines changed

2 files changed

+52
-58
lines changed

secp256k1-sys/src/lib.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1429,77 +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-
/// Length (in bytes) of the session structure.
1452+
/// Size (in bytes) of the session structure.
14531453
/// The session object holds all state needed for a MuSig2 signing session (e.g. aggregated nonce,
14541454
/// key aggregation info, and other state necessary for computing partial signatures).
1455-
pub const MUSIG_SESSION_LEN: usize = 133;
1455+
pub const MUSIG_SESSION_SIZE: usize = 133;
14561456

1457-
/// Length (in bytes) of the internal representation of a partial signature.
1457+
/// Size (in bytes) of the internal representation of a partial signature.
14581458
/// This structure include magic bytes ([0xeb, 0xfb, 0x1a, 0x32]) alongside the actual signature scalar.
1459-
pub const MUSIG_PART_SIG_LEN: usize = 36;
1459+
pub const MUSIG_PART_SIG_SIZE: usize = 36;
14601460

14611461
#[repr(C)]
14621462
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1463-
pub struct MusigKeyAggCache([c_uchar; MUSIG_KEYAGG_LEN]);
1464-
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);
14651465
impl_raw_debug!(MusigKeyAggCache);
14661466

14671467
#[repr(C)]
14681468
#[derive(Copy, Clone, PartialEq, Eq)]
1469-
pub struct MusigSecNonce([c_uchar; MUSIG_SECNONCE_LEN]);
1470-
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);
14711471
impl_raw_debug!(MusigSecNonce);
14721472

14731473
impl MusigSecNonce {
1474-
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 {
14751475
MusigSecNonce(bytes)
14761476
}
14771477

1478-
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 }
14791479
}
14801480

14811481
#[repr(C)]
14821482
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1483-
pub struct MusigPubNonce([c_uchar; MUSIG_PUBNONCE_LEN]);
1484-
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);
14851485
impl_raw_debug!(MusigPubNonce);
14861486

14871487
#[repr(C)]
14881488
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1489-
pub struct MusigAggNonce([c_uchar; MUSIG_AGGNONCE_LEN]);
1490-
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);
14911491
impl_raw_debug!(MusigAggNonce);
14921492

14931493
#[repr(C)]
14941494
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1495-
pub struct MusigSession([c_uchar; MUSIG_SESSION_LEN]);
1496-
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);
14971497
impl_raw_debug!(MusigSession);
14981498

14991499
#[repr(C)]
15001500
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
1501-
pub struct MusigPartialSignature([c_uchar; MUSIG_PART_SIG_LEN]);
1502-
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);
15031503
impl_raw_debug!(MusigPartialSignature);
15041504

15051505
#[cfg(secp256k1_fuzz)]

src/musig.rs

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

20-
/// Serialized length (in bytes) of the aggregated nonce.
20+
/// Serialized size (in bytes) of the aggregated nonce.
2121
/// The serialized form is used for transmitting or storing the aggregated nonce.
22-
pub const AGGNONCE_SERIALIZED_LEN: usize = 66;
22+
pub const AGGNONCE_SERIALIZED_SIZE: usize = 66;
2323

24-
/// Serialized length (in bytes) of an individual public nonce.
24+
/// Serialized size (in bytes) of an individual public nonce.
2525
/// The serialized form is used for transmission between signers.
26-
pub const PUBNONCE_SERIALIZED_LEN: usize = 66;
26+
pub const PUBNONCE_SERIALIZED_SIZE: usize = 66;
2727

28-
/// Serialized length (in bytes) of a partial signature.
28+
/// Serialized size (in bytes) of a partial signature.
2929
/// The serialized form is used for transmitting partial signatures to be
3030
/// aggregated into the final signature.
31-
pub const PART_SIG_SERIALIZED_LEN: usize = 32;
31+
pub const PART_SIG_SERIALIZED_SIZE: usize = 32;
3232

3333
/// Musig parsing errors
3434
#[derive(Debug, Clone, Copy, Eq, PartialEq, PartialOrd, Ord, Hash)]
@@ -257,9 +257,9 @@ impl fmt::Display for PartialSignature {
257257
impl core::str::FromStr for PartialSignature {
258258
type Err = ParseError;
259259
fn from_str(s: &str) -> Result<Self, Self::Err> {
260-
let mut res = [0u8; PART_SIG_SERIALIZED_LEN];
260+
let mut res = [0u8; PART_SIG_SERIALIZED_SIZE];
261261
match from_hex(s, &mut res) {
262-
Ok(PART_SIG_SERIALIZED_LEN) => PartialSignature::from_byte_array(&res),
262+
Ok(PART_SIG_SERIALIZED_SIZE) => PartialSignature::from_byte_array(&res),
263263
_ => Err(ParseError::MalformedArg),
264264
}
265265
}
@@ -287,7 +287,7 @@ impl<'de> serde::Deserialize<'de> for PartialSignature {
287287
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
288288
"a raw MuSig2 partial signature",
289289
|slice| {
290-
let bytes: &[u8; PART_SIG_SERIALIZED_LEN] =
290+
let bytes: &[u8; PART_SIG_SERIALIZED_SIZE] =
291291
slice.try_into().map_err(|_| ParseError::MalformedArg)?;
292292

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

300300
impl PartialSignature {
301301
/// Serialize a PartialSignature as a byte array.
302-
pub fn serialize(&self) -> [u8; PART_SIG_SERIALIZED_LEN] {
303-
let mut data = MaybeUninit::<[u8; 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();
304304
unsafe {
305305
if ffi::secp256k1_musig_partial_sig_serialize(
306306
ffi::secp256k1_context_no_precomp,
@@ -321,9 +321,7 @@ impl PartialSignature {
321321
/// # Errors:
322322
///
323323
/// - MalformedArg: If the signature [`PartialSignature`] is out of curve order
324-
pub fn from_byte_array(
325-
data: &[u8; PART_SIG_SERIALIZED_LEN],
326-
) -> Result<Self, ParseError> {
324+
pub fn from_byte_array(data: &[u8; PART_SIG_SERIALIZED_SIZE]) -> Result<Self, ParseError> {
327325
let mut partial_sig = MaybeUninit::<ffi::MusigPartialSignature>::uninit();
328326
unsafe {
329327
if ffi::secp256k1_musig_partial_sig_parse(
@@ -686,14 +684,14 @@ impl SecretNonce {
686684
///
687685
/// See <https://blockstream.com/2019/02/18/musig-a-new-multisignature-standard/>
688686
/// for more details about these risks.
689-
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] {
690688
self.0.dangerous_into_bytes()
691689
}
692690

693691
/// Function to create a new [`SecretNonce`] from a 32 byte array.
694692
///
695693
/// Refer to the warning on [`SecretNonce::dangerous_into_bytes`] for more details.
696-
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 {
697695
SecretNonce(ffi::MusigSecNonce::dangerous_from_bytes(array))
698696
}
699697
}
@@ -727,9 +725,9 @@ impl fmt::Display for PublicNonce {
727725
impl core::str::FromStr for PublicNonce {
728726
type Err = ParseError;
729727
fn from_str(s: &str) -> Result<Self, Self::Err> {
730-
let mut res = [0u8; PUBNONCE_SERIALIZED_LEN];
728+
let mut res = [0u8; PUBNONCE_SERIALIZED_SIZE];
731729
match from_hex(s, &mut res) {
732-
Ok(PUBNONCE_SERIALIZED_LEN) => PublicNonce::from_byte_array(&res),
730+
Ok(PUBNONCE_SERIALIZED_SIZE) => PublicNonce::from_byte_array(&res),
733731
_ => Err(ParseError::MalformedArg),
734732
}
735733
}
@@ -757,7 +755,7 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
757755
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
758756
"a raw MuSig2 public nonce",
759757
|slice| {
760-
let bytes: &[u8; PUBNONCE_SERIALIZED_LEN] =
758+
let bytes: &[u8; PUBNONCE_SERIALIZED_SIZE] =
761759
slice.try_into().map_err(|_| ParseError::MalformedArg)?;
762760

763761
Self::from_byte_array(bytes)
@@ -769,8 +767,8 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
769767

770768
impl PublicNonce {
771769
/// Serialize a PublicNonce
772-
pub fn serialize(&self) -> [u8; PUBNONCE_SERIALIZED_LEN] {
773-
let mut data = [0; PUBNONCE_SERIALIZED_LEN];
770+
pub fn serialize(&self) -> [u8; PUBNONCE_SERIALIZED_SIZE] {
771+
let mut data = [0; PUBNONCE_SERIALIZED_SIZE];
774772
unsafe {
775773
if ffi::secp256k1_musig_pubnonce_serialize(
776774
ffi::secp256k1_context_no_precomp,
@@ -791,9 +789,7 @@ impl PublicNonce {
791789
/// # Errors:
792790
///
793791
/// - MalformedArg: If the [`PublicNonce`] is 132 bytes, but out of curve order
794-
pub fn from_byte_array(
795-
data: &[u8; PUBNONCE_SERIALIZED_LEN],
796-
) -> Result<Self, ParseError> {
792+
pub fn from_byte_array(data: &[u8; PUBNONCE_SERIALIZED_SIZE]) -> Result<Self, ParseError> {
797793
let mut pub_nonce = MaybeUninit::<ffi::MusigPubNonce>::uninit();
798794
unsafe {
799795
if ffi::secp256k1_musig_pubnonce_parse(
@@ -844,9 +840,9 @@ impl fmt::Display for AggregatedNonce {
844840
impl core::str::FromStr for AggregatedNonce {
845841
type Err = ParseError;
846842
fn from_str(s: &str) -> Result<Self, Self::Err> {
847-
let mut res = [0u8; AGGNONCE_SERIALIZED_LEN];
843+
let mut res = [0u8; AGGNONCE_SERIALIZED_SIZE];
848844
match from_hex(s, &mut res) {
849-
Ok(AGGNONCE_SERIALIZED_LEN) => AggregatedNonce::from_byte_array(&res),
845+
Ok(AGGNONCE_SERIALIZED_SIZE) => AggregatedNonce::from_byte_array(&res),
850846
_ => Err(ParseError::MalformedArg),
851847
}
852848
}
@@ -874,7 +870,7 @@ impl<'de> serde::Deserialize<'de> for AggregatedNonce {
874870
d.deserialize_bytes(super::serde_util::BytesVisitor::new(
875871
"a raw MuSig2 aggregated nonce",
876872
|slice| {
877-
let bytes: &[u8; AGGNONCE_SERIALIZED_LEN] =
873+
let bytes: &[u8; AGGNONCE_SERIALIZED_SIZE] =
878874
slice.try_into().map_err(|_| ParseError::MalformedArg)?;
879875

880876
Self::from_byte_array(bytes)
@@ -954,8 +950,8 @@ impl AggregatedNonce {
954950
}
955951

956952
/// Serialize a AggregatedNonce into a 66 bytes array.
957-
pub fn serialize(&self) -> [u8; AGGNONCE_SERIALIZED_LEN] {
958-
let mut data = [0; AGGNONCE_SERIALIZED_LEN];
953+
pub fn serialize(&self) -> [u8; AGGNONCE_SERIALIZED_SIZE] {
954+
let mut data = [0; AGGNONCE_SERIALIZED_SIZE];
959955
unsafe {
960956
if ffi::secp256k1_musig_aggnonce_serialize(
961957
ffi::secp256k1_context_no_precomp,
@@ -976,9 +972,7 @@ impl AggregatedNonce {
976972
/// # Errors:
977973
///
978974
/// - MalformedArg: If the byte slice is 66 bytes, but the [`AggregatedNonce`] is invalid
979-
pub fn from_byte_array(
980-
data: &[u8; AGGNONCE_SERIALIZED_LEN],
981-
) -> Result<Self, ParseError> {
975+
pub fn from_byte_array(data: &[u8; AGGNONCE_SERIALIZED_SIZE]) -> Result<Self, ParseError> {
982976
let mut aggnonce = MaybeUninit::<ffi::MusigAggNonce>::uninit();
983977
unsafe {
984978
if ffi::secp256k1_musig_aggnonce_parse(

0 commit comments

Comments
 (0)