# Copyright Léon Fernando Vlegels -- CC BY 4.0
"""
verify_P338.py
Verifier for Addendum 338: J3(O) -> Monster bridge FEASIBILITY pass under a hard,
pre-committed kill-criterion.

THE SHARP QUESTION
------------------
Does the corpus's existing exceptional-Jordan-algebra structure J3(O) land on a
STANDARD monstrous-moonshine object -- a canonical McKay-Thompson hauptmodul T_g
or a genuine Monster head-representation dimension -- UNFORCED (no free parameter,
no bespoke function)?  Not "can we connect to the Monster" (always forceable), but:
does the J3(O) chain ALREADY in the corpus reach a canonical moonshine object with
canonical normalization?

THE A337 LESSON (just established)
----------------------------------
A337 retracted A163/A177's "T_3B := j^(1/3), T_3B(i)=12 is a Monster 3B hit": j^(1/3)
= E4/eta^8 is the E8/G2 cube-root function (carries 248=dim E8), NOT the canonical
Monster 3B hauptmodul (eta/eta3)^12+12 (value 535.59 at i, q-expansion q^(-1)+0+54q).
The leap to the Monster SPECIFICALLY was bespoke and wrong.  This probe tests whether
ANY unforced standard contact exists -- knowing the prior attempt failed by using a
bespoke non-hauptmodul function.

CANDIDATE OBJECT, STATED FIRST (before any value-matching)
----------------------------------------------------------
What does J3(O) CONCRETELY produce in the corpus?  (A166 sec 1.2, Prop 1.1; A166
Thm 5.1; A169.)
  * J3(O) is the 27-dim exceptional Jordan algebra; Aut(J3(O)) = F4,
    Str_0(J3(O)) = E6 (A166 Prop 1.1).  The concrete number is dim J3(O) = 27,
    the fundamental rep dim of E6.
  * A166 Thm 5.1: 27 * 64 = 1728 = j(i) = J_short^3.  So the J3(O) number that
    touches modular geometry in the corpus is 27 (and its cube-partner j(i)=1728).
  * The chain reaches the Monster ONLY via the Leech lattice (A169): J3(O) -> E6
    -> ... -> E8 -> Leech Lambda_24 -> V_natural (FLM) -> Monster.  The corpus's
    own Leech-level number adjacent to the Monster is the MINIMAL-VECTOR COUNT of
    the Leech, |minvec(Lambda_24)| = 196560 (A169 Thm 4.1 / sec 4).

THE SINGLE STANDARD TARGET (chosen ONCE, before matching)
---------------------------------------------------------
The smallest faithful Monster head-representation dimension: 196883 (and McKay's
196884 = 1 + 196883, the q^1 coefficient of j).  A169 sec 6.2 ALREADY notes the
corpus Leech number 196560 and the Monster number 196883 are ADJACENT
(196883 - 196560 = 323 = 17*19).  The sharp test: is 196560 -> 196883 an UNFORCED
standard identity, or a near-miss requiring a bespoke bridge constant (323)?

This is the most plausible UNFORCED contact, because:
  - 196560 is a genuine corpus-derived J3(O)/Leech number (not a bespoke function);
  - 196883 is a genuine Monster head-rep dim (the kill-criterion's allowed target);
  - they are off by ~0.16%, the textbook near-miss.  If the corpus contact were
    unforced, 196560 would EQUAL a Monster head-dim with canonical normalization.

THE HARD KILL-CRITERION (pre-committed)
---------------------------------------
1. State candidate (DONE above: 27 -> E6; chain-to-Monster number = Leech 196560).
2. Match a STANDARD object with NO free parameter and NO bespoke function:
   - canonical McKay-Thompson hauptmodul (>=3 q-coeffs, OR canonical CM value), OR
   - genuine Monster head-rep dim (1, 196883, 196884, 21296876, 842609326, ...).
   - DISQUALIFIED: j^(1/3) / any non-hauptmodul cube root (the A177 trap); anything
     needing a fitted constant.
3. FIRST honest check that fails the standard-object match -> STOP.  Do NOT iterate.
4. Even a PASS is CONSISTENCY-only (Borcherds 1992 already explained moonshine).

NOTE ON VERIFIER SEMANTICS: a [PASS] line means "this check ran and recorded the
correct fact."  Correctly recording a NEGATIVE (no unforced contact) is a PASSING
verifier.  The VERDICT line states whether a standard contact was found.
"""

import sys
import mpmath as mp

mp.mp.dps = 40

