#!/usr/bin/env python3 """ Standalone First-Principles & 12-Case Negative-Mutation Verifier for Atelier VII: "The Mouse's Fold & the Galois Ladder" """ import copy import json import math import os import sys def locate_data(): for cand in [ "public/data/origami_galois.json", "data/origami_galois.json", os.path.join(os.path.dirname(os.path.abspath(__file__)), "public/data/origami_galois.json"), os.path.join(os.path.dirname(os.path.abspath(__file__)), "data/origami_galois.json"), ]: if os.path.exists(cand): return cand raise FileNotFoundError("Could not locate origami_galois.json") def poly_mul(A, B): out = [0] * (len(A) + len(B) - 1) for i, a in enumerate(A): for j, b in enumerate(B): out[i + j] += a * b return out def reflect_point_across_line(px, py, m, b): # Line: m*x - y + b = 0 d = (m * px - py + b) / (m * m + 1.0) return (px - 2.0 * m * d, py + 2.0 * d) def verify_dataset(D): meta = D["meta"] polys = D["polygons"] if len(polys) != 98: raise ValueError(f"Expected 98 polygons (3..100), got {len(polys)}") c_compass = sum(1 for e in polys if e["class"] == "compass") c_orig = sum(1 for e in polys if e["class"] == "origami_unlocked") c_forb = sum(1 for e in polys if e["class"] == "forbidden_o6") if (c_compass, c_orig, c_forb) != (24, 35, 39): raise ValueError(f"Polygon census mismatch: got {(c_compass, c_orig, c_forb)} != (24, 35, 39)") if meta["compass_count"] != 24 or meta["origami_unlocked_count"] != 35 or meta["origami_total_count"] != 59: raise ValueError("Meta polygon counts mismatch!") # 1. Verify Gauss-Wantzel & Pierpont-Gleason exact criteria on every N in 3..100 fermat_primes = {3, 5, 17, 257, 65537} for e in polys: N = e["N"] # Compute totient independently phi = sum(1 for k in range(1, N + 1) if math.gcd(k, N) == 1) if e["phi"] != phi or e["real_degree"] != phi // 2: raise ValueError(f"Totient/degree mismatch at N={N}") deg = phi // 2 if (2 ** e["exp2"]) * (3 ** e["exp3"]) * e["unfactored_core"] != deg: raise ValueError(f"Degree factorization mismatch at N={N}") # Independent Gauss-Wantzel check: odd prime factors distinct and in fermat_primes x = N while x % 2 == 0: x //= 2 is_gw = True d = 3 temp = x while d * d <= temp: if temp % d == 0: cnt = 0 while temp % d == 0: cnt += 1 temp //= d if cnt > 1 or d not in fermat_primes: is_gw = False d += 2 if temp > 1 and temp not in fermat_primes: is_gw = False # Independent Pierpont-Gleason check: phi(N) is 3-smooth tphi = phi while tphi % 2 == 0: tphi //= 2 while tphi % 3 == 0: tphi //= 3 is_pierpont = (tphi == 1) expected_cls = "compass" if is_gw else ("origami_unlocked" if is_pierpont else "forbidden_o6") if e["class"] != expected_cls: raise ValueError(f"Classification mismatch at N={N}: {e['class']} != {expected_cls}") # Verify minimal polynomial Psi_N(x) for N <= 40 at all primitive roots 2*cos(2*pi*k/N) if N <= 40: coeffs = e["psi_coeffs"] if coeffs is None or len(coeffs) - 1 != deg or coeffs[-1] != 1: raise ValueError(f"Invalid monic Psi_{N} degree/lead!") prim_roots = [2.0 * math.cos(2.0 * math.pi * k / N) for k in range(1, N // 2 + 1) if math.gcd(k, N) == 1] if len(prim_roots) != deg: raise ValueError(f"Primitive cosine count mismatch at N={N}") # Reconstruct monic polynomial from primitive cosines and compare integer coefficients! prod = [1.0] for r in prim_roots: new_p = [0.0] * (len(prod) + 1) for idx, val in enumerate(prod): new_p[idx] -= r * val new_p[idx + 1] += val prod = new_p for idx in range(deg + 1): if abs(prod[idx] - coeffs[idx]) > 1e-7: raise ValueError(f"Psi_{N} coeff {idx} mismatch: {coeffs[idx]} vs {prod[idx]}") # 2. Verify Beloch O6 Simultaneous Focus-Directrix Reflection & Lill Orthogonal Laser on all presets for pr in D["cubic_presets"]: p, q, r = pr["p"], pr["q"], pr["r"] F1 = pr["F1"] l1_y = pr["l1_y"] F2 = pr["F2"] l2_x = pr["l2_x"] if abs(F1[0] - p) > 1e-12 or abs(F1[1] - (-q + 1.0)) > 1e-12 or abs(l1_y - (-q - 1.0)) > 1e-12: raise ValueError(f"Beloch Parabola 1 focus/directrix mismatch in {pr['id']}") if abs(F2[0] - r) > 1e-12 or abs(F2[1]) > 1e-12 or abs(l2_x - (-r)) > 1e-12: raise ValueError(f"Beloch Parabola 2 focus/directrix mismatch in {pr['id']}") V = pr["lill_vertices"] exp_V = [[0.0, 0.0], [1.0, 0.0], [1.0, -p], [1.0 - q, -p], [1.0 - q, -p + r]] for a, b_pt in zip(V, exp_V): if math.hypot(a[0] - b_pt[0], a[1] - b_pt[1]) > 1e-12: raise ValueError(f"Lill turtle vertex mismatch in {pr['id']}") if not pr["roots"]: raise ValueError(f"No roots in preset {pr['id']}") for m in pr["roots"]: val = m ** 3 + p * (m ** 2) + q * m + r if abs(val) > 1e-11: raise ValueError(f"Cubic root residual error in {pr['id']}: {val}") # Beloch O6 crease y = m*x + b where b = -q - p*m - m^2 b = -q - p * m - m * m R1 = reflect_point_across_line(F1[0], F1[1], m, b) R2 = reflect_point_across_line(F2[0], F2[1], m, b) if abs(R1[1] - l1_y) > 1e-11 or abs(R2[0] - l2_x) > 1e-11: raise ValueError(f"Beloch O6 simultaneous reflection failed in {pr['id']}!") # Lill laser path orthogonality & target hit R_lill_1 = (1.0, m) t1 = m + p R_lill_2 = (1.0 + t1 * m, -p) t2 = t1 * m + q R_lill_3 = (1.0 - q, -p - t2 * m) v0 = (R_lill_1[0] - V[0][0], R_lill_1[1] - V[0][1]) v1 = (R_lill_2[0] - R_lill_1[0], R_lill_2[1] - R_lill_1[1]) v2 = (R_lill_3[0] - R_lill_2[0], R_lill_3[1] - R_lill_2[1]) if abs(v0[0] * v1[0] + v0[1] * v1[1]) > 1e-11 or abs(v1[0] * v2[0] + v1[1] * v2[1]) > 1e-11: raise ValueError(f"Lill laser orthogonality broken in {pr['id']}!") if math.hypot(R_lill_3[0] - V[4][0], R_lill_3[1] - V[4][1]) > 1e-11: raise ValueError(f"Lill laser missed terminal target P4 in {pr['id']}!") # 3. Verify Explicit Galois Towers (N = 5, 7, 9, 11, 13, 17, 19) towers = D["galois_towers"] # N=13: eta0^2 + eta0 - 3 = 0, x^3 - eta0*x^2 - x + (eta0 - 1) = 0 t13 = towers["13"] eta0 = t13["steps"][0]["value"] x13 = t13["steps"][1]["value"] if abs(eta0 ** 2 + eta0 - 3.0) > 1e-12 or abs(x13 ** 3 - eta0 * (x13 ** 2) - x13 + (eta0 - 1.0)) > 1e-12: raise ValueError("N=13 quadratic+cubic Galois tower residual error!") if abs(x13 - 2.0 * math.cos(2.0 * math.pi / 13.0)) > 1e-12: raise ValueError("N=13 target cosine mismatch!") # N=17: Gauss 3-quadratic tower t17 = towers["17"] A0 = t17["steps"][0]["value"] B0 = t17["steps"][1]["value"] x17 = t17["steps"][2]["value"] A1 = -1.0 - A0 B2 = (A1 + math.sqrt(A1 * A1 + 4.0)) / 2.0 if abs(A0 * A0 + A0 - 4.0) > 1e-12 or abs(B0 * B0 - A0 * B0 - 1.0) > 1e-12 or abs(x17 * x17 - B0 * x17 + B2) > 1e-12: raise ValueError("N=17 Gauss quadratic tower residual error!") if abs(x17 - 2.0 * math.cos(2.0 * math.pi / 17.0)) > 1e-12: raise ValueError("N=17 target cosine mismatch!") # N=19: 2-cubic tower (deg 9 = 3 x 3) t19 = towers["19"] u0 = t19["steps"][0]["value"] x19 = t19["steps"][1]["value"] if abs(u0 ** 3 + u0 ** 2 - 6.0 * u0 - 7.0) > 1e-12: raise ValueError("N=19 first cubic residual error!") if abs(x19 ** 3 - u0 * (x19 ** 2) + (u0 * u0 - 5.0) * x19 + (u0 * u0 - 6.0)) > 1e-12: raise ValueError("N=19 second cubic residual error!") if abs(x19 - 2.0 * math.cos(2.0 * math.pi / 19.0)) > 1e-12: raise ValueError("N=19 target cosine mismatch!") # 4. Verify Mascheroni Variable-Span Compass Amplifier vs. Eisenstein Unit-Compass Trap for d in [4.0, 2.0, 0.5, 0.25, 0.125]: k = 1 if d > 0.5 else int(math.floor(0.5 / d)) + 1 kd = k * d if kd <= 0.5: raise ValueError(f"Mascheroni amplifier failed to cross 1/2 barrier for d={d}") ux = 1.0 / (2.0 * kd) recip = k * (2.0 * ux) if abs(recip - 1.0 / d) > 1e-14: raise ValueError(f"Mascheroni reciprocal error at d={d}") # Verify Eisenstein lattice closure for fixed unit-radius compass: all 0 < u^2 + u*v + v^2 <= 4 lie in {1, 3, 4} # and all unit-circle crossings between distinct lattice points have integer (u, v) coordinates (never (1/2, 0)) sq_norms = set() for u in range(-4, 5): for v in range(-4, 5): n2 = u * u + u * v + v * v if 0 < n2 <= 4: sq_norms.add(n2) dx = u + 0.5 * v dy = (math.sqrt(3.0) / 2.0) * v dist = math.hypot(dx, dy) h = math.sqrt(max(0.0, 1.0 - 0.25 * dist * dist)) for sgn in (-1.0, 1.0): cx = 0.5 * dx - sgn * h * (dy / dist) cy = 0.5 * dy + sgn * h * (dx / dist) cv = cy / (math.sqrt(3.0) / 2.0) cu = cx - 0.5 * cv if abs(cu - round(cu)) > 1e-11 or abs(cv - round(cv)) > 1e-11: raise ValueError(f"Unit-compass crossing escaped Eisenstein lattice: ({cu}, {cv})") # Verify both brass (r=2 at +2) and smaller-pair (r=1/sqrt(2) at 0 with iron at -1) routes to +1/2: # Route A (brass r=2 at +2, iron r=1 at 0): x = +1/4, y = +-sqrt(15)/4 -> reflects 0 across x=1/4 to +1/2 x_brass, y_brass = 0.25, math.sqrt(15.0) / 4.0 if abs(x_brass**2 + y_brass**2 - 1.0) > 1e-14 or abs((x_brass - 2.0)**2 + y_brass**2 - 4.0) > 1e-14 or abs(2.0 * x_brass - 0.0 - 0.5) > 1e-14: raise ValueError("Brass r=2 route to +1/2 failed!") # Route B (smaller r=1/sqrt(2) at 0, iron r=1 at -1): x = -1/4, y = +-sqrt(7)/4 -> reflects -1 across x=-1/4 to +1/2 x_small, y_small = -0.25, math.sqrt(7.0) / 4.0 if abs(x_small**2 + y_small**2 - 0.5) > 1e-14 or abs((x_small + 1.0)**2 + y_small**2 - 1.0) > 1e-14 or abs(2.0 * x_small - (-1.0) - 0.5) > 1e-14: raise ValueError("Smaller r=1/sqrt(2) route to +1/2 failed!") if sq_norms != {1, 3, 4}: raise ValueError(f"Unexpected Eisenstein norms <= 4: {sq_norms}") # 5. Verify Terra-Flash-Sol Prolate Trochoid Knot Triad (Elevation, Monotone 2->1 Ratio, & Loop Area Split) knots = D["trochoid_knots"] if not knots: raise ValueError("Missing trochoid_knots!") prev_ratio = None prev_area = None for kn in knots: r = kn["d_over_R"] t0 = kn["t0_rad"] if not (0.0 < t0 < math.pi): raise ValueError(f"Invalid knot parameter t0={t0}") if abs(math.sin(t0) / t0 - 1.0 / r) > 1e-12: raise ValueError(f"sin(t0)/t0 != R/d at d/R={r}") yk = 1.0 - r * math.cos(t0) ym = 1.0 - r if yk <= 0.0 or ym >= 0.0: raise ValueError(f"Prolate knot must satisfy y_knot > 0 and y_min < 0 at d/R={r}") if abs(yk - kn["y_knot_over_R"]) > 1e-12 or abs(yk / abs(ym) - kn["altitude_ratio"]) > 1e-10: raise ValueError(f"Prolate knot altitude/ratio mismatch at d/R={r}") t1 = math.acos(1.0 / r) def int_y2(u, lam=r): return (1.0 + 0.5 * lam * lam) * u - 2.0 * lam * math.sin(u) + 0.25 * lam * lam * math.sin(2.0 * u) area_frac = 1.0 - int_y2(t1) / int_y2(t0) if abs(area_frac - kn["area_above_fraction"]) > 1e-12 or not (0.5 < kn["area_above_fraction"] < 0.62): raise ValueError(f"Prolate loop area_above_fraction mismatch at d/R={r}") if prev_ratio is not None and not (kn["altitude_ratio"] < prev_ratio): raise ValueError(f"Prolate altitude_ratio not strictly decreasing at d/R={r}") if prev_area is not None and not (kn["area_above_fraction"] < prev_area): raise ValueError(f"Prolate area_above_fraction not strictly decreasing at d/R={r}") prev_ratio = kn["altitude_ratio"] prev_area = kn["area_above_fraction"] if abs(knots[0]["altitude_ratio"] - 2.0) > 2e-4: raise ValueError(f"Near-smooth-turn prolate knot ratio failed to approach 2: got {knots[0]['altitude_ratio']}") shallow_area_limit = 1.0 - 2.0 / (3.0 * math.sqrt(3.0)) if abs(knots[0]["area_above_fraction"] - shallow_area_limit) > 2e-5: raise ValueError(f"Shallow prolate loop area fraction failed to approach 1 - 2/(3*sqrt(3)): got {knots[0]['area_above_fraction']}") def run_self_tests(base): mutations = [ ("Misclassify heptagon N=7 as compass-constructible", lambda d: d["polygons"][4].update({"class": "compass"})), ("Misclassify 25-gon (5^2) as origami-constructible", lambda d: d["polygons"][22].update({"class": "origami_unlocked"})), ("Misclassify 49-gon (7^2) as origami-constructible", lambda d: d["polygons"][46].update({"class": "origami_unlocked"})), ("Corrupt Psi_17(x) constant term from +1 to -1", lambda d: d["polygons"][14]["psi_coeffs"].__setitem__(0, -1)), ("Shift Beloch Parabola 1 directrix l1_y on Heptagon preset", lambda d: d["cubic_presets"][0].update({"l1_y": 0.5})), ("Perturb Delian cube root 2^(1/3) by +1e-4", lambda d: d["cubic_presets"][1]["roots"].__setitem__(0, d["cubic_presets"][1]["roots"][0] + 1e-4)), ("Perturb Lill terminal target P4 on Angle Trisection", lambda d: d["cubic_presets"][2]["lill_vertices"][4].__setitem__(1, 9.0)), ("Break N=13 quadratic+cubic Galois tower step 2 root", lambda d: d["galois_towers"]["13"]["steps"][1].update({"value": 1.75})), ("Break N=17 Gauss 3-step compass tower step 1 period A0", lambda d: d["galois_towers"]["17"]["steps"][0].update({"value": 1.50})), ("Break N=19 two-cubic degree-9 tower step 1 period u0", lambda d: d["galois_towers"]["19"]["steps"][0].update({"value": -1.20})), ("Corrupt prolate trochoid knot parameter t0", lambda d: d["trochoid_knots"][3].update({"t0_rad": 0.5})), ("Break near-smooth-turn prolate knot 2:1 limiting ratio", lambda d: d["trochoid_knots"][0].update({"altitude_ratio": 1.5})), ("Corrupt shallow prolate loop area limit 1 - 2/(3*sqrt(3))", lambda d: d["trochoid_knots"][0].update({"area_above_fraction": 0.55})), ("Violate strict monotonicity of prolate altitude_ratio", lambda d: d["trochoid_knots"][4].update({"altitude_ratio": d["trochoid_knots"][3]["altitude_ratio"] + 0.01, "y_knot_over_R": (d["trochoid_knots"][3]["altitude_ratio"] + 0.01) * abs(d["trochoid_knots"][4]["y_min_over_R"])})), ] for idx, (desc, fn) in enumerate(mutations, 1): mut = copy.deepcopy(base) fn(mut) caught = False err_msg = "" try: verify_dataset(mut) except Exception as e: caught = True err_msg = str(e) if not caught: raise AssertionError(f"Self-test {idx} FAILED to catch mutation: {desc}") print(f" [PASS {idx:2d}/14] Caught negative mutation ({desc}) -> {err_msg}") print("ALL 14/14 NEGATIVE-MUTATION SELF-TESTS PASSED!") def main(): path = locate_data() with open(path) as f: D = json.load(f) verify_dataset(D) print("PRIMARY VERIFICATION PASSED: 98-polygon Gauss-Wantzel/Pierpont-Gleason census (24 + 35 + 39), exact Z[x] Chebyshev-Cyclotomic polynomials, Beloch O6 two-parabola cubic folds, Lill's orthogonal laser paths, N=13/17/19 Galois towers, Mascheroni's compass amplifier, and Terra-Flash prolate knot 2:1 limit verified!") if "--self-test" in sys.argv: run_self_tests(D) if __name__ == "__main__": main()