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 / one_layer / OM2-TUTORIAL.md
| 1 | # OM-2: from compact source to a check to machine code |
| 2 | |
| 3 | This is the working tutorial and the guarantee matrix for the OM-2 profile of Omegese. It is not a |
| 4 | specification: `spec/omegese-om2.md` and `spec/omegese-om2-errata.md` are normative, and |
| 5 | `docs/one_layer/OMEGESE.md` §9.13 says what `omc` implements of them. Where this document disagrees |
| 6 | with those, they win, and this document is stale. |
| 7 | |
| 8 | Estate when this was written: OM-2 was on the compiler stack `om2/compiler-s4` @ `f6a6492fd` |
| 9 | (PRs #750, #753, #754, #764) and the SHA-256 cutover `om2/sha-cutover` (PR #776), then draft. All of |
| 10 | them have since merged, with the qualification harness #752 (2026-10-25/26 UTC); `main` compiles OM-1 and |
| 11 | OM-2. The pins below are those of the measurements and stay as recorded; `main`'s compiler pin has |
| 12 | moved since (at `3364a1dfd` it is `befb8199...8a1d`, after #786 changed IR-mode boolean typing). |
| 13 | Nothing here is hardware-qualified: every result is HOST on Linux AArch64. |
| 14 | |
| 15 | ## 1. Pins |
| 16 | |
| 17 | | Item | Value | |
| 18 | |---|---| |
| 19 | | Compiler source | `seed/omc/omc.om` on `om2/compiler-s4` @ `f6a6492fd` | |
| 20 | | Compiler binary | `seed/out/omc`, sha256 `499236398aabc652e2dae27fe4bdc4c5db3d3726314356e0dbef23e3345803ec` | |
| 21 | | Contract | `spec/omegese-om2.md` sha256 `245bb74e…0881`, errata E1–E18 | |
| 22 | | Qualification | `om2/qualification`, records in `candidates/*/RESULTS.txt` (PR #752) | |
| 23 | | SHA-256 implementation | `om/sha256/sha256-om2.om`, sha256 `dbf002dd…94bb` | |
| 24 | | SHA-256 artifact | 21,340 bytes, 5,335 words, sha256 `27775fb2…570b` | |
| 25 | | Bridge | `aien-dev/omega-bridge` `feat/slice-abi` @ `a8959fb1`, ABI v2 | |
| 26 | | Baseline for every comparison | `main` @ `acb3f15cd` | |
| 27 | |
| 28 | ## 2. Tutorial |
| 29 | |
| 30 | ### Step 1 — opt in, one line |
| 31 | |
| 32 | ``` |
| 33 | note a first OM-2 unit |
| 34 | om2 |
| 35 | fn main to int |
| 36 | ret 0 |
| 37 | end |
| 38 | ``` |
| 39 | |
| 40 | Leading blank lines and `note` lines are skipped; the first line that is not one of those decides the |
| 41 | profile. `om2`, `om2 diag` or `om2 fmt` makes the unit OM-2. Any other line leaves it OM-1, where the |
| 42 | OM-2 words are not reserved and `rotr` is simply an unknown word (error 3). Nothing else about a unit's |
| 43 | shape changes: one statement per line, prefix operators, `end` blocks. |
| 44 | |
| 45 | ### Step 2 — let the compiler hold the checks |
| 46 | |
| 47 | ``` |
| 48 | om2 |
| 49 | layout blk 8 |
| 50 | field lo uint at 0 little |
| 51 | field hi uint at 4 little |
| 52 | end |
| 53 | abstract v32 ulong |
| 54 | holds v32 dup32 |
| 55 | note the erasing constructor: the body must be exactly this line |
| 56 | fn v32mk x uint to v32 |
| 57 | ret seal v32 or shl ulong x 32 ulong x |
| 58 | end |
| 59 | fn stamp c edit blk to uint |
| 60 | put c lo 0x01020304 |
| 61 | put c hi 0x05060708 |
| 62 | ret or get c lo shl get c hi 16 |
| 63 | end |
| 64 | ``` |
| 65 | |
| 66 | Three things are now checked by `omc` rather than at run time or not at all: |
| 67 | |
| 68 | - **External layout.** `layout blk 8` fixes a 8-byte image and where each field starts; a field past |
| 69 | `SIZE`, or a byte shared with an earlier field, is error 29 at that field line. Access goes through a |
| 70 | `view`/`edit` parameter, so the offsets and the byte order are the compiler's, not a comment's. |
| 71 | - **Constructor discipline.** No `ulong` becomes a `v32` except through `seal` inside `v32mk`. A `seal` |
| 72 | anywhere else is error 28. `holds v32 dup32` says every value keeps `high32 == low32`; the run-time |
| 73 | check is emitted unless the constructor body is exactly the line above, in which case it is erased |
| 74 | (`om2 diag` prints `omc-check 1 erased line=N rule=dup32`). Erasure is a claim the compiler proves from |
| 75 | an exact body shape, so it cannot drift silently: change the body and the check comes back. |
| 76 | - **Definite writes.** `fn seed s edit uint 8 fresh to uint` declares scratch with undefined incoming |
| 77 | elements. Reading an element that is not definitely written is error 27 on that line. |
| 78 | |
| 79 | ### Step 3 — compact rounds |
| 80 | |
| 81 | ``` |
| 82 | om2 |
| 83 | fn rounds s edit uint 8 fresh to uint |
| 84 | for i uint 8 unroll |
| 85 | put s i add rotr 0x9e3779b9 i 7 |
| 86 | end |
| 87 | ret get s 3 |
| 88 | end |
| 89 | ``` |
| 90 | |
| 91 | `unroll` emits the body eight times with `i` constant, so the loop branch and counter disappear; `rotr` |
| 92 | with a number amount becomes one `ROR`. Removing `unroll` changes size and speed, never behaviour. The |
| 93 | same is true for `inline`. Both are bounded by contract, not by hope: 64 copies per loop, 4 inline |
| 94 | levels and 65536 expansion fuel per function, 16 registers (errors 14 and 12). A function's emitted |
| 95 | words have no bound since 2026-11-11 (OMEGESE 9.6). |
| 96 | |
| 97 | Folding happens only inside an unrolled or inlined body, and never for an operation that would trap. A |
| 98 | **number** index into a sized array (up to 4095) becomes an immediate offset with no run-time check; |
| 99 | every other index keeps trap 3. Constants do not fold through parameters. |
| 100 | |
| 101 | ### Step 4 — one view, one slice, no wrapping arithmetic |
| 102 | |
| 103 | ``` |
| 104 | om2 |
| 105 | fn fold a view uint * to uint |
| 106 | x = new uint 1 0 |
| 107 | put x 0 uint len a |
| 108 | for i uint uint len a |
| 109 | put x 0 add get x 0 rotr get a i 5 |
| 110 | end |
| 111 | ret get x 0 |
| 112 | end |
| 113 | ``` |
| 114 | |
| 115 | `a view uint *` takes any length and `len a` is its run-time count. Every index into it is checked at |
| 116 | run time, including a number, and is never eliminated. |
| 117 | |
| 118 | `part A o k` slices only as a call argument for an unsized parameter, and it checks `o <= len A` then |
| 119 | `k <= len A - o` — the wrapped `o + k` is never formed. That matters at the edge: with `len A = 10`, |
| 120 | `o = 2^64-2`, `k = 4`, a wrapped sum test would pass; OM-2 traps 3. |
| 121 | |
| 122 | |
| 123 | A complete, compiled example of steps 2, 3 and 4 together is `examples/om2-tour.om` (sha256 |
| 124 | `856c541d…315f`). Observed with the pinned compiler: compile exit 0, run exit 0; changing one constant |
| 125 | inside `stamp` gives exit 11; `holds v32 dup31` gives exit 28; and with `om2 diag` the same unit emits |
| 126 | 15 `omc-site` records plus one `omc-check 1 erased line=11 rule=dup32` — the erased constructor check, |
| 127 | proved from the exact body shape rather than asserted. |
| 128 | |
| 129 | ### Step 5 — compile, run, refuse |
| 130 | |
| 131 | ```sh |
| 132 | OMC=seed/out/omc # check its sha256 against section 1 first |
| 133 | "$OMC" < prog.om > prog 2> prog.err; echo "compile rc=$?"; cat prog.err |
| 134 | chmod +x prog && ./prog; echo "run rc=$?" |
| 135 | ``` |
| 136 | |
| 137 | Program exit status is `main`'s value mod 256; a runtime trap exits `100 + code`. Never run the output of |
| 138 | a non-zero compile — a refusal may still leave bytes on stdout. |
| 139 | |
| 140 | Refusals observed with the pinned compiler (`omc: line N: error K`, exit `K`): |
| 141 | |
| 142 | | Source | Observed | |
| 143 | |---|---| |
| 144 | | `x = uint 0x100000000` | exit 8 (number out of range) | |
| 145 | | `x = uint 0X10` | exit 6 (malformed number) | |
| 146 | | `part a 0 2` as a statement | exit 9 (word in a disallowed place) | |
| 147 | | `rotr 1 1` in a unit without `om2` | exit 3 (unknown word) | |
| 148 | | `om2` twice | exit 16 (syntax) | |
| 149 | | `holds v32 dup31` | exit 28 (`obligation=ctor`) | |
| 150 | | a layout field past `SIZE` | exit 29 (`obligation=layout`) | |
| 151 | | a `get` of an unwritten `fresh` element | exit 27 (`obligation=init`) | |
| 152 | |
| 153 | With `om2 diag`, stderr also carries `omc-diag 1 code=… sev=… line=… col=… span=A-B expect=… found=… |
| 154 | obligation=… profile=OM-2`, one `omc-site 1 line=N iter=k` per run-time trap site, and one |
| 155 | `omc-check 1 kept|erased line=N rule=dup32|-` per `seal`. |
| 156 | |
| 157 | ### Step 6 — a unit with no `main` is called through the bridge |
| 158 | |
| 159 | A unit without `fn main` compiles to bare words and is not runnable on its own. `export fn NAME ...` |
| 160 | marks the entries: with any export, words `0..E-1` branch to the exports in source order, `E ≤ 32`, and |
| 161 | the other functions are private. `NAME out T N` declares a caller-owned buffer that the binding copies |
| 162 | out after status 0. `aien-dev/omega-bridge` owns the ABI and refuses what it cannot convert; the |
| 163 | `sha256_*` compatibility wrappers in the generated binding are out-of-line so that a freestanding object |
| 164 | including only the declaration header still needs nothing from libc beyond `mem*`. |
| 165 | |
| 166 | ### Step 7 — the whole thing, on SHA-256 |
| 167 | |
| 168 | `om/sha256/sha256-om2.om` is 344 lines, 72 of them the K/IV tables, replacing a 4,096-byte C adapter |
| 169 | plus an OM-1 unit. Measured on the same machine, interleaved, protocol fixed before the run |
| 170 | (`spec/omegese-om2-measure.md`): |
| 171 | |
| 172 | | Workload | `acb3f15c` (OM-1 + C adapter) | OM-2 cutover | ratio | |
| 173 | |---|---|---|---| |
| 174 | | 1 MiB, one call | 152.5 MB/s | 210.2 MB/s | 0.726 | |
| 175 | | 1 MiB in 1-byte updates | 14.3 MB/s | 32.8 MB/s | 0.436 | |
| 176 | | 55 bytes | 496 ns | 560 ns | 1.129 (declared REGRESSION) | |
| 177 | | 64 bytes | 896 ns | 816 ns | 0.911 | |
| 178 | |
| 179 | ## 3. Guarantee matrix |
| 180 | |
| 181 | Read each row as: what the compiler proves, what it checks at run time, what nothing establishes. The |
| 182 | class column is the strongest claim the current evidence supports; `MEASURED` numbers are HOST AArch64 |
| 183 | only. |
| 184 | |
| 185 | ### Functional |
| 186 | |
| 187 | | Claim | Status | Evidence | |
| 188 | |---|---|---| |
| 189 | | OM-2 SHA-256 agrees with the historical C on 2,000 cases | SUPPORTED | `tests/gates/run.sh om-units`: `SHA256-PARITY PASS cases=2000 diff=0` | |
| 190 | | Agrees with independent oracles (`sha256sum` 9.4, OpenSSL 3.0.13 hashlib) | SUPPORTED | 158 fixture vectors, 3,200 seeded random messages, 6 digests cross-checked in the measurement run | |
| 191 | | Every OM-1 unit keeps its meaning | SUPPORTED | C2 manifest 5,876/5,876 byte-identical outputs or identical errors, per commit | |
| 192 | | A wrong rotate count, K, IV, schedule index, pad byte, length or count is caught | SUPPORTED | 17/17 unit mutants killed, one class (a 32-bit truncated count) caught only by the ≥2^32-byte receipt | |
| 193 | | The compiler itself is consistent (fixpoint) | SUPPORTED, not diverse compilation | birth `om4(omc) = omc = omc(omc)` at every slice commit | |
| 194 | | Behaviour of arbitrary OM-2 programs | NOT ESTABLISHED | fixtures and mutants only; no proof, no exhaustive coverage | |
| 195 | |
| 196 | ### Bounds |
| 197 | |
| 198 | | Claim | Status | Evidence | |
| 199 | |---|---|---| |
| 200 | | Every index into an unsized view is checked, numbers included | SUPPORTED | `v6-idx`, `v6-write`, and the `view-nocheck` mutant killed | |
| 201 | | Constant indices into sized arrays are checked at compile time | SUPPORTED | error 21 fixtures; up to 4095 lowered to an immediate offset | |
| 202 | | `part` checks `o <= len A`, then `k <= len A - o`, `o` before `k` | SUPPORTED | `v6-part-wrap`, `v6-part-tail`, `v6-part-o`, `v6-part-order`; three independent mutants killed | |
| 203 | | Host I/O slices are checked against the count | SUPPORTED | existing OM-1 rules, unchanged | |
| 204 | | A trap that is unreachable in practice | NOT ESTABLISHED | the model is "every check that is not proven stays" | |
| 205 | |
| 206 | ### Initialization |
| 207 | |
| 208 | | Claim | Status | Evidence | |
| 209 | |---|---|---| |
| 210 | | A `fresh` element read before a definite write is refused | SUPPORTED | error 27 fixtures, 5 fresh mutants killed, join-intersection rules gated | |
| 211 | | A layout-typed context is fully written before it is read | SUPPORTED for the SHA unit | `f5-part`, and the mutant that leaves `w[15]` unwritten is error 27 at that line | |
| 212 | | `fresh` analysis across arbitrary control flow | PARTIAL BY DESIGN | if-without-else, `while` and rolled `for` do not advance the set; that is conservative, never optimistic | |
| 213 | | Stack exhaustion from deep recursion | UNSUPPORTED as a status | a fault, not a returned status (trap 6 covers the local-array budget only) | |
| 214 | |
| 215 | ### Module and FFI boundaries |
| 216 | |
| 217 | | Claim | Status | Evidence | |
| 218 | |---|---|---| |
| 219 | | Units compose by concatenation; there is no import | SUPPORTED (a limit, not a feature) | OM-1 rule, unchanged in OM-2 (FR-3 open) | |
| 220 | | ABI v2: `omega2_<export>` symbols, `omega2_ident_<unit>()`, `out` through a caller buffer | SUPPORTED | identity records (E1), 19 status-32 refusals that leave state untouched, six-language v1 refusal | |
| 221 | | A freestanding object needs no new libc surface | SUPPORTED | `rx_cortex.o` undefined set identical to `main`'s; `RX_CX_FS_ALLOWED` unchanged (E18) | |
| 222 | | Calling convention drift between compiler and bridge | DETECTED, not prevented | the bridge refuses unknown slots/statuses; a mismatch surfaces as status 32 or a link error | |
| 223 | | Packed structs, imports, shared objects, heap | UNSUPPORTED at this pin | FR-2, FR-3, FR-7, decision 24 | |
| 224 | |
| 225 | ### Timing and leakage |
| 226 | |
| 227 | | Claim | Status | Evidence | |
| 228 | |---|---|---| |
| 229 | | The OM-2 SHA's `init` and `hash` are clean under the static scanner | SUPPORTED | `pub:` scan PASS | |
| 230 | | The OM-2 SHA's `update` and `final` are clean under the static scanner | **NOT_RUN** | 83 and 3 MANUAL branches, 67 and 1 data-indexed accesses, all derived from the byte-count field the scanner cannot treat as public | |
| 231 | | Equal-length messages take the same instruction path | MEASURED on 28 pairs | single-step traces identical; negative controls detected | |
| 232 | | Constant-time behaviour in general | **NO CLAIM** | trace equivalence only (E12); speculative constant-time is UNSUPPORTED | |
| 233 | | Performance | MEASURED, see section 2 step 7 | bulk 1.38× faster; 1–55-byte inputs 6.5–12.9% slower under the predeclared policy | |
| 234 | |
| 235 | ### Effects and authority |
| 236 | |
| 237 | | Claim | Status | |
| 238 | |---|---| |
| 239 | | A guide, a model review or a passing gate grants no authority to run anything | RULE | |
| 240 | | Host execution is not hardware qualification, and no GPU or device claim is made here | RULE | |
| 241 | | Merging this stack moves `main`'s compiler pin for every consumer | RULE: the stack lands as one unit or not at all | |
| 242 | |
| 243 | ## 4. What to do when the compiler refuses |
| 244 | |
| 245 | 1. Read the code: `27` is initialization, `28` is constructor discipline, `29` is layout, `14` is |
| 246 | growth/fuel and also the 33rd export, `12` is registers, `9` is a word in the wrong place. |
| 247 | 2. Reproduce it as small as possible in the same profile, and keep the smallest failing unit. |
| 248 | 3. Decide which side is wrong: your program, a documented limit (OMEGESE §9.13), or the compiler. A |
| 249 | limit is not a bug, and an unimplemented word must be reported as such — never worked around in C. |
| 250 | 4. Fix one thing, rerun the unit and a negative control, and record the pin you used. |