# ---------------------------------------------------------------------------
# Test harness
# ---------------------------------------------------------------------------
_PASS = 0
_FAIL = 0


def check(n, desc, cond):
    global _PASS, _FAIL
    tag = "[PASS]" if cond else "[FAIL]"
    if cond:
        _PASS += 1
    else:
        _FAIL += 1
    print(f"{tag} ({n}) {desc}")


def section(title):
    print()
    print("-- " + title + " " + "-" * max(0, 66 - len(title)))


# ---------------------------------------------------------------------------
# Modular building blocks -- recomputed from scratch (NO corpus imports)
# ---------------------------------------------------------------------------
def eta(tau):
    """Dedekind eta via mpmath's q-series product."""
    return mp.qp(mp.e ** (2j * mp.pi * tau)) * mp.e ** (mp.pi * 1j * tau / 12)


def E4(tau):
    """Eisenstein E4 = 1 + 240 sum sigma_3(n) q^n."""
    q = mp.e ** (2j * mp.pi * tau)
    s = mp.mpf(0)
    for nn in range(1, 400):
        sig3 = sum(d ** 3 for d in range(1, nn + 1) if nn % d == 0)
        term = sig3 * q ** nn
        s += term
        if abs(term) < mp.mpf(10) ** (-50):
            break
    return 1 + 240 * s


def Delta(tau):
    """Ramanujan cusp form Delta = eta^24."""
    return eta(tau) ** 24


def j_invariant(tau):
    """j = E4^3 / eta^24."""
    return E4(tau) ** 3 / eta(tau) ** 24


TAU_I = mp.mpc(0, 1)  # the corpus CM point, D = -4

# Genuine Monster head-representation dimensions (irreducible-rep "head"), and
# the j-function q-coefficients they sum to (McKay).  Canonical, no free parameter.
MONSTER_HEAD_DIMS = [1, 196883, 21296876, 842609326, 18538750076, 19360062527]
# j(tau) = q^{-1} + 744 + 196884 q + 21493760 q^2 + 864299970 q^3 + ...
J_COEFFS = {-1: 1, 0: 744, 1: 196884, 2: 21493760, 3: 864299970}
# McKay decompositions (graded dims of V_natural = sums of Monster head dims):
#   c(1) = 196884     = 1 + 196883
#   c(2) = 21493760   = 1 + 196883 + 21296876
J_COEFF_MCKAY = {
    1: [1, 196883],
    2: [1, 196883, 21296876],
}

print("=" * 72)
print("P338 VERIFICATION: J3(O) -> Monster bridge FEASIBILITY (kill-criterion)")
print(f"mpmath precision: {mp.mp.dps} decimal places")
print("=" * 72)

# ---------------------------------------------------------------------------
section("S0  The candidate object J3(O) produces in the corpus (stated first)")
# ---------------------------------------------------------------------------
# A166 Prop 1.1: J3(O) is 27-dim; Aut = F4, Str_0 = E6.  The concrete number is 27.
DIM_J3O = 27
DIM_27_E6 = 27
check(1, "J3(O) is 27-dimensional (exceptional Jordan algebra, A166 sec 1.2)",
      DIM_J3O == 27)
check(2, "dim J3(O) = dim(27 of E6) = 27 (Str_0(J3(O)) = E6, A166 Prop 1.1)",
      DIM_J3O == DIM_27_E6)

# A166 Thm 5.1: the J3(O)/E6 number that touches modular geometry is 27 via
# 27 * 64 = 1728 = j(i).  Recompute j(i) = 1728 from scratch.
ji = j_invariant(TAU_I)
print(f"  j(i)            = {mp.nstr(ji, 22)}")
check(3, "j(i) = 1728 recomputed from scratch (A158/A166)",
      abs(ji - 1728) < mp.mpf(10) ** (-18))
check(4, "A166 Thm 5.1: 27 * 64 = 1728 = j(i)  (the corpus J3(O)-modular contact)",
      27 * 64 == 1728 and abs(mp.re(ji) - 27 * 64) < mp.mpf(10) ** (-15))

