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 / core / sovereign-core / om / store / closure.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/sovereign-core/om/store/closure.om |
| 3 | note Pure OM-2 conversion of aien-closure (manifest, digest, graph, declare, verify). |
| 4 | note Verified closure and artifact pin checker refusing missing/mismatched SHA-256 |
| 5 | note digests, dirty workspace or artifact flags, missing dependencies, and cycles. |
| 6 | note Compose after si/directives/om2.om and om/tls/sha2.om. |
| 7 | |
| 8 | const CLS_MAX_PINS ulong 32 |
| 9 | const CLS_MAX_EDGES ulong 64 |
| 10 | const CLS_HASH_BYTES ulong 32 |
| 11 | const CLS_HASH_TOTAL ulong 1024 |
| 12 | const CLS_NONE ulong 18446744073709551615 |
| 13 | |
| 14 | note Status and refusal codes |
| 15 | const CLS_OK ulong 0 |
| 16 | const CLS_ERR_FULL ulong 1 |
| 17 | const CLS_ERR_DUP_PIN ulong 2 |
| 18 | const CLS_ERR_NOT_FOUND ulong 3 |
| 19 | const CLS_ERR_DIRTY_TREE ulong 4 |
| 20 | const CLS_ERR_MISSING_DIGEST ulong 5 |
| 21 | const CLS_ERR_DIGEST_MISMATCH ulong 6 |
| 22 | const CLS_ERR_MISSING_DEP ulong 7 |
| 23 | const CLS_ERR_CYCLE ulong 8 |
| 24 | const CLS_ERR_EMPTY ulong 9 |
| 25 | const CLS_ERR_UNOBSERVED ulong 10 |
| 26 | |
| 27 | text cls_root_dom "AIEN-CLOSURE-V1" |
| 28 | |
| 29 | note Metadata: [0]=pin_cnt, [1]=edge_cnt, [2]=workspace_dirty(0/1), [3]=ok_cnt, [4]=deny_cnt |
| 30 | global cls_meta ulong 8 |
| 31 | |
| 32 | note Pinned artifact table (0..31) |
| 33 | global cls_pin_id ulong 32 |
| 34 | global cls_pin_dirty ulong 32 |
| 35 | global cls_pin_obs ulong 32 |
| 36 | global cls_exp_hash uchar 1024 |
| 37 | global cls_obs_hash uchar 1024 |
| 38 | |
| 39 | note Dependency graph edges (0..63) |
| 40 | global cls_edge_from ulong 64 |
| 41 | global cls_edge_to ulong 64 |
| 42 | |
| 43 | note Scratch arrays for Kahn's cycle detection |
| 44 | global cls_indeg ulong 32 |
| 45 | global cls_done ulong 32 |
| 46 | |
| 47 | fn cls_init to void |
| 48 | for i ulong 8 |
| 49 | put cls_meta i 0 |
| 50 | end |
| 51 | for i ulong CLS_MAX_PINS |
| 52 | put cls_pin_id i 0 |
| 53 | put cls_pin_dirty i 0 |
| 54 | put cls_pin_obs i 0 |
| 55 | put cls_indeg i 0 |
| 56 | put cls_done i 0 |
| 57 | end |
| 58 | for i ulong CLS_HASH_TOTAL |
| 59 | put cls_exp_hash i uchar 0 |
| 60 | put cls_obs_hash i uchar 0 |
| 61 | end |
| 62 | for i ulong CLS_MAX_EDGES |
| 63 | put cls_edge_from i 0 |
| 64 | put cls_edge_to i 0 |
| 65 | end |
| 66 | end |
| 67 | |
| 68 | fn cls_set_workspace_dirty dirty ulong to void |
| 69 | put cls_meta 2 dirty |
| 70 | end |
| 71 | |
| 72 | fn cls_pin_count to ulong |
| 73 | ret get cls_meta 0 |
| 74 | end |
| 75 | |
| 76 | fn cls_edge_count to ulong |
| 77 | ret get cls_meta 1 |
| 78 | end |
| 79 | |
| 80 | fn cls_ok_count to ulong |
| 81 | ret get cls_meta 3 |
| 82 | end |
| 83 | |
| 84 | fn cls_deny_count to ulong |
| 85 | ret get cls_meta 4 |
| 86 | end |
| 87 | |
| 88 | fn cls_is_zero_hash h view uchar 32 to bool |
| 89 | acc = uchar 0 |
| 90 | for i ulong 32 |
| 91 | acc = or acc get h i |
| 92 | end |
| 93 | ret eq acc uchar 0 |
| 94 | end |
| 95 | |
| 96 | fn cls_hash_eq a view uchar 32 b view uchar 32 to bool |
| 97 | diff = uchar 0 |
| 98 | for i ulong 32 |
| 99 | diff = or diff xor get a i get b i |
| 100 | end |
| 101 | ret eq diff uchar 0 |
| 102 | end |
| 103 | |
| 104 | fn cls_put_u64 buf edit uchar * off ulong v ulong to void |
| 105 | for i ulong 8 |
| 106 | sh = mul i 8 |
| 107 | put buf add off i uchar and shr v sh ulong 255 |
| 108 | end |
| 109 | end |
| 110 | |
| 111 | fn cls_find_pin art_id ulong to ulong |
| 112 | cnt = get cls_meta 0 |
| 113 | for i ulong cnt bound 32 |
| 114 | if eq get cls_pin_id i art_id |
| 115 | ret i |
| 116 | end |
| 117 | end |
| 118 | ret CLS_NONE |
| 119 | end |
| 120 | |
| 121 | note Register a pinned artifact with its expected SHA-256 digest and per-artifact dirty flag. |
| 122 | fn cls_add_pin art_id ulong exp_hash view uchar 32 dirty ulong to ulong |
| 123 | if cls_is_zero_hash exp_hash |
| 124 | ret CLS_ERR_MISSING_DIGEST |
| 125 | end |
| 126 | if ne cls_find_pin art_id CLS_NONE |
| 127 | ret CLS_ERR_DUP_PIN |
| 128 | end |
| 129 | cnt = get cls_meta 0 |
| 130 | if ge cnt CLS_MAX_PINS |
| 131 | ret CLS_ERR_FULL |
| 132 | end |
| 133 | put cls_pin_id cnt art_id |
| 134 | put cls_pin_dirty cnt dirty |
| 135 | put cls_pin_obs cnt 0 |
| 136 | hbase = mul cnt CLS_HASH_BYTES |
| 137 | for i ulong 32 |
| 138 | put cls_exp_hash add hbase i get exp_hash i |
| 139 | put cls_obs_hash add hbase i uchar 0 |
| 140 | end |
| 141 | put cls_meta 0 add cnt 1 |
| 142 | ret CLS_OK |
| 143 | end |
| 144 | |
| 145 | note Register a directed dependency edge from_id -> to_id. |
| 146 | fn cls_add_dep from_id ulong to_id ulong to ulong |
| 147 | ecnt = get cls_meta 1 |
| 148 | if ge ecnt CLS_MAX_EDGES |
| 149 | ret CLS_ERR_FULL |
| 150 | end |
| 151 | put cls_edge_from ecnt from_id |
| 152 | put cls_edge_to ecnt to_id |
| 153 | put cls_meta 1 add ecnt 1 |
| 154 | ret CLS_OK |
| 155 | end |
| 156 | |
| 157 | note Record an observed 32-byte SHA-256 digest for a pinned artifact. |
| 158 | fn cls_observe_digest art_id ulong obs_hash view uchar 32 to ulong |
| 159 | idx = cls_find_pin art_id |
| 160 | if eq idx CLS_NONE |
| 161 | ret CLS_ERR_NOT_FOUND |
| 162 | end |
| 163 | if cls_is_zero_hash obs_hash |
| 164 | ret CLS_ERR_MISSING_DIGEST |
| 165 | end |
| 166 | hbase = mul idx CLS_HASH_BYTES |
| 167 | for i ulong 32 |
| 168 | put cls_obs_hash add hbase i get obs_hash i |
| 169 | end |
| 170 | put cls_pin_obs idx 1 |
| 171 | ret CLS_OK |
| 172 | end |
| 173 | |
| 174 | note Hash payload bytes with SHA-256 and record as the observed digest for art_id. |
| 175 | fn cls_observe_payload art_id ulong payload view uchar * to ulong |
| 176 | idx = cls_find_pin art_id |
| 177 | if eq idx CLS_NONE |
| 178 | ret CLS_ERR_NOT_FOUND |
| 179 | end |
| 180 | dig = new uchar 32 0 |
| 181 | ctx = new ulong 40 0 |
| 182 | sha256_init ctx |
| 183 | sha256_update ctx payload |
| 184 | sha256_final ctx dig |
| 185 | ret cls_observe_digest art_id dig |
| 186 | end |
| 187 | |
| 188 | note Verify one pinned artifact slot: clean flag, observed, non-zero, constant-time digest match. |
| 189 | fn cls_check_pin idx ulong to ulong |
| 190 | if ne get cls_pin_dirty idx 0 |
| 191 | ret CLS_ERR_DIRTY_TREE |
| 192 | end |
| 193 | if eq get cls_pin_obs idx 0 |
| 194 | ret CLS_ERR_UNOBSERVED |
| 195 | end |
| 196 | exp_h = new uchar 32 0 |
| 197 | obs_h = new uchar 32 0 |
| 198 | hbase = mul idx CLS_HASH_BYTES |
| 199 | for i ulong 32 |
| 200 | put exp_h i get cls_exp_hash add hbase i |
| 201 | put obs_h i get cls_obs_hash add hbase i |
| 202 | end |
| 203 | if or cls_is_zero_hash exp_h cls_is_zero_hash obs_h |
| 204 | ret CLS_ERR_MISSING_DIGEST |
| 205 | end |
| 206 | if not cls_hash_eq exp_h obs_h |
| 207 | ret CLS_ERR_DIGEST_MISMATCH |
| 208 | end |
| 209 | ret CLS_OK |
| 210 | end |
| 211 | |
| 212 | note Decrement in-degrees of all targets reachable from node u_idx. |
| 213 | fn cls_peel_node u_idx ulong ecnt ulong to void |
| 214 | uid = get cls_pin_id u_idx |
| 215 | for e ulong ecnt bound 64 |
| 216 | if eq get cls_edge_from e uid |
| 217 | v_idx = cls_find_pin get cls_edge_to e |
| 218 | if ne v_idx CLS_NONE |
| 219 | deg = get cls_indeg v_idx |
| 220 | if gt deg 0 |
| 221 | put cls_indeg v_idx sub deg 1 |
| 222 | end |
| 223 | end |
| 224 | end |
| 225 | end |
| 226 | end |
| 227 | |
| 228 | note Verify dependency graph: every edge endpoint exists and the graph is acyclic (Kahn's sort). |
| 229 | fn cls_check_graph to ulong |
| 230 | cnt = get cls_meta 0 |
| 231 | ecnt = get cls_meta 1 |
| 232 | for i ulong cnt bound 32 |
| 233 | put cls_indeg i 0 |
| 234 | put cls_done i 0 |
| 235 | end |
| 236 | for e ulong ecnt bound 64 |
| 237 | u_idx = cls_find_pin get cls_edge_from e |
| 238 | v_idx = cls_find_pin get cls_edge_to e |
| 239 | if or eq u_idx CLS_NONE eq v_idx CLS_NONE |
| 240 | ret CLS_ERR_MISSING_DEP |
| 241 | end |
| 242 | put cls_indeg v_idx add get cls_indeg v_idx 1 |
| 243 | end |
| 244 | removed = ulong 0 |
| 245 | for step ulong cnt bound 32 |
| 246 | found = CLS_NONE |
| 247 | for i ulong cnt bound 32 |
| 248 | if and eq get cls_done i 0 eq get cls_indeg i 0 |
| 249 | found = i |
| 250 | break |
| 251 | end |
| 252 | end |
| 253 | if eq found CLS_NONE |
| 254 | ret CLS_ERR_CYCLE |
| 255 | end |
| 256 | put cls_done found 1 |
| 257 | removed = add removed 1 |
| 258 | cls_peel_node found ecnt |
| 259 | end |
| 260 | if ne removed cnt |
| 261 | ret CLS_ERR_CYCLE |
| 262 | end |
| 263 | ret CLS_OK |
| 264 | end |
| 265 | |
| 266 | note Compute deterministic SHA-256 closure root over all verified pins. |
| 267 | fn cls_compute_root out_root edit uchar 32 to void |
| 268 | ctx = new ulong 40 0 |
| 269 | sha256_init ctx |
| 270 | sha256_update ctx cls_root_dom |
| 271 | cnt = get cls_meta 0 |
| 272 | hdr = new uchar 8 0 |
| 273 | cls_put_u64 hdr 0 cnt |
| 274 | sha256_update ctx hdr |
| 275 | hbuf = new uchar 32 0 |
| 276 | for i ulong cnt bound 32 |
| 277 | cls_put_u64 hdr 0 get cls_pin_id i |
| 278 | sha256_update ctx hdr |
| 279 | hbase = mul i CLS_HASH_BYTES |
| 280 | for j ulong 32 |
| 281 | put hbuf j get cls_exp_hash add hbase j |
| 282 | end |
| 283 | sha256_update ctx hbuf |
| 284 | end |
| 285 | sha256_final ctx out_root |
| 286 | end |
| 287 | |
| 288 | fn cls_record_verdict st ulong to ulong |
| 289 | if eq st CLS_OK |
| 290 | put cls_meta 3 add get cls_meta 3 1 |
| 291 | else |
| 292 | put cls_meta 4 add get cls_meta 4 1 |
| 293 | end |
| 294 | ret st |
| 295 | end |
| 296 | |
| 297 | note Fail-closed closure verification: checks emptiness, dirty flags, all pin digests, and DAG acyclicity. |
| 298 | note Writes 32-byte canonical closure root into out_root only on CLS_OK (zeroes out_root on failure). |
| 299 | fn cls_verify_closure out_root edit uchar 32 to ulong |
| 300 | for i ulong 32 |
| 301 | put out_root i uchar 0 |
| 302 | end |
| 303 | cnt = get cls_meta 0 |
| 304 | if eq cnt 0 |
| 305 | ret cls_record_verdict CLS_ERR_EMPTY |
| 306 | end |
| 307 | if ne get cls_meta 2 0 |
| 308 | ret cls_record_verdict CLS_ERR_DIRTY_TREE |
| 309 | end |
| 310 | for i ulong cnt bound 32 |
| 311 | st = cls_check_pin i |
| 312 | if ne st CLS_OK |
| 313 | ret cls_record_verdict st |
| 314 | end |
| 315 | end |
| 316 | gst = cls_check_graph |
| 317 | if ne gst CLS_OK |
| 318 | ret cls_record_verdict gst |
| 319 | end |
| 320 | cls_compute_root out_root |
| 321 | ret cls_record_verdict CLS_OK |
| 322 | end |