{
 "item": "z408-key",
 "hypothesis": "Z32 = unchanged Z408 key + one reading order from the frozen family (printed, reverse, 2x17 down-and-two-across gather routes)",
 "corpus": {
  "title": "Moby-Dick; or, The Whale, by Herman Melville (Project Gutenberg eBook #2701)",
  "URL": "https://gutenberg.pglaf.org/2/7/0/2701/2701-0.txt",
  "letterCount": 955169,
  "sha256OfFileUsed": "f12eb530502755ef2b8f535e2ecef3e95802eab584e60ac7992ccf856256fdbe",
  "preparation": "text between the '*** START OF' line and the '*** END OF' marker, uppercased, everything except A-Z removed, concatenated without spaces; trigram counts over that single letter stream; P(c|ab)=(n(abc)+1)/(n(ab.)+26). gutenberg.org itself was showing a maintenance page at run time, so the PGLAF mirror of the same eBook was used."
 },
 "orderCount": 194,
 "orderCountNote": "204 raw gather routes (3 offsets x 34 start cells x 2 directions) -> 192 distinct; plus printed order and its reverse (neither coincides with a route) = 194. This matches the earlier count of 192 routes / 194 orders. The 12 duplicates are exactly the starts on a blank cell: for each offset (3) and direction (2), starting on either of the 2 blanks gives the same sequence as starting on the next cell visited, because blanks are skipped (3x2x2=12). Every walk visits all 34 cells because the step (+1 row mod 2, +/-2 col mod 17) has order lcm(2,17)=34.",
 "primary": {
  "observedT": -1.643567,
  "top5": [
   {
    "permutation": [
     23,
     10,
     27,
     14,
     31,
     1,
     18,
     5,
     22,
     9,
     26,
     13,
     30,
     17,
     4,
     21,
     8,
     25,
     12,
     29,
     16,
     3,
     20,
     7,
     24,
     11,
     28,
     15,
     32,
     2,
     19,
     6
    ],
    "reading": "XHENE_OLBW__IA_N_TADOFSILDTGIIEN",
    "score": -1.643567
   },
   {
    "permutation": [
     11,
     24,
     7,
     20,
     3,
     16,
     29,
     12,
     25,
     8,
     21,
     4,
     17,
     30,
     13,
     26,
     9,
     22,
     5,
     18,
     1,
     31,
     14,
     27,
     10,
     23,
     6,
     19,
     2,
     32,
     15,
     28
    ],
    "reading": "DLISFODAT_N_AI__WBLO_ENEHXNEIIGT",
    "score": -1.646036
   },
   {
    "permutation": [
     28,
     11,
     24,
     7,
     20,
     3,
     16,
     29,
     12,
     25,
     8,
     21,
     4,
     17,
     30,
     13,
     26,
     9,
     22,
     5,
     18,
     1,
     31,
     14,
     27,
     10,
     23,
     6,
     19,
     2,
     32,
     15
    ],
    "reading": "TDLISFODAT_N_AI__WBLO_ENEHXNEIIG",
    "score": -1.656191
   },
   {
    "permutation": [
     22,
     9,
     26,
     13,
     30,
     17,
     4,
     21,
     8,
     25,
     12,
     29,
     16,
     3,
     20,
     7,
     24,
     11,
     28,
     15,
     32,
     2,
     19,
     6,
     23,
     10,
     27,
     14,
     31,
     1,
     18,
     5
    ],
    "reading": "BW__IA_N_TADOFSILDTGIIENXHENE_OL",
    "score": -1.663948
   },
   {
    "permutation": [
     2,
     19,
     6,
     23,
     10,
     27,
     14,
     31,
     1,
     18,
     5,
     22,
     9,
     26,
     13,
     30,
     17,
     4,
     21,
     8,
     25,
     12,
     29,
     16,
     3,
     20,
     7,
     24,
     11,
     28,
     15,
     32
    ],
    "reading": "IENXHENE_OLBW__IA_N_TADOFSILDTGI",
    "score": -1.693399
   }
  ],
  "printedOrder": {
   "reading": "_IF_LNI_WHDA_NGOAOESNBXLT_ETDIEI",
   "score": -1.867434
  },
  "nullQuantiles": {
   "q50": -1.358229,
   "q95": -1.169782,
   "q99": -1.085002,
   "max": -0.982684
  },
  "shufflesAtLeastObserved": 1991,
  "p": 0.995502,
  "note": "observed T lies below the null median: the primary string scores worse under its best route than shuffled strings do"
 },
 "secondary": {
  "observedT": -1.231836,
  "top5": [
   {
    "permutation": [
     11,
     25,
     7,
     21,
     3,
     16,
     30,
     12,
     26,
     8,
     22,
     4,
     18,
     17,
     31,
     13,
     27,
     9,
     23,
     5,
     19,
     1,
     32,
     14,
     28,
     10,
     24,
     6,
     20,
     2,
     15,
     29
    ],
    "reading": "DTINFOI___B_O_E__W_LE__NTH_NS_GD",
    "score": -1.231836
   },
   {
    "permutation": [
     25,
     7,
     21,
     3,
     16,
     30,
     12,
     26,
     8,
     22,
     4,
     18,
     17,
     31,
     13,
     27,
     9,
     23,
     5,
     19,
     1,
     32,
     14,
     28,
     10,
     24,
     6,
     20,
     2,
     15,
     29,
     11
    ],
    "reading": "TINFOI___B_O_E__W_LE__NTH_NS_GDD",
    "score": -1.326223
   },
   {
    "permutation": [
     29,
     11,
     25,
     7,
     21,
     3,
     16,
     30,
     12,
     26,
     8,
     22,
     4,
     18,
     17,
     31,
     13,
     27,
     9,
     23,
     5,
     19,
     1,
     32,
     14,
     28,
     10,
     24,
     6,
     20,
     2,
     15
    ],
    "reading": "DDTINFOI___B_O_E__W_LE__NTH_NS_G",
    "score": -1.400136
   },
   {
    "permutation": [
     30,
     12,
     26,
     8,
     22,
     4,
     18,
     17,
     31,
     13,
     27,
     9,
     23,
     5,
     19,
     1,
     32,
     14,
     28,
     10,
     24,
     6,
     20,
     2,
     15,
     29,
     11,
     25,
     7,
     21,
     3,
     16
    ],
    "reading": "I___B_O_E__W_LE__NTH_NS_GDDTINFO",
    "score": -1.422307
   },
   {
    "permutation": [
     1,
     32,
     14,
     28,
     10,
     24,
     6,
     20,
     2,
     15,
     29,
     11,
     25,
     7,
     21,
     3,
     16,
     30,
     12,
     26,
     8,
     22,
     4,
     18,
     17,
     31,
     13,
     27,
     9,
     23,
     5,
     19
    ],
    "reading": "__NTH_NS_GDDTINFOI___B_O_E__W_LE",
    "score": -1.514615
   }
  ],
  "printedOrder": {
   "reading": "__F_LNI_WHD__NGO_OESNB__T__TDIE_",
   "score": -1.714641
  },
  "nullQuantiles": {
   "q50": -1.086161,
   "q95": -0.735671,
   "q99": -0.553508,
   "max": -0.228961
  },
  "shufflesAtLeastObserved": 1553,
  "p": 0.776612,
  "bestOrderSameAsPrimaryBest": false
 },
 "control": {
  "plainBlanked": "_LI_EKI_LING_EOPLEBECAUSE_TISSOM",
  "scatteredCiphertext": "_ELIITBGESCEIOUPLL_EN_IE_KSAO_MS",
  "bestReading": "PLEBECAUSE_TISSOM_LI_EKI_LING_EO",
  "bestIsControlOrRotation": true,
  "detected": true,
  "rankOfTrueOrder": 11,
  "trueOrderScore": -0.883463,
  "observedT": -0.831696,
  "nullQuantiles": {
   "q50": -1.339624,
   "q95": -1.16595,
   "q99": -1.094246,
   "max": -0.987729
  },
  "p": 0.0005,
  "note": "Blanking was applied to the control plaintext at the positions where the primary string has '_' (1,4,8,13,26), before scattering. The best order is a cyclic shift of the true route (same cycle, different start), so its reading is a rotation of the control text. Ten such shifts of the same route score above the true start, so the true order itself ranks 11th; the statistic finds the right route but not the right starting point."
 },
 "verdict": "no English-like reading",
 "limits": "Excludes only this exact combination: the unchanged Z408 key (as transcribed in the brief, with primary and secondary letter assignments) plus one of these 194 orders, scored by one Moby-Dick trigram model. Under it, neither the primary (p=0.9955) nor the secondary (p=0.7766) string reads more English-like than its own shuffles, and the two best orders differ; the power control shows the test would have flagged a real 32-letter English plaintext (with 5 blanks) enciphered this way (p=0.0005). Left open: a modified or different key, homophone choices other than the given assignments, other glyph readings for the unmatched positions, other transposition families (different grid shapes, steps, offsets, or columnar/other routes), nulls or encoding (codes, abbreviations, numbers, a key phrase or coordinates), and plaintext that is not ordinary English running text. With only 32 symbols and 5-11 blanks, a trigram statistic has limited power, and multiple orders inflate the best score; a low p would only be a lead for an independent test, never a decipherment.",
 "reproduce": {
  "seeds": "random.Random(20261201) primary, 20261202 secondary, 20261203 control; one Fisher-Yates shuffle per null draw, cumulative on the same list",
  "shuffles": 2000,
  "pFormula": "(1 + #shuffles with T >= observed T) / 2001",
  "command": "python3 z32.py [local copy of corpus]  (downloads CORPUS_URL if no path given)",
  "runtime": "about 6 s on CPython 3"
 },
 "code": "#!/usr/bin/env python3\n\"\"\"Z32 x unchanged Z408 key x frozen route family: trigram-score test.\nUsage: python3 z32.py [local_copy_of_corpus]  (else downloads CORPUS_URL).\"\"\"\nimport sys, re, math, random, json, hashlib, urllib.request\nCORPUS_URL = \"https://gutenberg.pglaf.org/2/7/0/2701/2701-0.txt\"  # Moby-Dick, PG eBook #2701\nSEED = 20261201\nNSHUF = 2000\nPRIMARY   = \"_IF_LNI_WHDA_NGOAOESNBXLT_ETDIEI\"\nSECONDARY = \"__F_LNI_WHD__NGO_OESNB__T__TDIE_\"\nCONTROL_PLAIN = \"ILIKEKILLINGPEOPLEBECAUSEITISSOM\"\nCONTROL_ROUTE = [1,18,5,22,9,26,13,30,17,4,21,8,25,12,29,16,3,20,7,24,11,28,15,32,2,19,6,23,10,27,14,31]\nassert len(PRIMARY) == len(SECONDARY) == len(CONTROL_PLAIN) == 32\n\ndef load_corpus():\n    if len(sys.argv) > 1:\n        raw = open(sys.argv[1], \"rb\").read()\n    else:\n        raw = urllib.request.urlopen(CORPUS_URL, timeout=120).read()\n    txt = raw.decode(\"utf-8\")\n    s = txt.index(\"*** START OF\"); s = txt.index(\"\\n\", s)\n    e = txt.index(\"*** END OF\")\n    letters = re.sub(r\"[^A-Z]\", \"\", txt[s:e].upper())  # A-Z only; accented/other chars dropped\n    return letters, hashlib.sha256(raw).hexdigest()\n\ndef build_model(L):\n    tri = {}; bi = {}\n    for i in range(len(L) - 2):\n        a = L[i:i+3]; tri[a] = tri.get(a, 0) + 1; bi[a[:2]] = bi.get(a[:2], 0) + 1\n    A = \"ABCDEFGHIJKLMNOPQRSTUVWXYZ\"\n    lp = {}\n    for x in A:\n        for y in A:\n            d = bi.get(x+y, 0) + 26\n            for z in A:\n                lp[x+y+z] = math.log10((tri.get(x+y+z, 0) + 1) / d)\n    return lp\n\ndef orders():\n    out = [tuple(range(1, 33)), tuple(range(32, 0, -1))]\n    routes = []\n    for off in (0, 1, 2):\n        grid = {}\n        for p in range(1, 18): grid[(0, p-1)] = p\n        for p in range(18, 33): grid[(1, off + p - 18)] = p\n        for r0 in (0, 1):\n            for c0 in range(17):\n                for dc in (2, -2):\n                    r, c, seq = r0, c0, []\n                    for _ in range(34):\n                        if (r, c) in grid: seq.append(grid[(r, c)])\n                        r, c = (r + 1) % 2, (c + dc) % 17\n                    assert sorted(seq) == list(range(1, 33))\n                    routes.append(tuple(seq))\n    raw_routes = len(routes)\n    uniq_routes = list(dict.fromkeys(routes))\n    allo = list(dict.fromkeys(out + uniq_routes))\n    return allo, raw_routes, len(uniq_routes)\n\ndef reading(s, perm): return \"\".join(s[p-1] for p in perm)\n\ndef score(r, lp):\n    v = [lp[r[i:i+3]] for i in range(len(r) - 2) if \"_\" not in r[i:i+3]]\n    return sum(v) / len(v) if v else float(\"-inf\")\n\ndef T(s, ords, lp): return max(score(reading(s, o), lp) for o in ords)\n\ndef quant(xs, q):\n    xs = sorted(xs); k = (len(xs) - 1) * q / 100.0; f = math.floor(k); c = min(f + 1, len(xs) - 1)\n    return xs[f] + (xs[c] - xs[f]) * (k - f)\n\ndef run(s, ords, lp, seed):\n    sc = sorted(((score(reading(s, o), lp), i) for i, o in enumerate(ords)), key=lambda t: (-t[0], t[1]))\n    obs = sc[0][0]\n    rng = random.Random(seed); chars = list(s); null = []\n    for _ in range(NSHUF):\n        rng.shuffle(chars); null.append(T(\"\".join(chars), ords, lp))\n    ge = sum(1 for t in null if t >= obs - 1e-12)\n    return {\n        \"observedT\": round(obs, 6),\n        \"top5\": [{\"permutation\": list(ords[i]), \"reading\": reading(s, ords[i]), \"score\": round(v, 6)} for v, i in sc[:5]],\n        \"printedOrder\": {\"reading\": reading(s, ords[0]), \"score\": round(score(reading(s, ords[0]), lp), 6)},\n        \"nullQuantiles\": {k: round(quant(null, q), 6) for k, q in ((\"q50\", 50), (\"q95\", 95), (\"q99\", 99), (\"max\", 100))},\n        \"shufflesAtLeastObserved\": ge,\n        \"p\": round((1 + ge) / (NSHUF + 1), 6),\n        \"_ranked\": sc,\n    }\n\ndef main():\n    L, sha = load_corpus(); lp = build_model(L)\n    ords, raw_routes, uniq_routes = orders()\n    res = {\"corpusLetterCount\": len(L), \"corpusSha256\": sha, \"rawRoutes\": raw_routes,\n           \"distinctRoutes\": uniq_routes, \"orderCount\": len(ords)}\n    for name, s, sd in ((\"primary\", PRIMARY, SEED), (\"secondary\", SECONDARY, SEED + 1)):\n        res[name] = run(s, ords, lp, sd)\n    # control: blank control letters where PRIMARY has '_', then scatter so CONTROL_ROUTE restores it\n    ctl = \"\".join(\"_\" if PRIMARY[i] == \"_\" else CONTROL_PLAIN[i] for i in range(32))\n    ct = [None] * 32\n    for i, p in enumerate(CONTROL_ROUTE): ct[p-1] = ctl[i]\n    ct = \"\".join(ct)\n    assert reading(ct, CONTROL_ROUTE) == ctl and tuple(CONTROL_ROUTE) in ords\n    c = run(ct, ords, lp, SEED + 2)\n    best = c[\"top5\"][0][\"reading\"]\n    rots = {ctl[k:] + ctl[:k] for k in range(32)}\n    ti = ords.index(tuple(CONTROL_ROUTE))\n    rank = 1 + [i for _, i in c[\"_ranked\"]].index(ti)\n    c.update({\"controlPlainBlanked\": ctl, \"scatteredCiphertext\": ct, \"bestReading\": best,\n              \"bestIsControlOrRotation\": best in rots, \"rankOfTrueOrder\": rank,\n              \"trueOrderScore\": round(score(ctl, lp), 6),\n              \"detected\": (c[\"p\"] < 0.05) and (best in rots)})\n    res[\"control\"] = c\n    P, S = res[\"primary\"], res[\"secondary\"]\n    same = P[\"_ranked\"][0][1] == S[\"_ranked\"][0][1]\n    res[\"primaryBestEqualsSecondaryBest\"] = same\n    res[\"verdict\"] = \"candidate reading\" if (P[\"p\"] < 0.01 and same) else \"no English-like reading\"\n    for k in (\"primary\", \"secondary\", \"control\"): res[k].pop(\"_ranked\")\n    print(json.dumps(res, indent=1))\n\nif __name__ == \"__main__\":\n    main()\n"
}