Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

Cross-platform Python CFFI bindings for libsecp256k1

Project description

Travis CI Codecov PyPI - Status PyPI - Version PyPI - Downloads License: MIT/Apache-2.0 Code style: black

This library provides well-tested Python CFFI bindings for libsecp256k1, the heavily optimized C library used by Bitcoin Core for operations on elliptic curve secp256k1.

Table of Contents

Features

  • Fastest available implementation (more than 10x faster than OpenSSL)
  • Clean, easy to use API
  • Frequent updates from libsecp256k1 master
  • Linux, macOS, and Windows all have binary packages for both 64 and 32-bit architectures
  • Linux & macOS use GMP for faster computation
  • Deterministic signatures via RFC 6979
  • Non-malleable signatures (lower-S form) by default
  • Secure, non-malleable ECDH implementation
  • Implements a fix for https://bugs.python.org/issue28150 to support Python 3.6+ on macOS

Installation

Coincurve is distributed on PyPI and is available on Linux/macOS and Windows and supports Python 2.7/3.5+ and PyPy3.5-v5.8.1+.

$ pip install coincurve

If you are on a system that doesn’t have a precompiled binary wheel (e.g. FreeBSD) then pip will fetch source to build yourself. You must have the necessary packages.

On Debian/Ubuntu the necessary system packages are:

  • build-essential
  • automake
  • pkg-config
  • libtool
  • libffi-dev
  • python3-dev (or python-dev for Python 2)
  • libgmp-dev (optional)

On macOS the necessary Homebrew packages are:

  • automake
  • pkg-config
  • libtool
  • libffi
  • gmp (optional)

API

Coincurve provides a simple API.

coincurve.verify_signature

verify_signature(signature, message, public_key, hasher=sha256, context=GLOBAL_CONTEXT)

Verifies some message was signed by the owner of a public key.

  • Parameters:

    • signature (bytes) - The signature to verify.
    • message (bytes) - The message that was supposedly signed.
    • public_key (bytes) - A public key in compressed or uncompressed form.
    • hasher - The hash function to use, can be None. hasher(message) must return 32 bytes.
    • context (coincurve.Context)
  • Returns: bool

coincurve.PrivateKey

All instances have a public_key of type coincurve.PublicKey

PrivateKey(secret=None, context=GLOBAL_CONTEXT)

  • Parameters:

    • secret (bytes) - The secret to use.
    • context (coincurve.Context)

Methods:

classmethod from_hex(hexed, context=GLOBAL_CONTEXT)

classmethod from_int(num, context=GLOBAL_CONTEXT)

classmethod from_pem(pem, context=GLOBAL_CONTEXT)

classmethod from_der(der, context=GLOBAL_CONTEXT)

sign(message, hasher=sha256, custom_nonce=None)

  • Parameters:

    • message (bytes) - The message to sign.
    • hasher - The hash function to use, can be None. hasher(message) must return 32 bytes.
    • custom_nonce - A tuple of arity 2 in the form of (nonce_fn, nonce_data). Refer to: secp256k1.h
  • Returns: bytes. 68 <= len(signature) <= 71

sign_recoverable(message, hasher=sha256)

  • Parameters:

    • message (bytes) - The message to sign.
    • hasher - The hash function to use, can be None. hasher(message) must return 32 bytes.
  • Returns: bytes

ecdh(public_key)

Computes a Diffie-Hellman secret in constant time. Note: This prevents malleability by returning sha256(compressed_public_key) instead of the x coordinate directly. See https://github.com/ofek/coincurve/issues/9.

  • Parameters:

    • public_key (bytes) - Another party’s public key in compressed or uncompressed form.
  • Returns: bytes

add(scalar, update=False)

  • Parameters:

    • scalar (bytes) - The scalar to add.
    • update (bool) - If True, will update and return self.
  • Returns: coincurve.PrivateKey

multiply(scalar, update=False)

  • Parameters:

    • scalar (bytes) - The scalar to multiply.
    • update (bool) - If True, will update and return self.
  • Returns: coincurve.PrivateKey

to_hex()

to_int()

to_pem()

to_der()

coincurve.PublicKey

PublicKey(data, context=GLOBAL_CONTEXT)

  • Parameters:

    • data (bytes) - The public key in compressed or uncompressed form.
    • context (coincurve.Context)

Methods:

classmethod from_secret(secret, context=GLOBAL_CONTEXT)

classmethod from_valid_secret(secret, context=GLOBAL_CONTEXT)

classmethod from_point(x, y, context=GLOBAL_CONTEXT)

classmethod from_signature_and_message(serialized_sig, message, hasher=sha256, context=GLOBAL_CONTEXT)

