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 / si / architecture / verify / authority.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note si/architecture/verify/authority.om: pure-Omega port of aien-architecture-mv |
| 3 | note verification engine: ADR/plan authority validator, capability register verifier, |
| 4 | note architectural layer & evidence immutability invariant checker. |
| 5 | note Compose after si/directives/om2.om and om/tls/sha2.om. |
| 6 | |
| 7 | const ARCH_OK ulong 0 |
| 8 | const ARCH_ERR_BAD_ADR_ID ulong 1 |
| 9 | const ARCH_ERR_NOT_BINDING ulong 2 |
| 10 | const ARCH_ERR_CYCLE_SUPERSEDE ulong 3 |
| 11 | const ARCH_ERR_UNQUALIFIED_REL ulong 4 |
| 12 | const ARCH_ERR_BAD_SHA ulong 5 |
| 13 | const ARCH_ERR_EMULATOR_AS_HW ulong 6 |
| 14 | const ARCH_ERR_DIRTY_TREE ulong 7 |
| 15 | const ARCH_ERR_NON_OMEGA_NEW ulong 8 |
| 16 | const ARCH_ERR_LAYER_INVERSION ulong 9 |
| 17 | const ARCH_ERR_BAD_LAYER ulong 10 |
| 18 | const ARCH_ERR_EVIDENCE_MUTATION ulong 11 |
| 19 | const ARCH_ERR_MISSING_RECEIPT ulong 12 |
| 20 | const ARCH_ERR_BAD_STATUS ulong 13 |
| 21 | |
| 22 | const AUTH_PROPOSED ulong 0 |
| 23 | const AUTH_ACCEPTED ulong 1 |
| 24 | const AUTH_SUPERSEDED ulong 2 |
| 25 | const AUTH_DEPRECATED ulong 3 |
| 26 | const AUTH_REJECTED ulong 4 |
| 27 | |
| 28 | const CAP_PLANNED ulong 0 |
| 29 | const CAP_IMPLEMENTED ulong 1 |
| 30 | const CAP_VERIFIED ulong 2 |
| 31 | const CAP_QUALIFIED_HW ulong 3 |
| 32 | const CAP_BLOCKED ulong 4 |
| 33 | |
| 34 | const HW_HOST_EMULATOR ulong 0 |
| 35 | const HW_GB10 ulong 1 |
| 36 | const HW_BARE_METAL ulong 2 |
| 37 | |
| 38 | const LANG_OMEGA ulong 1 |
| 39 | const LANG_C_LEGACY ulong 2 |
| 40 | const LANG_RUST_LEGACY ulong 3 |
| 41 | |
| 42 | const EV_OP_ADD ulong 1 |
| 43 | const EV_OP_MODIFY ulong 2 |
| 44 | const EV_OP_RENAME ulong 3 |
| 45 | const EV_OP_DELETE ulong 4 |
| 46 | |
| 47 | const LAYER_SEED ulong 0 |
| 48 | const LAYER_AIENUX ulong 1 |
| 49 | const LAYER_GPU ulong 2 |
| 50 | const LAYER_BRIDGE ulong 3 |
| 51 | const LAYER_CORE ulong 4 |
| 52 | const LAYER_PROTOCOL ulong 5 |
| 53 | const LAYER_APPS ulong 6 |
| 54 | const LAYER_SI ulong 7 |
| 55 | |
| 56 | text arch_adr_pfx "ADR-" |
| 57 | |
| 58 | note --- Basic helpers --- |
| 59 | |
| 60 | fn arch_write_all fd int buf view uchar * to bool |
| 61 | n = len buf |
| 62 | if eq n ulong 0 |
| 63 | ret true |
| 64 | end |
| 65 | if gt n ulong 1048576 |
| 66 | ret false |
| 67 | end |
| 68 | off = ulong 0 |
| 69 | while lt off n bound 1048576 |
| 70 | remn = sub n off |
| 71 | k = write fd buf off remn |
| 72 | if le k long 0 |
| 73 | ret false |
| 74 | end |
| 75 | off = add off ulong k |
| 76 | end |
| 77 | ret true |
| 78 | end |
| 79 | |
| 80 | fn arch_put_u64_le dst edit uchar * pos ulong v ulong to ulong |
| 81 | if gt add pos ulong 8 len dst |
| 82 | ret pos |
| 83 | end |
| 84 | for i ulong 8 |
| 85 | sh = mul i ulong 8 |
| 86 | put dst add pos i uchar and shr v sh ulong 255 |
| 87 | end |
| 88 | ret add pos ulong 8 |
| 89 | end |
| 90 | |
| 91 | fn arch_is_lower_hex s view uchar * expected_len ulong to bool |
| 92 | if ne len s expected_len |
| 93 | ret false |
| 94 | end |
| 95 | if gt expected_len ulong 128 |
| 96 | ret false |
| 97 | end |
| 98 | i = ulong 0 |
| 99 | while lt i expected_len bound 128 |
| 100 | b = get s i |
| 101 | ok = false |
| 102 | if and ge b uchar 48 le b uchar 57 |
| 103 | ok = true |
| 104 | end |
| 105 | if and ge b uchar 97 le b uchar 102 |
| 106 | ok = true |
| 107 | end |
| 108 | if not ok |
| 109 | ret false |
| 110 | end |
| 111 | i = add i ulong 1 |
| 112 | end |
| 113 | ret true |
| 114 | end |
| 115 | |
| 116 | fn arch_digest_eq a view uchar 32 b view uchar 32 to bool |
| 117 | for i ulong 32 |
| 118 | if ne get a i get b i |
| 119 | ret false |
| 120 | end |
| 121 | end |
| 122 | ret true |
| 123 | end |
| 124 | |
| 125 | note --- ADR & Plan Authority Validator --- |
| 126 | note Validates "ADR-NNNN" (exact 8 bytes, 4 ASCII digits, value in 1..9999). Returns number or -1. |
| 127 | |
| 128 | fn arch_parse_adr_id s view uchar * to long |
| 129 | if ne len s ulong 8 |
| 130 | ret long -1 |
| 131 | end |
| 132 | for i ulong 4 |
| 133 | if ne get s i get arch_adr_pfx i |
| 134 | ret long -1 |
| 135 | end |
| 136 | end |
| 137 | acc = ulong 0 |
| 138 | for j ulong 4 |
| 139 | b = get s add ulong 4 j |
| 140 | if or lt b uchar 48 gt b uchar 57 |
| 141 | ret long -1 |
| 142 | end |
| 143 | acc = add mul acc ulong 10 sub ulong b ulong 48 |
| 144 | end |
| 145 | if eq acc ulong 0 |
| 146 | ret long -1 |
| 147 | end |
| 148 | ret long acc |
| 149 | end |
| 150 | |
| 151 | note Validates ADR status & supersession invariants (no self/backward supersession cycles). |
| 152 | fn arch_validate_adr adr_id view uchar * status ulong superseded_by ulong to ulong |
| 153 | num = arch_parse_adr_id adr_id |
| 154 | if lt num long 0 |
| 155 | ret ARCH_ERR_BAD_ADR_ID |
| 156 | end |
| 157 | if gt status AUTH_REJECTED |
| 158 | ret ARCH_ERR_BAD_STATUS |
| 159 | end |
| 160 | if eq status AUTH_SUPERSEDED |
| 161 | if le superseded_by ulong num |
| 162 | ret ARCH_ERR_CYCLE_SUPERSEDE |
| 163 | end |
| 164 | else |
| 165 | if ne superseded_by ulong 0 |
| 166 | ret ARCH_ERR_CYCLE_SUPERSEDE |
| 167 | end |
| 168 | end |
| 169 | ret ARCH_OK |
| 170 | end |
| 171 | |
| 172 | note Only ACCEPTED ADRs govern live implementation; PROPOSED/open PR is never binding or qualified. |
| 173 | fn arch_adr_is_binding status ulong to bool |
| 174 | ret eq status AUTH_ACCEPTED |
| 175 | end |
| 176 | |
| 177 | fn arch_check_release_authority status ulong commit_sha view uchar * dirty bool to ulong |
| 178 | if dirty |
| 179 | ret ARCH_ERR_DIRTY_TREE |
| 180 | end |
| 181 | if not arch_is_lower_hex commit_sha ulong 40 |
| 182 | ret ARCH_ERR_BAD_SHA |
| 183 | end |
| 184 | if ne status AUTH_ACCEPTED |
| 185 | ret ARCH_ERR_UNQUALIFIED_REL |
| 186 | end |
| 187 | ret ARCH_OK |
| 188 | end |
| 189 | |
| 190 | note --- Capability Register Verifier --- |
| 191 | note Enforces: |
| 192 | note - Valid capability status & language rule (new capabilities must be pure Omega) |
| 193 | note - Dirty tree refused |
| 194 | note - VERIFIED / QUALIFIED_HW require valid 40-char commit SHA & 64-char receipt SHA-256 |
| 195 | note - QUALIFIED_HW refuses HW_HOST_EMULATOR (host/emulator pass != hardware qualification) |
| 196 | |
| 197 | fn arch_verify_cap_lang is_new bool lang_class ulong to ulong |
| 198 | if is_new |
| 199 | if ne lang_class LANG_OMEGA |
| 200 | ret ARCH_ERR_NON_OMEGA_NEW |
| 201 | end |
| 202 | end |
| 203 | ret ARCH_OK |
| 204 | end |
| 205 | |
| 206 | fn arch_verify_cap_receipts cap_status ulong commit_sha view uchar * receipt_sha view uchar * to ulong |
| 207 | if gt cap_status CAP_BLOCKED |
| 208 | ret ARCH_ERR_BAD_STATUS |
| 209 | end |
| 210 | if or eq cap_status CAP_VERIFIED eq cap_status CAP_QUALIFIED_HW |
| 211 | if not arch_is_lower_hex commit_sha ulong 40 |
| 212 | ret ARCH_ERR_BAD_SHA |
| 213 | end |
| 214 | if not arch_is_lower_hex receipt_sha ulong 64 |
| 215 | ret ARCH_ERR_MISSING_RECEIPT |
| 216 | end |
| 217 | end |
| 218 | ret ARCH_OK |
| 219 | end |
| 220 | |
| 221 | fn arch_verify_cap_hw cap_status ulong hw_target ulong dirty bool to ulong |
| 222 | if gt cap_status CAP_BLOCKED |
| 223 | ret ARCH_ERR_BAD_STATUS |
| 224 | end |
| 225 | if dirty |
| 226 | ret ARCH_ERR_DIRTY_TREE |
| 227 | end |
| 228 | if eq cap_status CAP_QUALIFIED_HW |
| 229 | if eq hw_target HW_HOST_EMULATOR |
| 230 | ret ARCH_ERR_EMULATOR_AS_HW |
| 231 | end |
| 232 | if gt hw_target HW_BARE_METAL |
| 233 | ret ARCH_ERR_EMULATOR_AS_HW |
| 234 | end |
| 235 | end |
| 236 | ret ARCH_OK |
| 237 | end |
| 238 | |
| 239 | note --- Architectural Layer Dependency & Evidence Immutability Invariants --- |
| 240 | note Layer ordering: 0=seed < 1=aienux < 2=gpu < 3=bridge < 4=core < 5=protocol < 6=apps < 7=si |
| 241 | note A layer may depend only on itself or lower layers (to_layer <= from_layer). |
| 242 | |
| 243 | fn arch_layer_dep_check from_layer ulong to_layer ulong to ulong |
| 244 | if or gt from_layer LAYER_SI gt to_layer LAYER_SI |
| 245 | ret ARCH_ERR_BAD_LAYER |
| 246 | end |
| 247 | if gt to_layer from_layer |
| 248 | ret ARCH_ERR_LAYER_INVERSION |
| 249 | end |
| 250 | ret ARCH_OK |
| 251 | end |
| 252 | |
| 253 | note Evidence tree is additions-only: MODIFY, RENAME, DELETE are forbidden. |
| 254 | fn arch_evidence_op_check op ulong to ulong |
| 255 | if eq op EV_OP_ADD |
| 256 | ret ARCH_OK |
| 257 | end |
| 258 | ret ARCH_ERR_EVIDENCE_MUTATION |
| 259 | end |
| 260 | |
| 261 | note Canonical SHA-256 digest of an authority record (adr_num, status, cap_status, commit_sha). |
| 262 | fn arch_record_digest adr_num ulong status ulong cap_status ulong commit_sha view uchar * dst edit uchar 32 to void |
| 263 | ctx = new ulong 40 0 |
| 264 | hdr = new uchar 24 0 |
| 265 | w1 = arch_put_u64_le hdr ulong 0 adr_num |
| 266 | w2 = arch_put_u64_le hdr ulong 8 status |
| 267 | w3 = arch_put_u64_le hdr ulong 16 cap_status |
| 268 | if or ne w1 ulong 8 or ne w2 ulong 16 ne w3 ulong 24 |
| 269 | trap 8 |
| 270 | end |
| 271 | sha256_init ctx |
| 272 | sha256_update ctx hdr |
| 273 | sha256_update ctx commit_sha |
| 274 | sha256_final ctx dst |
| 275 | end |