@@ -17,6 +17,19 @@ use crate::{
1717 Verification , XOnlyPublicKey ,
1818} ;
1919
20+ /// Serialized length (in bytes) of the aggregated nonce.
21+ /// The serialized form is used for transmitting or storing the aggregated nonce.
22+ pub const AGGNONCE_SERIALIZED_LEN : usize = 66 ;
23+
24+ /// Serialized length (in bytes) of an individual public nonce.
25+ /// The serialized form is used for transmission between signers.
26+ pub const PUBNONCE_SERIALIZED_LEN : usize = 66 ;
27+
28+ /// Serialized length (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_LEN : 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_LEN ] ;
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_LEN ) => 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_LEN ] =
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_LEN ] {
303+ let mut data = MaybeUninit :: < [ u8 ; PART_SIG_SERIALIZED_LEN ] > :: uninit ( ) ;
291304 unsafe {
292305 if ffi:: secp256k1_musig_partial_sig_serialize (
293306 ffi:: secp256k1_context_no_precomp,
@@ -309,7 +322,7 @@ impl PartialSignature {
309322 ///
310323 /// - MalformedArg: If the signature [`PartialSignature`] is out of curve order
311324 pub fn from_byte_array (
312- data : & [ u8 ; ffi :: MUSIG_PART_SIG_SERIALIZED_LEN ] ,
325+ data : & [ u8 ; PART_SIG_SERIALIZED_LEN ] ,
313326 ) -> Result < Self , ParseError > {
314327 let mut partial_sig = MaybeUninit :: < ffi:: MusigPartialSignature > :: uninit ( ) ;
315328 unsafe {
@@ -714,9 +727,9 @@ impl fmt::Display for PublicNonce {
714727impl core:: str:: FromStr for PublicNonce {
715728 type Err = ParseError ;
716729 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
717- let mut res = [ 0u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] ;
730+ let mut res = [ 0u8 ; PUBNONCE_SERIALIZED_LEN ] ;
718731 match from_hex ( s, & mut res) {
719- Ok ( ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ) => PublicNonce :: from_byte_array ( & res) ,
732+ Ok ( PUBNONCE_SERIALIZED_LEN ) => PublicNonce :: from_byte_array ( & res) ,
720733 _ => Err ( ParseError :: MalformedArg ) ,
721734 }
722735 }
@@ -744,7 +757,7 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
744757 d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
745758 "a raw MuSig2 public nonce" ,
746759 |slice| {
747- let bytes: & [ u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] =
760+ let bytes: & [ u8 ; PUBNONCE_SERIALIZED_LEN ] =
748761 slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
749762
750763 Self :: from_byte_array ( bytes)
@@ -756,8 +769,8 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
756769
757770impl PublicNonce {
758771 /// Serialize a PublicNonce
759- pub fn serialize ( & self ) -> [ u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] {
760- let mut data = [ 0 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] ;
772+ pub fn serialize ( & self ) -> [ u8 ; PUBNONCE_SERIALIZED_LEN ] {
773+ let mut data = [ 0 ; PUBNONCE_SERIALIZED_LEN ] ;
761774 unsafe {
762775 if ffi:: secp256k1_musig_pubnonce_serialize (
763776 ffi:: secp256k1_context_no_precomp,
@@ -779,7 +792,7 @@ impl PublicNonce {
779792 ///
780793 /// - MalformedArg: If the [`PublicNonce`] is 132 bytes, but out of curve order
781794 pub fn from_byte_array (
782- data : & [ u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] ,
795+ data : & [ u8 ; PUBNONCE_SERIALIZED_LEN ] ,
783796 ) -> Result < Self , ParseError > {
784797 let mut pub_nonce = MaybeUninit :: < ffi:: MusigPubNonce > :: uninit ( ) ;
785798 unsafe {
@@ -831,9 +844,9 @@ impl fmt::Display for AggregatedNonce {
831844impl core:: str:: FromStr for AggregatedNonce {
832845 type Err = ParseError ;
833846 fn from_str ( s : & str ) -> Result < Self , Self :: Err > {
834- let mut res = [ 0u8 ; ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ] ;
847+ let mut res = [ 0u8 ; AGGNONCE_SERIALIZED_LEN ] ;
835848 match from_hex ( s, & mut res) {
836- Ok ( ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ) => AggregatedNonce :: from_byte_array ( & res) ,
849+ Ok ( AGGNONCE_SERIALIZED_LEN ) => AggregatedNonce :: from_byte_array ( & res) ,
837850 _ => Err ( ParseError :: MalformedArg ) ,
838851 }
839852 }
@@ -861,7 +874,7 @@ impl<'de> serde::Deserialize<'de> for AggregatedNonce {
861874 d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
862875 "a raw MuSig2 aggregated nonce" ,
863876 |slice| {
864- let bytes: & [ u8 ; ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ] =
877+ let bytes: & [ u8 ; AGGNONCE_SERIALIZED_LEN ] =
865878 slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
866879
867880 Self :: from_byte_array ( bytes)
@@ -941,8 +954,8 @@ impl AggregatedNonce {
941954 }
942955
943956 /// 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 ] ;
957+ pub fn serialize ( & self ) -> [ u8 ; AGGNONCE_SERIALIZED_LEN ] {
958+ let mut data = [ 0 ; AGGNONCE_SERIALIZED_LEN ] ;
946959 unsafe {
947960 if ffi:: secp256k1_musig_aggnonce_serialize (
948961 ffi:: secp256k1_context_no_precomp,
@@ -964,7 +977,7 @@ impl AggregatedNonce {
964977 ///
965978 /// - MalformedArg: If the byte slice is 66 bytes, but the [`AggregatedNonce`] is invalid
966979 pub fn from_byte_array (
967- data : & [ u8 ; ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ] ,
980+ data : & [ u8 ; AGGNONCE_SERIALIZED_LEN ] ,
968981 ) -> Result < Self , ParseError > {
969982 let mut aggnonce = MaybeUninit :: < ffi:: MusigAggNonce > :: uninit ( ) ;
970983 unsafe {
0 commit comments