aien-sovereign-core-mv
AienOS host runtime: inference scheduling, paged KV state, the aien CLI and Spark verification tooling. Its inference engine is in alpha testing (DGX Spark GB10); Omega is the implementation destination.
Brought by drakestapleton
aien-sovereign-core-mv / om / store / closure_test.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note core/sovereign-core/om/store/closure_test.om |
| 3 | note Positive, negative, and mutant test suite for core/sovereign-core/om/store/closure.om. |
| 4 | note Compose with: |
| 5 | note si/directives/om2.om om/tls/sha2.om core/sovereign-core/om/store/closure.om |
| 6 | note core/sovereign-core/om/store/closure_test.om |
| 7 | |
| 8 | text cls_t_pass "PASS " |
| 9 | text cls_t_fail "FAIL " |
| 10 | text cls_t_nl "\n" |
| 11 | text cls_t_all_ok "CLOSURE TEST SUITE PASS\n" |
| 12 | text cls_t_all_bad "CLOSURE TEST SUITE FAIL\n" |
| 13 | |
| 14 | text cls_t_n1 "cls_pos_verified_dag_and_deterministic_root" |
| 15 | text cls_t_n2 "cls_neg_dirty_workspace_and_dirty_artifact" |
| 16 | text cls_t_n3 "cls_neg_missing_unobserved_and_mismatched_digest" |
| 17 | text cls_t_n4 "cls_neg_empty_missing_dep_and_cycle_detection" |
| 18 | text cls_t_n5 "cls_mutant_pin_tamper_and_root_sensitivity" |
| 19 | |
| 20 | text cls_art_a "artifact:alpha:v1:seed-omc" |
| 21 | text cls_art_b "artifact:beta:v1:runtime-core" |
| 22 | text cls_art_c "artifact:gamma:v1:closure-leaf" |
| 23 | text cls_art_b_bad "artifact:beta:v1:runtime-corX" |
| 24 | |
| 25 | global cls_t_fails ulong 1 |
| 26 | |
| 27 | fn cls_t_w s view uchar * to void |
| 28 | n = len s |
| 29 | off = ulong 0 |
| 30 | while lt off n bound 256 |
| 31 | k = write 1 s off sub n off |
| 32 | if le k 0 |
| 33 | break |
| 34 | end |
| 35 | off = add off ulong k |
| 36 | end |
| 37 | end |
| 38 | |
| 39 | fn cls_t_rep name view uchar * ok bool to void |
| 40 | if ok |
| 41 | cls_t_w cls_t_pass |
| 42 | else |
| 43 | cls_t_w cls_t_fail |
| 44 | put cls_t_fails 0 add get cls_t_fails 0 1 |
| 45 | end |
| 46 | cls_t_w name |
| 47 | cls_t_w cls_t_nl |
| 48 | end |
| 49 | |
| 50 | fn cls_t_sha msg view uchar * out_h edit uchar 32 to void |
| 51 | ctx = new ulong 40 0 |
| 52 | sha256_init ctx |
| 53 | sha256_update ctx msg |
| 54 | sha256_final ctx out_h |
| 55 | end |
| 56 | |
| 57 | note Test 1 (POSITIVE): 3-node DAG (10 -> 20 -> 30), clean tree, matching SHA-256 digests, deterministic root. |
| 58 | fn test_cls_pos_dag to bool |
| 59 | cls_init |
| 60 | ha = new uchar 32 0 |
| 61 | hb = new uchar 32 0 |
| 62 | hc = new uchar 32 0 |
| 63 | cls_t_sha cls_art_a ha |
| 64 | cls_t_sha cls_art_b hb |
| 65 | cls_t_sha cls_art_c hc |
| 66 | if ne cls_add_pin 10 ha 0 CLS_OK |
| 67 | ret false |
| 68 | end |
| 69 | if ne cls_add_pin 20 hb 0 CLS_OK |
| 70 | ret false |
| 71 | end |
| 72 | if ne cls_add_pin 30 hc 0 CLS_OK |
| 73 | ret false |
| 74 | end |
| 75 | if ne cls_add_dep 10 20 CLS_OK |
| 76 | ret false |
| 77 | end |
| 78 | if ne cls_add_dep 20 30 CLS_OK |
| 79 | ret false |
| 80 | end |
| 81 | if ne cls_observe_payload 10 cls_art_a CLS_OK |
| 82 | ret false |
| 83 | end |
| 84 | if ne cls_observe_payload 20 cls_art_b CLS_OK |
| 85 | ret false |
| 86 | end |
| 87 | if ne cls_observe_payload 30 cls_art_c CLS_OK |
| 88 | ret false |
| 89 | end |
| 90 | r1 = new uchar 32 0 |
| 91 | r2 = new uchar 32 0 |
| 92 | if ne cls_verify_closure r1 CLS_OK |
| 93 | ret false |
| 94 | end |
| 95 | if cls_is_zero_hash r1 |
| 96 | ret false |
| 97 | end |
| 98 | if ne cls_verify_closure r2 CLS_OK |
| 99 | ret false |
| 100 | end |
| 101 | if not cls_hash_eq r1 r2 |
| 102 | ret false |
| 103 | end |
| 104 | if ne cls_ok_count 2 |
| 105 | ret false |
| 106 | end |
| 107 | ret true |
| 108 | end |
| 109 | |
| 110 | note Test 2 (NEGATIVE): Dirty workspace flag and per-artifact dirty flag both refused. |
| 111 | fn test_cls_neg_dirty to bool |
| 112 | cls_init |
| 113 | ha = new uchar 32 0 |
| 114 | cls_t_sha cls_art_a ha |
| 115 | if ne cls_add_pin 10 ha 0 CLS_OK |
| 116 | ret false |
| 117 | end |
| 118 | if ne cls_observe_payload 10 cls_art_a CLS_OK |
| 119 | ret false |
| 120 | end |
| 121 | cls_set_workspace_dirty 1 |
| 122 | root = new uchar 32 0 |
| 123 | if ne cls_verify_closure root CLS_ERR_DIRTY_TREE |
| 124 | ret false |
| 125 | end |
| 126 | if not cls_is_zero_hash root |
| 127 | ret false |
| 128 | end |
| 129 | note Clear workspace dirty, add a dirty artifact -> still refused |
| 130 | cls_set_workspace_dirty 0 |
| 131 | hb = new uchar 32 0 |
| 132 | cls_t_sha cls_art_b hb |
| 133 | if ne cls_add_pin 20 hb 1 CLS_OK |
| 134 | ret false |
| 135 | end |
| 136 | if ne cls_observe_payload 20 cls_art_b CLS_OK |
| 137 | ret false |
| 138 | end |
| 139 | if ne cls_verify_closure root CLS_ERR_DIRTY_TREE |
| 140 | ret false |
| 141 | end |
| 142 | if ne cls_deny_count 2 |
| 143 | ret false |
| 144 | end |
| 145 | ret true |
| 146 | end |
| 147 | |
| 148 | note Test 3 (NEGATIVE): Missing/zero digest, duplicate pin, unobserved pin, mismatched payload digest. |
| 149 | fn test_cls_neg_digests to bool |
| 150 | cls_init |
| 151 | zero_h = new uchar 32 0 |
| 152 | if ne cls_add_pin 10 zero_h 0 CLS_ERR_MISSING_DIGEST |
| 153 | ret false |
| 154 | end |
| 155 | ha = new uchar 32 0 |
| 156 | hb = new uchar 32 0 |
| 157 | cls_t_sha cls_art_a ha |
| 158 | cls_t_sha cls_art_b hb |
| 159 | if ne cls_add_pin 10 ha 0 CLS_OK |
| 160 | ret false |
| 161 | end |
| 162 | if ne cls_add_pin 10 ha 0 CLS_ERR_DUP_PIN |
| 163 | ret false |
| 164 | end |
| 165 | if ne cls_observe_digest 10 zero_h CLS_ERR_MISSING_DIGEST |
| 166 | ret false |
| 167 | end |
| 168 | if ne cls_observe_digest 999 ha CLS_ERR_NOT_FOUND |
| 169 | ret false |
| 170 | end |
| 171 | note Unobserved pin must fail verification |
| 172 | root = new uchar 32 0 |
| 173 | if ne cls_verify_closure root CLS_ERR_UNOBSERVED |
| 174 | ret false |
| 175 | end |
| 176 | note Observe tampered payload -> CLS_ERR_DIGEST_MISMATCH |
| 177 | if ne cls_observe_payload 10 cls_art_b_bad CLS_OK |
| 178 | ret false |
| 179 | end |
| 180 | if ne cls_verify_closure root CLS_ERR_DIGEST_MISMATCH |
| 181 | ret false |
| 182 | end |
| 183 | if not cls_is_zero_hash root |
| 184 | ret false |
| 185 | end |
| 186 | ret true |
| 187 | end |
| 188 | |
| 189 | note Test 4 (NEGATIVE): Empty closure, missing dependency node, and 3-node cycle refusal. |
| 190 | fn test_cls_neg_graph to bool |
| 191 | cls_init |
| 192 | root = new uchar 32 0 |
| 193 | if ne cls_verify_closure root CLS_ERR_EMPTY |
| 194 | ret false |
| 195 | end |
| 196 | ha = new uchar 32 0 |
| 197 | hb = new uchar 32 0 |
| 198 | hc = new uchar 32 0 |
| 199 | cls_t_sha cls_art_a ha |
| 200 | cls_t_sha cls_art_b hb |
| 201 | cls_t_sha cls_art_c hc |
| 202 | if ne cls_add_pin 1 ha 0 CLS_OK |
| 203 | ret false |
| 204 | end |
| 205 | if ne cls_add_pin 2 hb 0 CLS_OK |
| 206 | ret false |
| 207 | end |
| 208 | if ne cls_observe_payload 1 cls_art_a CLS_OK |
| 209 | ret false |
| 210 | end |
| 211 | if ne cls_observe_payload 2 cls_art_b CLS_OK |
| 212 | ret false |
| 213 | end |
| 214 | note Missing dep node 99 |
| 215 | if ne cls_add_dep 1 99 CLS_OK |
| 216 | ret false |
| 217 | end |
| 218 | if ne cls_verify_closure root CLS_ERR_MISSING_DEP |
| 219 | ret false |
| 220 | end |
| 221 | note Rebuild with 3-node cycle 1 -> 2 -> 3 -> 1 |
| 222 | cls_init |
| 223 | if ne cls_add_pin 1 ha 0 CLS_OK |
| 224 | ret false |
| 225 | end |
| 226 | if ne cls_add_pin 2 hb 0 CLS_OK |
| 227 | ret false |
| 228 | end |
| 229 | if ne cls_add_pin 3 hc 0 CLS_OK |
| 230 | ret false |
| 231 | end |
| 232 | if ne cls_observe_payload 1 cls_art_a CLS_OK |
| 233 | ret false |
| 234 | end |
| 235 | if ne cls_observe_payload 2 cls_art_b CLS_OK |
| 236 | ret false |
| 237 | end |
| 238 | if ne cls_observe_payload 3 cls_art_c CLS_OK |
| 239 | ret false |
| 240 | end |
| 241 | if ne cls_add_dep 1 2 CLS_OK |
| 242 | ret false |
| 243 | end |
| 244 | if ne cls_add_dep 2 3 CLS_OK |
| 245 | ret false |
| 246 | end |
| 247 | if ne cls_add_dep 3 1 CLS_OK |
| 248 | ret false |
| 249 | end |
| 250 | if ne cls_verify_closure root CLS_ERR_CYCLE |
| 251 | ret false |
| 252 | end |
| 253 | ret true |
| 254 | end |
| 255 | |
| 256 | note Test 5 (MUTANT CONTROLS): 1-bit tamper in pinned expected digest fails; changed artifact changes root. |
| 257 | fn test_cls_mutant_controls to bool |
| 258 | cls_init |
| 259 | ha = new uchar 32 0 |
| 260 | hb = new uchar 32 0 |
| 261 | cls_t_sha cls_art_a ha |
| 262 | cls_t_sha cls_art_b hb |
| 263 | if ne cls_add_pin 10 ha 0 CLS_OK |
| 264 | ret false |
| 265 | end |
| 266 | if ne cls_observe_payload 10 cls_art_a CLS_OK |
| 267 | ret false |
| 268 | end |
| 269 | r1 = new uchar 32 0 |
| 270 | if ne cls_verify_closure r1 CLS_OK |
| 271 | ret false |
| 272 | end |
| 273 | note Mutant A: flip 1 bit in stored expected hash of pin 0 -> mismatch |
| 274 | orig = get cls_exp_hash 15 |
| 275 | put cls_exp_hash 15 xor orig uchar 1 |
| 276 | r_bad = new uchar 32 0 |
| 277 | if ne cls_verify_closure r_bad CLS_ERR_DIGEST_MISMATCH |
| 278 | ret false |
| 279 | end |
| 280 | put cls_exp_hash 15 orig |
| 281 | note Mutant B: replace artifact 10 with beta (matching pin & payload) -> root must differ |
| 282 | cls_init |
| 283 | if ne cls_add_pin 10 hb 0 CLS_OK |
| 284 | ret false |
| 285 | end |
| 286 | if ne cls_observe_payload 10 cls_art_b CLS_OK |
| 287 | ret false |
| 288 | end |
| 289 | r2 = new uchar 32 0 |
| 290 | if ne cls_verify_closure r2 CLS_OK |
| 291 | ret false |
| 292 | end |
| 293 | if cls_hash_eq r1 r2 |
| 294 | ret false |
| 295 | end |
| 296 | ret true |
| 297 | end |
| 298 | |
| 299 | fn main to int |
| 300 | put cls_t_fails 0 0 |
| 301 | cls_t_rep cls_t_n1 test_cls_pos_dag |
| 302 | cls_t_rep cls_t_n2 test_cls_neg_dirty |
| 303 | cls_t_rep cls_t_n3 test_cls_neg_digests |
| 304 | cls_t_rep cls_t_n4 test_cls_neg_graph |
| 305 | cls_t_rep cls_t_n5 test_cls_mutant_controls |
| 306 | if eq get cls_t_fails 0 0 |
| 307 | cls_t_w cls_t_all_ok |
| 308 | ret 0 |
| 309 | end |
| 310 | cls_t_w cls_t_all_bad |
| 311 | ret 1 |
| 312 | end |