Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Remove SECP256K1 deprecated context flags #135

Closed
wants to merge 15 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ jobs:

- name: Build wheels
uses: pypa/cibuildwheel@v2.16.2
env:
CIBW_TEST_REQUIRES: pytest pytest-benchmark
CIBW_TEST_COMMAND: python -m pytest {project}/tests

- uses: actions/upload-artifact@v3
with:
Expand Down Expand Up @@ -141,7 +144,7 @@ jobs:
- uses: actions/checkout@v4

- name: Install build dependencies
run: python -m pip install --upgrade cffi
run: python -m pip install --upgrade cffi pytest

- name: Build source distribution
run: python setup.py sdist
Expand Down
2 changes: 0 additions & 2 deletions _cffi_build/secp256k1.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@ typedef int (*secp256k1_nonce_function)(
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY ...
#define SECP256K1_FLAGS_BIT_COMPRESSION ...
#define SECP256K1_CONTEXT_NONE ...
#define SECP256K1_CONTEXT_VERIFY ...
#define SECP256K1_CONTEXT_SIGN ...
#define SECP256K1_CONTEXT_DECLASSIFY ...
#define SECP256K1_EC_COMPRESSED ...
#define SECP256K1_EC_UNCOMPRESSED ...
Expand Down
33 changes: 14 additions & 19 deletions coincurve/_windows_libsecp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,22 @@
unsigned int attempt
);

#define SECP256K1_FLAGS_TYPE_MASK ...
#define SECP256K1_FLAGS_TYPE_CONTEXT ...
#define SECP256K1_FLAGS_TYPE_COMPRESSION ...
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY ...
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN ...
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY ...
#define SECP256K1_FLAGS_BIT_COMPRESSION ...
#define SECP256K1_FLAGS_TYPE_MASK 255
#define SECP256K1_FLAGS_TYPE_CONTEXT 1
#define SECP256K1_FLAGS_TYPE_COMPRESSION 2
#define SECP256K1_FLAGS_BIT_CONTEXT_VERIFY 256
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN 512
#define SECP256K1_FLAGS_BIT_CONTEXT_DECLASSIFY 1024
#define SECP256K1_FLAGS_BIT_COMPRESSION 256
#define SECP256K1_TAG_PUBKEY_EVEN 2
#define SECP256K1_TAG_PUBKEY_ODD 3
#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED 4

#define SECP256K1_CONTEXT_NONE ...
#define SECP256K1_CONTEXT_NONE 1
#define SECP256K1_CONTEXT_DECLASSIFY 1025
#define SECP256K1_EC_COMPRESSED 258
#define SECP256K1_EC_UNCOMPRESSED 2

#define SECP256K1_CONTEXT_VERIFY ...
#define SECP256K1_CONTEXT_SIGN ...

#define SECP256K1_CONTEXT_DECLASSIFY ...

#define SECP256K1_EC_COMPRESSED ...
#define SECP256K1_EC_UNCOMPRESSED ...

