9
9
10
10
#include "../../../include/secp256k1.h"
11
11
#include "../../../include/secp256k1_schnorr_adaptor.h"
12
+
12
13
#include "../../hash.h"
14
+ #include "../../scalar.h"
13
15
14
16
/* Initializes SHA256 with fixed midstate. This midstate was computed by applying
15
17
* SHA256 to SHA256("SchnorrAdaptor/nonce")||SHA256("SchnorrAdaptor/nonce"). */
@@ -145,11 +147,11 @@ static int secp256k1_schnorr_adaptor_presign_internal(const secp256k1_context *c
145
147
/* R = k*G */
146
148
secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & rj , & k );
147
149
secp256k1_ge_set_gej (& r , & rj );
150
+
148
151
/* We declassify the non-secret values R and T to allow using them
149
152
* as branch points. */
150
153
secp256k1_declassify (ctx , & r , sizeof (rp ));
151
154
secp256k1_declassify (ctx , & adaptor_ge , sizeof (rp ));
152
-
153
155
/* R' = R + T */
154
156
secp256k1_gej_add_ge_var (& rpj , & rj , & adaptor_ge , NULL );
155
157
secp256k1_ge_set_gej (& rp , & rpj );
@@ -158,11 +160,19 @@ static int secp256k1_schnorr_adaptor_presign_internal(const secp256k1_context *c
158
160
secp256k1_declassify (ctx , & rp , sizeof (rp ));
159
161
secp256k1_fe_normalize_var (& rp .y );
160
162
161
- /* s = k + e * d when R'.y is even
162
- * = -k + e * d when R'.y is odd
163
- * TODO: explain the negation here?
164
- * just say here we negate based on R' rather than R because R'
165
- * will be in the final bip340 signature */
163
+ /* Determine if the secret nonce should be negated.
164
+ *
165
+ * pre_sig65[0:33] contains the compressed 33-byte encoding of the public
166
+ * nonce R' = k*G + T, where k is the secret nonce and T is the adaptor point.
167
+ *
168
+ * Since a BIP340 signature requires an x-only public nonce, in the case where
169
+ * R' = k*G + T has odd Y-coordinate, the x-only public nonce corresponding to
170
+ * the signature is actually -k*G - T. Therefore, we negate k to ensure that the
171
+ * adapted pre-signature will result in a valid BIP340 signature, with an even R'.y
172
+ *
173
+ * pre_sig65[33:65] = k + e * d if R'.y is even
174
+ * = -k + e * d if R'.y is odd
175
+ */
166
176
if (secp256k1_fe_is_odd (& rp .y )) {
167
177
secp256k1_scalar_negate (& k , & k );
168
178
}
@@ -208,7 +218,7 @@ int secp256k1_schnorr_adaptor_extract(const secp256k1_context *ctx, secp256k1_pu
208
218
if (!secp256k1_eckey_pubkey_parse (& rp , & pre_sig65 [0 ], 33 )) {
209
219
return 0 ;
210
220
}
211
-
221
+ /* s := pre_sig65[33:65] */
212
222
secp256k1_scalar_set_b32 (& s , & pre_sig65 [33 ], & overflow );
213
223
if (overflow ) {
214
224
return 0 ;
@@ -230,10 +240,15 @@ int secp256k1_schnorr_adaptor_extract(const secp256k1_context *ctx, secp256k1_pu
230
240
return 0 ;
231
241
}
232
242
233
- /* T = R' - R when R'.y is even
234
- * = R' + R when R'.y is odd
235
- * TODO: explain the negation here?
236
- * just say here presign negates like this */
243
+ /* Determine if R needs to be negated
244
+ *
245
+ * `adaptor_presign` negates the secret nonce k when R’.y is odd, during
246
+ * the computation of the s value (i.e., presig[33:65]). Therefore, we need
247
+ * to negate R = k*G (if R'.y is odd) before subtracting it from R'.
248
+ *
249
+ * T = R' - R if R'.y is even
250
+ * = R' + R if R'.y is odd
251
+ */
237
252
if (secp256k1_fe_is_odd (& rp .y )) {
238
253
secp256k1_gej_neg (& rj , & rj );
239
254
}
@@ -247,13 +262,11 @@ int secp256k1_schnorr_adaptor_extract(const secp256k1_context *ctx, secp256k1_pu
247
262
return 1 ;
248
263
}
249
264
250
- /* TODO: musig2 consideres `t` (secadaptor) to be secret. And `s` (pre-signature) to be public*/
251
- /* should we consider bip340 signature to be secret? or public?
252
- * I think it should be secret since, bip340 sig is computed by adding a secret t to presig */
253
265
int secp256k1_schnorr_adaptor_adapt (const secp256k1_context * ctx , unsigned char * sig64 , const unsigned char * pre_sig65 , const unsigned char * sec_adaptor32 ) {
254
266
secp256k1_scalar s ;
255
267
secp256k1_scalar t ;
256
268
int overflow ;
269
+ int ret = 1 ;
257
270
258
271
VERIFY_CHECK (ctx != NULL );
259
272
ARG_CHECK (sig64 != NULL );
@@ -266,59 +279,69 @@ int secp256k1_schnorr_adaptor_adapt(const secp256k1_context *ctx, unsigned char
266
279
return 0 ;
267
280
}
268
281
secp256k1_scalar_set_b32 (& t , sec_adaptor32 , & overflow );
269
- if (overflow ) {
270
- return 0 ;
271
- }
272
-
273
- /* TODO: add a comment about why we negate here*/
282
+ ret &= !overflow ;
283
+
284
+ /* Determine if the secret adaptor should be negated.
285
+ *
286
+ * pre_sig65[0:33] contains the compressed 33-byte encoding of the public
287
+ * nonce R' = (k + t)*G, where r is the secret nonce generated by
288
+ * `adaptor_presign` and t is the secret adaptor.
289
+ *
290
+ * Since a BIP340 signature requires an x-only public nonce, in the case where
291
+ * (k + t)*G has odd Y-coordinate, the x-only public nonce corresponding to the
292
+ * signature is actually (-k - t)*G. Thus adapting a pre-signature requires
293
+ * negating t in this case.
294
+ *
295
+ * sig64[32:64] = s + t if R'.y is even
296
+ * = s - t if R'.y is odd
297
+ */
274
298
if (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
275
299
secp256k1_scalar_negate (& t , & t );
276
300
}
277
301
278
302
secp256k1_scalar_add (& s , & s , & t );
279
303
secp256k1_scalar_get_b32 (& sig64 [32 ], & s );
280
304
memmove (sig64 , & pre_sig65 [1 ], 32 );
281
- secp256k1_scalar_clear (& s );
282
- secp256k1_scalar_clear (& t ); /* remove this? */
283
- return 1 ;
305
+ secp256k1_scalar_clear (& t );
306
+ return ret ;
284
307
}
285
308
286
- int secp256k1_schnorr_adaptor_extract_sec (const secp256k1_context * ctx , unsigned char * secadaptor , const unsigned char * pre_sig65 , const unsigned char * sig64 ) {
287
- secp256k1_scalar s0 ;
288
- secp256k1_scalar s ;
309
+ int secp256k1_schnorr_adaptor_extract_sec (const secp256k1_context * ctx , unsigned char * sec_adaptor32 , const unsigned char * pre_sig65 , const unsigned char * sig64 ) {
289
310
secp256k1_scalar t ;
311
+ secp256k1_scalar s ;
290
312
int overflow ;
313
+ int ret = 1 ;
291
314
292
315
VERIFY_CHECK (ctx != NULL );
293
- ARG_CHECK (secadaptor != NULL );
316
+ ARG_CHECK (sec_adaptor32 != NULL );
294
317
ARG_CHECK (pre_sig65 != NULL );
295
318
ARG_CHECK (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_EVEN || pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD );
296
319
ARG_CHECK (sig64 != NULL );
297
320
298
- /* s0 */
299
- secp256k1_scalar_set_b32 (& s0 , & pre_sig65 [33 ], & overflow );
300
- if (overflow ) {
301
- return 0 ;
302
- }
321
+ secp256k1_scalar_set_b32 (& t , & sig64 [32 ], & overflow );
322
+ ret &= !overflow ;
303
323
304
- /* s */
305
- secp256k1_scalar_set_b32 (& s , & sig64 [32 ], & overflow );
324
+ secp256k1_scalar_set_b32 (& s , & pre_sig65 [33 ], & overflow );
306
325
if (overflow ) {
307
326
return 0 ;
308
327
}
309
328
310
- if (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_EVEN ) {
311
- secp256k1_scalar_negate (& s0 , & s0 );
312
- } else if (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
313
- secp256k1_scalar_negate (& s , & s );
329
+ secp256k1_scalar_negate (& s , & s );
330
+ secp256k1_scalar_add (& t , & t , & s );
331
+ /* `adaptor_adapt` negates the secret adaptor t when R’.y is odd, during
332
+ * the computation of the BIP340 signature. Therefore, we need negate
333
+ * (sig[32:64] - pre_sig65[33:65]) in this case.
334
+ *
335
+ * t = (sig[32:64] - pre_sig65[33:65]) if R'.y is even
336
+ * = -(sig[32:64] - pre_sig65[33:65]) if R'.y is odd
337
+ */
338
+ if (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
339
+ secp256k1_scalar_negate (& t , & t );
314
340
}
315
- secp256k1_scalar_add (& t , & s0 , & s );
316
341
317
- secp256k1_scalar_get_b32 (secadaptor , & t );
318
- secp256k1_scalar_clear (& s );
342
+ secp256k1_scalar_get_b32 (sec_adaptor32 , & t );
319
343
secp256k1_scalar_clear (& t );
320
-
321
- return 1 ;
344
+ return ret ;
322
345
}
323
346
324
347
#endif
0 commit comments