#!/usr/bin/env python3 """Reproduce the verifier's non-BMP UTF-16 length boundary. Python standard library plus curl for the optional live check. The deterministic secp256k1 key is a public throwaway test scalar, not a wallet credential. Two low-volume live requests are made by default; use --offline for local only. """ from __future__ import annotations import argparse import base64 import hashlib import hmac import json import subprocess P = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F N = 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141 G = ( 0x79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798, 0x483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8, ) URL = "https://findsatoshi.work/api/genesis/verify" def sha256(data: bytes) -> bytes: return hashlib.sha256(data).digest() def dblsha(data: bytes) -> bytes: return sha256(sha256(data)) def point_add(a, b): if a is None: return b if b is None: return a x1, y1 = a x2, y2 = b if x1 == x2 and (y1 + y2) % P == 0: return None if a == b: slope = 3 * x1 * x1 * pow(2 * y1, -1, P) % P else: slope = (y2 - y1) * pow(x2 - x1, -1, P) % P x3 = (slope * slope - x1 - x2) % P return x3, (slope * (x1 - x3) - y1) % P def point_mul(k: int, point): result = None addend = point while k: if k & 1: result = point_add(result, addend) addend = point_add(addend, addend) k >>= 1 return result def ser32(value: int) -> bytes: return value.to_bytes(32, "big") def public_bytes(point, compressed=True): x, y = point if compressed: return bytes([2 + (y & 1)]) + ser32(x) return b"\x04" + ser32(x) + ser32(y) def varint(value: int) -> bytes: if value < 0xFD: return bytes([value]) if value <= 0xFFFF: return b"\xfd" + value.to_bytes(2, "little") raise ValueError("vector too large") def message_hash(message: str) -> bytes: encoded = message.encode("utf-8") prefix = b"Bitcoin Signed Message:\n" return dblsha(varint(len(prefix)) + prefix + varint(len(encoded)) + encoded) def nonce(secret: int, digest: bytes) -> int: x = ser32(secret) k = b"\x00" * 32 v = b"\x01" * 32 k = hmac.new(k, v + b"\x00" + x + digest, hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() k = hmac.new(k, v + b"\x01" + x + digest, hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() while True: v = hmac.new(k, v, hashlib.sha256).digest() candidate = int.from_bytes(v, "big") if 1 <= candidate < N: return candidate k = hmac.new(k, v + b"\x00", hashlib.sha256).digest() v = hmac.new(k, v, hashlib.sha256).digest() def sign(message: str, secret: int): digest = message_hash(message) z = int.from_bytes(digest, "big") k = nonce(secret, digest) rx, ry = point_mul(k, G) r = rx % N recid = (ry & 1) | (2 if rx >= N else 0) s = pow(k, -1, N) * (z + r * secret) % N if s > N // 2: s = N - s recid ^= 1 header = 31 + recid return header, r, s, base64.b64encode(bytes([header]) + ser32(r) + ser32(s)).decode() def recover(message: str, header: int, r: int, s: int): recid = (header - 27) & 3 x = r + (recid >> 1) * N assert x < P and 1 <= r < N and 1 <= s < N alpha = (pow(x, 3, P) + 7) % P y = pow(alpha, (P + 1) // 4, P) assert y * y % P == alpha if (y & 1) != (recid & 1): y = P - y z = int.from_bytes(message_hash(message), "big") return point_mul(pow(r, -1, N), point_add(point_mul(s, (x, y)), point_mul(-z % N, G))) def verify(point, message: str, r: int, s: int) -> bool: z = int.from_bytes(message_hash(message), "big") w = pow(s, -1, N) check = point_add(point_mul(z * w % N, G), point_mul(r * w % N, point)) return check is not None and check[0] % N == r def live(message: str, signature: str): body = json.dumps({"message": message, "signature": signature}, ensure_ascii=False, separators=(",", ":")).encode("utf-8") result = subprocess.run([ "curl", "-sS", "--max-time", "45", "-w", "\n%{http_code}", "-H", "Content-Type: application/json", "--data-binary", "@-", URL, ], input=body, capture_output=True, check=True) response_body, status_bytes = result.stdout.rsplit(b"\n", 1) status = int(status_bytes) try: payload = json.loads(response_body) except ValueError: payload = {} return status, hashlib.sha256(body).hexdigest(), payload def main() -> None: parser = argparse.ArgumentParser() parser.add_argument("--offline", action="store_true") args = parser.parse_args() secret = int.from_bytes(sha256(b"findsatoshi verifier-followup public deterministic throwaway key"), "big") % N public = point_mul(secret, G) expected = public_bytes(public).hex() vectors = { "ascii-2000": "a" * 2000, "bmp-2000": "é" * 2000, "emoji-1000": "\U0001f600" * 1000, "emoji-1001": "\U0001f600" * 1001, "emoji-2000": "\U0001f600" * 2000, } signed = {} local_ok = True for name, message in vectors.items(): header, r, s, signature = sign(message, secret) local_ok &= recover(message, header, r, s) == public local_ok &= verify(public, message, r, s) signed[name] = (message, signature) print(json.dumps({"local_recovery_matches_signer": local_ok, "local_ecdsa_verification": local_ok, "expected_compressed_pubkey": expected})) assert local_ok if args.offline: return results = {} for name in ("emoji-1000", "emoji-1001"): message, signature = signed[name] status, request_hash, payload = live(message, signature) recovered = payload.get("recovered_pubkey_compressed") results[name] = { "unicode_code_points": len(message), "utf16_code_units": len(message.encode("utf-16-le")) // 2, "http_status": status, "request_sha256": request_hash, "recovered_expected_key": recovered == expected if status == 200 else None, "response_error": payload.get("error"), } bug = (results["emoji-1000"]["http_status"] == 200 and results["emoji-1000"]["recovered_expected_key"] is True and results["emoji-1001"]["http_status"] == 400) print(json.dumps({"vectors": results, "utf16_length_bug_reproduced": bug}, indent=2)) if not bug: raise SystemExit("Boundary changed or request failed; inspect results before any lead submission.") if __name__ == "__main__": main()