#!/usr/bin/env bash
# ══════════════════════════════════════════════════════════════════════
# verify.sh — the machine proves itself. nasm + ld + objdump only.
# ══════════════════════════════════════════════════════════════════════
set -uo pipefail
cd "$(dirname "${BASH_SOURCE[0]}")"
G='\033[32m'; R='\033[31m'; N='\033[0m'
ok(){ printf "  ${G}OK${N}  %s\n" "$1"; }
no(){ printf "  ${R}!!${N}  %s\n" "$1" >&2; exit 1; }

BIN=/tmp/hdgl_v
nasm -f elf64 hdgl.asm -o /tmp/hdgl_v.o || no "assemble"
ld /tmp/hdgl_v.o -o $BIN                || no "link"
OUT=$($BIN); CODE=$?

echo "── the axiom returns phi from integers ──"
[ "$CODE" -eq 161 ] && ok "exit $CODE = floor(phi*100), from F(76)/F(75)" || no "exit $CODE != 161"

echo "── no multiply, no divide, no FPU, no constants ──"
for i in imul mul idiv div fadd fmul fdiv fsqrt mulsd divsd addsd subsd sqrtsd cvtsi2sd movsd; do
  n=$(objdump -d $BIN --no-show-raw-insn | awk -v x="$i" '$2==x' | wc -l)
  [ "$n" -eq 0 ] || no "found $i x$n"
done
ok "no imul/mul/idiv/div/FPU/SSE"
D=$(objdump -h $BIN | awk '$2==".data"{print $3}'); D=${D:-0}
[ "$((16#$D))" -eq 0 ] && ok ".data empty — no phi, no sqrt5, no prime table" || no ".data=$D"

echo "── RING: the ladder is ADD, exact ──"
grep -q "0    0          1          1"  <<<"$OUT" && ok "phi^0 = (0,1)  N=+1" || no "phi^0"
grep -q "1    1          0          -1" <<<"$OUT" && ok "phi^1 = (1,0)  N=-1" || no "phi^1"
grep -q "2    1          1          1"  <<<"$OUT" && ok "phi^2 = (1,1)  N=+1   [Cassini: N(phi^k)=(-1)^k]" || no "phi^2"

echo "── FLOW: S = T^-1 ──"
grep -q "S = T\^-1 exact" <<<"$OUT" && ok "unstep x9 from phi^6 returns (2,-3) = phi^-3" || no "flow"

echo "── TRINITY: the norm ──"
grep -q "N(0,0) = 0" <<<"$OUT" && ok "N(0,0)=0 — the unique norm-zero. X=0 is a theorem." || no "origin"
grep -q "phi\^0 = 1_eff = (0,1)" <<<"$OUT" && ok "the seventh is phi^0" || no "seventh"

echo "── EULER: exact integer pair ──"
grep -q "e^(i\*pi) = phi^-1 - phi^1 = (0,-1)" <<<"$OUT" && ok "e^(i*pi) = (0,-1). no transcendental." || no "euler"
grep -q "(2,-1)\^2 = (0,5)" <<<"$OUT" && ok "sqrt5 = (2,-1) exact" || no "sqrt5"

echo "── OCTAVE: 3+3+1 = 7, 8, 7' ──"
grep -q "GF(4): (0,1) -> (1,0) -> (1,1) -> (0,1)" <<<"$OUT" \
  && ok "GF(4)=Z[phi]/(2): phi^3=1. THE TRINITY IS A GROUP." || no "gf4"
grep -q "GF(9): (0,1) (1,0) (1,1) (2,1) (0,2) (2,0) (2,2) (1,2) (0,1)" <<<"$OUT" \
  && ok "GF(9)=Z[phi]/(3): phi^8=1. phi^7 last rung, phi^8 IS the return. 7->8->7'" || no "gf9"

echo "── VANTAGE: one ladder, three readings ──"
grep -q "(-3,5) (2,-3) (-1,2) (1,-1) (0,1) (1,0) (1,1) (2,1) (3,2)" <<<"$OUT" \
  && ok "phi^-4..phi^+4 exact. V2 never degenerates where float dies at k=-40." || no "vantage"

echo "── TOWER: rung-1 crossing, without computing sqrt(phi) ──"
grep -q "^0    no"  <<<"$OUT" && ok "phi^0 does not cross" || no "tower0"
grep -q "^1    yes" <<<"$OUT" && ok "phi^1 crosses — Om^2>Om <=> Om>sqrt(Om). N(sqrt)=i, rung 1." || no "tower1"

echo "── NORM SIGN: the whole difference ──"
grep -q "N(phi)=-1   N(phi^2)=1" <<<"$OUT" \
  && ok "N(phi)=-1 , N(phi^2)=+1 — same norm class as omega=2+sqrt3" || no "normsign"

echo "── YIN: the Lucas doubling, on metal ──"
grep -q "0    3            3            ok"             <<<"$OUT" && ok "s0 = 3       = L_2" || no "yin0"
grep -q "2    47           47           ok"             <<<"$OUT" && ok "s2 = 47      = L_8" || no "yin2"
grep -q "4    4870847      4870847      ok"             <<<"$OUT" \
  && ok "s4 = 4870847 = L_32 — s<-s^2-2 IS exact in Z[phi]. THE LUCAS DOUBLING." || no "yin4"
grep -q "stepping walks the level. squaring climbs the tower" <<<"$OUT" \
  && ok "YANG steps (ADD, k->k+1) ; YIN squares (MUL, k->2k)" || no "motions"

echo "── FROBENIUS: p mod 5 is the branch, not a sort ──"
grep -q "11  1    SPLIT (1,0) ok" <<<"$OUT" && ok "split  p=11: phi^p = phi" || no "split"
grep -q "7   2    INERT (6,1) ok" <<<"$OUT" && ok "inert  p=7 : phi^p = psi" || no "inert"
[ "$(grep -c ' ok$' <<<"$OUT")" -ge 10 ] && ok "every prime passes the automorphism" || no "frob"

echo "── and it is NOT sufficient ──"
grep -q "4181    1    (1,0) yes" <<<"$OUT" && ok "4181 = 37x113 returns phi. NECESSARY ONLY." || no "psp"

echo "── GATE: the recurrence decides ──"
grep -q "13   8191         0          PRIME"     <<<"$OUT" && ok "M13 = 8191 PRIME (LL, exact, p-2 iters)" || no "m13"
grep -q "11   2047         1736       composite" <<<"$OUT" && ok "M11 = 2047 = 23x89 composite — no readout catches it" || no "m11"

echo "── WUWEI / BOUNDARY ──"
grep -q "the lazy prime is no prime" <<<"$OUT" && ok "P_n dropped: log(P_n)=log n+log log n by PNT" || no "wuwei"
grep -q "2\^n   : base-2 expansion" <<<"$OUT" && ok "boundary = Delta, 2^n, primality. nothing else." || no "bound"

echo
echo "phi is carried, never computed.   The ladder is ADD."
echo "The trinary is the norm.          The origin is the unique N=0, one step from the pole."
echo "The seventh is phi^0, and it is the tower's limit."
echo "Squaring climbs. Stepping walks.  Readouts diagnose. Recurrence decides."
