#!/usr/bin/env python3 """Job #3643: check the revision of research/history/staging/attack-0830-varE-identification.md. Everything here is measured from the two files and from the fixer that produced them; the shas of the removed and added blocks were frozen after the fixer ran and before this checker was written, so the checker cannot be tuned to whatever the fixer happened to emit. The claim under test: the revision is the SERVED file v2 (33cc3583) with the seven edits the three findings ask for, and with nothing else touched -- every other line byte-identical, no number moved, the ledger block intact, no new bare-math site, and the note's producer still reproducing its embedded output. verify-revision.py [--base served-v2.md] [--revised revised-....md] [--json] Exit 0 all checks pass, 2 any check fails. """ import argparse import hashlib import json import os import re import subprocess import sys HERE = os.path.dirname(os.path.abspath(__file__)) BASE_SHA = "33cc35833d33533777971079dc0d763e5b7e6be8a3bc9fab7bb0249208ddd6fb" REVISED_SHA = "d1200fd06efc3a1007f8a6ddc1178ceda9599bbf32dd8515bace8f5b78fa922e" # (name, first line in the base, last line in the base, sha of the base block, # anchor of the first new line, anchor of the last new line, sha of the new block) BLOCKS = [ ("ledger verdict", 8, 8, "d7f33aaeb75e1b9927a5d28a439cf176b43ca4adb5864e346176829256ad5f3c", "verdict: Neither closes", "verdict: Neither closes", "ef8b30b9a0d3bdef1a41070140929586203f4b1845bb4f0683fa1ffe127744d9"), ("section 0 PART G bullet", 89, 90, "d6bcb355394dcc1b182af9c8cfb690a9a0d08fc8ccaac0abbcf8bb7b0e1796eb", " obstruction is unbalanced, and the Kloosterman-fraction bounds in print reach", " three-branch and sharp-cut complements of the cell (section 4).", "0f4dbb63a824115c19c97216674499c4c80e9edc973a955be555efd751df8e30"), ("section 0 Henriot bullet", 94, 95, "b6c76a7ed542e34c3f17f394c3050da4e5d53eba5fde3963fbf651edd0a52d03", " (although they are still valid)\") applies to `F_0(h) F_1(h-2) F_1(h+2)` as the", " powers (`redteam-0830-imports.md` row a5; section 5), and gives", "f2a425236ff4134b96905d39919f88dd98612cd352c080bb233244ae488ae515"), ("section 1 4^omega sentence", 154, 154, "73e838f1afee70125373307e446cb7a43f9cc09333d0f2bbd995d5d98ed50979", "`sum_{n>2L} W_mix(n)` are both `O(ln^3 y)` by the `4^omega` weighted coefficient", "section 4)), so the", "8916a6dbfbfe8a3f92b61d3d881cd8549a59079f81654db8434690ad70441c8d"), ("section 3 item 5", 246, 249, "83e0b516219a9e1a560c3257a38a1a7a7c0b37d5e73c0a3e48aed0eb3c1844e8", " dominant cell sits at `min(d, e) <= L^{2/5}`. For `e(a inv(m)/n)` with", " what is needed there.", "2b7032c98b8393cb3416d740cb66734f5c1d14cfa7baf00e69806d8b0a718ffd"), ("section 5 Henriot row", 322, 322, "623272c6238a374df895077480ef6499bc1123538d934dd2c0d2da394ecaede2", "| Henriot 2012, MPCPS 152", "| Henriot 2012, MPCPS 152", "fbdfc7244b08b40d7bd1ea7002d36a34a1daef585dbb22832ef2936d8520f165"), ("section 5 DFI / Bettin-Chandee rows", 325, 325, "910889bb8642b013ebfb98de95af0bad7c9bc6624b87404036fa75f732b13395", "| Duke-Friedlander-Iwaniec 1997, Theorem 1", "| Bettin-Chandee Theorem 1 (read, p.2)", "2ba15b378dc304724fc747c624178b86128fd173236557951d49c35eb5c02b96"), ] FIXER_SHA = "613777b6530ea24a12d6852de77acbb882234d3fde13a1a2474f5d835ca9e028" OLD_PHRASES = [ "outside the range of every bilinear Kloosterman-fraction bound", "its hypotheses met as stated", "by the `4^omega` count", "for every `eps`; `x^alpha <= y <= x` met on dyadic blocks", "| **APPLIES AS STATED.** Deduction below |", "on the range sqrt(2x) < Q <= x", "and no bound in print for", "unbalanced is exactly where no Kloosterman-", ] NEW_PHRASES = [ "on Harper's range sqrt(2N) < Q <= N (N the length of his sum)", "applies as the erratum states it, under eps < alpha/600 and with F_0, F_1 read at prime powers", "the `4^omega` weighted coefficient", "mass (the sum of `2^{#zero labels}` over the assignments of labels to the primes)", "the classes number `3^omega` (return #913", "APPLIES AS THE ERRATUM STATES IT", "for `eps < alpha/600` and with `F_0, F_1` read at prime powers", "HYPOTHESIS MET for block (A)**, `\\|a\\| << MN`", "Duke-Friedlander-Iwaniec 1997, Theorem 1 (statement read at the page in Shen 2607.06575 Lemma 5", "what no bound in print reaches is", "a short factor below every fixed power `L^delta`", "outside the reach of the bilinear Kloosterman-fraction bounds in print below every fixed power L^delta", ] def sha_bytes(b): return hashlib.sha256(b).hexdigest() def sha_text(t): return hashlib.sha256(t.encode("utf-8")).hexdigest() def main() -> int: ap = argparse.ArgumentParser() ap.add_argument("--base", default=os.path.join(HERE, "served-v2.md")) ap.add_argument("--revised", default=os.path.join(HERE, "revised-attack-0830-varE-identification.md")) ap.add_argument("--fixer", default=os.path.join(HERE, "apply-revision.py")) ap.add_argument("--json", action="store_true") a = ap.parse_args() sys.stdout.reconfigure(newline="\n", encoding="utf-8", errors="replace") checks = [] def check(name, ok, detail=""): checks.append({"check": name, "ok": bool(ok), "detail": detail}) print("%-64s %s %s" % (name, "ok " if ok else "FAIL", detail)) base_raw = open(a.base, "rb").read() rev_raw = open(a.revised, "rb").read() base_lines = base_raw.decode("utf-8").split("\n") rev_lines = rev_raw.decode("utf-8").split("\n") check("V1 the base file is the served v2 sha", sha_bytes(base_raw) == BASE_SHA, BASE_SHA[:16] + "...") check("V2 the revision is the frozen sha", sha_bytes(rev_raw) == REVISED_SHA, sha_bytes(rev_raw)[:16] + "...") fixer_sha = sha_bytes(open(a.fixer, "rb").read()) if FIXER_SHA is None: print("\nFIXER_SHA is not frozen yet; it is " + fixer_sha) else: check("V3 the fixer is the frozen sha", fixer_sha == FIXER_SHA, fixer_sha[:16] + "...") # V4: rebuild with the fixer, and require the bytes to be the revision's tmp = os.path.join(HERE, "verify-rebuild.md") r = subprocess.run([sys.executable, a.fixer, "--base", a.base, "--out", tmp], cwd=HERE, capture_output=True, text=True) ok = r.returncode == 0 and sha_bytes(open(tmp, "rb").read()) == sha_bytes(rev_raw) check("V4 the fixer rebuilds the revision byte for byte", ok, "fixer exit %d" % r.returncode) os.remove(tmp) # V5: every new block is present at its anchor and hashes to the frozen sha for name, first, last, old_sha, a_first, a_last, new_sha in BLOCKS: base_block = "\n".join(base_lines[first - 1:last]) + "\n" check("V5a [%s] the replaced block is the frozen base block" % name, sha_text(base_block) == old_sha, old_sha[:16] + "...") hits = [i for i, l in enumerate(rev_lines) if l.startswith(a_first)] if len(hits) != 1: check("V5b [%s] the new block anchor is unique" % name, False, "%d hits" % len(hits)) continue i = hits[0] j = next(k for k in range(i, len(rev_lines)) if rev_lines[k].startswith(a_last)) block = "\n".join(rev_lines[i:j + 1]) + "\n" if new_sha is None: print(" [%s] new block measures %s (%d lines)" % (name, sha_text(block), j - i + 1)) else: check("V5b [%s] the new block is the frozen sha" % name, sha_text(block) == new_sha, sha_text(block)[:16] + "...") # V6: every other line byte-identical. Walk both files, skipping the replaced spans. replaced = {} for name, first, last, *_ in BLOCKS: replaced.setdefault(name, []).append((first, last)) keep = [] skip = set() for name, spans in replaced.items(): for first, last in spans: skip.update(range(first, last + 1)) for n in range(1, len(base_lines) + 1): if n not in skip: keep.append(base_lines[n - 1]) added = [] skip2 = set() for name, first, last, old_sha, a_first, a_last, new_sha in BLOCKS: hits = [i for i, l in enumerate(rev_lines) if l.startswith(a_first)] i = hits[0] j = next(k for k in range(i, len(rev_lines)) if rev_lines[k].startswith(a_last)) skip2.update(range(i, j + 1)) for n in range(len(rev_lines)): if n not in skip2: added.append(rev_lines[n]) check("V6 every line outside the seven edits is byte-identical", keep == added, "%d + %d lines compared" % (len(keep), len(added))) check("V6b the line-count arithmetic holds (420 base + 15 = 435)", len(base_lines) == 421 and len(rev_lines) == 436 and len(rev_lines) - len(base_lines) == 15, "%d -> %d" % (len(base_lines) - 1, len(rev_lines) - 1)) rev_text = rev_raw.decode("utf-8") base_text = base_raw.decode("utf-8") # V7: the old phrasings are gone, the new ones present exactly once for p in OLD_PHRASES: check("V7a gone: %r" % p[:52], rev_text.count(p) == 0, "still %d" % rev_text.count(p)) for p in NEW_PHRASES: check("V7b once: %r" % p[:52], rev_text.count(p) == 1, "%d occurrences" % rev_text.count(p)) # V8: the ledger block is intact apart from the verdict line def ledger(text): lines = text.split("\n") s = next(i for i, l in enumerate(lines) if l.startswith("") return lines[s:e + 1] lb, lr = ledger(base_text), ledger(rev_text) check("V8 the ledger block is the same length", len(lb) == len(lr), "%d lines" % len(lb)) keys_b = [l.split(":", 1)[0] for l in lb] keys_r = [l.split(":", 1)[0] for l in lr] check("V8b the ledger keys are unchanged", keys_b == keys_r, json.dumps(keys_b)) diff_keys = [k for k, x, y in zip(keys_r, lb, lr) if x != y and not k.startswith("verdict")] check("V8c only the verdict line of the ledger changed", diff_keys == [], json.dumps(diff_keys)) # V9: no new bare-math site (^ or _ followed by a brace, outside code spans) bare = re.compile(r"[A-Za-z0-9}\]]\s*[\^_]\{") def bare_sites(text): out = [] for n, line in enumerate(text.split("\n"), 1): stripped = re.sub(r"`[^`]*`", "", line) if bare.search(stripped): out.append(n) return out bs, rs = bare_sites(base_text), bare_sites(rev_text) check("V9 the number of bare-math sites is unchanged", len(bs) == len(rs), "%d -> %d sites (lines %s)" % (len(bs), len(rs), bs)) dirty = [] for name, first, last, old_sha, a_first, a_last, new_sha in BLOCKS: i = [k for k, l in enumerate(rev_lines) if l.startswith(a_first)][0] j = next(k for k in range(i, len(rev_lines)) if rev_lines[k].startswith(a_last)) block = re.sub(r"`[^`]*`", "", "\n".join(rev_lines[i:j + 1])) if bare.search(block): dirty.append(name) check("V9b no added block carries a bare-math site", not dirty, json.dumps(dirty)) # V10: parentheses. A block may start inside a parenthesis that an earlier line opened, # so what must hold is that the edit changes no net depth, and the whole file stays # balanced -- not that each fragment balances alone. def net_parens(text): return re.sub(r"`[^`]*`", "X", text).count("(") - re.sub(r"`[^`]*`", "X", text).count(")") check("V10 the whole file is still balanced", net_parens(base_text) == net_parens(rev_text) == 0, "net %d -> %d" % (net_parens(base_text), net_parens(rev_text))) for name, first, last, old_sha, a_first, a_last, new_sha in BLOCKS: base_block = "\n".join(base_lines[first - 1:last]) hits = [i for i, l in enumerate(rev_lines) if l.startswith(a_first)] i = hits[0] j = next(k for k in range(i, len(rev_lines)) if rev_lines[k].startswith(a_last)) new_block = "\n".join(rev_lines[i:j + 1]) check("V10b [%s] the edit changes no net parenthesis depth" % name, net_parens(base_block) == net_parens(new_block), "%d -> %d" % (net_parens(base_block), net_parens(new_block))) # V11: hygiene check("V11 the revision is UTF-8, LF-only, ends with a newline, has no tab", b"\r" not in rev_raw and rev_raw.endswith(b"\n") and b"\t" not in rev_raw and isinstance(rev_raw.decode("utf-8"), str), "%d bytes" % len(rev_raw)) check("V11b no local path or credential shape in the revision", "D:\\" not in rev_text and "%LOCALAPPDATA%" not in rev_text and "twin-primes.token" not in rev_text, "clean") # V12: the toolkit table gained exactly one row and every row is still a table row def table_rows(text, header): lines = text.split("\n") s = next(i for i, l in enumerate(lines) if l.startswith(header)) e = next(i for i in range(s, len(lines)) if lines[i].strip() == "" and i > s + 2) return [lines[k] for k in range(s, e) if lines[k].startswith("|")] hdr = "| source | statement as read" rb, rr = table_rows(base_text, hdr), table_rows(rev_text, hdr) check("V12 the section 5 toolkit table gained exactly one row", len(rr) == len(rb) + 1, "%d -> %d" % (len(rb), len(rr))) check("V12b every row of that table still starts and ends with a pipe", all(l.startswith("|") and l.rstrip().endswith("|") for l in rr), "%d rows" % len(rr)) # V13: the note's producer is untouched and still reproduces its embedded output r = subprocess.run([sys.executable, os.path.join(HERE, "verify-producer.py")], cwd=HERE, capture_output=True, text=True) check("V13 the producer still runs and reproduces its embedded block", r.returncode == 0, "verify-producer.py exit %d" % r.returncode) check("V13b the revision did not touch the producer's path or the note's producer line", rev_lines[11] == "`research/history/staging/attack-0830-varE-identification.js` (embedded by", rev_lines[11][:40]) ok = all(c["ok"] for c in checks) failed = [c["check"] for c in checks if not c["ok"]] print() print(json.dumps({"ok": ok, "checks": len(checks), "failed": failed}, indent=1)) return 0 if ok else 2 if __name__ == "__main__": raise SystemExit(main())