Cross-platform Python CFFI bindings for libsecp256k1
Project description
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.
Coincurve replaces secp256k1-py.
New features include:
Cleaner API
Uses newest version of libsecp256k1
Support for Windows
Linux, macOS, and Windows all have binary packages for both 64 and 32-bit architectures
Linux & macOS use GMP for faster computation
A global context is used by default, drastically increasing performance
Fixed ECDH
A fix to remove CFFI warnings
Implements a fix for https://bugs.python.org/issue28150 to support Python 3.6+ on macOS
Table of Contents
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.
Changelog
Important changes are emphasized.
7.1.0
Pin version of libsecp256k1
Improve docs
7.0.0
Improvements from libsecp256k1 master
Fix build script
6.0.0
Resolved #6. You can choose to use this or remain on 5.2.0. This will only be a temporary change. See https://github.com/ofek/coincurve/commit/3e93480b3e38c6b9beb0bc2de83bc3630fc74c46
5.2.0
Added support for supplying a custom nonce to PrivateKey.sign.
5.1.0
Added PublicKey.combine_keys class method.
Improvements to documentation.
5.0.1
Fixed an issue where validate_secret would occasionally erroneously error on user-provided secrets (secrets not generated by Coincurve itself) if there were not exactly 256 bits of entropy. See #5
5.0.0
4.5.1
First public stable release
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Hashes for coincurve-7.1.0-py2.py3-none-win_amd64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 683efd4f0ee89fa15ce7d6c4bb0cb48c86178f66a5d8538698ffdad8a079df3a |
|
MD5 | e8c8c8b2bab75ef8c2c2ed4d29d6640e |
|
BLAKE2b-256 | a87930f33c9f9047ecac8ff3e4ef815bc597c4c65691ee52e70c091a939e414e |
Hashes for coincurve-7.1.0-py2.py3-none-win32.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 14f775b2a2034661ba125655529c883ba09936d16edd2edd28714d9be6ed7ca8 |
|
MD5 | 2b547fff2bab7e398ca97e3c90be3ad8 |
|
BLAKE2b-256 | 9d6a774640aafb1089876eb7ba10dc45c1648e53c06f89302e8e9f44c41258a5 |
Hashes for coincurve-7.1.0-cp36-cp36m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ad41b88ceffb953f4abc8c2b09123aeb4ba2f6949301ba1a8908832415f38255 |
|
MD5 | c1f37de961fe63159ee726d97990a56c |
|
BLAKE2b-256 | 654f1f8cdade4b8457095c0c7afb6751ed3064abad4a753d5350aa1e8de8f2cb |
Hashes for coincurve-7.1.0-cp36-cp36m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7afa48c11124b1d14baf63ef543e3438af0d8ba82b580f7a3b0c82b1fae2c697 |
|
MD5 | 098283eed358bcd8a2a4c0e874475346 |
|
BLAKE2b-256 | 4bd151c1d2430125d77a5fe9f3c4d7a5d31bc319193b534883d0845491dade80 |
Hashes for coincurve-7.1.0-cp36-cp36m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 76b8c2738c9535223248c8da2b71de791b50ef18b6b8f1cd11b16242af9096b2 |
|
MD5 | a2ad511b234c2398ea218808e135fcd3 |
|
BLAKE2b-256 | df15a05b960d92df139542f113162362584b249544a5b74b195483ecb3283331 |
Hashes for coincurve-7.1.0-cp35-cp35m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9f565ca651e4304387460d8230cd8cc23ccea97c4a03f260b6b6238728516122 |
|
MD5 | 0beb4c6aecf61fd3f4dd15ff00c5e972 |
|
BLAKE2b-256 | 33dcdda43d6c4f918c240d6cf25348f76fe9131c0a8faef9c0cb17a875fd0763 |
Hashes for coincurve-7.1.0-cp35-cp35m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 29252b639e4c86d258cd01f747a8a00bc3e0533606e04cd1716af4b27cdcfe35 |
|
MD5 | ce939864f8e263c8667b358af056ec0a |
|
BLAKE2b-256 | 60bfc2f72bb63e757aa819d5e46a374068c5a633549c8dfec99054bc8e21674c |
Hashes for coincurve-7.1.0-cp35-cp35m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ed671ecef3eb69aa46f1805d136b793a627fce94fc90d20f86ac5a357e00c08f |
|
MD5 | 5b79ceee4f3cb8d4ebbe45b7bfeee704 |
|
BLAKE2b-256 | 4ac55954fb0610f6f4743846839f2f724e4b6dc31c209ada268878b9518004a8 |
Hashes for coincurve-7.1.0-cp27-cp27mu-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 605e73623f3b4d7a041af8032d55902fbb6b36151db427bad1655493a9bafcb3 |
|
MD5 | 0e3fff97d7a80a7265b1fa95d3d5688c |
|
BLAKE2b-256 | d97e0bf10583deb8d5b4abaa961d7d52c62c90dcb08cde5ff5fb95cb399e0c49 |
Hashes for coincurve-7.1.0-cp27-cp27mu-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ca8008b6c32726f15a50c3e9bac81c22d370b466377b21a8104e263610e90d94 |
|
MD5 | 3414551584e790f0ec25c446b80480ab |
|
BLAKE2b-256 | 667caa7f00cb4c89b513bf877688cb15f5b443d1a751008d00f5f75efaf66612 |
Hashes for coincurve-7.1.0-cp27-cp27m-manylinux1_x86_64.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 59bf263ce47e25865ea3491fa4d2d075ca3feb77d5ba961e95124d20b97916f8 |
|
MD5 | e47d0bacab38c5bc2f6d436686e7c100 |
|
BLAKE2b-256 | 96880f9a0b8f807e5c80aa64a06648ff7c90838307d285a599abb8d19ac42452 |
Hashes for coincurve-7.1.0-cp27-cp27m-manylinux1_i686.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7d66d081f46ce0e2c63d55cc791e7c1e6b0115eaee3c965edc0fb4f93d541537 |
|
MD5 | 85d8fc19cd8ebb6f106bb48744fa13ab |
|
BLAKE2b-256 | fb419fbe83d6499bc9118798babb420c7f7a875a728d15f7ed40c0515df10ed9 |
Hashes for coincurve-7.1.0-cp27-cp27m-macosx_10_6_intel.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 23bb379ab641423aea822effc3c63f06893f126cfd877e310382549048b6c4a6 |
|
MD5 | 368f08dcd5903826bebe40a8871c6b61 |
|
BLAKE2b-256 | 5f71bc669034bb9e6820f5987591cce02c81d379a7f34141e8fc57a3ac6bf740 |