### Install build dependencies on RHEL/CentOS Source: https://github.com/ofek/coincurve/blob/master/docs/install.md Installs the necessary packages for building coincurve from source on RHEL or CentOS systems. Requires root privileges. ```bash sudo yum install -y autoconf automake gcc gcc-c++ libffi-devel libtool make pkgconfig python3-devel ``` -------------------------------- ### Install build dependencies on macOS Source: https://github.com/ofek/coincurve/blob/master/docs/install.md Installs the necessary tools and libraries for building coincurve from source on macOS using Homebrew. Ensure Xcode command-line tools are installed. ```bash xcode-select --install brew install autoconf automake libffi libtool pkg-config python ``` -------------------------------- ### Install build dependencies on Debian/Ubuntu Source: https://github.com/ofek/coincurve/blob/master/docs/install.md Installs the necessary packages for building coincurve from source on Debian or Ubuntu-based systems. Requires root privileges. ```bash sudo apt-get install -y autoconf automake build-essential libffi-dev libtool pkg-config python3-dev ``` -------------------------------- ### Install build dependencies on Alpine Linux Source: https://github.com/ofek/coincurve/blob/master/docs/install.md Installs the necessary packages for building coincurve from source on Alpine Linux. Requires root privileges. ```bash sudo apk add autoconf automake build-base libffi-dev libtool pkgconfig python3-dev ``` -------------------------------- ### Install coincurve with pip Source: https://github.com/ofek/coincurve/blob/master/docs/install.md Use this command to install the coincurve library from PyPI. Ensure you have pip version 19.3 or later for binary wheel support. ```bash pip install coincurve ``` -------------------------------- ### Install coincurve without binary wheels Source: https://github.com/ofek/coincurve/blob/master/docs/install.md Use this option with pip to force building coincurve from source, bypassing pre-compiled binary wheels. This is useful on distributions without wheel support or for specific build configurations. ```bash pip install coincurve --no-binary coincurve ``` -------------------------------- ### Get Public Key Point Coordinates Source: https://context7.com/ofek/coincurve/llms.txt Extracts the (x, y) coordinate point of the public key as integers. These coordinates define the public key on the secp256k1 curve. ```python # Get the (x, y) point coordinates as integers x, y = public_key.point() print(f"X coordinate: {x}") print(f"Y coordinate: {y}") ``` -------------------------------- ### Run Benchmarks Source: https://github.com/ofek/coincurve/blob/master/docs/benchmarks.md Execute the benchmark script using either Hatch or UV. ```bash [hatch|uv] run scripts/bench.py ``` -------------------------------- ### Apply Platform-Specific Definitions Source: https://github.com/ofek/coincurve/blob/master/cm_python_module/CMakeLists.txt Sets preprocessor definitions based on the detected operating system. ```cmake if(CMAKE_SYSTEM_NAME STREQUAL "Linux") target_compile_definitions(${CFFI_OUTPUT_LIBRARY} PUBLIC "IS_LINUX") endif() if(CMAKE_SYSTEM_NAME STREQUAL "Darwin") target_compile_definitions(${CFFI_OUTPUT_LIBRARY} PUBLIC "IS_MACOS") endif() if(CMAKE_SYSTEM_NAME STREQUAL "Windows") target_compile_definitions(${CFFI_OUTPUT_LIBRARY} PUBLIC "IS_WINDOWS") endif() ``` -------------------------------- ### Create Build Directory Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Ensures a directory exists for generated C code. This is a prerequisite for subsequent build steps. ```cmake file(MAKE_DIRECTORY ${PROJECT_BINARY_DIR}/_gen_c_file) ``` -------------------------------- ### Create Directory with CMake Source: https://github.com/ofek/coincurve/blob/master/cm_library_cffi_headers/CMakeLists.txt Ensures a directory exists for generated CFFI code. Use this at the beginning of your CMakeLists.txt to prepare the output location. ```cmake file(MAKE_DIRECTORY ${CFFI_HEADERS_DIR}) ``` -------------------------------- ### Create PublicKeyXOnly from Secret Source: https://context7.com/ofek/coincurve/llms.txt Generates an x-only public key directly from a private key's secret scalar. This is a convenient way to obtain the x-only representation. ```python # Create from secret directly xonly_from_secret = PublicKeyXOnly.from_secret(private_key.secret) assert xonly_from_secret == xonly_pubkey print("X-only public key operations successful!") ``` -------------------------------- ### Verify Schnorr Signatures with X-Only Public Keys Source: https://context7.com/ofek/coincurve/llms.txt Demonstrates verifying a Schnorr signature against a message hash and handling verification failure for incorrect messages. ```python # Verify with x-only public key is_valid = xonly_pubkey.verify(signature, message_hash) print(f"Schnorr signature valid: {is_valid}") # Output: Schnorr signature valid: True # Wrong message fails verification wrong_hash = sha256(b"Wrong message").digest() is_valid = xonly_pubkey.verify(signature, wrong_hash) print(f"Wrong message valid: {is_valid}") # Output: Wrong message valid: False ``` -------------------------------- ### Format Public Key to Bytes Source: https://context7.com/ofek/coincurve/llms.txt Serializes the public key to bytes in either compressed (default) or uncompressed format. Compressed format is 33 bytes, while uncompressed is 65 bytes. ```python from coincurve import PrivateKey public_key = PrivateKey().public_key # Compressed format (33 bytes) - default compressed = public_key.format(compressed=True) print(f"Compressed: {compressed.hex()}") print(f"Compressed length: {len(compressed)} bytes") # Uncompressed format (65 bytes) uncompressed = public_key.format(compressed=False) print(f"Uncompressed: {uncompressed.hex()}") print(f"Uncompressed length: {len(uncompressed)} bytes") ``` -------------------------------- ### PrivateKey.sign_schnorr Source: https://context7.com/ofek/coincurve/llms.txt Creates a BIP340 Schnorr signature. ```APIDOC ## PrivateKey.sign_schnorr ### Description Creates a BIP340 Schnorr signature for a 32-byte message. ### Parameters - **message** (bytes) - Required - A 32-byte message (typically a hash). ``` -------------------------------- ### PublicKeyXOnly.verify Source: https://context7.com/ofek/coincurve/llms.txt Verifies a Schnorr signature against an x-only public key and a message hash. ```APIDOC ## PublicKeyXOnly.verify ### Description Verifies a Schnorr signature against an x-only public key and a message hash. ### Method `verify(signature: bytes, message_hash: bytes) -> bool` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from coincurve.keys import PublicKeyXOnly from hashlib import sha256 # Assume xonly_pubkey and signature are already defined # xonly_pubkey = PublicKeyXOnly(b'...') # signature = b'...' message_hash = sha256(b"Some message").digest() is_valid = xonly_pubkey.verify(signature, message_hash) print(f"Schnorr signature valid: {is_valid}") wrong_hash = sha256(b"Wrong message").digest() is_valid = xonly_pubkey.verify(signature, wrong_hash) print(f"Wrong message valid: {is_valid}") ``` ### Response #### Success Response (200) - **bool**: True if the signature is valid, False otherwise. #### Response Example ```json { "is_valid": true } ``` ``` -------------------------------- ### PublicKeyXOnly Object Source: https://github.com/ofek/coincurve/blob/master/docs/api.md Manages X-only public keys for Schnorr signatures. ```APIDOC ## coincurve.PublicKeyXOnly ### Description Represents and manages X-only public keys, primarily used with Schnorr signatures. ### Methods - `__init__`: Initializes a PublicKeyXOnly object. - `verify`: Verifies a Schnorr signature against a message. - `format`: Formats the public key. - `tweak_add`: Adds a tweak to the public key. - `from_secret`: Creates an X-only PublicKey from a private key secret. - `from_valid_secret`: Creates an X-only PublicKey from a private key secret, validating it. ### Parameters (Specific parameters for each method are not detailed in the provided text.) ### Request Example (Not applicable) ### Response (Not specified, depends on the method called.) ``` -------------------------------- ### Parse PublicKeyXOnly from Bytes Source: https://context7.com/ofek/coincurve/llms.txt Creates a PublicKeyXOnly object from a 32-byte x-coordinate. This allows reconstructing an x-only public key from its serialized form. ```python # Parse from 32-byte x-coordinate parsed = PublicKeyXOnly(serialized) assert parsed == xonly_pubkey ``` -------------------------------- ### Manage Private Keys with PrivateKey class Source: https://context7.com/ofek/coincurve/llms.txt The PrivateKey class handles key generation and derivation. It can generate random keys or be initialized from existing 32-byte secrets. ```python from coincurve import PrivateKey import os # Generate a new random private key private_key = PrivateKey() print(f"Generated key (hex): {private_key.to_hex()}") # Create from existing secret bytes (32 bytes) secret = bytes.fromhex("c28a9f80738f770d527803a566cf6fc3edf6cea586c4fc4a5223a5ad797e1ac3") private_key = PrivateKey(secret) # Access the associated public key public_key = private_key.public_key print(f"Public key (compressed): {public_key.format().hex()}") # Output: Public key (compressed): 033d5c2875c9bd116875a71a5db64cffcb13396b163d039b1d932782489180433 ``` -------------------------------- ### PublicKeyXOnly Source: https://context7.com/ofek/coincurve/llms.txt BIP340 x-only public key representation used for Schnorr signatures. ```APIDOC ## PublicKeyXOnly ### Description BIP340 x-only public key representation used for Schnorr signatures and Bitcoin Taproot. Only stores the x-coordinate (32 bytes) with parity information. ### Parameters #### Method Parameters - **secret** (bytes) - Required - The private key secret used to derive the x-only key. ``` -------------------------------- ### Manage Vendored Library Dependencies Source: https://github.com/ofek/coincurve/blob/master/cm_python_module/CMakeLists.txt Handles linking logic for vendored libraries, distinguishing between local builds and system-provided libraries. ```cmake if (PROJECT_IGNORE_SYSTEM_LIB OR NOT VENDORED_AS_SYSTEM_LIB_FOUND) # The build-type seems to be defined as 'MODULE', which creates issues with missing variables # for CMake: (This only happens on Windows though ...) set(CMAKE_MODULE_LINKER_FLAGS_COVERAGE "") add_dependencies(${CFFI_OUTPUT_LIBRARY} ${CFFI_INPUT_LIBRARY}) add_dependencies(${CFFI_OUTPUT_LIBRARY} cffi-c-binding) target_include_directories(${CFFI_OUTPUT_LIBRARY} PUBLIC ${VENDORED_HEADERS_DIR}) # Link the vendored library to the output library # https://docs.python.org/3/c-api/stable.html#limited-c-api target_link_libraries(${CFFI_OUTPUT_LIBRARY} PRIVATE ${CFFI_INPUT_LIBRARY}) elseif(VENDORED_AS_SYSTEM_LIB_FOUND) message(STATUS "Vendored system library found: ${VENDORED_AS_SYSTEM_LIB_LIBRARIES}") target_include_directories(${CFFI_OUTPUT_LIBRARY} PRIVATE ${VENDORED_AS_SYSTEM_LIB_INCLUDE_DIRS}) add_dependencies(${CFFI_OUTPUT_LIBRARY} cffi-c-binding) # On windows, using the LDFLAGS field creates /libpath... secp256k1.lib (correct), but at a later stage # /libpath is converted to \libpath and fails to be interpreted as a flag by the linker # This may be an issue with libsecp256k1.pc, i.e. wrong slash used that triggers the slash conversion target_link_libraries(${CFFI_OUTPUT_LIBRARY} PRIVATE ${VENDORED_AS_SYSTEM_LIB_LIBRARIES}) string(REPLACE "/libpath:" "" VENDORED_AS_SYSTEM_LIB_LIBRARY_DIRS ${VENDORED_AS_SYSTEM_LIB_LIBRARY_DIRS}) string(REPLACE "/LIBPATH:" "" VENDORED_AS_SYSTEM_LIB_LIBRARY_DIRS ${VENDORED_AS_SYSTEM_LIB_LIBRARY_DIRS}) target_link_directories(${CFFI_OUTPUT_LIBRARY} PRIVATE ${VENDORED_AS_SYSTEM_LIB_LIBRARY_DIRS}) else() message(FATAL_ERROR "Vendored library not found.") endif() ``` -------------------------------- ### Configure CFFI Shared Library Source: https://github.com/ofek/coincurve/blob/master/cm_python_module/CMakeLists.txt Defines the shared library creation process using Python_add_library, adjusting for Windows versus other platforms. ```cmake if (CMAKE_SYSTEM_NAME STREQUAL "Windows") Python_add_library(${CFFI_OUTPUT_LIBRARY} MODULE USE_SABI 3.8 "${CFFI_C_CODE_DIR}/${CFFI_C_CODE}") else() set(Python_SOABI ${SKBUILD_SOABI}) Python_add_library(${CFFI_OUTPUT_LIBRARY} MODULE WITH_SOABI "${CFFI_C_CODE_DIR}/${CFFI_C_CODE}") target_compile_definitions(${CFFI_OUTPUT_LIBRARY} PRIVATE Py_LIMITED_API) endif() set_source_files_properties("${CFFI_C_CODE_DIR}/${CFFI_C_CODE}" PROPERTIES GENERATED 1) ``` -------------------------------- ### Format PublicKeyXOnly to Bytes Source: https://context7.com/ofek/coincurve/llms.txt Serializes the BIP340 x-only public key to its 32-byte representation. This format is used for Schnorr signatures and Bitcoin Taproot. ```python from coincurve import PrivateKey, PublicKeyXOnly # Get x-only public key from private key private_key = PrivateKey() xonly_pubkey = private_key.public_key_xonly # Serialize to 32 bytes serialized = xonly_pubkey.format() print(f"X-only pubkey: {serialized.hex()}") print(f"Length: {len(serialized)} bytes") ``` -------------------------------- ### Sign with Schnorr signatures using PrivateKey.sign_schnorr Source: https://context7.com/ofek/coincurve/llms.txt Creates BIP340 Schnorr signatures. The input message must be exactly 32 bytes, typically a hash. ```python from coincurve import PrivateKey import os private_key = PrivateKey() # Message must be exactly 32 bytes (typically a hash) from hashlib import sha256 message_hash = sha256(b"Data to sign").digest() ``` -------------------------------- ### Sign with Schnorr signatures Source: https://context7.com/ofek/coincurve/llms.txt Demonstrates signing messages with Schnorr signatures using auto-generated, custom, or no auxiliary randomness. Verification is performed using the x-only public key. ```python # Sign with auto-generated auxiliary randomness schnorr_sig = private_key.sign_schnorr(message_hash) print(f"Schnorr signature length: {len(schnorr_sig)} bytes") # Always 64 bytes # Sign with custom auxiliary randomness aux_rand = os.urandom(32) schnorr_sig = private_key.sign_schnorr(message_hash, aux_randomness=aux_rand) # Sign without auxiliary randomness (deterministic) schnorr_sig_det = private_key.sign_schnorr(message_hash, aux_randomness=None) # Verify using x-only public key is_valid = private_key.public_key_xonly.verify(schnorr_sig, message_hash) print(f"Schnorr signature valid: {is_valid}") # Output: Schnorr signature valid: True ``` -------------------------------- ### Manage public keys Source: https://context7.com/ofek/coincurve/llms.txt Parses public keys from compressed or uncompressed byte formats and derives them from secret bytes. ```python from coincurve import PublicKey, PrivateKey # Create from private key private_key = PrivateKey() public_key = private_key.public_key # Parse from bytes (compressed format - 33 bytes, starts with 0x02 or 0x03) compressed = public_key.format(compressed=True) parsed_compressed = PublicKey(compressed) # Parse from bytes (uncompressed format - 65 bytes, starts with 0x04) uncompressed = public_key.format(compressed=False) parsed_uncompressed = PublicKey(uncompressed) # Both parse to the same key assert parsed_compressed == parsed_uncompressed # Derive public key directly from secret bytes public_key = PublicKey.from_secret(private_key.secret) print(f"Public key (compressed): {public_key.format().hex()}") ``` -------------------------------- ### Create Public Key from Point Coordinates Source: https://context7.com/ofek/coincurve/llms.txt Constructs a PublicKey object from explicit (x, y) coordinates on the secp256k1 curve. This allows recreating a public key if its coordinates are known. ```python from coincurve import PublicKey, PrivateKey # Get coordinates from an existing key original_key = PrivateKey().public_key x, y = original_key.point() # Recreate public key from coordinates reconstructed = PublicKey.from_point(x, y) # They should be equal assert reconstructed == original_key print(f"X: {x}") print(f"Y: {y}") print(f"Reconstructed key: {reconstructed.format().hex()}") ``` -------------------------------- ### PublicKey.format and PublicKey.point Source: https://context7.com/ofek/coincurve/llms.txt Methods to serialize public keys into compressed or uncompressed formats and extract coordinate points. ```APIDOC ## PublicKey.format and PublicKey.point ### Description Serializes the public key to bytes in compressed or uncompressed format, or extracts the (x, y) coordinate point. ### Parameters #### Method Parameters - **compressed** (bool) - Optional - If True, returns 33-byte compressed format; if False, returns 65-byte uncompressed format. ``` -------------------------------- ### PrivateKey Source: https://context7.com/ofek/coincurve/llms.txt Class for managing elliptic curve private keys. ```APIDOC ## PrivateKey ### Description Represents an elliptic curve private key used for signing and deriving shared secrets. Automatically generates a random key if no secret is provided. ### Parameters - **secret** (bytes) - Optional - 32-byte secret to initialize the key. ``` -------------------------------- ### PrivateKey.sign_recoverable Source: https://context7.com/ofek/coincurve/llms.txt Creates a recoverable ECDSA signature. ```APIDOC ## PrivateKey.sign_recoverable ### Description Creates a 65-byte recoverable ECDSA signature that allows the public key to be recovered from the signature and message. ``` -------------------------------- ### PublicKeyXOnly.tweak_add Source: https://context7.com/ofek/coincurve/llms.txt Adds a scalar to the x-only public key, modifying it in place. Used in BIP341 Taproot key derivation. ```APIDOC ## PublicKeyXOnly.tweak_add ### Description Adds a scalar to the x-only public key, modifying it in place. Used in BIP341 Taproot key derivation. ### Method `tweak_add(tweak: bytes)` ### Parameters #### Path Parameters None #### Query Parameters None #### Request Body None ### Request Example ```python from coincurve import PublicKeyXOnly # BIP341 test vector pubkey = PublicKeyXOnly( bytes.fromhex("d6889cb081036e0faefa3a35157ad71086b123b2b144b649798b494c300a961d") ) tweak = bytes.fromhex("b86e7be8f39bab32a6f2c0443abbc210f0edac0e2c53d501b36b64437d9c6c70") # Apply tweak (modifies in place) pubkey.tweak_add(tweak) # Verify result matches expected output expected = bytes.fromhex("53a1f6e454df1aa2776a2814a721372d6258050de330b3c6d10ee8f4e0dda343") assert pubkey.format() == expected print(f"Tweaked pubkey: {pubkey.format().hex()}") print(f"Parity: {pubkey.parity}") # Parity bit for Taproot ``` ### Response #### Success Response (200) None (modifies the PublicKeyXOnly object in place) #### Response Example None ``` -------------------------------- ### Verify Python Module Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Includes and runs a CMake module to verify the presence and usability of the 'cffi' Python module with the specified Python executable. ```cmake include(VerifyPythonModule) VerifyPythonModule(cffi ${Python_EXECUTABLE}) ``` -------------------------------- ### PublicKey.combine and PublicKey.combine_keys Source: https://context7.com/ofek/coincurve/llms.txt Adds multiple public keys together using elliptic curve point addition for multi-signature schemes. ```APIDOC ## PublicKey.combine and PublicKey.combine_keys ### Description Adds multiple public keys together using elliptic curve point addition. Useful for multi-signature schemes and key aggregation. ### Parameters #### Method Parameters - **keys** (list) - Required - A list of PublicKey objects to combine. - **update** (bool) - Optional - If True, updates the instance in-place. ``` -------------------------------- ### Combine Multiple Public Keys Source: https://context7.com/ofek/coincurve/llms.txt Adds multiple public keys together using elliptic curve point addition. This is useful for multi-signature schemes and key aggregation. The class method `combine_keys` takes a list of keys, while the instance method `combine` adds other keys to the instance key. ```python from coincurve import PrivateKey, PublicKey # Create multiple key pairs key1 = PrivateKey() key2 = PrivateKey() key3 = PrivateKey() # Combine using class method combined = PublicKey.combine_keys([key1.public_key, key2.public_key, key3.public_key]) print(f"Combined public key: {combined.format().hex()}") # Combine using instance method (adds other keys to self) combined2 = key1.public_key.combine([key2.public_key, key3.public_key]) assert combined == combined2 # Update in-place key1.public_key.combine([key2.public_key], update=True) print("Keys combined successfully!") ``` -------------------------------- ### Tweak X-Only Public Keys for Taproot Source: https://context7.com/ofek/coincurve/llms.txt Applies a scalar tweak to an x-only public key in place, as required for BIP341 Taproot key derivation. ```python from coincurve import PublicKeyXOnly # BIP341 test vector pubkey = PublicKeyXOnly( bytes.fromhex("d6889cb081036e0faefa3a35157ad71086b123b2b144b649798b494c300a961d") ) tweak = bytes.fromhex("b86e7be8f39bab32a6f2c0443abbc210f0edac0e2c53d501b36b64437d9c6c70") # Apply tweak (modifies in place) pubkey.tweak_add(tweak) # Verify result matches expected output expected = bytes.fromhex("53a1f6e454df1aa2776a2814a721372d6258050de330b3c6d10ee8f4e0dda343") assert pubkey.format() == expected print(f"Tweaked pubkey: {pubkey.format().hex()}") print(f"Parity: {pubkey.parity}") # Parity bit for Taproot ``` -------------------------------- ### Set CFFI C Code Variables Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Initializes variables for the CFFI C code filename and directory if they are not already defined. This standardizes paths for CFFI integration. ```cmake if (NOT CFFI_C_CODE) set(CFFI_C_CODE _cffi_c_code.c) endif() ``` ```cmake if (NOT CFFI_C_CODE_DIR) set(CFFI_C_CODE_DIR "${PROJECT_BINARY_DIR}/_gen_c_file") endif() ``` -------------------------------- ### PrivateKey.add / PrivateKey.multiply Source: https://context7.com/ofek/coincurve/llms.txt Performs scalar arithmetic on private keys. ```APIDOC ## PrivateKey.add / PrivateKey.multiply ### Description Performs scalar addition or multiplication on the private key, useful for HD wallet derivation. ### Parameters #### Request Body - **scalar** (bytes) - Required - The scalar value to add or multiply. - **update** (bool) - Optional - If True, performs the operation in-place. ``` -------------------------------- ### PublicKey.verify Source: https://context7.com/ofek/coincurve/llms.txt Verifies an ECDSA signature against a message. ```APIDOC ## PublicKey.verify ### Description Verifies an ECDSA signature against a message. Returns True if valid, False otherwise. ### Parameters #### Request Body - **signature** (bytes) - Required - The ECDSA signature to verify. - **message** (bytes) - Required - The original message. ``` -------------------------------- ### Verify Signature with Pre-hashed Message Source: https://context7.com/ofek/coincurve/llms.txt Verifies a signature against a pre-hashed message using the public key. The hasher should be set to None when providing a pre-hashed message. ```python from hashlib import sha256 msg_hash = sha256(message).digest() signature_prehash = private_key.sign(msg_hash, hasher=None) is_valid = public_key.verify(signature_prehash, msg_hash, hasher=None) print(f"Pre-hashed valid: {is_valid}") ``` -------------------------------- ### Process Headers for CFFI Generation Source: https://github.com/ofek/coincurve/blob/master/cm_library_cffi_headers/CMakeLists.txt Iterates through found source headers, generates CFFI headers using the defined macro, and adds dependencies. This ensures that the CFFI headers are built when needed. ```cmake add_custom_target(headers-for-cffi) foreach(src_header ${src_headers}) get_filename_component(cffi_header ${src_header} NAME) get_filename_component(src_header_dir ${src_header} DIRECTORY) generate_cffi_header(${src_header_dir} ${cffi_header} ${CFFI_HEADERS_DIR}) add_dependencies(headers-for-cffi ${cffi_header}) endforeach() ``` -------------------------------- ### Verify Schnorr Signature with PublicKeyXOnly Source: https://context7.com/ofek/coincurve/llms.txt Verifies a BIP340 Schnorr signature using the x-only public key. This method is used for verifying signatures in systems that employ Schnorr signatures, such as Bitcoin Taproot. ```python from coincurve import PrivateKey from hashlib import sha256 private_key = PrivateKey() xonly_pubkey = private_key.public_key_xonly # Create a 32-byte message hash message_hash = sha256(b"BIP340 message").digest() # Sign with Schnorr signature = private_key.sign_schnorr(message_hash) ``` -------------------------------- ### Copy Build Script Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Copies the Python build script (build.py) to the designated CFFI C code directory. This script is essential for generating C source files. ```cmake file(COPY ${CMAKE_CURRENT_LIST_DIR}/build.py DESTINATION ${CFFI_C_CODE_DIR}) ``` -------------------------------- ### PrivateKey.sign Source: https://context7.com/ofek/coincurve/llms.txt Creates a DER-encoded ECDSA signature. ```APIDOC ## PrivateKey.sign ### Description Creates a DER-encoded ECDSA signature for a message. Uses SHA-256 hashing by default. ### Parameters - **message** (bytes) - Required - The message to sign. - **hasher** (callable) - Optional - The hashing function to use (default: SHA-256). Pass None if the message is already hashed. ``` -------------------------------- ### Serialize and deserialize private keys Source: https://context7.com/ofek/coincurve/llms.txt Converts private keys between hex, integer, PEM, and DER formats. These methods ensure interoperability across different cryptographic systems. ```python from coincurve import PrivateKey # Create a key private_key = PrivateKey() # Export to different formats hex_key = private_key.to_hex() int_key = private_key.to_int() pem_key = private_key.to_pem() der_key = private_key.to_der() print(f"Hex: {hex_key}") print(f"Integer: {int_key}") print(f"PEM:\n{pem_key.decode()}") print(f"DER length: {len(der_key)} bytes") # Import from different formats key_from_hex = PrivateKey.from_hex(hex_key) key_from_int = PrivateKey.from_int(int_key) key_from_pem = PrivateKey.from_pem(pem_key) key_from_der = PrivateKey.from_der(der_key) # All recreated keys are equal assert key_from_hex == key_from_int == key_from_pem == key_from_der == private_key print("All serialization round-trips successful!") ``` -------------------------------- ### PublicKey.add and PublicKey.multiply Source: https://context7.com/ofek/coincurve/llms.txt Performs scalar operations on public keys for key derivation and cryptographic protocols. ```APIDOC ## PublicKey.add and PublicKey.multiply ### Description Performs scalar operations on public keys for key derivation and cryptographic protocols. ### Parameters #### Method Parameters - **tweak** (bytes) - Required - A 32-byte scalar value. - **update** (bool) - Optional - If True, updates the instance in-place. ``` -------------------------------- ### PrivateKey Object Source: https://github.com/ofek/coincurve/blob/master/docs/api.md Manages private keys for cryptographic operations. ```APIDOC ## coincurve.PrivateKey ### Description Represents and manages private keys for cryptographic operations. ### Methods - `__init__`: Initializes a PrivateKey object. - `sign`: Signs a message with the private key. - `sign_recoverable`: Signs a message and returns recovery information. - `sign_schnorr`: Signs a message using the Schnorr signature scheme. - `ecdh`: Performs Elliptic Curve Diffie-Hellman key exchange. - `add`: Adds another PrivateKey to this one. - `multiply`: Multiplies the private key scalar by another scalar. - `to_int`: Converts the private key to an integer. - `to_hex`: Converts the private key to a hexadecimal string. - `to_pem`: Exports the private key in PEM format. - `to_der`: Exports the private key in DER format. - `from_int`: Creates a PrivateKey from an integer. - `from_hex`: Creates a PrivateKey from a hexadecimal string. - `from_pem`: Creates a PrivateKey from PEM format. - `from_der`: Creates a PrivateKey from DER format. ### Parameters (Specific parameters for each method are not detailed in the provided text.) ### Request Example (Not applicable) ### Response (Not specified, depends on the method called.) ``` -------------------------------- ### Generate CFFI Source File Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Configures a custom CMake command to generate the CFFI C-source file using a Python script. This command depends on the build script and headers, and its output is the generated C file. ```cmake add_custom_command( OUTPUT ${CFFI_C_CODE_DIR}/${CFFI_C_CODE} COMMAND ${Python_EXECUTABLE} ${CFFI_C_CODE_DIR}/build.py ${CFFI_HEADERS_DIR} ${CFFI_C_CODE_DIR}/${CFFI_C_CODE} ${_static_build} MAIN_DEPENDENCY ${CMAKE_CURRENT_LIST_DIR}/build.py DEPENDS headers-for-cffi WORKING_DIRECTORY ${CFFI_C_CODE_DIR} COMMENT "Generating CFFI source file" ) ``` -------------------------------- ### PublicKey Object Source: https://github.com/ofek/coincurve/blob/master/docs/api.md Manages public keys for cryptographic operations. ```APIDOC ## coincurve.PublicKey ### Description Represents and manages public keys for cryptographic operations. ### Methods - `__init__`: Initializes a PublicKey object. - `verify`: Verifies a signature against a message. - `format`: Formats the public key into a specific format (e.g., compressed, uncompressed). - `point`: Returns the public key point on the curve. - `combine`: Combines multiple public keys. - `add`: Adds another PublicKey to this one. - `multiply`: Multiplies the public key point by a scalar. - `combine_keys`: Combines multiple public keys. - `from_signature_and_message`: Derives a PublicKey from a signature and message. - `from_secret`: Creates a PublicKey from a private key secret. - `from_valid_secret`: Creates a PublicKey from a private key secret, validating it. - `from_point`: Creates a PublicKey from a point on the curve. ### Parameters (Specific parameters for each method are not detailed in the provided text.) ### Request Example (Not applicable) ### Response (Not specified, depends on the method called.) ``` -------------------------------- ### Find Source Headers with CMake Source: https://github.com/ofek/coincurve/blob/master/cm_library_cffi_headers/CMakeLists.txt Locates source header files using CMake's file globbing. It supports finding headers from a specified directory or issuing a fatal error if headers cannot be found. ```cmake if(VENDORED_HEADERS_DIR) file(GLOB src_headers ${VENDORED_HEADERS_DIR}/*.h) elseif(VENDORED_AS_SYSTEM_LIB_FOUND) message(WARNING "Using system library ${VENDORED_LIBRARY_CMAKE_TARGET}. The list of headers is set to:" " /*${VENDORED_LIBRARY_CMAKE_TARGET}/*.h") file(GLOB src_headers ${VENDORED_AS_SYSTEM_LIB_INCLUDE_DIRS}/*${VENDORED_LIBRARY_CMAKE_TARGET}*.h) message(STATUS " Generating CFFI header for ${src_headers}") else() message(FATAL_ERROR "Headers for CFFI cannot be found. Exiting") endif() ``` -------------------------------- ### Create recoverable signatures with PrivateKey.sign_recoverable Source: https://context7.com/ofek/coincurve/llms.txt Generates signatures that allow public key recovery from the signature and message. This is commonly used for Ethereum transaction verification. ```python from coincurve import PrivateKey, PublicKey private_key = PrivateKey() message = b"Ethereum transaction payload" # Create recoverable signature recoverable_sig = private_key.sign_recoverable(message) print(f"Recoverable signature length: {len(recoverable_sig)} bytes") # 65 bytes # Recover the public key from signature and message recovered_pubkey = PublicKey.from_signature_and_message(recoverable_sig, message) # Verify recovery worked correctly assert recovered_pubkey == private_key.public_key print("Public key successfully recovered from signature!") ``` -------------------------------- ### Perform scalar key arithmetic Source: https://context7.com/ofek/coincurve/llms.txt Performs scalar addition or multiplication on private keys. Use the update=True parameter for in-place modification. ```python from coincurve import PrivateKey # Start with a simple key for demonstration private_key = PrivateKey(b"\x01".rjust(32, b"\x00")) # Key = 1 print(f"Initial key value: {private_key.to_int()}") # Output: 1 # Add a scalar (returns new key by default) new_key = private_key.add(b"\x09") print(f"After adding 9: {new_key.to_int()}") # Output: 10 # Multiply by a scalar key_5 = PrivateKey(b"\x05".rjust(32, b"\x00")) multiplied = key_5.multiply(b"\x05") print(f"5 * 5 = {multiplied.to_int()}") # Output: 25 # Update in-place instead of creating new key key = PrivateKey(b"\x01".rjust(32, b"\x00")) same_key = key.add(b"\x09", update=True) assert key is same_key print(f"In-place updated: {key.to_int()}") # Output: 10 ``` -------------------------------- ### Signature Verification Source: https://github.com/ofek/coincurve/blob/master/docs/api.md Provides functionality to verify cryptographic signatures. ```APIDOC ## coincurve.verify_signature ### Description Verifies a cryptographic signature against a message and public key. ### Method (Not specified, assumed to be a function call) ### Endpoint (Not applicable, this is a library function) ### Parameters (Parameters not explicitly defined in the provided text, but typically include signature, message, and public key) ### Request Example (Not applicable) ### Response (Not specified, typically a boolean indicating verification success or failure) ``` -------------------------------- ### PublicKey.from_signature_and_message Source: https://context7.com/ofek/coincurve/llms.txt Recovers a public key from a recoverable ECDSA signature and the original message. ```APIDOC ## PublicKey.from_signature_and_message ### Description Recovers the public key from a recoverable ECDSA signature and the original message. Essential for Ethereum-style signature verification. ### Parameters #### Method Parameters - **recoverable_sig** (bytes) - Required - The recoverable signature. - **message** (bytes) - Required - The original message that was signed. ``` -------------------------------- ### Recover Public Key from Signature and Message Source: https://context7.com/ofek/coincurve/llms.txt Recovers the public key that was used to sign a message. This is essential for verifying signatures, particularly in Ethereum-style transactions. ```python from coincurve import PrivateKey, PublicKey # Create signature private_key = PrivateKey() message = b"Ethereum signed message" recoverable_sig = private_key.sign_recoverable(message) # Recover public key from signature recovered = PublicKey.from_signature_and_message(recoverable_sig, message) # Verify it matches the original assert recovered.format() == private_key.public_key.format() print("Public key recovered successfully!") print(f"Recovered public key: {recovered.format().hex()}") ``` -------------------------------- ### Verify ECDSA signatures with verify_signature Source: https://context7.com/ofek/coincurve/llms.txt Use this standalone function to verify DER-encoded signatures without needing a PublicKey object. It supports both compressed and uncompressed public key formats. ```python from coincurve import verify_signature, PrivateKey # Create a key pair and sign a message private_key = PrivateKey() message = b"Hello, Bitcoin!" signature = private_key.sign(message) # Verify the signature using the standalone function public_key_bytes = private_key.public_key.format(compressed=True) is_valid = verify_signature(signature, message, public_key_bytes) print(f"Signature valid: {is_valid}") # Output: Signature valid: True # Verify with uncompressed public key format public_key_uncompressed = private_key.public_key.format(compressed=False) is_valid = verify_signature(signature, message, public_key_uncompressed) print(f"Signature valid (uncompressed): {is_valid}") # Output: Signature valid (uncompressed): True # Verification fails with wrong message wrong_message = b"Wrong message" is_valid = verify_signature(signature, wrong_message, public_key_bytes) print(f"Wrong message: {is_valid}") # Output: Wrong message: False ``` -------------------------------- ### Perform Scalar Operations on Public Keys Source: https://context7.com/ofek/coincurve/llms.txt Performs scalar addition (point addition) and scalar multiplication on public keys. These operations are fundamental for key derivation and various cryptographic protocols. The `update=True` argument modifies the key in-place. ```python from coincurve import PrivateKey import os public_key = PrivateKey().public_key tweak = os.urandom(32) # Add a scalar to the public key (EC point addition with scalar * G) tweaked_add = public_key.add(tweak) print(f"Tweaked (add): {tweaked_add.format().hex()}") # Multiply public key by a scalar (EC point multiplication) tweaked_mul = public_key.multiply(tweak) print(f"Tweaked (mul): {tweaked_mul.format().hex()}") # In-place update original = PrivateKey().public_key original.add(tweak, update=True) print("Public key tweaked in-place!") ``` -------------------------------- ### Generate CFFI Header Macro Source: https://github.com/ofek/coincurve/blob/master/cm_library_cffi_headers/CMakeLists.txt Defines a CMake macro to generate CFFI headers. This macro sets up a custom command that executes a Python script to compose the headers, specifying dependencies and working directory. ```cmake macro(generate_cffi_header src_header cffi_header cffi_dir) add_custom_command( OUTPUT ${cffi_dir}/${cffi_header} COMMAND ${Python_EXECUTABLE} ${CMAKE_CURRENT_LIST_DIR}/compose_cffi_headers.py ${src_header} ${cffi_header} ${cffi_dir} MAIN_DEPENDENCY ${CMAKE_CURRENT_LIST_DIR}/compose_cffi_headers.py DEPENDS ${src_header} WORKING_DIRECTORY ${cffi_dir} ) add_custom_target(${cffi_header} ALL DEPENDS ${cffi_dir}/${cffi_header}) endmacro() ``` -------------------------------- ### Determine Build Type Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Sets a variable to indicate whether a static build is being performed based on the presence of VENDORED_HEADERS_DIR. This influences how the CFFI C-file is generated. ```cmake if (VENDORED_HEADERS_DIR) set(_static_build 'ON') else() message(STATUS "CFFI C-file is built for a SHARED system library") set(_static_build 'OFF') endif() ``` -------------------------------- ### PrivateKey.sign_schnorr Source: https://context7.com/ofek/coincurve/llms.txt Signs a message hash using the Schnorr signature scheme with optional auxiliary randomness. ```APIDOC ## PrivateKey.sign_schnorr ### Description Signs a message hash using the Schnorr signature scheme. Supports auto-generated, custom, or deterministic (None) auxiliary randomness. ### Parameters #### Request Body - **message_hash** (bytes) - Required - The hash of the message to sign. - **aux_randomness** (bytes/None) - Optional - 32 bytes of randomness for the signature, or None for deterministic signing. ``` -------------------------------- ### Sign messages with PrivateKey.sign Source: https://context7.com/ofek/coincurve/llms.txt Generates DER-encoded ECDSA signatures. By default, it hashes the message with SHA-256, but can accept pre-hashed messages by setting hasher=None. ```python from coincurve import PrivateKey, verify_signature private_key = PrivateKey() message = b"Transaction data to sign" # Sign with default SHA-256 hashing signature = private_key.sign(message) print(f"DER signature length: {len(signature)} bytes") # Verify the signature is_valid = verify_signature(signature, message, private_key.public_key.format()) print(f"Valid: {is_valid}") # Output: Valid: True # Sign a pre-hashed message (pass hasher=None) from hashlib import sha256 msg_hash = sha256(message).digest() signature_prehashed = private_key.sign(msg_hash, hasher=None) ``` -------------------------------- ### PrivateKey.ecdh Source: https://context7.com/ofek/coincurve/llms.txt Computes an EC Diffie-Hellman shared secret. ```APIDOC ## PrivateKey.ecdh ### Description Computes an EC Diffie-Hellman shared secret in constant time. Returns the SHA-256 hash of the compressed shared point. ### Parameters #### Request Body - **public_key** (bytes) - Required - The compressed public key of the other party. ``` -------------------------------- ### verify_signature Source: https://context7.com/ofek/coincurve/llms.txt Verifies an ECDSA signature without requiring a PublicKey object. ```APIDOC ## verify_signature ### Description A standalone function to verify ECDSA signatures. It parses the public key, hashes the message using SHA-256 by default, and verifies the DER-encoded signature. ### Parameters - **signature** (bytes) - Required - The DER-encoded signature. - **message** (bytes) - Required - The message to verify. - **public_key** (bytes) - Required - The public key bytes (compressed or uncompressed). ### Response - **is_valid** (bool) - Returns True if the signature is valid, False otherwise. ``` -------------------------------- ### Add CFFI C-Binding Target Source: https://github.com/ofek/coincurve/blob/master/cm_library_c_binding/CMakeLists.txt Defines a custom CMake target 'cffi-c-binding' that depends on the generated CFFI C-source file. It also ensures this target depends on 'headers-for-cffi'. ```cmake add_custom_target(cffi-c-binding ALL DEPENDS ${CFFI_C_CODE_DIR}/${CFFI_C_CODE}) add_dependencies(cffi-c-binding headers-for-cffi) ``` -------------------------------- ### Verify ECDSA signatures Source: https://context7.com/ofek/coincurve/llms.txt Verifies an ECDSA signature against a message. Returns a boolean indicating validity. ```python from coincurve import PrivateKey, PublicKey private_key = PrivateKey() public_key = private_key.public_key message = b"Message to verify" # Create signature signature = private_key.sign(message) # Verify signature is_valid = public_key.verify(signature, message) print(f"Signature valid: {is_valid}") # Output: Signature valid: True # Tampered message fails verification tampered_message = b"Tampered message" is_valid = public_key.verify(signature, tampered_message) print(f"Tampered message valid: {is_valid}") # Output: Tampered message valid: False ``` -------------------------------- ### Compute ECDH shared secret Source: https://context7.com/ofek/coincurve/llms.txt Computes a shared secret in constant time using the Diffie-Hellman protocol. The result is a SHA-256 hash of the compressed shared point. ```python from coincurve import PrivateKey # Alice and Bob each generate their key pairs alice_private = PrivateKey() bob_private = PrivateKey() # Alice computes shared secret using Bob's public key alice_shared = alice_private.ecdh(bob_private.public_key.format()) # Bob computes shared secret using Alice's public key bob_shared = bob_private.ecdh(alice_private.public_key.format()) # Both arrive at the same 32-byte shared secret assert alice_shared == bob_shared print(f"Shared secret: {alice_shared.hex()}") print(f"Shared secret length: {len(alice_shared)} bytes") # 32 bytes ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.