mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 09:37:31 +00:00
ecc: abstract away some usage of python-ecdsa: bytes<->int conversions
This commit is contained in:
parent
004acb906d
commit
2cf2135528
7 changed files with 22 additions and 22 deletions
|
@ -65,7 +65,7 @@ def _CKD_priv(parent_privkey: bytes, parent_chaincode: bytes,
|
|||
child_privkey = (I_left + ecc.string_to_number(parent_privkey)) % ecc.CURVE_ORDER
|
||||
if I_left >= ecc.CURVE_ORDER or child_privkey == 0:
|
||||
raise ecc.InvalidECPointException()
|
||||
child_privkey = ecc.number_to_string(child_privkey, ecc.CURVE_ORDER)
|
||||
child_privkey = int.to_bytes(child_privkey, length=32, byteorder='big', signed=False)
|
||||
child_chaincode = I[32:]
|
||||
return child_privkey, child_chaincode
|
||||
|
||||
|
|
|
@ -101,8 +101,8 @@ def python_validate_rrsig(rrset, rrsig, keys, origin=None, now=None):
|
|||
keyptr = keyptr[2:]
|
||||
rsa_e = keyptr[0:bytes]
|
||||
rsa_n = keyptr[bytes:]
|
||||
n = ecdsa.util.string_to_number(rsa_n)
|
||||
e = ecdsa.util.string_to_number(rsa_e)
|
||||
n = int.from_bytes(rsa_n, byteorder='big', signed=False)
|
||||
e = int.from_bytes(rsa_e, byteorder='big', signed=False)
|
||||
pubkey = rsakey.RSAKey(n, e)
|
||||
sig = rrsig.signature
|
||||
|
||||
|
@ -117,15 +117,15 @@ def python_validate_rrsig(rrset, rrsig, keys, origin=None, now=None):
|
|||
# shouldn't happen
|
||||
raise ValidationFailure('unknown ECDSA curve')
|
||||
keyptr = candidate_key.key
|
||||
x = ecdsa.util.string_to_number(keyptr[0:key_len])
|
||||
y = ecdsa.util.string_to_number(keyptr[key_len:key_len * 2])
|
||||
x = int.from_bytes(keyptr[0:key_len], byteorder='big', signed=False)
|
||||
y = int.from_bytes(keyptr[key_len:key_len * 2], byteorder='big', signed=False)
|
||||
assert ecdsa.ecdsa.point_is_valid(curve.generator, x, y)
|
||||
point = ecdsa.ellipticcurve.Point(curve.curve, x, y, curve.order)
|
||||
verifying_key = ecdsa.keys.VerifyingKey.from_public_point(point, curve)
|
||||
r = rrsig.signature[:key_len]
|
||||
s = rrsig.signature[key_len:]
|
||||
sig = ecdsa.ecdsa.Signature(ecdsa.util.string_to_number(r),
|
||||
ecdsa.util.string_to_number(s))
|
||||
sig = ecdsa.ecdsa.Signature(int.from_bytes(r, byteorder='big', signed=False),
|
||||
int.from_bytes(s, byteorder='big', signed=False))
|
||||
|
||||
else:
|
||||
raise ValidationFailure('unknown algorithm %u' % rrsig.algorithm)
|
||||
|
@ -156,7 +156,7 @@ def python_validate_rrsig(rrset, rrsig, keys, origin=None, now=None):
|
|||
return
|
||||
|
||||
elif _is_ecdsa(rrsig.algorithm):
|
||||
diglong = ecdsa.util.string_to_number(digest)
|
||||
diglong = int.from_bytes(digest, byteorder='big', signed=False)
|
||||
if verifying_key.pubkey.verifies(diglong, sig):
|
||||
return
|
||||
|
||||
|
|
|
@ -33,7 +33,6 @@ import ecdsa
|
|||
from ecdsa.ecdsa import curve_secp256k1, generator_secp256k1
|
||||
from ecdsa.curves import SECP256k1
|
||||
from ecdsa.ellipticcurve import Point
|
||||
from ecdsa.util import string_to_number, number_to_string
|
||||
|
||||
from .util import bfh, bh2u, assert_bytes, to_bytes, InvalidPassword, profiler, randrange
|
||||
from .crypto import (sha256d, aes_encrypt_with_iv, aes_decrypt_with_iv, hmac_oneshot)
|
||||
|
@ -57,6 +56,10 @@ def point_at_infinity():
|
|||
return ECPubkey(None)
|
||||
|
||||
|
||||
def string_to_number(b: bytes) -> int:
|
||||
return int.from_bytes(b, byteorder='big', signed=False)
|
||||
|
||||
|
||||
def sig_string_from_der_sig(der_sig: bytes, order=CURVE_ORDER) -> bytes:
|
||||
r, s = ecdsa.util.sigdecode_der(der_sig, order)
|
||||
return ecdsa.util.sigencode_string(r, s, order)
|
||||
|
@ -392,7 +395,7 @@ class ECPrivkey(ECPubkey):
|
|||
|
||||
@classmethod
|
||||
def from_secret_scalar(cls, secret_scalar: int):
|
||||
secret_bytes = number_to_string(secret_scalar, CURVE_ORDER)
|
||||
secret_bytes = int.to_bytes(secret_scalar, length=32, byteorder='big', signed=False)
|
||||
return ECPrivkey(secret_bytes)
|
||||
|
||||
@classmethod
|
||||
|
@ -408,7 +411,7 @@ class ECPrivkey(ECPubkey):
|
|||
scalar = string_to_number(privkey_bytes) % CURVE_ORDER
|
||||
if scalar == 0:
|
||||
raise Exception('invalid EC private key scalar: zero')
|
||||
privkey_32bytes = number_to_string(scalar, CURVE_ORDER)
|
||||
privkey_32bytes = int.to_bytes(scalar, length=32, byteorder='big', signed=False)
|
||||
return privkey_32bytes
|
||||
|
||||
def __repr__(self):
|
||||
|
@ -417,11 +420,11 @@ class ECPrivkey(ECPubkey):
|
|||
@classmethod
|
||||
def generate_random_key(cls):
|
||||
randint = randrange(CURVE_ORDER)
|
||||
ephemeral_exponent = number_to_string(randint, CURVE_ORDER)
|
||||
ephemeral_exponent = int.to_bytes(randint, length=32, byteorder='big', signed=False)
|
||||
return ECPrivkey(ephemeral_exponent)
|
||||
|
||||
def get_secret_bytes(self) -> bytes:
|
||||
return number_to_string(self.secret_scalar, CURVE_ORDER)
|
||||
return int.to_bytes(self.secret_scalar, length=32, byteorder='big', signed=False)
|
||||
|
||||
def sign(self, data: bytes, sigencode=None, sigdecode=None) -> bytes:
|
||||
if sigencode is None:
|
||||
|
|
|
@ -36,7 +36,7 @@ from .bitcoin import deserialize_privkey, serialize_privkey
|
|||
from .bip32 import (convert_bip32_path_to_list_of_uint32, BIP32_PRIME,
|
||||
is_xpub, is_xprv, BIP32Node, normalize_bip32_derivation,
|
||||
convert_bip32_intpath_to_strpath)
|
||||
from .ecc import string_to_number, number_to_string
|
||||
from .ecc import string_to_number
|
||||
from .crypto import (pw_decode, pw_encode, sha256, sha256d, PW_HASH_VERSION_LATEST,
|
||||
SUPPORTED_PW_HASH_VERSIONS, UnsupportedPasswordHashVersion, hash_160)
|
||||
from .util import (InvalidPassword, WalletFileException,
|
||||
|
@ -626,7 +626,7 @@ class Old_KeyStore(MasterPublicKeyMixin, Deterministic_KeyStore):
|
|||
|
||||
def _get_private_key_from_stretched_exponent(self, for_change, n, secexp):
|
||||
secexp = (secexp + self.get_sequence(self.mpk, for_change, n)) % ecc.CURVE_ORDER
|
||||
pk = number_to_string(secexp, ecc.CURVE_ORDER)
|
||||
pk = int.to_bytes(secexp, length=32, byteorder='big', signed=False)
|
||||
return pk
|
||||
|
||||
def get_private_key(self, sequence: Sequence[int], password):
|
||||
|
|
|
@ -275,7 +275,7 @@ def derive_blinded_privkey(basepoint_secret: bytes, per_commitment_secret: bytes
|
|||
k1 = ecc.string_to_number(basepoint_secret) * ecc.string_to_number(sha256(basepoint + per_commitment_point))
|
||||
k2 = ecc.string_to_number(per_commitment_secret) * ecc.string_to_number(sha256(per_commitment_point + basepoint))
|
||||
sum = (k1 + k2) % ecc.CURVE_ORDER
|
||||
return ecc.number_to_string(sum, CURVE_ORDER)
|
||||
return int.to_bytes(sum, length=32, byteorder='big', signed=False)
|
||||
|
||||
|
||||
def make_htlc_tx_output(amount_msat, local_feerate, revocationpubkey, local_delayedpubkey, success, to_self_delay):
|
||||
|
|
|
@ -15,7 +15,6 @@ from electrum.bip32 import (BIP32Node, convert_bip32_intpath_to_strpath,
|
|||
normalize_bip32_derivation, is_all_public_derivation)
|
||||
from electrum.crypto import sha256d, SUPPORTED_PW_HASH_VERSIONS
|
||||
from electrum import ecc, crypto, constants
|
||||
from electrum.ecc import number_to_string, string_to_number
|
||||
from electrum.util import bfh, bh2u, InvalidPassword, randrange
|
||||
from electrum.storage import WalletStorage
|
||||
from electrum.keystore import xtype_from_derivation
|
||||
|
@ -111,7 +110,7 @@ class Test_bitcoin(ElectrumTestCase):
|
|||
addr_c = public_key_to_p2pkh(pubkey_c)
|
||||
|
||||
#print "Private key ", '%064x'%pvk
|
||||
eck = ecc.ECPrivkey(number_to_string(pvk,_r))
|
||||
eck = ecc.ECPrivkey.from_secret_scalar(pvk)
|
||||
|
||||
#print "Compressed public key ", pubkey_c.encode('hex')
|
||||
enc = ecc.ECPubkey(pubkey_c).encrypt_message(message)
|
||||
|
|
|
@ -27,8 +27,6 @@ import hashlib
|
|||
import time
|
||||
from datetime import datetime
|
||||
|
||||
import ecdsa
|
||||
|
||||
from . import util
|
||||
from .util import profiler, bh2u
|
||||
from .logging import get_logger
|
||||
|
@ -250,8 +248,8 @@ class X509(object):
|
|||
exponent = spk.next_node(modulus)
|
||||
rsa_n = spk.get_value_of_type(modulus, 'INTEGER')
|
||||
rsa_e = spk.get_value_of_type(exponent, 'INTEGER')
|
||||
self.modulus = ecdsa.util.string_to_number(rsa_n)
|
||||
self.exponent = ecdsa.util.string_to_number(rsa_e)
|
||||
self.modulus = int.from_bytes(rsa_n, byteorder='big', signed=False)
|
||||
self.exponent = int.from_bytes(rsa_e, byteorder='big', signed=False)
|
||||
else:
|
||||
subject_public_key = der.next_node(public_key_algo)
|
||||
spk = der.get_value_of_type(subject_public_key, 'BIT STRING')
|
||||
|
|
Loading…
Add table
Reference in a new issue