@@ -17,7 +17,7 @@ pub struct Nonce<Z = NonZero>(pub [Point<Normal, Public, Z>; 2]);
17
17
impl < Z : ZeroChoice > Nonce < Z > {
18
18
/// Reads the pair of nonces from 66 bytes (two 33-byte serialized points).
19
19
///
20
- /// If either pair of 33 bytes is `[0u8;32 ]` that point is interpreted as `Zero`.
20
+ /// If either pair of 33 bytes is `[0u8;33 ]` that point is interpreted as `Zero`.
21
21
pub fn from_bytes ( bytes : [ u8 ; 66 ] ) -> Option < Self > {
22
22
let R1 = Point :: from_slice ( & bytes[ ..33 ] ) ?;
23
23
let R2 = Point :: from_slice ( & bytes[ 33 ..] ) ?;
@@ -35,7 +35,7 @@ impl<Z: ZeroChoice> Nonce<Z> {
35
35
36
36
/// Serializes a public nonce as as 66 bytes (two 33-byte serialized points).
37
37
///
38
- /// If either point is `Zero` it will be serialized as `[0u8;32 ]`.
38
+ /// If either point is `Zero` it will be serialized as `[0u8;33 ]`.
39
39
pub fn to_bytes ( & self ) -> [ u8 ; 66 ] {
40
40
let mut bytes = [ 0u8 ; 66 ] ;
41
41
bytes[ ..33 ] . copy_from_slice ( self . 0 [ 0 ] . to_bytes ( ) . as_ref ( ) ) ;
@@ -72,7 +72,7 @@ impl Nonce<Zero> {
72
72
}
73
73
74
74
secp256kfun:: impl_fromstr_deserialize! {
75
- name => "public nonce pair " ,
75
+ name => "public binonce " ,
76
76
fn from_bytes<Z : ZeroChoice >( bytes: [ u8 ; 66 ] ) -> Option <Nonce <Z >> {
77
77
Nonce :: from_bytes( bytes)
78
78
}
@@ -86,51 +86,64 @@ secp256kfun::impl_display_serialize! {
86
86
87
87
/// A pair of secret nonces along with the public portion.
88
88
///
89
- /// A nonce key pair can be created manually with [`from_secrets `]
89
+ /// A nonce key pair can be created manually with [`from_secret `]
90
90
///
91
- /// [`from_secrets `]: Self::from_secrets
91
+ /// [`from_secret `]: Self::from_secret
92
92
#[ derive( Debug , Clone , PartialEq ) ]
93
93
pub struct NonceKeyPair {
94
94
/// The public nonce
95
95
pub public : Nonce < NonZero > ,
96
96
/// The secret nonce
97
- pub secret : [ Scalar ; 2 ] ,
97
+ pub secret : SecretNonce ,
98
98
}
99
99
100
- impl NonceKeyPair {
101
- /// Load nonces from two secret scalars
102
- pub fn from_secrets ( secret : [ Scalar ; 2 ] ) -> Self {
103
- let [ ref r1, ref r2] = secret;
104
- let R1 = g ! ( r1 * G ) . normalize ( ) ;
105
- let R2 = g ! ( r2 * G ) . normalize ( ) ;
106
- NonceKeyPair {
107
- public : Nonce ( [ R1 , R2 ] ) ,
108
- secret,
109
- }
110
- }
111
- /// Deserializes a nonce key pair from 64-bytes (two 32-byte serialized scalars).
100
+ /// A pair of secret nonces.
101
+ ///
102
+ /// ⚠ An attacker getting this allows them to extract your secret share from a signature share.
103
+ #[ derive( Debug , Clone , PartialEq ) ]
104
+ pub struct SecretNonce ( pub [ Scalar ; 2 ] ) ;
105
+
106
+ impl SecretNonce {
107
+ /// Deserializes a secret binonce from 64-bytes (two 32-byte serialized scalars).
112
108
pub fn from_bytes ( bytes : [ u8 ; 64 ] ) -> Option < Self > {
113
109
let r1 = Scalar :: from_slice ( & bytes[ ..32 ] ) ?. non_zero ( ) ?;
114
110
let r2 = Scalar :: from_slice ( & bytes[ 32 ..] ) ?. non_zero ( ) ?;
115
- let R1 = g ! ( r1 * G ) . normalize ( ) ;
116
- let R2 = g ! ( r2 * G ) . normalize ( ) ;
117
- let pub_nonce = Nonce ( [ R1 , R2 ] ) ;
118
- Some ( NonceKeyPair {
119
- public : pub_nonce,
120
- secret : [ r1, r2] ,
121
- } )
111
+ Some ( Self ( [ r1, r2] ) )
122
112
}
123
113
124
- /// Serializes a nonce key pair to 64-bytes (two 32-bytes serialized scalars).
114
+ /// Serializes a secret binonce to 64-bytes (two 32-bytes serialized scalars).
125
115
pub fn to_bytes ( & self ) -> [ u8 ; 64 ] {
126
116
let mut bytes = [ 0u8 ; 64 ] ;
127
- bytes[ ..32 ] . copy_from_slice ( self . secret [ 0 ] . to_bytes ( ) . as_ref ( ) ) ;
128
- bytes[ 32 ..] . copy_from_slice ( self . secret [ 1 ] . to_bytes ( ) . as_ref ( ) ) ;
117
+ bytes[ ..32 ] . copy_from_slice ( self . 0 [ 0 ] . to_bytes ( ) . as_ref ( ) ) ;
118
+ bytes[ 32 ..] . copy_from_slice ( self . 0 [ 1 ] . to_bytes ( ) . as_ref ( ) ) ;
129
119
bytes
130
120
}
131
121
122
+ /// Generate a nonce secret binonce from an rng
123
+ pub fn random ( rng : & mut impl RngCore ) -> Self {
124
+ Self ( [ Scalar :: random ( rng) , Scalar :: random ( rng) ] )
125
+ }
126
+
127
+ /// Convert a secret nonce into a key pair by computing the public nonce
128
+ pub fn into_keypair ( self ) -> NonceKeyPair {
129
+ NonceKeyPair :: from_secret ( self )
130
+ }
131
+ }
132
+
133
+ impl NonceKeyPair {
134
+ /// Load nonces from two secret scalars
135
+ pub fn from_secret ( secret : SecretNonce ) -> Self {
136
+ let [ ref r1, ref r2] = secret. 0 ;
137
+ let R1 = g ! ( r1 * G ) . normalize ( ) ;
138
+ let R2 = g ! ( r2 * G ) . normalize ( ) ;
139
+ NonceKeyPair {
140
+ public : Nonce ( [ R1 , R2 ] ) ,
141
+ secret,
142
+ }
143
+ }
144
+
132
145
/// Get the secret portion of the nonce key pair (don't share this!)
133
- pub fn secret ( & self ) -> & [ Scalar ; 2 ] {
146
+ pub fn secret ( & self ) -> & SecretNonce {
134
147
& self . secret
135
148
}
136
149
@@ -139,21 +152,46 @@ impl NonceKeyPair {
139
152
self . public
140
153
}
141
154
142
- /// Generate a nonce keypair from an rng
155
+ /// Generate a random secret nonce and conver to a keypair
143
156
pub fn random ( rng : & mut impl RngCore ) -> Self {
144
- Self :: from_secrets ( [ Scalar :: random ( rng) , Scalar :: random ( rng) ] )
157
+ Self :: from_secret ( SecretNonce :: random ( rng) )
158
+ }
159
+ }
160
+
161
+ impl AsRef < SecretNonce > for NonceKeyPair {
162
+ fn as_ref ( & self ) -> & SecretNonce {
163
+ self . secret ( )
164
+ }
165
+ }
166
+
167
+ impl AsRef < SecretNonce > for SecretNonce {
168
+ fn as_ref ( & self ) -> & SecretNonce {
169
+ self
170
+ }
171
+ }
172
+
173
+ secp256kfun:: impl_fromstr_deserialize! {
174
+ name => "secret binonce" ,
175
+ fn from_bytes( bytes: [ u8 ; 64 ] ) -> Option <SecretNonce > {
176
+ SecretNonce :: from_bytes( bytes)
177
+ }
178
+ }
179
+
180
+ secp256kfun:: impl_display_serialize! {
181
+ fn to_bytes( value: & SecretNonce ) -> [ u8 ; 64 ] {
182
+ value. to_bytes( )
145
183
}
146
184
}
147
185
148
186
secp256kfun:: impl_fromstr_deserialize! {
149
- name => "secret nonce pair " ,
187
+ name => "secret binonce " ,
150
188
fn from_bytes( bytes: [ u8 ; 64 ] ) -> Option <NonceKeyPair > {
151
- NonceKeyPair :: from_bytes( bytes)
189
+ Some ( NonceKeyPair :: from_secret ( SecretNonce :: from_bytes( bytes) ? ) )
152
190
}
153
191
}
154
192
155
193
secp256kfun:: impl_display_serialize! {
156
- fn to_bytes( nkp : & NonceKeyPair ) -> [ u8 ; 64 ] {
157
- nkp . to_bytes( )
194
+ fn to_bytes( value : & NonceKeyPair ) -> [ u8 ; 64 ] {
195
+ value . secret . to_bytes( )
158
196
}
159
197
}
0 commit comments