31
31
//! // Since we're using deterministic nonces it's important we only use the session id once
32
32
//! let my_nonces = musig.gen_nonces(&keylist, b"session-id-1337");
33
33
//! // send this to the other parties
34
- //! let my_public_nonce = my_nonces[0].public;
34
+ //! let my_public_nonce = my_nonces[0].public() ;
35
35
//! # let nonces = musig.gen_nonces(&_keylist, b"session-id-1337");
36
- //! # let p1_nonce = nonces[0].public;
37
- //! # let p3_nonce = nonces[1].public;
38
- //! # let mut _session = musig.start_sign_session_deterministic(&_keylist, my_nonces.iter().map(|n| n.public), b"session-id-1337", message).unwrap();
36
+ //! # let p1_nonce = nonces[0].public() ;
37
+ //! # let p3_nonce = nonces[1].public() ;
38
+ //! # let mut _session = musig.start_sign_session_deterministic(&_keylist, my_nonces.iter().map(|n| n.public() ), b"session-id-1337", message).unwrap();
39
39
//! // Once you've got the nonces from the other two (p1_nonce and p3_nonce) you can start the signing session.
40
40
//! let mut session = musig.start_sign_session(&keylist, my_nonces, [p1_nonce, p3_nonce], message).unwrap();
41
41
//! // but since we're using deterministic nonce generation we can just remember the session id.
58
58
//! ## Description
59
59
//!
60
60
//! The MuSig2 multisignature scheme lets you aggregate multiple public keys into a single public
61
- //! key that requires each corresponding secret key to authorize signatures under the aggregate key.
61
+ //! key that requires all of the corresponding secret keys to authorize a signature under the aggregate key.
62
62
//!
63
63
//! This implementation is protocol compatible with the implementation merged into
64
64
//! [secp256k1-zkp].
@@ -145,9 +145,13 @@ pub enum Party {
145
145
Remote ( XOnly ) ,
146
146
}
147
147
148
- /// A struct which represents a list of keys aggregated into a single key.
148
+ /// A list of keys aggregated into a single key.
149
149
///
150
- /// This can't be serialized but it's very efficient to re-create it from the initial list of keys.
150
+ /// Created using [`MuSig::new_keylist`].
151
+ ///
152
+ /// The `KeyList` can't be serialized but it's very efficient to re-create it from the initial list of keys.
153
+ ///
154
+ /// [`MuSig::new_keylist`]
151
155
#[ derive( Debug , Clone ) ]
152
156
pub struct KeyList {
153
157
/// The parties involved in the key aggregation.
@@ -203,7 +207,7 @@ impl KeyList {
203
207
///
204
208
/// Returns a new keylist with the same parties but a different aggregated public key. In the
205
209
/// unusual case that the tweak is exactly equal to the negation of the aggregated secret key
206
- /// this returns `None`.
210
+ /// it returns `None`.
207
211
pub fn tweak ( & self , tweak : Scalar < impl Secrecy , impl ZeroChoice > ) -> Option < Self > {
208
212
let mut tweak = s ! ( self . tweak + tweak) . mark :: < Public > ( ) ;
209
213
let ( agg_key, needs_negation) = g ! ( self . agg_key + tweak * G )
@@ -331,16 +335,42 @@ secp256kfun::impl_display_serialize! {
331
335
332
336
/// A pair of secret nonces along with the public portion.
333
337
///
334
- /// Depending on whether you are using deterministic nonce derivation or not
338
+ /// A nonce key pair can be created manually with [`from_secrets`] or with [`MuSig::gen_nonces`].
339
+ ///
340
+ /// [`from_secrets`]: Self::from_secrets
341
+ /// [`MuSig::gen_nonces`]: Self::gen_nonces
335
342
#[ derive( Debug , Clone , PartialEq ) ]
336
343
pub struct NonceKeyPair {
337
344
/// The public nonce
338
- pub public : Nonce ,
345
+ public : Nonce ,
339
346
/// The secret nonce
340
- pub secret : [ Scalar ; 2 ] ,
347
+ secret : [ Scalar ; 2 ] ,
341
348
}
342
349
343
350
impl NonceKeyPair {
351
+ /// Creates a keypair from two secret scalars.
352
+ ///
353
+ /// ## Security
354
+ ///
355
+ /// You must never use the same `NonceKeyPair` into two signing sessions.
356
+ ///
357
+ /// ## Example
358
+ /// ```
359
+ /// use schnorr_fun::{ fun::Scalar, musig::NonceKeyPair };
360
+ /// let nkp = NonceKeyPair::from_secrets([
361
+ /// Scalar::random(&mut rand::thread_rng()),
362
+ /// Scalar::random(&mut rand::thread_rng())
363
+ /// ]);
364
+ /// ```
365
+ pub fn from_secrets ( secret : [ Scalar ; 2 ] ) -> Self {
366
+ let [ ref r1, ref r2] = secret;
367
+ let R1 = g ! ( r1 * G ) . normalize ( ) ;
368
+ let R2 = g ! ( r2 * G ) . normalize ( ) ;
369
+ NonceKeyPair {
370
+ public : Nonce ( [ R1 , R2 ] ) ,
371
+ secret
372
+ }
373
+ }
344
374
/// Deserializes a nonce key pair from 64-bytes (two 32-byte serialized scalars).
345
375
pub fn from_bytes ( bytes : [ u8 ; 64 ] ) -> Option < Self > {
346
376
let r1 = Scalar :: from_slice ( & bytes[ ..32 ] ) ?. mark :: < NonZero > ( ) ?;
@@ -361,6 +391,16 @@ impl NonceKeyPair {
361
391
bytes[ 32 ..] . copy_from_slice ( self . secret [ 1 ] . to_bytes ( ) . as_ref ( ) ) ;
362
392
bytes
363
393
}
394
+
395
+ /// Get the secret portion of the nonce key pair (don't share this!)
396
+ pub fn secret ( & self ) -> & [ Scalar ; 2 ] {
397
+ & self . secret
398
+ }
399
+
400
+ /// Get the public portion of the nonce key pair (share this!)
401
+ pub fn public ( & self ) -> Nonce {
402
+ self . public
403
+ }
364
404
}
365
405
366
406
secp256kfun:: impl_fromstr_deserialize! {
@@ -497,7 +537,7 @@ impl<H: Digest<OutputSize = U32> + Clone, NG> MuSig<H, Schnorr<H, NG>> {
497
537
/// Start a signing session.
498
538
///
499
539
/// You must provide you local secret nonces (the public portion must be shared with the other signer(s)).
500
- /// If you are using deterministic signing it's possible to use [`start_sign_session_deterministic`] instead.
540
+ /// If you are using deterministic nonce generations it's possible to use [`start_sign_session_deterministic`] instead.
501
541
///
502
542
/// ## Return Value
503
543
///
@@ -538,9 +578,10 @@ impl<H: Digest<OutputSize = U32> + Clone, NG> MuSig<H, Schnorr<H, NG>> {
538
578
/// Start an encrypted signing session.
539
579
///
540
580
/// i.e. a session to produce an adaptor signature under `encryption_key`.
581
+ /// See [`adaptor`] for a more general description of adaptor signatures.
541
582
///
542
583
/// You must provide you local secret nonces (the public portion must be shared with the other
543
- /// signer(s)). If you are using deterministic signing it's possible to use
584
+ /// signer(s)). If you are using deterministic nonce generation it's possible to use
544
585
/// [`start_encrypted_sign_session_deterministic`] instead.
545
586
///
546
587
/// ## Return Value
@@ -555,6 +596,7 @@ impl<H: Digest<OutputSize = U32> + Clone, NG> MuSig<H, Schnorr<H, NG>> {
555
596
/// `keylist`.
556
597
///
557
598
/// [`start_encrypted_sign_session_deterministic`]: Self::start_sign_session_deterministic
599
+ /// [`adaptor`]: crate::adaptor
558
600
pub fn start_encrypted_sign_session (
559
601
& self ,
560
602
keylist : & KeyList ,
@@ -789,6 +831,12 @@ where
789
831
/// Same as [`start_sign_session`] but re-generate the local nonces deterministically from the
790
832
/// `sid` instead of passing them in.
791
833
///
834
+ /// ## Security
835
+ ///
836
+ /// Each call to this function must have a unique `sid`. Never call it twice with the same
837
+ /// `sid`, otherwise you risk revealing your secret key with the two signatures generated from
838
+ /// it if `message` or `remote_nonces` changes.
839
+ ///
792
840
/// [`start_sign_session`]: Self::start_sign_session
793
841
pub fn start_sign_session_deterministic (
794
842
& self ,
@@ -804,6 +852,12 @@ where
804
852
/// Same as [`start_encrypted_sign_session`] but re-generate the local nonces deterministically from the
805
853
/// `sid` instead of passing them in.
806
854
///
855
+ /// ## Security
856
+ ///
857
+ /// Each call to this function must have a unique `sid`. Never call it twice with the same
858
+ /// `sid`, otherwise you risk revealing your secret key with the two signatures generated from
859
+ /// it if `message` or `remote_nonces` changes.
860
+ ///
807
861
/// [`start_encrypted_sign_session`]: Self::start_encrypted_sign_session
808
862
pub fn start_encrypted_sign_session_deterministic (
809
863
& self ,
0 commit comments