@@ -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 ) ]
2235pub enum ParseError {
@@ -244,9 +257,9 @@ impl fmt::Display for PartialSignature {
244257impl 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
287300impl 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 {
714725impl 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
757768impl 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 {
831840impl 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