Changeset View
Changeset View
Standalone View
Standalone View
test/functional/test_framework/key.py
#!/usr/bin/env python3 | #!/usr/bin/env python3 | ||||
# Copyright (c) 2019 Pieter Wuille | # Copyright (c) 2019 Pieter Wuille | ||||
# Copyright (c) 2019-2020 The Bitcoin developers | # Copyright (c) 2019-2020 The Bitcoin developers | ||||
"""Test-only secp256k1 elliptic curve implementation | """Test-only secp256k1 elliptic curve implementation | ||||
WARNING: This code is slow, uses bad randomness, does not properly protect | WARNING: This code is slow, uses bad randomness, does not properly protect | ||||
keys, and is trivially vulnerable to side channel attacks. Do not use for | keys, and is trivially vulnerable to side channel attacks. Do not use for | ||||
anything but tests. | anything but tests. | ||||
""" | """ | ||||
import hashlib | import hashlib | ||||
import random | import random | ||||
from .address import byte_to_base58 | |||||
def modinv(a, n): | def modinv(a, n): | ||||
"""Compute the modular inverse of a modulo n | """Compute the modular inverse of a modulo n | ||||
See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers | See https://en.wikipedia.org/wiki/Extended_Euclidean_algorithm#Modular_integers | ||||
""" | """ | ||||
t1, t2 = 0, 1 | t1, t2 = 0, 1 | ||||
r1, r2 = n, a | r1, r2 = n, a | ||||
▲ Show 20 Lines • Show All 393 Lines • ▼ Show 20 Lines | def sign_schnorr(self, msg32): | ||||
pubkey.get_bytes() + | pubkey.get_bytes() + | ||||
msg32).digest(), | msg32).digest(), | ||||
'big') | 'big') | ||||
s = (k + e * int.from_bytes(self.get_bytes(), 'big')) % SECP256K1_ORDER | s = (k + e * int.from_bytes(self.get_bytes(), 'big')) % SECP256K1_ORDER | ||||
sig = Rx + s.to_bytes(32, 'big') | sig = Rx + s.to_bytes(32, 'big') | ||||
assert pubkey.verify_schnorr(sig, msg32) | assert pubkey.verify_schnorr(sig, msg32) | ||||
return sig | return sig | ||||
def bytes_to_wif(b, compressed=True): | |||||
if compressed: | |||||
b += b'\x01' | |||||
return byte_to_base58(b, 239) | |||||
def generate_wif_key(): | |||||
# Makes a WIF privkey for imports | |||||
k = ECKey() | |||||
k.generate() | |||||
return bytes_to_wif(k.get_bytes(), k.is_compressed) |