#!/usr/bin/env python3 """Check every published private key of the 2015 puzzle transaction against the chain. Python 3 standard library only. Fetches the raw funding transaction 08389f34c98c606322740c0be6a7125d9860bb8d5cb182c02f98461e5fa6cd15 (mempool.space, then blockstream.info; the txid is re-checked against the bytes) and a public key table, then for every puzzle n with a published key: 1. derives the compressed P2PKH address from the key and compares it with output n-1, 2. checks the key has exactly n bits, i.e. 2^(n-1) <= key < 2^n. """ import hashlib, re, sys, urllib.request TXID = '08389f34c98c606322740c0be6a7125d9860bb8d5cb182c02f98461e5fa6cd15' TABLE = 'https://raw.githubusercontent.com/HomelessPhD/BTC32/main/BTC32_solved_unsolved.txt' APIS = ['https://mempool.space/api/tx/{}/hex', 'https://blockstream.info/api/tx/{}/hex'] P = 2**256 - 2**32 - 977 G = (0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8) B58 = '123456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz' def get(url): return urllib.request.urlopen(urllib.request.Request(url, headers={'User-Agent': 'Mozilla/5.0'}), timeout=60).read() def add(a, b): if a is None: return b if b is None: return a if a[0] == b[0] and (a[1] + b[1]) % P == 0: return None l = (3 * a[0] * a[0] * pow(2 * a[1], -1, P) if a == b else (b[1] - a[1]) * pow(b[0] - a[0], -1, P)) % P x = (l * l - a[0] - b[0]) % P return x, (l * (a[0] - x) - a[1]) % P def mul(k, pt=G): r = None while k: if k & 1: r = add(r, pt) pt = add(pt, pt); k >>= 1 return r def ripemd160(b): try: return hashlib.new('ripemd160', b).digest() except ValueError: pass # pure-Python fallback for builds without the legacy provider f = [lambda x, y, z: x ^ y ^ z, lambda x, y, z: (x & y) | (~x & z), lambda x, y, z: (x | ~y) ^ z, lambda x, y, z: (x & z) | (y & ~z), lambda x, y, z: x ^ (y | ~z)] K1 = [0, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]; K2 = [0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0] R1 = [list(range(16)), [7,4,13,1,10,6,15,3,12,0,9,5,2,14,11,8], [3,10,14,4,9,15,8,1,2,7,0,6,13,11,5,12], [1,9,11,10,0,8,12,4,13,3,7,15,14,5,6,2], [4,0,5,9,7,12,2,10,14,1,3,8,11,6,15,13]] R2 = [[5,14,7,0,9,2,11,4,13,6,15,8,1,10,3,12], [6,11,3,7,0,13,5,10,14,15,8,12,4,9,1,2], [15,5,1,3,7,14,6,9,11,8,12,2,10,0,4,13], [8,6,4,1,3,11,15,0,5,12,2,13,9,7,10,14], [12,15,10,4,1,5,8,7,6,2,13,14,0,3,9,11]] S1 = [[11,14,15,12,5,8,7,9,11,13,14,15,6,7,9,8], [7,6,8,13,11,9,7,15,7,12,15,9,11,7,13,12], [11,13,6,7,14,9,13,15,14,8,13,6,5,12,7,5], [11,12,14,15,14,15,9,8,9,14,5,6,8,6,5,12], [9,15,5,11,6,8,13,12,5,12,13,14,11,8,5,6]] S2 = [[8,9,9,11,13,15,15,5,7,7,8,11,14,14,12,6], [9,13,15,7,12,8,9,11,7,7,12,7,6,15,13,11], [9,7,15,11,8,6,6,14,12,13,5,14,13,13,7,5], [15,5,8,11,14,14,6,14,6,9,12,9,12,5,15,8], [8,5,12,9,12,5,14,6,8,13,6,5,15,13,11,11]] rol = lambda x, n: ((x << n) | (x >> (32 - n))) & 0xffffffff m = b + b'\x80' + b'\0' * ((55 - len(b)) % 64) + (8 * len(b)).to_bytes(8, 'little') h = [0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0] for i in range(0, len(m), 64): X = [int.from_bytes(m[i + 4 * j:i + 4 * j + 4], 'little') for j in range(16)] a1, b1, c1, d1, e1 = h; a2, b2, c2, d2, e2 = h for r in range(5): for j in range(16): t = (rol((a1 + (f[r](b1, c1, d1) & 0xffffffff) + X[R1[r][j]] + K1[r]) & 0xffffffff, S1[r][j]) + e1) & 0xffffffff a1, b1, c1, d1, e1 = e1, t, b1, rol(c1, 10), d1 t = (rol((a2 + (f[4 - r](b2, c2, d2) & 0xffffffff) + X[R2[r][j]] + K2[r]) & 0xffffffff, S2[r][j]) + e2) & 0xffffffff a2, b2, c2, d2, e2 = e2, t, b2, rol(c2, 10), d2 t = (h[1] + c1 + d2) & 0xffffffff h = [t, (h[2] + d1 + e2) & 0xffffffff, (h[3] + e1 + a2) & 0xffffffff, (h[4] + a1 + b2) & 0xffffffff, (h[0] + b1 + c2) & 0xffffffff] return b''.join(x.to_bytes(4, 'little') for x in h) def address(k): x, y = mul(k) pub = bytes([2 + (y & 1)]) + x.to_bytes(32, 'big') payload = b'\0' + ripemd160(hashlib.sha256(pub).digest()) data = payload + hashlib.sha256(hashlib.sha256(payload).digest()).digest()[:4] n, s = int.from_bytes(data, 'big'), '' while n: n, r = divmod(n, 58); s = B58[r] + s return '1' * (len(data) - len(data.lstrip(b'\0'))) + s def b58decode_check(a): n = 0 for c in a: n = n * 58 + B58.index(c) raw = n.to_bytes(25, 'big') assert hashlib.sha256(hashlib.sha256(raw[:-4]).digest()).digest()[:4] == raw[-4:] return raw[1:21] def outputs(raw): def varint(i): v = raw[i] if v < 0xfd: return v, i + 1 n = {0xfd: 2, 0xfe: 4, 0xff: 8}[v] return int.from_bytes(raw[i + 1:i + 1 + n], 'little'), i + 1 + n i = 4 n, i = varint(i) for _ in range(n): i += 36; l, i = varint(i); i += l + 4 n, i = varint(i); outs = [] for _ in range(n): i += 8; l, i = varint(i); outs.append(raw[i:i + l]); i += l return outs for api in APIS: try: raw = bytes.fromhex(get(api.format(TXID)).decode().strip()) if hashlib.sha256(hashlib.sha256(raw).digest()).digest()[::-1].hex() == TXID: break except Exception: pass else: sys.exit('could not fetch the funding transaction') spks = outputs(raw) print('funding tx %s: %d outputs' % (TXID, len(spks))) table = get(TABLE) print('key table %s sha256 %s' % (TABLE, hashlib.sha256(table).hexdigest())) rows = re.findall(r'^\s*(\d+)\s*\|\s*([0-9A-Fa-f]{64})\s*\|\s*(\w+)', table.decode('utf-8', 'replace'), re.M) good = 0 for n_s, key_hex, listed in rows: n, k = int(n_s), int(key_hex, 16) a = address(k) spk = spks[n - 1] on_chain = spk[:3] == b'\x76\xa9\x14' and spk[3:23] == b58decode_check(a) bits = (1 << (n - 1)) <= k < (1 << n) good += on_chain and bits and a == listed print('#%-3d key bits=%-3d address %s matches output %d: %s in [2^%d, 2^%d): %s' % (n, k.bit_length(), a, n - 1, on_chain, n - 1, n, bits)) print('published keys checked: %d, all three checks pass: %d' % (len(rows), good)) sys.exit(0 if rows and good == len(rows) else 1)