Skip to content
pawelmarel1Public

About

⚡ fastec — szybka biblioteka secp256k1 dla Pythona (C++): pubkey, hash160, adresy, wielowątkowe przeszukiwanie. Miliony kluczy/s. Fast secp256k1 for Python.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Latest commit

 

History

6 Commits

Folders and files

Repository files navigation

⚡ fastec

Szybka arytmetyka secp256k1 dla Pythona — prędkość C++, wygoda Pythona. Fast secp256k1 arithmetic for Python — C++ speed, Python convenience.

platforma arch python C++ speed license

Autor / Author: Paweł Marel · © 2026 · LICENSE

secp256k1 · Bitcoin · elliptic curve · pubkey · hash160 · P2PKH address · vanity · multithreaded key search · Python C++ extension · x86-64 / ARM64


🇵🇱 Polski

fastec to rozszerzenie C++ dla Pythona do operacji na krzywej secp256k1 (tej samej, której używa Bitcoin). Generuje klucze publiczne, adresy i skróty hash160, oraz przeszukuje zakresy kluczy wielowątkowo — z prędkością milionów kluczy na sekundę.

Dlaczego szybko?

  • Mnożenie stałą bazą — tablica wielokrotności generatora G: mnożenie k·G to 64 dodawania zamiast 256 podwojeń.
  • Inwersja wsadowa Montgomery'ego — jedna inwersja modularna na całą partię kluczy zamiast jednej na każdy klucz (najdroższa operacja liczona raz).
  • Wielowątkowość ze zwolnionym GIL — przeszukiwanie działa równolegle na wszystkich rdzeniach.

Wydajność (zmierzona, 8 wątków)

operacja fastec czysty Python (coincurve/hashlib) przyspieszenie
search_pubkey (przeszukiwanie) ~3 600 000 / s ~18 000 / s ~200×
search_hash160 (klucz→adres) ~1 400 000 / s ~8 000 / s ~175×
pub_range (lista pubkey) ~1 160 000 / s ~13 000 / s ~87×
pojedynczy pub_from_priv ~21 000 / s ~10 500 / s ~2×

Cała arytmetyka jest zgodna bit-w-bit z libsecp256k1 (coincurve) i hashlib — zweryfikowana na setkach tysięcy losowych przypadków (patrz Weryfikacja).

Wymagania

  • python3 (3.9+), python3-venv
  • g++ z obsługą C++20 (do wersji kompilowanej ze źródeł)

Instalacja

A) Ze źródeł (Linux / macOS / Windows):

Linux / macOS: ./install.sh Windows (PowerShell): .\install.ps1

B) Gotowa binarka (Linux aarch64, CPython 3.13):

cp bin/fastec.cpython-313-aarch64-linux-gnu.so /sciezka/do/twojego/projektu/fastec.so
# potem: import fastec

Binarka jest zbudowana pod konkretną platformę i wersję Pythona. Na innych systemach użyj wersji ze źródeł (A).

Architektury / Architectures: pełne wsparcie x86-64 (Intel/AMD) oraz ARM64/AArch64. Wieloplatformowość: Linux — GCC/Clang (od razu). macOS — Apple Clang (xcode-select --install). Windows — Clang lub MinGW-w64 (nie MSVC, bo kod używa unsigned __int128).

Użycie

import fastec

# klucz prywatny -> skompresowany klucz publiczny
pub = fastec.pub_from_int(0x1a2d2eb49)          # bytes (33)
pub = fastec.pub_from_priv(priv_bytes32)         # to samo, z bytes

# klucz publiczny -> hash160 -> adres P2PKH
h160 = fastec.hash160(pub)                       # bytes (20)
addr = fastec.addr_from_h160(h160)               # str, np. "1..."

# kolejne klucze [start, start+count) -> lista pubkey / hash160
pubs   = fastec.pub_range(1, 100000)             # [bytes33, ...]
hashes = fastec.hash160_range(1, 100000)         # [bytes20, ...]

# przeszukiwanie wielowątkowe -> zwraca klucz (int) lub None
key = fastec.search_pubkey(start, count, target_pub33, nthreads=0)
key = fastec.search_hash160(start, count, target_h160_20, nthreads=0)
key = fastec.search_vanity(start, count, "1Prefix", nthreads=0)

nthreads=0 = automatycznie wszystkie rdzenie.


🇬🇧 English

fastec is a C++ Python extension for secp256k1 curve operations (the curve used by Bitcoin). It derives public keys, addresses and hash160 digests, and searches key ranges multithreaded — at millions of keys per second.

