@@ -228,6 +228,17 @@ pub fn tweaked_public_key(
228
228
ProjectivePoint :: GENERATOR * tweak ( & pk, merkle_root) + pk
229
229
}
230
230
231
+ /// Creates a real BIP341 tweaked public key by assuming an even y-coordinate.
232
+ pub fn real_tweaked_pubkey (
233
+ public_key : & <<Secp256K1Sha256 as Ciphersuite >:: Group as Group >:: Element ,
234
+ merkle_root : & [ u8 ] ,
235
+ ) -> <<Secp256K1Sha256 as Ciphersuite >:: Group as Group >:: Element {
236
+ let tweaked_pubkey = tweaked_public_key ( public_key, merkle_root) ;
237
+ AffinePoint :: decompact ( & tweaked_pubkey. to_affine ( ) . x ( ) )
238
+ . unwrap ( )
239
+ . into ( )
240
+ }
241
+
231
242
/// Create a BIP341 compliant tweaked secret key
232
243
pub fn tweaked_secret_key (
233
244
secret : <<<Secp256K1Sha256 as Ciphersuite >:: Group as Group >:: Field as Field >:: Scalar ,
@@ -325,7 +336,13 @@ impl Ciphersuite for Secp256K1Sha256 {
325
336
verifying_key : & Element < S > ,
326
337
) -> <<Self :: Group as Group >:: Field as Field >:: Scalar {
327
338
let t = tweak ( & verifying_key, & [ ] ) ;
328
- z + t * challenge. clone ( ) . to_scalar ( )
339
+ let tc = t * challenge. clone ( ) . to_scalar ( ) ;
340
+ let tweaked_pubkey = tweaked_public_key ( & verifying_key, & [ ] ) ;
341
+ if tweaked_pubkey. to_affine ( ) . y_is_odd ( ) . into ( ) {
342
+ z - tc
343
+ } else {
344
+ z + tc
345
+ }
329
346
}
330
347
331
348
/// compute tweaked signature_share
@@ -343,7 +360,13 @@ impl Ciphersuite for Secp256K1Sha256 {
343
360
}
344
361
345
362
let mut kp = key_package. clone ( ) ;
346
- if key_package. verifying_key ( ) . y_is_odd ( ) {
363
+ let public_key = key_package. verifying_key ( ) ;
364
+ let pubkey_is_odd = public_key. y_is_odd ( ) ;
365
+ let tweaked_pubkey_is_odd = tweaked_public_key ( public_key. element ( ) , & [ ] )
366
+ . to_affine ( )
367
+ . y_is_odd ( )
368
+ . into ( ) ;
369
+ if pubkey_is_odd != tweaked_pubkey_is_odd {
347
370
kp. negate_signing_share ( ) ;
348
371
}
349
372
@@ -354,7 +377,7 @@ impl Ciphersuite for Secp256K1Sha256 {
354
377
fn tweaked_public_key (
355
378
public_key : & <Self :: Group as Group >:: Element ,
356
379
) -> <Self :: Group as Group >:: Element {
357
- tweaked_public_key ( public_key, & [ ] )
380
+ real_tweaked_pubkey ( public_key, & [ ] )
358
381
}
359
382
360
383
/// calculate tweaked R
0 commit comments