#!/usr/bin/env python3 """Proof Miner local checker. Python 3.8+, no packages. Check a candidate on your own machine before you send it to the Lean server. The rules here are the same as the trusted Lean definitions (IsleDefs.SLP, IsleDefs.AES, IsleDefs.Sbox), but only Lean decides. python3 pm_check.py sbox CIRCUIT AES S-box circuit over {XOR, XNOR, AND} python3 pm_check.py mix CIRCUIT AES MixColumns, XOR gates only python3 pm_check.py invmix CIRCUIT AES InvMixColumns, XOR gates only python3 pm_check.py apn TABLE S-box table (2^n numbers): permutation? differential uniformity? python3 pm_check.py selftest CIRCUIT is a file with either JSON: {"gates": [["xor", 0, 1], ["and", 2, 8], ...], "outputs": [10, 9, ...]} Lean: IsleDefs.SLP.mk ([⟨.xor, 0, 1⟩, ⟨.and, 2, 8⟩] : List IsleDefs.Gate) [10, 9] Wires 0 … n−1 are the inputs (input bit i is bit i of x). Gate k (from 0) writes wire n + k and reads two wires with smaller numbers. The outputs list gives output bit 0 first. TABLE is a file of 2^n integers (decimal or 0x hex), entry x first, separated by spaces, commas or newlines. """ import json import re import sys # ---------------------------------------------------------------- AES (FIPS 197), same as IsleDefs.AES def gmul(a, b): p = 0 for _ in range(8): if b & 1: p ^= a b >>= 1 a = ((a << 1) ^ 0x11B) if a & 0x80 else (a << 1) return p def ginv(x): r = 1 for _ in range(254): r = gmul(r, x) return r if x else 0 def rotl8(b, s): return ((b << s) | (b >> (8 - s))) & 255 def sbox(x): b = ginv(x) return b ^ rotl8(b, 1) ^ rotl8(b, 2) ^ rotl8(b, 3) ^ rotl8(b, 4) ^ 0x63 def mix_with(m, w): a = [(w >> (8 * i)) & 255 for i in range(4)] out = 0 for r in range(4): s = 0 for i in range(4): s ^= gmul(m[r][i], a[i]) out |= s << (8 * r) return out MIX = [[2, 3, 1, 1], [1, 2, 3, 1], [1, 1, 2, 3], [3, 1, 1, 2]] INVMIX = [[14, 11, 13, 9], [9, 14, 11, 13], [13, 9, 14, 11], [11, 13, 9, 14]] def mix_columns(w): return mix_with(MIX, w) def inv_mix_columns(w): return mix_with(INVMIX, w) # ---------------------------------------------------------------- circuits, same as IsleDefs.SLP OPS = ("xor", "xnor", "and") def parse_circuit(text): text = text.strip() if text.startswith("{"): j = json.loads(text) gates = [(str(g[0]).lower(), int(g[1]), int(g[2])) for g in j["gates"]] return gates, [int(o) for o in j["outputs"]] gl = re.findall(r"⟨\s*\.?(xor|xnor|and)\s*,\s*(\d+)\s*,\s*(\d+)\s*⟩", text) if not gl: raise SystemExit("cannot read the circuit: give JSON or the Lean form SLP.mk ([⟨.xor, 0, 1⟩, ...] : List IsleDefs.Gate) [outputs]") m = re.search(r"\[([\d\s,]*)\]\s*\)?\s*$", text) if not m: raise SystemExit("cannot find the output list (the last [...] in the text)") outs = [int(v) for v in re.findall(r"\d+", m.group(1))] return [(op, int(a), int(b)) for op, a, b in gl], outs def well_formed(n, gates, outs): for k, (op, a, b) in enumerate(gates): if op not in OPS: return f"gate {k}: unknown op {op}" if not (a < n + k and b < n + k): return f"gate {k} (wire {n + k}) reads wire {max(a, b)}; it may read only wires below {n + k}" for o in outs: if o >= n + len(gates): return f"output wire {o} does not exist (last wire is {n + len(gates) - 1})" return None def evaluate(ins, full, gates): ws = list(ins) for op, a, b in gates: u, v = ws[a], ws[b] ws.append(u ^ v if op == "xor" else (u ^ v ^ full) if op == "xnor" else (u & v)) return ws def depth(n, gates, outs): ds = [0] * n for _, a, b in gates: ds.append(max(ds[a], ds[b]) + 1) return max((ds[o] for o in outs), default=0) def truth_table(n, p): t = 0 for x in range(2 ** n): if p(x): t |= 1 << x return t def computes(n, m, f, gates, outs): """SLP.computes n m f: wellFormed, m outputs, output j has the truth table of x ↦ bit j of f x.""" bad = well_formed(n, gates, outs) if bad: return False, bad if len(outs) != m: return False, f"{len(outs)} outputs, need {m}" ins = [truth_table(n, lambda x, i=i: (x >> i) & 1) for i in range(n)] ws = evaluate(ins, 2 ** (2 ** n) - 1, gates) vals = [f(x) for x in range(2 ** n)] for j, o in enumerate(outs): want = truth_table(n, lambda x, j=j: (vals[x] >> j) & 1) if ws[o] != want: x = next(x for x in range(2 ** n) if ((ws[o] >> x) & 1) != ((want >> x) & 1)) return False, f"output bit {j} (wire {o}) is wrong, first at input x = {x:#x}" return True, "correct" def computes_linear(n, L, gates, outs): """SLP.computesLinear n L: wellFormed, XOR gates only, n outputs, output j has the form of row j of L.""" bad = well_formed(n, gates, outs) if bad: return False, bad if any(op != "xor" for op, _, _ in gates): return False, "a gate is not XOR; this problem allows XOR gates only" if len(outs) != n: return False, f"{len(outs)} outputs, need {n}" cols = [L(1 << i) for i in range(n)] ws = evaluate([1 << i for i in range(n)], 0, gates) for j, o in enumerate(outs): row = sum(1 << i for i in range(n) if (cols[i] >> j) & 1) if ws[o] != row: return False, f"output bit {j} (wire {o}) is wrong: it XORs inputs {ws[o]:#x}, need {row:#x}" return True, "correct" def lean_circuit(gates, outs): gs = ", ".join(f"⟨.{op}, {a}, {b}⟩" for op, a, b in gates) return f"IsleDefs.SLP.mk ([{gs}] : List IsleDefs.Gate) [{', '.join(map(str, outs))}]" PROOF = "theorem main : Isle.Stmt.L_ := by\n unfold Isle.Stmt.L_\n exact ⟨{w}, by decide +kernel⟩" # NIST circuit complexity challenges (targets of the problems) and the best known results (NIST, Nov 2026). TARGETS = { "sbox": ("AES S-box", [("A ≤ 27", lambda s, a, d: a <= 27), ("G ≤ 99, GD ≤ 15", lambda s, a, d: s <= 99 and d <= 15), ("G ≤ 115, GD ≤ 10", lambda s, a, d: s <= 115 and d <= 10)], "best known: A 28 · G 115 at GD 15 · G 127 at GD 12 · G 159 at GD 10"), "mix": ("AES MixColumns", [("G ≤ 86, GD ≤ 5", lambda s, a, d: s <= 86 and d <= 5), ("G ≤ 93, GD ≤ 3", lambda s, a, d: s <= 93 and d <= 3)], "best known: 88 XOR at depth 5 · 89 at depth 4 · 97 at depth 3"), "invmix": ("AES InvMixColumns", [("G ≤ 105, GD ≤ 5", lambda s, a, d: s <= 105 and d <= 5), ("G ≤ 88, GD ≤ 7", lambda s, a, d: s <= 88 and d <= 7)], "best known: 117 XOR at depth 5 · 98 at depth 7 · 94 at depth 9"), } def check_circuit(kind, text): gates, outs = parse_circuit(text) if kind == "sbox": n = 8 ok, why = computes(8, 8, sbox, gates, outs) else: n = 32 ok, why = computes_linear(32, mix_columns if kind == "mix" else inv_mix_columns, gates, outs) name, targets, best = TARGETS[kind] s, a, d = len(gates), sum(op == "and" for op, _, _ in gates), depth(n, gates, outs) print(f"{name}: {'CORRECT' if ok else 'WRONG'} ({why})") print(f"gates G = {s} · AND gates A = {a} · gate depth GD = {d}") print(best) hit = [t for t, f in targets if f(s, a, d)] print("targets met: " + (", ".join(hit) if hit else "none")) if ok and hit: print("\nThis meets a target. Send it as the proof of the problem's target lemma:\n") print(PROOF.format(w=lean_circuit(gates, outs))) return 0 if ok else 1 # ---------------------------------------------------------------- S-box tables, same as IsleDefs.Sbox def diff_uniformity(t, n): N, worst = 2 ** n, 0 for a in range(1, N): c = [0] * N for x in range(N): c[t[x ^ a] ^ t[x]] += 1 worst = max(worst, max(c)) return worst def pack(t, n): return sum(v << (n * x) for x, v in enumerate(t)) def check_table(text): t = [int(v, 0) for v in re.findall(r"0[xX][0-9a-fA-F]+|\d+", text)] n = len(t).bit_length() - 1 if len(t) < 2 or 2 ** n != len(t): raise SystemExit(f"the table has {len(t)} entries; it needs 2^n entries") if any(v >= 2 ** n for v in t): raise SystemExit(f"an entry is {max(t)}; entries must be below 2^{n} = {2 ** n}") perm = len(set(t)) == len(t) du = diff_uniformity(t, n) print(f"{n}-bit table: permutation {'yes' if perm else 'no'} · differential uniformity {du}{' (APN)' if du == 2 else ''}") if n == 8: print("the AES S-box has differential uniformity 4. No 8-bit APN permutation is known.") if perm and du == 2: lit = hex(pack(t, n)) print(f"\nAn APN permutation. The packed table (entry x is bits {n}x … {n}x+{n - 1}):\n{lit}\n") print(PROOF.format(w=lit)) return 0 if (perm and du == 2) else 1 # ---------------------------------------------------------------- self test def selftest(): assert sbox(0x00) == 0x63 and sbox(0x01) == 0x7C and sbox(0x53) == 0xED and sbox(0xFF) == 0x16 assert mix_columns(0x455313DB) == 0xBCA14D8E and inv_mix_columns(0xBCA14D8E) == 0x455313DB assert all(inv_mix_columns(mix_columns(w)) == w for w in (1, 0xDEADBEEF, 0x01020304, 0xFFFFFFFF)) # a plain XOR circuit for MixColumns: one chain of XORs per output bit g, outs = naive_linear(32, mix_columns) assert computes_linear(32, mix_columns, g, outs)[0] assert not computes_linear(32, inv_mix_columns, g, outs)[0] # a 3-input full adder: sum = a ⊕ b ⊕ c, carry = majority fa = [("xor", 0, 1), ("xor", 3, 2), ("and", 0, 1), ("and", 3, 2), ("xor", 5, 6)] f = lambda x: ((x & 1) ^ (x >> 1 & 1) ^ (x >> 2 & 1)) | (((x & 1) + (x >> 1 & 1) + (x >> 2 & 1)) >> 1) << 1 assert computes(3, 2, f, fa, [4, 7])[0] and not computes(3, 2, f, fa, [7, 4])[0] assert depth(3, fa, [4, 7]) == 3 gold5 = [cube(x, 5, 0b100101) for x in range(32)] assert len(set(gold5)) == 32 and diff_uniformity(gold5, 5) == 2 assert diff_uniformity([sbox(x) for x in range(256)], 8) == 4 print(f"selftest ok (plain MixColumns circuit: {len(g)} XOR gates)") return 0 def naive_linear(n, L): cols = [L(1 << i) for i in range(n)] gates, outs = [], [] for j in range(n): ins = [i for i in range(n) if (cols[i] >> j) & 1] w = ins[0] for i in ins[1:]: gates.append(("xor", w, i)) w = n + len(gates) - 1 outs.append(w) return gates, outs def cube(x, n, poly): def mul(a, b): p = 0 for _ in range(n): if b & 1: p ^= a b >>= 1 a <<= 1 if a >> n: a ^= poly return p return mul(mul(x, x), x) def main(argv): if len(argv) == 2 and argv[1] == "selftest": return selftest() if len(argv) != 3 or argv[1] not in ("sbox", "mix", "invmix", "apn"): print(__doc__) return 2 text = open(argv[2], encoding="utf-8").read() return check_table(text) if argv[1] == "apn" else check_circuit(argv[1], text) if __name__ == "__main__": sys.exit(main(sys.argv))