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 / ng / verify / egp3_checker.om

773 lines · 13386 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/verify/egp3_checker.om: Standalone EGP3 Structural Proof Checker Library Unit.
3note ZERO code shared with seed/ng/egraph/ or seed/ng/src/ir.om.
4note Pure OM-1 compatible with OM-2 (no om2 line, no fn main, all symbols prefixed vc3_).
5note
6note Mirrors seed/helix/proofs/NgEGraphIR.lean (opcodes 1..11, rules 1..18, 32-bit and 64-bit).
7note
8note EGP3 binary format:
9note Header (8 bytes):
10note [0..3] magic "EGP3" (69, 71, 80, 51)
11note [4..7] u32 LE cert_count
12note Per cert (header 28 bytes):
13note [+0..+3] u32 LE fn_idx
14note [+4..+7] u32 LE blk_idx
15note [+8..+11] u32 LE width_bits (32 or 64)
16note [+12..+15] u32 LE node_count (N, 1..4096)
17note [+16..+19] u32 LE root_before (< N)
18note [+20..+23] u32 LE root_after (< N)
19note [+24..+27] u32 LE trace_count (T, 0..8192)
20note N nodes * 32 bytes each (8 * u32 LE):
21note (op, a, b, c, val_lo, val_hi, width, flags)
22note Opcodes: 1=CONST 2=SSA_VAL 3=ADD 4=SUB 5=MUL 6=FMA 7=SHL 8=SHR 9=AND 10=OR 11=XOR
23note T trace steps * 12 bytes each (3 * u32 LE):
24note (rule_id, lhs_node, rhs_node)
25note
26note Rules 1..18:
27note 1: add(mul(a,b), c) = fma(a,b,c)
28note 2: add(c, mul(a,b)) = fma(a,b,c)
29note 3: add(x, 0) = x
30note 4: add(0, x) = x
31note 5: mul(x, 1) = x
32note 6: mul(x, 0) = 0
33note 7: add(x, y) = add(y, x)
34note 8: mul(x, y) = mul(y, x)
35note 9: add(add(x,y), z) = add(x, add(y,z))
36note 10: mul(mul(x,y), z) = mul(x, mul(y,z))
37note 11: and(x, x) = x
38note 12: fma(a, b, 0) = mul(a, b)
39note 13: sub(x, 0) = x
40note 14: sub(x, x) = 0
41note 15: xor(x, x) = 0
42note 16: or(x, 0) = x
43note 17: shl(x, 0) = x
44note 18: mul(x, 1 <<< k) = shl(x, k) for k < width_bits
45note
46note Usage by caller:
47note Populate vc3_buf[0..nbytes) and call vc3_verify nbytes.
48note Returns 0 on PASS, nonzero failure code 1..11 on refusal (fail-closed).
49note Summary stats in vc3_stat[0..5]:
50note [0]=certs_ok [1]=steps_ok [2]=evals_ok [3]=fail_cert [4]=fail_detail [5]=fail_rule
51
52global vc3_buf uchar 1048576
53global vc3_par ulong 4096
54global vc3_rnk ulong 4096
55global vc3_op ulong 4096
56global vc3_a ulong 4096
57global vc3_b ulong 4096
58global vc3_c ulong 4096
59global vc3_val ulong 4096
60global vc3_vraw ulong 4096
61global vc3_wid ulong 4096
62global vc3_flg ulong 4096
63global vc3_evst ulong 4096
64global vc3_evvl ulong 4096
65global vc3_nc ulong 1
66global vc3_cw ulong 1
67global vc3_everr ulong 1
68global vc3_stat ulong 8
69
70fn vc3_rdu32 off ulong to ulong
71b0 = ulong get vc3_buf off
72b1 = shl ulong get vc3_buf add off 1 8
73b2 = shl ulong get vc3_buf add off 2 16
74b3 = shl ulong get vc3_buf add off 3 24
75ret or or b0 b1 or b2 b3
76end
77
78fn vc3_mask v ulong wid ulong to ulong
79if eq wid 32
80ret and v 4294967295
81end
82ret v
83end
84
85fn vc3_find x ulong to ulong
86cur = x
87for i ulong 64
88p = get vc3_par cur
89if eq p cur
90ret cur
91end
92gp = get vc3_par p
93put vc3_par cur gp
94cur = gp
95end
96ret cur
97end
98
99fn vc3_union u ulong v ulong to bool
100ru = vc3_find u
101rv = vc3_find v
102if eq ru rv
103ret false
104end
105ku = get vc3_rnk ru
106kv = get vc3_rnk rv
107if lt ku kv
108put vc3_par ru rv
109else
110if gt ku kv
111put vc3_par rv ru
112else
113put vc3_par rv ru
114put vc3_rnk ru add ku 1
115end
116end
117ret true
118end
119
120fn vc3_has_const id ulong want ulong to bool
121cid = vc3_find id
122n = get vc3_nc 0
123for i ulong n bound 4096
124if eq get vc3_op i 1
125if eq get vc3_val i want
126if eq vc3_find i cid
127ret true
128end
129end
130end
131end
132ret false
133end
134
135fn vc3_nodes_cong i ulong j ulong to bool
136op = get vc3_op i
137if ne op get vc3_op j
138ret false
139end
140if eq op 1
141ret eq get vc3_val i get vc3_val j
142end
143if eq op 2
144ret eq get vc3_vraw i get vc3_vraw j
145end
146if ne vc3_find get vc3_a i vc3_find get vc3_a j
147ret false
148end
149if ne vc3_find get vc3_b i vc3_find get vc3_b j
150ret false
151end
152if eq op 6
153ret eq vc3_find get vc3_c i vc3_find get vc3_c j
154end
155ret true
156end
157
158fn vc3_rebuild to void
159n = get vc3_nc 0
160chg = true
161while chg bound 16
162chg = false
163for i ulong n bound 4096
164j = add i 1
165while lt j n bound 4096
166if ne vc3_find i vc3_find j
167if vc3_nodes_cong i j
168if vc3_union i j
169chg = true
170end
171end
172end
173j = add j 1
174end
175end
176end
177end
178
179fn vc3_chk_fma u ulong v ulong comm bool to bool
180if ne get vc3_op u 3
181ret false
182end
183if ne get vc3_op v 6
184ret false
185end
186ms = get vc3_a u
187cs = get vc3_b u
188if comm
189ms = get vc3_b u
190cs = get vc3_a u
191end
192if ne vc3_find cs vc3_find get vc3_c v
193ret false
194end
195cm = vc3_find ms
196va = vc3_find get vc3_a v
197vb = vc3_find get vc3_b v
198n = get vc3_nc 0
199for j ulong n bound 4096
200if eq get vc3_op j 5
201if eq vc3_find j cm
202if eq vc3_find get vc3_a j va
203if eq vc3_find get vc3_b j vb
204ret true
205end
206end
207end
208end
209end
210ret false
211end
212
213fn vc3_chk_comm u ulong v ulong want_op ulong to bool
214if ne get vc3_op u want_op
215ret false
216end
217if ne get vc3_op v want_op
218ret false
219end
220if ne vc3_find get vc3_a u vc3_find get vc3_b v
221ret false
222end
223ret eq vc3_find get vc3_b u vc3_find get vc3_a v
224end
225
226fn vc3_chk_assoc_inner j ulong v ulong u ulong want_op ulong to bool
227cx = vc3_find get vc3_a v
228if ne vc3_find get vc3_a j cx
229ret false
230end
231cy = vc3_find get vc3_b j
232cz = vc3_find get vc3_b u
233ck = vc3_find get vc3_b v
234n = get vc3_nc 0
235for k ulong n bound 4096
236if eq get vc3_op k want_op
237if eq vc3_find k ck
238if eq vc3_find get vc3_a k cy
239if eq vc3_find get vc3_b k cz
240ret true
241end
242end
243end
244end
245end
246ret false
247end
248
249fn vc3_chk_assoc u ulong v ulong want_op ulong to bool
250if ne get vc3_op u want_op
251ret false
252end
253if ne get vc3_op v want_op
254ret false
255end
256cj = vc3_find get vc3_a u
257n = get vc3_nc 0
258for j ulong n bound 4096
259if eq get vc3_op j want_op
260if eq vc3_find j cj
261if vc3_chk_assoc_inner j v u want_op
262ret true
263end
264end
265end
266end
267ret false
268end
269
270fn vc3_chk_fma_zero u ulong v ulong to bool
271if ne get vc3_op u 6
272ret false
273end
274if ne get vc3_op v 5
275ret false
276end
277if not vc3_has_const get vc3_c u 0
278ret false
279end
280if ne vc3_find get vc3_a u vc3_find get vc3_a v
281ret false
282end
283ret eq vc3_find get vc3_b u vc3_find get vc3_b v
284end
285
286fn vc3_chk_r18 u ulong v ulong wid ulong to bool
287if ne get vc3_op u 5
288ret false
289end
290if ne get vc3_op v 7
291ret false
292end
293if ne vc3_find get vc3_a u vc3_find get vc3_a v
294ret false
295end
296ck = vc3_find get vc3_b v
297n = get vc3_nc 0
298for j ulong n bound 4096
299if eq get vc3_op j 1
300if eq vc3_find j ck
301kr = get vc3_vraw j
302if lt kr wid
303p2 = shl ulong 1 kr
304if vc3_has_const get vc3_b u p2
305ret true
306end
307end
308end
309end
310end
311ret false
312end
313
314fn vc3_chk_bin_id u ulong v ulong want_op ulong want_c ulong to bool
315if ne get vc3_op u want_op
316ret false
317end
318if not vc3_has_const get vc3_b u want_c
319ret false
320end
321ret eq vc3_find v vc3_find get vc3_a u
322end
323
324fn vc3_chk_self_zero u ulong v ulong want_op ulong to bool
325if ne get vc3_op u want_op
326ret false
327end
328if ne vc3_find get vc3_a u vc3_find get vc3_b u
329ret false
330end
331ret vc3_has_const v 0
332end
333
334fn vc3_chk_r1_10 rule ulong u ulong v ulong to bool
335if eq rule 1
336ret vc3_chk_fma u v false
337end
338if eq rule 2
339ret vc3_chk_fma u v true
340end
341if eq rule 3
342ret vc3_chk_bin_id u v 3 0
343end
344if eq rule 4
345if ne get vc3_op u 3
346ret false
347end
348if not vc3_has_const get vc3_a u 0
349ret false
350end
351ret eq vc3_find v vc3_find get vc3_b u
352end
353if eq rule 5
354ret vc3_chk_bin_id u v 5 1
355end
356if eq rule 6
357if ne get vc3_op u 5
358ret false
359end
360if not vc3_has_const get vc3_b u 0
361ret false
362end
363ret vc3_has_const v 0
364end
365if eq rule 7
366ret vc3_chk_comm u v 3
367end
368if eq rule 8
369ret vc3_chk_comm u v 5
370end
371if eq rule 9
372ret vc3_chk_assoc u v 3
373end
374if eq rule 10
375ret vc3_chk_assoc u v 5
376end
377ret false
378end
379
380fn vc3_chk_r11_18 rule ulong u ulong v ulong wid ulong to bool
381if eq rule 11
382if ne get vc3_op u 9
383ret false
384end
385if ne vc3_find get vc3_a u vc3_find get vc3_b u
386ret false
387end
388ret eq vc3_find v vc3_find get vc3_a u
389end
390if eq rule 12
391ret vc3_chk_fma_zero u v
392end
393if eq rule 13
394ret vc3_chk_bin_id u v 4 0
395end
396if eq rule 14
397ret vc3_chk_self_zero u v 4
398end
399if eq rule 15
400ret vc3_chk_self_zero u v 11
401end
402if eq rule 16
403ret vc3_chk_bin_id u v 10 0
404end
405if eq rule 17
406ret vc3_chk_bin_id u v 7 0
407end
408if eq rule 18
409ret vc3_chk_r18 u v wid
410end
411ret false
412end
413
414fn vc3_chk_dir rule ulong u ulong v ulong wid ulong to bool
415if le rule 10
416ret vc3_chk_r1_10 rule u v
417end
418ret vc3_chk_r11_18 rule u v wid
419end
420
421fn vc3_verify_step rule ulong u ulong v ulong to bool
422n = get vc3_nc 0
423if ge u n
424ret false
425end
426if ge v n
427ret false
428end
429if eq rule 0
430ret false
431end
432if gt rule 18
433ret false
434end
435wid = get vc3_cw 0
436if vc3_chk_dir rule u v wid
437ret true
438end
439if vc3_chk_dir rule v u wid
440ret true
441end
442vc3_rebuild
443if vc3_chk_dir rule u v wid
444ret true
445end
446ret vc3_chk_dir rule v u wid
447end
448
449fn vc3_env_val sid ulong env_t ulong wid ulong to ulong
450a = mul add sid 1 ulong 2654435761
451b = mul add env_t 1 ulong 6364136223846793005
452h = xor a b
453h = xor h shr h 17
454h = mul h ulong 1442695040888963407
455h = xor h add mul env_t 101 mul sid 31
456ret vc3_mask h wid
457end
458
459fn vc3_eval_bin op ulong va ulong vb ulong wid ulong to ulong
460if eq op 3
461ret vc3_mask add va vb wid
462end
463if eq op 4
464ret vc3_mask sub va vb wid
465end
466if eq op 5
467ret vc3_mask mul va vb wid
468end
469if eq op 7
470sh = and vb sub wid 1
471ret vc3_mask shl va sh wid
472end
473if eq op 8
474sh = and vb sub wid 1
475ret shr vc3_mask va wid sh
476end
477if eq op 9
478ret vc3_mask and va vb wid
479end
480if eq op 10
481ret vc3_mask or va vb wid
482end
483if eq op 11
484ret vc3_mask xor va vb wid
485end
486put vc3_everr 0 1
487ret 0
488end
489
490fn vc3_eval_node id ulong env_t ulong wid ulong depth ulong to ulong
491if ne get vc3_everr 0 0
492ret 0
493end
494if ge id get vc3_nc 0
495put vc3_everr 0 1
496ret 0
497end
498st = get vc3_evst id
499if eq st 2
500ret get vc3_evvl id
501end
502if eq st 1
503put vc3_everr 0 1
504ret 0
505end
506if gt depth 256
507put vc3_everr 0 1
508ret 0
509end
510put vc3_evst id 1
511op = get vc3_op id
512res = ulong 0
513if eq op 1
514res = vc3_mask get vc3_val id wid
515else
516if eq op 2
517res = vc3_env_val get vc3_vraw id env_t wid
518else
519nd = add depth 1
520va = vc3_eval_node get vc3_a id env_t wid nd
521vb = vc3_eval_node get vc3_b id env_t wid nd
522if eq op 6
523vc = vc3_eval_node get vc3_c id env_t wid nd
524res = vc3_mask add mul va vb vc wid
525else
526res = vc3_eval_bin op va vb wid
527end
528end
529end
530put vc3_evvl id res
531put vc3_evst id 2
532ret res
533end
534
535fn vc3_check_evals root_b ulong root_a ulong wid ulong to bool
536n = get vc3_nc 0
537for t ulong 4
538for i ulong n bound 4096
539put vc3_evst i 0
540end
541put vc3_everr 0 0
542v0 = vc3_eval_node root_b t wid 0
543v1 = vc3_eval_node root_a t wid 0
544if ne get vc3_everr 0 0
545ret false
546end
547if ne v0 v1
548ret false
549end
550end
551ret true
552end
553
554fn vc3_load_one i ulong p ulong nc ulong wid ulong to ulong
555op = vc3_rdu32 p
556if eq op 0
557ret 6
558end
559if gt op 11
560ret 6
561end
562nw = vc3_rdu32 add p 24
563if ne nw 0
564if ne nw wid
565ret 6
566end
567end
568put vc3_par i i
569put vc3_rnk i 0
570put vc3_op i op
571put vc3_wid i nw
572put vc3_flg i vc3_rdu32 add p 28
573na = vc3_rdu32 add p 4
574nb = vc3_rdu32 add p 8
575nc_op = vc3_rdu32 add p 12
576if gt op 2
577if ge na nc
578ret 6
579end
580if ge nb nc
581ret 6
582end
583if eq op 6
584if ge nc_op nc
585ret 6
586end
587end
588end
589put vc3_a i na
590put vc3_b i nb
591put vc3_c i nc_op
592vr = or vc3_rdu32 add p 16 shl vc3_rdu32 add p 20 32
593put vc3_vraw i vr
594put vc3_val i vc3_mask vr wid
595ret 0
596end
597
598fn vc3_load_nodes pos ulong nc ulong wid ulong to ulong
599p = pos
600for i ulong nc bound 4096
601ec = vc3_load_one i p nc wid
602if ne ec 0
603ret ec
604end
605p = add p 32
606end
607ret 0
608end
609
610fn vc3_replay_trace pos ulong nt ulong to ulong
611p = pos
612for s ulong nt bound 8192
613ru = vc3_rdu32 p
614lu = vc3_rdu32 add p 4
615rv = vc3_rdu32 add p 8
616if eq ru 0
617put vc3_stat 4 s
618put vc3_stat 5 ru
619ret 8
620end
621if gt ru 18
622put vc3_stat 4 s
623put vc3_stat 5 ru
624ret 8
625end
626n = get vc3_nc 0
627if ge lu n
628put vc3_stat 4 s
629put vc3_stat 5 ru
630ret 8
631end
632if ge rv n
633put vc3_stat 4 s
634put vc3_stat 5 ru
635ret 8
636end
637if not vc3_verify_step ru lu rv
638put vc3_stat 4 s
639put vc3_stat 5 ru
640ret 9
641end
642ign = vc3_union lu rv
643p = add p 12
644end
645ret 0
646end
647
648fn vc3_verify_cert pos ulong nbytes ulong ci ulong to long
649if gt 28 sub nbytes pos
650put vc3_stat 3 ci
651put vc3_stat 4 1
652ret long -4
653end
654wid = vc3_rdu32 add pos 8
655nc = vc3_rdu32 add pos 12
656if ne wid 32
657if ne wid 64
658put vc3_stat 3 ci
659put vc3_stat 4 wid
660ret long -4
661end
662end
663if eq nc 0
664put vc3_stat 3 ci
665put vc3_stat 4 nc
666ret long -5
667end
668if gt nc 4096
669put vc3_stat 3 ci
670put vc3_stat 4 nc
671ret long -5
672end
673root_b = vc3_rdu32 add pos 16
674root_a = vc3_rdu32 add pos 20
675nt = vc3_rdu32 add pos 24
676if ge root_b nc
677put vc3_stat 3 ci
678put vc3_stat 4 root_b
679ret long -5
680end
681if ge root_a nc
682put vc3_stat 3 ci
683put vc3_stat 4 root_a
684ret long -5
685end
686if gt nt 8192
687put vc3_stat 3 ci
688put vc3_stat 4 nt
689ret long -5
690end
691put vc3_nc 0 nc
692put vc3_cw 0 wid
693p = add pos 28
694if gt mul nc 32 sub nbytes p
695put vc3_stat 3 ci
696put vc3_stat 4 2
697ret long -6
698end
699ec = vc3_load_nodes p nc wid
700if ne ec 0
701put vc3_stat 3 ci
702put vc3_stat 4 ec
703ret long -6
704end
705p = add p mul nc 32
706if gt mul nt 12 sub nbytes p
707put vc3_stat 3 ci
708put vc3_stat 4 3
709ret long -7
710end
711vc3_rebuild
712ec = vc3_replay_trace p nt
713if ne ec 0
714put vc3_stat 3 ci
715ret neg long ec
716end
717p = add p mul nt 12
718vc3_rebuild
719if ne vc3_find root_b vc3_find root_a
720put vc3_stat 3 ci
721put vc3_stat 4 4
722ret long -10
723end
724if not vc3_check_evals root_b root_a wid
725put vc3_stat 3 ci
726put vc3_stat 4 5
727ret long -11
728end
729put vc3_stat 1 add get vc3_stat 1 nt
730put vc3_stat 2 add get vc3_stat 2 4
731ret long p
732end
733
734fn vc3_verify nbytes ulong to ulong
735for i ulong 8
736put vc3_stat i 0
737end
738if gt nbytes 1048576
739ret 1
740end
741if lt nbytes 8
742ret 1
743end
744if ne get vc3_buf 0 69
745ret 2
746end
747if ne get vc3_buf 1 71
748ret 2
749end
750if ne get vc3_buf 2 80
751ret 2
752end
753if ne get vc3_buf 3 51
754ret 2
755end
756cc = vc3_rdu32 4
757if gt cc 4096
758ret 3
759end
760pos = ulong 8
761for ci ulong cc bound 4096
762nxt = vc3_verify_cert pos nbytes ci
763if lt nxt 0
764ret ulong neg nxt
765end
766pos = ulong nxt
767end
768if ne pos nbytes
769ret 3
770end
771put vc3_stat 0 cc
772ret 0
773end