Tweak a private key (after negating it if needed).
(key, tweak)
| 417 | return (P[0].to_bytes(32, 'big'), not SECP256K1.has_even_y(P)) |
| 418 | |
| 419 | def tweak_add_privkey(key, tweak): |
| 420 | """Tweak a private key (after negating it if needed).""" |
| 421 | |
| 422 | assert len(key) == 32 |
| 423 | assert len(tweak) == 32 |
| 424 | |
| 425 | x = int.from_bytes(key, 'big') |
| 426 | if x == 0 or x >= SECP256K1_ORDER: |
| 427 | return None |
| 428 | if not SECP256K1.has_even_y(SECP256K1.mul([(SECP256K1_G, x)])): |
| 429 | x = SECP256K1_ORDER - x |
| 430 | t = int.from_bytes(tweak, 'big') |
| 431 | if t >= SECP256K1_ORDER: |
| 432 | return None |
| 433 | x = (x + t) % SECP256K1_ORDER |
| 434 | if x == 0: |
| 435 | return None |
| 436 | return x.to_bytes(32, 'big') |
| 437 | |
| 438 | def tweak_add_pubkey(key, tweak): |
| 439 | """Tweak a public key and return whether the result had to be negated.""" |
no test coverage detected