Why it's fast

  • Fixed-base multiplication — a precomputed table of generator G multiples: k·G is 64 additions instead of 256 doublings.
  • Montgomery batch inversion — one modular inversion per whole batch instead of one per key (the most expensive operation computed once).
  • Multithreading with released GIL — searches run in parallel across all cores.

Performance (measured, 8 threads)

operation fastec pure Python (coincurve/hashlib) speedup
search_pubkey (search) ~3,600,000 / s ~18,000 / s ~200×
search_hash160 (key→address) ~1,400,000 / s ~8,000 / s ~175×
pub_range (pubkey list) ~1,160,000 / s ~13,000 / s ~87×
single pub_from_priv ~21,000 / s ~10,500 / s ~2×

All arithmetic is bit-for-bit identical to libsecp256k1 (coincurve) and hashlib — verified over hundreds of thousands of random cases (see Verification).

Requirements

  • python3 (3.9+), python3-venv
  • g++ with C++20 support (for the source build)

Installation

A) From source (Linux / macOS / Windows):

Linux / macOS: ./install.sh Windows (PowerShell): .\install.ps1

Architectures: full support for x86-64 (Intel/AMD) and ARM64/AArch64. Cross-platform: Linux — GCC/Clang (out of the box). macOS — Apple Clang (xcode-select --install). Windows — Clang or MinGW-w64 (not MSVC; the code uses unsigned __int128).

B) Prebuilt binary (Linux aarch64, CPython 3.13):

cp bin/fastec.cpython-313-aarch64-linux-gnu.so /path/to/your/project/fastec.so
# then: import fastec

The binary is built for a specific platform and Python version. On other systems, use the source build (A).

Usage

import fastec

pub    = fastec.pub_from_int(0x1a2d2eb49)         # private key -> compressed pubkey (bytes33)
h160   = fastec.hash160(pub)                       # pubkey -> hash160 (bytes20)
addr   = fastec.addr_from_h160(h160)               # hash160 -> P2PKH address (str)
pubs   = fastec.pub_range(1, 100000)               # consecutive keys -> [pubkey...]
key    = fastec.search_pubkey(start, count, target_pub33, nthreads=0)   # -> int or None
key    = fastec.search_hash160(start, count, target_h160_20, nthreads=0)
key    = fastec.search_vanity(start, count, "1Prefix", nthreads=0)

nthreads=0 = use all cores automatically.


🚀 Szybki start / Quick start

PL: Komplet komend od zera — pobranie repo, zależności i instalacja. / EN: Full commands from scratch — clone, dependencies, install.

Linux (Debian / Ubuntu)

sudo apt update && sudo apt install -y git python3 python3-venv g++
git clone https://github.com/pawelmarel1/fastec.git
cd fastec
./install.sh
source venv/bin/activate
python -c "import fastec; print(fastec.addr_from_h160(fastec.hash160(fastec.pub_from_int(1))))"

macOS

xcode-select --install                 # jednorazowo / once (kompilator / compiler)
git clone https://github.com/pawelmarel1/fastec.git
cd fastec
./install.sh
source venv/bin/activate
python3 -c "import fastec; print(fastec.addr_from_h160(fastec.hash160(fastec.pub_from_int(1))))"

Windows (PowerShell)

# wymaga: Python 3.9+, Git oraz LLVM/Clang lub MinGW-w64 (nie MSVC)
# requires: Python 3.9+, Git and LLVM/Clang or MinGW-w64 (not MSVC)
winget install Git.Git Python.Python.3.12 LLVM.LLVM
git clone https://github.com/pawelmarel1/fastec.git
cd fastec
.\install.ps1
.�env\Scripts\Activate.ps1
python -c "import fastec; print(fastec.addr_from_h160(fastec.hash160(fastec.pub_from_int(1))))"

PL: Opcjonalnie, do weryfikacji zgodności zainstaluj bibliotekę referencyjną: EN: Optionally, for correctness verification install the reference library:

pip install coincurve      # potem / then:  python benchmark.py

📁 Przykłady / Examples

Katalog examples/ zawiera / The examples/ folder contains:

plik / file opis / description
example.py użycie w Pythonie / Python usage (priv → pubkey → adres, przeszukiwanie / search)
example_cpp.cpp integracja w projekcie C++ przez field.h / C++ integration via field.h
example_asm.md wołanie z asemblera przez wrapper extern "C" / calling from assembly via extern "C" wrapper
# Python:
python examples/example.py
# C++:
g++ -O3 -std=c++20 examples/example_cpp.cpp -o example_cpp && ./example_cpp

📊 Benchmarki / Benchmarks

PL: Zmierz wydajność na swoim procesorze: EN: Measure performance on your CPU:

python benchmark.py

Skrypt wypisze gotowy wiersz do poniższej tabeli. Prześlij go Pull Requestem — zbieramy wyniki z różnych procesorów! / The script prints a ready row for the table below. Send it via Pull Request — we collect results from many CPUs!

CPU rdzenie / cores search_pubkey /s search_hash160 /s pub_range /s pub_from_priv /s
Snapdragon 7s Gen 3 (Cortex-A720+A520) 8 2 034 391 914 415 677 808 9 934
Twój CPU tutaj / Your CPU here

PL: Wiersz referencyjny zmierzono na urządzeniu mobilnym ARM przy obciążonych rdzeniach — desktopowe CPU x86 (Intel/AMD) osiągają znacznie wyższe wartości. Wyniki skalują się w przybliżeniu liniowo z liczbą rdzeni i przepustowością mnożenia całkowitoliczbowego. EN: The reference row was measured on an ARM mobile device under core load — x86 desktop CPUs (Intel/AMD) reach much higher figures. Results scale roughly linearly with core count and integer-multiply throughput.

✅ Weryfikacja / Verification

Po instalacji uruchom / After install, run:

import fastec, coincurve, hashlib, os

# zgodnosc pubkey / pubkey match
k = os.urandom(32)
assert fastec.pub_from_priv(k) == coincurve.PublicKey.from_valid_secret(k).format(True)

# zgodnosc hash160 / hash160 match
pub = fastec.pub_from_int(1)
ref = hashlib.new("ripemd160", hashlib.sha256(pub).digest()).digest()
assert fastec.hash160(pub) == ref

# przeszukiwanie znajduje podlozony klucz / search finds a planted key
target = fastec.pub_from_int(1_500_000)
assert fastec.search_pubkey(1_000_000, 1_000_000, target, 8) == 1_500_000

print("fastec: OK")

🧪 Experimental GPU research

Goal (experimental): to attempt pushing single-GPU secp256k1 throughput toward 2×–3× of current implementations. State-of-the-art single-card kangaroo solvers reach roughly 8 billion keys/s on an RTX 4090; the raw arithmetic ceiling of the silicon leaves theoretical room above that, and this project explores how much of it can be reached.

⚠️ This is a genuine research experiment, not a guaranteed result. Achieving 2×–3× would mean beating expert-tuned CUDA at the practical limits of the hardware — it targets the silicon ceiling and may only partially succeed (a realistic outcome is matching, with 2× as the stretch target). No performance figure is promised.

Main levers under investigation:

  • reduced-radix field representation (e.g. 5×52-bit limbs) to shorten carry chains,
  • hand-written PTX/SASS for hardware carry handling (add.cc / addc),
  • higher occupancy through lower register pressure,
  • cache-friendly distinguished-point (DP) table layout.

Why donations

I do not personally own — and cannot afford — the high-end NVIDIA GPUs (RTX 4090 / 5090 / Tesla class) required to develop and benchmark this on real hardware. Voluntary donations go directly toward purchasing that GPU hardware and covering the research and compute costs of the experiment.

Donations remain entirely voluntary and unlock no features — the CPU library is fully free and public regardless. Fundraising progress can be tracked at the addresses below.

💝 Dobrowolny donat / Voluntary donation

PL: fastec jest darmowe i publiczne. Jeśli oszczędza Ci czas lub uważasz je za wartościowe, możesz — całkowicie dobrowolnie — wesprzeć dalszy rozwój dowolną kwotą. Donat nie jest wymagany i nie odblokowuje żadnych funkcji: cała biblioteka jest w pełni dostępna bez niego.

EN: fastec is free and public. If it saves you time or you find it valuable, you may — entirely voluntarily — support further development with any amount. A donation is not required and does not unlock any features: the whole library is fully available without it.

sieć / network adres / address
Bitcoin (BTC) bc1qf9z86cw92vj9dkcvqwaleaezke2jtyvqg8dcww
BNB Smart Chain (BEP20) 0x717B0c7ad1d27a184BC0165F0eCB454614a51BE3

Dziękuję! / Thank you! — Paweł Marel


📄 Licencja / License

© 2026 Paweł Marel. Wszelkie prawa zastrzeżone / All rights reserved. Zobacz / see LICENSE.

About

⚡ fastec — szybka biblioteka secp256k1 dla Pythona (C++): pubkey, hash160, adresy, wielowątkowe przeszukiwanie. Miliony kluczy/s. Fast secp256k1 for Python.

Topics

Resources

Stars

0 stars

Watchers

0 watching

Forks

Releases

Packages

Contributors

Languages

⌂ 换网址