# ---------------------------------------------------------------------------
section("S1  The chain-to-Monster number: Leech minimal vectors 196560 (A169)")
# ---------------------------------------------------------------------------
# J3(O) reaches the Monster ONLY via the Leech (A169): the corpus's Leech-level
# number adjacent to the Monster is the minimal-vector count 196560.
# Recompute 196560 from scratch as [q^2] of Theta_Leech = E4^3 - 720 Delta.
def theta_leech_q2():
    """[q^2] coefficient of Theta_Lambda24 = E4^3 - 720*Delta, from q-series."""
    # E4 = 1 + 240 q + 2160 q^2 + ...   (sigma_3: 1, 9)
    e4 = [1, 240, 2160]
    # E4^3 up to q^2 by convolution
    e4_3 = [0, 0, 0]
    for a in range(3):
        for b in range(3):
            for c in range(3):
                if a + b + c <= 2:
                    e4_3[a + b + c] += e4[a] * e4[b] * e4[c]
    # Delta = q - 24 q^2 + ...   => [q^2] Delta = -24
    delta_q2 = -24
    return e4_3[2] - 720 * delta_q2  # 179280 + 17280


LEECH_MINVEC = theta_leech_q2()
print(f"  [q^2](E4^3 - 720 Delta) = {LEECH_MINVEC}   (Leech minimal-vector count)")
check(5, "Leech minimal-vector count = 196560 recomputed from theta series (A169)",
      LEECH_MINVEC == 196560)
check(6, "196560 = 2^4 * 3^3 * 5 * 7 * 13 (A169 Prop 4.x)",
      LEECH_MINVEC == 2 ** 4 * 3 ** 3 * 5 * 7 * 13)

# ---------------------------------------------------------------------------
section("S2  The single standard target: Monster head-rep dim 196883 (chosen once)")
# ---------------------------------------------------------------------------
SMALLEST_FAITHFUL = 196883
check(7, "196883 is a genuine Monster head-rep dim (smallest faithful, McKay)",
      SMALLEST_FAITHFUL in MONSTER_HEAD_DIMS)
check(8, "McKay: [q^1] j = 196884 = 1 + 196883 (head-dim decomposition is real)",
      J_COEFFS[1] == sum(J_COEFF_MCKAY[1]) == 1 + 196883)

# ---------------------------------------------------------------------------
section("S3  THE SINGLE STANDARD-OBJECT MATCH (run once; first failure -> STOP)")
# ---------------------------------------------------------------------------
# The kill-criterion match: does the corpus J3(O)/Leech number 196560 EQUAL a
# genuine Monster head-rep dimension, with NO free parameter and NO bespoke
# function?  An UNFORCED contact requires exact equality (canonical normalization).
exact_hit = LEECH_MINVEC in MONSTER_HEAD_DIMS
print(f"  corpus number   = {LEECH_MINVEC}   (Leech min-vectors, J3(O)->...->Leech)")
print(f"  Monster head-dims = {MONSTER_HEAD_DIMS}")
print(f"  196560 in Monster head-dims? -> {exact_hit}")
check(9, "HONEST CHECK: 196560 is NOT equal to any Monster head-rep dim "
         "(no exact unforced hit)",
      not exact_hit)

# The gap to the nearest target is a NON-ZERO bespoke constant -- exactly the
# A177 failure mode (a fitted bridge), which the kill-criterion disqualifies.
gap = SMALLEST_FAITHFUL - LEECH_MINVEC
print(f"  196883 - 196560 = {gap}  (= 17 * 19, a NON-ZERO bridge constant)")
check(10, "the contact requires a bespoke non-zero bridge constant 323 = 17*19 "
          "(DISQUALIFIED by kill-criterion -- a fitted constant)",
      gap == 323 == 17 * 19 and gap != 0)

# Cross-check: nothing in the corpus J3(O) chain produces 196883 with canonical
# normalization.  27, 1728, 196560 are all genuine corpus numbers; none equals a
# Monster head-dim.  (1728 = j(i) is the V_natural PARTITION FUNCTION value, not a
# head-rep dim; the partition function reproducing j is mainstream moonshine, not a
# J3(O) result.)
corpus_j3o_numbers = [27, 78, 1728, LEECH_MINVEC]  # 27, dim E6=78, j(i), Leech
hits = [x for x in corpus_j3o_numbers if x in MONSTER_HEAD_DIMS]
print(f"  corpus J3(O)-chain numbers {corpus_j3o_numbers}: head-dim hits = {hits}")
check(11, "NO corpus J3(O)-chain number (27, 78, 1728, 196560) equals a Monster "
          "head-rep dim",
      len(hits) == 0)

