@@ -17,6 +17,19 @@ use crate::{
17
17
Verification , XOnlyPublicKey ,
18
18
} ;
19
19
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
+
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_SIZE ] ;
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_SIZE ) => 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_SIZE ] =
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_SIZE ] {
303
+ let mut data = MaybeUninit :: < [ u8 ; PART_SIG_SERIALIZED_SIZE ] > :: uninit ( ) ;
291
304
unsafe {
292
305
if ffi:: secp256k1_musig_partial_sig_serialize (
293
306
ffi:: secp256k1_context_no_precomp,
@@ -308,9 +321,7 @@ impl PartialSignature {
308
321
/// # Errors:
309
322
///
310
323
/// - 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 > {
314
325
let mut partial_sig = MaybeUninit :: < ffi:: MusigPartialSignature > :: uninit ( ) ;
315
326
unsafe {
316
327
if ffi:: secp256k1_musig_partial_sig_parse (
@@ -673,14 +684,14 @@ impl SecretNonce {
673
684
///
674
685
/// See <https://blockstream.com/2019/02/18/musig-a-new-multisignature-standard/>
675
686
/// 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 ] {
677
688
self . 0 . dangerous_into_bytes ( )
678
689
}
679
690
680
691
/// Function to create a new [`SecretNonce`] from a 32 byte array.
681
692
///
682
693
/// 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 {
684
695
SecretNonce ( ffi:: MusigSecNonce :: dangerous_from_bytes ( array) )
685
696
}
686
697
}
@@ -714,9 +725,9 @@ impl fmt::Display for PublicNonce {
714
725
impl core:: str:: FromStr for PublicNonce {
715
726
type Err = ParseError ;
716
727
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 ] ;
718
729
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) ,
720
731
_ => Err ( ParseError :: MalformedArg ) ,
721
732
}
722
733
}
@@ -744,7 +755,7 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
744
755
d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
745
756
"a raw MuSig2 public nonce" ,
746
757
|slice| {
747
- let bytes: & [ u8 ; ffi :: MUSIG_PUBNONCE_SERIALIZED_LEN ] =
758
+ let bytes: & [ u8 ; PUBNONCE_SERIALIZED_SIZE ] =
748
759
slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
749
760
750
761
Self :: from_byte_array ( bytes)
@@ -756,8 +767,8 @@ impl<'de> serde::Deserialize<'de> for PublicNonce {
756
767
757
768
impl PublicNonce {
758
769
/// 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 ] ;
761
772
unsafe {
762
773
if ffi:: secp256k1_musig_pubnonce_serialize (
763
774
ffi:: secp256k1_context_no_precomp,
@@ -778,9 +789,7 @@ impl PublicNonce {
778
789
/// # Errors:
779
790
///
780
791
/// - 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 > {
784
793
let mut pub_nonce = MaybeUninit :: < ffi:: MusigPubNonce > :: uninit ( ) ;
785
794
unsafe {
786
795
if ffi:: secp256k1_musig_pubnonce_parse (
@@ -831,9 +840,9 @@ impl fmt::Display for AggregatedNonce {
831
840
impl core:: str:: FromStr for AggregatedNonce {
832
841
type Err = ParseError ;
833
842
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 ] ;
835
844
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) ,
837
846
_ => Err ( ParseError :: MalformedArg ) ,
838
847
}
839
848
}
@@ -861,7 +870,7 @@ impl<'de> serde::Deserialize<'de> for AggregatedNonce {
861
870
d. deserialize_bytes ( super :: serde_util:: BytesVisitor :: new (
862
871
"a raw MuSig2 aggregated nonce" ,
863
872
|slice| {
864
- let bytes: & [ u8 ; ffi :: MUSIG_AGGNONCE_SERIALIZED_LEN ] =
873
+ let bytes: & [ u8 ; AGGNONCE_SERIALIZED_SIZE ] =
865
874
slice. try_into ( ) . map_err ( |_| ParseError :: MalformedArg ) ?;
866
875
867
876
Self :: from_byte_array ( bytes)
@@ -941,8 +950,8 @@ impl AggregatedNonce {
941
950
}
942
951
943
952
/// 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 ] ;
946
955
unsafe {
947
956
if ffi:: secp256k1_musig_aggnonce_serialize (
948
957
ffi:: secp256k1_context_no_precomp,
@@ -963,9 +972,7 @@ impl AggregatedNonce {
963
972
/// # Errors:
964
973
///
965
974
/// - 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 > {
969
976
let mut aggnonce = MaybeUninit :: < ffi:: MusigAggNonce > :: uninit ( ) ;
970
977
unsafe {
971
978
if ffi:: secp256k1_musig_aggnonce_parse (
0 commit comments