classmethod combine_keys(public_keys, context=GLOBAL_CONTEXT)

  • Parameters:

    • public_keys (list) - A list of coincurve.PublicKey to add.
    • context (coincurve.Context)
  • Returns: coincurve.PublicKey

format(compressed=True)

  • Parameters:

    • compressed (bool)
  • Returns: The public key serialized to bytes.

point()

  • Returns: (x, y)

verify(signature, message, hasher=sha256)

Verifies some message was signed by the owner of this public key.

  • Parameters:

    • signature (bytes) - The signature to verify.
    • message (bytes) - The message that was supposedly signed.
    • hasher - The hash function to use, can be None. hasher(message) must return 32 bytes.
  • Returns: bool

add(scalar, update=False)

  • Parameters:

    • scalar (bytes) - The scalar to add.
    • update (bool) - If True, will update and return self.
  • Returns: coincurve.PublicKey

multiply(scalar, update=False)

  • Parameters:

    • scalar (bytes) - The scalar to multiply.
    • update (bool) - If True, will update and return self.
  • Returns: coincurve.PublicKey

combine(public_keys, update=False)

  • Parameters:

    • public_keys (list) - A list of coincurve.PublicKey to add.
    • update (bool) - If True, will update and return self.
  • Returns: coincurve.PublicKey

License

Coincurve is distributed under the terms of both

at your option.

Credits

  • Contributors of libsecp256k1.
  • Contributors of secp256k1-py. While Coincurve is nearly a complete rewrite, much of the build system provided by ulope remains.

History

Important changes are emphasized.

13.0.0

  • New: Binary wheels for Python 3.8!
  • Support building on OpenBSD
  • Improve handling of PEM private key deserialization
  • Improve ECDH documentation
  • Improvements from libsecp256k1 master

12.0.0

  • New: Binary wheels on Linux for PyPy3.6 v7.1.1-beta!
  • New: Binary wheels on macOS for Python 3.8.0-alpha.3!
  • New: Binary wheels on Linux are now also built with the new manylinux2010 spec for 64-bit platforms!
  • Improvements from libsecp256k1 master

11.0.0

  • Fix some linking scenarios by placing bundled libsecp256k1 dir first in path
  • Allow override of system libsecp256k1 with environment variable
  • Add benchmarks
  • Use Codecov to track coverage
  • Use black for code formatting

10.0.0

  • Support tox for testing
  • Compatibility with latest libsecp256k1 ECDH API
  • Make libgmp optional when building from source

9.0.0

  • Fixed wheels for macOS
  • Breaking: Drop support for 32-bit macOS

View all history

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for coincurve, version 13.0.0
Filename, size File type Python version Upload date Hashes
Filename, size coincurve-13.0.0-cp27-cp27m-macosx_10_9_x86_64.whl (374.5 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp27-cp27m-manylinux1_i686.whl (535.3 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp27-cp27m-manylinux1_x86_64.whl (530.9 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp27-cp27m-manylinux2010_x86_64.whl (521.0 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp27-cp27mu-manylinux1_i686.whl (535.3 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp27-cp27mu-manylinux1_x86_64.whl (530.9 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp27-cp27mu-manylinux2010_x86_64.whl (521.0 kB) File type Wheel Python version cp27 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp35-cp35m-macosx_10_13_x86_64.whl (374.6 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp35-cp35m-manylinux1_i686.whl (532.4 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp35-cp35m-manylinux1_x86_64.whl (527.4 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp35-cp35m-manylinux2010_x86_64.whl (517.6 kB) File type Wheel Python version cp35 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp36-cp36m-macosx_10_9_x86_64.whl (374.6 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp36-cp36m-manylinux1_i686.whl (532.4 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp36-cp36m-manylinux1_x86_64.whl (527.4 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp36-cp36m-manylinux2010_x86_64.whl (517.3 kB) File type Wheel Python version cp36 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp37-cp37m-macosx_10_9_x86_64.whl (374.6 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp37-cp37m-manylinux1_i686.whl (532.4 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp37-cp37m-manylinux1_x86_64.whl (527.4 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp37-cp37m-manylinux2010_x86_64.whl (517.3 kB) File type Wheel Python version cp37 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp38-cp38-macosx_10_9_x86_64.whl (374.7 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp38-cp38-manylinux1_i686.whl (532.8 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp38-cp38-manylinux1_x86_64.whl (528.0 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-cp38-cp38-manylinux2010_x86_64.whl (517.9 kB) File type Wheel Python version cp38 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-pp371-pypy3_71-manylinux2010_x86_64.whl (493.9 kB) File type Wheel Python version pp371 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-py2.py3-none-win32.whl (262.0 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size coincurve-13.0.0-py2.py3-none-win_amd64.whl (240.7 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes
Filename, size coincurve-13.0.0.tar.gz (961.6 kB) File type Source Python version None Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page