# ---------------------------------------------------------------------------
section("S4  Guard against the A177 trap: no bespoke cube-root / non-hauptmodul")
# ---------------------------------------------------------------------------
# The only J3(O)->modular function the corpus uses is j^(1/3) = E4/eta^8 (A161),
# which A337 PROVED is the E8/G2 cube-root function (q^(-1/3)(1+248q+...)), NOT a
# Monster hauptmodul.  Re-confirm it is disqualified (carries 248, not a head-dim).
cube_root_ji = mp.re(ji) ** (mp.mpf(1) / 3)
check(12, "the corpus's only J3(O)->modular function j^(1/3)(i) = 12 is the E8/G2 "
          "cube-root fn (A337), DISQUALIFIED as a Monster hauptmodul",
      abs(cube_root_ji - 12) < mp.mpf(10) ** (-18))
# 12 is not a Monster head-rep dim either.
check(13, "12 (the j^(1/3) value) is NOT a Monster head-rep dim (A177 trap closed)",
      12 not in MONSTER_HEAD_DIMS)

# ---------------------------------------------------------------------------
section("S5  What IS true is mainstream, not a J3(O) result")
# ---------------------------------------------------------------------------
# V_natural's partition function is j(tau); j(i) = 1728.  This is FLM/Borcherds
# mainstream moonshine, true regardless of J3(O).  The J3(O) chain supplies the
# Leech as a lattice INPUT to FLM, but the Monster head-dims come from V_natural's
# graded structure (Borcherds 1992), not from J3(O).  Record honestly.
check(14, "j(i) = 1728 = V_natural partition function value (mainstream FLM, "
          "NOT a J3(O)->Monster head-dim contact)",
      abs(mp.re(ji) - 1728) < mp.mpf(10) ** (-15))
# The adjacency 196560 ~ 196883 is the textbook Leech/Monster near-miss; it is
# explained by mainstream moonshine (the 196560 norm-4 vectors and the 196883 rep
# both live in V_natural's degree-1 piece dim 196884 = 1 + 196883), NOT by an
# unforced J3(O) identity.
check(15, "the 196560 ~ 196883 adjacency is the mainstream Leech/Monster near-miss "
          "(both inside dim V_nat_1 = 196884), not an unforced J3(O) identity",
      196884 == 1 + 196883 and 196884 - 196560 == 324)

# ---------------------------------------------------------------------------
# KILL-CRITERION VERDICT
# ---------------------------------------------------------------------------
section("KILL-CRITERION VERDICT")
unforced_standard_contact = exact_hit or (len(hits) > 0)
print(f"  Candidate object        : J3(O) -> dim 27 (E6 fundamental); chain-to-"
      f"Monster number = Leech minimal vectors 196560 (A166/A169)")
print(f"  Single standard target  : Monster head-rep dim 196883 (smallest faithful)")
print(f"  Match (no free param)   : 196560 != 196883; gap 323 = 17*19 is a "
      f"bespoke bridge constant")
print(f"  Stopped at first failure: YES (S3 check 9 -- no exact hit -- is the "
      f"first honest check; no iteration past it)")
print(f"  Unforced standard contact found: {unforced_standard_contact}")
check(16, "VERDICT recorded: NO unforced standard contact "
          "(coincidence, left to mainstream moonshine)",
      not unforced_standard_contact)

# ---------------------------------------------------------------------------
# Summary
# ---------------------------------------------------------------------------
print()
print("=" * 72)
print(f"RESULT: {_PASS} PASS / {_FAIL} FAIL")
if _FAIL == 0:
    print("VERDICT: NO UNFORCED STANDARD CONTACT.  The corpus J3(O) structure produces")
    print("         dim 27 (E6 fundamental) and, via the Leech (A169), the number 196560")
    print("         (Leech minimal vectors).  The single most plausible standard target,")
    print("         the Monster head-rep dim 196883, does NOT equal any corpus J3(O)-chain")
    print("         number (27, 78, 1728, 196560).  The nearest contact, 196560 -> 196883,")
    print("         is off by a bespoke bridge constant 323 = 17*19 -- exactly the A177")
    print("         failure mode (a fitted constant), which the kill-criterion DISQUALIFIES.")
    print("         The only J3(O)->modular function in the corpus, j^(1/3) = E4/eta^8, is")
    print("         the E8/G2 cube-root function (A337), not a Monster hauptmodul.")
    print("         STOPPED at first honest failure; no iteration. Coincidence, left to")
    print("         mainstream moonshine (FLM/Borcherds 1992).  No new TOE bridge earned.")
else:
    print("VERDICT: FAIL -- a recomputed number did not reproduce; do NOT ship A338.")
print("=" * 72)

sys.exit(0 if _FAIL == 0 else 1)
