Browse Source

Add verify_compact()

export COINCURVE_IGNORE_SYSTEM_LIB=1
anonswap
tecnovert 4 years ago
parent
commit
45ce99b6ab
No known key found for this signature in database GPG Key ID: 6C1A887B4701EAE3
  1. 11
      coincurve/ecdsa.py
  2. 12
      coincurve/keys.py
  3. 8
      setup.py

11
coincurve/ecdsa.py

@ -25,6 +25,17 @@ def der_to_cdata(der, context=GLOBAL_CONTEXT):
return cdata
def parse_compact(data, context=GLOBAL_CONTEXT):
cdata = ffi.new('secp256k1_ecdsa_signature *')
assert(len(data) == 64)
parsed = lib.secp256k1_ecdsa_signature_parse_compact(context.ctx, cdata, data)
if not parsed:
raise ValueError('The DER-encoded signature could not be parsed.')
return cdata
def recover(message, recover_sig, hasher=sha256, context=GLOBAL_CONTEXT):
msg_hash = hasher(message) if hasher is not None else message
if len(msg_hash) != 32:

12
coincurve/keys.py

@ -1,7 +1,7 @@
from asn1crypto.keys import ECDomainParameters, ECPointBitString, ECPrivateKey, PrivateKeyAlgorithm, PrivateKeyInfo
from coincurve.context import GLOBAL_CONTEXT
from coincurve.ecdsa import cdata_to_der, der_to_cdata, deserialize_recoverable, recover, serialize_recoverable
from coincurve.ecdsa import parse_compact, cdata_to_der, der_to_cdata, deserialize_recoverable, recover, serialize_recoverable
from coincurve.flags import EC_COMPRESSED, EC_UNCOMPRESSED
from coincurve.utils import (
bytes_to_hex,
@ -249,6 +249,16 @@ class PublicKey:
# A performance hack to avoid global bool() lookup.
return not not verified
def verify_compact(self, signature, message, hasher=sha256):
msg_hash = hasher(message) if hasher is not None else message
if len(msg_hash) != 32:
raise ValueError('Message hash must be 32 bytes long.')
verified = lib.secp256k1_ecdsa_verify(self.context.ctx, parse_compact(signature), msg_hash, self.public_key)
# A performance hack to avoid global bool() lookup.
return not not verified
def add(self, scalar, update=False):
scalar = pad_scalar(scalar)

8
setup.py

@ -139,9 +139,9 @@ class build_clib(_build_clib):
return build_flags('libsecp256k1', 'l', os.path.abspath(self.build_temp))
def run(self):
#if has_system_lib():
# log.info('Using system library')
# return
if has_system_lib():
log.info('Using system library')
return
build_temp = os.path.abspath(self.build_temp)
@ -270,7 +270,7 @@ else:
setup(
name='coincurve',
version='13.0.0',
version='13.0.1',
description='Cross-platform Python CFFI bindings for libsecp256k1',
long_description=open('README.rst', 'r').read(),

Loading…
Cancel
Save