Skip to content

Commit

Permalink
use libsecp256k1 master
Browse files Browse the repository at this point in the history
this removes schnorr
[bitcoin-core/secp256k1#425
  • Loading branch information
ofek committed Mar 28, 2017
1 parent 1112bb8 commit b3da2c9
Show file tree
Hide file tree
Showing 6 changed files with 5 additions and 195 deletions.
10 changes: 0 additions & 10 deletions .travis/install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,6 @@ if [[ $TRAVIS_OS_NAME == "osx" ]]; then
source ~/virtualenv/python${TRAVIS_PYTHON_VERSION}/bin/activate
fi

# Build lib-secp256k1 to test non bundled installation
if [[ $BUNDLED -eq 0 ]]; then
git clone git://github.com/bitcoin/secp256k1.git libsecp256k1_ext
builtin pushd libsecp256k1_ext
./autogen.sh
./configure --enable-module-recovery --enable-experimental --enable-module-ecdh --enable-module-schnorr
make
builtin popd
fi

# Install necessary packages
python -m pip install -U setuptools cffi pytest coverage coveralls

Expand Down
18 changes: 2 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
# Coincurve

Python CFFI bindings for [libsecp256k1](https://github.com/bitcoin/secp256k1)
Python CFFI bindings for [libsecp256k1](https://github.com/bitcoin-core/secp256k1)
(an experimental and optimized C library for EC operations on curve secp256k1).

This is a fork of [https://github.com/ludbb/secp256k1-py](https://github.com/ludbb/secp256k1-py).

New features:

- Newer version of [libsecp256k1](https://github.com/bitcoin-core/secp256k1)
- Implements a fix for [https://bugs.python.org/issue28150](https://bugs.python.org/issue28150)
to support Python 3.6
- Supports Windows (soon)
Expand Down Expand Up @@ -156,21 +157,6 @@ convert a recoverable signature to a normal signature, i.e. one that can be used
> NOTE: `ecdsa_recover*` can only be used if the `secp256k1` C library is compiled with support for it. If there is no support, an Exception will be raised when calling any of them.

#### class `secp256k1.Schnorr`

The `Schnorr` class is intended to be used as a mix in. Its methods can be accessed from any `secp256k1.PrivateKey` or `secp256k1.PublicKey` instances.

##### Methods

- `schnorr_recover(msg, schnorr_sig, raw=False, digest=hashlib.sha256)` -> internal object<br/>
recover and return a public key from a Schnorr signature. `schnorr_sig` is expected to be the result from `schnorr_partial_combine` or `schnorr_sign`. `msg`, `raw`, and `digest` are used as described in `ecdsa_sign`.

- `schnorr_partial_combine(schnorr_sigs)` -> bytes<br/>
combine multiple Schnorr partial signatures. `raw_sigs` is expected to be a list (or similar iterable) of signatures resulting from `PrivateKey.schnorr_partial_sign`. If the signatures cannot be combined, an Exception is raised.

> NOTE: `schnorr_*` can only be used if the `secp256k1` C library is compiled with support for it. If there is no support, an Exception will be raised when calling any of them.

#### Constants

##### `secp256k1.FLAG_SIGN`
Expand Down
1 change: 0 additions & 1 deletion _cffi_build/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@ def _mk_ffi(sources, name='_libsecp256k1', **kwargs):
Source('secp256k1.h', '#include <secp256k1.h>'),
Source('secp256k1_ecdh.h', '#include <secp256k1_ecdh.h>'),
Source('secp256k1_recovery.h', '#include <secp256k1_recovery.h>'),
Source('secp256k1_schnorr.h', '#include <secp256k1_schnorr.h>'),
]

ffi = _mk_ffi(modules, libraries=['secp256k1'])
48 changes: 0 additions & 48 deletions _cffi_build/secp256k1_schnorr.h

This file was deleted.

120 changes: 2 additions & 118 deletions coincurve/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
NO_FLAGS = lib.SECP256K1_CONTEXT_NONE

HAS_RECOVERABLE = hasattr(lib, 'secp256k1_ecdsa_sign_recoverable')
HAS_SCHNORR = hasattr(lib, 'secp256k1_schnorr_sign')
HAS_ECDH = hasattr(lib, 'secp256k1_ecdh')


Expand Down Expand Up @@ -158,48 +157,7 @@ def ecdsa_recoverable_convert(self, recover_sig):
return normal_sig


class Schnorr: # Use as a mixin; instance.ctx is assumed to exist.

def schnorr_recover(self, msg, schnorr_sig, raw=False,
digest=hashlib.sha256):
if not HAS_SCHNORR:
raise Exception("secp256k1_schnorr not enabled")
if self.flags & FLAG_VERIFY != FLAG_VERIFY:
raise Exception("instance not configured for sig verification")

msg32 = _hash32(msg, raw, digest)
pubkey = ffi.new('secp256k1_pubkey *')

recovered = lib.secp256k1_schnorr_recover(
self.ctx, pubkey, schnorr_sig, msg32)
if recovered:
return pubkey
raise Exception('failed to recover public key')

def schnorr_partial_combine(self, schnorr_sigs):
"""Combine multiple Schnorr partial signatures."""
if not HAS_SCHNORR:
raise Exception("secp256k1_schnorr not enabled")
assert len(schnorr_sigs) > 0

sig64 = ffi.new('char [64]')
sig64sin = []
for sig in schnorr_sigs:
if not isinstance(sig, bytes):
raise TypeError('expected bytes, got {}'.format(type(sig)))
if len(sig) != 64:
raise Exception('invalid signature length')
sig64sin.append(ffi.new('char []', sig))

res = lib.secp256k1_schnorr_partial_combine(
self.ctx, sig64, sig64sin, len(sig64sin))
if res <= 0:
raise Exception('failed to combine signatures ({})'.format(res))

return bytes(ffi.buffer(sig64, 64))


class PublicKey(Base, ECDSA, Schnorr):
class PublicKey(Base, ECDSA):

def __init__(self, pubkey=None, raw=False, flags=FLAG_VERIFY, ctx=None):
Base.__init__(self, ctx, flags)
Expand Down Expand Up @@ -286,21 +244,6 @@ def ecdsa_verify(self, msg, raw_sig, raw=False, digest=hashlib.sha256):

return bool(verified)

def schnorr_verify(self, msg, schnorr_sig, raw=False,
digest=hashlib.sha256):
assert self.public_key, "No public key defined"
if not HAS_SCHNORR:
raise Exception("secp256k1_schnorr not enabled")
if self.flags & FLAG_VERIFY != FLAG_VERIFY:
raise Exception("instance not configured for sig verification")

msg32 = _hash32(msg, raw, digest)

verified = lib.secp256k1_schnorr_verify(
self.ctx, schnorr_sig, msg32, self.public_key)

return bool(verified)

def ecdh(self, scalar):
assert self.public_key, "No public key defined"
if not HAS_ECDH:
Expand All @@ -317,7 +260,7 @@ def ecdh(self, scalar):
return bytes(ffi.buffer(result, 32))


class PrivateKey(Base, ECDSA, Schnorr):
class PrivateKey(Base, ECDSA):

def __init__(self, privkey=None, raw=True, flags=ALL_FLAGS, ctx=None):
assert flags in (ALL_FLAGS, FLAG_SIGN)
Expand Down Expand Up @@ -406,65 +349,6 @@ def ecdsa_sign_recoverable(self, msg, raw=False, digest=hashlib.sha256):

return raw_sig

def schnorr_sign(self, msg, raw=False, digest=hashlib.sha256):
if not HAS_SCHNORR:
raise Exception("secp256k1_schnorr not enabled")

msg32 = _hash32(msg, raw, digest)
sig64 = ffi.new('char [64]')

signed = lib.secp256k1_schnorr_sign(
self.ctx, sig64, msg32, self.private_key, ffi.NULL, ffi.NULL)
assert signed == 1

return bytes(ffi.buffer(sig64, 64))

def schnorr_generate_nonce_pair(self, msg, raw=False,
digest=hashlib.sha256):
"""
Generate a nonce pair deterministically for use with
schnorr_partial_sign.
"""
if not HAS_SCHNORR:
raise Exception("secp256k1_schnorr not enabled")

msg32 = _hash32(msg, raw, digest)
pubnonce = ffi.new('secp256k1_pubkey *')
privnonce = ffi.new('char [32]')

valid = lib.secp256k1_schnorr_generate_nonce_pair(
self.ctx, pubnonce, privnonce, msg32, self.private_key,
ffi.NULL, ffi.NULL)
assert valid == 1

return pubnonce, privnonce

def schnorr_partial_sign(self, msg, privnonce, pubnonce_others,
raw=False, digest=hashlib.sha256):
"""
Produce a partial Schnorr signature, which can be combined using
schnorr_partial_combine to end up with a full signature that is
verifiable using PublicKey.schnorr_verify.
To combine pubnonces, use PublicKey.combine.
Do not pass the pubnonce produced for the respective privnonce;
combine the pubnonces from other signers and pass that instead.
"""
if not HAS_SCHNORR:
raise Exception("secp256k1_schnorr not enabled")

msg32 = _hash32(msg, raw, digest)
sig64 = ffi.new('char [64]')

res = lib.secp256k1_schnorr_partial_sign(
self.ctx, sig64, msg32, self.private_key,
pubnonce_others, privnonce)
if res <= 0:
raise Exception('failed to partially sign ({})'.format(res))

return bytes(ffi.buffer(sig64, 64))


def _hash32(msg, raw, digest):
if not raw:
Expand Down
3 changes: 1 addition & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

# Version of libsecp256k1 to download if none exists in the `libsecp256k1`
# directory
LIB_TARBALL_URL = "https://github.com/bitcoin-core/secp256k1/archive/c5b32e16c4d2560ce829caf88a413fc06fd83d09.tar.gz"
LIB_TARBALL_URL = "https://github.com/bitcoin-core/secp256k1/archive/master.tar.gz"


# We require setuptools >= 3.3
Expand Down Expand Up @@ -196,7 +196,6 @@ def run(self):
"--with-bignum=gmp",
"--enable-experimental",
"--enable-module-ecdh",
"--enable-module-schnorr",
]

log.debug("Running configure: {}".format(" ".join(cmd)))
Expand Down

0 comments on commit b3da2c9

Please sign in to comment.