Skip to content

Commit

Permalink
Updated to use secp256k1 tags. Using: v0.3.2 (#126)
Browse files Browse the repository at this point in the history
* Updated to use secp256k1 tags. Using: v0.3.2

* Ruff compliance

* Reinstate use of revision number. Remove prelim Source in build.py

* Updated _windows_libsecp256k1.py

* Updated secp256k1.h

* Mistakenly used v0.3.3-dev. Added `nake check` that highlights the version number during the build

* New syntax for 'tox -e docs-ci -- build' ?

* 'bench' fails - commenting to test the workflow - updated deprecated actions

* Update macos runner. Missing dlll ?

* Odd ill-numbered dll

* Remove UPSTREAM_TAG comment

Co-authored-by: Ofek Lev <[email protected]>

* Remove TARBALL_TAG comment

Co-authored-by: Ofek Lev <[email protected]>

* Remove superfluous bool() casting

Co-authored-by: Ofek Lev <[email protected]>

---------

Co-authored-by: Ofek Lev <[email protected]>
  • Loading branch information
MementoRC and ofek authored Sep 3, 2023
1 parent a3c2fdb commit 2dfea67
Show file tree
Hide file tree
Showing 11 changed files with 114 additions and 90 deletions.
8 changes: 5 additions & 3 deletions .github/scripts/build-windows-wheels.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -ex

build_dll() {
./autogen.sh
./configure --host=$1 --enable-module-recovery --enable-experimental --enable-module-ecdh --enable-module-extrakeys --enable-module-schnorrsig --enable-benchmark=no --enable-tests=no --enable-openssl-tests=no --enable-exhaustive-tests=no --enable-static --disable-dependency-tracking --with-pic
./configure --host=$1 --enable-module-recovery --enable-experimental --enable-module-ecdh --enable-module-extrakeys --enable-module-schnorrsig --enable-benchmark=no --enable-tests=no --enable-exhaustive-tests=no --enable-static --disable-dependency-tracking --with-pic
make
}

Expand All @@ -26,14 +26,16 @@ cp 64bit 32bit -R

cd 64bit
build_dll x86_64-w64-mingw32
mv .libs/libsecp256k1-0.dll ../clean/coincurve/libsecp256k1.dll
# Not sure why it ended-up being a -2.dll instead of -0.dll: Researching
mv .libs/libsecp256k1-?.dll ../clean/coincurve/libsecp256k1.dll
cd ../clean
python setup.py bdist_wheel --plat-name=win_amd64
rm coincurve/libsecp256k1.dll

cd ../32bit
build_dll i686-w64-mingw32
mv .libs/libsecp256k1-0.dll ../clean/coincurve/libsecp256k1.dll
# Not sure why it ended-up being a -2.dll instead of -0.dll: Researching
mv .libs/libsecp256k1-?.dll ../clean/coincurve/libsecp256k1.dll
cd ../clean
python setup.py bdist_wheel --plat-name=win32

Expand Down
35 changes: 18 additions & 17 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ concurrency:
cancel-in-progress: true

env:
COINCURVE_UPSTREAM_REF: ddf2b2910eb19032f8dd657c66735115ae24bfba
COINCURVE_UPSTREAM_REF: acf5c55ae6a94e5ca847e07def40427547876101
COINCURVE_IGNORE_SYSTEM_LIB: '1'
CIBW_BEFORE_ALL_MACOS: ./.github/scripts/install-macos-build-deps.sh
CIBW_ENVIRONMENT_PASS_LINUX: >
Expand All @@ -28,8 +28,9 @@ env:
b=PrivateKey();
assert a.ecdh(b.public_key.format())==b.ecdh(a.public_key.format())
"
CIBW_TEST_SKIP: "*-macosx_arm64"
CIBW_SKIP: >
pp*
pp*
jobs:
test:
Expand All @@ -40,10 +41,10 @@ jobs:
PYTHON_VERSION: '3.10'

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up Python ${{ env.PYTHON_VERSION }}
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: ${{ env.PYTHON_VERSION }}

Expand Down Expand Up @@ -77,12 +78,12 @@ jobs:
runs-on: ubuntu-20.04

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Build wheels
uses: pypa/[email protected]

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
with:
name: artifacts
path: wheelhouse/*.whl
Expand All @@ -92,17 +93,17 @@ jobs:
name: Build macOS wheels
needs:
- test
runs-on: macos-10.15
runs-on: macos-12

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Build wheels
uses: pypa/[email protected]
env:
CIBW_ARCHS_MACOS: x86_64

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
with:
name: artifacts
path: wheelhouse/*.whl
Expand All @@ -112,10 +113,10 @@ jobs:
name: Build macOS wheels for ARM
needs:
- test
runs-on: macos-10.15
runs-on: macos-12

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Build wheels
uses: pypa/[email protected]
Expand All @@ -124,7 +125,7 @@ jobs:
COINCURVE_CROSS_HOST: aarch64-apple-darwin
CFLAGS: -target arm64-apple-macos11

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
with:
name: artifacts
path: wheelhouse/*.whl
Expand All @@ -137,7 +138,7 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Install build dependencies
run: python -m pip install --upgrade cffi
Expand All @@ -148,7 +149,7 @@ jobs:
- name: Build Windows wheels
run: ./.github/scripts/build-windows-wheels.sh

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
with:
name: artifacts
path: dist/*
Expand All @@ -165,7 +166,7 @@ jobs:
(github.ref == 'refs/heads/master' || startsWith(github.event.ref, 'refs/tags'))
steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3

- name: Set up QEMU
uses: docker/setup-qemu-action@v1
Expand All @@ -177,7 +178,7 @@ jobs:
env:
CIBW_ARCHS_LINUX: aarch64

- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
with:
name: artifacts
path: wheelhouse/*.whl
Expand All @@ -196,7 +197,7 @@ jobs:
github.event_name == 'push' && startsWith(github.event.ref, 'refs/tags')
steps:
- uses: actions/download-artifact@v2
- uses: actions/download-artifact@v3
with:
name: artifacts
path: dist
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ jobs:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v2
- uses: actions/checkout@v3
with:
# Fetch all history for applying timestamps to every page
fetch-depth: 0

- name: Set up Python
uses: actions/setup-python@v2
uses: actions/setup-python@v3
with:
python-version: '3.9'

Expand All @@ -30,7 +30,7 @@ jobs:
run: python -m pip install --upgrade tox

- name: Build documentation
run: tox -e docs-ci build
run: tox -e docs-ci -- build

- uses: actions/upload-artifact@v2
with:
Expand Down
20 changes: 19 additions & 1 deletion _cffi_build/secp256k1.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
typedef struct secp256k1_context_struct secp256k1_context;
typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;

typedef struct {
unsigned char data[64];
Expand All @@ -22,15 +23,27 @@ typedef int (*secp256k1_nonce_function)(
#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_CONTEXT_NONE ...

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

#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 ...
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN ...
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD ...

void secp256k1_selftest(void);

secp256k1_context* secp256k1_context_create(
unsigned int flags
);
Expand All @@ -55,6 +68,11 @@ void secp256k1_context_set_error_callback(
const void* data
);

secp256k1_scratch_space* secp256k1_scratch_space_create(
const secp256k1_context* ctx,
size_t size
);

int secp256k1_ec_pubkey_parse(
const secp256k1_context* ctx,
secp256k1_pubkey* pubkey,
Expand Down
39 changes: 28 additions & 11 deletions coincurve/_windows_libsecp256k1.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

BASE_DEFINITIONS = """
typedef struct secp256k1_context_struct secp256k1_context;
typedef struct secp256k1_scratch_space_struct secp256k1_scratch_space;
typedef struct {
unsigned char data[64];
Expand All @@ -22,20 +23,31 @@
unsigned int attempt
);
#define SECP256K1_FLAGS_TYPE_MASK 255
#define SECP256K1_FLAGS_TYPE_CONTEXT 1
#define SECP256K1_FLAGS_TYPE_COMPRESSION 2
#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_BIT_CONTEXT_VERIFY 256
#define SECP256K1_FLAGS_BIT_CONTEXT_SIGN 512
#define SECP256K1_FLAGS_BIT_COMPRESSION 256
#define SECP256K1_CONTEXT_NONE ...
#define SECP256K1_CONTEXT_VERIFY 257
#define SECP256K1_CONTEXT_SIGN 513
#define SECP256K1_CONTEXT_NONE 1
#define SECP256K1_CONTEXT_VERIFY ...
#define SECP256K1_CONTEXT_SIGN ...
#define SECP256K1_EC_COMPRESSED 258
#define SECP256K1_EC_UNCOMPRESSED 2
#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 ...
#define SECP256K1_TAG_PUBKEY_HYBRID_EVEN ...
#define SECP256K1_TAG_PUBKEY_HYBRID_ODD ...
void secp256k1_selftest(void);
secp256k1_context* secp256k1_context_create(
unsigned int flags
Expand All @@ -61,6 +73,11 @@
const void* data
);
secp256k1_scratch_space* secp256k1_scratch_space_create(
const secp256k1_context* ctx,
size_t size
);
int secp256k1_ec_pubkey_parse(
const secp256k1_context* ctx,
secp256k1_pubkey* pubkey,
Expand Down
7 changes: 4 additions & 3 deletions coincurve/context.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
from os import urandom
from threading import Lock
from typing import Optional

from coincurve.flags import CONTEXT_ALL, CONTEXT_FLAGS

from ._libsecp256k1 import ffi, lib


class Context:
def __init__(self, seed: bytes = None, flag=CONTEXT_ALL, name: str = ''):
def __init__(self, seed: Optional[bytes] = None, flag=CONTEXT_ALL, name: str = ''):
if flag not in CONTEXT_FLAGS:
raise ValueError('{} is an invalid context flag.'.format(flag))
raise ValueError(f'{flag} is an invalid context flag.')
self._lock = Lock()

self.ctx = ffi.gc(lib.secp256k1_context_create(flag), lib.secp256k1_context_destroy)
self.reseed(seed)

self.name = name

def reseed(self, seed: bytes = None):
def reseed(self, seed: Optional[bytes] = None):
"""
Protects against certain possible future side-channel timing attacks.
"""
Expand Down
4 changes: 2 additions & 2 deletions coincurve/keys.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import os
from typing import Tuple
from typing import Optional, Tuple

from asn1crypto.keys import ECDomainParameters, ECPointBitString, ECPrivateKey, PrivateKeyAlgorithm, PrivateKeyInfo

Expand All @@ -24,7 +24,7 @@


class PrivateKey:
def __init__(self, secret: bytes = None, context: Context = GLOBAL_CONTEXT):
def __init__(self, secret: Optional[bytes] = None, context: Context = GLOBAL_CONTEXT):
"""
:param secret: The secret used to initialize the private key.
If not provided or `None`, a new key will be generated.
Expand Down
2 changes: 1 addition & 1 deletion coincurve/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def pad_scalar(scalar: bytes) -> bytes:

def validate_secret(secret: bytes) -> bytes:
if not 0 < bytes_to_int(secret) < GROUP_ORDER_INT:
raise ValueError('Secret scalar must be greater than 0 and less than {}.'.format(GROUP_ORDER_INT))
raise ValueError(f'Secret scalar must be greater than 0 and less than {GROUP_ORDER_INT}.')
return pad_scalar(secret)


Expand Down
Loading

0 comments on commit 2dfea67

Please sign in to comment.