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 / seed / ng / pack / gate_pack.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/pack/gate_pack.om: Program-mode verification gate for OMPK v1 Semantic Packaging, |
| 3 | note DAG dependency resolution, Compile-on-Arrive TargetDesc realization, semantic deduplication, |
| 4 | note and 7 fail-closed negative controls. |
| 5 | note |
| 6 | note Link order: |
| 7 | note seed/ng/sha256.om seed/ng/target/target_desc.om seed/ng/pack/sem_pack.om seed/ng/pack/gate_pack.om |
| 8 | |
| 9 | global gp_dig uchar 24 |
| 10 | global gp_chk ulong 1 |
| 11 | global gp_neg ulong 1 |
| 12 | global gp_slots ulong 16 |
| 13 | global gp_tmp_a uchar 32 |
| 14 | global gp_tmp_b uchar 32 |
| 15 | |
| 16 | text gp_msg_pass "NG-PACK-GATE PASS checks=" |
| 17 | text gp_msg_dedup " dedup_hits=" |
| 18 | text gp_msg_neg " neg_controls=" |
| 19 | text gp_msg_of7 "/7\n" |
| 20 | text gp_msg_fail "NG-PACK-GATE FAIL at check=" |
| 21 | text gp_msg_nl "\n" |
| 22 | |
| 23 | note Canonical OIR sample payloads for DAG, multi-target, semantic dedup, and negative controls |
| 24 | text gp_ir_leaf "OIR1:fn leaf(x:u64)->u64 { add x 1 }" |
| 25 | text gp_ir_mid "OIR1:fn mid(x:u64)->u64 { mul (call leaf x) 2 }" |
| 26 | text gp_ir_root "OIR1:fn root(x:u64)->u64 { add (call mid x) 7 }" |
| 27 | text gp_ir_sm121 "OIR1:kernel w4_gemm_m4(A,B,C) smem=32768 warp=32 hmma16816" |
| 28 | text gp_ir_poly_a "OIR1:fn f_alpha(x:u64)->u64 { add (mul x 2) 2 }" |
| 29 | text gp_ir_poly_b "OIR1:fn g_beta(y:u64)->u64 { mul (add y 1) 2 }" |
| 30 | text gp_ir_zero_fid "OIR1:fn fallback_sem_unit(z:u64)->u64 { xor z 42 }" |
| 31 | text gp_ir_orphan "OIR1:fn orphan_caller(x:u64)->u64 { call missing_dep x }" |
| 32 | text gp_ir_cyc_a "OIR1:fn cycle_node_a(x:u64)->u64 { call cycle_node_b x }" |
| 33 | text gp_ir_cyc_b "OIR1:fn cycle_node_b(x:u64)->u64 { call cycle_node_a x }" |
| 34 | text gp_ir_cyc_c "OIR1:fn cycle_self_c(x:u64)->u64 { call cycle_self_c x }" |
| 35 | text gp_ir_bad_smem "OIR1:kernel huge_smem_fail smem=131072 warp=32" |
| 36 | text gp_ir_bad_mma "OIR1:kernel amx_on_avx2_fail mma=amx161616" |
| 37 | text gp_ir_unproven "OIR1:fn unproven_candidate(x:u64)->u64 { add x 99 }" |
| 38 | |
| 39 | fn gp_say s ulong to void |
| 40 | if eq s 0 |
| 41 | w = write 1 gp_msg_pass 0 len gp_msg_pass |
| 42 | ret |
| 43 | end |
| 44 | if eq s 1 |
| 45 | w = write 1 gp_msg_dedup 0 len gp_msg_dedup |
| 46 | ret |
| 47 | end |
| 48 | if eq s 2 |
| 49 | w = write 1 gp_msg_neg 0 len gp_msg_neg |
| 50 | ret |
| 51 | end |
| 52 | if eq s 3 |
| 53 | w = write 1 gp_msg_of7 0 len gp_msg_of7 |
| 54 | ret |
| 55 | end |
| 56 | if eq s 4 |
| 57 | w = write 1 gp_msg_fail 0 len gp_msg_fail |
| 58 | ret |
| 59 | end |
| 60 | w = write 1 gp_msg_nl 0 len gp_msg_nl |
| 61 | end |
| 62 | |
| 63 | fn gp_num v ulong to void |
| 64 | x = v |
| 65 | p = ulong 24 |
| 66 | for i ulong 21 |
| 67 | p = sub p 1 |
| 68 | put gp_dig p uchar add 48 rem x 10 |
| 69 | x = div x 10 |
| 70 | if eq x 0 |
| 71 | w = write 1 gp_dig p sub 24 p |
| 72 | ret |
| 73 | end |
| 74 | end |
| 75 | end |
| 76 | |
| 77 | fn gp_fail id ulong to int |
| 78 | gp_say 4 |
| 79 | gp_num id |
| 80 | gp_say 5 |
| 81 | ret int id |
| 82 | end |
| 83 | |
| 84 | fn gp_add_chk n ulong to void |
| 85 | put gp_chk 0 add get gp_chk 0 n |
| 86 | end |
| 87 | |
| 88 | fn gp_add_neg to void |
| 89 | put gp_neg 0 add get gp_neg 0 1 |
| 90 | end |
| 91 | |
| 92 | note Load one of the static IR text fixtures into pk_ir_in |
| 93 | fn gp_load_ir_a id ulong to void |
| 94 | pk_ir_reset |
| 95 | if eq id 0 |
| 96 | n = len gp_ir_leaf |
| 97 | for i ulong n bound 256 |
| 98 | rc = pk_ir_push get gp_ir_leaf i |
| 99 | end |
| 100 | ret |
| 101 | end |
| 102 | if eq id 1 |
| 103 | n = len gp_ir_mid |
| 104 | for i ulong n bound 256 |
| 105 | rc = pk_ir_push get gp_ir_mid i |
| 106 | end |
| 107 | ret |
| 108 | end |
| 109 | if eq id 2 |
| 110 | n = len gp_ir_root |
| 111 | for i ulong n bound 256 |
| 112 | rc = pk_ir_push get gp_ir_root i |
| 113 | end |
| 114 | ret |
| 115 | end |
| 116 | if eq id 3 |
| 117 | n = len gp_ir_sm121 |
| 118 | for i ulong n bound 256 |
| 119 | rc = pk_ir_push get gp_ir_sm121 i |
| 120 | end |
| 121 | ret |
| 122 | end |
| 123 | if eq id 4 |
| 124 | n = len gp_ir_poly_a |
| 125 | for i ulong n bound 256 |
| 126 | rc = pk_ir_push get gp_ir_poly_a i |
| 127 | end |
| 128 | ret |
| 129 | end |
| 130 | if eq id 5 |
| 131 | n = len gp_ir_poly_b |
| 132 | for i ulong n bound 256 |
| 133 | rc = pk_ir_push get gp_ir_poly_b i |
| 134 | end |
| 135 | ret |
| 136 | end |
| 137 | if eq id 6 |
| 138 | n = len gp_ir_zero_fid |
| 139 | for i ulong n bound 256 |
| 140 | rc = pk_ir_push get gp_ir_zero_fid i |
| 141 | end |
| 142 | ret |
| 143 | end |
| 144 | end |
| 145 | |
| 146 | fn gp_load_ir_b id ulong to void |
| 147 | pk_ir_reset |
| 148 | if eq id 7 |
| 149 | n = len gp_ir_orphan |
| 150 | for i ulong n bound 256 |
| 151 | rc = pk_ir_push get gp_ir_orphan i |
| 152 | end |
| 153 | ret |
| 154 | end |
| 155 | if eq id 8 |
| 156 | n = len gp_ir_cyc_a |
| 157 | for i ulong n bound 256 |
| 158 | rc = pk_ir_push get gp_ir_cyc_a i |
| 159 | end |
| 160 | ret |
| 161 | end |
| 162 | if eq id 9 |
| 163 | n = len gp_ir_cyc_b |
| 164 | for i ulong n bound 256 |
| 165 | rc = pk_ir_push get gp_ir_cyc_b i |
| 166 | end |
| 167 | ret |
| 168 | end |
| 169 | if eq id 10 |
| 170 | n = len gp_ir_cyc_c |
| 171 | for i ulong n bound 256 |
| 172 | rc = pk_ir_push get gp_ir_cyc_c i |
| 173 | end |
| 174 | ret |
| 175 | end |
| 176 | if eq id 11 |
| 177 | n = len gp_ir_bad_smem |
| 178 | for i ulong n bound 256 |
| 179 | rc = pk_ir_push get gp_ir_bad_smem i |
| 180 | end |
| 181 | ret |
| 182 | end |
| 183 | if eq id 12 |
| 184 | n = len gp_ir_bad_mma |
| 185 | for i ulong n bound 256 |
| 186 | rc = pk_ir_push get gp_ir_bad_mma i |
| 187 | end |
| 188 | ret |
| 189 | end |
| 190 | n = len gp_ir_unproven |
| 191 | for i ulong n bound 256 |
| 192 | rc = pk_ir_push get gp_ir_unproven i |
| 193 | end |
| 194 | end |
| 195 | |
| 196 | fn gp_load_ir id ulong to void |
| 197 | if le id 6 |
| 198 | gp_load_ir_a id |
| 199 | ret |
| 200 | end |
| 201 | gp_load_ir_b id |
| 202 | end |
| 203 | |
| 204 | fn gp_buf_eq32 offA ulong offB ulong to bool |
| 205 | for i ulong 32 |
| 206 | if ne get pk_buf add offA i get pk_buf add offB i |
| 207 | ret false |
| 208 | end |
| 209 | end |
| 210 | ret true |
| 211 | end |
| 212 | |
| 213 | fn gp_tmp_eq32 to bool |
| 214 | for i ulong 32 |
| 215 | if ne get gp_tmp_a i get gp_tmp_b i |
| 216 | ret false |
| 217 | end |
| 218 | end |
| 219 | ret true |
| 220 | end |
| 221 | |
| 222 | note ---- Test 0: Exact OMPK v1 Wire Layout & FIPS 180-4 SHA-256 ("abc") Check ---- |
| 223 | |
| 224 | fn gp_test_wire_exact to int |
| 225 | pk_reset_builder |
| 226 | rc = pk_ir_push uchar 97 |
| 227 | rc = pk_ir_push uchar 98 |
| 228 | rc = pk_ir_push uchar 99 |
| 229 | pk_set_req EXEC_SIMT_WARP 32768 64 32 MMA_16816_BF16 FEAT_HMMA16816 |
| 230 | if ne pk_pack PK_OK |
| 231 | ret gp_fail 101 |
| 232 | end |
| 233 | note Check exact magic "OMPK" (79,77,80,75) and version u32 LE == 1 |
| 234 | if or ne ulong get pk_buf 0 79 ne ulong get pk_buf 1 77 |
| 235 | ret gp_fail 102 |
| 236 | end |
| 237 | if or ne ulong get pk_buf 2 80 ne ulong get pk_buf 3 75 |
| 238 | ret gp_fail 103 |
| 239 | end |
| 240 | if ne pk_get_u32 4 1 |
| 241 | ret gp_fail 104 |
| 242 | end |
| 243 | note Check FIPS 180-4 SHA-256("abc") = ba7816bf...f20015ad in sem_id [8..39] |
| 244 | if or ne ulong get pk_buf 8 186 ne ulong get pk_buf 9 120 |
| 245 | ret gp_fail 105 |
| 246 | end |
| 247 | if or ne ulong get pk_buf 10 22 ne ulong get pk_buf 11 191 |
| 248 | ret gp_fail 106 |
| 249 | end |
| 250 | if or ne ulong get pk_buf 38 21 ne ulong get pk_buf 39 173 |
| 251 | ret gp_fail 107 |
| 252 | end |
| 253 | note Check 6x u64 LE contract fields at [72..119] |
| 254 | if or ne pk_get_u64 72 EXEC_SIMT_WARP ne pk_get_u64 80 32768 |
| 255 | ret gp_fail 108 |
| 256 | end |
| 257 | if or ne pk_get_u64 88 64 ne pk_get_u64 96 32 |
| 258 | ret gp_fail 109 |
| 259 | end |
| 260 | if or ne pk_get_u64 104 MMA_16816_BF16 ne pk_get_u64 112 FEAT_HMMA16816 |
| 261 | ret gp_fail 110 |
| 262 | end |
| 263 | note Check import_count [120..123] == 0, ir_len [124..127] == 3, payload "abc" at [128..130] |
| 264 | if or ne pk_get_u32 120 0 ne pk_get_u32 124 3 |
| 265 | ret gp_fail 111 |
| 266 | end |
| 267 | if or ne ulong get pk_buf 128 97 ne ulong get pk_buf 130 99 |
| 268 | ret gp_fail 112 |
| 269 | end |
| 270 | note Check bad magic -> PK_ERR_MAGIC and bad version -> PK_ERR_VERSION |
| 271 | put pk_buf 0 uchar 88 |
| 272 | if ne pk_verify PK_ERR_MAGIC |
| 273 | ret gp_fail 113 |
| 274 | end |
| 275 | put pk_buf 0 uchar 79 |
| 276 | pk_put_u32 4 2 |
| 277 | if ne pk_verify PK_ERR_VERSION |
| 278 | ret gp_fail 114 |
| 279 | end |
| 280 | gp_add_chk 14 |
| 281 | ret 0 |
| 282 | end |
| 283 | |
| 284 | note ---- Test 1: Wire Format + 3-Package DAG (leaf -> mid -> root) ---- |
| 285 | |
| 286 | fn gp_test_dag to int |
| 287 | note Pack leaf (no imports, default func_id == sem_id) |
| 288 | pk_reset_builder |
| 289 | gp_load_ir 0 |
| 290 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 291 | if ne pk_pack PK_OK |
| 292 | ret gp_fail 1 |
| 293 | end |
| 294 | if ne get pk_len 0 add PK_HDR_LEN len gp_ir_leaf |
| 295 | ret gp_fail 2 |
| 296 | end |
| 297 | if not gp_buf_eq32 8 40 |
| 298 | ret gp_fail 3 |
| 299 | end |
| 300 | if ne pk_verify PK_OK |
| 301 | ret gp_fail 4 |
| 302 | end |
| 303 | if ne pk_store_put PK_OK |
| 304 | ret gp_fail 5 |
| 305 | end |
| 306 | leafSlot = get pk_last_slot 0 |
| 307 | put gp_slots 0 leafSlot |
| 308 | pk_copy_store_sem_to_query leafSlot |
| 309 | if not pk_store_has |
| 310 | ret gp_fail 6 |
| 311 | end |
| 312 | note Idempotent store re-put returns same slot without duplicating |
| 313 | if ne pk_store_put PK_OK |
| 314 | ret gp_fail 7 |
| 315 | end |
| 316 | if or ne get pk_last_slot 0 leafSlot ne get pk_store_count 0 1 |
| 317 | ret gp_fail 8 |
| 318 | end |
| 319 | gp_add_chk 8 |
| 320 | |
| 321 | note Pack mid (imports leaf) |
| 322 | pk_reset_builder |
| 323 | gp_load_ir 1 |
| 324 | pk_set_req EXEC_SCALAR_SIMD 0 12 4 0 FEAT_FMA |
| 325 | if ne pk_add_import_from_store leafSlot PK_OK |
| 326 | ret gp_fail 9 |
| 327 | end |
| 328 | if ne pk_pack PK_OK |
| 329 | ret gp_fail 10 |
| 330 | end |
| 331 | if ne get pk_len 0 add add PK_HDR_LEN 32 len gp_ir_mid |
| 332 | ret gp_fail 11 |
| 333 | end |
| 334 | if ne pk_store_put PK_OK |
| 335 | ret gp_fail 12 |
| 336 | end |
| 337 | midSlot = get pk_last_slot 0 |
| 338 | put gp_slots 1 midSlot |
| 339 | gp_add_chk 4 |
| 340 | |
| 341 | note Pack root (imports mid) |
| 342 | pk_reset_builder |
| 343 | gp_load_ir 2 |
| 344 | pk_set_req EXEC_SCALAR_SIMD 0 16 4 0 FEAT_FMA |
| 345 | if ne pk_add_import_from_store midSlot PK_OK |
| 346 | ret gp_fail 13 |
| 347 | end |
| 348 | if ne pk_pack PK_OK |
| 349 | ret gp_fail 14 |
| 350 | end |
| 351 | if ne pk_store_put PK_OK |
| 352 | ret gp_fail 15 |
| 353 | end |
| 354 | rootSlot = get pk_last_slot 0 |
| 355 | put gp_slots 2 rootSlot |
| 356 | |
| 357 | note Resolve 3-package DAG rooted at root: must yield [leaf, mid, root] |
| 358 | pk_copy_store_sem_to_query rootSlot |
| 359 | if ne pk_resolve_deps PK_OK |
| 360 | ret gp_fail 16 |
| 361 | end |
| 362 | if ne get pk_topo_len 0 3 |
| 363 | ret gp_fail 17 |
| 364 | end |
| 365 | if ne get pk_topo_order 0 leafSlot |
| 366 | ret gp_fail 18 |
| 367 | end |
| 368 | if ne get pk_topo_order 1 midSlot |
| 369 | ret gp_fail 19 |
| 370 | end |
| 371 | if ne get pk_topo_order 2 rootSlot |
| 372 | ret gp_fail 20 |
| 373 | end |
| 374 | gp_add_chk 8 |
| 375 | ret 0 |
| 376 | end |
| 377 | |
| 378 | note ---- Test 2: Compile-on-Arrive Realization on TID_X925 and TID_SM121 ---- |
| 379 | |
| 380 | fn gp_test_realize_targets to int |
| 381 | rootSlot = get gp_slots 2 |
| 382 | if ne pk_store_load rootSlot PK_OK |
| 383 | ret gp_fail 21 |
| 384 | end |
| 385 | x925Feat = tdGet TID_X925 TDF_FEAT |
| 386 | if ne pk_realize_on_arrive TID_X925 x925Feat 1024 7 PK_OK |
| 387 | ret gp_fail 22 |
| 388 | end |
| 389 | if or ne get pk_real_out 2 0 ne get pk_real_out 5 1 |
| 390 | ret gp_fail 23 |
| 391 | end |
| 392 | xSlot = get pk_real_out 1 |
| 393 | xPtr = get pk_real_out 4 |
| 394 | note Repeat arrival of same package on TID_X925 hits realization cache (zero recompile) |
| 395 | if ne pk_realize_on_arrive TID_X925 x925Feat 1024 7 PK_OK |
| 396 | ret gp_fail 24 |
| 397 | end |
| 398 | if or ne get pk_real_out 2 1 ne get pk_real_out 1 xSlot |
| 399 | ret gp_fail 25 |
| 400 | end |
| 401 | if or ne get pk_real_out 4 xPtr ne get pk_real_out 5 1 |
| 402 | ret gp_fail 26 |
| 403 | end |
| 404 | |
| 405 | note Pack and realize SM121 Blackwell SIMT kernel package |
| 406 | pk_reset_builder |
| 407 | gp_load_ir 3 |
| 408 | pk_set_req EXEC_SIMT_WARP 32768 64 32 MMA_16816_BF16 FEAT_HMMA16816 |
| 409 | if ne pk_pack PK_OK |
| 410 | ret gp_fail 27 |
| 411 | end |
| 412 | if ne pk_store_put PK_OK |
| 413 | ret gp_fail 28 |
| 414 | end |
| 415 | smFeat = tdGet TID_SM121 TDF_FEAT |
| 416 | if ne pk_realize_on_arrive TID_SM121 smFeat 4096 7 PK_OK |
| 417 | ret gp_fail 29 |
| 418 | end |
| 419 | if or ne get pk_real_out 2 0 ne get pk_real_out 5 2 |
| 420 | ret gp_fail 30 |
| 421 | end |
| 422 | if eq get pk_real_out 1 xSlot |
| 423 | ret gp_fail 31 |
| 424 | end |
| 425 | gp_add_chk 11 |
| 426 | ret 0 |
| 427 | end |
| 428 | |
| 429 | note ---- Test 3: Semantic Deduplication (Different IR / Names, Identical func_id) ---- |
| 430 | |
| 431 | fn gp_test_semantic_dedup to int |
| 432 | x925Feat = tdGet TID_X925 TDF_FEAT |
| 433 | note Package A: f_alpha(x) = 2*x + 2, canonical polynomial (w=64, c0=2, c1=2, c2=0) |
| 434 | pk_reset_builder |
| 435 | gp_load_ir 4 |
| 436 | pk_compute_poly_func_id 64 2 2 0 |
| 437 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 438 | if ne pk_pack PK_OK |
| 439 | ret gp_fail 32 |
| 440 | end |
| 441 | if gp_buf_eq32 8 40 |
| 442 | ret gp_fail 33 |
| 443 | end |
| 444 | for i ulong 32 |
| 445 | put gp_tmp_a i get pk_buf add 8 i |
| 446 | end |
| 447 | if ne pk_store_put PK_OK |
| 448 | ret gp_fail 34 |
| 449 | end |
| 450 | if ne pk_realize_on_arrive TID_X925 x925Feat 64 7 PK_OK |
| 451 | ret gp_fail 35 |
| 452 | end |
| 453 | if or ne get pk_real_out 2 0 ne get pk_real_out 5 3 |
| 454 | ret gp_fail 36 |
| 455 | end |
| 456 | slotA = get pk_real_out 1 |
| 457 | ptrA = get pk_real_out 4 |
| 458 | keyA = get pk_real_out 3 |
| 459 | |
| 460 | note Package B: g_beta(y) = (y + 1) * 2 (different IR bytes and sem_id, identical func_id!) |
| 461 | pk_reset_builder |
| 462 | gp_load_ir 5 |
| 463 | pk_compute_poly_func_id 64 2 2 0 |
| 464 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 465 | if ne pk_pack PK_OK |
| 466 | ret gp_fail 37 |
| 467 | end |
| 468 | for i ulong 32 |
| 469 | put gp_tmp_b i get pk_buf add 8 i |
| 470 | end |
| 471 | note Confirm sem_id_a != sem_id_b (genuinely different IR payloads) |
| 472 | if gp_tmp_eq32 |
| 473 | ret gp_fail 38 |
| 474 | end |
| 475 | if ne pk_store_put PK_OK |
| 476 | ret gp_fail 39 |
| 477 | end |
| 478 | note Compile-on-Arrive MUST hit the exact same cached realization with zero redundant codegen! |
| 479 | if ne pk_realize_on_arrive TID_X925 x925Feat 64 7 PK_OK |
| 480 | ret gp_fail 40 |
| 481 | end |
| 482 | if ne get pk_real_out 2 1 |
| 483 | ret gp_fail 41 |
| 484 | end |
| 485 | if or ne get pk_real_out 1 slotA ne get pk_real_out 4 ptrA |
| 486 | ret gp_fail 42 |
| 487 | end |
| 488 | if or ne get pk_real_out 3 keyA ne get pk_real_out 5 3 |
| 489 | ret gp_fail 43 |
| 490 | end |
| 491 | |
| 492 | note Verify all-zero func_id gracefully falls back to sem_id cache key |
| 493 | pk_reset_builder |
| 494 | gp_load_ir 6 |
| 495 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 496 | if ne pk_pack PK_OK |
| 497 | ret gp_fail 44 |
| 498 | end |
| 499 | for i ulong 32 |
| 500 | put pk_buf add 40 i 0 |
| 501 | end |
| 502 | if ne pk_realize_on_arrive TID_X925 x925Feat 999 7 PK_OK |
| 503 | ret gp_fail 45 |
| 504 | end |
| 505 | if or ne get pk_real_out 7 1 ne get pk_real_out 2 0 |
| 506 | ret gp_fail 46 |
| 507 | end |
| 508 | fbSlot = get pk_real_out 1 |
| 509 | if ne pk_realize_on_arrive TID_X925 x925Feat 999 7 PK_OK |
| 510 | ret gp_fail 47 |
| 511 | end |
| 512 | if or ne get pk_real_out 2 1 ne get pk_real_out 1 fbSlot |
| 513 | ret gp_fail 48 |
| 514 | end |
| 515 | gp_add_chk 17 |
| 516 | ret 0 |
| 517 | end |
| 518 | |
| 519 | note ---- Test 4: 7 Fail-Closed Negative Controls ---- |
| 520 | |
| 521 | fn gp_neg_tamper_and_len to int |
| 522 | x925Feat = tdGet TID_X925 TDF_FEAT |
| 523 | cgBefore = get pk_real_out 5 |
| 524 | note Neg Control 1: 1-bit tamper in OIR payload -> PK_ERR_DIGEST |
| 525 | if ne pk_store_load get gp_slots 2 PK_OK |
| 526 | ret gp_fail 50 |
| 527 | end |
| 528 | irStart = add PK_HDR_LEN 32 |
| 529 | orig = get pk_buf irStart |
| 530 | put pk_buf irStart xor orig uchar 1 |
| 531 | if ne pk_verify PK_ERR_DIGEST |
| 532 | ret gp_fail 51 |
| 533 | end |
| 534 | if ne pk_store_put PK_ERR_DIGEST |
| 535 | ret gp_fail 52 |
| 536 | end |
| 537 | if ne pk_realize_on_arrive TID_X925 x925Feat 1024 7 PK_ERR_DIGEST |
| 538 | ret gp_fail 53 |
| 539 | end |
| 540 | if ne get pk_real_out 5 cgBefore |
| 541 | ret gp_fail 54 |
| 542 | end |
| 543 | gp_add_neg |
| 544 | gp_add_chk 5 |
| 545 | |
| 546 | note Neg Control 2: Trailing garbage byte -> PK_ERR_LENGTH |
| 547 | if ne pk_store_load get gp_slots 0 PK_OK |
| 548 | ret gp_fail 55 |
| 549 | end |
| 550 | lLen = get pk_len 0 |
| 551 | put pk_buf lLen uchar 90 |
| 552 | put pk_len 0 add lLen 1 |
| 553 | if ne pk_verify PK_ERR_LENGTH |
| 554 | ret gp_fail 56 |
| 555 | end |
| 556 | if ne pk_realize_on_arrive TID_X925 x925Feat 1024 7 PK_ERR_LENGTH |
| 557 | ret gp_fail 57 |
| 558 | end |
| 559 | gp_add_neg |
| 560 | gp_add_chk 3 |
| 561 | |
| 562 | note Neg Control 3: Truncated package (payload and header) -> PK_ERR_LENGTH |
| 563 | if ne pk_store_load get gp_slots 0 PK_OK |
| 564 | ret gp_fail 58 |
| 565 | end |
| 566 | put pk_len 0 sub get pk_len 0 1 |
| 567 | if ne pk_verify PK_ERR_LENGTH |
| 568 | ret gp_fail 59 |
| 569 | end |
| 570 | put pk_len 0 64 |
| 571 | if ne pk_verify PK_ERR_LENGTH |
| 572 | ret gp_fail 60 |
| 573 | end |
| 574 | gp_add_neg |
| 575 | gp_add_chk 3 |
| 576 | ret 0 |
| 577 | end |
| 578 | |
| 579 | fn gp_neg_deps_and_cycles to int |
| 580 | note Neg Control 4: Missing digest import -> PK_ERR_MISSING_IMPORT |
| 581 | pk_reset_builder |
| 582 | gp_load_ir 7 |
| 583 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 584 | for i ulong 32 |
| 585 | put pk_query_id i uchar add 160 i |
| 586 | end |
| 587 | if ne pk_add_import_from_query PK_OK |
| 588 | ret gp_fail 61 |
| 589 | end |
| 590 | if ne pk_pack PK_OK |
| 591 | ret gp_fail 62 |
| 592 | end |
| 593 | if ne pk_store_put PK_OK |
| 594 | ret gp_fail 63 |
| 595 | end |
| 596 | pk_copy_store_sem_to_query get pk_last_slot 0 |
| 597 | if ne pk_resolve_deps PK_ERR_MISSING_IMPORT |
| 598 | ret gp_fail 64 |
| 599 | end |
| 600 | gp_add_neg |
| 601 | gp_add_chk 4 |
| 602 | |
| 603 | note Neg Control 5: Cyclic imports (A -> B -> A AND direct self-cycle C -> C) -> PK_ERR_CYCLE |
| 604 | note Precompute sem_A and sem_B from their canonical IR bytes |
| 605 | gp_load_ir 8 |
| 606 | pk_sha256_ir_in get pk_ir_in_len 0 |
| 607 | for i ulong 32 |
| 608 | put gp_tmp_a i get sh_dig i |
| 609 | end |
| 610 | gp_load_ir 9 |
| 611 | pk_sha256_ir_in get pk_ir_in_len 0 |
| 612 | for i ulong 32 |
| 613 | put gp_tmp_b i get sh_dig i |
| 614 | end |
| 615 | |
| 616 | note Pack and store A importing sem_B |
| 617 | pk_reset_builder |
| 618 | gp_load_ir 8 |
| 619 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 620 | for i ulong 32 |
| 621 | put pk_query_id i get gp_tmp_b i |
| 622 | end |
| 623 | if ne pk_add_import_from_query PK_OK |
| 624 | ret gp_fail 65 |
| 625 | end |
| 626 | if ne pk_pack PK_OK |
| 627 | ret gp_fail 66 |
| 628 | end |
| 629 | if ne pk_store_put PK_OK |
| 630 | ret gp_fail 67 |
| 631 | end |
| 632 | cycASlot = get pk_last_slot 0 |
| 633 | |
| 634 | note Pack and store B importing sem_A |
| 635 | pk_reset_builder |
| 636 | gp_load_ir 9 |
| 637 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 638 | for i ulong 32 |
| 639 | put pk_query_id i get gp_tmp_a i |
| 640 | end |
| 641 | if ne pk_add_import_from_query PK_OK |
| 642 | ret gp_fail 68 |
| 643 | end |
| 644 | if ne pk_pack PK_OK |
| 645 | ret gp_fail 69 |
| 646 | end |
| 647 | if ne pk_store_put PK_OK |
| 648 | ret gp_fail 70 |
| 649 | end |
| 650 | |
| 651 | note DFS starting at A MUST refuse A -> B -> A with PK_ERR_CYCLE |
| 652 | pk_copy_store_sem_to_query cycASlot |
| 653 | if ne pk_resolve_deps PK_ERR_CYCLE |
| 654 | ret gp_fail 71 |
| 655 | end |
| 656 | |
| 657 | note Direct self-cycle C -> C MUST also refuse with PK_ERR_CYCLE |
| 658 | gp_load_ir 10 |
| 659 | pk_sha256_ir_in get pk_ir_in_len 0 |
| 660 | for i ulong 32 |
| 661 | put gp_tmp_a i get sh_dig i |
| 662 | end |
| 663 | pk_reset_builder |
| 664 | gp_load_ir 10 |
| 665 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 666 | for i ulong 32 |
| 667 | put pk_query_id i get gp_tmp_a i |
| 668 | end |
| 669 | if ne pk_add_import_from_query PK_OK |
| 670 | ret gp_fail 72 |
| 671 | end |
| 672 | if ne pk_pack PK_OK |
| 673 | ret gp_fail 73 |
| 674 | end |
| 675 | if ne pk_store_put PK_OK |
| 676 | ret gp_fail 74 |
| 677 | end |
| 678 | pk_copy_store_sem_to_query get pk_last_slot 0 |
| 679 | if ne pk_resolve_deps PK_ERR_CYCLE |
| 680 | ret gp_fail 75 |
| 681 | end |
| 682 | gp_add_neg |
| 683 | gp_add_chk 11 |
| 684 | ret 0 |
| 685 | end |
| 686 | |
| 687 | fn gp_neg_contract to int |
| 688 | cgBefore = get pk_real_out 5 |
| 689 | note Neg Control 6a: SMEM overflow (131072 > SM121 101376) -> TDK_ERR_SMEM before codegen |
| 690 | pk_reset_builder |
| 691 | gp_load_ir 11 |
| 692 | pk_set_req EXEC_SIMT_WARP 131072 64 32 0 0 |
| 693 | if ne pk_pack PK_OK |
| 694 | ret gp_fail 76 |
| 695 | end |
| 696 | smFeat = tdGet TID_SM121 TDF_FEAT |
| 697 | if ne pk_realize_on_arrive TID_SM121 smFeat 8192 7 TDK_ERR_SMEM |
| 698 | ret gp_fail 77 |
| 699 | end |
| 700 | if ne get pk_real_out 5 cgBefore |
| 701 | ret gp_fail 78 |
| 702 | end |
| 703 | |
| 704 | note Neg Control 6b: MMA_161616_AMX on TID_X64_AVX2 -> TDK_ERR_MMA before codegen |
| 705 | pk_reset_builder |
| 706 | gp_load_ir 12 |
| 707 | pk_set_req EXEC_SCALAR_SIMD 0 12 8 MMA_161616_AMX 0 |
| 708 | if ne pk_pack PK_OK |
| 709 | ret gp_fail 79 |
| 710 | end |
| 711 | avxFeat = tdGet TID_X64_AVX2 TDF_FEAT |
| 712 | if ne pk_realize_on_arrive TID_X64_AVX2 avxFeat 8192 7 TDK_ERR_MMA |
| 713 | ret gp_fail 80 |
| 714 | end |
| 715 | if ne get pk_real_out 5 cgBefore |
| 716 | ret gp_fail 81 |
| 717 | end |
| 718 | gp_add_neg |
| 719 | gp_add_chk 6 |
| 720 | ret 0 |
| 721 | end |
| 722 | |
| 723 | fn gp_neg_proof to int |
| 724 | cgBefore = get pk_real_out 5 |
| 725 | note Neg Control 7: Unproven realization (proofMask = 3) -> refused by tdCacheInstall |
| 726 | pk_reset_builder |
| 727 | gp_load_ir 13 |
| 728 | pk_set_req EXEC_SCALAR_SIMD 0 8 4 0 FEAT_FMA |
| 729 | if ne pk_pack PK_OK |
| 730 | ret gp_fail 82 |
| 731 | end |
| 732 | x925Feat = tdGet TID_X925 TDF_FEAT |
| 733 | if ne pk_realize_on_arrive TID_X925 x925Feat 7777 3 PK_ERR_PROOF |
| 734 | ret gp_fail 83 |
| 735 | end |
| 736 | if or ne get pk_real_out 1 PK_NONE ne get pk_real_out 5 cgBefore |
| 737 | ret gp_fail 84 |
| 738 | end |
| 739 | unpKey = get pk_real_out 3 |
| 740 | if ne tdCacheLookup unpKey TID_X925 x925Feat 7777 PK_NONE |
| 741 | ret gp_fail 85 |
| 742 | end |
| 743 | note Direct tdCacheInstall negative check with proofMask = 3 |
| 744 | smFeat = tdGet TID_SM121 TDF_FEAT |
| 745 | if ne tdCacheInstall 555555 TID_SM121 smFeat 7777 131072 3 PK_NONE |
| 746 | ret gp_fail 86 |
| 747 | end |
| 748 | gp_add_neg |
| 749 | gp_add_chk 5 |
| 750 | ret 0 |
| 751 | end |
| 752 | |
| 753 | fn main to int |
| 754 | tdInitAll |
| 755 | tdCacheInit |
| 756 | pk_reset_all |
| 757 | put gp_chk 0 0 |
| 758 | put gp_neg 0 0 |
| 759 | |
| 760 | r = gp_test_wire_exact |
| 761 | if ne r 0 |
| 762 | ret r |
| 763 | end |
| 764 | r = gp_test_dag |
| 765 | if ne r 0 |
| 766 | ret r |
| 767 | end |
| 768 | r = gp_test_realize_targets |
| 769 | if ne r 0 |
| 770 | ret r |
| 771 | end |
| 772 | r = gp_test_semantic_dedup |
| 773 | if ne r 0 |
| 774 | ret r |
| 775 | end |
| 776 | r = gp_neg_tamper_and_len |
| 777 | if ne r 0 |
| 778 | ret r |
| 779 | end |
| 780 | r = gp_neg_deps_and_cycles |
| 781 | if ne r 0 |
| 782 | ret r |
| 783 | end |
| 784 | r = gp_neg_contract |
| 785 | if ne r 0 |
| 786 | ret r |
| 787 | end |
| 788 | r = gp_neg_proof |
| 789 | if ne r 0 |
| 790 | ret r |
| 791 | end |
| 792 | if ne get gp_neg 0 7 |
| 793 | ret gp_fail 99 |
| 794 | end |
| 795 | |
| 796 | gp_say 0 |
| 797 | gp_num get gp_chk 0 |
| 798 | gp_say 1 |
| 799 | gp_num get pk_real_out 6 |
| 800 | gp_say 2 |
| 801 | gp_num get gp_neg 0 |
| 802 | gp_say 3 |
| 803 | ret 0 |
| 804 | end |