mirror of
https://github.com/LBRYFoundation/LBRY-Vault.git
synced 2025-08-23 17:47:31 +00:00
ecc: small clean-up
This commit is contained in:
parent
1669dd9782
commit
30bb7dd6f4
1 changed files with 13 additions and 12 deletions
|
@ -25,7 +25,7 @@
|
||||||
|
|
||||||
import base64
|
import base64
|
||||||
import hashlib
|
import hashlib
|
||||||
from typing import Union, Tuple
|
from typing import Union, Tuple, Optional
|
||||||
|
|
||||||
import ecdsa
|
import ecdsa
|
||||||
from ecdsa.ecdsa import curve_secp256k1, generator_secp256k1
|
from ecdsa.ecdsa import curve_secp256k1, generator_secp256k1
|
||||||
|
@ -83,12 +83,12 @@ def sig_string_from_r_and_s(r: int, s: int, order=CURVE_ORDER) -> bytes:
|
||||||
return ecdsa.util.sigencode_string_canonize(r, s, order)
|
return ecdsa.util.sigencode_string_canonize(r, s, order)
|
||||||
|
|
||||||
|
|
||||||
def point_to_ser(P, compressed=True) -> bytes:
|
def point_to_ser(point, compressed=True) -> Optional[bytes]:
|
||||||
if isinstance(P, tuple):
|
if isinstance(point, tuple):
|
||||||
assert len(P) == 2, 'unexpected point: %s' % P
|
assert len(point) == 2, f'unexpected point: {point}'
|
||||||
x, y = P
|
x, y = point
|
||||||
else:
|
else:
|
||||||
x, y = P.x(), P.y()
|
x, y = point.x(), point.y()
|
||||||
if x is None or y is None: # infinity
|
if x is None or y is None: # infinity
|
||||||
return None
|
return None
|
||||||
if compressed:
|
if compressed:
|
||||||
|
@ -96,7 +96,7 @@ def point_to_ser(P, compressed=True) -> bytes:
|
||||||
return bfh('04'+('%064x' % x)+('%064x' % y))
|
return bfh('04'+('%064x' % x)+('%064x' % y))
|
||||||
|
|
||||||
|
|
||||||
def get_y_coord_from_x(x: int, odd: bool=True) -> int:
|
def get_y_coord_from_x(x: int, *, odd: bool) -> int:
|
||||||
curve = curve_secp256k1
|
curve = curve_secp256k1
|
||||||
_p = curve.p()
|
_p = curve.p()
|
||||||
_a = curve.a()
|
_a = curve.a()
|
||||||
|
@ -117,7 +117,8 @@ def ser_to_point(ser: bytes) -> Tuple[int, int]:
|
||||||
if ser[0] == 0x04:
|
if ser[0] == 0x04:
|
||||||
return string_to_number(ser[1:33]), string_to_number(ser[33:])
|
return string_to_number(ser[1:33]), string_to_number(ser[33:])
|
||||||
x = string_to_number(ser[1:])
|
x = string_to_number(ser[1:])
|
||||||
return x, get_y_coord_from_x(x, ser[0] == 0x03)
|
odd = ser[0] == 0x03
|
||||||
|
return x, get_y_coord_from_x(x, odd=odd)
|
||||||
|
|
||||||
|
|
||||||
def _ser_to_python_ecdsa_point(ser: bytes) -> ecdsa.ellipticcurve.Point:
|
def _ser_to_python_ecdsa_point(ser: bytes) -> ecdsa.ellipticcurve.Point:
|
||||||
|
@ -182,7 +183,7 @@ class _PubkeyForPointAtInfinity:
|
||||||
|
|
||||||
class ECPubkey(object):
|
class ECPubkey(object):
|
||||||
|
|
||||||
def __init__(self, b: bytes):
|
def __init__(self, b: Optional[bytes]):
|
||||||
if b is not None:
|
if b is not None:
|
||||||
assert_bytes(b)
|
assert_bytes(b)
|
||||||
point = _ser_to_python_ecdsa_point(b)
|
point = _ser_to_python_ecdsa_point(b)
|
||||||
|
@ -434,8 +435,8 @@ class ECPrivkey(ECPubkey):
|
||||||
sig65, recid = bruteforce_recid(sig_string)
|
sig65, recid = bruteforce_recid(sig_string)
|
||||||
return sig65
|
return sig65
|
||||||
|
|
||||||
def decrypt_message(self, encrypted: Tuple[str, bytes], magic: bytes=b'BIE1') -> bytes:
|
def decrypt_message(self, encrypted: Union[str, bytes], magic: bytes=b'BIE1') -> bytes:
|
||||||
encrypted = base64.b64decode(encrypted)
|
encrypted = base64.b64decode(encrypted) # type: bytes
|
||||||
if len(encrypted) < 85:
|
if len(encrypted) < 85:
|
||||||
raise Exception('invalid ciphertext: length')
|
raise Exception('invalid ciphertext: length')
|
||||||
magic_found = encrypted[:4]
|
magic_found = encrypted[:4]
|
||||||
|
@ -446,7 +447,7 @@ class ECPrivkey(ECPubkey):
|
||||||
raise Exception('invalid ciphertext: invalid magic bytes')
|
raise Exception('invalid ciphertext: invalid magic bytes')
|
||||||
try:
|
try:
|
||||||
ecdsa_point = _ser_to_python_ecdsa_point(ephemeral_pubkey_bytes)
|
ecdsa_point = _ser_to_python_ecdsa_point(ephemeral_pubkey_bytes)
|
||||||
except AssertionError as e:
|
except InvalidECPointException as e:
|
||||||
raise Exception('invalid ciphertext: invalid ephemeral pubkey') from e
|
raise Exception('invalid ciphertext: invalid ephemeral pubkey') from e
|
||||||
if not ecdsa.ecdsa.point_is_valid(generator_secp256k1, ecdsa_point.x(), ecdsa_point.y()):
|
if not ecdsa.ecdsa.point_is_valid(generator_secp256k1, ecdsa_point.x(), ecdsa_point.y()):
|
||||||
raise Exception('invalid ciphertext: invalid ephemeral pubkey')
|
raise Exception('invalid ciphertext: invalid ephemeral pubkey')
|
||||||
|
|
Loading…
Add table
Reference in a new issue