@@ -17,6 +17,19 @@ use crate::{
17
17
Verification , XOnlyPublicKey ,
18
18
} ;
19
19
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
+
20
33
/// Musig parsing errors
21
34
#[ derive( Debug , Clone , Copy , Eq , PartialEq , PartialOrd , Ord , Hash ) ]
22
35
pub enum ParseError {
@@ -244,9 +257,9 @@ impl fmt::Display for PartialSignature {
244
257
impl core:: str:: FromStr for PartialSignature {
245
258
type Err = ParseError ;
246
259
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 ] ;
248
261
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) ,
250
263
_ => Err ( ParseError :: MalformedArg ) ,
251
264
}
252
265
}
@@ -274,7 +287,7 @@ impl<'de> serde::Deserialize<'de> for PartialSignature {
274
287
d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
275
288
"a raw MuSig2 partial signature" ,
276
289
|slice| {
277
- let bytes: & [ u8 ; ffi :: MUSIG_PART_SIG_SERIALIZED_LEN ] =
290
+ let bytes: & [ u8 ; PART_SIG_SERIALIZED_LEN ] =
278
291
slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
279
292
280
293
Self :: from_byte_array ( bytes)
@@ -286,8 +299,8 @@ impl<'de> serde::Deserialize<'de> for PartialSignature {
286
299
287
300
impl PartialSignature {
288
301
/// 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 ( ) ;
291
304
unsafe {
292
305
if ffi:: secp256k1_musig_partial_sig_serialize (
293
306
ffi:: secp256k1_context_no_precomp,
@@ -309,7 +322,7 @@ impl PartialSignature {
309
322
///
310
323
/// - MalformedArg: If the signature [`PartialSignature`] is out of curve order
311
324
pub fn from_byte_array (
312
- data : & [ u8 ; ffi :: MUSIG_PART_SIG_SERIALIZED_LEN ] ,
325
+ data : & [ u8 ; PART_SIG_SERIALIZED_LEN ] ,
313
326
) -> Result < Self , ParseError > {
314
327
let mut partial_sig = MaybeUninit :: < ffi:: MusigPartialSignature > :: uninit ( ) ;
315
328
unsafe {
@@ -714,9 +727,9 @@ impl fmt::Display for PublicNonce {
714
727
impl core:: str:: FromStr for PublicNonce {
715
728
type Err = ParseError ;
716
729
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 ] ;
718
731
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) ,
720
733
_ => Err ( ParseError :: MalformedArg ) ,
721
734
}
722
735
}
@@ -744,7 +757,7 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
744
757
d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
745
758
"a raw MuSig2 public nonce" ,
746
759
|slice| {
747
- let bytes: & [ u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] =
760
+ let bytes: & [ u8 ; PUBNONCE_SERIALIZED_LEN ] =
748
761
slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
749
762
750
763
Self :: from_byte_array ( bytes)
@@ -756,8 +769,8 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
756
769
757
770
impl PublicNonce {
758
771
/// 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 ] ;
761
774
unsafe {
762
775
if ffi:: secp256k1_musig_pubnonce_serialize (
763
776
ffi:: secp256k1_context_no_precomp,
@@ -779,7 +792,7 @@ impl PublicNonce {
779
792
///
780
793
/// - MalformedArg: If the [`PublicNonce`] is 132 bytes, but out of curve order
781
794
pub fn from_byte_array (
782
- data : & [ u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] ,
795
+ data : & [ u8 ; PUBNONCE_SERIALIZED_LEN ] ,
783
796
) -> Result < Self , ParseError > {
784
797
let mut pub_nonce = MaybeUninit :: < ffi:: MusigPubNonce > :: uninit ( ) ;
785
798
unsafe {
@@ -831,9 +844,9 @@ impl fmt::Display for AggregatedNonce {
831
844
impl core:: str:: FromStr for AggregatedNonce {
832
845
type Err = ParseError ;
833
846
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 ] ;
835
848
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) ,
837
850
_ => Err ( ParseError :: MalformedArg ) ,
838
851
}
839
852
}
@@ -861,7 +874,7 @@ impl<'de> serde::Deserialize<'de> for AggregatedNonce {
861
874
d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
862
875
"a raw MuSig2 aggregated nonce" ,
863
876
|slice| {
864
- let bytes: & [ u8 ; ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ] =
877
+ let bytes: & [ u8 ; AGGNONCE_SERIALIZED_LEN ] =
865
878
slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
866
879
867
880
Self :: from_byte_array ( bytes)
@@ -941,8 +954,8 @@ impl AggregatedNonce {
941
954
}
942
955
943
956
/// 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 ] ;
946
959
unsafe {
947
960
if ffi:: secp256k1_musig_aggnonce_serialize (
948
961
ffi:: secp256k1_context_no_precomp,
@@ -964,7 +977,7 @@ impl AggregatedNonce {
964
977
///
965
978
/// - MalformedArg: If the byte slice is 66 bytes, but the [`AggregatedNonce`] is invalid
966
979
pub fn from_byte_array (
967
- data : & [ u8 ; ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ] ,
980
+ data : & [ u8 ; AGGNONCE_SERIALIZED_LEN ] ,
968
981
) -> Result < Self , ParseError > {
969
982
let mut aggnonce = MaybeUninit :: < ffi:: MusigAggNonce > :: uninit ( ) ;
970
983
unsafe {
0 commit comments