#!/usr/bin/env python3
"""INDEPENDENT RE-VERIFICATION of Part I (finiteness + alpha-ladder infeasibility).
Re-derives every headline number from scratch; then attacks with a hostile recount:
 (a) does the x43,200 multiplier reproduce? does |A|=8.01e9?
 (b) is the alpha-formula monotone / is exactly one k hit-capable?
 (c) HOSTILE ladder recount: charge the ~105 Fano 3-fold families (Mori-Mukai) that
     attack 3 claims are 'folded into the ladder budget' but attack 8's arithmetic omits;
     also charge denser-formula variants. Does the infeasibility conclusion survive?
 (d) check E5's '~4000x' vs ledger 'x140' margin statements.
"""
from math import comb, pi
from fractions import Fraction
import json

out = {}

# ---------- (a) multiplier + |A| reproduction (independent arithmetic) ----------
# attack 7 prefactor completion: distinct (a/b, m) values
vals = {(Fraction(a, b), m) for a in (1, 2, 3, 4) for b in (1, 2, 4, 8) for m in (-1, 0, 1)}
assert len(vals) == 30, len(vals)
mults = [10, 12, 3, 6, 3, 2, Fraction(len(vals), 9)]
M = 1
for m in mults: M *= m
out["multiplier"] = float(M)                       # expect 43200
E2core, E2full = 185_472, 47_480_832
out["A_core"] = float(E2core * M)                  # expect 8.0124e9
out["A_full"] = float(E2full * M)                  # expect 2.0512e12
# E2 core independent rebuild: 92 micro-pairs x gauge7 x CKM8 x grammar9 x chi-state4
CAP = 195
alt_k = [6, 8, 10, 24, 78, 96, 195]
pairs = {(comb(q1 * ng + 2, 2) + n, ng) for q1 in (1, 3, 6) for ng in range(1, 10)
         for n in (5, 6) if comb(q1 * ng + 2, 2) + n <= CAP}
pairs |= {(k, ng) for k in alt_k for ng in range(1, 10) if k <= CAP}
out["micro_pairs"] = len(pairs)                    # expect 92
out["E2_core_rebuilt"] = len(pairs) * 7 * 8 * 9 * 4
assert out["E2_core_rebuilt"] == E2core
assert E2full == E2core * 16 * 16

# ---------- (b) alpha ladder: monotonicity + hit capability ----------
ALPHA = 137.035999084
def alpha_inv(k):
    d = k + 4
    return (pi ** 1.5 / 24) * 10 * k * (k + 3) / (k + 4) * (1 + 7 / (80 * (d * d - 1)))
mono = all(alpha_inv(k + 1) > alpha_inv(k) for k in range(1, 5000))
hitcap_1pct = [k for k in range(1, 5001) if abs(alpha_inv(k) / ALPHA - 1) < 0.01]
ppm = abs(alpha_inv(60) - ALPHA) / ALPHA * 1e6
step = alpha_inv(61) - alpha_inv(60)
out["monotone_to_5000"] = mono
out["hit_capable_k_within_1pct"] = hitcap_1pct     # expect [60] only
out["ppm_match_at_60"] = ppm                       # expect 0.0056
out["step"] = step                                 # expect 2.318

# ---------- (c) ladder budget: reproduction + HOSTILE recount ----------
p_ladder = 2 * ALPHA * (ppm * 1e-6) / step         # window-as-good-as-DFD / rung spacing
out["p_ladder"] = p_ladder                         # expect 6.6e-7
n_G8d = 10 * 10 * 12 * 3 * 3                       # = 10,800 as booked in E5 attack 8
out["exp_false_G8d"] = p_ladder * n_G8d            # expect 7.2e-3
out["ladders_needed"] = 1 / p_ladder               # expect 1.5e6
out["margin_G8d"] = (1 / p_ladder) / n_G8d         # expect ~139 (ledger says x140; E5 prose says ~4000x -- check)

# HOSTILE: dim-9 opens ~105 Fano 3-fold deformation families (Mori-Mukai, a THEOREM,
# so still finite/countable-by-classification). attack 3 said 'folded into ladder budget';
# attack 8's 10,800 = 10 surfaces x 10 rays x 12 lens x 3 dims x 3 formulas does NOT
# contain a 105. Charge them fully: at dim 9 the surface coordinate is 105 families
# (instead of 10 surfaces), same 10 ray-truncation, 12 lens, 3 formulas.
n_dim7 = 10 * 10 * 12 * 1 * 3
n_dim9 = 105 * 10 * 12 * 1 * 3
n_dim11 = 105 * 10 * 12 * 1 * 3                    # 4-fold families not classified; reuse 105 as proxy floor
n_hostile = n_dim7 + n_dim9 + n_dim11
out["n_hostile"] = n_hostile
out["exp_false_hostile"] = p_ladder * n_hostile
out["margin_hostile"] = (1 / p_ladder) / n_hostile
# EXTRA-HOSTILE: also allow x5 denser rival ladders (smaller effective rung spacing)
out["exp_false_extra_hostile_x5_density"] = p_ladder * n_hostile * 5
out["margin_extra_hostile"] = (1 / p_ladder) / (n_hostile * 5)

# ---------- (d) internal consistency of prose ----------
out["E5_prose_4000x_check"] = {"1.5e6/10800": (1 / p_ladder) / 10800,
                               "1.5e6/360(surf*lens*dim)": (1 / p_ladder) / 360,
                               "verdict": "E5 prose '~4000x the honesty-guard menu' only works vs the 360-menu; "
                                          "vs its own 10,800 ladder count the margin is ~x140 (ledger states x140 -- correct)"}

# ---------- 15-dimension bookkeeping ----------
E2_dims = ["q1", "gauge_partition", "N_gen", "padding_n", "assembly_rule", "CKM_E1",
           "closure_grammar", "sign_bits", "axiom_adoption_bits", "chi_primordial_state"]
E5_new = ["Mc_surface", "Mg_lens_quotient", "dimX", "alpha_formula_grammar", "subtraction_rule"]
E5_extending = {"Wolfenstein_basis": "extends CKM_E1", "prefactor_completion": "extends closure_grammar",
                "TrY2_bit": "inside alpha_formula_grammar"}
out["dimension_count"] = {"E2": len(E2_dims), "E5_new": len(E5_new), "total": len(E2_dims) + len(E5_new),
                          "extending_not_new": E5_extending}

print(json.dumps(out, indent=1, default=str))
json.dump(out, open("verify3x/partI_verify.json", "w"), indent=1, default=str)
