# decryption_complete: false # source: archive/local/external_voynich_units/voynich_decipherment_repro_bundle/decipherment_attack_v6/run_differentials.py # stated_sha256: ae047ec0724b2d0cc28c5df7434f671e9d58b198f9828cf79df21c54d6659942 # matches_stated_sha256: false # pdf_pages: 493-495 # note: The stated SHA-256 is the digest printed in the PDF. # The PDF page extract follows the form feed. decryption_complete stays false. Voynich Complete Reproducibility Compendium - 2026-08-28 Page 3492 archive/local/external_voynich_units/voynich_decipherme nt_repro_bundle/decipherment_attack_v6/run_differential s.py SHA-256: ae047ec0724b2d0cc28c5df7434f671e9d58b198f9828cf79df21c54d6659942 | 8,601 bytes | workspace supplement #!/usr/bin/env python3 """Real-minus-surrogate language-match differentials for the five synthetic homophonic ciphers of validate_synthetics.py, over several seeds. For each configuration (H2, H3, H5, H3mix, H3rot) and each seed s: 1. build the synthetic ciphertext exactly as validate_synthetics.py does (make_tables + encipher + truncate), but with the cipher RNG re-seeded to ``20260809 + s`` immediately before the configuration is built, so that every (config, seed) cipher is reproducible on its own (seed 0 of H2 is therefore byte-identical to the H2 cipher of validate_synthetics.py, whose module RNG is fresh when H2 is built); 2. build the matched order-3 surrogate of that ciphertext with attack_voynich.ngram_generate (seed 7 + s), i.e. the same negative control that attack_voynich.py applies to Voynichese; 3. push both streams through the identical pipeline (BPE k -> 22 Brown classes -> near-bijective substitution attack, restarts=8, attack seed s) against the Latin held-out LM (right language) and the German LM (wrong language), and record the language-match index of each; 4. differential = index(real) - index(surrogate), per language. Output: output/differentials.json with per-seed values and per-config mean/min/max of the differential (Latin and German). Nothing else in output/ is touched. Usage (from this directory): python3 run_differentials.py [--seeds 0-4] [--restarts 8] [--workers 5] """ from __future__ import annotations import argparse import json import os import random import sys import time from multiprocessing import Pool from pathlib import Path HERE = Path(__file__).resolve().parent sys.path.insert(0, str(HERE)) import validate_synthetics as vs # noqa: E402 from attack_lib import build_lms, match_index, pipeline_classes, truncate # noqa: E402 from attack_voynich import ngram_generate # noqa: E402 OUT = HERE / "output" LANGS = ("latin_holdout", "german") CIPHER_SEED_BASE = 20260809 # validate_synthetics.py module RNG seed SURROGATE_SEED_BASE = 7 # attack_voynich.ngram_generate default seed CONFIG_ORDER = ["H3", "H2", "H5", "H3mix", "H3rot"] # priority order _STATE: dict = {} def _init_worker(): """Per-process setup: LMs and the Caesar plaintext lines.""" _STATE["lms"] = build_lms(list(LANGS)) base = vs.caesar_lines() freq = vs.Counter(c for ln in base for w in ln for c in w) _STATE["base"] = base _STATE["letters"] = [c for c, _ in freq.most_common()] def build_cipher(name: str, seed: int): """Synthetic ciphertext for config `name`, cipher RNG seeded per (config, seed).""" cfg = next(c for c in vs.CONFIGS if c[0] == name) _, n_var, lens, choice = cfg vs.rng = random.Random(CIPHER_SEED_BASE + seed) # make_tables/encipher read vs.rng tab = vs.make_tables(_STATE["letters"], n_var, lens) clines, _prov = vs.encipher(_STATE["base"], tab, choice) clines = truncate(clines, vs.TARGET) n_groups = n_var * len(_STATE["letters"]) k = 110 if n_groups > 80 else 64 return clines, k, n_groups def run_task(args): name, seed, restarts = args if not _STATE: _init_worker() t0 = time.time() clines, k, n_groups = build_cipher(name, seed) surrogate = ngram_generate(clines, seed=SURROGATE_SEED_BASE + seed) Voynich Complete Reproducibility Compendium - 2026-08-28 Page 3493 row = {"config": name, "seed": seed, "k": k, "n_groups": n_groups, "restarts": restarts, "m": 22, "glyphs_real": sum(len(w) for ln in clines for w in ln), "glyphs_surrogate": sum(len(w) for ln in surrogate for w in ln), "identical_lines": sum(a == b for a, b in zip(clines, surrogate))} for stream_name, lines in (("real", clines), ("surrogate", surrogate)): _segmap, ustream, maps = pipeline_classes(lines, k, [22]) cm = maps[22] for lang in LANGS: lm, anchor = _STATE["lms"][lang] r = match_index(ustream, cm, 22, lm, anchor, restarts=restarts, seed=seed) row[f"{stream_name}_{lang}"] = { "index": r["index"], "test_bits": r["test_bits"], "scrambled_bits": r["scrambled_bits"], "anchor_bits": r["anchor_bits"], } for lang in LANGS: row[f"differential_{lang}"] = (row[f"real_{lang}"]["index"] - row[f"surrogate_{lang}"]["index"]) row["seconds"] = time.time() - t0 return row def parse_seeds(text: str) -> list[int]: out = [] for part in text.split(","): if "-" in part: a, b = part.split("-") out.extend(range(int(a), int(b) + 1)) else: out.append(int(part)) return out def summarise(rows: list[dict]) -> dict: summary = {} for name in CONFIG_ORDER: sub = [r for r in rows if r["config"] == name] if not sub: continue entry = {"n_seeds": len(sub), "seeds": sorted(r["seed"] for r in sub)} for lang in LANGS: d = [r[f"differential_{lang}"] for r in sub] real = [r[f"real_{lang}"]["index"] for r in sub] sur = [r[f"surrogate_{lang}"]["index"] for r in sub] entry[lang] = { "differential_mean": sum(d) / len(d), "differential_min": min(d), "differential_max": max(d), "real_mean": sum(real) / len(real), "surrogate_mean": sum(sur) / len(sur), } summary[name] = entry return summary def main() -> None: ap = argparse.ArgumentParser() ap.add_argument("--seeds", default="0-4") ap.add_argument("--configs", default=",".join(CONFIG_ORDER)) ap.add_argument("--restarts", type=int, default=8) ap.add_argument("--workers", type=int, default=5) ap.add_argument("--output", type=Path, default=OUT / "differentials.json") args = ap.parse_args() seeds = parse_seeds(args.seeds) configs = [c for c in CONFIG_ORDER if c in args.configs.split(",")] # priority: main configs over all seeds first, then the two variants tasks = [(c, s, args.restarts) for c in configs if c in ("H3", "H2", "H5") for s in seeds] tasks += [(c, s, args.restarts) for c in configs if c in ("H3mix", "H3rot") for s in seeds] print(f"{len(tasks)} tasks, {args.workers} workers", flush=True) rows: list[dict] = [] started = time.time() def flush(): args.output.parent.mkdir(exist_ok=True) payload = { "description": "index(real synthetic cipher) - index(order-3 surrogate) " "per config and seed; m=22 classes; latin_holdout = right " "language, german = wrong language", "cipher_seed": f"random.Random({CIPHER_SEED_BASE} + seed) per config", "surrogate_seed": f"attack_voynich.ngram_generate(seed={SURROGATE_SEED_BASE} + seed)", "attack_seed": "seed", "restarts": args.restarts, "elapsed_seconds": time.time() - started, "summary": summarise(rows), "runs": sorted(rows, key=lambda r: (CONFIG_ORDER.index(r["config"]), r["seed"])), } args.output.write_text(json.dumps(payload, indent=2, default=float) + "\n") def report(row): rows.append(row) print(f"{row['config']:6s} seed={row['seed']} " f"latin real={row['real_latin_holdout']['index']:.3f} " f"sur={row['surrogate_latin_holdout']['index']:.3f} " f"diff={row['differential_latin_holdout']:+.3f} | " f"german real={row['real_german']['index']:.3f} " f"sur={row['surrogate_german']['index']:.3f} " Voynich Complete Reproducibility Compendium - 2026-08-28 Page 3494 f"diff={row['differential_german']:+.3f} " f"({row['seconds']:.0f}s)", flush=True) flush() if args.workers > 1: with Pool(args.workers, initializer=_init_worker) as pool: for row in pool.imap_unordered(run_task, tasks): report(row) else: _init_worker() for task in tasks: report(run_task(task)) flush() print("\nSUMMARY (differential = real - surrogate)") for name, entry in summarise(rows).items(): for lang in LANGS: e = entry[lang] print(f"{name:6s} {lang:13s} n={entry['n_seeds']} " f"mean={e['differential_mean']:+.3f} " f"min={e['differential_min']:+.3f} max={e['differential_max']:+.3f} " f"(real {e['real_mean']:.3f}, surrogate {e['surrogate_mean']:.3f})") print(f"wrote {args.output}") if __name__ == "__main__": main()