py-ecc

8.0.0 · active · verified Sun Apr 12

py-ecc is a pure-Python library for elliptic curve cryptography, offering implementations for curves like secp256k1 (used in Bitcoin and Ethereum), alt_bn128, and bls12_381. Currently at version 8.0.0, the library is actively maintained with releases tied to evolving cryptographic standards, especially for BLS.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a secp256k1 private key, derive its public key, sign a message hash, and then verify the signature by recovering the public key.

import os
from py_ecc.secp256k1 import privtopub, ecsign, ecrecover, N

# 1. Generate a private key
# In a real application, use a cryptographically secure random source (e.g., from a KDF or secure RNG)
privkey = int(os.urandom(32).hex(), 16) % N
print(f"Private Key: {hex(privkey)}")

# 2. Derive the public key
pubkey = privtopub(privkey)
print(f"Public Key (uncompressed, X, Y tuple): {pubkey}")

# 3. Create a message hash (must be 32 bytes)
msg_hash = int.from_bytes(os.urandom(32), 'big')
print(f"Message Hash: {hex(msg_hash)}")

# 4. Sign the message
v, r, s = ecsign(msg_hash, privkey)
print(f"Signature: v={v}, r={hex(r)}, s={hex(s)}")

# 5. Recover the public key from the signature
recovered_pubkey = ecrecover(msg_hash, v, r, s)
print(f"Recovered Public Key: {recovered_pubkey}")

# 6. Verify the signature
assert recovered_pubkey == pubkey, "Signature verification failed!"
print("Signature verification successful!")

view raw JSON →