Cross-platform Python CFFI bindings for libsecp256k1
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

215 lines
5.3 KiB

7 years ago
Coincurve
=========
.. image:: https://img.shields.io/pypi/v/coincurve.svg?style=flat-square
:target: https://pypi.org/project/coincurve
.. image:: https://img.shields.io/travis/ofek/coincurve.svg?branch=master&style=flat-square
:target: https://travis-ci.org/ofek/coincurve
.. image:: https://img.shields.io/pypi/pyversions/coincurve.svg?style=flat-square
:target: https://pypi.org/project/coincurve
.. image:: https://img.shields.io/badge/license-MIT-blue.svg?style=flat-square
:target: https://en.wikipedia.org/wiki/MIT_License
-----
This library provides well-tested Python CFFI bindings for
`libsecp256k1 <https://github.com/bitcoin-core/secp256k1>`_, the heavily
optimized C library used by `Bitcoin Core <https://github.com/bitcoin/bitcoin>`_
for operations on elliptic curve secp256k1.
Coincurve replaces `secp256k1-py <https://github.com/ludbb/secp256k1-py>`_.
New features include:
7 years ago
- Uses newest version of `libsecp256k1 <https://github.com/bitcoin-core/secp256k1>`_
7 years ago
- 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
- Endomorphism optimization is enabled
- A global context is used by default, drastically increasing performance
7 years ago
- Fixed ECDH
7 years ago
- A fix to remove CFFI warnings
- Implements a fix for `<https://bugs.python.org/issue28150>`_ to support Python 3.6+ on macOS
7 years ago
**To retain backward compatibility with secp256k1-py, use coincurve==2.1.1 specifically!**
Anything after that has a modified (cleaner :) API and only supports newest versions of Python.
7 years ago
Installation
------------
Coincurve is distributed on PyPI and is available on Linux/macOS and Windows and
7 years ago
supports Python 2.7/3.5+ and PyPy2.7-v5.7.1/PyPy3.5-v5.7.1+.
7 years ago
.. code-block:: bash
$ pip install coincurve
API
---
7 years ago
Coincurve provides a simple API.
7 years ago
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. hasher(message) must return 32 bytes.
- **context** (``coincurve.Context``)
* Returns: ``bool``
7 years ago
coincurve.PrivateKey
^^^^^^^^^^^^^^^^^^^^
All instances have a ``public_key`` of type ``coincurve.PublicKey``
``PrivateKey(secret=None, context=GLOBAL_CONTEXT)``
7 years ago
* Parameters:
7 years ago
- **secret** (``bytes``) - The secret to use.
- **context** (``coincurve.Context``)
7 years ago
Methods
~~~~~~~
7 years ago
*classmethod* ``from_int(num, context=GLOBAL_CONTEXT)``
*classmethod* ``from_pem(pem, context=GLOBAL_CONTEXT)``
*classmethod* ``from_der(der, context=GLOBAL_CONTEXT)``
7 years ago
``sign(message, hasher=sha256)``
7 years ago
* Parameters:
7 years ago
- **message** (``bytes``) - The message to sign.
- **hasher** - The hash function to use. hasher(message) must return 32 bytes.
7 years ago
* Returns: ``bytes``. 71 <= len(signature) <= 72
7 years ago
``sign_recoverable(message, hasher=sha256)``
* Parameters:
7 years ago
- **message** (``bytes``) - The message to sign.
- **hasher** - The hash function to use. hasher(message) must return 32 bytes.
7 years ago
* Returns: ``bytes``
``ecdh(public_key)``
7 years ago
7 years ago
Computes a Diffie-Hellman secret in constant time.
7 years ago
* Parameters:
7 years ago
- **public_key** (``bytes``) - Another party's public key in compressed or uncompressed form.
7 years ago
7 years ago
* Returns: ``bytes``
7 years ago
7 years ago
``add(scalar, update=False)``
* Parameters:
7 years ago
- **scalar** (``bytes``) - The scalar to add.
- **update** - If ``True``, will update and return ``self``.
7 years ago
* Returns: ``coincurve.PrivateKey``
``multiply(scalar, update=False)``
* Parameters:
7 years ago
7 years ago
- **scalar** (``bytes``) - The scalar to multiply.
- **update** - If ``True``, will update and return ``self``.
7 years ago
7 years ago
* Returns: ``coincurve.PrivateKey``
7 years ago
7 years ago
``to_int()``
7 years ago
7 years ago
``to_pem()``
7 years ago
7 years ago
``to_der()``
7 years ago
7 years ago
coincurve.PublicKey
^^^^^^^^^^^^^^^^^^^
``PublicKey(data, context=GLOBAL_CONTEXT)``
* Parameters:
- **data** (``bytes``) - The public key in compressed or uncompressed form.
- **context** (``coincurve.Context``)
7 years ago
Methods
~~~~~~~
7 years ago
*classmethod* ``from_secret(secret, context=GLOBAL_CONTEXT)``
*classmethod* ``from_valid_secret(secret, context=GLOBAL_CONTEXT)``
*classmethod* ``from_point(x, y, context=GLOBAL_CONTEXT)``
``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. hasher(message) must return 32 bytes.
7 years ago
* Returns: ``bool``
7 years ago
``add(scalar, update=False)``
* Parameters:
- **scalar** (``bytes``) - The scalar to add.
- **update** - If ``True``, will update and return ``self``.
* Returns: ``coincurve.PublicKey``
``multiply(scalar, update=False)``
* Parameters:
- **scalar** (``bytes``) - The scalar to multiply.
- **update** - If ``True``, will update and return ``self``.
* Returns: ``coincurve.PublicKey``
7 years ago
7 years ago