124 lines
5.8 KiB
Python
124 lines
5.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Independent re-verification of the transformer-archive artifact.
|
|
|
|
Exit code 0 iff ALL of the following hold, re-derived from the files on disk:
|
|
* manifest.json parses and carries the expected keys
|
|
* SOURCE-text.md matches its recorded sha256 + byte size
|
|
* figure1.png matches its recorded sha256 + byte size, is a real PNG, and its
|
|
IHDR dimensions match the manifest
|
|
* the figure's in-text reference string occurs in the snapshot
|
|
* every quote recorded in manifest.json is a VERBATIM substring of the snapshot
|
|
* every "QUOTE| " line in archive.md is a verbatim substring of the snapshot and
|
|
the count matches the manifest
|
|
* the archive's section map matches the manifest's section map
|
|
* the figure-label split (present-in-text vs figure-only) is re-derived from the
|
|
snapshot and matches the manifest
|
|
* the archive is non-trivial (>= 8 claims, >= 8 sections) and cites both payloads
|
|
|
|
A fabricated quote, a swapped figure, or an edited snapshot makes this exit nonzero.
|
|
"""
|
|
import hashlib, json, os, struct, sys
|
|
|
|
D = os.path.dirname(os.path.abspath(__file__))
|
|
SRC = os.path.join(D, "SOURCE-text.md")
|
|
FIG = os.path.join(D, "figure1.png")
|
|
MAN = os.path.join(D, "manifest.json")
|
|
ARC = os.path.join(D, "archive.md")
|
|
|
|
failures = []
|
|
|
|
|
|
def check(cond, msg):
|
|
print(("ok - " if cond else "FAIL - ") + msg)
|
|
if not cond:
|
|
failures.append(msg)
|
|
|
|
|
|
def sha(path):
|
|
h = hashlib.sha256()
|
|
with open(path, "rb") as f:
|
|
for chunk in iter(lambda: f.read(1 << 16), b""):
|
|
h.update(chunk)
|
|
return h.hexdigest()
|
|
|
|
|
|
# --- files exist -----------------------------------------------------------
|
|
for p in (SRC, FIG, MAN, ARC):
|
|
check(os.path.isfile(p), "exists: %s" % os.path.basename(p))
|
|
if failures:
|
|
print("\n%d failure(s) - aborting" % len(failures))
|
|
sys.exit(1)
|
|
|
|
source = open(SRC, encoding="utf-8").read()
|
|
arc = open(ARC, encoding="utf-8").read()
|
|
man = json.load(open(MAN, encoding="utf-8"))
|
|
|
|
# --- 1. manifest shape -----------------------------------------------------
|
|
for key in ("source", "figure", "sections", "quotes", "vision_labels_present_in_text"):
|
|
check(key in man, "manifest has key: %s" % key)
|
|
|
|
# --- 2. snapshot integrity -------------------------------------------------
|
|
check(sha(SRC) == man["source"]["sha256"],
|
|
"snapshot sha256 matches manifest (%s)" % man["source"]["sha256"][:16])
|
|
check(os.path.getsize(SRC) == man["source"]["bytes"],
|
|
"snapshot byte size matches manifest (%d)" % man["source"]["bytes"])
|
|
|
|
# --- 3. figure integrity ---------------------------------------------------
|
|
check(sha(FIG) == man["figure"]["sha256"],
|
|
"figure sha256 matches manifest (%s)" % man["figure"]["sha256"][:16])
|
|
check(os.path.getsize(FIG) == man["figure"]["bytes"],
|
|
"figure byte size matches manifest (%d)" % man["figure"]["bytes"])
|
|
head = open(FIG, "rb").read(24)
|
|
check(head[:8] == b"\x89PNG\r\n\x1a\n", "figure has PNG magic bytes")
|
|
w, h = struct.unpack(">II", head[16:24])
|
|
check((w, h) == (man["figure"]["png_width"], man["figure"]["png_height"]),
|
|
"figure IHDR %dx%d matches manifest" % (w, h))
|
|
check(man["figure"]["source_ref_in_text"] in source,
|
|
"figure reference %r occurs in the snapshot" % man["figure"]["source_ref_in_text"])
|
|
|
|
# --- 4. every recorded quote is verbatim in the snapshot --------------------
|
|
check(len(man["quotes"]) >= 8, "manifest records >= 8 quotes (got %d)" % len(man["quotes"]))
|
|
bad = [q["id"] for q in man["quotes"] if q["text"] not in source]
|
|
check(not bad, "all %d manifest quotes are verbatim substrings of the snapshot%s"
|
|
% (len(man["quotes"]), " (bad: %s)" % bad if bad else ""))
|
|
|
|
# --- 5. archive.md quote lines --------------------------------------------
|
|
quote_lines = [ln[len("QUOTE| "):] for ln in arc.splitlines() if ln.startswith("QUOTE| ")]
|
|
check(len(quote_lines) == len(man["quotes"]),
|
|
"archive.md has %d QUOTE lines, manifest has %d" % (len(quote_lines), len(man["quotes"])))
|
|
bad2 = [i for i, q in enumerate(quote_lines) if q not in source]
|
|
check(not bad2, "all archive.md QUOTE lines are verbatim substrings of the snapshot%s"
|
|
% (" (bad idx: %s)" % bad2 if bad2 else ""))
|
|
check(sorted(quote_lines) == sorted(q["text"] for q in man["quotes"]),
|
|
"archive.md QUOTE lines are exactly the manifest quotes")
|
|
|
|
# --- 6. section map --------------------------------------------------------
|
|
check(len(man["sections"]) >= 8, "manifest records >= 8 sections (got %d)" % len(man["sections"]))
|
|
sec_lines = [ln[2:].strip() for ln in arc.splitlines() if ln.startswith("- #")]
|
|
check(len(sec_lines) == len(man["sections"]),
|
|
"archive.md has %d section lines, manifest has %d" % (len(sec_lines), len(man["sections"])))
|
|
check(sec_lines == ["#" * s["level"] + " `" + s["title"] + "`" for s in man["sections"]],
|
|
"archive.md section map equals the manifest section map")
|
|
|
|
# --- 7. figure-label cross-check, re-derived from the snapshot -------------
|
|
labels = man["vision_labels_present_in_text"] + man["vision_labels_absent_from_text"]
|
|
recomputed_present = [l for l in labels if l in source]
|
|
recomputed_absent = [l for l in labels if l not in source]
|
|
check(recomputed_present == man["vision_labels_present_in_text"],
|
|
"re-derived present-in-text labels match manifest (%d)" % len(recomputed_present))
|
|
check(recomputed_absent == man["vision_labels_absent_from_text"],
|
|
"re-derived figure-only labels match manifest (%d)" % len(recomputed_absent))
|
|
|
|
# --- 8. archive cites both payloads and the source -------------------------
|
|
for needle in ("SOURCE-text.md", "figure1.png", "https://arxiv.org/html/1706.03762v7",
|
|
"verify.py"):
|
|
check(needle in arc, "archive.md cites %r" % needle)
|
|
|
|
print()
|
|
if failures:
|
|
print("%d FAILURE(S)" % len(failures))
|
|
sys.exit(1)
|
|
print("ALL CHECKS PASSED (%d quotes, %d sections, %d figure labels cross-checked)"
|
|
% (len(man["quotes"]), len(man["sections"]), len(labels)))
|
|
sys.exit(0)
|