or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

bls-signatures.mdbls12-381.mdbn128.mdfields.mdindex.mdoptimized.mdsecp256k1.md
tile.json

tessl/pypi-py-ecc

Elliptic curve crypto in python including secp256k1, alt_bn128, and bls12_381

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
pypipkg:pypi/py-ecc@8.0.x

To install, run

npx @tessl/cli install tessl/pypi-py-ecc@8.0.0

index.mddocs/

py-ecc

A comprehensive elliptic curve cryptography library for Python that implements multiple elliptic curve algorithms including secp256k1 (used in Bitcoin), alt_bn128/bn128 (used in Ethereum), and bls12_381 (used in Ethereum 2.0). The library provides both reference implementations and optimized versions of cryptographic operations, making it essential for blockchain and cryptocurrency applications.

Package Information

  • Package Name: py-ecc
  • Language: Python
  • Installation: pip install py_ecc

Core Imports

import py_ecc

Common imports for specific functionality:

# BLS signatures
from py_ecc.bls import G2Basic, G2MessageAugmentation, G2ProofOfPossession

# secp256k1 operations
from py_ecc.secp256k1 import privtopub, ecdsa_raw_sign, ecdsa_raw_recover

# BLS12-381 curve operations
from py_ecc.bls12_381 import G1, G2, multiply, pairing

# Optimized implementations
from py_ecc.optimized_bls12_381 import pairing as fast_pairing

Basic Usage

import py_ecc
from py_ecc.bls import G2Basic
from py_ecc.secp256k1 import privtopub, ecdsa_raw_sign

# BLS signature example
private_key = 42
message = b"Hello, world!"

# Generate public key and sign message
public_key = G2Basic.SkToPk(private_key)
signature = G2Basic.Sign(private_key, message)

# Verify signature
is_valid = G2Basic.Verify(public_key, message, signature)
print(f"Signature valid: {is_valid}")

# secp256k1 example
secp_privkey = b'\x79\x2e\xca\x68\x2b\x89\x0b\x31\x35\x62\x47\xf2\xb0\x46\x62\xbf\xf4\x48\xb6\xbb\x19\xea\x1c\x8a\xb4\x8d\xa2\x22\xc8\x94\xef\x9b'
secp_pubkey = privtopub(secp_privkey)
v, r, s = ecdsa_raw_sign(b'\x35' * 32, secp_privkey)

Architecture

py-ecc is organized around different elliptic curves and their implementations:

  • Curve Modules: Each curve (secp256k1, bn128, bls12_381) has its own module with curve-specific operations
  • Field Arithmetic: Underlying finite field operations supporting all curves
  • BLS Signatures: High-level BLS signature schemes built on top of BLS12-381
  • Optimization Layers: Both reference and optimized implementations for performance-critical operations

The library provides both educational reference implementations and production-ready optimized versions, allowing developers to choose between clarity and performance based on their needs.

Capabilities

BLS Signature Schemes

High-level BLS signature schemes according to IETF standards, providing digital signatures with aggregation capabilities for blockchain applications.

class G2Basic:
    @staticmethod
    def SkToPk(sk: int) -> bytes: ...
    @staticmethod
    def KeyValidate(pk: bytes) -> bool: ...
    @staticmethod
    def Sign(sk: int, message: bytes) -> bytes: ...
    @staticmethod
    def Verify(pk: bytes, message: bytes, signature: bytes) -> bool: ...
    @staticmethod
    def Aggregate(signatures: Sequence[bytes]) -> bytes: ...
    @staticmethod
    def AggregateVerify(pks: Sequence[bytes], messages: Sequence[bytes], signature: bytes) -> bool: ...
    @staticmethod
    def FastAggregateVerify(pks: Sequence[bytes], message: bytes, signature: bytes) -> bool: ...

BLS Signatures

secp256k1 Operations

Bitcoin's elliptic curve operations including key generation, ECDSA signing, and signature recovery.

def privtopub(privkey: bytes) -> Tuple[int, int]: ...
def ecdsa_raw_sign(msghash: bytes, priv: bytes) -> Tuple[int, int, int]: ...
def ecdsa_raw_recover(msghash: bytes, vrs: Tuple[int, int, int]) -> Tuple[int, int]: ...
def multiply(a: Tuple[int, int], n: int) -> Tuple[int, int]: ...
def add(a: Tuple[int, int], b: Tuple[int, int]) -> Tuple[int, int]: ...

secp256k1

BLS12-381 Curve Operations

Reference implementation of BLS12-381 elliptic curve operations including point arithmetic and bilinear pairings.

def multiply(p, n): ...
def add(p1, p2): ...
def pairing(p1, p2): ...
def is_on_curve(p) -> bool: ...
def final_exponentiate(p): ...

BLS12-381 Curve

BN128 Curve Operations

Reference implementation of BN128 (alt_bn128) elliptic curve operations used in Ethereum's precompiled contracts.

def multiply(p, n): ...
def add(p1, p2): ...
def pairing(p1, p2): ...
def is_on_curve(p) -> bool: ...
def final_exponentiate(p): ...

BN128 Curve

Optimized Implementations

High-performance versions of BLS12-381 and BN128 operations with additional specialized functions for production use.

# Optimized BLS12-381
def pairing(p1, p2): ...
def multiply_clear_cofactor_G1(p): ...
def multiply_clear_cofactor_G2(p): ...
def optimized_swu_G1(t): ...
def optimized_swu_G2(t): ...

Optimized Operations

Field Arithmetic

Finite field arithmetic supporting all implemented curves with both basic and extension field operations.

class FQ:
    def __init__(self, val: int): ...
    def __add__(self, other): ...
    def __mul__(self, other): ...
    def __pow__(self, other): ...

class FQ2:
    def __init__(self, coeffs: Sequence): ...
    
class FQ12:
    def __init__(self, coeffs: Sequence): ...

Field Arithmetic

Types

from typing import Tuple, Optional, Sequence

# Point representations
PlainPoint2D = Tuple[int, int]
PlainPoint3D = Tuple[int, int, int]

# Generic field element types
Point2D = Optional[Tuple[Field, Field]]
Point3D = Optional[Tuple[Field, Field, Field]]

# BLS types (from eth-typing)
BLSPubkey = bytes
BLSSignature = bytes