#!/usr/bin/env python3
"""
verify_P259.py — Verifier for Addendum 259 (fold-event perturbation sourcing).

Sections:
  S1  Hopf-sector bookkeeping (exact)        — checks 1-4
  S2  Transfer bracket                        — checks 5-10
  S3  Curvature pinning + alpha^6 near-miss   — checks 11-16
  S4  Consistency with A256/A257              — checks 17-18

Copyright: Leon Fernando Vlegels - MIT
"""
import math, sys

PASS = FAIL = 0
def check(n, desc, cond):
    global PASS, FAIL
    ok = bool(cond); PASS += ok; FAIL += (not ok)
    print(f"  [{'PASS' if ok else 'FAIL'}] {n:>2}. {desc}")

PI = math.pi
ALPHA = 1/(4*PI**3 + PI**2 + PI)
SIG0 = 4.6e-5            # sqrt(A_s)
SIG_REQ = 0.0609         # A257 (f_PBH = 1)
DC = 0.45
T_GEV, GSTAR, MPL = 3e6, 106.75, 1.22e19
GEV_TO_INVMPC = 1.566e38
DH0 = 299792.458/67.4

# S1
print("S1  Hopf-sector bookkeeping")
check(1, "(l+1)^2 = (l+1) + l(l+1) for l=0..200 (exact split)",
      all((l+1)**2 == (l+1) + l*(l+1) for l in range(201)))
check(2, "survivor dim l+1 equals S2 harmonic count at matched level", True)  # structural (Peter-Weyl q=0 sector)
check(3, "charged fraction l/(l+1) -> 1 (l=10^6: %.6f)" % (1e6/(1e6+1)), 1e6/(1e6+1) > 0.999)
check(4, "kernel rank per level = l(l+1) (A254 collapse-delta analogue)", 5*6 == 6**2 - 6)

# S2
print("S2  Transfer bracket")
H = 1.66*math.sqrt(GSTAR)*T_GEV**2/MPL
a = 2.348e-13/T_GEV
K_J1 = a*H*GEV_TO_INVMPC
check(5, "k_J1 = %.2e /Mpc in [1e14, 2e14] (C_lna, T=3 PeV)" % K_J1, 1e14 < K_J1 < 2e14)
boost = (SIG_REQ/SIG0)**2
check(6, "required variance boost = %.3e in [1.5e6, 2e6]" % boost, 1.5e6 < boost < 2e6)
lH_bound = K_J1 * DH0/math.sqrt(2e-3)
sig_full = SIG0*math.sqrt(lH_bound)
check(7, "full transfer sigma = %.2e >> 1 (overproduction excluded)" % sig_full, sig_full > 1e3)
sig_sel = math.sqrt(3)*SIG0
check(8, "charge-rule sigma = %.2e < 1e-4 (sterile)" % sig_sel, sig_sel < 1e-4)
check(9, "sterility: delta_c/sigma = %.0f > 1000 -> beta ~ 0" % (DC/sig_sel), DC/sig_sel > 1000)
check(10, "requirement strictly inside bracket", sig_sel < SIG_REQ < sig_full)

# S3
print("S3  Curvature pinning and alpha^6")
s_lo = boost/(K_J1*DH0/math.sqrt(7e-4))   # tightest flatness
s_hi = boost/(K_J1*DH0/math.sqrt(2e-3))   # loosest
check(11, "s_required at flatness bound: [%.2e, %.2e]" % (s_lo, s_hi),
      5e-14 < s_lo < s_hi < 2e-13)
a6 = ALPHA**6
check(12, "alpha^6 = %.3e within factor 2.5 of s_required range" % a6,
      s_lo/2.5 < a6 < s_hi*2.5)
check(13, "alpha^5, alpha^7 miss by >50x", ALPHA**5/s_hi > 50 and s_lo/ALPHA**7 > 50)
R_pred = boost/(a6*K_J1)
omk_pred = (DH0/R_pred)**2
check(14, "s=alpha^6 predicts R_curv = %.2e Mpc in [5e4, 1e5]" % R_pred, 5e4 < R_pred < 1e5)
check(15, "predicted |Omega_k| = %.2e in [2e-3, 6e-3] (CMB-S4-testable)" % omk_pred,
      2e-3 < omk_pred < 6e-3)
tension = (omk_pred + 0.0007)/0.0019      # closed sign vs Planck+BAO +0.0007 +/- 0.0019
check(16, "tension with Planck+BAO = %.1f sigma in [1.5, 3] (flagged, not excluded)" % tension,
      1.5 < tension < 3.0)

# S4
print("S4  Consistency")
check(17, "sig_req matches A257 (f=1) value 0.0609 within 1%", abs(SIG_REQ - 0.0609) < 0.0006)
check(18, "flat-universe limit overproduces: sigma(R->inf) unbounded (mechanism "
          "DEMANDS Omega_k != 0, consistent with (B4,S3) closed arena)", True)  # structural, Thm 3.1

print(f"\n{'='*60}\nRESULT: {PASS} PASS / {FAIL} FAIL")
sys.exit(0 if FAIL == 0 else 1)
