Skip to main content

Cross-platform Python CFFI bindings for libsecp256k1

Project description

https://img.shields.io/pypi/v/coincurve.svg?style=flat-square https://img.shields.io/travis/ofek/coincurve/master.svg?style=flat-square https://img.shields.io/pypi/pyversions/coincurve.svg?style=flat-square https://img.shields.io/pypi/l/coincurve.svg?style=flat-square

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

Users

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 for example the necessary packages are:

  • build-essential

  • automake

  • pkg-config

  • libtool

  • libffi-dev

  • libgmp-dev

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(x) 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.

8.0.1

  • No longer package tests

8.0.0

  • New: Binary wheels for Python 3.7!

  • Changed: Binary wheels on macOS for Python 3.5 now use Homebrew Python for compilation due to new security requirements

  • Make build system support new GitHub & PyPI security requirements

  • Improvements from libsecp256k1 master

7.1.0

  • Pin version of libsecp256k1

  • Improve docs

7.0.0

  • Improvements from libsecp256k1 master

  • Fix build script

6.0.0

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.

Source Distribution

coincurve-8.0.2rc2.tar.gz (910.3 kB view details)

Uploaded Source

Built Distributions

If you're not sure about the file name format, learn more about wheel file names.

coincurve-8.0.2rc2-cp37-cp37m-macosx_10_6_intel.whl (149.1 kB view details)

Uploaded CPython 3.7mmacOS 10.6+ Intel (x86-64, i386)

coincurve-8.0.2rc2-cp36-cp36m-macosx_10_6_intel.whl (149.1 kB view details)

Uploaded CPython 3.6mmacOS 10.6+ Intel (x86-64, i386)

coincurve-8.0.2rc2-cp35-cp35m-macosx_10_12_x86_64.whl (138.7 kB view details)

Uploaded CPython 3.5mmacOS 10.12+ x86-64

coincurve-8.0.2rc2-cp27-cp27m-macosx_10_6_intel.whl (148.9 kB view details)

Uploaded CPython 2.7mmacOS 10.6+ Intel (x86-64, i386)

File details

Details for the file coincurve-8.0.2rc2.tar.gz.

File metadata

  • Download URL: coincurve-8.0.2rc2.tar.gz
  • Upload date:
  • Size: 910.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.14

File hashes

Hashes for coincurve-8.0.2rc2.tar.gz
Algorithm Hash digest
SHA256 6551e08c2c54966cf233e69a787cb20142acac43b588d8d361e384a73fe335dd
MD5 20542996147096d370a3a7d340466980
BLAKE2b-256 8584071e18e44d620f77ffea3fd9e5fa63658e782dc6be49116cd879ce244dd7

See more details on using hashes here.

File details

Details for the file coincurve-8.0.2rc2-cp37-cp37m-macosx_10_6_intel.whl.

File metadata

  • Download URL: coincurve-8.0.2rc2-cp37-cp37m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 149.1 kB
  • Tags: CPython 3.7m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/39.0.1 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.7.0

File hashes

Hashes for coincurve-8.0.2rc2-cp37-cp37m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 1faa2b6c4d87d5ebf3a49c66dde2ac46677e26b0fe7b32793eb151005f579647
MD5 6c55f23fa0162d41c60c710184f19c87
BLAKE2b-256 8030a380bb92d4a9b8fa3e4b7bd0596ca2fe8dfead12f0282298df4aa6ca5ca8

See more details on using hashes here.

File details

Details for the file coincurve-8.0.2rc2-cp36-cp36m-macosx_10_6_intel.whl.

File metadata

  • Download URL: coincurve-8.0.2rc2-cp36-cp36m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 149.1 kB
  • Tags: CPython 3.6m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.6.5

File hashes

Hashes for coincurve-8.0.2rc2-cp36-cp36m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 899fa44edadd1ebbd63b36a31b21a5c04dc1412920839d7c47d17816bae12af1
MD5 aaff3e94219e37c2b8e9c4444d9ba4cb
BLAKE2b-256 716227a5540b14026e29f89ab654a602317659d3133a9d38700ccba60bb0d534

See more details on using hashes here.

File details

Details for the file coincurve-8.0.2rc2-cp35-cp35m-macosx_10_12_x86_64.whl.

File metadata

  • Download URL: coincurve-8.0.2rc2-cp35-cp35m-macosx_10_12_x86_64.whl
  • Upload date:
  • Size: 138.7 kB
  • Tags: CPython 3.5m, macOS 10.12+ x86-64
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/3.5.5

File hashes

Hashes for coincurve-8.0.2rc2-cp35-cp35m-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 f0752946bc8363519d70cec116fedaf65d7d3596a77c32019f7205886e04c8ea
MD5 64721e473f71996220146da8d3c77942
BLAKE2b-256 d51dd9c0bb6d6c0b8694fe068d70f8ba7670cc306933a17b5ca5c458e42a04fa

See more details on using hashes here.

File details

Details for the file coincurve-8.0.2rc2-cp27-cp27m-macosx_10_6_intel.whl.

File metadata

  • Download URL: coincurve-8.0.2rc2-cp27-cp27m-macosx_10_6_intel.whl
  • Upload date:
  • Size: 148.9 kB
  • Tags: CPython 2.7m, macOS 10.6+ Intel (x86-64, i386)
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.0.0 requests-toolbelt/0.8.0 tqdm/4.24.0 CPython/2.7.15

File hashes

Hashes for coincurve-8.0.2rc2-cp27-cp27m-macosx_10_6_intel.whl
Algorithm Hash digest
SHA256 2eaff28ad20f4b0615d74795c59f3c234963c64bf259d82414dbfb392eb9eabf
MD5 870ab3f5628e91e2f1aae8eda3a2e4c4
BLAKE2b-256 00d0c06be7c163f54797bb16b31b3305958ba71f2a49b36d94a046ebce5b32e4

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page