1a91b81de5
Documenta comandi di build/run per scanner Python e bruteforce C++, il flusso dati tra i due componenti e i dettagli architetturali non ovvi (partizionamento keyspace, euristica di rilevamento P2PK, accoppiamento tra formati file).
5.4 KiB
5.4 KiB
CLAUDE.md
This file provides guidance to Claude Code (claude.ai/code) when working with code in this repository.
Project purpose
Educational/research suite for studying Bitcoin P2PK (Pay-to-Public-Key) transactions and demonstrating why ECDSA secp256k1 bruteforce is computationally infeasible (keyspace 2^256). All docs and CLI output are in Italian. Two independent components share data via files, not code:
databases/— Python scanner that walks the Bitcoin blockchain via the mempool.space API, finds P2PK outputs, stores them in SQLite, and checks UTXO spent/unspent status.bruteforce/— C++ program that loads target public keys and searches the private-key space for matches, as a performance demonstration (not a realistic attack — success probability is ~2^-256).
Data flows one way: scanner → SQLite DB (databases/bitcoin_p2pk_study.db) → extract_p2pk_utxo.py filters unspent P2PK → target_keys.txt → C++ bruteforce consumes it.
Commands
Python scanner (databases/)
python3 -m venv .venv && source .venv/bin/activate
pip install -r requirements.txt
cd databases
python3 scan_blockchain.py # interactive: prompts for start/end block + delay
python3 view_db.py # generates p2pk_report.html
python3 view_db.py --stats # prints stats to terminal
C++ bruteforce (bruteforce/)
cd bruteforce
make install-deps # apt packages: build-essential, libsecp256k1-dev, libgmp-dev, autoconf, libtool, pkg-config
make # builds secp256k1 locally from source if bruteforce/secp256k1/ doesn't exist (~5 min first time), then compiles p2pk_bruteforce
make clean # remove binaries/object files
make clean-all # also removes the locally-built secp256k1 tree
make debug # -g -O0 build: p2pk_bruteforce_debug
make bench # 10-second timed run
make pgo # profile-guided optimization build (3-step: generate → run 30s → use)
make valgrind # leak check on the debug build
make help # list all targets
python3 extract_p2pk_utxo.py [db_path] [output.txt] # default: ../databases/bitcoin_p2pk_study.db -> target_keys.txt
python3 extract_p2pk_utxo.py --stats # DB stats only, no extraction
./p2pk_bruteforce [target_keys.txt] # runs until Ctrl+C; logs to progress.csv, matches to found_keys.txt
There is no test suite in this repo.
Architecture notes
bruteforce/p2pk_bruteforce.cpp (single file, everything lives here)
- One pthread per (core count − 1); each thread gets a disjoint slice of the 256-bit keyspace via
partition_keyspace(only the top 64 bits are partitioned — the search relies on random start offsets within each thread's slice, not a full 256-bit range split). - Target pubkeys are loaded from a
.txtfile (uncompressed hex,04prefix, one per line, header line skipped) into both anunordered_map<secp256k1_pubkey,...>(exact match) and a 64MB Bloom filter (fast negative rejection, checked first viacheck_match_fast). - Performance trick: instead of computing
privkey * Gper candidate, it computes one EC multiplication then derives the nextEC_BATCH_SIZE(256) keys via EC point addition against precomputed multiples of G (precompute_generator_multiples), which is much cheaper than repeated scalar multiplication. - Build system auto-detects a local
bruteforce/secp256k1/(built bybuild_secp256k1.shtargeting this specific CPU with-march=native) and links against it via rpath instead of the system lib; falls back to systemlibsecp256k1/libgmpif absent. - Matches are written to
found_keys.txt; periodic throughput stats go to stdout andprogress.csvevery 10s.
databases/scan_blockchain.py
P2PKBlockchainScannerclass wraps all mempool.space API access (get_block_hash,get_block_transactionswith pagination,check_utxo_status) and SQLite persistence.- P2PK detection is deliberately redundant — a script is classified as P2PK if ANY of 4 independent checks match: explicit
scriptpubkey_type, script byte length (67 or 35 bytes), ASM pattern (<pubkey> OP_CHECKSIG), or raw hex pattern (41<pubkey>ac/21<pubkey>ac). This exists because the API'sscriptpubkey_typefield is not reliable for very old (pre-2012) P2PK outputs. - Scanning is resumable:
scan_progresstable (single row,id=1) trackslast_scanned_block; reruns default tolast_scanned_block + 1.UNIQUE(txid, output_index)onp2pk_addressesprevents duplicate inserts, so overlapping scan ranges are safe. - This script and its SQLite DB/CSV outputs are intended to be committed and shared across contributors scanning different block ranges (see
.gitignore— DB/CSV/HTML are NOT excluded).
bruteforce/extract_p2pk_utxo.py
- Reads only
is_unspent = 1rows from the scanner's DB, strips the41.../21...acscript wrapper to get the raw pubkey, and re-adds the04prefix. - Compressed pubkeys (33-byte, script length 70/hex prefix
21) are explicitly skipped — the C++ bruteforce only generates and matches uncompressed public keys.
Key coupling to be aware of
- The bruteforce binary's target file format (uncompressed hex pubkeys,
04prefix, header line) is produced exclusively byextract_p2pk_utxo.py— if editing one side's format, update the other. MakefileCFLAGS use-march=native/-mtune=nativeand-ffast-math; binaries are not portable across different CPUs and should be rebuilt (make clean && make) after moving to different hardware.