interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / seed / helix / dual_checker.om

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