Metadata-Version: 2.4
Name: fastk1
Version: 0.3.0
Summary: Fast secp256k1 sequential key and address generation for Apple Silicon and Linux x86_64
Author: fastsecp256k1 contributors
License: MIT
Project-URL: Homepage, https://github.com/adkidlabs/fastsecp256k1
Project-URL: Repository, https://github.com/adkidlabs/fastsecp256k1
Project-URL: Issues, https://github.com/adkidlabs/fastsecp256k1/issues
Project-URL: Changelog, https://github.com/adkidlabs/fastsecp256k1/blob/main/HISTORY.md
Keywords: secp256k1,bitcoin,cryptography,ecdsa,apple-silicon,arm64,benchmark,keygen
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: License :: OSI Approved :: MIT License
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: C
Classifier: Operating System :: MacOS :: MacOS X
Classifier: Operating System :: POSIX :: Linux
Classifier: Topic :: Security :: Cryptography
Requires-Python: >=3.8
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: cffi>=1.15
Provides-Extra: test
Requires-Dist: pytest>=7; extra == "test"
Dynamic: license-file

# fastsecp256k1 (Python: `fastk1`)

Fast secp256k1 **sequential** key and address generation, written from scratch in portable C,
specialized for Apple Silicon (ARM64) with a generic x86-64 fallback.

[![tests](https://github.com/adkidlabs/fastsecp256k1/actions/workflows/test.yml/badge.svg)](https://github.com/adkidlabs/fastsecp256k1/actions/workflows/test.yml)
[![PyPI](https://img.shields.io/pypi/v/fastk1)](https://pypi.org/project/fastk1/)
[![License: MIT](https://img.shields.io/badge/License-MIT-yellow.svg)](LICENSE)

```sh
pip install fastk1
```

## Why it's fast

Three ideas, each measured:

1. **Sequential walking** — for consecutive keys you don't need a scalar multiplication per key:
   from `start·G`, every next public key is one mixed addition (`P += G`) in Jacobian
   coordinates. One full `scalar_mul` per 16384-key chunk.
2. **Montgomery batch inversion** — field inversion costs ~255 squarings; collecting 2048 points
   and inverting once amortizes it to ~one inversion + three multiplications per key (**9.6×**
   over per-key inversion, measured).
3. **Fused keygen + hashing** — each worker generates *and* hashes its own sub-range, so
   SHA-256/RIPEMD-160 parallelize too instead of serializing behind keygen (**3.7×**, measured).

Field arithmetic is 4×64 limbs, all hot operations inlined; the SHA-256 path uses ARMv8 crypto
extensions (`sha256h`/`sha256h2`) when compiled on ARM and is portable C elsewhere.

## Measured performance (Apple M1 Pro, 10 cores)

| Operation | Single-thread | 4-thread |
|-----------|:---:|:---:|
| Key generation | ~3.0M keys/s | ~10.7M keys/s |
| Address generation (compressed) | ~300K addrs/s | ~1.1M addrs/s |

Full scanner built on this library (C++ CPU + Metal GPU): ~10.4M keys/s CPU, ~12.2M keys/s GPU —
see `HISTORY.md` for the optimization log.

## Quick start

```python
from fastk1 import generate, generate_addresses

# 5 compressed public keys starting from k = 1
keys = generate(b'\x00' * 31 + b'\x01', 5)          # bytearray, 5 × 33 bytes

# P2PKH addresses for the same keys
addrs = generate_addresses(b'\x00' * 31 + b'\x01', 5, addr_type='p2pkh')

# Bech32 addresses for Litecoin, 4 threads
addrs = generate_addresses(b'\xaa' * 32, 50000,
                           addr_type='bech32', coin='ltc', num_threads=4)
```

### C API

```c
#include "address.h"

uint8_t start[32] = {0};
start[31] = 1;

/* 1000 compressed P2PKH addresses (2 threads) */
char addrs[1000 * ADDRESS_MAX_STR_LEN];
fastk1_generate_addresses(addrs, start, 1000, ADDR_P2PKH, 2);

/* Single pubkey → address */
char addr[ADDRESS_MAX_STR_LEN];
fastk1_pubkey_to_address(addr, pubkey33, ADDR_BECH32);
```

## Supported address types and coins

| Type | Description | Example prefix |
|------|-------------|----------------|
| P2PKH | Legacy (1xxx) | `1` |
| P2SH | Nested segwit (3xxx) | `3` |
| Bech32 | Native segwit (bc1...) | `bc1` |

| Coin | P2PKH prefix | P2SH prefix | Bech32 HRP |
|------|:---:|:---:|:---:|
| Bitcoin | 0x00 | 0x05 | `bc` |
| Litecoin | 0x30 | 0x32 | `ltc` |
| Dogecoin | 0x1e | 0x16 | `doge` |
| Dash | 0x4c | 0x10 | `dash` |
| Ravencoin | 0x3c | 0x7a | `rvn` |

## Test vectors

- G compressed HASH160: `751e76e8199196d454941c45d1b3a323f1433bd6`
- G P2PKH address: `1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH`
- G Bech32 (P2WPKH) address: `bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5` (the canonical BIP-173 vector)
- G P2SH-P2WPKH address: `3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN`

## How it works

```
├── include/         # C headers
│   ├── field.h      # Field arithmetic (all inline)
│   ├── group.h      # Point operations
│   ├── scalar.h     # Scalar arithmetic
│   ├── ecmult.h     # wNAF multiplication
│   ├── walker.h     # Sequential key walker
│   ├── address.h    # Address encoding (P2PKH, P2SH, Bech32)
│   └── fastk1.h     # Curve & coin constants
├── src/             # C sources (self-contained: SHA-256, RIPEMD-160, keccak included)
├── python/fastk1/   # cffi bindings (ABI mode, zero-copy buffers)
├── tests/           # all 393 C tests + pytest wrapper
└── bench/           # bench_walker, bench_mt, bench_address, bench_scaling
```

The hot loop uses Jacobian coordinates with mixed addition (Jacobian + affine G); pubkey
serialization uses **batch Montgomery inversion** for affine conversion. Address encoding:
HASH160 (RIPEMD-160 over SHA-256), Base58Check, and BIP-173 Bech32 — all self-contained, no
external dependencies.

## Building and testing

```sh
make            # library + test binaries
make test       # all C tests
make bench      # benchmarks
python -m pip install .            # builds the C library, then installs
python -m pip install .[test]
pytest tests/ -q                   # Python-level tests
```

Requirements: a C compiler (clang/gcc), make, Python 3.8+, cffi. macOS (arm64 tuned) and
Linux x86_64/aarch64.

## Responsible use

This is a research and education tool for benchmarking sequential secp256k1 key derivation —
useful for performance work, protocol study, and CTF-style puzzles. It is not a wallet, and it
is not intended for accessing funds or data you do not own. Use responsibly and in accordance
with your local laws.

## History

The full optimization log — the batch-inversion 9.6×, the fused-hashing 3.7×, the
SIGBUS/alloca and RIPEMD-160 shift bugs — is in [`HISTORY.md`](HISTORY.md).

## License

MIT — see [LICENSE](LICENSE).
