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 / helix / dual_checker.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/helix/dual_checker.om: Phase 2 Standalone Structural Pure-Omega Dual-Checker Trust Anchor. |
| 3 | note Compiled strictly by Strand A (seed/out/omc) with ZERO shared code with egraph_opt.om, |
| 4 | note Lean 4, or protocol/uor-gpu. |
| 5 | note |
| 6 | note Independently re-checks TWO trust surfaces from first principles: |
| 7 | note Surface A (EGP2 Structural Proof Kernel): |
| 8 | note 1. Deserializes the full e-graph node table (op, a, b, c, val) for each kernel. |
| 9 | note 2. Independently computes the pre-optimization GB10 cost of root_before. |
| 10 | note 3. Replays union-find from scratch, structurally checking EVERY rewrite step's exact |
| 11 | note operand/e-class/constant preconditions (mirroring HelixKernelIR.lean rules 1..12). |
| 12 | note 4. Verifies find(root_before) == find(root_after) in the verified equivalence closure. |
| 13 | note 5. Independently recomputes post-optimization GB10 cost and asserts cost_after < cost_before. |
| 14 | note 6. Independently evaluates BOTH root_before and extracted root_after on 4 concrete register |
| 15 | note environments (32-bit wrapping arithmetic) and asserts exact semantic equality. |
| 16 | note 7. Runs TWO negative controls against itself: |
| 17 | note - Unsound rule ID (rule 99: add -> mul) -> MUST be rejected. |
| 18 | note - Structural spoof (valid rule 3 add(x,0)->x applied to add(x,1)->x) -> MUST be rejected. |
| 19 | note Surface B (uor-gpu Launch-Gate & Authority Antibodies 8..11): |
| 20 | note Enforces and red-teams all 4 uor-gpu antibodies from HelixAntibodies.lean: |
| 21 | note Antibody 8 (p0-sig-smuggle): sigCanonical == 1 |
| 22 | note Antibody 9 (trailing-bytes): actualLen == declaredLen |
| 23 | note Antibody 10 (misaligned-qmd): qmdAddr % 256 == 0 and qmdBytes == 384 |
| 24 | note Antibody 11 (zero-gen-cap): capGeneration > 0 |
| 25 | note |
| 26 | note Usage: dual_checker EGP2_CERT |
| 27 | |
| 28 | global dcBuf uchar 16384 |
| 29 | global dcPath uchar 1024 |
| 30 | global dcPar ulong 512 |
| 31 | global dcOp ulong 512 |
| 32 | global dcA ulong 512 |
| 33 | global dcB ulong 512 |
| 34 | global dcC ulong 512 |
| 35 | global dcVal ulong 512 |
| 36 | global dcCost ulong 512 |
| 37 | global dcBest ulong 512 |
| 38 | global dcNc ulong 1 |
| 39 | global dcEnv ulong 8 |
| 40 | |
| 41 | text dcUse "usage: dual_checker EGP2_CERT\n" |
| 42 | text dcHdr "=== PHASE 2: STRAND A STRUCTURAL DUAL-CHECKER TRUST ANCHOR (EGP2 + UOR-GPU ANTIBODIES 8..11) ===\n" |
| 43 | text dcEgOk " DUAL_CHECK_EGP2: PASS kernels=" |
| 44 | text dcEgSt " verified_steps=" |
| 45 | text dcEgEv " eval_checks=12\n" |
| 46 | text dcEgBad " DUAL_CHECK_EGP2: FAIL\n" |
| 47 | text dcNeg1Ok " DUAL_CHECK_NEG_1 (unsound rule 99 add->mul): KILLED\n" |
| 48 | text dcNeg2Ok " DUAL_CHECK_NEG_2 (structural spoof rule 3 on add(x,1)->x): KILLED\n" |
| 49 | text dcNegBad " DUAL_CHECK_NEG_CONTROL: BREACH!\n" |
| 50 | text dcA8 " ANTIBODY 8 (uor-gpu p0-sig-smuggle): KILLED (code=21)\n" |
| 51 | text dcA9 " ANTIBODY 9 (uor-gpu trailing-bytes): KILLED (code=22)\n" |
| 52 | text dcA10 " ANTIBODY 10 (uor-gpu misaligned-qmd): KILLED (code=23)\n" |
| 53 | text dcA11 " ANTIBODY 11 (uor-gpu zero-gen-cap): KILLED (code=24)\n" |
| 54 | text dcPass "DUAL_CHECKER PASS egp2_structural_verified=1 neg_controls=2/2 uorgpu_antibodies=4/4 total_helix_antibodies=12/12\n" |
| 55 | text dcFail "DUAL_CHECKER FAIL\n" |
| 56 | |
| 57 | fn dcWrt s view uchar * to void |
| 58 | r = write 1 s 0 len s |
| 59 | end |
| 60 | |
| 61 | fn dcNum v ulong to void |
| 62 | b = new uchar 24 0 |
| 63 | p = ulong 24 |
| 64 | x = v |
| 65 | while true bound 24 |
| 66 | p = sub p 1 |
| 67 | put b p uchar add 48 rem x 10 |
| 68 | x = div x 10 |
| 69 | if eq x 0 |
| 70 | break |
| 71 | end |
| 72 | end |
| 73 | r = write 1 b p sub 24 p |
| 74 | end |
| 75 | |
| 76 | fn dcRdU32 o ulong to ulong |
| 77 | a = ulong get dcBuf o |
| 78 | a = or a shl ulong get dcBuf add o 1 8 |
| 79 | a = or a shl ulong get dcBuf add o 2 16 |
| 80 | ret or a shl ulong get dcBuf add o 3 24 |
| 81 | end |
| 82 | |
| 83 | fn dcFind x ulong to ulong |
| 84 | c = x |
| 85 | for i ulong 64 |
| 86 | p = get dcPar c |
| 87 | if eq p c |
| 88 | ret c |
| 89 | end |
| 90 | c = p |
| 91 | end |
| 92 | ret c |
| 93 | end |
| 94 | |
| 95 | fn dcHasConst id ulong want ulong to bool |
| 96 | c = dcFind id |
| 97 | n = get dcNc 0 |
| 98 | for i ulong n bound 512 |
| 99 | if and eq dcFind i c and eq get dcOp i 1 eq get dcVal i want |
| 100 | ret true |
| 101 | end |
| 102 | end |
| 103 | ret false |
| 104 | end |
| 105 | |
| 106 | note Structural verification of FMA fusion (Rule 1: add(mul(a,b), c) -> fma(a,b,c); Rule 2: commuted) |
| 107 | fn dcCheckFma u ulong v ulong comm bool to bool |
| 108 | if or ne get dcOp u 3 ne get dcOp v 6 |
| 109 | ret false |
| 110 | end |
| 111 | mSide = get dcA u |
| 112 | cSide = get dcB u |
| 113 | if comm |
| 114 | mSide = get dcB u |
| 115 | cSide = get dcA u |
| 116 | end |
| 117 | if ne dcFind cSide dcFind get dcC v |
| 118 | ret false |
| 119 | end |
| 120 | cm = dcFind mSide |
| 121 | va = dcFind get dcA v |
| 122 | vb = dcFind get dcB v |
| 123 | n = get dcNc 0 |
| 124 | for j ulong n bound 512 |
| 125 | if and eq dcFind j cm eq get dcOp j 5 |
| 126 | if and eq dcFind get dcA j va eq dcFind get dcB j vb |
| 127 | ret true |
| 128 | end |
| 129 | end |
| 130 | end |
| 131 | ret false |
| 132 | end |
| 133 | |
| 134 | fn dcCheckFmaZero u ulong v ulong to bool |
| 135 | if or ne get dcOp u 6 ne get dcOp v 5 |
| 136 | ret false |
| 137 | end |
| 138 | if not dcHasConst get dcC u 0 |
| 139 | ret false |
| 140 | end |
| 141 | if ne dcFind get dcA u dcFind get dcA v |
| 142 | ret false |
| 143 | end |
| 144 | ret eq dcFind get dcB u dcFind get dcB v |
| 145 | end |
| 146 | |
| 147 | note Full structural step verification against the deserialized e-graph node table |
| 148 | fn dcVerifyStep rule ulong u ulong v ulong to bool |
| 149 | n = get dcNc 0 |
| 150 | if or ge u n ge v n |
| 151 | ret false |
| 152 | end |
| 153 | opU = get dcOp u |
| 154 | if eq rule 1 |
| 155 | ret dcCheckFma u v false |
| 156 | end |
| 157 | if eq rule 2 |
| 158 | ret dcCheckFma u v true |
| 159 | end |
| 160 | if eq rule 3 |
| 161 | if ne opU 3 |
| 162 | ret false |
| 163 | end |
| 164 | ret and dcHasConst get dcB u 0 eq dcFind v dcFind get dcA u |
| 165 | end |
| 166 | if eq rule 4 |
| 167 | if ne opU 3 |
| 168 | ret false |
| 169 | end |
| 170 | ret and dcHasConst get dcA u 0 eq dcFind v dcFind get dcB u |
| 171 | end |
| 172 | if eq rule 5 |
| 173 | if ne opU 5 |
| 174 | ret false |
| 175 | end |
| 176 | ret and dcHasConst get dcB u 1 eq dcFind v dcFind get dcA u |
| 177 | end |
| 178 | if eq rule 6 |
| 179 | if ne opU 5 |
| 180 | ret false |
| 181 | end |
| 182 | ret and dcHasConst get dcB u 0 dcHasConst v 0 |
| 183 | end |
| 184 | if eq rule 11 |
| 185 | if ne opU 9 |
| 186 | ret false |
| 187 | end |
| 188 | ret and eq dcFind get dcA u dcFind get dcB u eq dcFind v dcFind get dcA u |
| 189 | end |
| 190 | if eq rule 12 |
| 191 | ret dcCheckFmaZero u v |
| 192 | end |
| 193 | ret false |
| 194 | end |
| 195 | |
| 196 | fn dcOpCost op ulong to ulong |
| 197 | if or eq op 1 eq op 2 |
| 198 | ret 0 |
| 199 | end |
| 200 | if or eq op 3 eq op 4 |
| 201 | ret 2 |
| 202 | end |
| 203 | if eq op 5 |
| 204 | ret 3 |
| 205 | end |
| 206 | if eq op 6 |
| 207 | ret 2 |
| 208 | end |
| 209 | ret 1 |
| 210 | end |
| 211 | |
| 212 | fn dcComputeCosts to void |
| 213 | n = get dcNc 0 |
| 214 | for i ulong n bound 512 |
| 215 | put dcCost i 999999 |
| 216 | put dcBest i i |
| 217 | end |
| 218 | for iter ulong 12 |
| 219 | for i ulong n bound 512 |
| 220 | op = get dcOp i |
| 221 | base = dcOpCost op |
| 222 | c = base |
| 223 | if and ne op 1 ne op 2 |
| 224 | ca = get dcCost dcFind get dcA i |
| 225 | cb = get dcCost dcFind get dcB i |
| 226 | c = add add base ca cb |
| 227 | if eq op 6 |
| 228 | c = add c get dcCost dcFind get dcC i |
| 229 | end |
| 230 | end |
| 231 | r = dcFind i |
| 232 | if lt c get dcCost r |
| 233 | put dcCost r c |
| 234 | put dcBest r i |
| 235 | end |
| 236 | end |
| 237 | end |
| 238 | end |
| 239 | |
| 240 | note Direct 32-bit wrapping denotational evaluator mirroring HelixKernelIR.lean `eval` |
| 241 | fn dcEvalNode id ulong useBest bool depth ulong to ulong |
| 242 | if gt depth 32 |
| 243 | ret 0 |
| 244 | end |
| 245 | cur = id |
| 246 | if useBest |
| 247 | cur = get dcBest dcFind id |
| 248 | end |
| 249 | op = get dcOp cur |
| 250 | if eq op 1 |
| 251 | ret and get dcVal cur 4294967295 |
| 252 | end |
| 253 | if eq op 2 |
| 254 | rv = get dcVal cur |
| 255 | if lt rv 8 |
| 256 | ret get dcEnv rv |
| 257 | end |
| 258 | ret 0 |
| 259 | end |
| 260 | va = dcEvalNode get dcA cur useBest add depth 1 |
| 261 | vb = dcEvalNode get dcB cur useBest add depth 1 |
| 262 | if eq op 3 |
| 263 | ret and add va vb 4294967295 |
| 264 | end |
| 265 | if eq op 4 |
| 266 | ret and sub va vb 4294967295 |
| 267 | end |
| 268 | if eq op 5 |
| 269 | ret and mul va vb 4294967295 |
| 270 | end |
| 271 | if eq op 6 |
| 272 | vc = dcEvalNode get dcC cur useBest add depth 1 |
| 273 | ret and add mul va vb vc 4294967295 |
| 274 | end |
| 275 | if eq op 9 |
| 276 | ret and va vb |
| 277 | end |
| 278 | if eq op 10 |
| 279 | ret and or va vb 4294967295 |
| 280 | end |
| 281 | ret 0 |
| 282 | end |
| 283 | |
| 284 | fn dcCheckSemanticEquiv rootB ulong rootA ulong to bool |
| 285 | for t ulong 4 |
| 286 | put dcEnv 0 add mul t 17 3 |
| 287 | put dcEnv 1 add mul t 31 5 |
| 288 | put dcEnv 2 add mul t 101 11 |
| 289 | v0 = dcEvalNode rootB false 0 |
| 290 | v1 = dcEvalNode rootA true 0 |
| 291 | if ne v0 v1 |
| 292 | ret false |
| 293 | end |
| 294 | end |
| 295 | ret true |
| 296 | end |
| 297 | |
| 298 | fn dcVerifyOneKernel pos ulong nbytes ulong stepsOut edit ulong 1 to long |
| 299 | if gt add pos 20 nbytes |
| 300 | ret long -1 |
| 301 | end |
| 302 | nc = dcRdU32 add pos 4 |
| 303 | rootB = dcRdU32 add pos 8 |
| 304 | rootA = dcRdU32 add pos 12 |
| 305 | nt = dcRdU32 add pos 16 |
| 306 | if or eq nc 0 or gt nc 512 or ge rootB nc or ge rootA nc eq nt 0 |
| 307 | ret long -1 |
| 308 | end |
| 309 | put dcNc 0 nc |
| 310 | p = add pos 20 |
| 311 | if gt add p mul nc 20 nbytes |
| 312 | ret long -1 |
| 313 | end |
| 314 | for i ulong nc bound 512 |
| 315 | put dcPar i i |
| 316 | put dcOp i dcRdU32 p |
| 317 | put dcA i dcRdU32 add p 4 |
| 318 | put dcB i dcRdU32 add p 8 |
| 319 | put dcC i dcRdU32 add p 12 |
| 320 | put dcVal i dcRdU32 add p 16 |
| 321 | p = add p 20 |
| 322 | end |
| 323 | note Independently compute pre-optimization GB10 cost before any unions |
| 324 | dcComputeCosts |
| 325 | cbef = get dcCost rootB |
| 326 | if gt add p mul nt 12 nbytes |
| 327 | ret long -1 |
| 328 | end |
| 329 | for s ulong nt bound 512 |
| 330 | ru = dcRdU32 p |
| 331 | lu = dcRdU32 add p 4 |
| 332 | rv = dcRdU32 add p 8 |
| 333 | if not dcVerifyStep ru lu rv |
| 334 | ret long -1 |
| 335 | end |
| 336 | r1 = dcFind lu |
| 337 | r2 = dcFind rv |
| 338 | if ne r1 r2 |
| 339 | put dcPar r2 r1 |
| 340 | end |
| 341 | p = add p 12 |
| 342 | end |
| 343 | note Verify equivalence closure connects root_before and root_after! |
| 344 | if ne dcFind rootB dcFind rootA |
| 345 | ret long -1 |
| 346 | end |
| 347 | note Independently compute post-optimization GB10 cost and assert strict reduction! |
| 348 | dcComputeCosts |
| 349 | caft = get dcCost dcFind rootA |
| 350 | if ge caft cbef |
| 351 | ret long -1 |
| 352 | end |
| 353 | note Independently evaluate both ASTs on 4 concrete environments! |
| 354 | if not dcCheckSemanticEquiv rootB rootA |
| 355 | ret long -1 |
| 356 | end |
| 357 | put stepsOut 0 add get stepsOut 0 nt |
| 358 | ret long p |
| 359 | end |
| 360 | |
| 361 | fn dcVerifyEgp2 nbytes ulong to long |
| 362 | if lt nbytes 8 |
| 363 | ret long -1 |
| 364 | end |
| 365 | if or ne get dcBuf 0 69 ne get dcBuf 1 71 |
| 366 | ret long -1 |
| 367 | end |
| 368 | if or ne get dcBuf 2 80 ne get dcBuf 3 50 |
| 369 | ret long -1 |
| 370 | end |
| 371 | nk = dcRdU32 4 |
| 372 | if or eq nk 0 gt nk 16 |
| 373 | ret long -1 |
| 374 | end |
| 375 | pos = ulong 8 |
| 376 | tot = new ulong 1 0 |
| 377 | for k ulong nk bound 16 |
| 378 | nxt = dcVerifyOneKernel pos nbytes tot |
| 379 | if lt nxt 0 |
| 380 | ret long -1 |
| 381 | end |
| 382 | pos = ulong nxt |
| 383 | end |
| 384 | if ne pos nbytes |
| 385 | ret long -1 |
| 386 | end |
| 387 | ret long get tot 0 |
| 388 | end |
| 389 | |
| 390 | note Structural negative controls against our own checker! |
| 391 | fn dcRunNegControls to bool |
| 392 | note Build tiny graph: 0=kreg(0), 1=kconst(1), 2=kadd(0, 1), 3=kmul(0, 1) |
| 393 | put dcNc 0 4 |
| 394 | for i ulong 4 |
| 395 | put dcPar i i |
| 396 | end |
| 397 | put dcOp 0 2 |
| 398 | put dcVal 0 0 |
| 399 | put dcOp 1 1 |
| 400 | put dcVal 1 1 |
| 401 | put dcOp 2 3 |
| 402 | put dcA 2 0 |
| 403 | put dcB 2 1 |
| 404 | put dcOp 3 5 |
| 405 | put dcA 3 0 |
| 406 | put dcB 3 1 |
| 407 | note Control 1: unsound rule 99 (add -> mul) MUST be rejected |
| 408 | if dcVerifyStep 99 2 3 |
| 409 | ret false |
| 410 | end |
| 411 | dcWrt dcNeg1Ok |
| 412 | note Control 2: structural spoof -- valid rule 3 (add(x,0)->x) applied to add(x,1)->x MUST be rejected! |
| 413 | if dcVerifyStep 3 2 0 |
| 414 | ret false |
| 415 | end |
| 416 | dcWrt dcNeg2Ok |
| 417 | ret true |
| 418 | end |
| 419 | |
| 420 | note Independent uor-gpu Launch-Gate checker (mirroring HelixAntibodies.lean launchGateAccept) |
| 421 | fn dcCheckLaunch sigOk ulong declLen ulong actLen ulong qmdAddr ulong qmdBytes ulong capGen ulong to ulong |
| 422 | if eq sigOk 0 |
| 423 | ret 21 |
| 424 | end |
| 425 | if ne actLen declLen |
| 426 | ret 22 |
| 427 | end |
| 428 | if or ne rem qmdAddr 256 0 ne qmdBytes 384 |
| 429 | ret 23 |
| 430 | end |
| 431 | if eq capGen 0 |
| 432 | ret 24 |
| 433 | end |
| 434 | ret 0 |
| 435 | end |
| 436 | |
| 437 | fn main to int |
| 438 | if ne argcnt 2 |
| 439 | dcWrt dcUse |
| 440 | ret 90 |
| 441 | end |
| 442 | k = argread 1 dcPath 0 1023 |
| 443 | if lt k 1 |
| 444 | ret 90 |
| 445 | end |
| 446 | fd = fopen dcPath 0 ulong k 0 0 |
| 447 | if lt fd 0 |
| 448 | ret 90 |
| 449 | end |
| 450 | nb = read int fd dcBuf 0 16384 |
| 451 | z = fclose int fd |
| 452 | if lt nb 8 |
| 453 | ret 1 |
| 454 | end |
| 455 | dcWrt dcHdr |
| 456 | st = dcVerifyEgp2 ulong nb |
| 457 | if lt st 0 |
| 458 | dcWrt dcEgBad |
| 459 | dcWrt dcFail |
| 460 | ret 1 |
| 461 | end |
| 462 | dcWrt dcEgOk |
| 463 | dcNum dcRdU32 4 |
| 464 | dcWrt dcEgSt |
| 465 | dcNum ulong st |
| 466 | dcWrt dcEgEv |
| 467 | if not dcRunNegControls |
| 468 | dcWrt dcNegBad |
| 469 | dcWrt dcFail |
| 470 | ret 1 |
| 471 | end |
| 472 | if ne dcCheckLaunch 1 512 512 4096 384 1 0 |
| 473 | dcWrt dcFail |
| 474 | ret 1 |
| 475 | end |
| 476 | if ne dcCheckLaunch 0 512 512 4096 384 1 21 |
| 477 | dcWrt dcFail |
| 478 | ret 1 |
| 479 | end |
| 480 | dcWrt dcA8 |
| 481 | if ne dcCheckLaunch 1 512 513 4096 384 1 22 |
| 482 | dcWrt dcFail |
| 483 | ret 1 |
| 484 | end |
| 485 | dcWrt dcA9 |
| 486 | if or ne dcCheckLaunch 1 512 512 4100 384 1 23 ne dcCheckLaunch 1 512 512 4096 380 1 23 |
| 487 | dcWrt dcFail |
| 488 | ret 1 |
| 489 | end |
| 490 | dcWrt dcA10 |
| 491 | if ne dcCheckLaunch 1 512 512 4096 384 0 24 |
| 492 | dcWrt dcFail |
| 493 | ret 1 |
| 494 | end |
| 495 | dcWrt dcA11 |
| 496 | dcWrt dcPass |
| 497 | ret 0 |
| 498 | end |