#!/usr/bin/env python3 """Verify v0.1.0's enabled wallet-transaction coin-selection heuristic. Stdlib-only static analysis. Reads the preserved .tgz in memory; never executes or extracts archived code. Also checks the saved live-case/lead snapshots for novelty terms. Writes deterministic JSON evidence with exact lines and bytes. """ import argparse import hashlib import json import re import tarfile from pathlib import Path ROOT = Path(__file__).resolve().parent DEFAULT_ARCHIVE = ROOT.parent / "sources" / "v0.1" / "bitcoin-0.1.0.tgz" DEFAULT_CASE_DIR = ROOT / "case-snapshot" EXPECTED_ARCHIVE = ( 2_935_983, "ce9da46516e3042741224a7f9061e3181a5a4d17abba72b6e82922af3753d756", ) EXPECTED_MEMBERS = { "bitcoin/src/main.cpp": (82_983, "c89f44185df7ff5d68342e5f533ae1282d5cf1259306d14c1c79315e973e0e1c"), "bitcoin/src/main.h": (33_212, "f134c66c90c9ee054573a4f3199a0231b95c9e7a26e1ee45a73748ff4ac3511e"), "bitcoin/src/ui.cpp": (97_185, "bec80418526c9fe45f3b83c99c0a708363942df271d3f048c18fec6ebff8fa63"), "bitcoin/bitcoin.exe": (6_440_960, "fbcac071d92e26d82ec917214e334bd43850c0691f113bab1d4741c9bdd30d2d"), } EXPECTED_CASE = ( 25_138, "75c73c19b16fd46ad9af97f7c690059d605e0ee653ce08dd0eae475b8268fb47", ) EXPECTED_LEADS = { "lead_0mwvunisl27e5f5f6e9bb619d": (3_592, "7f016a5bc93ea3c2ceb46266e00d12e5406f0056d593a3505b3196958383382f"), "lead_0mww2mv1b9a88ab2ea80f0157": (8_312, "30a3b7effb0268b9fc81a55a5c16aa456117c5268ee4235067454af46ce12a8e"), "lead_0mwwiktkz3fb08446945a9d2c": (7_309, "c7e4bd67c9dd414b6cdda9f7ab9b0bd551c2a63a5012b05ade2778cfb8cfe607"), "lead_0mwxd0fsl477b4aa5af8a2530": (4_741, "42e5319d2cf44e389f0e40fcab9ec97727616bec29b82554d662558ceec133e4"), "lead_0mwxl06rl0f50bcc9a8854ace": (3_710, "5bfa003a0c0d39dfd75af8daa9ca96a88f332094897f798fc94f4e25a6997175"), "lead_0mwxn5z210b6240f516f0c98f": (592, "a3733701e3317f12e3066fee18e3dc711d03e037c7f2cdc06887d7993faf1860"), "lead_0mwxo8axrb9cca0932ec79e8e": (6_173, "7b28f75d3cbb0f528dbdea042bd3cb90449b21670b7762e1fbd2745e3e3ba6a5"), "lead_0mwxquvq137d8b26ed07b78d4": (550, "c0281f423fc0c59925d4745d44c16397541fdc25803c3bbda13ed173e95ff7c5"), "lead_0mwxt0i22151221df4870197b": (3_816, "c978b1fc3351363679bdb24540844961f88e23eb7734715a1962aef42ffe962f"), } SNIPPETS = [ ( "toolbar-opens-send-dialog", "bitcoin/src/ui.cpp", 848, 860, ["CMainFrame::OnButtonSend", "CSendDialog dialog(this)", "dialog.ShowModal()"], ), ( "ordinary-address-payment-calls-sendmoney", "bitcoin/src/ui.cpp", 1337, 1374, ["CSendDialog::OnButtonSend", "AddressToHash160", "SendMoney(scriptPubKey, nValue, wtx)"], ), ( "output-credit-requires-ownership", "bitcoin/src/main.h", 314, 324, ["bool IsMine", "::IsMine(scriptPubKey)", "int64 GetCredit", "return nValue"], ), ( "selectable-value-is-wallet-transaction-credit", "bitcoin/src/main.h", 484, 490, ["int64 GetCredit", "foreach(const CTxOut& txout, vout)", "nCredit += txout.GetCredit()"], ), ( "wallet-groups-filter-exact-match-and-smallest-larger", "bitcoin/src/main.cpp", 2410, 2446, ["bool SelectCoins", "mapWallet", "IsFinal", "fSpent", "GetCredit", "n == nTargetValue", "nLowestLarger"], ), ( "fallback-and-randomized-subset-sum-loop", "bitcoin/src/main.cpp", 2448, 2489, ["nTotalLower < nTargetValue", "Solve subset sum by stochastic approximation", "nRep < 1000", "rand() % 2", "nPass < 2", "nTotal < nBest"], ), ( "overpayment-tie-break-and-selection-return", "bitcoin/src/main.cpp", 2491, 2508, ["nLowestLarger - nTargetValue <= nBest - nTargetValue", "setCoinsRet.insert", "SelectCoins() best subset: ", "return true"], ), ( "create-transaction-invokes-selector-with-fee-inclusive-target", "bitcoin/src/main.cpp", 2514, 2542, ["bool CreateTransaction", "nValue += nFee", "SelectCoins(nValue, setCoins)", "nValueIn += pcoin->GetCredit()", "vout.push_back"], ), ( "selected-groups-contribute-all-owned-outputs", "bitcoin/src/main.cpp", 2563, 2574, ["foreach(CWalletTx* pcoin, setCoins)", "pcoin->vout[nOut].IsMine()", "SignSignature"], ), ( "sendmoney-calls-create-transaction", "bitcoin/src/main.cpp", 2625, 2656, ["bool SendMoney", "CreateTransaction(scriptPubKey, nValue, wtxNew, nFeeRequired)", "CommitTransactionSpent", "RelayWalletTransaction"], ), ] PE_STRING_SOURCES = { "SelectCoins() best subset: ": "bitcoin/src/main.cpp", "Error: Transaction creation failed ": "bitcoin/src/main.cpp", "SendMoney: %s": "bitcoin/src/main.cpp", } NOVELTY_TERMS = [ "SelectCoins", "subset sum", "stochastic approximation", "coin selection", "nLowestLarger", "vfIncluded", "vfBest", ] def sha256(data): return hashlib.sha256(data).hexdigest() def check_bytes(data, expected, label): size, digest = expected assert len(data) == size, (label, "bytes", len(data), size) assert sha256(data) == digest, (label, "sha256", sha256(data), digest) return {"filename": label, "bytes": size, "sha256": digest} def main(): parser = argparse.ArgumentParser() parser.add_argument("--archive", type=Path, default=DEFAULT_ARCHIVE) parser.add_argument("--case-dir", type=Path, default=DEFAULT_CASE_DIR) parser.add_argument("--output", type=Path, default=ROOT / "evidence.json") args = parser.parse_args() archive_record = check_bytes(args.archive.read_bytes(), EXPECTED_ARCHIVE, args.archive.name) archive_record["url"] = "https://www.zorinaq.com/pub/bitcoin-0.1.0.tgz" files = {} with tarfile.open(args.archive, "r:gz") as tf: for member in tf.getmembers(): if member.isfile(): assert member.name not in files, member.name files[member.name] = tf.extractfile(member).read() assert len(files) == 48, len(files) assert sum(map(len, files.values())) == 9_291_155 member_records = {} for name, expected in EXPECTED_MEMBERS.items(): assert name in files, name member_records[name] = check_bytes(files[name], expected, name.rsplit("/", 1)[-1]) member_records[name]["member"] = name manifest = [ {"member": name, "bytes": len(data), "sha256": sha256(data)} for name, data in sorted(files.items()) ] def snippet(check_id, name, first, last, needles): lines = files[name].splitlines(keepends=True) raw = b"".join(lines[first - 1:last]) text = raw.decode("utf-8") for needle in needles: assert needle in text, (check_id, needle) return { "id": check_id, "member": name, "line_start": first, "line_end": last, "byte_start": sum(map(len, lines[:first - 1])), "byte_end": sum(map(len, lines[:last])), "bytes": len(raw), "sha256": sha256(raw), "text": text, } excerpts = [snippet(*row) for row in SNIPPETS] source_names = sorted( name for name in files if name.startswith("bitcoin/src/") and name.endswith((".cpp", ".h")) ) source_text = { # Latin-1 is a lossless byte-to-code-point view for ASCII token scans. name: files[name].decode("latin-1") for name in source_names } selector_references = [] for name, text in source_text.items(): for number, line in enumerate(text.splitlines(), 1): if re.search(r"\bSelectCoins\b", line): selector_references.append({"member": name, "line": number, "text": line.strip()}) assert [(r["member"], r["line"]) for r in selector_references] == [ ("bitcoin/src/main.cpp", 2410), ("bitcoin/src/main.cpp", 2501), ("bitcoin/src/main.cpp", 2535), ], selector_references exe = files["bitcoin/bitcoin.exe"] printable_runs = [(m.start(), m.group()) for m in re.finditer(rb"[\x20-\x7e]{4,}", exe)] pe_strings = [] for token, source_member in PE_STRING_SOURCES.items(): raw = token.encode("ascii") assert raw in files[source_member], token matches = [ {"byte_offset": offset, "byte_offset_hex": hex(offset), "bytes": len(raw), "sha256": sha256(raw)} for offset, value in printable_runs if value == raw ] assert matches, token pe_strings.append({"text": token, "source_member": source_member, "exe_occurrences": matches}) case_path = args.case_dir / "case.json" case_record = check_bytes(case_path.read_bytes(), EXPECTED_CASE, case_path.name) case_record["url"] = "https://findsatoshi.work/api/cases/version-zero-point-one" case = json.loads(case_path.read_text()) lead_records = [] novelty_documents = [case_path.read_text()] for lead_id, expected in EXPECTED_LEADS.items(): path = args.case_dir / (lead_id + ".json") record = check_bytes(path.read_bytes(), expected, path.name) record["lead_id"] = lead_id record["url"] = "https://findsatoshi.work/api/leads/" + lead_id lead_records.append(record) novelty_documents.append(path.read_text()) timeline_ids = [row["lead_id"] for row in case.get("timeline", [])] assert len(timeline_ids) == len(set(timeline_ids)) == 9 assert set(timeline_ids) == set(EXPECTED_LEADS) novelty_counts = { term: sum(text.lower().count(term.lower()) for text in novelty_documents) for term in NOVELTY_TERMS } assert all(count == 0 for count in novelty_counts.values()), novelty_counts evidence = { "format": "findsatoshi.version-zero-point-one.coin-selection-static-evidence.v1", "case_id": "version-zero-point-one", "finding": "The surviving hash-matched Bitcoin v0.1.0 archive contains an enabled stochastic wallet-transaction selection heuristic with exact matches, a smallest-larger fallback and up to 1,000 randomized subset-sum passes.", "scope": "Static analysis of the surviving VERSION 101 archive. No claim about January 8 launch-day differences, later versions, selection optimality, privacy intent or historical use frequency.", "sources": { "archive": archive_record, "live_case_snapshot": case_record, "public_lead_snapshots": lead_records, }, "archive_integrity": { "regular_files": len(files), "total_regular_file_bytes": sum(map(len, files.values())), "key_members": member_records, "manifest": manifest, }, "algorithm": [ "A selectable unit is one final, unspent CWalletTx's aggregate GetCredit(), not an individual UTXO.", "A single group exactly equal to the target returns immediately; smaller groups and the smallest larger group are tracked.", "If all smaller groups cannot meet the target, the smallest larger group is selected; if none exists, selection fails.", "Otherwise the source performs at most 1,000 repetitions, stopping early at an exact target. Each repetition has up to two passes: rand() % 2 choices, then omitted groups.", "The best observed subset minimizes overpayment. A smallest-larger group wins when its overpayment is less than or equal to the subset's.", "This is a stochastic heuristic, not a proof of globally optimal coin selection.", ], "enabled_call_path": [ "CMainFrame::OnButtonSend -> CSendDialog", "CSendDialog::OnButtonSend -> SendMoney (ordinary Bitcoin-address branch)", "SendMoney -> CreateTransaction", "CreateTransaction -> SelectCoins(nValue, setCoins), after adding the fee to the target", ], "selector_references_exhaustive_cpp_h_scan": selector_references, "source_excerpts": excerpts, "binary_corroboration": { "member": "bitcoin/bitcoin.exe", "bytes": len(exe), "sha256": sha256(exe), "method": "ASCII runs of four or more printable bytes, matched in full; offsets are zero-based in the PE file.", "strings": pe_strings, "limit": "Strings corroborate compiled inclusion; the enabled call path is established from source, not inferred from strings alone.", }, "public_novelty_check": { "timeline_records": len(timeline_ids), "public_lead_details_checked": len(lead_records), "terms_and_case_insensitive_occurrences": novelty_counts, "limit": "This establishes non-duplication within the saved live public case and lead details, not worldwide first discovery.", }, "limits": [ "No executable or archived code was run and no network messages were sent.", "Source groups whole wallet-transaction credits; describing this as modern UTXO-by-UTXO coin control would be inaccurate.", "Randomized subset-sum search is heuristic and bounded; no optimal-selection guarantee is claimed.", "PE strings are not used alone to infer functionality.", ], } encoded = (json.dumps(evidence, indent=2, ensure_ascii=False) + "\n").encode("utf-8") args.output.write_bytes(encoded) print("PASS: exact archive, source slices, enabled call path, PE strings and public novelty snapshot verified") print("selector references:", [(r["member"], r["line"]) for r in selector_references]) print("output:", args.output) print("bytes:", len(encoded)) print("sha256:", sha256(encoded)) if __name__ == "__main__": main()