fastk1 0.3.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- fastk1-0.3.0/LICENSE +21 -0
- fastk1-0.3.0/MANIFEST.in +8 -0
- fastk1-0.3.0/Makefile +83 -0
- fastk1-0.3.0/PKG-INFO +174 -0
- fastk1-0.3.0/README.md +146 -0
- fastk1-0.3.0/bench/bench_address.c +50 -0
- fastk1-0.3.0/bench/bench_mt.c +39 -0
- fastk1-0.3.0/bench/bench_profile.c +64 -0
- fastk1-0.3.0/bench/bench_profile2.c +66 -0
- fastk1-0.3.0/bench/bench_scaling.c +62 -0
- fastk1-0.3.0/bench/bench_walker.c +44 -0
- fastk1-0.3.0/fastk1.egg-info/PKG-INFO +174 -0
- fastk1-0.3.0/fastk1.egg-info/SOURCES.txt +44 -0
- fastk1-0.3.0/fastk1.egg-info/dependency_links.txt +1 -0
- fastk1-0.3.0/fastk1.egg-info/requires.txt +4 -0
- fastk1-0.3.0/fastk1.egg-info/top_level.txt +1 -0
- fastk1-0.3.0/include/address.h +57 -0
- fastk1-0.3.0/include/bindings.h +37 -0
- fastk1-0.3.0/include/ecmult.h +21 -0
- fastk1-0.3.0/include/fastk1.h +33 -0
- fastk1-0.3.0/include/field.h +318 -0
- fastk1-0.3.0/include/group.h +60 -0
- fastk1-0.3.0/include/keccak.h +9 -0
- fastk1-0.3.0/include/scalar.h +33 -0
- fastk1-0.3.0/include/sha256.h +22 -0
- fastk1-0.3.0/include/walker.h +31 -0
- fastk1-0.3.0/pyproject.toml +49 -0
- fastk1-0.3.0/python/fastk1/__init__.py +160 -0
- fastk1-0.3.0/setup.cfg +4 -0
- fastk1-0.3.0/setup.py +67 -0
- fastk1-0.3.0/src/address.c +913 -0
- fastk1-0.3.0/src/binary_marker.c +35 -0
- fastk1-0.3.0/src/bindings.c +27 -0
- fastk1-0.3.0/src/ecmult.c +39 -0
- fastk1-0.3.0/src/field.c +6 -0
- fastk1-0.3.0/src/group.c +295 -0
- fastk1-0.3.0/src/keccak.c +105 -0
- fastk1-0.3.0/src/scalar.c +147 -0
- fastk1-0.3.0/src/sha256.c +128 -0
- fastk1-0.3.0/src/walker.c +162 -0
- fastk1-0.3.0/tests/test_address.c +667 -0
- fastk1-0.3.0/tests/test_ecmult.c +248 -0
- fastk1-0.3.0/tests/test_fastk1.py +75 -0
- fastk1-0.3.0/tests/test_field.c +86 -0
- fastk1-0.3.0/tests/test_group.c +175 -0
- fastk1-0.3.0/tests/test_walker.c +329 -0
fastk1-0.3.0/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 fastsecp256k1
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
fastk1-0.3.0/MANIFEST.in
ADDED
fastk1-0.3.0/Makefile
ADDED
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
UNAME := $(shell uname)
|
|
2
|
+
ARCH := $(shell uname -m)
|
|
3
|
+
ifeq ($(UNAME), Darwin)
|
|
4
|
+
CC = clang
|
|
5
|
+
ifeq ($(ARCH), arm64)
|
|
6
|
+
ARCH_FLAGS = -march=armv8.4-a -mtune=apple-m1
|
|
7
|
+
else
|
|
8
|
+
ARCH_FLAGS = -march=x86-64 -mtune=generic
|
|
9
|
+
endif
|
|
10
|
+
LDFLAGS += -framework Security
|
|
11
|
+
LIB_SO = libfastk1.dylib
|
|
12
|
+
else
|
|
13
|
+
CC = gcc
|
|
14
|
+
ifneq (,$(filter $(ARCH),arm64 aarch64))
|
|
15
|
+
ARCH_FLAGS =
|
|
16
|
+
else
|
|
17
|
+
ARCH_FLAGS = -march=x86-64 -mtune=generic
|
|
18
|
+
endif
|
|
19
|
+
LIB_SO = libfastk1.so
|
|
20
|
+
endif
|
|
21
|
+
|
|
22
|
+
CFLAGS = -O3 $(ARCH_FLAGS) -flto -funroll-loops -Wall -Wextra -Werror
|
|
23
|
+
CFLAGS += -Iinclude -Isrc -fPIC
|
|
24
|
+
LDFLAGS += -lm -lpthread -flto
|
|
25
|
+
|
|
26
|
+
SRC = src/field.c src/group.c src/scalar.c src/ecmult.c src/walker.c src/address.c src/keccak.c src/sha256.c src/bindings.c
|
|
27
|
+
HDR = include/fastk1.h include/field.h include/group.h include/scalar.h include/ecmult.h include/walker.h include/address.h include/sha256.h include/keccak.h include/bindings.h
|
|
28
|
+
OBJ = $(SRC:.c=.o)
|
|
29
|
+
|
|
30
|
+
LIB_A = libfastk1.a
|
|
31
|
+
|
|
32
|
+
TEST_BINS = tests/test_field tests/test_group tests/test_ecmult tests/test_walker tests/test_address
|
|
33
|
+
|
|
34
|
+
BENCH_BINS = bench/bench_walker bench/bench_mt bench/bench_address bench/bench_scaling
|
|
35
|
+
|
|
36
|
+
.PHONY: all clean test bench
|
|
37
|
+
|
|
38
|
+
all: $(LIB_A) $(LIB_SO) $(TEST_BINS)
|
|
39
|
+
cp $(LIB_SO) python/fastk1/$(LIB_SO)
|
|
40
|
+
|
|
41
|
+
bench: $(BENCH_BINS)
|
|
42
|
+
|
|
43
|
+
$(LIB_A): $(OBJ)
|
|
44
|
+
ar rcs $@ $^
|
|
45
|
+
|
|
46
|
+
$(LIB_SO): $(OBJ)
|
|
47
|
+
$(CC) -shared -o $@ $^ $(LDFLAGS)
|
|
48
|
+
|
|
49
|
+
src/%.o: src/%.c $(HDR)
|
|
50
|
+
$(CC) $(CFLAGS) -c -o $@ $<
|
|
51
|
+
|
|
52
|
+
tests/test_field: tests/test_field.c $(LIB_A)
|
|
53
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
54
|
+
|
|
55
|
+
tests/test_group: tests/test_group.c $(LIB_A)
|
|
56
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
57
|
+
|
|
58
|
+
tests/test_ecmult: tests/test_ecmult.c $(LIB_A)
|
|
59
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
60
|
+
|
|
61
|
+
tests/test_walker: tests/test_walker.c $(LIB_A)
|
|
62
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
63
|
+
|
|
64
|
+
tests/test_address: tests/test_address.c $(LIB_A)
|
|
65
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
66
|
+
|
|
67
|
+
bench/bench_walker: bench/bench_walker.c $(LIB_A)
|
|
68
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
69
|
+
|
|
70
|
+
bench/bench_mt: bench/bench_mt.c $(LIB_A)
|
|
71
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
72
|
+
|
|
73
|
+
bench/bench_address: bench/bench_address.c $(LIB_A)
|
|
74
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
75
|
+
|
|
76
|
+
bench/bench_scaling: bench/bench_scaling.c $(LIB_A)
|
|
77
|
+
$(CC) $(CFLAGS) -o $@ $< $(LIB_A) $(LDFLAGS)
|
|
78
|
+
|
|
79
|
+
test: all
|
|
80
|
+
tests/test_field && tests/test_group && tests/test_ecmult && tests/test_walker && tests/test_address
|
|
81
|
+
|
|
82
|
+
clean:
|
|
83
|
+
rm -f $(OBJ) $(LIB_A) $(LIB_SO) $(TEST_BINS) $(BENCH_BINS)
|
fastk1-0.3.0/PKG-INFO
ADDED
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: fastk1
|
|
3
|
+
Version: 0.3.0
|
|
4
|
+
Summary: Fast secp256k1 sequential key and address generation for Apple Silicon and Linux x86_64
|
|
5
|
+
Author: fastsecp256k1 contributors
|
|
6
|
+
License: MIT
|
|
7
|
+
Project-URL: Homepage, https://github.com/adkidlabs/fastsecp256k1
|
|
8
|
+
Project-URL: Repository, https://github.com/adkidlabs/fastsecp256k1
|
|
9
|
+
Project-URL: Issues, https://github.com/adkidlabs/fastsecp256k1/issues
|
|
10
|
+
Project-URL: Changelog, https://github.com/adkidlabs/fastsecp256k1/blob/main/HISTORY.md
|
|
11
|
+
Keywords: secp256k1,bitcoin,cryptography,ecdsa,apple-silicon,arm64,benchmark,keygen
|
|
12
|
+
Classifier: Development Status :: 4 - Beta
|
|
13
|
+
Classifier: Intended Audience :: Developers
|
|
14
|
+
Classifier: Intended Audience :: Science/Research
|
|
15
|
+
Classifier: License :: OSI Approved :: MIT License
|
|
16
|
+
Classifier: Programming Language :: Python :: 3
|
|
17
|
+
Classifier: Programming Language :: C
|
|
18
|
+
Classifier: Operating System :: MacOS :: MacOS X
|
|
19
|
+
Classifier: Operating System :: POSIX :: Linux
|
|
20
|
+
Classifier: Topic :: Security :: Cryptography
|
|
21
|
+
Requires-Python: >=3.8
|
|
22
|
+
Description-Content-Type: text/markdown
|
|
23
|
+
License-File: LICENSE
|
|
24
|
+
Requires-Dist: cffi>=1.15
|
|
25
|
+
Provides-Extra: test
|
|
26
|
+
Requires-Dist: pytest>=7; extra == "test"
|
|
27
|
+
Dynamic: license-file
|
|
28
|
+
|
|
29
|
+
# fastsecp256k1 (Python: `fastk1`)
|
|
30
|
+
|
|
31
|
+
Fast secp256k1 **sequential** key and address generation, written from scratch in portable C,
|
|
32
|
+
specialized for Apple Silicon (ARM64) with a generic x86-64 fallback.
|
|
33
|
+
|
|
34
|
+
[](https://github.com/adkidlabs/fastsecp256k1/actions/workflows/test.yml)
|
|
35
|
+
[](https://pypi.org/project/fastk1/)
|
|
36
|
+
[](LICENSE)
|
|
37
|
+
|
|
38
|
+
```sh
|
|
39
|
+
pip install fastk1
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
## Why it's fast
|
|
43
|
+
|
|
44
|
+
Three ideas, each measured:
|
|
45
|
+
|
|
46
|
+
1. **Sequential walking** — for consecutive keys you don't need a scalar multiplication per key:
|
|
47
|
+
from `start·G`, every next public key is one mixed addition (`P += G`) in Jacobian
|
|
48
|
+
coordinates. One full `scalar_mul` per 16384-key chunk.
|
|
49
|
+
2. **Montgomery batch inversion** — field inversion costs ~255 squarings; collecting 2048 points
|
|
50
|
+
and inverting once amortizes it to ~one inversion + three multiplications per key (**9.6×**
|
|
51
|
+
over per-key inversion, measured).
|
|
52
|
+
3. **Fused keygen + hashing** — each worker generates *and* hashes its own sub-range, so
|
|
53
|
+
SHA-256/RIPEMD-160 parallelize too instead of serializing behind keygen (**3.7×**, measured).
|
|
54
|
+
|
|
55
|
+
Field arithmetic is 4×64 limbs, all hot operations inlined; the SHA-256 path uses ARMv8 crypto
|
|
56
|
+
extensions (`sha256h`/`sha256h2`) when compiled on ARM and is portable C elsewhere.
|
|
57
|
+
|
|
58
|
+
## Measured performance (Apple M1 Pro, 10 cores)
|
|
59
|
+
|
|
60
|
+
| Operation | Single-thread | 4-thread |
|
|
61
|
+
|-----------|:---:|:---:|
|
|
62
|
+
| Key generation | ~3.0M keys/s | ~10.7M keys/s |
|
|
63
|
+
| Address generation (compressed) | ~300K addrs/s | ~1.1M addrs/s |
|
|
64
|
+
|
|
65
|
+
Full scanner built on this library (C++ CPU + Metal GPU): ~10.4M keys/s CPU, ~12.2M keys/s GPU —
|
|
66
|
+
see `HISTORY.md` for the optimization log.
|
|
67
|
+
|
|
68
|
+
## Quick start
|
|
69
|
+
|
|
70
|
+
```python
|
|
71
|
+
from fastk1 import generate, generate_addresses
|
|
72
|
+
|
|
73
|
+
# 5 compressed public keys starting from k = 1
|
|
74
|
+
keys = generate(b'\x00' * 31 + b'\x01', 5) # bytearray, 5 × 33 bytes
|
|
75
|
+
|
|
76
|
+
# P2PKH addresses for the same keys
|
|
77
|
+
addrs = generate_addresses(b'\x00' * 31 + b'\x01', 5, addr_type='p2pkh')
|
|
78
|
+
|
|
79
|
+
# Bech32 addresses for Litecoin, 4 threads
|
|
80
|
+
addrs = generate_addresses(b'\xaa' * 32, 50000,
|
|
81
|
+
addr_type='bech32', coin='ltc', num_threads=4)
|
|
82
|
+
```
|
|
83
|
+
|
|
84
|
+
### C API
|
|
85
|
+
|
|
86
|
+
```c
|
|
87
|
+
#include "address.h"
|
|
88
|
+
|
|
89
|
+
uint8_t start[32] = {0};
|
|
90
|
+
start[31] = 1;
|
|
91
|
+
|
|
92
|
+
/* 1000 compressed P2PKH addresses (2 threads) */
|
|
93
|
+
char addrs[1000 * ADDRESS_MAX_STR_LEN];
|
|
94
|
+
fastk1_generate_addresses(addrs, start, 1000, ADDR_P2PKH, 2);
|
|
95
|
+
|
|
96
|
+
/* Single pubkey → address */
|
|
97
|
+
char addr[ADDRESS_MAX_STR_LEN];
|
|
98
|
+
fastk1_pubkey_to_address(addr, pubkey33, ADDR_BECH32);
|
|
99
|
+
```
|
|
100
|
+
|
|
101
|
+
## Supported address types and coins
|
|
102
|
+
|
|
103
|
+
| Type | Description | Example prefix |
|
|
104
|
+
|------|-------------|----------------|
|
|
105
|
+
| P2PKH | Legacy (1xxx) | `1` |
|
|
106
|
+
| P2SH | Nested segwit (3xxx) | `3` |
|
|
107
|
+
| Bech32 | Native segwit (bc1...) | `bc1` |
|
|
108
|
+
|
|
109
|
+
| Coin | P2PKH prefix | P2SH prefix | Bech32 HRP |
|
|
110
|
+
|------|:---:|:---:|:---:|
|
|
111
|
+
| Bitcoin | 0x00 | 0x05 | `bc` |
|
|
112
|
+
| Litecoin | 0x30 | 0x32 | `ltc` |
|
|
113
|
+
| Dogecoin | 0x1e | 0x16 | `doge` |
|
|
114
|
+
| Dash | 0x4c | 0x10 | `dash` |
|
|
115
|
+
| Ravencoin | 0x3c | 0x7a | `rvn` |
|
|
116
|
+
|
|
117
|
+
## Test vectors
|
|
118
|
+
|
|
119
|
+
- G compressed HASH160: `751e76e8199196d454941c45d1b3a323f1433bd6`
|
|
120
|
+
- G P2PKH address: `1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH`
|
|
121
|
+
- G Bech32 (P2WPKH) address: `bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5` (the canonical BIP-173 vector)
|
|
122
|
+
- G P2SH-P2WPKH address: `3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN`
|
|
123
|
+
|
|
124
|
+
## How it works
|
|
125
|
+
|
|
126
|
+
```
|
|
127
|
+
├── include/ # C headers
|
|
128
|
+
│ ├── field.h # Field arithmetic (all inline)
|
|
129
|
+
│ ├── group.h # Point operations
|
|
130
|
+
│ ├── scalar.h # Scalar arithmetic
|
|
131
|
+
│ ├── ecmult.h # wNAF multiplication
|
|
132
|
+
│ ├── walker.h # Sequential key walker
|
|
133
|
+
│ ├── address.h # Address encoding (P2PKH, P2SH, Bech32)
|
|
134
|
+
│ └── fastk1.h # Curve & coin constants
|
|
135
|
+
├── src/ # C sources (self-contained: SHA-256, RIPEMD-160, keccak included)
|
|
136
|
+
├── python/fastk1/ # cffi bindings (ABI mode, zero-copy buffers)
|
|
137
|
+
├── tests/ # all 393 C tests + pytest wrapper
|
|
138
|
+
└── bench/ # bench_walker, bench_mt, bench_address, bench_scaling
|
|
139
|
+
```
|
|
140
|
+
|
|
141
|
+
The hot loop uses Jacobian coordinates with mixed addition (Jacobian + affine G); pubkey
|
|
142
|
+
serialization uses **batch Montgomery inversion** for affine conversion. Address encoding:
|
|
143
|
+
HASH160 (RIPEMD-160 over SHA-256), Base58Check, and BIP-173 Bech32 — all self-contained, no
|
|
144
|
+
external dependencies.
|
|
145
|
+
|
|
146
|
+
## Building and testing
|
|
147
|
+
|
|
148
|
+
```sh
|
|
149
|
+
make # library + test binaries
|
|
150
|
+
make test # all C tests
|
|
151
|
+
make bench # benchmarks
|
|
152
|
+
python -m pip install . # builds the C library, then installs
|
|
153
|
+
python -m pip install .[test]
|
|
154
|
+
pytest tests/ -q # Python-level tests
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
Requirements: a C compiler (clang/gcc), make, Python 3.8+, cffi. macOS (arm64 tuned) and
|
|
158
|
+
Linux x86_64/aarch64.
|
|
159
|
+
|
|
160
|
+
## Responsible use
|
|
161
|
+
|
|
162
|
+
This is a research and education tool for benchmarking sequential secp256k1 key derivation —
|
|
163
|
+
useful for performance work, protocol study, and CTF-style puzzles. It is not a wallet, and it
|
|
164
|
+
is not intended for accessing funds or data you do not own. Use responsibly and in accordance
|
|
165
|
+
with your local laws.
|
|
166
|
+
|
|
167
|
+
## History
|
|
168
|
+
|
|
169
|
+
The full optimization log — the batch-inversion 9.6×, the fused-hashing 3.7×, the
|
|
170
|
+
SIGBUS/alloca and RIPEMD-160 shift bugs — is in [`HISTORY.md`](HISTORY.md).
|
|
171
|
+
|
|
172
|
+
## License
|
|
173
|
+
|
|
174
|
+
MIT — see [LICENSE](LICENSE).
|
fastk1-0.3.0/README.md
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
1
|
+
# fastsecp256k1 (Python: `fastk1`)
|
|
2
|
+
|
|
3
|
+
Fast secp256k1 **sequential** key and address generation, written from scratch in portable C,
|
|
4
|
+
specialized for Apple Silicon (ARM64) with a generic x86-64 fallback.
|
|
5
|
+
|
|
6
|
+
[](https://github.com/adkidlabs/fastsecp256k1/actions/workflows/test.yml)
|
|
7
|
+
[](https://pypi.org/project/fastk1/)
|
|
8
|
+
[](LICENSE)
|
|
9
|
+
|
|
10
|
+
```sh
|
|
11
|
+
pip install fastk1
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Why it's fast
|
|
15
|
+
|
|
16
|
+
Three ideas, each measured:
|
|
17
|
+
|
|
18
|
+
1. **Sequential walking** — for consecutive keys you don't need a scalar multiplication per key:
|
|
19
|
+
from `start·G`, every next public key is one mixed addition (`P += G`) in Jacobian
|
|
20
|
+
coordinates. One full `scalar_mul` per 16384-key chunk.
|
|
21
|
+
2. **Montgomery batch inversion** — field inversion costs ~255 squarings; collecting 2048 points
|
|
22
|
+
and inverting once amortizes it to ~one inversion + three multiplications per key (**9.6×**
|
|
23
|
+
over per-key inversion, measured).
|
|
24
|
+
3. **Fused keygen + hashing** — each worker generates *and* hashes its own sub-range, so
|
|
25
|
+
SHA-256/RIPEMD-160 parallelize too instead of serializing behind keygen (**3.7×**, measured).
|
|
26
|
+
|
|
27
|
+
Field arithmetic is 4×64 limbs, all hot operations inlined; the SHA-256 path uses ARMv8 crypto
|
|
28
|
+
extensions (`sha256h`/`sha256h2`) when compiled on ARM and is portable C elsewhere.
|
|
29
|
+
|
|
30
|
+
## Measured performance (Apple M1 Pro, 10 cores)
|
|
31
|
+
|
|
32
|
+
| Operation | Single-thread | 4-thread |
|
|
33
|
+
|-----------|:---:|:---:|
|
|
34
|
+
| Key generation | ~3.0M keys/s | ~10.7M keys/s |
|
|
35
|
+
| Address generation (compressed) | ~300K addrs/s | ~1.1M addrs/s |
|
|
36
|
+
|
|
37
|
+
Full scanner built on this library (C++ CPU + Metal GPU): ~10.4M keys/s CPU, ~12.2M keys/s GPU —
|
|
38
|
+
see `HISTORY.md` for the optimization log.
|
|
39
|
+
|
|
40
|
+
## Quick start
|
|
41
|
+
|
|
42
|
+
```python
|
|
43
|
+
from fastk1 import generate, generate_addresses
|
|
44
|
+
|
|
45
|
+
# 5 compressed public keys starting from k = 1
|
|
46
|
+
keys = generate(b'\x00' * 31 + b'\x01', 5) # bytearray, 5 × 33 bytes
|
|
47
|
+
|
|
48
|
+
# P2PKH addresses for the same keys
|
|
49
|
+
addrs = generate_addresses(b'\x00' * 31 + b'\x01', 5, addr_type='p2pkh')
|
|
50
|
+
|
|
51
|
+
# Bech32 addresses for Litecoin, 4 threads
|
|
52
|
+
addrs = generate_addresses(b'\xaa' * 32, 50000,
|
|
53
|
+
addr_type='bech32', coin='ltc', num_threads=4)
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
### C API
|
|
57
|
+
|
|
58
|
+
```c
|
|
59
|
+
#include "address.h"
|
|
60
|
+
|
|
61
|
+
uint8_t start[32] = {0};
|
|
62
|
+
start[31] = 1;
|
|
63
|
+
|
|
64
|
+
/* 1000 compressed P2PKH addresses (2 threads) */
|
|
65
|
+
char addrs[1000 * ADDRESS_MAX_STR_LEN];
|
|
66
|
+
fastk1_generate_addresses(addrs, start, 1000, ADDR_P2PKH, 2);
|
|
67
|
+
|
|
68
|
+
/* Single pubkey → address */
|
|
69
|
+
char addr[ADDRESS_MAX_STR_LEN];
|
|
70
|
+
fastk1_pubkey_to_address(addr, pubkey33, ADDR_BECH32);
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## Supported address types and coins
|
|
74
|
+
|
|
75
|
+
| Type | Description | Example prefix |
|
|
76
|
+
|------|-------------|----------------|
|
|
77
|
+
| P2PKH | Legacy (1xxx) | `1` |
|
|
78
|
+
| P2SH | Nested segwit (3xxx) | `3` |
|
|
79
|
+
| Bech32 | Native segwit (bc1...) | `bc1` |
|
|
80
|
+
|
|
81
|
+
| Coin | P2PKH prefix | P2SH prefix | Bech32 HRP |
|
|
82
|
+
|------|:---:|:---:|:---:|
|
|
83
|
+
| Bitcoin | 0x00 | 0x05 | `bc` |
|
|
84
|
+
| Litecoin | 0x30 | 0x32 | `ltc` |
|
|
85
|
+
| Dogecoin | 0x1e | 0x16 | `doge` |
|
|
86
|
+
| Dash | 0x4c | 0x10 | `dash` |
|
|
87
|
+
| Ravencoin | 0x3c | 0x7a | `rvn` |
|
|
88
|
+
|
|
89
|
+
## Test vectors
|
|
90
|
+
|
|
91
|
+
- G compressed HASH160: `751e76e8199196d454941c45d1b3a323f1433bd6`
|
|
92
|
+
- G P2PKH address: `1BgGZ9tcN4rm9KBzDn7KprQz87SZ26SAMH`
|
|
93
|
+
- G Bech32 (P2WPKH) address: `bc1qw508d6qejxtdg4y5r3zarvary0c5xw7kv8f3t5` (the canonical BIP-173 vector)
|
|
94
|
+
- G P2SH-P2WPKH address: `3JvL6Ymt8MVWiCNHC7oWU6nLeHNJKLZGLN`
|
|
95
|
+
|
|
96
|
+
## How it works
|
|
97
|
+
|
|
98
|
+
```
|
|
99
|
+
├── include/ # C headers
|
|
100
|
+
│ ├── field.h # Field arithmetic (all inline)
|
|
101
|
+
│ ├── group.h # Point operations
|
|
102
|
+
│ ├── scalar.h # Scalar arithmetic
|
|
103
|
+
│ ├── ecmult.h # wNAF multiplication
|
|
104
|
+
│ ├── walker.h # Sequential key walker
|
|
105
|
+
│ ├── address.h # Address encoding (P2PKH, P2SH, Bech32)
|
|
106
|
+
│ └── fastk1.h # Curve & coin constants
|
|
107
|
+
├── src/ # C sources (self-contained: SHA-256, RIPEMD-160, keccak included)
|
|
108
|
+
├── python/fastk1/ # cffi bindings (ABI mode, zero-copy buffers)
|
|
109
|
+
├── tests/ # all 393 C tests + pytest wrapper
|
|
110
|
+
└── bench/ # bench_walker, bench_mt, bench_address, bench_scaling
|
|
111
|
+
```
|
|
112
|
+
|
|
113
|
+
The hot loop uses Jacobian coordinates with mixed addition (Jacobian + affine G); pubkey
|
|
114
|
+
serialization uses **batch Montgomery inversion** for affine conversion. Address encoding:
|
|
115
|
+
HASH160 (RIPEMD-160 over SHA-256), Base58Check, and BIP-173 Bech32 — all self-contained, no
|
|
116
|
+
external dependencies.
|
|
117
|
+
|
|
118
|
+
## Building and testing
|
|
119
|
+
|
|
120
|
+
```sh
|
|
121
|
+
make # library + test binaries
|
|
122
|
+
make test # all C tests
|
|
123
|
+
make bench # benchmarks
|
|
124
|
+
python -m pip install . # builds the C library, then installs
|
|
125
|
+
python -m pip install .[test]
|
|
126
|
+
pytest tests/ -q # Python-level tests
|
|
127
|
+
```
|
|
128
|
+
|
|
129
|
+
Requirements: a C compiler (clang/gcc), make, Python 3.8+, cffi. macOS (arm64 tuned) and
|
|
130
|
+
Linux x86_64/aarch64.
|
|
131
|
+
|
|
132
|
+
## Responsible use
|
|
133
|
+
|
|
134
|
+
This is a research and education tool for benchmarking sequential secp256k1 key derivation —
|
|
135
|
+
useful for performance work, protocol study, and CTF-style puzzles. It is not a wallet, and it
|
|
136
|
+
is not intended for accessing funds or data you do not own. Use responsibly and in accordance
|
|
137
|
+
with your local laws.
|
|
138
|
+
|
|
139
|
+
## History
|
|
140
|
+
|
|
141
|
+
The full optimization log — the batch-inversion 9.6×, the fused-hashing 3.7×, the
|
|
142
|
+
SIGBUS/alloca and RIPEMD-160 shift bugs — is in [`HISTORY.md`](HISTORY.md).
|
|
143
|
+
|
|
144
|
+
## License
|
|
145
|
+
|
|
146
|
+
MIT — see [LICENSE](LICENSE).
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
#include "address.h"
|
|
2
|
+
#include "walker.h"
|
|
3
|
+
#include "scalar.h"
|
|
4
|
+
#include <stdio.h>
|
|
5
|
+
#include <stdlib.h>
|
|
6
|
+
#include <sys/time.h>
|
|
7
|
+
|
|
8
|
+
static double now_sec(void) {
|
|
9
|
+
struct timeval tv;
|
|
10
|
+
gettimeofday(&tv, NULL);
|
|
11
|
+
return tv.tv_sec + tv.tv_usec / 1000000.0;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
static double bench(const uint8_t *start_bytes, int count, int addr_type, int coin, int threads, int compressed) {
|
|
15
|
+
char *addrs = (char *)malloc(count * ADDRESS_MAX_STR_LEN);
|
|
16
|
+
fastk1_generate_addresses_ex(addrs, start_bytes, 1000, addr_type, coin, 1, compressed);
|
|
17
|
+
double t0 = now_sec();
|
|
18
|
+
fastk1_generate_addresses_ex(addrs, start_bytes, count, addr_type, coin, threads, compressed);
|
|
19
|
+
double t1 = now_sec();
|
|
20
|
+
free(addrs);
|
|
21
|
+
return (count / (t1 - t0));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
int main(int argc, char **argv) {
|
|
25
|
+
int count = 100000;
|
|
26
|
+
if (argc > 1) count = atoi(argv[1]);
|
|
27
|
+
|
|
28
|
+
uint8_t start_bytes[32] = {0};
|
|
29
|
+
start_bytes[31] = 1;
|
|
30
|
+
|
|
31
|
+
printf("=== Address generation benchmark (n=%d) ===\n", count);
|
|
32
|
+
|
|
33
|
+
/* ST */
|
|
34
|
+
double st = bench(start_bytes, count, ADDR_P2PKH, COIN_BTC, 1, 1);
|
|
35
|
+
printf("P2PKH compressed ST: %.0f addrs/s\n", st);
|
|
36
|
+
|
|
37
|
+
/* MT */
|
|
38
|
+
double mt = bench(start_bytes, count, ADDR_P2PKH, COIN_BTC, 4, 1);
|
|
39
|
+
printf("P2PKH compressed MT4: %.0f addrs/s (%.1fx)\n", mt, mt / st);
|
|
40
|
+
|
|
41
|
+
/* ST uncompressed */
|
|
42
|
+
double stu = bench(start_bytes, count, ADDR_P2PKH, COIN_BTC, 1, 0);
|
|
43
|
+
printf("P2PKH uncompressed ST: %.0f addrs/s\n", stu);
|
|
44
|
+
|
|
45
|
+
/* MT uncompressed */
|
|
46
|
+
double mtu = bench(start_bytes, count, ADDR_P2PKH, COIN_BTC, 4, 0);
|
|
47
|
+
printf("P2PKH uncompressed MT4: %.0f addrs/s (%.1fx)\n", mtu, mtu / stu);
|
|
48
|
+
|
|
49
|
+
return 0;
|
|
50
|
+
}
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
#include "walker.h"
|
|
2
|
+
#include "scalar.h"
|
|
3
|
+
#include <stdio.h>
|
|
4
|
+
#include <stdlib.h>
|
|
5
|
+
#include <sys/time.h>
|
|
6
|
+
|
|
7
|
+
static double now_sec(void) {
|
|
8
|
+
struct timeval tv;
|
|
9
|
+
gettimeofday(&tv, NULL);
|
|
10
|
+
return tv.tv_sec + tv.tv_usec / 1000000.0;
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
int main(int argc, char **argv) {
|
|
14
|
+
int count = 100000;
|
|
15
|
+
int threads = 4;
|
|
16
|
+
if (argc > 1) count = atoi(argv[1]);
|
|
17
|
+
if (argc > 2) threads = atoi(argv[2]);
|
|
18
|
+
|
|
19
|
+
scalar start;
|
|
20
|
+
uint8_t bytes[32] = {0};
|
|
21
|
+
bytes[31] = 1;
|
|
22
|
+
scalar_from_bytes(&start, bytes);
|
|
23
|
+
|
|
24
|
+
uint8_t *out = (uint8_t *)malloc(count * 33);
|
|
25
|
+
|
|
26
|
+
walker_gen_keys_mt(out, &start, 1000, threads);
|
|
27
|
+
double t0 = now_sec();
|
|
28
|
+
walker_gen_keys_mt(out, &start, count, threads);
|
|
29
|
+
double t1 = now_sec();
|
|
30
|
+
|
|
31
|
+
double elapsed = t1 - t0;
|
|
32
|
+
double rate = count / elapsed;
|
|
33
|
+
|
|
34
|
+
printf("count=%d threads=%d elapsed=%.4fs rate=%.0f keys/s\n",
|
|
35
|
+
count, threads, elapsed, rate);
|
|
36
|
+
|
|
37
|
+
free(out);
|
|
38
|
+
return 0;
|
|
39
|
+
}
|
|
@@ -0,0 +1,64 @@
|
|
|
1
|
+
#include "field.h"
|
|
2
|
+
#include "group.h"
|
|
3
|
+
#include "scalar.h"
|
|
4
|
+
#include "ecmult.h"
|
|
5
|
+
#include "walker.h"
|
|
6
|
+
#include <stdio.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <sys/time.h>
|
|
9
|
+
|
|
10
|
+
static double now_sec(void) {
|
|
11
|
+
struct timeval tv;
|
|
12
|
+
gettimeofday(&tv, NULL);
|
|
13
|
+
return tv.tv_sec + tv.tv_usec / 1000000.0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
int main(void) {
|
|
17
|
+
int count = 4096;
|
|
18
|
+
scalar start;
|
|
19
|
+
uint8_t bytes[32] = {0};
|
|
20
|
+
bytes[31] = 1;
|
|
21
|
+
scalar_from_bytes(&start, bytes);
|
|
22
|
+
|
|
23
|
+
/* Profile: walker_walk only (no batch conversion, no serialization) */
|
|
24
|
+
ge *points = (ge *)malloc(count * sizeof(ge));
|
|
25
|
+
double t0 = now_sec();
|
|
26
|
+
walker_walk(points, &start, count);
|
|
27
|
+
double t1 = now_sec();
|
|
28
|
+
printf("walker_walk (%d): %.4f s (%.0f keys/s)\n", count, t1 - t0, count / (t1 - t0));
|
|
29
|
+
|
|
30
|
+
/* Profile: batch_to_affine only */
|
|
31
|
+
ge_affine *affine = (ge_affine *)malloc(count * sizeof(ge_affine));
|
|
32
|
+
t0 = now_sec();
|
|
33
|
+
ge_batch_to_affine(affine, points, count);
|
|
34
|
+
t1 = now_sec();
|
|
35
|
+
printf("batch_to_affine: %.4f s\n", t1 - t0);
|
|
36
|
+
|
|
37
|
+
/* Profile: serialize only */
|
|
38
|
+
uint8_t *out = (uint8_t *)malloc(count * 33);
|
|
39
|
+
t0 = now_sec();
|
|
40
|
+
for (int i = 0; i < count; i++)
|
|
41
|
+
ge_serialize_compressed(out + i * 33, &affine[i]);
|
|
42
|
+
t1 = now_sec();
|
|
43
|
+
printf("serialize: %.4f s\n", t1 - t0);
|
|
44
|
+
|
|
45
|
+
/* Profile: fe_mul (isolated) */
|
|
46
|
+
fe a, b, c;
|
|
47
|
+
fe_from_u64(&a, 0x1234567890ABCDEFULL);
|
|
48
|
+
fe_from_u64(&b, 0xFEDCBA0987654321ULL);
|
|
49
|
+
int N = 200000;
|
|
50
|
+
t0 = now_sec();
|
|
51
|
+
for (int i = 0; i < N; i++) fe_mul(&c, &a, &b);
|
|
52
|
+
t1 = now_sec();
|
|
53
|
+
printf("fe_mul x%d: %.4f s (%.0f ops/s)\n", N, t1 - t0, N / (t1 - t0));
|
|
54
|
+
|
|
55
|
+
t0 = now_sec();
|
|
56
|
+
for (int i = 0; i < N; i++) fe_sqr(&c, &a);
|
|
57
|
+
t1 = now_sec();
|
|
58
|
+
printf("fe_sqr x%d: %.4f s (%.0f ops/s)\n", N, t1 - t0, N / (t1 - t0));
|
|
59
|
+
|
|
60
|
+
free(points);
|
|
61
|
+
free(affine);
|
|
62
|
+
free(out);
|
|
63
|
+
return 0;
|
|
64
|
+
}
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
#include "field.h"
|
|
2
|
+
#include "group.h"
|
|
3
|
+
#include "scalar.h"
|
|
4
|
+
#include "ecmult.h"
|
|
5
|
+
#include "walker.h"
|
|
6
|
+
#include <stdio.h>
|
|
7
|
+
#include <stdlib.h>
|
|
8
|
+
#include <sys/time.h>
|
|
9
|
+
|
|
10
|
+
static double now_sec(void) {
|
|
11
|
+
struct timeval tv;
|
|
12
|
+
gettimeofday(&tv, NULL);
|
|
13
|
+
return tv.tv_sec + tv.tv_usec / 1000000.0;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
int main(void) {
|
|
17
|
+
int n = 100000;
|
|
18
|
+
scalar start;
|
|
19
|
+
uint8_t bytes[32] = {0};
|
|
20
|
+
bytes[31] = 1;
|
|
21
|
+
scalar_from_bytes(&start, bytes);
|
|
22
|
+
|
|
23
|
+
ge *p = (ge *)malloc(n * sizeof(ge));
|
|
24
|
+
|
|
25
|
+
/* walk */
|
|
26
|
+
double t0 = now_sec();
|
|
27
|
+
walker_walk(p, &start, n);
|
|
28
|
+
double t1 = now_sec();
|
|
29
|
+
printf("walk %d: %.4f s (%.0f keys/s)\n", n, t1 - t0, n / (t1 - t0));
|
|
30
|
+
|
|
31
|
+
/* batch affine in chunks of 4096 */
|
|
32
|
+
ge_affine *a = (ge_affine *)malloc(4096 * sizeof(ge_affine));
|
|
33
|
+
t0 = now_sec();
|
|
34
|
+
for (int i = 0; i < n; i += 4096) {
|
|
35
|
+
int todo = n - i;
|
|
36
|
+
if (todo > 4096) todo = 4096;
|
|
37
|
+
ge_batch_to_affine(a, p + i, todo);
|
|
38
|
+
}
|
|
39
|
+
t1 = now_sec();
|
|
40
|
+
printf("batch_affine %d (chunked): %.4f s\n", n, t1 - t0);
|
|
41
|
+
free(a);
|
|
42
|
+
|
|
43
|
+
/* serialize */
|
|
44
|
+
uint8_t *out = (uint8_t *)malloc(n * 33);
|
|
45
|
+
/* Re-do batch to get fresh affine data */
|
|
46
|
+
a = (ge_affine *)malloc(4096 * sizeof(ge_affine));
|
|
47
|
+
for (int i = 0; i < n; i += 4096) {
|
|
48
|
+
int todo = n - i;
|
|
49
|
+
if (todo > 4096) todo = 4096;
|
|
50
|
+
ge_batch_to_affine(a, p + i, todo);
|
|
51
|
+
t0 = now_sec();
|
|
52
|
+
for (int j = 0; j < todo; j++)
|
|
53
|
+
ge_serialize_compressed(out + (i + j) * 33, &a[j]);
|
|
54
|
+
t1 = now_sec();
|
|
55
|
+
}
|
|
56
|
+
printf("serialize %d (chunked): %.4f s\n", n, t1 - t0);
|
|
57
|
+
|
|
58
|
+
/* full pipeline */
|
|
59
|
+
t0 = now_sec();
|
|
60
|
+
walker_gen_keys(out, &start, n);
|
|
61
|
+
t1 = now_sec();
|
|
62
|
+
printf("full %d: %.4f s (%.0f keys/s)\n", n, t1 - t0, n / (t1 - t0));
|
|
63
|
+
|
|
64
|
+
free(p); free(a); free(out);
|
|
65
|
+
return 0;
|
|
66
|
+
}
|