# conscious
*by [zchg.org](https://zchg.org)*

> A self-correcting spectral Markov dynamical sieve and Mersenne prime engine
> running on GPU warps, CPU phi-lattice math, and dual interactive TUIs.

GPU target: **RTX 2060 12 GB** (CUDA sm_75).  Folder: `primes/`.

---

## Simple Guide

### What It Is

**conscious** is a Windows-native prime-research workbench with three interlocking layers:

1. **`prime_ui.exe`** — A full-screen TUI (8 modules) for prime mathematics: pipeline
   sieve, Mersenne LL verifier, zeta-zero viewer, phi-lattice explorer, and a
   live Alpine Linux shell — all driven from one keyboard menu.

2. **`conscious.exe`** — The CUDA GPU engine.  Runs dual-slot λₖ–σ spectral resonance
   (Markov trit gating) and schoolbook warp-parallel squaring for Lucas-Lehmer
   verification on sm_75 hardware.

3. **Slot4096 Alpine OS** — A Docker container whose *entire configuration* —
   hostname, packages, timezone, user UID, shell limits, ulimits — is derived
   deterministically from a single 4096-slot phi-irrational lattice.  No config
   files, no cloud provisioning — just math.

---

### Quick Start

**Prerequisites:** MSYS2/clang, CUDA toolkit (sm_75), MSVC 2017+, Docker Desktop.

#### 1. Build the TUI

```bat
cmd /c build_prime_ui.bat
```
Expected: `[OK] Built: prime_ui.exe`

#### 2. Build the GPU engine (optional, needs CUDA)

```bat
cmd /c build_conscious.bat
```

#### 3. Run

```bat
.\prime_ui.exe
```

#### 4. First-time Alpine setup — menu `[6]`

Select **[6] Alpine OS Install** from the main menu.  This:
- Seeds the 4096-slot phi-lattice (50 Weyl resonance steps)
- Derives all OS parameters from lattice slots 0–22
- Writes `lattice_init.sh`, `lattice_entropy.bin`, `lattice_state.env` to the
  current directory
- Prints the full derived OS manifest (hostname, mirror, packages, tz, uid…)

#### 5. Launch the lattice-powered container — menu `[8]`

Select **[8] Alpine OS Shell**.

- If no container exists → boots a fresh Alpine instance using `docker run`,
  runs the 14-step `lattice_init.sh` inside it, and drops you into a
  `slot4096@phi4096-XXXXXXXX:~$` prompt.
- If the container is already running (shown as `[live: phi4096-lattice]` in the
  menu) → re-attaches instantly with `docker exec`.

#### 6. Detach without stopping

```
Ctrl-P  Ctrl-Q
```

The container keeps running.  Menu `[8]` will show the live badge and re-attach
on the next selection.

#### 7. Stop the container

```sh
# inside the container
exit
```
The `docker rm` cleanup runs automatically after `prime_ui` regains control.

---

### How It Works

```
prime_ui.exe
    │
    ├─[1-5,7]  Pure-C prime math (sieve, LL, zeta, benchmark)
    │
    └─[6] Alpine Install
           │
           ├── lattice_seed()          seed[0..4095] = φ-Weyl irrational spacing
           ├── 50× resonance steps     Kuramoto-style slot coupling
           ├── lattice_derive_*()      slots[0..22] → hostname/mirror/pkgs/tz/uid/…
           ├── write lattice_init.sh   14-step Alpine boot script (Unix LF, "wb")
           ├── write lattice_entropy.bin  32 KB of lattice-derived entropy
           └── write lattice_state.env    KEY=VALUE for env passthrough

       [8] Alpine OS Shell
           │
           ├── docker inspect phi4096-lattice   → running? exists?
           ├── if running  → docker exec -it … su - slot4096
           └── if not      → docker run -it --name phi4096-lattice
                               --cap-add SYS_ADMIN
                               -e LATTICE_N / LATTICE_STEPS / LATTICE_SEED
                               -v lattice_init.sh:/lattice/init.sh:ro
                               -v lattice_entropy.bin:/lattice/entropy.bin:ro
                               -v lattice_state.env:/lattice/state.env:ro
                               alpine sh /lattice/init.sh
```

Inside the container `lattice_init.sh` runs 14 ordered steps:

| Step | Action |
|------|--------|
| 1 | Set hostname from `LATTICE_SEED` derivation |
| 2 | Select APK mirror (one of 8 CDN choices, slot[4]) |
| 3 | `apk update` |
| 4 | Install lattice-selected packages (slots[5..15] bit-select from 11 candidates) |
| 5 | Set timezone (slot[16] → UTC offset) |
| 6 | Create user `slot4096` with lattice-derived UID (slot[18] → 1000–9999) |
| 7 | Set `nice` priority (slot[17] → −20..+19) |
| 8 | Write `/etc/profile.d/lattice.sh` (ulimit/umask/HISTSIZE/TMOUT from slots[19..22]) |
| 9 | Write `/etc/motd` — 64-char ANSI box with seed, slots, hostname |
| 10 | Write `/etc/issue` — seed + parameters banner |
| 11 | Write `/home/slot4096/.profile` (sources `/etc/profile.d/lattice.sh`) |
| 12 | Verify packages are present (`command -v`) |
| 13 | Print final confirmation lines |
| 14 | `su - slot4096` → interactive shell |

---

### Under the Hood

#### Phi-Weyl Lattice Seeding

The lattice is seeded by irrational Weyl spacing — the fractional parts of
`k·φ` (golden ratio, φ ≈ 1.6180339887) for k = 1..4096.  Because φ is
maximally irrational, successive values never cluster; the 4096-slot array
covers [0,1) almost uniformly.  After seeding, 50 Kuramoto-style coupling
steps diffuse entropy across all slots so local structure dissolves.

The seed extracted for Docker env-vars is a 64-bit XOR-fold over slots[0..63].

#### Slot → OS Parameter Derivation

Each OS parameter is a pure function of one or more lattice slots:

| Parameter | Slots | Method |
|-----------|-------|--------|
| Hostname suffix | 0–3 | XOR-fold → 8 hex chars |
| APK mirror | 4 | `floor(slot*8)` → index into 8 CDN strings |
| Package list | 5–15 | bit `i` set if `slot[5+i] > 0.5` |
| Timezone | 16 | `floor(slot*25)−12` → `Etc/GMT±n` |
| nice | 17 | `floor(slot*40)−20` |
| UID | 18 | `1000 + floor(slot*9000)` |
| ulimit -n | 19 | `1024 + floor(slot*64512)` |
| umask | 20 | index into `{002,007,022,027}` |
| HISTSIZE | 21 | `500 + floor(slot*9000)` |
| TMOUT | 22 | `300 + floor(slot*3300)` |

The same seed always yields the same container — the OS is a mathematical
consequence of the lattice state, not a configuration decision.

#### GPU: Warp-Parallel Schoolbook Squaring (`k_sqr_warp`)

Lucas-Lehmer requires repeated squaring of a ~p-bit integer mod 2^p−1.
`k_sqr_warp` assigns a full 32-thread warp to each output limb.  Each thread
accumulates partial products for its limb, then `__shfl_down_sync` reduces
the 32 partial sums to a single limb value — no global memory round-trip.
Mersenne fold (`s mod 2^p−1 = hi + lo`) is applied inline.

No cuFFT is used.  Schoolbook keeps integers exact to the bit.

#### CPU: Sequential Carry (HDGL wu-wei)

After each squaring kernel, the CPU performs the carry-propagation pass.
On RTX 2060 hardware, sequential carry on CPU runs 10–12× faster than any
GPU thread-parallel implementation — the GPU's strength is the parallel
multiply; the CPU's strength is the sequential dependency chain.  Both do
their natural job: **wu-wei** (acting in accordance with nature).

#### Dual-Slot λₖ–σ Fused Engine (`conscious_fused_engine.cu`)

A single CUDA module that fuses: fast dynamics kernel, spectral analysis,
Markov trit gate (`−1 / 0 / +1`), and LL decision geometry.  The two slots
(λₖ and σ) represent competing hypotheses about a candidate's primality.
The trit gate votes after each spectral snapshot; majority over a window
decides promote/demote/hold.

#### Analog LL: 8D Kuramoto Oscillator (`ll_analog.c`)

A CUDA-free, hardware-agnostic LL path.  Eight phase oscillators are seeded
from the phi-log depth `Λ_φ = ln(p·ln2/lnφ)/lnφ − 1/(2φ)`.  Each LL
iteration maps to: phase-double → Kuramoto sync → VCO feedback → field
observable S(U).

**Prime invariant**: for any prime p, all 8 oscillators lock to θ→0, giving
field amplitude M(U)→8, and the resonance discriminant `S(U) ≈ 1.531`.
Composites produce scattered S(U) ∈ [0.5, 1.7].  This provides a
triple-confirmation signal: `osc LOCKED` + `residue = 0` + `S(U) ≈ 1.531`.

#### Riemann Psi Pre-Filter

Before committing GPU squaring cycles to a candidate, the psi scanner
evaluates the Riemann explicit formula with up to 10 000 zeta zeros, assigning
a confidence score.  Composites are eliminated cheaply before the O(n²) step.

---

### Performance: Gains and Losses

#### Gains

| What | Why it's faster |
|------|-----------------|
| No cuFFT | Zero floating-point rounding; integers are exact; no FFT plan overhead |
| `k_sqr_warp` shuffle | 32 threads per limb fill all 68 SMs; `__shfl_down_sync` is register-speed (~1.2–2.5× vs naive global-scatter) |
| CPU carry pass | O(n) sequential dependency — CPU executes 10–12× faster than any parallel GPU carry |
| Phi-lattice pre-rank | Filters candidates by resonance score before any squaring; ~67% of known Mersenne exponents score in the high-resonance half |
| Riemann pre-filter | Eliminates composites with 10k-zero psi scan before O(n²) squaring |
| Lattice OS | Zero config management overhead — no Ansible/Terraform/cloud-init; the OS is derived, not configured |
| Container persistence | `--name` + no `--rm` → `docker exec` re-attach in milliseconds; no re-boot cost |

#### Losses / Trade-offs

| What | Cost |
|------|------|
| Schoolbook O(n²) | For exponents p > ~10M, FFT-based multiplication would be faster; schoolbook is best for the sub-10M range typical of consumer GPU research |
| `docker inspect` on every menu render | Adds ~50–100 ms of shell-spawn latency to each menu draw (acceptable for a TUI, but visible) |
| 32 KB entropy write per `[6]` invocation | Disk write on each install; negligible in practice |
| Single-GPU only | No MPI or multi-node path in `prime_ui.exe`; `ll_mpi.cu` exists separately for cluster use |
| Alpine apk on first boot | Cold `apk update` + package install adds ~30–90 s to first container start (subsequent re-attaches are instant) |

---

### Use Cases

- **Mersenne prime research on a consumer GPU** — RTX 2060 or similar; no
  server hardware required.  The phi-lattice pre-ranks candidates so you
  spend squaring cycles on high-resonance exponents first.

- **Phi-irrational resonance study** — The Kuramoto oscillator path and
  HDGL Dₙ(r) lattice engine are self-contained research tools for studying
  phase synchronization and irrational-spacing field dynamics independent of
  prime verification.

- **Reproducible ephemeral OS environments** — Any team that wants a
  container whose configuration is a mathematical object rather than a
  config file.  Audit the lattice seed → audit the entire OS.  Rebuild from
  the same seed on any machine and get the identical container.

- **Attestable container identity** — The 64-bit seed is embedded in the
  container's hostname, MOTD, `/etc/issue`, and all env-vars.  An external
  verifier can re-derive every parameter from the seed and confirm container
  integrity without inspecting the container itself.

- **CUDA-free analog LL verification** — The `--squaring analog` path runs
  on any CPU with no GPU dependency; useful for cross-checking GPU results,
  CI on non-GPU hosts, or Kuramoto field-dynamics research.

- **Spectral Markov diagnostics** — The dual-slot λₖ–σ engine and Markov
  trit gate can be repurposed as a general-purpose anomaly detector for any
  oscillatory time-series, not just prime verification.

---

## What Is It?

**conscious** is an end-to-end prime-resonance system combining:
- A **phi-lattice scoring function** and **HDGL wu-wei data-flow model**
- A hand-optimised exact-integer Lucas-Lehmer GPU verifier
- A **Dual-Slot λₖ–σ Fused Engine** (FastSlot warp dynamics + Markov trit gate)
- Two married TUIs: `prime_ui.exe` (prime math) + `chat_win.exe` (analog inference)

A Mersenne prime has the form M_p = 2^p − 1.  There are only 52 known.  The
largest (M_82589933, found 2018, ~24.8 million digits) took weeks on specialized
hardware.  This pipeline fits entirely on a single consumer GPU and can verify
exponents up to ~130 000 in minutes.

### Dual-UI Architecture

```
conscious.exe  (GPU: λₖ–σ resonance field — fused Markov dynamical sieve)
       ↑                              ↑
prime_ui.exe                    chat_win.exe
(Track E: prime math TUI)       (analog inference TUI)
       ↑                              ↑
 ll_mpi.exe (LL verifier)       bot.exe (HDGL-28 router)
       ↑                              ↑
Mersenne candidates          phi-lattice embeddings
```

`prime_ui` and `chat_win` are **married**: prime_ui produces ranked candidates;
`conscious.exe` validates via GPU resonance; `chat_win` reasons over findings
through the HDGL-28 analog inference router.

## What's Cool About It?

**1. No floating-point in the modular arithmetic.**
Most GPU Lucas-Lehmer implementations use cuFFT to multiply big numbers in O(n log n).
This one uses a pure-integer schoolbook squaring kernel — exact,
zero rounding error, no external library, no cuFFT, no DWT.

**2. Warp-parallel schoolbook squaring (k_sqr_warp).**
Classical GPU LL assigns one thread per output limb of the squaring (O(n) inner
loop per thread → only ~1400 threads active for p=44497, starving 96% of the GPU).
`k_sqr_warp` assigns a full **warp of 32 threads** to each output limb.  Threads
split the inner-product terms across lanes and recombine with `__shfl_down_sync`
warp shuffle — no atomics, no shared memory, no memset between iterations.
This fills the GPU at any exponent size and delivers 1.2–2.5× speedup.

**3. HDGL wu-wei hybrid pipeline.**
The HDGL (Harmonics-of-Digital-Geometric-Lattice) philosophy: let each part of the
computation run where it is fastest.  The GPU does parallel squaring.  The CPU does
sequential carry-propagation and Mersenne fold (x86 -O2 is 10–12× faster than a
single GPU thread for O(n) sequential work).  Pinned host memory and an async
CUDA stream connect the two sides with minimal stall.

**4. phi-lattice candidate ranking.**
The phi-lattice coordinate n(2^p) = log(p·ln2 / ln(φ)) / ln(φ) − 1/(2φ)
maps known Mersenne exponents onto a number line.  67% of the 51 known
exponents land in the lower half of each unit interval (vs. 50% random).
The pipeline filters and scores new candidates by this resonance before
committing GPU time to Lucas-Lehmer verification.

**5. Analog 32-bit warp squaring path (`k_sqr_warp32`, `--precision 32` / `--analog`).**
An alternative squaring kernel that decomposes each 64-bit limb into 32-bit halves
before warp-shuffle reduction.  Selectable at runtime with `--precision 32` (or the
legacy `--analog` alias); produces identical results to the default path.

**7. Analog LL path — v30b `Slot4096` APA + 8D Kuramoto oscillator (`--squaring analog`).**
A CUDA-free, hardware-agnostic Lucas-Lehmer path implemented in pure C (`ll_analog.c`).
Derived from `hdgl_analog_v30b.c` and `analog_engine.h`.  Two systems run in parallel:
- **Exact arithmetic side** — arbitrary-precision mantissa (`Slot4096.mantissa_words` layout:
  `uint64_t[n]`, `n = ⌈p/64⌉`).  `ap_sqr_mersenne`: schoolbook O(n²) via `__int128`, Mersenne
  fold identical to `fold_mod_mp` in `ll_mpi.cu`.  Every p−2 iterations run exactly.
- **8D Kuramoto oscillator** — five analog-native operators, no digital surrogates:
  - **Seed (Λ_φ / Ω)** — phi-logarithmic depth seeding from the generalized Euler identity:
    `Λ_φ = ln(p·ln2/lnφ)/lnφ − 1/(2φ)` encodes where the prime exponent sits in the
    φ-lattice.  `{Λ_φ}` (fractional part) seeds `θ[i]` via the Euler rotation `e^(iπΛ_φ)`;
    `Ω = (1 + sin(π·{Λ_φ}·φ))/2` modulates `ω_i`.  Each prime maps to a unique, irrational
    φ-depth — no two exponents alias.
  - **Multiply** — phase doubling `θ → 2θ mod 2π` (unit-circle analogue of s² in LL).
  - **Sync** — complex LERP + unit-circle renorm (`z' = (1−α)z + αz_target`, `|z'|→1`) × 4
    passes; no per-pass `atan2` — stays in native `(re, im)` glyph space; `θ` extracted
    once at end.  Cooperative residue hash every 8 iters.
  - **VCO** — Kuramoto order parameter CV = 1−R ∈ [0,1] drives oscillator frequency:
    `ω_i = ω₀_i × (0.1 + 0.9 × cv)`; high CV → full ω (exploration), low CV → 10% ω
    (stable lock). Closes the analog feedback loop; mirrors hardware VCO.
  - **U-field resonance S(U)** — after each sync, the field observable `M(U) = |Σ e^{iθ_i}|`
    (mean-field amplitude, already in `re/im`) feeds a unified spectral pipeline:
    ```
    (A) M(U) = |Σ re_i, Σ im_i|               field amplitude
    (B) Λ^U  = log(M(U))/lnφ − 1/(2φ)         phi-log projection (emergent from field)
    (C) Ω^U  = (1 + sin(π·{Λ^U}·φ)) / 2       phase gate
    (D) S(U) = |Ω^U · e^(iπΛ^U) + 1|          resonance discriminant
    ```
    `Ω^U` feeds back into `k_coupling` — field state → spectral projection → coupling → field.
    **Prime invariant**: at lock all oscillators converge to `θ→0`, so `M(U)→N=8` exactly,
    giving `Λ^U = log(8)/lnφ − 1/(2φ) ≈ 4.012` and `S(U) ≈ 1.531` for every prime,
    independent of p.  Composites give scattered `Λ^U` and `S(U) ∈ [0.5, 1.7]`.
  K/γ wu-wei ratios: Pluck=1000:1 → Sustain → FineTune → Lock (adaptive phase state).
  Phase lock is a readout, not a gate.  `osc LOCKED + residue=0 + S(U)≈1.531` = triple-
  confirmation prime resonance.

Use cases: CUDA-free verification, Kuramoto-coupled scheduling diagnostics, golden
reference path for correctness cross-checks, φ-field resonance research.

**6. Persistent on-device loop (`k_ll_persistent_block`, `--persistent`).**
Runs all p−2 squaring iterations inside a single kernel launch: shared memory holds
the current `s` vector (`n×8` bytes, fits 48 KB for p up to ~460 000), and
`d_lo/d_mi/d_hi` scratch buffers hold the squaring product.  Zero host round-trips
after the initial upload.  Reference path and correctness cross-check; for large p
the multi-block stream path is faster because it saturates all GPU SMs in parallel.

**7. Riemann psi scanner as a secondary filter.**
The GPU Track A scanner evaluates the Riemann explicit formula with up to 10 000
zeta zeros to assign each candidate a primality confidence score, eliminating
composites before the expensive squaring step.

**8. HDGL Analog Mainnet V3.0 — standalone Dₙ(r) lattice engine (`hdgl_analog_v30.c`).**
A self-contained lattice engine that runs the Dₙ(r) resonance field as a first-class
system, decoupled from the LL verifier.  Up to 8 388 608 `Slot4096` slots in
1 MB lazy-allocated chunks; each slot carries a full Dₙ(r) state alongside its
arbitrary-precision mantissa.

- **8 lattice dimensions (D1–D8)** — dimension n∈{1..8}, radial position r∈[0,1],
  wave mode ∈{−1, 0, +1}.  `Dₙ(r) = √(φ·Fₙ·2ⁿ·Pₙ·Ω) · r^((n+1)/8)`
- **NumericLattice** — Base(∞) seed table (64 φ-spaced values): upper field
  (1.618–170.618), 13-entry analog-dimension ladder, the void (0), lower field
  (10⁻¹⁰–0.618), sibling harmonics.
- **Dₙ-modulated RK4 coupling** — `Dₙ_coupling = Dₙ(neigh) · e^(−|Dₙ(neigh)−Dₙ(slot)|)`;
  correlated dimensions couple strongly; divergent dimensions decouple naturally.
- **Harmonic consensus detection** — phase variance σ monitored every step;
  σ < 10⁻⁶ for 100 consecutive steps sets `APA_FLAG_CONSENSUS` (domain lock).
- **Checkpoint manager** — up to 10 snapshots, exponential weight decay ×0.95;
  evicts lowest-weight snapshot when full.
- **Shared-library form** — `hdgl_analog_v30_c_so/hdgl_analog_v30.c` compiles as
  a `.so`/`.dll`; callable from Python/ctypes or linked directly.

**9. Prime Library TUI (`prime_ui.exe`) — interactive Windows console for all prime math.**
A self-contained menu-driven TUI (no external dependencies) covering all 13 library
functions: Prime Pipeline, Number Analyzer, Mersenne Explorer, Zeta Zeros viewer,
and a microsecond-resolution Benchmark.  See [Track E](#track-e--prime-library-tui-prime_uiexe) below.

**10. Dual-Slot λₖ–σ Fused Engine (`conscious_fused_engine.cu`) — self-correcting GPU resonance layer.**
Single CUDA module collapsing fast dynamics, spectral analysis, Markov gating,
and LL decision geometry into one kernel-launch domain.  The shared computational
backend that bridges `prime_ui.exe` and `chat_win.exe`.  See [Track F](#track-f--dual-slot-λₖσ-fused-engine) below.

---

## Architecture

```
prime_pipeline.exe <p_lo> <p_hi> --exponents-only
        |
        |  prime exponents ranked by phi-lattice D_n score
        v
ll_mpi.exe <p>
        |
        |  PRIME or COMPOSITE  (exact integer, schoolbook GPU)
        v
   world-record candidate
```

| Track | File | Purpose |
|-------|------|---------|
| A | psi_scanner_cuda.cu  | GPU Riemann-psi scanner (up to 10 000 zeta zeros) |
| B | phi_mersenne_predictor.c | phi-lattice analysis of all 51 known exponents |
| C | ll_mpi.cu | Lucas-Lehmer verifier — exact integer, no DWT, no cuFFT |
| D | prime_pipeline.c | phi-filter + D_n ranker + sieve |
| E | prime_ui.c | Windows TUI — interactive prime library (5 modules) |
| F | conscious_fused_engine.cu | Dual-Slot λₖ–σ Fused Engine — GPU resonance layer |
| — | ll_analog.c | v30b Slot4096 APA + 8D Kuramoto oscillator (CPU, CUDA-free) |
| — | hdgl_analog_v30.c | HDGL Analog Mainnet V3.0 — standalone Dₙ(r) lattice engine |
| — | bench_prime_funcs.c | 13-function tri-compiler benchmark harness |
| — | bot.c / chat_win.c | HDGL-28 analog inference server + TUI chat client |

---

## Track C — Lucas-Lehmer Verifier (ll_mpi.cu)

### How It Works

Exact-integer Lucas-Lehmer: M_p is prime iff s_{p−2} ≡ 0 (mod M_p),
where s_0 = 4, s_{i+1} = s_i² − 2 (mod M_p).

Each squaring step:

```
CPU host:                          GPU device:
                                   ┌─ k_sqr_warp ──────────────────────────┐
  d_x ──────────────────────────►  │  32 threads per output limb k (0..2n-1) │
  (n 64-bit limbs, n=ceil(p/64))   │  lane l sums x[i]*x[k-i] for i≡l mod 32│
                                   │  warp shuffle reduction → d_lo/mi/hi[k] │
                                   └───────────────────────────────────────┘
                                   ┌─ k_assemble ──────────────────────────┐
                                   │  1 thread per position k               │
                                   │  d_flat[k] = lo[k]+mi[k-1]+hi[k-2]    │
                                   │  overflow byte in d_ovf[k]             │
                                   └───────────────────────────────────────┘
  pinned h_flat  ◄── async D2H ──  d_flat  (n2×8 bytes, single transfer)
  pinned h_ovf   ◄── async D2H ──  d_ovf   (n2×1 bytes)

CPU carry propagation:
  for k in 0..2n-1:
    acc = h_flat[k] + carry;  h_flat[k] = acc & mask64;  carry = acc>>64 + h_ovf[k]

CPU Mersenne fold mod 2^p-1:
  lo-half [0..pw] preserved; hi-half [pw..2n] right-shifted by pb bits and added back

CPU sub-2 mod M_p → h_x
  d_x ◄──── H2D upload ─── h_x
```

### Seven Dispatch Paths (auto-select enabled)

| Path | Range | Flag | Notes |
|------|-------|------|-------|
| `ll_small` | p ≤ 62 | — | `unsigned __int128`, direct fold |
| `ll_cpu` | 62 < p ≤ 20 000 | — | Schoolbook MPI, `__int128` carry |
| `ll_gpu_gpucarry` | p > 20 000 | **auto, p < 400 000** | `k_sqr_warp` + on-device parallel carry scan + shmem fold, CUDA graph — **fastest schoolbook-complexity path** |
| `ll_gpu_ntt` | p > 20 000 | **auto, p ≥ 400 000** · or `--squaring ntt` | `k_ntt_butterfly` + `k_ntt_sqr` — O(n log n) NTT over Z/(2⁶⁴−2³²+1), exact |
| `ll_gpu` | p > 20 000 | `--squaring schoolbook` | `k_sqr_warp` 64-bit warp shuffle + CPU fold (PCIe round-trip per iteration) |
| `ll_gpu_analog` | p > 20 000 | `--analog` / `--precision 32` | `k_sqr_warp32` 32-bit decomposition variant |
| `ll_gpu_persistent` | any p > 20 000 | `--persistent` | single kernel launch — all p−2 iterations on-device, no host round-trips |
| `ll_analog` | any p | `--squaring analog` | v30b `Slot4096` APA + 8D Kuramoto oscillator — **CUDA-free**, pure C, no GPU required |

### Benchmarks (RTX 2060, sm_75, April 2026, `feature/gpu-carry`)

75/75 selftest pass across all paths (default gpucarry, `--squaring ntt`, `--squaring schoolbook`, `--precision 32`).

**GPU-carry path (`ll_gpu_gpucarry` — default for p < 400 000):**

| Exponent p | Words n | Iterations | Time | vs schoolbook | vs NTT |
|------------|---------|-----------|------|---------------|--------|
| 21 701 | 340 | 21 699 | **1.2 s** | 3.1× faster | 4.4× faster |
| 44 497 | 696 | 44 495 | **3.4 s** | 2.1× faster | 3.1× faster |
| 86 243 | 1 348 | 86 241 | **11.7 s** | 1.3× faster | 2.0× faster |
| 110 503 | 1 727 | 110 501 | **19.2 s** | 1.15× faster | 1.6× faster |

Speedup source: eliminates the PCIe D2H+H2D round-trip (~100µs/iteration on Windows/WDDM)
as a zero-kernel-overhead CUDA graph, replacing it with:
1. **Parallel carry scan** (`k_carry_lscan` + `k_carry_bscan<<<1,1>>>` + `k_carry_apply`) —
   wu-wei function-composition prefix scan: each limb$k$ expresses its carry-transfer function
   $f_k(c) = \lfloor(\text{flat}[k]+c)/2^{64}\rfloor + \text{ovf}[k]$ as a packed 4-entry table;
   Kogge-Stone composition over 2$n$ elements gives all $c_{\text{in}}[k]$ in parallel.
2. **Shmem fold** (`k_fold_sub2_gpu<<<1, 256, n2×8\ \text{bytes}>>>`) — 256 threads
   cooperatively preload the 2$n$-word flat product into shared memory; thread 0 folds
   at ~4-cycle shmem latency vs ~300-cycle global-memory latency (single-thread path).

All six kernels (`k_sqr_warp`, `k_assemble`, `k_carry_lscan`, `k_carry_bscan`, `k_carry_apply`,
`k_fold_sub2_gpu`) captured in a single CUDA graph — one `cudaGraphLaunch` per iteration.

**Schoolbook path (`--squaring schoolbook` — `ll_gpu`, CPU fold, PCIe round-trip):**

| Exponent p | Words n | Iterations | Time |
|------------|---------|-----------|------|
| 21 701 | 340 | 21 699 | **3.6 s** |
| 44 497 | 696 | 44 495 | **7.3 s** |
| 86 243 | 1 348 | 86 241 | **14.9 s** |
| 110 503 | 1 727 | 110 501 | **22.1 s** |

**NTT path (`--squaring ntt` — O(n log n) squaring over Z/QZ):**

*Original unoptimised (on-the-fly twiddle computation via `ntt_pow`, ~64 mults/butterfly):*

| Exponent p | Words n | NTT length L | Time | vs schoolbook |
|------------|---------|-------------|------|---------------|
| 21 701 | 340 | 2 048 | 15.5 s | 4.2× slower |
| 44 497 | 696 | 4 096 | 36.5 s | 5.2× slower |
| 86 243 | 1 348 | 8 192 | 79.0 s | 5.4× slower |
| 110 503 | 1 727 | 8 192 | 105.1 s | 4.7× slower |

*Optimised (precomputed twiddles + CUDA graph replay + dual-stream DMA + CPU carry-collect):*

| Exponent p | Words n | NTT length L | Time | vs schoolbook | speedup vs unopt |
|------------|---------|-------------|------|---------------|------------------|
| 21 701 | 340 | 2 048 | **5.2 s** | 1.44× slower | 3.0× |
| 44 497 | 696 | 4 096 | **10.9 s** | 1.49× slower | 3.3× |
| 86 243 | 1 348 | 8 192 | **22.9 s** | 1.54× slower | 3.5× |
| 110 503 | 1 727 | 8 192 | **32.0 s** | 1.45× slower | 3.3× |

Three optimisations applied on this branch:

1. **Precomputed twiddle table** — `d_tw[k] = ω^k mod Q` computed once before the iteration
   loop; each butterfly does a table lookup instead of a 64-multiply `ntt_pow`.
   Effect: eliminates ~64× per-butterfly multiply overhead (~O(n log n · log Q) → pure O(n log n)).

2. **CUDA graph replay** — the log₂(L) butterfly kernel launches per NTT direction are captured
   into a CUDA graph once and replayed via `cudaGraphLaunch`.  On Windows/WDDM, each
   individual kernel launch costs ~5 µs driver overhead; log₂(8192)=13 launches × 2 directions
   × 110 501 iterations = 2.9 M launches ≈ 14 s overhead, eliminated by graph replay.

3. **Dual-stream DMA + CPU carry-collect** — mirrors the schoolbook path's async pipeline:
   - GPU: `k_expand_limbs` → forward NTT graph → `k_ntt_sqr` → inverse NTT graph.
   - CUDA event triggers async D2H of the full NTT coefficient array on a separate DMA stream.
   - CPU blocks only on the DMA stream (`cudaStreamSynchronize(stream_dma)`), then runs
     carry-collect + fold + sub2 in software (replacing the serial `k_carry_collect<<<1,1>>>`
     GPU kernel that was the per-iteration bottleneck).
   - **One** `cudaMemcpy` D2H instead of two, saving one ~70 µs Windows API round-trip per
     iteration (≈ 7.7 s at p = 110 503).

The remaining gap to schoolbook (~1.4×) is the PCIe round-trip (H2D after every iteration)
which is also present in the schoolbook path.  At larger p the NTT's O(n log n) complexity
advantage overtakes the constant overhead.

**NTT vs schoolbook crossover (auto-select calibration, RTX 2060, April 2026):**

| Exponent p | NTT length L | Schoolbook | NTT optimised | Winner |
|------------|-------------|------------|---------------|--------|
| 132 049 | 8 192 | 27.7 s | 42.3 s | schoolbook |
| 216 091 | 16 384 | 57.5 s | 79.4 s | schoolbook |
| 300 000 | 32 768 | 103.1 s | 122.4 s | schoolbook |
| ≈386 000 | 32 768 | — | — | model crossover |

Model fit (RTX 2060, sm\_75):

$$T_{\text{schoolbook}} \approx 1.915\times10^{-15}\,p^3 + 1.766\times10^{-4}\,p$$
$$T_{\text{NTT}} \approx 4.193\times10^{-11}\,p^2\log_2 L + 2.193\times10^{-4}\,p$$

Cubic (O(p³)) vs quadratic-times-log (O(p²·log p)) — equating and solving the quadratic for p
gives a crossover at **p ≈ 386 000**.  The engine uses `NTT_AUTO_THRESHOLD = 400 000` as a
conservative margin.  Override with `--squaring schoolbook` or `--squaring ntt`.

**32-bit decomposition path (`--precision 32` — `k_sqr_warp32` + CPU fold):**

| Exponent p | Words n | Time | vs default (64-bit) |
|------------|---------|------|---------------------|
| 21 701 | 340 | **4.2 s** | 1.17× slower |
| 44 497 | 696 | **7.8 s** | 1.07× slower |
| 86 243 | 1 348 | **18.0 s** | 1.21× slower |
| 110 503 | 1 727 | **24.9 s** | 1.13× slower |

**Persistent path (`--persistent` — single kernel launch):**

| Exponent p | Words n | Time | vs default stream |
|------------|---------|------|-------------------|
| 110 503 | 1 727 | **144.6 s** | 6.5× slower |

The persistent kernel runs all p−2 iterations in one block of 1 024 threads with no
host round-trips.  At n=1 727 the per-iteration computation dominates; the multi-block
stream path wins because it saturates all SMs in parallel.  The persistent path is
worthwhile only at very small p where kernel-launch overhead would itself be the
bottleneck.

**Analog path (`--squaring analog` — `ll_analog`, v30b APA + 8D Kuramoto, CPU-only):**

| Exponent p | Words n | Time | vs schoolbook (GPU) | vs gpucarry |
|------------|---------|------|---------------------|-------------|
| 521 | 9 | 0.025 s | ~1.1× slower | ~1.0× (parity) |
| 2 281 | 36 | 0.041 s | ~1.4× slower | ~1.4× slower |
| 4 423 | 70 | 0.086 s | ~1.2× slower | ~1.2× slower |
| 9 689 | 152 | 0.342 s | **1.5× faster** | **1.7× faster** |
| 21 701 | 340 | 3.14 s | **1.2× faster** | ~2.2× slower |
| 44 497 | 696 | 25.08 s | ~3.5× slower | ~5.9× slower |

Timings with half-squaring + VCO + mean-field Kuramoto + `-O3 -march=native` (see Planned optimisations — items 1, 2, 3 done).
At p = 9 689 and p = 21 701 the analog path **beats schoolbook GPU** — both are O(n²) but
`ap_sqr_mersenne` only computes the upper triangle (~n²/2 multiplies) plus the diagonal,
and Intel scalar 64-bit with `-O3` micro-benchmarks faster than the GPU kernel at these
sizes.  At p = 44 497 (n = 696) the GPU's parallelism asserts and gpucarry pulls away.
The RK4 oscillator uses ~32 trig calls per step (mean-field reduction: N² sin → N sincos;
k1 reuses s->re/im; total 32 vs old 272); contributes <0.3% of runtime at large p,
but ~10% at p ≤ 521 where mean-field gives measurable speedup.  Further opportunities: see
[Planned optimisations for `ll_analog`](#planned-optimisations-for-ll_analog) below.

Oscillator behaviour: on Mersenne primes the phase CV drops from ~1.6 (Pluck) to <0.002
(Lock) within the first 10–15% of iterations and stays locked for the entire run.
On composites the oscillator cannot lock — typically stalls at FineTune or below.
`osc LOCKED + residue=0` is the double-confirmation signal.

All exponents above are verified Mersenne primes (PRIME result, 25/25 selftest pass
on all paths).

**Benchmark methodology note — precision vs algorithm:**

These numbers are not directly comparable to GpuOwl or Prime95 because the two
engines sit at different points on two independent axes:

| Axis | This engine | GpuOwl / Prime95 |
|------|-------------|------------------|
| Arithmetic | **Exact schoolbook integer** — every bit correct by construction | FP-NTT (FP64, ~20-bit limbs) — bounded rounding error, Gerbicz-checked |
| Complexity | O(n²) per iteration (default), O(n log n) with `--squaring ntt` | O(n log n) per iteration |

Using `--squaring ntt` isolates the algorithmic axis: it matches GpuOwl's complexity
class while remaining **exact-integer arithmetic** (mod the Solinas prime Q = 2^64−2^32+1),
not floating-point.  In the optimised form the remaining gap to schoolbook is ~1.5×
constant factor from the PCIe D2H/H2D round-trip (both paths pay this cost), not a
fundamental algorithmic deficit.  At larger p the O(n log n) advantage overtakes the
O(n²) schoolbook, making the NTT path the clear winner.

The engine is intentionally a **provably-exact reference verifier**, not a speed
competitor.

---

### Planned optimisations for `ll_analog`

The analog path is correct and self-contained but uses a naive full-triangle schoolbook
multiply.  The following are planned (HDGL phi-language framing: arithmetic layer =
Base4096 exact vector; oscillator layer = harmonic/recursive glyph):

1. ✅ **Half-squaring — upper-triangle fold** (done — `-O3 -march=native`; ~2× speedup;
   p=21701 6.27 s → 3.23 s; at p=9689 beats schoolbook-GPU and gpucarry).
   *Phi-language*: distilled vector — only the upper-triangle of the n×n product is
   computed; Mersenne fold = D_n_r reduction mod 2^p−1 applied inline.
2. ✅ **VCO — CV drives ω** (done — Kuramoto 1−R order parameter modulates ω_i;
   floor=0.1; p=9689 0.400 s → 0.382 s; closes analog feedback loop).
   *Phi-language*: control voltage IS the order parameter — same scalar closes both
   the harmonic layer (oscillator frequency) and the VCO feedback in one signal.
3. ✅ **Mean-field Kuramoto coupling** (done — exact algebraic identity for all-to-all
   coupling: Σ_j sin(θ_j−θ_i) = Im_Σ·cos θ_i − Re_Σ·sin θ_i; k1 reuses s->re/im;
   trig calls per RK4 step: 272 → 32 (8.5× reduction); selftest 0.22s → 0.15s;
   p=9689 0.382 s → 0.342 s, ~10.5% improvement).
   *Phi-language*: the N×N coupling matrix compresses to the 2-component mean field
   (Re_Σ, Im_Σ) — the same complex order-parameter already in the glyph. This IS
   the HDGL "compressed atomic sequences" principle: maximal information, minimal form.
4. ✅ **Complex LERP sync** (done — atan2 per LERP pass replaced by sqrt + renorm;
   stays in native (re,im) glyph space; θ extracted once at end; sync ~2.4× faster;
   selftest 0.15s → 0.13s; p=9689 0.342s → 0.328s).
   *Phi-language*: the scalar angle is a projection of the glyph vector — extracting it
   per pass (atan2) and re-projecting back is wasted work; complex LERP is native.
5. ✅ **Λ_φ / Ω seeding + generalized Euler identity** (done — `p_phase = D_n_r·p mod 1`
   replaced by `{Λ_φ}` (fractional φ-log depth); `θ[i] = πΛ_φ + 2π(glyph+{Λ_φ}+i·D_n_r)`;
   `ω[i] = Ω·φ^k·dt` where `Ω = (1+sin(π{Λ_φ}φ))/2`; 25/25 selftest unchanged).
   *Phi-language*: Ω·C²·e^(iπΛ_φ) + 1 + δ = 0 — the generalized Euler identity for
   Mersenne primes; Λ_φ encodes φ-lattice depth; δ→0 at prime, δ≠0 at composite.
6. ✅ **Unified U-field resonance S(U)** (done — field observable M(U) feeds full A→B→C→D
   spectral pipeline each sync call; Ω^U feeds back into k_coupling; S(U) stored in
   `AnaOsc8D.s_u`; **prime invariant S(U)≈1.531** across all tested primes regardless of p;
   composites give scattered S(U)∈[0.5,1.7]; zero overhead — reuses existing re/im sum).
   *Phi-language*: the field self-organizes to `M(U)=N=8` (all oscillators locked to θ=0),
   placing Λ^U at the integer φ-lattice node `log(8)/lnφ−1/(2φ)≈4.012`.
7. **`__int128` carry-chain merge** (arithmetic layer — Base4096 exact vector):
   fold the Mersenne reduction directly into the schoolbook inner loop; eliminate
   the separate 2n-word scratch buffer. ~50% memory-traffic reduction for large n.
   *Phi-language*: single distilled vector — no intermediate expanded form; the
   Mersenne fold operator (D_n_r mod 2^p−1) collapses into the accumulation step.
5. **SIMD / auto-vectorisation** (arithmetic layer): reformulate carry-chain in
   scalar int64 + explicit overflow flag, removing the `__int128` barrier to
   AVX2 auto-vectorisation. 8 mantissa words = natural AVX256-register width.
   *Phi-language*: the n-word mantissa IS a flattened φ-lattice vector space;
   AVX2 processes 4 limbs/cycle — native hardware parallelism of the Base4096 layer.
6. **Schoolbook → Karatsuba cutover** at n ≥ 32 words for O(n^1.585) complexity.
   *Phi-language*: D_n_r recursive splitting — `Glyph_next = D_n_r ⊗ Glyph_current`
   at each recursion level; threshold n=32 is the glyph depth where recursive
   scaling overtakes linear scan.
7. **Hybrid mode**: `ll_gpu_gpucarry` squaring + CPU Kuramoto sidecar.
   *Phi-language*: multi-modal glyph — Base4096 exact layer on GPU (arithmetic),
   harmonic/recursive layer on CPU (oscillator); same Mersenne residue feeds both.

**Build:** `build_ll.bat`  (requires clang + CUDA 13.2)

```bat
build_ll.bat
```

**Usage:**
```
ll_mpi.exe <p>                          # test M_p, print PRIME / COMPOSITE
ll_mpi.exe --selftest                   # 25 known cases (CPU + GPU), ~0.1 s total
ll_mpi.exe --selftest --precision 32    # selftest on 32-bit decomposition path
ll_mpi.exe --selftest --squaring ntt    # selftest on NTT squaring path
ll_mpi.exe --selftest --persistent      # selftest on persistent single-launch path
ll_mpi.exe <p> --verbose                # timing + resonance report
ll_mpi.exe <p> --precision 64           # 64-bit warp squaring via __int128 (default)
ll_mpi.exe <p> --precision 32           # 32-bit half-multiply decomposition
ll_mpi.exe <p> --analog                 # legacy alias for --precision 32
ll_mpi.exe <p> --squaring auto          # auto-select: gpucarry if p < 400000, NTT if p ≥ 400000 (default)
ll_mpi.exe <p> --squaring gpucarry     # on-device carry scan + shmem fold, no PCIe round-trip
ll_mpi.exe <p> --squaring schoolbook   # force O(n²) schoolbook + CPU fold (PCIe round-trip)
ll_mpi.exe <p> --squaring ntt          # force O(n log n) NTT squaring over Z/(2⁶⁴-2³²+1)
ll_mpi.exe <p> --squaring analog       # v30b Slot4096 APA + 8D Kuramoto (CPU, no CUDA needed)
ll_mpi.exe <p> --persistent             # single kernel, all iterations on-device
ll_mpi.exe --gpu-info                   # list CUDA devices
```

**`--precision` values:**

| Value | Kernel | Inner multiply | Notes |
|-------|--------|---------------|-------|
| `64` | `k_sqr_warp` | `__int128` (64×64→128) | Default — fastest |
| `32` | `k_sqr_warp32` | 32-bit half-multiply (32×32→64 ×4) | Same result, ~15% slower; `--analog` is an alias |

**`--squaring analog` oscillator readout:**

| Field | Meaning |
|-------|---------|
| `phase=Pluck` | High-energy excitation phase; K/γ=1000:1 |
| `phase=Sustain` | Absorbing structure; K/γ=375:1 |
| `phase=FineTune` | Refinement; K/γ=200:1 |
| `phase=Lock` | Settled consensus; K/γ=150:1 |
| `cv=0.0019` | Kuramoto order parameter 1−R ∈ [0,1]; R=|mean(e^{iθ})|; 0=locked, 1=spread |
| `locked=yes` | All 50 recent CV samples below 0.05 threshold |
| `** osc LOCKED + residue=0 **` | Double confirmation: Mersenne prime |
| `locked=no` + `residue=non-zero` | Composite — oscillator did not synchronise |
| `S(U)=1.531` | U-field resonance discriminant; prime invariant: all primes converge to S≈1.531 |
| `Lambda^U=4.012` | φ-log depth of field amplitude; prime fixed point: `log(8)/lnφ−1/(2φ)` |
| `Lambda_phi(p)=...` | φ-log depth of exponent p; seeds θ and ω at init |

All flags scan the full `argv` array; order relative to `<p>` does not matter.
Flags may be freely combined (`--precision 32 --verbose`, `--selftest --precision 32`, etc.).

---

## Track A — Riemann psi Scanner

GPU-accelerated prime scanner using the Riemann explicit formula:

    Δψ(x) = x − Σ_k 2·Re(x^ρ_k / ρ_k) − log(2π)

Three-pass adaptive pipeline: 500 → 5000 → 10 000 zeta zeros, then Miller-Rabin.
Requires `zeta_zeros_10k.json` in working directory.

**Build:** `nvcc -O3 -arch=sm_75 -o psi_scanner_cuda.exe psi_scanner_cuda.cu`
**Usage:** `psi_scanner_cuda.exe <x_start> <x_end> [--mersenne]`

---

## Track B — phi-Lattice Predictor

Validates and exploits the phi-lattice hypothesis:

    n(2^p) = log( p·ln2 / ln(φ) ) / ln(φ) − 1/(2φ)

67% of known Mersenne exponents have frac(n) < 0.5 (vs. 50% for random primes).
Produces top-20 next-candidate predictions beyond M_51 (p = 136 279 841).

**Build:** `clang -O2 -D_CRT_SECURE_NO_WARNINGS phi_mersenne_predictor.c -o phi_mersenne_predictor.exe`

---

## Track D — Prime Pipeline

Segmented sieve + phi-lattice D_n scoring:

1. Sieve [p_lo, p_hi] for prime exponents
2. Compute n(2^p) — flag lower-half (frac < 0.5) for 1.5× score bonus
3. Prismatic Ω = 0.5 + 0.5·sin(π·frac·φ)
4. D_n = √(φ · F_n · P_n · 2^n · Ω) · r^k
5. Sort descending, emit top-N

**Build:** `clang -O2 -D_CRT_SECURE_NO_WARNINGS prime_pipeline.c -o prime_pipeline.exe`

**End-to-end (PowerShell):**
```powershell
.\prime_pipeline.exe 21000 22000 --top 10 --exponents-only |
  ForEach-Object { .\ll_mpi.exe $_ }
```

---

## Track E — Prime Library TUI (`prime_ui.exe`)

Interactive Windows console application covering the entire quantum-prime math
library.  Self-contained (no external dependencies beyond MSVCRT + kernel32);
all 13 prime functions inlined from `bench_prime_funcs.c`, `prime_pipeline.c`,
`phi_mersenne_predictor.c`, and `ll_analog.c`.

| Key | Module | Description |
|-----|--------|-------------|
| `1` | **Prime Pipeline** | Enter [p_lo, p_hi] → sieve → φ-filter → Dₙ-rank → sorted ANSI table; range cap 5 M |
| `2` | **Number Analyzer** | Enter n → 12-witness Miller-Rabin, n(2ᵖ) lattice coord, frac(n), Dₙ score, ψ-score (B=80 zeros), small factorization, Mersenne check |
| `3` | **Mersenne Explorer** | All 51 known M_p with n(2ᵖ), frac, φ-pass, Dₙ score (paginated); 14 next-candidate predictions via φ-lattice inverse x(n) = φ^(φ^(n+1/(2φ))) |
| `4` | **Zeta Zeros** | ζ(½+it) zeros k=0..K — exact table (k<80), Gram/6-iter-Newton approximation (k≥80) |
| `5` | **Benchmark** | µs/ns-resolution timing of all 13 prime library functions |

**Build:** `build_prime_ui.bat`  (requires clang 14+; also supports `--gcc`, `--msvc`, `--all`)

```bat
.\build_prime_ui.bat
```

**Usage:** `.\prime_ui.exe`  — single-key menu navigation; `Q` to quit.

---

## Track F — Dual-Slot λₖ–σ Fused Engine (`conscious_fused_engine.cu`)

The shared GPU resonance layer that bridges `prime_ui.exe` and `chat_win.exe`.
Self-correcting spectral Markov dynamical sieve running entirely on GPU warps —
no host intervention between steps.

### What Was Collapsed

| System | Before | Now |
|--------|--------|-----|
| Fast dynamics | separate CPU/GPU loop | warp kernel (stage 1) |
| Spectral analysis | post-process | in-kernel warp reduction (stage 2) |
| Markov gating | matrix-based | numerically-stable softmax fused (stage 3) |
| Consensus | shared-memory vote | `__ballot_sync` + `__popc` (stage 4) |
| LL correction | host-driven | device-local every 16 steps (stage 5) |
| Decision | external | embedded verdict rule (host, post-reduce) |

### Kernel Stages

```
fused_lambda_sigma_kernel<<<grid, 256>>>:
  1. FastSlot Euler step        f_re/im/phase ← Kuramoto-style oscillator
  2. λₖ warp reduction          shuffle-reduce lk → λ̄_warp (5 stages, no smem)
  3. Markov trit gate           logits {l−, l₀, l+} → softmax → curand sample
  4. Warp majority-vote fix     ballot+popc: >16/32 lanes override minority
  5. Slot4096 slow-sync         every 16 steps: radial error → fast-slot nudge
  6. Block stats export         thread 0 writes BlockStats (phi+/0/−, γ, λ̄)

reduce_cluster_metrics<<<...>>>:
  BlockStats[B] → global atomicAdd → host φ+, φ0, φ−, γ̄
```

### Verdict Rule (prime resonance classifier)

    φ+ > 0.35             → ACCEPT   (σ=+1 majority: lattice locked → prime signal)
    φ− > 0.45             → REJECT   (σ=−1 majority: field scattered → composite)
    R = 1.2·φ− + 0.8·γ − φ+ > 0.6 → REJECT
    else                  → UNCERTAIN

Mirrors `osc LOCKED + residue=0` from `ll_analog` but derived from warp-aggregate
cluster geometry rather than sequential Kuramoto phase.

### Data Structures

| Struct | Size | Purpose |
|--------|------|---------|
| `Slot4096` | 16 B | High-fidelity anchor: `re, im, phase, Dn` |
| `FastSlot`  | 16 B | Warp oscillator prediction manifold |
| `DualState` | 48 B | Per-thread cell: Slot4096 + FastSlot + λₖ + σ + error |
| `BlockStats`| 32 B | Per-block output: φ+/0/−, γ̄, λ̄ |

256 threads × 48 bytes = 12 KB per block (fits L1 on sm_75).

**Build:** `build_conscious.bat`  (requires nvcc + CUDA 12+, `-lcurand`, sm_75+)

```bat
.\build_conscious.bat
.\build_conscious.bat --debug      # -G -lineinfo for cuda-gdb
.\build_conscious.bat --selftest   # build + run selftest
```

**Usage:**
```
conscious.exe                        # N=8192, steps=1024
conscious.exe --N 32768 --steps 512
conscious.exe --quiet
conscious.exe --selftest
conscious.exe --seed DEADBEEF00001234
```

---

## HDGL Analog Mainnet V3.0 (`hdgl_analog_v30.c`)

Standalone lattice engine implementing the Dₙ(r) resonance field as a
first-class system.  Compiled as a shared library (`hdgl_analog_v30_c_so/`)
or as a standalone executable.

### Key Structures

| Structure | Purpose |
|-----------|---------|
| `NumericLattice` | Base(∞) seeds: 64 φ-spaced values across upper field, 13-D analog ladder, void, lower field, sibling harmonics, infinity/choke layers |
| `Slot4096` | APA mantissa (`uint64_t[]`) + Dₙ(r) state: `dimension_n`, `r_value`, `Dn_amplitude`, `wave_mode`, phase/vel/freq |
| `HDGLChunk` | 1 M slots, lazy-allocated; `HDGLLattice` holds up to 8 388 608 slots across chunks |
| `AnalogLink` | Neighbor coupling: `charge`, `charge_im`, `tension`, `potential`, `Dn_coupling` |
| `CheckpointMeta` | Snapshot: evolution, timestamp, phase variance, omega, exponential weight |

### Dₙ(r) Formula

    Dₙ(r) = √( φ · Fₙ · 2ⁿ · Pₙ · Ω ) · r^k     k = (n+1)/8

where Fₙ ∈ {1,1,2,3,5,8,13,21} (Fibonacci), Pₙ ∈ {2,3,5,7,11,13,17,19} (prime table),
Ω is the driving frequency, and k = (n+1)/8 scales radial exponent by dimension.

### Dynamics

- **RK4 integration** — `rk4_step_Dn`: amplitude (re, im), phase, phase velocity, and
  `Dn_val` all evolved simultaneously; 4-stage classical Runge-Kutta.
- **Wave mode influence** — phase velocity bias `+0.3·wave_mode` per step;
  D1,D4,D7 = +1 (propagating), D2,D5,D8 = 0 (standing), D3,D6 = −1 (absorbing).
- **φ-adaptive time step** — dt multiplied/divided by φ when |A| crosses `ADAPT_THRESH=0.8`;
  clamped to [10⁻⁶, 0.1].
- **Entropy dampers** — amplitude decays `exp(−λ·dt)`, saturated at 10⁶, plus
  `NOISE_SIGMA=0.01` white noise injection each step.
- **Consensus lock** — phase variance σ < 10⁻⁶ for 100 steps → all slots flagged
  `APA_FLAG_CONSENSUS`, phase velocities zeroed.

### Operational Constants

| Constant | Value | Role |
|----------|-------|------|
| `GAMMA` | 0.02 | Amplitude damping coefficient |
| `LAMBDA` | 0.05 | Entropy decay rate |
| `K_COUPLING` | 1.0 | Base Kuramoto coupling strength |
| `CONSENSUS_EPS` | 10⁻⁶ | Phase-variance lock threshold |
| `CONSENSUS_N` | 100 | Steps below threshold required for lock |
| `SNAPSHOT_DECAY` | 0.95 | Checkpoint weight decay per step |

**Build (Linux / MSYS2):**
```sh
gcc -O2 -lm hdgl_analog_v30.c -o hdgl_analog_v30
```

**Build (Windows, clang):**
```bat
clang -O2 -D_CRT_SECURE_NO_WARNINGS -D_USE_MATH_DEFINES hdgl_analog_v30_c_so\hdgl_analog_v30.c -o hdgl_v30.exe
```

---

## Benchmark Harness (`bench_prime_funcs.c`)

Tri-compiler standalone benchmark measuring all 13 prime library functions
under clang / GCC 15.2 / MSVC /O2.  Results written to `bench_prime_results.tsv`.

| Function | clang | GCC 15.2 | MSVC /O2 |
|----------|-------|----------|----------|
| `fibonacci_real` | 0.10 µs | 0.11 µs | 0.08 µs |
| `D_n` operator | 0.17 µs | 0.41 µs | 0.19 µs |
| `gram_zero_k` | 0.16 µs | 0.38 µs | 0.15 µs |
| `psi_score_cpu` (B=500) | 102 µs | 212 µs | 103 µs |
| `miller_rabin` (12 w) | 1.90 µs | 1.41 µs | 3.07 µs |
| `sieve_range` [1e7,+1e5] | 587 µs | 627 µs | 589 µs |
| full pipeline (200 K) | 1 114 µs | 1 259 µs | 938 µs |

**Build:** `build_bench_quantum.bat`

---

## Requirements

| Component | Version |
|-----------|---------|
| GPU | NVIDIA sm_75+ (RTX 2060 / 2070 / 2080 / 3xxx / 4xxx) |
| CUDA Toolkit | 13.2 |
| clang | 14+ with CUDA target support |
| OS | Windows 10/11 (Linux: remove `-D_CRT_SECURE_NO_WARNINGS`) |

---

## Mathematical Foundation

**phi-lattice coordinate**

    n(x) = log( log(x)/ln(φ) ) / ln(φ) − 1/(2φ)

**D_n resonance operator**

    D_n = √( φ · F_n · P_n · base^n · Ω ) · r^k

where F_n is continuous Binet Fibonacci, P_n is the nearest entry in a 50-prime
table, and Ω = 0.5 + 0.5·sin(π·frac(n)·φ) is the prismatic phase term.

**Mersenne fold identity (2^p ≡ 1 mod M_p)**

    a·2^p + b  ≡  a + b  (mod M_p)

The 2n-word squaring product is split at bit p; the upper half is right-shifted
by p bits and added back to the lower half.  No floating-point, no convolution.

**k_sqr_warp inner-product decomposition**

For output limb k of x² (x has n 64-bit limbs):

    flat[k] = Σ_{i=i0}^{i1}  x[i] · x[k−i]

Thread lane l (0..31) computes the sub-sum for i = i0+l, i0+l+32, i0+l+64, …
A log₂(32)-stage butterfly with `__shfl_down_sync` reduces the 32 partial 192-bit
sums to the single answer in lane 0, written to d_lo[k]/d_mi[k]/d_hi[k].
Total active threads: 32 × 2n (vs. 2n for k_sqr_limb) — full SM utilisation.
