67
67
//!
68
68
//! [the excellent paper]: https://eprint.iacr.org/2020/1261.pdf
69
69
//! [secp256k1-zkp]: https://github.com/ElementsProject/secp256k1-zkp/pull/131
70
+ pub use crate :: binonce:: { Nonce , NonceKeyPair } ;
70
71
use crate :: { adaptor:: EncryptedSignature , KeyPair , Message , Schnorr , Signature , Vec } ;
71
72
use secp256kfun:: {
72
73
derive_nonce,
@@ -300,123 +301,6 @@ impl<H: Digest<OutputSize = U32> + Clone, S> MuSig<H, S> {
300
301
}
301
302
}
302
303
303
- /// A nonce (pair of points) that each party must share with the others in the first stage of signing.
304
- #[ derive( Clone , Copy , PartialEq , Debug ) ]
305
- pub struct Nonce ( pub [ Point ; 2 ] ) ;
306
-
307
- impl Nonce {
308
- /// Reads the pair of nonces from 66 bytes (two 33-byte serialized points).
309
- pub fn from_bytes ( bytes : [ u8 ; 66 ] ) -> Option < Self > {
310
- let R1 = Point :: from_slice ( & bytes[ ..33 ] ) ?;
311
- let R2 = Point :: from_slice ( & bytes[ 33 ..] ) ?;
312
- Some ( Nonce ( [ R1 , R2 ] ) )
313
- }
314
-
315
- /// Serializes a public nonce as as 66 bytes (two 33-byte serialized points).
316
- pub fn to_bytes ( & self ) -> [ u8 ; 66 ] {
317
- let mut bytes = [ 0u8 ; 66 ] ;
318
- bytes[ ..33 ] . copy_from_slice ( self . 0 [ 0 ] . to_bytes ( ) . as_ref ( ) ) ;
319
- bytes[ 33 ..] . copy_from_slice ( self . 0 [ 1 ] . to_bytes ( ) . as_ref ( ) ) ;
320
- bytes
321
- }
322
- }
323
-
324
- secp256kfun:: impl_fromstr_deserialize! {
325
- name => "MuSig2 public nonce pair" ,
326
- fn from_bytes( bytes: [ u8 ; 66 ] ) -> Option <Nonce > {
327
- Nonce :: from_bytes( bytes)
328
- }
329
- }
330
-
331
- secp256kfun:: impl_display_serialize! {
332
- fn to_bytes( nonce: & Nonce ) -> [ u8 ; 66 ] {
333
- nonce. to_bytes( )
334
- }
335
- }
336
-
337
- /// A pair of secret nonces along with the public portion.
338
- ///
339
- /// A nonce key pair can be created manually with [`from_secrets`] or with [`MuSig::gen_nonces`].
340
- ///
341
- /// [`from_secrets`]: Self::from_secrets
342
- /// [`MuSig::gen_nonces`]: MuSig::gen_nonces
343
- #[ derive( Debug , Clone , PartialEq ) ]
344
- pub struct NonceKeyPair {
345
- /// The public nonce
346
- public : Nonce ,
347
- /// The secret nonce
348
- secret : [ Scalar ; 2 ] ,
349
- }
350
-
351
- impl NonceKeyPair {
352
- /// Creates a keypair from two secret scalars.
353
- ///
354
- /// ## Security
355
- ///
356
- /// You must never use the same `NonceKeyPair` into two signing sessions.
357
- ///
358
- /// ## Example
359
- /// ```
360
- /// use schnorr_fun::{fun::Scalar, musig::NonceKeyPair};
361
- /// let nkp = NonceKeyPair::from_secrets([
362
- /// Scalar::random(&mut rand::thread_rng()),
363
- /// Scalar::random(&mut rand::thread_rng()),
364
- /// ]);
365
- /// ```
366
- pub fn from_secrets ( secret : [ Scalar ; 2 ] ) -> Self {
367
- let [ ref r1, ref r2] = secret;
368
- let R1 = g ! ( r1 * G ) . normalize ( ) ;
369
- let R2 = g ! ( r2 * G ) . normalize ( ) ;
370
- NonceKeyPair {
371
- public : Nonce ( [ R1 , R2 ] ) ,
372
- secret,
373
- }
374
- }
375
- /// Deserializes a nonce key pair from 64-bytes (two 32-byte serialized scalars).
376
- pub fn from_bytes ( bytes : [ u8 ; 64 ] ) -> Option < Self > {
377
- let r1 = Scalar :: from_slice ( & bytes[ ..32 ] ) ?. mark :: < NonZero > ( ) ?;
378
- let r2 = Scalar :: from_slice ( & bytes[ 32 ..] ) ?. mark :: < NonZero > ( ) ?;
379
- let R1 = g ! ( r1 * G ) . normalize ( ) ;
380
- let R2 = g ! ( r2 * G ) . normalize ( ) ;
381
- let pub_nonce = Nonce ( [ R1 , R2 ] ) ;
382
- Some ( NonceKeyPair {
383
- public : pub_nonce,
384
- secret : [ r1, r2] ,
385
- } )
386
- }
387
-
388
- /// Serializes a nonce key pair to 64-bytes (two 32-bytes serialized scalars).
389
- pub fn to_bytes ( & self ) -> [ u8 ; 64 ] {
390
- let mut bytes = [ 0u8 ; 64 ] ;
391
- bytes[ ..32 ] . copy_from_slice ( self . secret [ 0 ] . to_bytes ( ) . as_ref ( ) ) ;
392
- bytes[ 32 ..] . copy_from_slice ( self . secret [ 1 ] . to_bytes ( ) . as_ref ( ) ) ;
393
- bytes
394
- }
395
-
396
- /// Get the secret portion of the nonce key pair (don't share this!)
397
- pub fn secret ( & self ) -> & [ Scalar ; 2 ] {
398
- & self . secret
399
- }
400
-
401
- /// Get the public portion of the nonce key pair (share this!)
402
- pub fn public ( & self ) -> Nonce {
403
- self . public
404
- }
405
- }
406
-
407
- secp256kfun:: impl_fromstr_deserialize! {
408
- name => "MuSig secret nonce pair" ,
409
- fn from_bytes( bytes: [ u8 ; 64 ] ) -> Option <NonceKeyPair > {
410
- NonceKeyPair :: from_bytes( bytes)
411
- }
412
- }
413
-
414
- secp256kfun:: impl_display_serialize! {
415
- fn to_bytes( nkp: & NonceKeyPair ) -> [ u8 ; 64 ] {
416
- nkp. to_bytes( )
417
- }
418
- }
419
-
420
304
impl < H : Digest < OutputSize = U32 > + Clone , NG : NonceGen > MuSig < H , Schnorr < H , NG > > {
421
305
/// Generate nonces for your local keys in keylist.
422
306
///
@@ -441,6 +325,7 @@ impl<H: Digest<OutputSize = U32> + Clone, NG: NonceGen> MuSig<H, Schnorr<H, NG>>
441
325
/// [`Deterministic`]: secp256kfun::nonce::Deterministic
442
326
/// [`start_sign_session`]: Self::start_sign_session
443
327
/// [`start_sign_session_deterministic`]: Self::start_sign_session_deterministic
328
+ /// [`NonceKeyPair`]: schnorr_fun::binonce::NonceKeyPair
444
329
pub fn gen_nonces ( & self , keylist : & KeyList , sid : & [ u8 ] ) -> Vec < NonceKeyPair > {
445
330
keylist
446
331
. parties
0 commit comments