#define SECP256K1_TAG_PUBKEY_EVEN ...
#define SECP256K1_TAG_PUBKEY_ODD ...
#define SECP256K1_TAG_PUBKEY_UNCOMPRESSED ...
extern const secp256k1_context *secp256k1_context_static;
extern void secp256k1_selftest(void);
extern secp256k1_context *secp256k1_context_create(
Expand Down
4 changes: 2 additions & 2 deletions coincurve/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
from threading import Lock
from typing import Optional

from coincurve.flags import CONTEXT_ALL, CONTEXT_FLAGS
from coincurve.flags import CONTEXT_FLAGS, CONTEXT_NONE

from ._libsecp256k1 import ffi, lib


class Context:
def __init__(self, seed: Optional[bytes] = None, flag=CONTEXT_ALL, name: str = ''):
def __init__(self, seed: Optional[bytes] = None, flag=CONTEXT_NONE, name: str = ''):
if flag not in CONTEXT_FLAGS:
raise ValueError(f'{flag} is an invalid context flag.')
self._lock = Lock()
Expand Down
5 changes: 1 addition & 4 deletions coincurve/flags.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,7 @@
from ._libsecp256k1 import lib

CONTEXT_SIGN = lib.SECP256K1_CONTEXT_SIGN
CONTEXT_VERIFY = lib.SECP256K1_CONTEXT_VERIFY
CONTEXT_ALL = CONTEXT_SIGN | CONTEXT_VERIFY
CONTEXT_NONE = lib.SECP256K1_CONTEXT_NONE
CONTEXT_FLAGS = {CONTEXT_SIGN, CONTEXT_VERIFY, CONTEXT_ALL, CONTEXT_NONE}
CONTEXT_FLAGS = {CONTEXT_NONE}

EC_COMPRESSED = lib.SECP256K1_EC_COMPRESSED
EC_UNCOMPRESSED = lib.SECP256K1_EC_UNCOMPRESSED
4 changes: 2 additions & 2 deletions coincurve/keys.py
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ def combine_keys(cls, public_keys, context: Context = GLOBAL_CONTEXT):

return PublicKey(public_key, context)

def format(self, compressed: bool = True) -> bytes: # noqa: A003
def format(self, compressed: bool = True) -> bytes:
"""
Format the public key.

Expand Down Expand Up @@ -579,7 +579,7 @@ def from_valid_secret(cls, secret: bytes, context: Context = GLOBAL_CONTEXT):

return cls(xonly_pubkey, parity=not not pk_parity[0], context=context)

def format(self) -> bytes: # noqa: A003
def format(self) -> bytes:
"""Serialize the public key.

:return: The public key serialized as 32 bytes.
Expand Down
2 changes: 2 additions & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
asn1crypto
cffi>=1.3.0
7 changes: 6 additions & 1 deletion tests/test_bench.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
import sys

from coincurve import PrivateKey, PublicKey, verify_signature

from .samples import MESSAGE, PRIVATE_KEY_BYTES, PUBLIC_KEY_COMPRESSED, SIGNATURE
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from tests.samples import MESSAGE, PRIVATE_KEY_BYTES, PUBLIC_KEY_COMPRESSED, SIGNATURE # noqa: E402


def test_verify_signature_util(benchmark):
Expand Down
7 changes: 6 additions & 1 deletion tests/test_ecdsa.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import os
import sys

from coincurve.ecdsa import cdata_to_der, der_to_cdata

from .samples import SIGNATURE
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from tests.samples import SIGNATURE # noqa: E402


def test_der():
Expand Down
25 changes: 25 additions & 0 deletions tests/test_flags.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
from coincurve.flags import (
CONTEXT_FLAGS,
CONTEXT_NONE,
EC_COMPRESSED,
EC_UNCOMPRESSED,
)


def test_context_flags():
expected_flags = {
CONTEXT_NONE,
}
assert CONTEXT_FLAGS == expected_flags


def test_context_none():
assert CONTEXT_NONE == (1 << 0)


def test_ec_compressed():
assert EC_COMPRESSED == (1 << 1) | (1 << 8)


def test_ec_uncompressed():
assert EC_UNCOMPRESSED == (1 << 1)
7 changes: 6 additions & 1 deletion tests/test_keys.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import sys
from hashlib import sha512
from os import urandom

Expand All @@ -7,7 +9,10 @@
from coincurve.keys import PrivateKey, PublicKey, PublicKeyXOnly
from coincurve.utils import bytes_to_int, int_to_bytes_padded, verify_signature

from .samples import (
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))


from tests.samples import ( # noqa: E402
MESSAGE,
PRIVATE_KEY_BYTES,
PRIVATE_KEY_DER,
Expand Down
12 changes: 11 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import os
import sys
from os import urandom

import pytest
Expand All @@ -17,7 +19,15 @@
verify_signature,
)

from .samples import MESSAGE, PRIVATE_KEY_DER, PUBLIC_KEY_COMPRESSED, PUBLIC_KEY_UNCOMPRESSED, SIGNATURE
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..')))

from tests.samples import ( # noqa: E402
MESSAGE,
PRIVATE_KEY_DER,
PUBLIC_KEY_COMPRESSED,
PUBLIC_KEY_UNCOMPRESSED,
SIGNATURE,
)


class TestPadScalar:
Expand Down
Loading