@@ -99,4 +99,272 @@ static int nonce_function_schnorr_adaptor(unsigned char *nonce32, const unsigned
99
99
100
100
const secp256k1_nonce_function_hardened_schnorr_adaptor secp256k1_nonce_function_schnorr_adaptor = nonce_function_schnorr_adaptor ;
101
101
102
+ static int secp256k1_schnorr_adaptor_presign_internal (const secp256k1_context * ctx , unsigned char * pre_sig65 , const unsigned char * msg32 , const secp256k1_keypair * keypair , const secp256k1_pubkey * adaptor , secp256k1_nonce_function_hardened_schnorr_adaptor noncefp , void * ndata ) {
103
+ secp256k1_scalar sk ;
104
+ secp256k1_scalar e ;
105
+ secp256k1_scalar k ;
106
+ secp256k1_gej rj , rpj ;
107
+ secp256k1_ge r , rp ;
108
+ secp256k1_ge pk ;
109
+ secp256k1_ge adaptor_ge ;
110
+ unsigned char nonce32 [32 ] = { 0 };
111
+ unsigned char pk_buf [32 ];
112
+ unsigned char seckey [32 ];
113
+ unsigned char adaptor_buff [33 ];
114
+ size_t cmprssd_len = 33 ; /* for serializing `adaptor_ge` and `pre_sig65` */
115
+ int serialize_ret = 0 ;
116
+ int ret = 1 ;
117
+
118
+ VERIFY_CHECK (ctx != NULL );
119
+ ARG_CHECK (secp256k1_ecmult_gen_context_is_built (& ctx -> ecmult_gen_ctx ));
120
+ ARG_CHECK (pre_sig65 != NULL );
121
+ ARG_CHECK (msg32 != NULL );
122
+ ARG_CHECK (keypair != NULL );
123
+ ARG_CHECK (adaptor != NULL );
124
+
125
+ if (noncefp == NULL ) {
126
+ noncefp = secp256k1_nonce_function_schnorr_adaptor ;
127
+ }
128
+
129
+ /* T := adaptor_ge */
130
+ if (!secp256k1_pubkey_load (ctx , & adaptor_ge , adaptor )){
131
+ return 0 ;
132
+ }
133
+
134
+ ret &= secp256k1_keypair_load (ctx , & sk , & pk , keypair );
135
+ /* Because we are signing for a x-only pubkey, the secret key is negated
136
+ * before signing if the point corresponding to the secret key does not
137
+ * have an even Y. */
138
+ if (secp256k1_fe_is_odd (& pk .y )) {
139
+ secp256k1_scalar_negate (& sk , & sk );
140
+ }
141
+
142
+ /* Generate the nonce k */
143
+ secp256k1_scalar_get_b32 (seckey , & sk );
144
+ secp256k1_fe_get_b32 (pk_buf , & pk .x );
145
+ serialize_ret = secp256k1_eckey_pubkey_serialize (& adaptor_ge , adaptor_buff , & cmprssd_len , 1 );
146
+ VERIFY_CHECK (serialize_ret );
147
+ ret &= !!noncefp (nonce32 , msg32 , seckey , adaptor_buff , pk_buf , schnorr_adaptor_algo , sizeof (schnorr_adaptor_algo ), ndata );
148
+ secp256k1_scalar_set_b32 (& k , nonce32 , NULL );
149
+ ret &= !secp256k1_scalar_is_zero (& k );
150
+ secp256k1_scalar_cmov (& k , & secp256k1_scalar_one , !ret );
151
+
152
+ /* R = k*G */
153
+ secp256k1_ecmult_gen (& ctx -> ecmult_gen_ctx , & rj , & k );
154
+ secp256k1_ge_set_gej (& r , & rj );
155
+
156
+ /* We declassify the non-secret values R and T to allow using them
157
+ * as branch points. */
158
+ secp256k1_declassify (ctx , & rj , sizeof (rj ));
159
+ secp256k1_declassify (ctx , & adaptor_ge , sizeof (adaptor_ge ));
160
+ /* R' = R + T */
161
+ secp256k1_gej_add_ge_var (& rpj , & rj , & adaptor_ge , NULL );
162
+ secp256k1_ge_set_gej (& rp , & rpj );
163
+
164
+ /* We declassify R' (non-secret value) to branch on it */
165
+ secp256k1_declassify (ctx , & rp , sizeof (rp ));
166
+ secp256k1_fe_normalize_var (& rp .y );
167
+
168
+ /* Determine if the secret nonce should be negated.
169
+ *
170
+ * pre_sig65[0:33] contains the compressed 33-byte encoding of the public
171
+ * nonce R' = k*G + T, where k is the secret nonce and T is the adaptor point.
172
+ *
173
+ * Since a BIP340 signature requires an x-only public nonce, in the case where
174
+ * R' = k*G + T has odd Y-coordinate, the x-only public nonce corresponding to
175
+ * the signature is actually -k*G - T. Therefore, we negate k to ensure that the
176
+ * adapted pre-signature will result in a valid BIP340 signature, with an even R'.y
177
+ *
178
+ * pre_sig65[33:65] = k + e * d if R'.y is even
179
+ * = -k + e * d if R'.y is odd
180
+ */
181
+ if (secp256k1_fe_is_odd (& rp .y )) {
182
+ secp256k1_scalar_negate (& k , & k );
183
+ }
184
+ serialize_ret = secp256k1_eckey_pubkey_serialize (& rp , pre_sig65 , & cmprssd_len , 1 );
185
+ /* R' is not the point at infinity with overwhelming probability */
186
+ VERIFY_CHECK (serialize_ret );
187
+ (void ) serialize_ret ;
188
+
189
+ secp256k1_schnorrsig_challenge (& e , & pre_sig65 [1 ], msg32 , 32 , pk_buf );
190
+ secp256k1_scalar_mul (& e , & e , & sk );
191
+ secp256k1_scalar_add (& e , & e , & k );
192
+ secp256k1_scalar_get_b32 (& pre_sig65 [33 ], & e );
193
+
194
+ secp256k1_memczero (pre_sig65 , 65 , !ret );
195
+ secp256k1_scalar_clear (& k );
196
+ secp256k1_scalar_clear (& sk );
197
+ memset (seckey , 0 , sizeof (seckey ));
198
+
199
+ return ret ;
200
+ }
201
+
202
+ int secp256k1_schnorr_adaptor_presign (const secp256k1_context * ctx , unsigned char * pre_sig65 , const unsigned char * msg32 , const secp256k1_keypair * keypair , const secp256k1_pubkey * adaptor , const unsigned char * aux_rand32 ) {
203
+ /* We cast away const from the passed aux_rand32 argument since we know the default nonce function does not modify it. */
204
+ return secp256k1_schnorr_adaptor_presign_internal (ctx , pre_sig65 , msg32 , keypair , adaptor , secp256k1_nonce_function_schnorr_adaptor , (unsigned char * )aux_rand32 );
205
+ }
206
+
207
+
208
+ int secp256k1_schnorr_adaptor_extract (const secp256k1_context * ctx , secp256k1_pubkey * adaptor , const unsigned char * pre_sig65 , const unsigned char * msg32 , const secp256k1_xonly_pubkey * pubkey ) {
209
+ secp256k1_scalar s ;
210
+ secp256k1_scalar e ;
211
+ secp256k1_ge pk ;
212
+ secp256k1_gej pkj ;
213
+ secp256k1_ge adaptor_ge ;
214
+ secp256k1_gej adaptor_gej ;
215
+ secp256k1_gej rj ;
216
+ secp256k1_ge rp ;
217
+ unsigned char buf [32 ];
218
+ int overflow ;
219
+
220
+ VERIFY_CHECK (ctx != NULL );
221
+ ARG_CHECK (adaptor != NULL );
222
+ ARG_CHECK (pre_sig65 != NULL );
223
+ ARG_CHECK (msg32 != NULL );
224
+ ARG_CHECK (pubkey != NULL );
225
+
226
+ /* R' := pre_sig65[0:33] */
227
+ if (!secp256k1_eckey_pubkey_parse (& rp , & pre_sig65 [0 ], 33 )) {
228
+ return 0 ;
229
+ }
230
+ /* s := pre_sig65[33:65] */
231
+ secp256k1_scalar_set_b32 (& s , & pre_sig65 [33 ], & overflow );
232
+ if (overflow ) {
233
+ return 0 ;
234
+ }
235
+
236
+ if (!secp256k1_xonly_pubkey_load (ctx , & pk , pubkey )) {
237
+ return 0 ;
238
+ }
239
+
240
+ /* Compute e */
241
+ secp256k1_fe_get_b32 (buf , & pk .x );
242
+ secp256k1_schnorrsig_challenge (& e , & pre_sig65 [1 ], msg32 , 32 , buf );
243
+
244
+ /* Compute R = s*G + (-e)*P */
245
+ secp256k1_scalar_negate (& e , & e );
246
+ secp256k1_gej_set_ge (& pkj , & pk );
247
+ secp256k1_ecmult (& rj , & pkj , & e , & s );
248
+ if (secp256k1_gej_is_infinity (& rj )) {
249
+ return 0 ;
250
+ }
251
+
252
+ /* Determine if R needs to be negated
253
+ *
254
+ * `adaptor_presign` negates the secret nonce k when R’.y is odd, during
255
+ * the computation of the s value (i.e., presig[33:65]). Therefore, we need
256
+ * to negate R = k*G (if R'.y is odd) before subtracting it from R' = R + T.
257
+ *
258
+ * T = R' - R if R'.y is even
259
+ * = R' + R if R'.y is odd
260
+ */
261
+ secp256k1_fe_normalize_var (& rp .y );
262
+ if (!secp256k1_fe_is_odd (& rp .y )) {
263
+ secp256k1_gej_neg (& rj , & rj );
264
+ }
265
+ secp256k1_gej_add_ge_var (& adaptor_gej , & rj , & rp , NULL );
266
+ secp256k1_ge_set_gej (& adaptor_ge , & adaptor_gej );
267
+ if (secp256k1_ge_is_infinity (& adaptor_ge )) {
268
+ return 0 ;
269
+ }
270
+ secp256k1_pubkey_save (adaptor , & adaptor_ge );
271
+
272
+ return 1 ;
273
+ }
274
+
275
+ int secp256k1_schnorr_adaptor_adapt (const secp256k1_context * ctx , unsigned char * sig64 , const unsigned char * pre_sig65 , const unsigned char * sec_adaptor32 ) {
276
+ secp256k1_scalar s ;
277
+ secp256k1_scalar t ;
278
+ int overflow ;
279
+ int ret = 1 ;
280
+
281
+ VERIFY_CHECK (ctx != NULL );
282
+ ARG_CHECK (sig64 != NULL );
283
+ ARG_CHECK (pre_sig65 != NULL );
284
+ ARG_CHECK (sec_adaptor32 != NULL );
285
+
286
+ if (pre_sig65 [0 ] != SECP256K1_TAG_PUBKEY_EVEN && pre_sig65 [0 ] != SECP256K1_TAG_PUBKEY_ODD ) {
287
+ return 0 ;
288
+ }
289
+ secp256k1_scalar_set_b32 (& s , & pre_sig65 [33 ], & overflow );
290
+ if (overflow ) {
291
+ return 0 ;
292
+ }
293
+ secp256k1_scalar_set_b32 (& t , sec_adaptor32 , & overflow );
294
+ ret &= !overflow ;
295
+
296
+ /* Determine if the secret adaptor should be negated.
297
+ *
298
+ * pre_sig65[0:33] contains the compressed 33-byte encoding of the public
299
+ * nonce R' = (k + t)*G, where r is the secret nonce generated by
300
+ * `adaptor_presign` and t is the secret adaptor.
301
+ *
302
+ * Since a BIP340 signature requires an x-only public nonce, in the case where
303
+ * (k + t)*G has odd Y-coordinate, the x-only public nonce corresponding to the
304
+ * signature is actually (-k - t)*G. Thus adapting a pre-signature requires
305
+ * negating t in this case.
306
+ *
307
+ * sig64[32:64] = s + t if R'.y is even
308
+ * = s - t if R'.y is odd
309
+ */
310
+ if (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
311
+ secp256k1_scalar_negate (& t , & t );
312
+ }
313
+ secp256k1_scalar_add (& s , & s , & t );
314
+ secp256k1_scalar_get_b32 (& sig64 [32 ], & s );
315
+ memmove (sig64 , & pre_sig65 [1 ], 32 );
316
+
317
+ secp256k1_memczero (sig64 , 64 , !ret );
318
+ secp256k1_scalar_clear (& t );
319
+ return ret ;
320
+ }
321
+
322
+ int secp256k1_schnorr_adaptor_extract_sec (const secp256k1_context * ctx , unsigned char * sec_adaptor32 , const unsigned char * pre_sig65 , const unsigned char * sig64 ) {
323
+ secp256k1_scalar t ;
324
+ secp256k1_scalar s ;
325
+ int overflow ;
326
+ int ret = 1 ;
327
+
328
+ VERIFY_CHECK (ctx != NULL );
329
+ ARG_CHECK (sec_adaptor32 != NULL );
330
+ ARG_CHECK (pre_sig65 != NULL );
331
+ ARG_CHECK (sig64 != NULL );
332
+
333
+ if (pre_sig65 [0 ] != SECP256K1_TAG_PUBKEY_EVEN && pre_sig65 [0 ] != SECP256K1_TAG_PUBKEY_ODD ) {
334
+ return 0 ;
335
+ }
336
+ secp256k1_scalar_set_b32 (& s , & pre_sig65 [33 ], & overflow );
337
+ if (overflow ) {
338
+ return 0 ;
339
+ }
340
+ secp256k1_scalar_set_b32 (& t , & sig64 [32 ], & overflow );
341
+ ret &= !overflow ;
342
+
343
+ /*TODO: should we parse presig[0:33] & sig[0:32], to make sure the presig &
344
+ * has valid public nonce point?
345
+ *
346
+ * But we don't care about their validity here right? Then why do we ARG_CHECK
347
+ * presig[0] parity byte?
348
+ *
349
+ * Here, the inputs are invalid but the output is valid :/ */
350
+
351
+ secp256k1_scalar_negate (& s , & s );
352
+ secp256k1_scalar_add (& t , & t , & s );
353
+ /* `adaptor_adapt` negates the secret adaptor t when R’.y is odd, during
354
+ * the computation of the BIP340 signature. Therefore, we need negate
355
+ * (sig[32:64] - pre_sig65[33:65]) in this case.
356
+ *
357
+ * t = (sig[32:64] - pre_sig65[33:65]) if R'.y is even
358
+ * = -(sig[32:64] - pre_sig65[33:65]) if R'.y is odd
359
+ */
360
+ if (pre_sig65 [0 ] == SECP256K1_TAG_PUBKEY_ODD ) {
361
+ secp256k1_scalar_negate (& t , & t );
362
+ }
363
+ secp256k1_scalar_get_b32 (sec_adaptor32 , & t );
364
+
365
+ secp256k1_memczero (sec_adaptor32 , 32 , !ret );
366
+ secp256k1_scalar_clear (& t );
367
+ return ret ;
368
+ }
369
+
102
370
#endif
0 commit comments