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 / om / meaning / meaning.om

1798 lines · 29933 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note om/meaning/meaning.om: FUNCTION_ID (omega.function.v1) and the graph of meaning, as an OM-2
3note library unit for program mode (no om2 line, no main; it keeps its state in mg* globals, so it
4note links only into a program, not into function mode). Needs om/tls/sha2.om before it.
5note Specification: spec/program-identity.md section 9 (FUNCTION_ID) and section 10 (graph).
6note
7note Programs. A program is a width w (1..64) and a postfix (RPN) token list over
8note 1 x (the parameter) 2 c (a constant below 2^w) 3 add 4 sub 5 mul 9 and 10 or
9note (the token codes are the OpCode numbers of src/omega_types.h; 1 and 2 are OP_IDENTITY and
10note OP_CONSTANT read as "parameter" and "literal"). Arithmetic wraps modulo 2^w. The V0 program
11note body of spec/program-identity.md 2.1, f(x) = op_n(...op_1(x, c_1)..., c_n), is the token list
12note x c_1 op_1 c_2 op_2 ... c_n op_n.
13note
14note API (p is a program index, n a node index, slot a 32-byte id slot in mgi)
15note mg_reset empty programs, ids and graph
16note mg_pnew w -> p; mg_ptok p op v build a program token by token
17note mg_pchk p -> 0 | code 0 = well formed (see the function for codes)
18note mg_eval p x -> f(x) the semantics: wrap arithmetic modulo 2^w
19note mg_frag p -> 1 | 0 1 = the polynomial fragment (FUNCTION_ID is canonical)
20note mg_canon p -> K canonical falling-factorial coefficients into mgc[0..K)
21note mg_fid p slot -> 1 | 2 FUNCTION_ID into slot: 1 canonical, 2 intensional fallback
22note mg_body p slot the body's SEMANTIC_ID (OMG0 apply graph, as the C builders)
23note mg_pid bs w po pl qo ql program_id v2 (omega.program.v2) into slot 102
24note mg_addprog p po pl qo ql -> n add the program, its body and its function to the graph
25note mg_exh a b, mg_wit a b prove two body nodes equal (exhaustive, w <= 32) or not
26note mg_nbr n rel, mg_class n, mg_prov n queries into mgq; return the count
27note mg_verify -> failures recheck every node id and every edge receipt
28note mg_ser -> length, mg_gdig slot, mg_load length canonical storage in mgo, its digest, reading
29note Contract texts (pre/post) are byte runs in the heap mgh, written with mg_hpush.
30
31const MGNMAX ulong 4096
32const MGRMAX ulong 8192
33const MGEMAX ulong 16384
34const MGNONE ulong 18446744073709551615
35
36global mgs ulong 64
37global mgtk ulong 2097152
38global mgpr ulong 262144
39global mgst ulong 64
40global mgcl ulong 64
41global mgcv ulong 64
42global mgc ulong 80
43global mgd ulong 80
44global mgb uchar 4096
45global mgi uchar 1048576
46global mgh uchar 4194304
47global mgnd ulong 16384
48global mgrc ulong 16384
49global mged ulong 65536
50global mgq ulong 16384
51global mgsn ulong 4096
52global mgsr ulong 8192
53global mgse ulong 16384
54global mgvp ulong 4096
55global mgo uchar 4194304
56text mgtfd "omega.function.v1"
57text mgtpd "omega.program.v2"
58text mgtrd "omega.meaning.receipt.v1"
59text mgtgd "omega.meaning.graph.v1"
60text mgtpa "omega.param"
61
62note mgs cells: 0 tokens used, 1 programs, 2 mgb cursor, 3 nodes, 4 receipts, 5 edges, 6 heap used,
63note 8 mgo cursor, 9 mgo read cursor, 10 read error flag, 11 read limit
64note id slots: 0 type, 1 param, 2 operation, 3 pre, 4 post, 5 fallback body, 16..79 body stack,
65note 100 body, 101 function, 102 program, 103 receipt, 104..109 verify and load scratch,
66note 1024 + n node n, 8192 + r receipt r, 16384.. free for callers
67
68fn mg_reset to void
69for i ulong 16
70put mgs i 0
71end
72end
73
74fn mg_mask w ulong to ulong
75if ge w 64
76ret not ulong 0
77end
78ret sub shl ulong 1 w 1
79end
80
81note ---- programs ----
82
83fn mg_pnew w ulong to ulong
84p = get mgs 1
85if ge p 65536
86trap 12
87end
88put mgpr mul p 4 get mgs 0
89put mgpr add mul p 4 1 0
90put mgpr add mul p 4 2 w
91put mgs 1 add p 1
92ret p
93end
94
95fn mg_ptok p ulong o ulong v ulong to void
96t = get mgs 0
97if ge t 1048576
98trap 12
99end
100put mgtk mul t 2 o
101put mgtk add mul t 2 1 v
102put mgs 0 add t 1
103q = add mul p 4 1
104put mgpr q add get mgpr q 1
105end
106
107fn mg_top t ulong to ulong
108ret get mgtk mul t 2
109end
110
111fn mg_tv t ulong to ulong
112ret get mgtk add mul t 2 1
113end
114
115fn mg_pw p ulong to ulong
116ret get mgpr add mul p 4 2
117end
118
119note 0 well formed; 1 width not 1..64; 2 no tokens or over 4096; 3 unknown token; 4 constant not
120note below 2^w; 5 operation without two operands; 6 stack over 64; 7 not exactly one result
121fn mg_pchk p ulong to ulong
122b = get mgpr mul p 4
123n = get mgpr add mul p 4 1
124w = mg_pw p
125if eq w 0
126ret 1
127end
128if gt w 64
129ret 1
130end
131if eq n 0
132ret 2
133end
134if gt n 4096
135ret 2
136end
137m = mg_mask w
138sp = ulong 0
139for i ulong n
140o = mg_top add b i
141v = mg_tv add b i
142if le o 2
143if eq o 0
144ret 3
145end
146if eq o 2
147if ne and v not m 0
148ret 4
149end
150end
151if ge sp 64
152ret 6
153end
154sp = add sp 1
155continue
156end
157if ne o 3
158if ne o 4
159if ne o 5
160if ne o 9
161if ne o 10
162ret 3
163end
164end
165end
166end
167end
168if lt sp 2
169ret 5
170end
171sp = sub sp 1
172end
173if ne sp 1
174ret 7
175end
176ret 0
177end
178
179fn mg_op o ulong l ulong r ulong m ulong to ulong
180if eq o 3
181ret and add l r m
182end
183if eq o 4
184ret and sub l r m
185end
186if eq o 5
187ret and mul l r m
188end
189if eq o 9
190ret and l r
191end
192if eq o 10
193ret and or l r m
194end
195trap 9
196end
197
198fn mg_eval p ulong x ulong to ulong
199b = get mgpr mul p 4
200n = get mgpr add mul p 4 1
201m = mg_mask mg_pw p
202sp = ulong 0
203for i ulong n
204o = mg_top add b i
205if eq o 1
206put mgst sp and x m
207sp = add sp 1
208continue
209end
210if eq o 2
211put mgst sp mg_tv add b i
212sp = add sp 1
213continue
214end
215sp = sub sp 1
216put mgst sub sp 1 mg_op o get mgst sub sp 1 get mgst sp m
217end
218ret get mgst 0
219end
220
221note ---- the polynomial fragment ----
222note Abstract evaluation, one class per stack entry: 0 constant (value in mgcv), 1 polynomial in x,
223note 2 outside. add sub mul of classes 0/1 stay polynomial; mul by the constant 0 is the constant 0.
224note and/or with a constant side: and all-ones = the other side, and 0 = constant 0, or 0 = the other
225note side, or all-ones = constant all-ones (exact identities modulo 2^w). Everything else is class 2.
226
227fn mg_fset j ulong c ulong v ulong to void
228put mgcl j c
229put mgcv j v
230end
231
232fn mg_fstep o ulong j ulong m ulong to void
233lc = get mgcl j
234rc = get mgcl add j 1
235lv = get mgcv j
236rv = get mgcv add j 1
237if eq lc 0
238if eq rc 0
239mg_fset j 0 mg_op o lv rv m
240ret
241end
242end
243if le o 5
244if eq o 5
245if eq lc 0
246if eq lv 0
247mg_fset j 0 0
248ret
249end
250end
251if eq rc 0
252if eq rv 0
253mg_fset j 0 0
254ret
255end
256end
257end
258if eq lc 2
259mg_fset j 2 0
260ret
261end
262if eq rc 2
263mg_fset j 2 0
264ret
265end
266mg_fset j 1 0
267ret
268end
269c = ulong 0
270oc = ulong 2
271ov = ulong 0
272if eq lc 0
273c = lv
274oc = rc
275ov = rv
276else
277if eq rc 0
278c = rv
279oc = lc
280ov = lv
281else
282mg_fset j 2 0
283ret
284end
285end
286if eq o 9
287if eq c m
288mg_fset j oc ov
289ret
290end
291if eq c 0
292mg_fset j 0 0
293ret
294end
295mg_fset j 2 0
296ret
297end
298if eq c 0
299mg_fset j oc ov
300ret
301end
302if eq c m
303mg_fset j 0 m
304ret
305end
306mg_fset j 2 0
307end
308
309fn mg_frag p ulong to ulong
310b = get mgpr mul p 4
311n = get mgpr add mul p 4 1
312m = mg_mask mg_pw p
313sp = ulong 0
314for i ulong n
315o = mg_top add b i
316if eq o 1
317mg_fset sp 1 0
318sp = add sp 1
319continue
320end
321if eq o 2
322mg_fset sp 0 mg_tv add b i
323sp = add sp 1
324continue
325end
326sp = sub sp 1
327mg_fstep o sub sp 1 m
328end
329if eq get mgcl 0 2
330ret 0
331end
332ret 1
333end
334
335note ---- canonical form (spec/program-identity.md 9.2) ----
336
337fn mg_tz k ulong to ulong
338t = k
339z = ulong 0
340while eq and t 1 0 bound 64
341t = shr t 1
342z = add z 1
343end
344ret z
345end
346
347note K(w): the least k with v2(k!) >= w (Wolfram table in the spec: K(8) = 10, K(64) = 66)
348fn mg_kof w ulong to ulong
349v = ulong 0
350k = ulong 0
351while lt v w bound 200
352k = add k 1
353v = add v mg_tz k
354end
355ret k
356end
357
358note inverse of an odd u modulo 2^64: Newton y <- y (2 - u y), 3 -> 6 -> 12 -> 24 -> 48 -> 96 bits
359fn mg_inv u ulong to ulong
360y = u
361for i ulong 5
362y = mul y sub 2 mul u y
363end
364ret y
365end
366
367note f(0..K-1), forward differences d_k = Delta^k f(0) mod 2^w = k! b_k, then with k! = 2^v u (u odd)
368note a_k = (d_k / 2^v) u^-1 mod 2^(w - v): the unique coefficients with 0 <= a_k < 2^(w - v2(k!)) and
369note f(x) = sum a_k x(x-1)...(x-k+1) mod 2^w. A d_k not divisible by 2^v means f is not a polynomial
370note function: trap 9 (the fragment check makes this unreachable).
371fn mg_canon p ulong to ulong
372w = mg_pw p
373m = mg_mask w
374kk = mg_kof w
375for t ulong kk
376put mgd t mg_eval p t
377end
378k = ulong 1
379while lt k kk bound 100
380j = sub kk 1
381while ge j k bound 100
382put mgd j and sub get mgd j get mgd sub j 1 m
383j = sub j 1
384end
385k = add k 1
386end
387u = ulong 1
388v = ulong 0
389for i ulong kk
390if gt i 0
391u = mul u shr i mg_tz i
392v = add v mg_tz i
393end
394dk = get mgd i
395if ne and dk sub shl ulong 1 v 1 0
396trap 9
397end
398a = mul shr dk v mg_inv u
399put mgc i and a mg_mask sub w v
400end
401ret kk
402end
403
404note ---- byte building and hashing ----
405
406fn mg_b0 to void
407put mgs 2 0
408end
409
410fn mg_b8 v ulong to void
411c = get mgs 2
412put mgb c uchar and v 255
413put mgs 2 add c 1
414end
415
416fn mg_b16 v ulong to void
417mg_b8 shr v 8
418mg_b8 v
419end
420
421fn mg_b32 v ulong to void
422mg_b16 shr v 16
423mg_b16 and v 65535
424end
425
426fn mg_b64 v ulong to void
427mg_b32 shr v 32
428mg_b32 and v 4294967295
429end
430
431fn mg_bz n ulong to void
432for i ulong n
433mg_b8 0
434end
435end
436
437fn mg_bid s ulong to void
438o = mul s 32
439for i ulong 32
440mg_b8 ulong get mgi add o i
441end
442end
443
444fn mg_bh o ulong n ulong to void
445for i ulong n
446mg_b8 ulong get mgh add o i
447end
448end
449
450note domain tags (always followed by 0x00): 1 function, 2 program v2, 3 receipt, 4 graph
451fn mg_bdom k ulong to void
452mg_b0
453if eq k 1
454for i ulong len mgtfd
455mg_b8 ulong get mgtfd i
456end
457end
458if eq k 2
459for i ulong len mgtpd
460mg_b8 ulong get mgtpd i
461end
462end
463if eq k 3
464for i ulong len mgtrd
465mg_b8 ulong get mgtrd i
466end
467end
468mg_b8 0
469end
470
471fn mg_hash s ulong to void
472n = get mgs 2
473a = new uchar 4096 0
474o = new uchar 32 0
475for i ulong n
476put a i get mgb i
477end
478k = sha2_digest 256 part a 0 n o
479if ne k 32
480trap 10
481end
482for j ulong 32
483put mgi add mul s 32 j get o j
484end
485end
486
487fn mg_idcp s ulong d ulong to void
488for i ulong 32
489put mgi add mul d 32 i get mgi add mul s 32 i
490end
491end
492
493note 0 equal, 1 a < b, 2 a > b (bytewise, as memcmp)
494fn mg_idcmp a ulong b ulong to ulong
495for i ulong 32
496x = get mgi add mul a 32 i
497y = get mgi add mul b 32 i
498if lt x y
499ret 1
500end
501if gt x y
502ret 2
503end
504end
505ret 0
506end
507
508fn mg_idz s ulong to void
509for i ulong 32
510put mgi add mul s 32 i 0
511end
512end
513
514note ---- OMG0 objects (spec/canonical-encoding.md; payloads are the C host-struct images of
515note src/omega_types.h on LP64 little-endian, zero padded, exactly as src/omega_core.c hashes them) ----
516
517fn mg_bhdr k ulong to void
518mg_b0
519mg_b8 79
520mg_b8 77
521mg_b8 71
522mg_b8 48
523mg_b8 1
524mg_b8 k
525end
526
527note TypePayload{tag u32 = 3 UNSIGNED_INT, width u16, pad 2, length u32 0, elem_type 32 zero}: 44 bytes
528fn mg_tyid w ulong s ulong to void
529mg_bhdr 2
530mg_b16 0
531mg_b16 0
532mg_b16 0
533mg_b32 44
534mg_b8 3
535mg_bz 3
536mg_b8 and w 255
537mg_b8 shr w 8
538mg_bz 38
539mg_hash s
540end
541
542note parameter 0: attribute "omega.param" = 00 00, ValuePayload{type_id, byte_len 0, bytes 0}: 98
543fn mg_parid t ulong s ulong to void
544mg_bhdr 1
545mg_b16 1
546mg_b8 len mgtpa
547for i ulong len mgtpa
548mg_b8 ulong get mgtpa i
549end
550mg_b16 2
551mg_b16 0
552mg_b16 0
553mg_b16 0
554mg_b32 98
555mg_bid t
556mg_bz 66
557end
558
559fn mg_parfin s ulong to void
560mg_hash s
561end
562
563note literal: ValuePayload{type_id, byte_len LE = (w+7)/8, value big-endian in byte_len bytes}: 98
564fn mg_valid t ulong w ulong v ulong s ulong to void
565mg_bhdr 1
566mg_b16 0
567mg_b16 0
568mg_b16 0
569mg_b32 98
570mg_bid t
571nb = shr add w 7 3
572mg_b8 nb
573mg_b8 0
574for i ulong nb
575mg_b8 shr v mul sub sub nb 1 i 8
576end
577mg_bz sub 64 nb
578mg_hash s
579end
580
581note OperationPayload{opcode u32, overflow u32 = 1 WRAP, type_id, arity 2, input_types T T 0 0,
582note output_type T, pad 3}: 204 bytes
583fn mg_opid t ulong o ulong s ulong to void
584mg_bhdr 3
585mg_b16 0
586mg_b16 0
587mg_b16 0
588mg_b32 204
589mg_b8 o
590mg_bz 3
591mg_b8 1
592mg_bz 3
593mg_bid t
594mg_b8 2
595mg_bid t
596mg_bid t
597mg_bz 64
598mg_bid t
599mg_bz 3
600mg_hash s
601end
602
603note ApplyPayload{op_id, operand_count 2, operands a b 0 0}: 161 bytes
604fn mg_apid o ulong a ulong b ulong s ulong to void
605mg_bhdr 3
606mg_b16 0
607mg_b16 0
608mg_b16 0
609mg_b32 161
610mg_bid o
611mg_b8 2
612mg_bid a
613mg_bid b
614mg_bz 64
615mg_hash s
616end
617
618note the body's SEMANTIC_ID: the root apply object built bottom-up; for a V0 chain this is exactly
619note omega_program_body_root_id (src/omega_program.c), checked against the program_id goldens
620fn mg_body p ulong s ulong to void
621b = get mgpr mul p 4
622n = get mgpr add mul p 4 1
623w = mg_pw p
624mg_tyid w 0
625mg_parid 0 1
626mg_parfin 1
627sp = ulong 16
628for i ulong n
629o = mg_top add b i
630if eq o 1
631mg_idcp 1 sp
632sp = add sp 1
633continue
634end
635if eq o 2
636mg_valid 0 w mg_tv add b i sp
637sp = add sp 1
638continue
639end
640mg_opid 0 o 2
641mg_apid 2 sub sp 2 sub sp 1 sub sp 2
642sp = sub sp 1
643end
644mg_idcp 16 s
645end
646
647note constraint object: no attributes or relations, one constraint {kind, text}, empty payload
648fn mg_cid k ulong o ulong n ulong s ulong to void
649if gt n 128
650trap 10
651end
652mg_bhdr 5
653mg_b16 0
654mg_b16 0
655mg_b16 1
656mg_b16 k
657mg_b16 n
658mg_bh o n
659mg_b32 0
660mg_hash s
661end
662
663note program_id v2 = SHA256("omega.program.v2" 0x00 || body || T(w) || T(w) || pre || post) -> slot 102
664fn mg_pid bs ulong w ulong po ulong pl ulong qo ulong ql ulong to void
665mg_tyid w 0
666mg_cid 7 po pl 3
667mg_cid 8 qo ql 4
668mg_bdom 2
669mg_bid bs
670mg_bid 0
671mg_bid 0
672mg_bid 3
673mg_bid 4
674mg_hash 102
675end
676
677note ---- FUNCTION_ID (spec/program-identity.md 9) ----
678note class 1: SHA256("omega.function.v1" 0x00 || 01 || w u16 || K u16 || a_0..a_{K-1} u64 BE)
679note class 2: SHA256("omega.function.v1" 0x00 || 02 || w u16 || body SEMANTIC_ID)
680note The preimage stays in mgb after the call.
681fn mg_fid p ulong s ulong to ulong
682w = mg_pw p
683if eq mg_frag p 1
684kk = mg_canon p
685mg_bdom 1
686mg_b8 1
687mg_b16 w
688mg_b16 kk
689for i ulong kk
690mg_b64 get mgc i
691end
692mg_hash s
693ret 1
694end
695mg_body p 5
696mg_bdom 1
697mg_b8 2
698mg_b16 w
699mg_bid 5
700mg_hash s
701ret 2
702end
703
704note ---- heap ----
705
706fn mg_hpush v ulong to void
707h = get mgs 6
708if ge h len mgh
709trap 12
710end
711put mgh h uchar and v 255
712put mgs 6 add h 1
713end
714
715fn mg_hpos to ulong
716ret get mgs 6
717end
718
719note copy mgb[from..cursor) to the heap; returns its offset
720fn mg_hput f ulong to ulong
721h = get mgs 6
722c = get mgs 2
723for i ulong sub c f
724mg_hpush ulong get mgb add f i
725end
726ret h
727end
728
729fn mg_h8 o ulong to ulong
730ret ulong get mgh o
731end
732
733fn mg_h16 o ulong to ulong
734ret or shl mg_h8 o 8 mg_h8 add o 1
735end
736
737fn mg_h64 o ulong to ulong
738v = ulong 0
739for i ulong 8
740v = or shl v 8 mg_h8 add o i
741end
742ret v
743end
744
745note ---- graph store (spec/program-identity.md 10) ----
746note node kinds: 1 PROGRAM (program_id v2), 2 BODY (body SEMANTIC_ID), 3 FUNCTION (FUNCTION_ID)
747note node cells: kind, heap offset, content length, aux (BODY: program index; FUNCTION: class;
748note PROGRAM: body node). Edge cells: src node, rel, dst node, receipt index + 1 (0 = none).
749note rel codes are RelationKind (src/omega_types.h): 2 NOT_EQUAL, 4 CONTAINS, 7 DERIVED_FROM,
750note 9 EQUIVALENT_TO.
751
752fn mg_ns n ulong to ulong
753ret add 1024 n
754end
755
756fn mg_nk n ulong to ulong
757ret get mgnd mul n 4
758end
759
760fn mg_no n ulong to ulong
761ret get mgnd add mul n 4 1
762end
763
764fn mg_nl n ulong to ulong
765ret get mgnd add mul n 4 2
766end
767
768fn mg_na n ulong to ulong
769ret get mgnd add mul n 4 3
770end
771
772fn mg_nfind s ulong to ulong
773for i ulong get mgs 3
774if eq mg_idcmp s mg_ns i 0
775ret i
776end
777end
778ret MGNONE
779end
780
781fn mg_nadd k ulong s ulong o ulong l ulong a ulong to ulong
782f = mg_nfind s
783if ne f MGNONE
784if ne mg_nk f k
785trap 11
786end
787ret f
788end
789n = get mgs 3
790if ge n MGNMAX
791trap 12
792end
793mg_idcp s mg_ns n
794put mgnd mul n 4 k
795put mgnd add mul n 4 1 o
796put mgnd add mul n 4 2 l
797put mgnd add mul n 4 3 a
798put mgs 3 add n 1
799ret n
800end
801
802fn mg_rfind s ulong to ulong
803for i ulong get mgs 4
804if eq mg_idcmp s add 8192 i 0
805ret i
806end
807end
808ret MGNONE
809end
810
811note receipt content is mgb[25..cursor) after mg_bdom 3; id = SHA256(whole mgb) -> slot 103
812fn mg_radd to ulong
813mg_hash 103
814f = mg_rfind 103
815if ne f MGNONE
816ret f
817end
818r = get mgs 4
819if ge r MGRMAX
820trap 12
821end
822h = mg_hput 25
823put mgrc mul r 2 h
824put mgrc add mul r 2 1 sub get mgs 2 25
825mg_idcp 103 add 8192 r
826put mgs 4 add r 1
827ret r
828end
829
830fn mg_eadd s ulong rl ulong d ulong r ulong to void
831ne0 = get mgs 5
832for e ulong ne0
833if eq get mged mul e 4 s
834if eq get mged add mul e 4 1 rl
835if eq get mged add mul e 4 2 d
836if eq get mged add mul e 4 3 r
837ret
838end
839end
840end
841end
842end
843if ge ne0 MGEMAX
844trap 12
845end
846put mged mul ne0 4 s
847put mged add mul ne0 4 1 rl
848put mged add mul ne0 4 2 d
849put mged add mul ne0 4 3 r
850put mgs 5 add ne0 1
851end
852
853fn mg_lo a ulong b ulong to ulong
854if eq mg_idcmp mg_ns a mg_ns b 2
855ret b
856end
857ret a
858end
859
860note receipt kind 1 CANON: 01 || lo id || hi id || w u16 || FUNCTION_ID
861fn mg_rcanon a ulong b ulong f ulong to void
862lo = mg_lo a b
863hi = sub add a b lo
864mg_bdom 3
865mg_b8 1
866mg_bid mg_ns lo
867mg_bid mg_ns hi
868mg_b16 mg_pw mg_na lo
869mg_bid mg_ns f
870r = mg_radd
871mg_eadd lo 9 hi add r 1
872end
873
874fn mg_fnode bn ulong to void
875cl = mg_fid mg_na bn 101
876h = mg_hput 0
877fnn = mg_nadd 3 101 h get mgs 2 cl
878mg_eadd fnn 7 bn 0
879if ne cl 1
880ret
881end
882for e ulong get mgs 5
883if eq get mged mul e 4 fnn
884if eq get mged add mul e 4 1 7
885o = get mged add mul e 4 2
886if ne o bn
887mg_rcanon o bn fnn
888end
889end
890end
891end
892end
893
894note body content: w u16 || token count u16 || tokens (op u8, and for op 2 the constant u64 BE)
895fn mg_bcont p ulong to void
896b = get mgpr mul p 4
897n = get mgpr add mul p 4 1
898mg_b0
899mg_b16 mg_pw p
900mg_b16 n
901for i ulong n
902o = mg_top add b i
903mg_b8 o
904if eq o 2
905mg_b64 mg_tv add b i
906end
907end
908end
909
910note program content: body id || w u16 || pre length u16 || pre || post length u16 || post
911fn mg_addprog p ulong po ulong pl ulong qo ulong ql ulong to ulong
912w = mg_pw p
913mg_body p 100
914mg_bcont p
915h = mg_hput 0
916n0 = get mgs 3
917bn = mg_nadd 2 100 h get mgs 2 p
918if gt get mgs 3 n0
919mg_fnode bn
920end
921mg_pid 100 w po pl qo ql
922mg_b0
923mg_bid 100
924mg_b16 w
925mg_b16 pl
926mg_bh po pl
927mg_b16 ql
928mg_bh qo ql
929h = mg_hput 0
930pn = mg_nadd 1 102 h get mgs 2 bn
931mg_eadd pn 4 bn 0
932ret pn
933end
934
935note 0 when the programs agree on all 2^w inputs (w <= 32), else first differing x + 1
936fn mg_exeq pa ulong pb ulong w ulong to ulong
937inner = ulong 65536
938outer = ulong 1
939if lt w 16
940inner = shl ulong 1 w
941else
942outer = shl ulong 1 sub w 16
943end
944for oi ulong outer
945for ii ulong inner
946x = add mul oi inner ii
947if ne mg_eval pa x mg_eval pb x
948ret add x 1
949end
950end
951end
952ret 0
953end
954
955note receipt kind 3 WITNESS: 03 || lo || hi || w u16 || x u64 || f_lo(x) u64 || f_hi(x) u64
956fn mg_rwit a ulong b ulong x ulong to void
957lo = mg_lo a b
958hi = sub add a b lo
959mg_bdom 3
960mg_b8 3
961mg_bid mg_ns lo
962mg_bid mg_ns hi
963mg_b16 mg_pw mg_na lo
964mg_b64 x
965mg_b64 mg_eval mg_na lo x
966mg_b64 mg_eval mg_na hi x
967r = mg_radd
968mg_eadd lo 2 hi add r 1
969end
970
971note 0 equal (EQUIVALENT_TO edge with an EXHAUSTIVE receipt), 1 differ (NOT_EQUAL edge with a
972note WITNESS receipt), 3 widths differ, 4 w over 32. a and b are BODY nodes.
973fn mg_exh a ulong b ulong to ulong
974pa = mg_na a
975pb = mg_na b
976w = mg_pw pa
977if ne w mg_pw pb
978ret 3
979end
980if gt w 32
981ret 4
982end
983x = mg_exeq pa pb w
984if ne x 0
985mg_rwit a b sub x 1
986ret 1
987end
988lo = mg_lo a b
989hi = sub add a b lo
990mg_bdom 3
991mg_b8 2
992mg_bid mg_ns lo
993mg_bid mg_ns hi
994mg_b16 w
995mg_b64 shl ulong 1 w
996r = mg_radd
997mg_eadd lo 9 hi add r 1
998ret 0
999end
1000
1001note candidate inputs: 0..127, the edges (all-ones, all-ones - 1, 2^(w-1), 2^(w-1) - 1), then splitmix64
1002fn mg_cand i ulong w ulong to ulong
1003m = mg_mask w
1004if lt i 128
1005ret and i m
1006end
1007if eq i 128
1008ret m
1009end
1010if eq i 129
1011ret and sub m 1 m
1012end
1013if eq i 130
1014ret shl ulong 1 sub w 1
1015end
1016if eq i 131
1017ret and sub shl ulong 1 sub w 1 1 m
1018end
1019z = mul i 0x9e3779b97f4a7c15
1020z = mul xor z shr z 30 0xbf58476d1ce4e5b9
1021z = mul xor z shr z 27 0x94d049bb133111eb
1022ret and xor z shr z 31 m
1023end
1024
1025note search 4096 candidates for a witness; 1 found (NOT_EQUAL edge added), 0 none (nothing added:
1026note no witness is not a proof of equality)
1027fn mg_wit a ulong b ulong to ulong
1028pa = mg_na a
1029pb = mg_na b
1030w = mg_pw pa
1031if ne w mg_pw pb
1032ret 0
1033end
1034for i ulong 4096
1035x = mg_cand i w
1036if ne mg_eval pa x mg_eval pb x
1037mg_rwit a b x
1038ret 1
1039end
1040end
1041ret 0
1042end
1043
1044note ---- queries (results in mgq) ----
1045
1046fn mg_qhas v ulong n ulong to ulong
1047for i ulong n
1048if eq get mgq i v
1049ret 1
1050end
1051end
1052ret 0
1053end
1054
1055note every node joined to n by an edge (either direction); rel 0 = any relation
1056fn mg_nbr n ulong rl ulong to ulong
1057c = ulong 0
1058for e ulong get mgs 5
1059r = get mged add mul e 4 1
1060if eq rl 0
1061r = rl
1062end
1063if eq r rl
1064s = get mged mul e 4
1065d = get mged add mul e 4 2
1066if eq s n
1067put mgq c d
1068c = add c 1
1069else
1070if eq d n
1071put mgq c s
1072c = add c 1
1073end
1074end
1075end
1076end
1077ret c
1078end
1079
1080note the equivalence class of n: closure over EQUIVALENT_TO edges, n first
1081fn mg_class n ulong to ulong
1082put mgq 0 n
1083c = ulong 1
1084h = ulong 0
1085while lt h c bound 16384
1086v = get mgq h
1087for e ulong get mgs 5
1088if eq get mged add mul e 4 1 9
1089s = get mged mul e 4
1090d = get mged add mul e 4 2
1091o = MGNONE
1092if eq s v
1093o = d
1094end
1095if eq d v
1096o = s
1097end
1098if ne o MGNONE
1099if eq mg_qhas o c 0
1100put mgq c o
1101c = add c 1
1102end
1103end
1104end
1105end
1106h = add h 1
1107end
1108ret c
1109end
1110
1111note provenance of n: every edge index touching n (its receipts explain each relation)
1112fn mg_prov n ulong to ulong
1113c = ulong 0
1114for e ulong get mgs 5
1115if eq get mged mul e 4 n
1116put mgq c e
1117c = add c 1
1118else
1119if eq get mged add mul e 4 2 n
1120put mgq c e
1121c = add c 1
1122end
1123end
1124end
1125ret c
1126end
1127
1128note ---- verification ----
1129
1130note rebuild a program from body content at heap o, length l; MGNONE if malformed
1131fn mg_bparse o ulong l ulong to ulong
1132if lt l 4
1133ret MGNONE
1134end
1135w = mg_h16 o
1136n = mg_h16 add o 2
1137if eq n 0
1138ret MGNONE
1139end
1140if gt n 4096
1141ret MGNONE
1142end
1143p = mg_pnew w
1144q = add o 4
1145e = add o l
1146for i ulong n
1147if ge q e
1148ret MGNONE
1149end
1150t = mg_h8 q
1151q = add q 1
1152v = ulong 0
1153if eq t 2
1154if gt add q 8 e
1155ret MGNONE
1156end
1157v = mg_h64 q
1158q = add q 8
1159end
1160mg_ptok p t v
1161end
1162if ne q e
1163ret MGNONE
1164end
1165if ne mg_pchk p 0
1166ret MGNONE
1167end
1168ret p
1169end
1170
1171fn mg_hideq o ulong s ulong to ulong
1172for i ulong 32
1173if ne get mgh add o i get mgi add mul s 32 i
1174ret 0
1175end
1176end
1177ret 1
1178end
1179
1180fn mg_hid o ulong s ulong to void
1181for i ulong 32
1182put mgi add mul s 32 i get mgh add o i
1183end
1184end
1185
1186note 1 when node n (kind 1) has well-formed content whose program_id v2 recomputes to its id
1187fn mg_vprog n ulong to ulong
1188o = mg_no n
1189l = mg_nl n
1190if lt l 38
1191ret 0
1192end
1193w = mg_h16 add o 32
1194pl = mg_h16 add o 34
1195if gt add 38 pl l
1196ret 0
1197end
1198ql = mg_h16 add add o 36 pl
1199if ne l add add 38 pl ql
1200ret 0
1201end
1202mg_hid o 106
1203mg_pid 106 w add o 36 pl add add o 38 pl ql
1204if eq mg_idcmp 102 mg_ns n 0
1205ret 1
1206end
1207ret 0
1208end
1209
1210fn mg_vnodes to ulong
1211bad = ulong 0
1212for n ulong get mgs 3
1213k = mg_nk n
1214put mgvp n MGNONE
1215if eq k 2
1216p = mg_bparse mg_no n mg_nl n
1217if eq p MGNONE
1218bad = add bad 1
1219continue
1220end
1221put mgvp n p
1222mg_body p 104
1223if ne mg_idcmp 104 mg_ns n 0
1224bad = add bad 1
1225end
1226continue
1227end
1228if eq k 3
1229mg_b0
1230mg_bh mg_no n mg_nl n
1231mg_hash 105
1232if ne mg_idcmp 105 mg_ns n 0
1233bad = add bad 1
1234end
1235continue
1236end
1237if eq k 1
1238if eq mg_vprog n 0
1239bad = add bad 1
1240end
1241continue
1242end
1243bad = add bad 1
1244end
1245ret bad
1246end
1247
1248fn mg_vrecs to ulong
1249bad = ulong 0
1250for r ulong get mgs 4
1251mg_bdom 3
1252mg_bh get mgrc mul r 2 get mgrc add mul r 2 1
1253mg_hash 105
1254if ne mg_idcmp 105 add 8192 r 0
1255bad = add bad 1
1256end
1257end
1258ret bad
1259end
1260
1261note 1 when the receipt of edge e (src s, rel rl, dst d) proves the relation
1262fn mg_vrec s ulong rl ulong d ulong ri ulong to ulong
1263if eq ri 0
1264ret 0
1265end
1266o = get mgrc mul sub ri 1 2
1267l = get mgrc add mul sub ri 1 2 1
1268if lt l 67
1269ret 0
1270end
1271k = mg_h8 o
1272if eq mg_hideq add o 1 mg_ns s 0
1273ret 0
1274end
1275if eq mg_hideq add o 33 mg_ns d 0
1276ret 0
1277end
1278if ne mg_idcmp mg_ns s mg_ns d 1
1279ret 0
1280end
1281pa = get mgvp s
1282pb = get mgvp d
1283if eq pa MGNONE
1284ret 0
1285end
1286if eq pb MGNONE
1287ret 0
1288end
1289w = mg_h16 add o 65
1290if ne w mg_pw pa
1291ret 0
1292end
1293if ne w mg_pw pb
1294ret 0
1295end
1296if eq k 1
1297if ne rl 9
1298ret 0
1299end
1300if ne l 99
1301ret 0
1302end
1303if ne mg_fid pa 107 1
1304ret 0
1305end
1306if ne mg_fid pb 108 1
1307ret 0
1308end
1309if eq mg_hideq add o 67 107 0
1310ret 0
1311end
1312if eq mg_hideq add o 67 108 0
1313ret 0
1314end
1315ret 1
1316end
1317if eq k 2
1318if ne rl 9
1319ret 0
1320end
1321if ne l 75
1322ret 0
1323end
1324if gt w 32
1325ret 0
1326end
1327if ne mg_h64 add o 67 shl ulong 1 w
1328ret 0
1329end
1330if ne mg_exeq pa pb w 0
1331ret 0
1332end
1333ret 1
1334end
1335if eq k 3
1336if ne rl 2
1337ret 0
1338end
1339if ne l 91
1340ret 0
1341end
1342x = mg_h64 add o 67
1343ya = mg_h64 add o 75
1344if ne mg_eval pa x ya
1345ret 0
1346end
1347if ne mg_eval pb x mg_h64 add o 83
1348ret 0
1349end
1350if eq ya mg_h64 add o 83
1351ret 0
1352end
1353ret 1
1354end
1355ret 0
1356end
1357
1358fn mg_vedges to ulong
1359bad = ulong 0
1360for e ulong get mgs 5
1361s = get mged mul e 4
1362rl = get mged add mul e 4 1
1363d = get mged add mul e 4 2
1364ri = get mged add mul e 4 3
1365if eq rl 4
1366if ne mg_nk s 1
1367bad = add bad 1
1368continue
1369end
1370if ne mg_nk d 2
1371bad = add bad 1
1372continue
1373end
1374if eq mg_hideq mg_no s mg_ns d 0
1375bad = add bad 1
1376end
1377continue
1378end
1379if eq rl 7
1380if ne mg_nk s 3
1381bad = add bad 1
1382continue
1383end
1384if ne mg_nk d 2
1385bad = add bad 1
1386continue
1387end
1388p = get mgvp d
1389if eq p MGNONE
1390bad = add bad 1
1391continue
1392end
1393c = mg_fid p 107
1394if ne mg_idcmp 107 mg_ns s 0
1395bad = add bad 1
1396end
1397continue
1398end
1399if ne mg_nk s 2
1400bad = add bad 1
1401continue
1402end
1403if ne mg_nk d 2
1404bad = add bad 1
1405continue
1406end
1407if eq mg_vrec s rl d ri 0
1408bad = add bad 1
1409end
1410end
1411ret bad
1412end
1413
1414note number of failures over every node id, receipt id and edge receipt (0 = the graph checks)
1415fn mg_verify to ulong
1416a = mg_vnodes
1417b = mg_vrecs
1418c = mg_vedges
1419ret add add a b c
1420end
1421
1422note ---- canonical storage (spec/program-identity.md 10.3) ----
1423
1424fn mg_o8 v ulong to void
1425c = get mgs 8
1426put mgo c uchar and v 255
1427put mgs 8 add c 1
1428end
1429
1430fn mg_o32 v ulong to void
1431mg_o8 shr v 24
1432mg_o8 shr v 16
1433mg_o8 shr v 8
1434mg_o8 v
1435end
1436
1437fn mg_oid s ulong to void
1438for i ulong 32
1439mg_o8 ulong get mgi add mul s 32 i
1440end
1441end
1442
1443fn mg_oh o ulong n ulong to void
1444for i ulong n
1445mg_o8 ulong get mgh add o i
1446end
1447end
1448
1449note edge order: src id, rel, dst id, receipt id (none = 32 zero bytes, slot 109)
1450fn mg_ers e ulong to ulong
1451r = get mged add mul e 4 3
1452if eq r 0
1453mg_idz 109
1454ret 109
1455end
1456ret add 8192 sub r 1
1457end
1458
1459fn mg_ecmp a ulong b ulong to ulong
1460c = mg_idcmp mg_ns get mged mul a 4 mg_ns get mged mul b 4
1461if ne c 0
1462ret c
1463end
1464ra = get mged add mul a 4 1
1465rb = get mged add mul b 4 1
1466if lt ra rb
1467ret 1
1468end
1469if gt ra rb
1470ret 2
1471end
1472c = mg_idcmp mg_ns get mged add mul a 4 2 mg_ns get mged add mul b 4 2
1473if ne c 0
1474ret c
1475end
1476sa = mg_ers a
1477mg_idcp sa 108
1478ret mg_idcmp 108 mg_ers b
1479end
1480
1481note insertion sorts of index lists: 1 nodes (mgsn), 2 receipts (mgsr), 3 edges (mgse)
1482fn mg_gt k ulong a ulong b ulong to ulong
1483c = ulong 0
1484if eq k 1
1485c = mg_idcmp mg_ns a mg_ns b
1486else
1487if eq k 2
1488c = mg_idcmp add 8192 a add 8192 b
1489else
1490c = mg_ecmp a b
1491end
1492end
1493if eq c 2
1494ret 1
1495end
1496ret 0
1497end
1498
1499fn mg_sget k ulong i ulong to ulong
1500if eq k 1
1501ret get mgsn i
1502end
1503if eq k 2
1504ret get mgsr i
1505end
1506ret get mgse i
1507end
1508
1509fn mg_sput k ulong i ulong v ulong to void
1510if eq k 1
1511put mgsn i v
1512ret
1513end
1514if eq k 2
1515put mgsr i v
1516ret
1517end
1518put mgse i v
1519end
1520
1521fn mg_sort k ulong n ulong to void
1522for i ulong n
1523mg_sput k i i
1524end
1525i = ulong 1
1526while lt i n bound 999999
1527cur = mg_sget k i
1528j = i
1529while gt j 0 bound 999999
1530pv = mg_sget k sub j 1
1531if eq mg_gt k pv cur 0
1532break
1533end
1534mg_sput k j pv
1535j = sub j 1
1536end
1537mg_sput k j cur
1538i = add i 1
1539end
1540end
1541
1542note "omega.meaning.graph.v1" 0x00 || u32 N || N x (id, kind u8, u32 len, content)
1543note || u32 R || R x (id, u32 len, content) || u32 E || E x (src id, rel u16, dst id, receipt id)
1544fn mg_sern nn ulong to void
1545mg_o32 nn
1546for i ulong nn
1547n = get mgsn i
1548mg_oid mg_ns n
1549mg_o8 mg_nk n
1550mg_o32 mg_nl n
1551mg_oh mg_no n mg_nl n
1552end
1553end
1554
1555fn mg_serr nr ulong to void
1556mg_o32 nr
1557for i ulong nr
1558r = get mgsr i
1559l = get mgrc add mul r 2 1
1560mg_oid add 8192 r
1561mg_o32 l
1562mg_oh get mgrc mul r 2 l
1563end
1564end
1565
1566fn mg_sere ne0 ulong to void
1567mg_o32 ne0
1568for i ulong ne0
1569e = get mgse i
1570q = mul e 4
1571rl = get mged add q 1
1572mg_oid mg_ns get mged q
1573mg_o8 shr rl 8
1574mg_o8 rl
1575mg_oid mg_ns get mged add q 2
1576mg_oid mg_ers e
1577end
1578end
1579
1580fn mg_ser to ulong
1581put mgs 8 0
1582for i ulong len mgtgd
1583mg_o8 ulong get mgtgd i
1584end
1585mg_o8 0
1586mg_sort 1 get mgs 3
1587mg_sort 2 get mgs 4
1588mg_sort 3 get mgs 5
1589mg_sern get mgs 3
1590mg_serr get mgs 4
1591mg_sere get mgs 5
1592ret get mgs 8
1593end
1594
1595note SHA-256 of mgo[0..n) -> slot s, streamed in 4096-byte runs
1596fn mg_odig n ulong s ulong to void
1597c = new ulong 40 0
1598a = new uchar 4096 0
1599o = new uchar 32 0
1600sha256_init c
1601q = ulong 0
1602while lt q n bound 999999
1603k = sub n q
1604if gt k 4096
1605k = 4096
1606end
1607for i ulong k
1608put a i get mgo add q i
1609end
1610sha256_update c part a 0 k
1611q = add q k
1612end
1613sha256_final c o
1614for j ulong 32
1615put mgi add mul s 32 j get o j
1616end
1617end
1618
1619fn mg_gdig s ulong to ulong
1620n = mg_ser
1621mg_odig n s
1622ret n
1623end
1624
1625note ---- reading the storage back ----
1626
1627fn mg_i8 to ulong
1628c = get mgs 9
1629if ge c get mgs 11
1630put mgs 10 1
1631ret 0
1632end
1633put mgs 9 add c 1
1634ret ulong get mgo c
1635end
1636
1637fn mg_i32 to ulong
1638v = ulong 0
1639for i ulong 4
1640v = or shl v 8 mg_i8
1641end
1642ret v
1643end
1644
1645fn mg_iid s ulong to void
1646for i ulong 32
1647put mgi add mul s 32 i uchar mg_i8
1648end
1649end
1650
1651fn mg_ih n ulong to ulong
1652h = get mgs 6
1653for i ulong n
1654mg_hpush mg_i8
1655end
1656ret h
1657end
1658
1659note replace the whole state by the graph in mgo[0..n). 0 ok; 1 truncated or trailing bytes; 2 bad
1660note domain tag; 3 order not strictly increasing (non-canonical or duplicate); 4 malformed body;
1661note 5 edge names an unknown node or receipt; 6 bad node kind. Run mg_verify afterwards.
1662fn mg_load n ulong to ulong
1663mg_reset
1664put mgs 9 0
1665put mgs 10 0
1666put mgs 11 n
1667for i ulong len mgtgd
1668if ne mg_i8 ulong get mgtgd i
1669ret 2
1670end
1671end
1672if ne mg_i8 0
1673ret 2
1674end
1675nn = mg_i32
1676if gt nn MGNMAX
1677ret 1
1678end
1679for i ulong nn
1680mg_iid 104
1681if gt i 0
1682if ne mg_idcmp mg_ns sub i 1 104 1
1683ret 3
1684end
1685end
1686k = mg_i8
1687l = mg_i32
1688if gt l 65536
1689ret 1
1690end
1691h = mg_ih l
1692if ne get mgs 10 0
1693ret 1
1694end
1695a = ulong 0
1696if eq k 2
1697a = mg_bparse h l
1698if eq a MGNONE
1699ret 4
1700end
1701end
1702if eq k 3
1703if lt l 19
1704ret 4
1705end
1706a = mg_h8 add h 18
1707end
1708if eq k 0
1709ret 6
1710end
1711if gt k 3
1712ret 6
1713end
1714f = mg_nadd k 104 h l a
1715end
1716nr = mg_i32
1717if gt nr MGRMAX
1718ret 1
1719end
1720for i ulong nr
1721mg_iid add 8192 i
1722if gt i 0
1723if ne mg_idcmp add 8191 i add 8192 i 1
1724ret 3
1725end
1726end
1727l = mg_i32
1728if gt l 65536
1729ret 1
1730end
1731h = mg_ih l
1732put mgrc mul i 2 h
1733put mgrc add mul i 2 1 l
1734put mgs 4 add i 1
1735end
1736ne0 = mg_i32
1737if gt ne0 MGEMAX
1738ret 1
1739end
1740for i ulong ne0
1741mg_iid 104
1742rl = mg_i8
1743rl = or shl rl 8 mg_i8
1744mg_iid 105
1745mg_iid 106
1746if ne get mgs 10 0
1747ret 1
1748end
1749s = mg_nfind 104
1750d = mg_nfind 105
1751if eq s MGNONE
1752ret 5
1753end
1754if eq d MGNONE
1755ret 5
1756end
1757r = ulong 0
1758mg_idz 109
1759if ne mg_idcmp 106 109 0
1760r = mg_rfind 106
1761if eq r MGNONE
1762ret 5
1763end
1764r = add r 1
1765end
1766e = get mgs 5
1767put mged mul e 4 s
1768put mged add mul e 4 1 rl
1769put mged add mul e 4 2 d
1770put mged add mul e 4 3 r
1771put mgs 5 add e 1
1772if gt e 0
1773if ne mg_ecmp sub e 1 e 1
1774ret 3
1775end
1776end
1777end
1778if ne get mgs 10 0
1779ret 1
1780end
1781if ne get mgs 9 n
1782ret 1
1783end
1784for i ulong get mgs 3
1785if eq mg_nk i 1
1786if lt mg_nl i 32
1787ret 4
1788end
1789mg_hid mg_no i 107
1790b = mg_nfind 107
1791if eq b MGNONE
1792ret 5
1793end
1794put mgnd add mul i 4 3 b
1795end
1796end
1797ret 0
1798end