omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / docs / turing / TURING_YIELD_RANGE_CODER_OM.md
| 1 | # TURING Yield: a decodable bitstream in Omega (L111) |
| 2 | |
| 3 | Drake, 2026-10-19 (ledger L111): "decodable bitstream for TURING Yield: a real lossless coder ... |
| 4 | whose output length equals the ideal code length T within a stated bound, so T becomes bytes on |
| 5 | disk". Under the Omega-first rule the coder is written in Omega, not C. |
| 6 | |
| 7 | ## What it is |
| 8 | |
| 9 | `src/turing/tc_range.om` is coder A of `calibration/docs/CODER_SPEC.md` (range coder, `TCR1`) |
| 10 | written in Omega. It reads the same TPS1 probability stream and TSY1 symbol file as the C |
| 11 | reference `src/turing/tc_range.c` (built into `turing-coder`, `mk/turing_exp001_b.mk`), writes the |
| 12 | same coded file, and decodes it back. |
| 13 | |
| 14 | Build (the unit is composed after the FIPS 180-4 unit `seed/ng/sha256.om`, with an `om2` first |
| 15 | line so its unsized text views compile; compiler = the repository's own `seed/out/omc`): |
| 16 | |
| 17 | { printf 'om2\n'; cat seed/ng/sha256.om src/turing/tc_range.om; } | seed/out/omc > tcr-om |
| 18 | |
| 19 | CLI, same shape as `turing-coder` (range only): |
| 20 | |
| 21 | tcr-om encode range IN.tps IN.tsy OUT.tcr |
| 22 | tcr-om decode range IN.tps IN.tcr OUT.tsy |
| 23 | tcr-om verify range IN.tps IN.tsy IN.tcr |
| 24 | tcr-om selftest |
| 25 | |
| 26 | Exit 0 accepted, 2 refused (one stderr line with the CODER_SPEC section 10 code and name), 1 usage, |
| 27 | 3 ALARM (a proved property failed on this run; never expected, loud if it happens). Encode, |
| 28 | decode and verify print `count`, `ideal_ub`, `coded_bytes`, `coded_bits`, `payload_bytes`, |
| 29 | `shifts`, `bound_ub`, `slack_ub`, `lower_ub`, `overhead_ub`, `bound=PASS` and `lower=PASS`. |
| 30 | |
| 31 | Refusals follow `turing-coder`'s order: encode checks the TPS1, the TSY1, then the pair; decode |
| 32 | the TPS1, the coded file, then the decode; verify the TPS1, the coded file and the whole decode, |
| 33 | then the TSY1 and the pair, then byte identity (compared through the TSY1 digests of domain, |
| 34 | header and symbols, which stands for comparing the bytes). On every single-fault input of the |
| 35 | gate both tools report the same code. |
| 36 | |
| 37 | Every file is streamed through fixed buffers: memory does not grow with the number of symbols |
| 38 | (640 KB peak on a 2,172,776-symbol, 91 MB stream where the C tool used 154 MB). Each input is read |
| 39 | twice: a validation pass (trailer digest with SHA-256 in Omega, header, every record or symbol) |
| 40 | and the coding pass, which re-checks every record and symbol and hashes the file again; a file |
| 41 | that changed between the passes is refused (DIGEST). An output path that names an input (same |
| 42 | device and inode) is refused before anything is opened for writing, and a refusal after the |
| 43 | output was created, including IO (102) from a failed fsync or fclose in `ofinish`, removes it. |
| 44 | |
| 45 | Two ceilings come from the 64-bit micro-bit sums, not from a buffer (Wolfram W3). `tpsload` |
| 46 | refuses `n > 1152514738906` with ARG before the body-size check, because `16005647*n + 480000000` |
| 47 | no longer fits in `2^64-1`. `report` refuses a coded file of more than `2305843009213` bytes with |
| 48 | ARG, because `coded_bytes * 8 * 1000000` no longer fits. The host gate builds a 160-byte TPS1 |
| 49 | (header and a real trailer, no record body) at `n = cap+1` and requires ARG; the same shape with |
| 50 | `n = 1` is FORMAT, which is the size check, so that refusal is not mistaken for the cap. A sum |
| 51 | that would wrap anyway raises ALARM 7. `tcr-om selftest overflow` forms the upper sum at |
| 52 | `n = cap+1` with the maximum per-symbol ideal and must take that alarm. `selftest` itself forms |
| 53 | the same sum at `n = cap` and the coded-byte product at the byte cap, and must not alarm. |
| 54 | |
| 55 | ## The bounds |
| 56 | |
| 57 | For a coded file of n symbols (header and flush counted, CODER_SPEC section 4 accounting), with |
| 58 | T the section 8 ideal length of exactly the coded symbols: |
| 59 | |
| 60 | upper: coded_bits <= T + 480 + 0.005647 n |
| 61 | lower: coded_bits >= T + 472 - G, G = sum, over the symbols coded as the last symbol of |
| 62 | their row (s = K - 1), of log2(1 + 256/q) |
| 63 | |
| 64 | The integer inequalities below are what the program evaluates on every encode, decode, |
| 65 | verify and selftest. The lower one is a necessary consequence of the stated bound (no |
| 66 | false ALARM 6 when that bound holds) but not a sufficient one: the rounding of T into |
| 67 | `ideal_ub`, the `- n` slack, and the excess of `gain_ub` over 10^6 G open a gap of at |
| 68 | most `1.49984 n + 24.37 m` micro-bits (m is the number of last-symbol occurrences; |
| 69 | derived in proof step 5). So `lower=PASS` certifies |
| 70 | |
| 71 | coded_bits >= T + 472 - G - (1.49984 n + 24.37 m) / 10^6 |
| 72 | |
| 73 | The stated bound itself comes from Lean `encode_lower` on the model, together with the |
| 74 | logarithm and the length identity. Tightening the integer test so that a pass implied |
| 75 | the stated form would raise the threshold by that gap and could ALARM on a stream that |
| 76 | still meets `encode_lower`. In integers (micro-bits): |
| 77 | |
| 78 | 1,000,000 x coded_bits <= ideal_ub + 5647 n + 480,000,000 |
| 79 | 1,000,000 x coded_bits >= ideal_ub + 472,000,000 - n - gain_ub |
| 80 | |
| 81 | where gain_ub sums, over the last-symbol occurrences, ub(q) - ub(q + 256) + 2 for q <= 65280 and |
| 82 | 5649 for larger q (an upper bound of 10^6 log2(1 + 256/q); receipts W2 and W3). |
| 83 | |
| 84 | Where the terms come from: 480 bits are the 448-bit header plus 32 bits of flush; 0.005647 bits |
| 85 | per symbol is the worst case of the `range >> 16` truncation. On the lower side, the last symbol |
| 86 | of a row takes the slack `range - r * cum`, which can give it up to 256/q more than its share, so |
| 87 | that symbol can code below its ideal (by about 8 bits at q = 1). "T bytes on disk" therefore holds |
| 88 | up to these two stated terms, and the lower term shows in the data: on the gate's real units the |
| 89 | coded length sits 1.2 to 17.8 bits above the lower bound, and a selftest case that codes only last |
| 90 | symbols comes out 70,682 bits below its ideal length, still inside the lower bound. |
| 91 | |
| 92 | Relation to the frozen calibration envelope (CODER_SPEC section 9, S4): S4 is an empirical |
| 93 | two-sided band, |overhead - 448| <= 64 + 0.001 N bits, fitted to observed data. The bounds here are |
| 94 | worst-case and proved: the upper slope 0.005647 bits per symbol is larger than S4's 0.001 |
| 95 | (CODER_SPEC itself notes adversarial sequences reach about 0.0056), and the lower term depends on |
| 96 | the data. Nothing here changes S4 or its receipts; on every gate unit the Omega files are the C |
| 97 | coder's bytes, so they sit exactly where the C coder's do. |
| 98 | |
| 99 | ### Proof chain |
| 100 | |
| 101 | 1. **Model and telescoping (Lean 4, `docs/turing/proofs/TcrBound.lean`).** An explicit model of |
| 102 | section 5 (`step`, `renorm`, `run`), for every list of valid symbols (q >= 1, |
| 103 | cum + q <= 65536, cum + q = 65536 for the last symbol of the row): |
| 104 | - `encode_bound`: (2^32 - 1) * prod q_t * 16711681^n * 256^S < 2^32 * (2^40)^n |
| 105 | - `encode_lower`: 2^24 * (2^16)^n <= (2^32 - 1) * prod wq_t * 256^S, wq_t = q_t (+ 256 for a |
| 106 | last symbol) |
| 107 | |
| 108 | where S is the number of renormalisation shifts. Lemmas `step_lb` and `step_ub` (each step keeps |
| 109 | at least (1 - 65535/2^24) of p * R and at most R * wq / 2^16), `step_ge` (R' >= 256 and R' <= R, |
| 110 | so at most two shifts and no overflow), `renorm_spec`, `run_bound`, `run_lower`. Negative |
| 111 | controls: `bad_step_violates` (r = R >> 17 breaks the step inequality), `tight_without_loss` |
| 112 | (the per-symbol loss factor is needed) and `lower_needs_slack` (the +256 credit is needed). |
| 113 | The gate runs `lean TcrBound.lean` with the toolchain pinned in |
| 114 | `docs/turing/proofs/lean-toolchain` (`leanprover/lean4:v4.35.0`) and requires 10 axiom lines, |
| 115 | no `sorry` and no axiom outside propext, Classical.choice, Quot.sound (observed: `step_lb`, |
| 116 | `step_ub` [propext, Quot.sound]; `run_bound`, `encode_bound`, `run_lower`, `encode_lower` |
| 117 | [propext, Classical.choice, Quot.sound]; the three controls and `constants` none). |
| 118 | 2. **Logarithm (receipts W1, W3).** log2 of `encode_bound`: |
| 119 | 8S < sum_t (16 - log2 q_t) + n (24 - log2 16711681) + (32 - log2 (2^32 - 1)), where |
| 120 | sum_t (16 - log2 q_t) is the exact ideal length I, `24 - log2 16711681 = -log2(1 - 65535/2^24)` |
| 121 | = eps with 10^6 eps = 5646.4768..., and 32 - log2(2^32 - 1) = 3.359e-10. log2 of |
| 122 | `encode_lower`: 8S >= I - G - (log2(2^32 - 1) - 24), with log2(2^32 - 1) - 24 = 7.99999999966. |
| 123 | 3. **Length identity (checked, not proved).** payload_bytes = S + 4, where S is the number of |
| 124 | renormalisation shifts (one per time `range` goes below `2^24`), not a value computed from the |
| 125 | payload. The flush is five `shift_low` calls; the fifth resolves (low is 0 by then) and its |
| 126 | cache byte is the only one not emitted, which is why the emitted payload is S + 4 rather than |
| 127 | S + 5. What each path actually compares: |
| 128 | |
| 129 | - **encode.** `encs` counts S in `EN[5]`. `encend` does not. `ST[6]` is the bytes emitted |
| 130 | (`EN[6]`). `report` raises ALARM 1 when those bytes differ from S + 4. A carry before the |
| 131 | first cached byte has nowhere to land; `shlow` raises the same ALARM 1 instead of refusing |
| 132 | the input. |
| 133 | - **decode and verify.** `decs` counts S in `DC[5]`, once per payload byte read in that loop. |
| 134 | `decbeg` reads four bytes and does not count them. `ST[6]` is the declared payload. `ST[7]` |
| 135 | is `DC[5]`. `report` raises ALARM 1 when the declared payload differs from S + 4. It does not |
| 136 | store `payload - 4` back into `ST[7]`. |
| 137 | - **selftest.** The same two comparisons: emitted bytes against `EN[5] + 4`, and, after a |
| 138 | decode that round-trips, the payload against `DC[5] + 4`. |
| 139 | |
| 140 | A run that passes has `coded_bits = 8(S + 4) + 448 = 8S + 480`. A decoder that counts two |
| 141 | shifts for one renorm byte still decodes, then fails ALARM 1; the host gate builds that mutant |
| 142 | and requires the identity alarm on verify. Lean does not prove this identity. |
| 143 | 4. **Integer ideal (receipt W2).** Wolfram recomputed the section 8 ub rule for every |
| 144 | q = 1..65535: exact - ub lies in [-0.500192, +0.49984] ub; sum ub = 94,539,136,349 and |
| 145 | sum q * ub mod 2^64 = 1,549,081,999,916,376. `tcr-om selftest` recomputes both sums in Omega and |
| 146 | must match (ALARM 5 otherwise), so the Omega ub equals the rule for every q. |
| 147 | 5. **Combining.** Upper: 10^6 coded_bits < ideal_ub + n (10^6 eps + 0.49984) + 480 x 10^6 + |
| 148 | 3.4e-4, and 5647 >= 10^6 eps + 0.501 leaves at least 0.022 ub per symbol, more than the |
| 149 | 3.4e-4 ub constant for any n >= 1; n = 0 gives equality (480 bits, a 4-byte payload). Lower: |
| 150 | 10^6 coded_bits > ideal_ub - 0.500192 n + 472 x 10^6 - 10^6 G, and 10^6 G <= gain_ub (each term |
| 151 | is within 0.500192 + 0.49984 < 2 ub of its exact gain; for q > 65280 the gain is at most its |
| 152 | value at q = 65281, 5646.48 ub < 5649, and the excess is largest at q = 65535: |
| 153 | 5649 - 10^6 log2(1 + 256/65535) = 24.36515... < 24.37). The program's integer test is |
| 154 | `10^6 coded_bits >= ideal_ub + 472 x 10^6 - n - gain_ub`. Against the stated form |
| 155 | `10^6 coded_bits >= 10^6 T + 472 x 10^6 - 10^6 G`, a pass therefore implies the stated |
| 156 | bound minus a gap of `(10^6 T - ideal_ub) + n + (gain_ub - 10^6 G)` micro-bits. |
| 157 | `10^6 T - ideal_ub` is at most `0.49984 n` (W2). The `- n` term contributes another `n`. |
| 158 | `gain_ub - 10^6 G` is at most 24.37 ub per last symbol, so the gap is at most |
| 159 | `1.49984 n + 24.37 m` micro-bits. (The W2 interval is written [-0.500192, +0.49984] |
| 160 | because the receipt minimum is -0.500191829..., which is slightly below -0.50019.) |
| 161 | |
| 162 | Model-to-system link: the Lean `step`/`renorm` are a transcription of CODER_SPEC section 5 and of |
| 163 | the arithmetic in `encs`. The Lean proof covers that model, not `encs`. It never mentions `low`, |
| 164 | the cache, pending bytes, or `coded_bits`. `tc_range.om` is byte-identical to the C reference on |
| 165 | every gate unit and on two whole dev files (below). The renormalisation loops in `encs` and `decs` |
| 166 | carry `bound 2`, so a third shift (a range below 256, which `step_ge` rules out) traps (exit 104). |
| 167 | Byte identity with C and the run-time checks are what tie the model to the program. A change to |
| 168 | `encs` that the Lean model does not follow is caught by the byte identity with C and by those |
| 169 | checks, not by Lean. |
| 170 | |
| 171 | ## Results (EXERCISED on the Spark, omc `26540e6d...`, omega-mv base e4271abd) |
| 172 | |
| 173 | `make test-turing-range-om` (part of the `turing-yield` gate step): |
| 174 | |
| 175 | - selftest: 726 synthetic cases, 1,926,840 symbols, K = 2..16, four row shapes, three symbol |
| 176 | choices (sampled, least likely, always the last symbol), n = 0 to 200,000: round trip exact, |
| 177 | identity and both bounds hold; one random payload byte flipped per case is detected in all 726 |
| 178 | cases; ub rule exhaustive match. Smallest upper slack for n >= 1: 5647 ub (a single q = 1 |
| 179 | symbol, within eps of the bound); smallest lower slack 11,228 ub. |
| 180 | - 14 real units (2 committed CTR1 traces x the TY-2 candidate, uniform:9 and the five EXP-001 |
| 181 | candidates B0..B3, M): Omega encode byte-identical to C, C verifies every Omega file, Omega |
| 182 | verifies every C file, Omega decode byte-identical to the TSY1, ideal_ub equal to the C value, |
| 183 | both bounds PASS. Example (trace crumbs 05-07, TY-2 candidate): n = 1418, ideal 1456.957896 |
| 184 | bits, coded 1936 bits (242 bytes), upper bound 1944.965342 bits, lower bound 1922.925988 bits. |
| 185 | - Single-fault inputs refused with the same code as the C tool: flipped payload byte (CORRUPT), |
| 186 | truncated (TRUNC), trailing byte (TRAIL), other TPS1 (BINDING), altered magic (HEADER), |
| 187 | corrupted TPS1 (DIGEST), TSY1 of another trace (COUNT), corrupted TSY1 (DIGEST). A decode |
| 188 | refused after its output was created leaves no file; an output path naming an input is refused |
| 189 | (ARG) and the input is unchanged. An encode whose `ofinish` fsync fails (forced in the gate) |
| 190 | exits 2 with IO (102) and leaves no output; a mutant that clears the open flag before that |
| 191 | check leaves the file and is caught. |
| 192 | - Mutants caught on that run: `range >> 15` (no longer byte-identical), upper-bound constants |
| 193 | lowered (ALARM 2), ub rounding removed (ALARM 5), lower-bound constant raised by 10 bits |
| 194 | (ALARM 6). |
| 195 | - Lean: 10 theorems checked, standard axioms only. |
| 196 | |
| 197 | The host script's later checks (decoder shift alarm, `n = cap+1` refused ARG, ALARM 7 on a |
| 198 | wrapping sum, the `ofinish` fsync-failure cleanup and its old-order mutant, and the |
| 199 | `range >> 15` mutant required to differ or alarm on more than one unit) are not part of the |
| 200 | Spark paragraph above. |
| 201 | |
| 202 | Whole dev file (seed 1 only; seeds 8 to 10 untouched), not part of the gate because the data is not |
| 203 | in the repository: 2,172,776 symbols, TY-2 candidate: ideal 1,752,630.382351 bits, coded 1,754,208 |
| 204 | bits (219,276 bytes), byte-identical to C, round trip exact, upper slack 11,172 bits, lower bound |
| 205 | 1,752,900.795377 bits; uniform:9 also byte-identical, both bounds PASS. Single core, nice 10, on a |
| 206 | shared machine at load 28 to 37: Omega encode 5.4 to 10.8 s, verify 5.1 to 21.8 s, decode 5.5 to |
| 207 | 17.7 s; C encode 0.52 to 0.84 s. |
| 208 | |
| 209 | ## Not claimed |
| 210 | |
| 211 | - Coder B (rANS, `TCA1`) is not ported; `tcr-om` refuses `rans` with usage. |
| 212 | - The length identity (proof step 3) is checked on encode, decode, verify and selftest, as that |
| 213 | step describes. It is not proved in Lean. The Lean proof covers the model (`step`, `renorm`, |
| 214 | `run`), not `encs`. |
| 215 | - The Lean check runs `lean` on the single file (it imports nothing); there is no lake project in |
| 216 | this repository. |
| 217 | - C remains the reference implementation of the calibration profile; nothing here changes |
| 218 | CODER_SPEC, S4 or any frozen receipt. |
| 219 | |
| 220 | ## Wolfram receipts (Wolfram Language evaluator, 2026-11-15) |
| 221 | |
| 222 | W1: |
| 223 | |
| 224 | eps = -Log2[1 - 65535/2^24]; |
| 225 | {N[eps*10^6, 20], N[eps*10^6 + 501/1000, 20], Ceiling[eps*10^6 + 501/1000], N[32 - Log2[2^32 - 1], 20]} |
| 226 | -> {5646.4768125983298532..., 5646.9778125983298532..., 5647, 3.3590361500552744...*10^-10} |
| 227 | FullSimplify[24 - Log2[16711681] == -Log2[1 - 65535/2^24]] -> True |
| 228 | |
| 229 | W2 (the CODER_SPEC section 8 rule, transcribed, for every q): |
| 230 | |
| 231 | ub[q_] := Module[{ip = BitLength[q] - 1, m, f = 0, l, x}, m = BitShiftLeft[q, 62 - ip]; |
| 232 | Do[m = Floor[m*m/2^62]; If[m >= 2^63, f = BitOr[f, BitShiftLeft[1, 32 - i]]; m = BitShiftRight[m, 1]], {i, 1, 32}]; |
| 233 | l = BitOr[BitShiftLeft[ip, 32], f]; x = BitShiftLeft[16, 32] - l; BitShiftRight[x*1000000 + 2^31, 32]]; |
| 234 | t = Table[ub[q], {q, 1, 65535}]; d = Table[N[-Log2[q/65536]*10^6, 40] - t[[q]], {q, 1, 65535}]; |
| 235 | {ub[1], ub[32768], ub[43481], ub[46819], Max[d], Min[d], Total[t], Mod[Sum[q*t[[q]], {q, 1, 65535}], 2^64]} |
| 236 | -> {16000000, 1000000, 591903, 485194, 0.49983840009545..., -0.50019182915248..., 94539136349, 1549081999916376} |
| 237 | |
| 238 | The four test values agree with CODER_SPEC section 8. |
| 239 | |
| 240 | W3: |
| 241 | |
| 242 | {N[10^6*Log2[1 + 256/65281], 20], Ceiling[10^6*Log2[1 + 256/65281]], N[10^6*Log2[1 + 256/1], 20], |
| 243 | N[Log2[2^32 - 1] - 24, 20], Floor[(2^64 - 1 - 480*10^6)/16005647], Floor[(2^64 - 1)/(8*10^6)]} |
| 244 | -> {5646.4768139155787531..., 5647, 8.0056245491938781...*10^6, 7.9999999996640963..., 1152514738906, 2305843009213} |
| 245 | |
| 246 | 16005647 = 16,000,000 + 5647 is the largest growth of the upper bound per symbol in ub (ub(1) plus |
| 247 | the slope), so the bound of n symbols fits a 64-bit word up to the stated n. |