4
4
//! Your public nonces are derived from scalars which must be kept secret.
5
5
//! Derived binonces should be unique and and must not be reused for signing under any circumstances
6
6
//! as this can leak your secret key.
7
- use secp256kfun:: { g, marker:: * , Point , Scalar , G } ;
7
+ use crate :: Message ;
8
+ use secp256kfun:: { derive_nonce, g, marker:: * , nonce:: NonceGen , Point , Scalar , G } ;
8
9
9
10
/// A nonce (pair of points) that each party must share with the others in the first stage of signing.
11
+ ///
12
+ /// The type argument determines whether the nonces can be `Zero` or not. The [musig
13
+ /// spec](https://github.com/jonasnick/bips/pull/21) specifies that the aggregate nonce is allowed
14
+ /// to be zero to avoid having to abort the protocol in this case.
10
15
#[ derive( Clone , Copy , PartialEq , Debug ) ]
11
- pub struct Nonce ( pub [ Point ; 2 ] ) ;
16
+ pub struct Nonce < Z = NonZero > ( pub [ Point < Normal , Public , Z > ; 2 ] ) ;
17
+
18
+ impl Nonce < Zero > {
19
+ /// Reads the pair of nonces from 66 bytes (two 33-byte serialized points).
20
+ ///
21
+ /// If either pair of 33 bytes is `[0u8;32]` that point is interpreted as `Zero`.
22
+ pub fn from_bytes ( bytes : [ u8 ; 66 ] ) -> Option < Self > {
23
+ fn deser ( bytes : & [ u8 ] ) -> Option < Point < Normal , Public , Zero > > {
24
+ Point :: from_slice ( & bytes)
25
+ . map ( |p| p. mark :: < Zero > ( ) )
26
+ . or_else ( || {
27
+ if bytes == [ 0u8 ; 33 ] . as_slice ( ) {
28
+ Some ( Point :: zero ( ) )
29
+ } else {
30
+ None
31
+ }
32
+ } )
33
+ }
34
+
35
+ let R1 = deser ( & bytes[ 33 ..] ) ?;
36
+ let R2 = deser ( & bytes[ ..33 ] ) ?;
37
+
38
+ Some ( Nonce ( [ R1 , R2 ] ) )
39
+ }
12
40
13
- impl Nonce {
41
+ /// Serializes a public nonce as as 66 bytes (two 33-byte serialized points).
42
+ ///
43
+ /// If either point is `Zero` it will be serialized as `[0u8;32]`.
44
+ pub fn to_bytes ( self ) -> [ u8 ; 66 ] {
45
+ let mut bytes = [ 0u8 ; 66 ] ;
46
+ bytes[ ..33 ] . copy_from_slice (
47
+ & self . 0 [ 0 ]
48
+ . mark :: < NonZero > ( )
49
+ . map ( |p| p. to_bytes ( ) )
50
+ . unwrap_or ( [ 0u8 ; 33 ] ) ,
51
+ ) ;
52
+ bytes[ 33 ..] . copy_from_slice (
53
+ & self . 0 [ 1 ]
54
+ . mark :: < NonZero > ( )
55
+ . map ( |p| p. to_bytes ( ) )
56
+ . unwrap_or ( [ 0u8 ; 33 ] ) ,
57
+ ) ;
58
+ bytes
59
+ }
60
+ }
61
+
62
+ impl Nonce < NonZero > {
14
63
/// Reads the pair of nonces from 66 bytes (two 33-byte serialized points).
15
64
pub fn from_bytes ( bytes : [ u8 ; 66 ] ) -> Option < Self > {
16
65
let R1 = Point :: from_slice ( & bytes[ ..33 ] ) ?;
@@ -25,7 +74,9 @@ impl Nonce {
25
74
bytes[ 33 ..] . copy_from_slice ( self . 0 [ 1 ] . to_bytes ( ) . as_ref ( ) ) ;
26
75
bytes
27
76
}
77
+ }
28
78
79
+ impl < Z > Nonce < Z > {
29
80
/// Negate the two nonces
30
81
pub fn conditional_negate ( & mut self , needs_negation : bool ) {
31
82
self . 0 [ 0 ] = self . 0 [ 0 ] . conditional_negate ( needs_negation) ;
@@ -35,13 +86,26 @@ impl Nonce {
35
86
36
87
secp256kfun:: impl_fromstr_deserialize! {
37
88
name => "public nonce pair" ,
38
- fn from_bytes( bytes: [ u8 ; 66 ] ) -> Option <Nonce > {
39
- Nonce :: from_bytes( bytes)
89
+ fn from_bytes( bytes: [ u8 ; 66 ] ) -> Option <Nonce <Zero >> {
90
+ Nonce :: <Zero >:: from_bytes( bytes)
91
+ }
92
+ }
93
+
94
+ secp256kfun:: impl_display_serialize! {
95
+ fn to_bytes( nonce: & Nonce <Zero >) -> [ u8 ; 66 ] {
96
+ nonce. to_bytes( )
97
+ }
98
+ }
99
+
100
+ secp256kfun:: impl_fromstr_deserialize! {
101
+ name => "public nonce pair" ,
102
+ fn from_bytes( bytes: [ u8 ; 66 ] ) -> Option <Nonce <NonZero >> {
103
+ Nonce :: <NonZero >:: from_bytes( bytes)
40
104
}
41
105
}
42
106
43
107
secp256kfun:: impl_display_serialize! {
44
- fn to_bytes( nonce: & Nonce ) -> [ u8 ; 66 ] {
108
+ fn to_bytes( nonce: & Nonce < NonZero > ) -> [ u8 ; 66 ] {
45
109
nonce. to_bytes( )
46
110
}
47
111
}
@@ -54,7 +118,7 @@ secp256kfun::impl_display_serialize! {
54
118
#[ derive( Debug , Clone , PartialEq ) ]
55
119
pub struct NonceKeyPair {
56
120
/// The public nonce
57
- pub public : Nonce ,
121
+ pub public : Nonce < NonZero > ,
58
122
/// The secret nonce
59
123
pub secret : [ Scalar ; 2 ] ,
60
124
}
@@ -97,9 +161,57 @@ impl NonceKeyPair {
97
161
}
98
162
99
163
/// Get the public portion of the nonce key pair (share this!)
100
- pub fn public ( & self ) -> Nonce {
164
+ pub fn public ( & self ) -> Nonce < NonZero > {
101
165
self . public
102
166
}
167
+
168
+ /// Generate a new `NonceKeyPair` from application data.
169
+ ///
170
+ /// Each nonce generated must only be passed to [`MuSig::sign`] once.
171
+ ///
172
+ /// You must always pass in a:
173
+ ///
174
+ /// - `nonce_gen`: [`NonceGen`] containing the underlying algorithm for generating the nonce
175
+ /// - `secret`: The secret scalar whose secrecy depends on the uniquness of the nonces generated.
176
+ /// - `session_id`: Some application defined identifier for the signing session that the resulting nonce will be used in.
177
+ ///
178
+ /// How important the `session_id` is depends on whether you add a `message` and whether you are using randomness in your `nonce_gen`.
179
+ /// If you are using a deterministic `nonce_gen` it is crucial that this is set to a unique value for each signing session.
180
+ /// If your application doesn't naturally provide you with a unique value store a counter.
181
+ ///
182
+ /// Optionally you may pass in `public_key` and `message` which should be passed in when available.
183
+ ///
184
+ /// [`MuSig::sign`]: crate::musig::MuSig::sign
185
+ pub fn generate (
186
+ nonce_gen : & impl NonceGen ,
187
+ secret : & Scalar ,
188
+ session_id : & [ u8 ] ,
189
+ public_key : Option < Point < impl Normalized > > ,
190
+ message : Option < Message < ' _ > > ,
191
+ ) -> Self {
192
+ let message = message. unwrap_or ( Message :: raw ( b"" ) ) ;
193
+ let msg_len = ( message. len ( ) as u64 ) . to_be_bytes ( ) ;
194
+ let sid_len = ( session_id. len ( ) as u64 ) . to_be_bytes ( ) ;
195
+ let pk_bytes = public_key. map ( |p| p. to_bytes ( ) ) . unwrap_or ( [ 0u8 ; 33 ] ) ;
196
+ let r1 = derive_nonce ! (
197
+ nonce_gen => nonce_gen,
198
+ secret => secret,
199
+ public => [ b"r1" , pk_bytes, msg_len, message, sid_len, session_id]
200
+ ) ;
201
+ let r2 = derive_nonce ! (
202
+ nonce_gen => nonce_gen,
203
+ secret => secret,
204
+ public => [ b"r2" , pk_bytes, msg_len, message, sid_len, session_id]
205
+ ) ;
206
+
207
+ let R1 = g ! ( r1 * G ) . normalize ( ) ;
208
+ let R2 = g ! ( r2 * G ) . normalize ( ) ;
209
+
210
+ NonceKeyPair {
211
+ public : Nonce ( [ R1 , R2 ] ) ,
212
+ secret : [ r1, r2] ,
213
+ }
214
+ }
103
215
}
104
216
105
217
secp256kfun:: impl_fromstr_deserialize! {
0 commit comments