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 / protocol / synapse / claims / src / 70_cmds.om

849 lines · 14009 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
2note Subcommands. Options live in gop as (offset, length) pairs into the argument buffer av (offset tbad
3note = absent); see 90_main.om for the slot map.
4
5const ohome ulong 0
6const onow ulong 2
7const osig ulong 4
8const oag ulong 6
9const ose ulong 8
10const otg ulong 10
11const ottl ulong 12
12const omode ulong 14
13const ost ulong 16
14const oin ulong 18
15const oev ulong 20
16const oimp ulong 22
17const oid ulong 24
18const oall ulong 26
19const oapf ulong 28
20const opos ulong 30
21const ocmd ulong 32
22const oevn ulong 33
23
24global gevl ulong 512
25
26text musage "usage: synapse-claims [--home DIR] [--now TS] [--sig-bin PATH] init|claim|renew|release|state|who|list|conflicts|verify|expire ..."
27text mmagent "missing or invalid --agent"
28text mmsess "missing or invalid --session"
29text mmtg "missing or invalid --target"
30text mmttl "invalid --ttl (1..2592000)"
31text mmmode "invalid --mode (excl|shared)"
32text mmst "invalid --state"
33text mmin "invalid --intent (UTF-8, at most 4096 bytes)"
34text mmev "invalid --evidence"
35text mmimp "invalid --import"
36text mmid "missing --id"
37text mkeyg "keygen failed (synapse-claims-sig)"
38text mpub "ledger.pub unreadable"
39text mverb "signature check failed to run (synapse-claims-sig)"
40text tcli "cli"
41text tpulse "pulse"
42text tdefag "synapse-claims"
43text timp "import:"
44text tconf "{\"ok\":false,\"error\":\"conflict\",\"holder\":"
45text tconf2 "conflict: "
46text theld " held by "
47text tuntil ") until "
48text tokid "{\"ok\":true,\"id\":\""
49text tseq "\",\"seq\":"
50text texp2 ",\"expires\":"
51text tokpub "{\"ok\":true,\"pub\":\""
52text tvok "{\"ok\":true,\"events\":"
53text thead ",\"head\":\""
54text tvbad "{\"ok\":false,\"seq\":"
55text terr2 ",\"error\":\""
56text ta "{\"a\":"
57text tb ",\"b\":"
58text treas ",\"reason\":\""
59text tdid "did:key:"
60text tokv "ok "
61text tbadv "bad "
62
63fn op_has slot ulong to bool
64ret ne get gop slot tbad
65end
66
67fn op_sf av edit uchar * slot ulong msg view uchar * to ulong
68if eq op_has slot false
69die 64 msg
70end
71o = get gop slot
72n = get gop add slot 1
73if eq sf_ok part av o n false
74die 64 msg
75end
76ret ar_ln av o n
77end
78
79note decimal option value, 1..19 digits; tbad when absent or not a number
80fn op_uint av edit uchar * slot ulong to ulong
81if eq op_has slot false
82ret tbad
83end
84o = get gop slot
85n = get gop add slot 1
86if or eq n 0 gt n 19
87ret tbad
88end
89v = ulong 0
90for i ulong n
91c = get av add o i
92if or lt c 48 gt c 57
93ret tbad
94end
95v = add mul v 10 ulong sub c 48
96end
97ret v
98end
99
100fn op_ttl av edit uchar * to ulong
101if eq op_has ottl false
102ret 3600
103end
104v = op_uint av ottl
105if or or eq v tbad eq v 0 gt v 2592000
106die 64 mmttl
107end
108ret v
109end
110
111note option word from wall[base .. base+cnt); base 18 is --mode, base 5 is --state
112fn op_word av edit uchar * slot ulong base ulong cnt ulong to ulong
113o = get gop slot
114n = get gop add slot 1
115w = tw_find part av o n wall base cnt
116if eq w tbad
117if eq base 18
118die 64 mmmode
119end
120die 64 mmst
121end
122ret sub w base
123end
124
125note u8_cont: the k continuation bytes after av[p] are 128..191, the first one within lo..hi
126fn u8_cont av edit uchar * p ulong k ulong lo ulong hi ulong to bool
127for j ulong k
128d = ulong get av add p add j 1
129if eq j 0
130if or lt d lo gt d hi
131ret false
132end
133else
134if or lt d 128 gt d 191
135ret false
136end
137end
138end
139ret true
140end
141
142note UTF-8 check (RFC 3629: no overlongs, no surrogates, max U+10FFFF) of av[o..o+n)
143fn utf8_ok av edit uchar * o ulong n ulong to bool
144i = ulong 0
145while lt i n bound 5000
146c = ulong get av add o i
147k = ulong 0
148lo = ulong 128
149hi = ulong 191
150if lt c 128
151i = add i 1
152continue
153end
154if and ge c 194 le c 223
155k = 1
156end
157if and ge c 224 le c 239
158k = 2
159if eq c 224
160lo = 160
161end
162if eq c 237
163hi = 159
164end
165end
166if and ge c 240 le c 244
167k = 3
168if eq c 240
169lo = 144
170end
171if eq c 244
172hi = 143
173end
174end
175if eq k 0
176ret false
177end
178if gt add i k sub n 1
179ret false
180end
181if eq u8_cont av add o i k lo hi false
182ret false
183end
184i = add add i k 1
185end
186ret true
187end
188
189note intent: JSON-escaped copy in the arena (offset; length in gx xpos); empty when absent
190fn op_intent av edit uchar * to ulong
191st = get gx xarn
192put gx xpos 0
193if eq op_has oin false
194ret st
195end
196o = get gop oin
197n = get gop add oin 1
198if gt n 4096
199die 64 mmin
200end
201if eq utf8_ok av o n false
202die 64 mmin
203end
204for i ulong n
205c = get av add o i
206if or eq c 34 eq c 92
207ar_b 92
208ar_b c
209continue
210end
211if eq c 10
212ar_b 92
213ar_b 110
214continue
215end
216if eq c 9
217ar_b 92
218ar_b 116
219continue
220end
221if eq c 13
222ar_b 92
223ar_b 114
224continue
225end
226if lt c 32
227ar_b 92
228ar_b 117
229ar_b 48
230ar_b 48
231ar_b hexd ulong shr c 4
232ar_b hexd ulong and c 15
233continue
234end
235ar_b c
236end
237put gx xpos sub get gx xarn st
238ret st
239end
240
241note evidence: `"u1","u2"` in the arena (offset; length in gx xpos)
242fn op_evid av edit uchar * to ulong
243st = get gx xarn
244for i ulong get gop oevn
245o = get gevl mul i 2
246n = get gevl add mul i 2 1
247if eq sf_ok part av o n false
248die 64 mmev
249end
250if gt i 0
251ar_b 44
252end
253ar_b 34
254ar_lnv av o n
255ar_b 34
256end
257put gx xpos sub get gx xarn st
258ret st
259end
260
261note origin: "cli" | "import:" ORIGIN (prefix kept when already present)
262fn op_origin av edit uchar * to ulong
263if eq op_has oimp false
264st = ar_t tcli
265put gx xpos len tcli
266ret st
267end
268o = get gop oimp
269n = get gop add oimp 1
270if eq sf_ok part av o n false
271die 64 mmimp
272end
273st = get gx xarn
274pre = false
275if ge n len timp
276pre = true
277for i ulong len timp
278if ne get av add o i get timp i
279pre = false
280end
281end
282end
283if eq pre false
284ar_tv timp
285end
286ar_lnv av o n
287put gx xpos sub get gx xarn st
288if gt get gx xpos 256
289die 64 mmimp
290end
291ret st
292end
293
294fn o_ok id ulong seq ulong ex ulong to void
295o_t tokid
296o_id id
297o_t tseq
298o_u seq
299o_t texp2
300o_tsq ex
301o_b 125
302o_b 10
303end
304
305fn conflict i ulong to void
306o_t tconf
307o_hold i
308o_b 125
309o_b 10
310o_flush
311put gx xsel 1
312o_t tpref
313o_t tconf2
314o_ar c_get i ctg c_get i ctgl
315o_t theld
316o_ar c_get i cag c_get i cagl
317o_b 32
318o_b 40
319o_id c_get i cid
320o_t tuntil
321o_ts c_get i cexp
322o_b 58
323o_b 32
324o_ar c_get i cin c_get i cinl
325o_b 10
326e_flush
327exit 2
328end
329
330note holder of a conflicting active excl claim against (kind, target, agent), or ncl
331fn cl_clash kd ulong t ulong tl ulong a ulong al ulong to ulong
332for i ulong get gx xnc
333if c_act i
334if eq c_get i cmode 0
335if eq ar_eq c_get i cag c_get i cagl a al false
336if ne ov c_get i ckind c_get i ctg c_get i ctgl kd t tl 0
337ret i
338end
339end
340end
341end
342end
343ret ncl
344end
345
346fn ev_emit ln edit uchar * echo bool to void
347n = ev_build ln
348pend_add ln n
349if echo
350ret
351end
352pe_flush false
353end
354
355fn cm_claim av edit uchar * ln edit uchar * to void
356ag = op_sf av oag mmagent
357agl = get gop add oag 1
358se = op_sf av ose mmsess
359if eq op_has otg false
360die 64 mmtg
361end
362t = tg_canon av get gop otg get gop add otg 1
363if eq t tbad
364die 64 mmtg
365end
366put gev etg t
367put gev etgl get gx xpos
368put gev ekind get gx xkind
369put gev ettl op_ttl av
370md = ulong 0
371if eq get gx xkind 0
372md = 1
373end
374if op_has omode
375md = op_word av omode 18 2
376end
377put gev emode md
378put gev est 0
379if op_has ost
380put gev est op_word av ost 5 8
381end
382put gev ein op_intent av
383put gev einl get gx xpos
384put gev eev op_evid av
385put gev eevl get gx xpos
386put gev eor op_origin av
387put gev eorl get gx xpos
388put gev eag ag
389put gev eagl agl
390put gev ese se
391put gev esel get gop add ose 1
392put gev eop 0
393put gev eexp add get gx xnow get gev ettl
394lk_take 2
395ld_all ln
396if eq md 0
397h = cl_clash get gev ekind get gev etg get gev etgl ag agl
398if and ne h ncl eq op_has oimp false
399conflict h
400end
401end
402ev_emit ln false
403o_ok get gev eid get gx xseq get gev eexp
404end
405
406note cl_mine: claim index for --id held by --agent (exit 4 otherwise)
407fn cl_mine av edit uchar * to ulong
408if eq op_has oid false
409die 64 mmid
410end
411ag = op_sf av oag mmagent
412o = get gop oid
413n = get gop add oid 1
414if ne n 16
415die 4 mnf
416end
417for i ulong 16
418if eq hexv get av add o i 16
419die 4 mnf
420end
421end
422c = ht_find hx_u av o
423if eq c ncl
424die 4 mnf
425end
426if eq ar_eq c_get c cag c_get c cagl ag get gop add oag 1 false
427die 4 mnh
428end
429if ne c_get c cclo 0
430die 4 mna
431end
432ret c
433end
434
435fn ev_from i ulong to void
436put gev eid c_get i cid
437put gev eag c_get i cag
438put gev eagl c_get i cagl
439put gev ese c_get i cse
440put gev esel c_get i csel
441put gev ekind c_get i ckind
442put gev etg c_get i ctg
443put gev etgl c_get i ctgl
444put gev emode c_get i cmode
445put gev est c_get i cst
446put gev ein c_get i cin
447put gev einl c_get i cinl
448put gev ettl 0
449put gev eexp c_get i cexp
450put gev eev 0
451put gev eevl 0
452end
453
454note hd_live: renew needs a lease that has not expired yet (exit 4 otherwise)
455fn hd_live i ulong to void
456if le c_get i cexp get gx xnow
457die 4 mna
458end
459end
460
461note hd_fin: sets the renew/release expiry of the event in gev, appends it and prints the result
462fn hd_fin ln edit uchar * op ulong ttl ulong to void
463if eq op 1
464put gev ettl ttl
465put gev eexp add get gx xnow ttl
466end
467if eq op 2
468put gev eexp get gx xnow
469end
470ev_emit ln false
471o_ok get gev eid get gx xseq get gev eexp
472end
473
474note cm_hold: renew (op 1), release (op 2), state (op 3)
475fn cm_hold av edit uchar * ln edit uchar * op ulong to void
476ttl = ulong 0
477if eq op 1
478ttl = op_ttl av
479end
480if and eq op 3 eq op_has ost false
481die 64 mmst
482end
483stc = tbad
484if op_has ost
485stc = op_word av ost 5 8
486end
487ev = op_evid av
488evl = get gx xpos
489orr = op_origin av
490orl = get gx xpos
491lk_take 2
492ld_all ln
493i = cl_mine av
494if eq op 1
495hd_live i
496end
497ev_from i
498put gev eop op
499put gev eor orr
500put gev eorl orl
501put gev eev ev
502put gev eevl evl
503if ne stc tbad
504put gev est stc
505end
506hd_fin ln op ttl
507end
508
509fn cm_who av edit uchar * ln edit uchar * to void
510if eq op_has opos false
511die 64 mmtg
512end
513t = tg_canon av get gop opos get gop add opos 1
514if eq t tbad
515die 64 mmtg
516end
517kd = get gx xkind
518tl = get gx xpos
519lk_take 1
520ld_all ln
521for i ulong get gx xnc
522if c_act i
523if ne ov c_get i ckind c_get i ctg c_get i ctgl kd t tl 0
524o_claim i
525o_b 10
526end
527end
528end
529end
530
531note agent prefix filter: agent == P or starts with P "."
532fn apf_ok av edit uchar * i ulong to bool
533if eq op_has oapf false
534ret true
535end
536o = get gop oapf
537n = get gop add oapf 1
538a = c_get i cag
539al = c_get i cagl
540if lt al n
541ret false
542end
543for k ulong n
544if ne ar_g add a k get av add o k
545ret false
546end
547end
548if eq al n
549ret true
550end
551ret eq ar_g add a n 46
552end
553
554fn cm_list av edit uchar * ln edit uchar * to void
555lk_take 1
556ld_all ln
557all = op_has oall
558for i ulong get gx xnc
559if or all c_act i
560if apf_ok av i
561o_claim i
562o_b 10
563end
564end
565end
566end
567
568note cf_pair: prints the conflict line of exclusive claims i and k (k active, other agent, overlapping)
569fn cf_pair i ulong k ulong to bool
570if eq c_act k false
571ret false
572end
573if ne c_get k cmode 0
574ret false
575end
576if ar_eq c_get i cag c_get i cagl c_get k cag c_get k cagl
577ret false
578end
579ka = c_get k ckind
580kt = c_get k ctg
581kl = c_get k ctgl
582r = ov c_get i ckind c_get i ctg c_get i ctgl ka kt kl
583if eq r 0
584ret false
585end
586o_t ta
587o_claim i
588o_t tb
589o_claim k
590o_t treas
591o_wd wreason sub r 1
592o_q
593o_b 125
594o_b 10
595ret true
596end
597
598fn cm_conf ln edit uchar * to void
599lk_take 1
600ld_all ln
601found = false
602nc = get gx xnc
603for i ulong nc
604if c_act i
605if eq c_get i cmode 0
606for j ulong sub nc add i 1
607if cf_pair i add add i j 1
608found = true
609end
610end
611end
612end
613end
614if found
615o_flush
616exit 1
617end
618end
619
620fn cm_expire av edit uchar * ln edit uchar * to void
621ag = ulong 0
622agl = ulong 0
623if op_has oag
624ag = op_sf av oag mmagent
625agl = get gop add oag 1
626else
627ag = ar_t tdefag
628agl = len tdefag
629end
630orr = ar_t tcli
631orl = len tcli
632pst = ar_t tpulse
633if ar_eq ag agl pst len tpulse
634orr = pst
635orl = len tpulse
636end
637lk_take 2
638ld_all ln
639for i ulong get gx xnc
640if eq c_get i cclo 0
641if le c_get i cexp get gx xnow
642ev_from i
643put gev eop 4
644put gev eag ag
645put gev eagl agl
646put gev eor orr
647put gev eorl orl
648n = ev_build ln
649pend_add ln n
650c_put i cclo 2
651if eq pe_room false
652pe_flush true
653end
654end
655end
656end
657pe_flush true
658end
659
660note read a small file (whole) into gso; returns its length
661fn rd_small off ulong n ulong to ulong
662fd = fopen gpth off n 524288 0
663if lt fd 0
664die 70 mpub
665end
666k = read int fd gso 0 4096
667c = fclose int fd
668if le k 0
669die 70 mpub
670end
671ret ulong k
672end
673
674fn cm_init to void
675a = faccess gpth get gx xpkey get gx xpkeyl 0
676if ne a 0
677si_open
678pk_reset
679pk_g get gx xsig get gx xsigl
680pk_t tkeyg
681pk_g get gx xpkd get gx xpkdl
682r = si_run
683if ne r 0
684die 70 mkeyg
685end
686end
687n = rd_small get gx xppub get gx xppubl
688if eq get gso sub n 1 10
689n = sub n 1
690end
691if lt n len tdid
692die 70 mpub
693end
694for i ulong n
695c = get gso i
696if or or lt c 33 gt c 126 or eq c 34 eq c 92
697die 70 mpub
698end
699end
700o_t tokpub
701for j ulong n
702o_b get gso j
703end
704o_b 34
705o_b 125
706o_b 10
707end
708
709fn v_fail seq ulong why ulong to void
710o_t tvbad
711o_u seq
712o_t terr2
713o_wd wwhy why
714o_b 34
715o_b 125
716o_b 10
717o_flush
718exit 3
719end
720
721note vf_line: full check of one line k (= expected seq). 0 ok, else wwhy index + 1.
722fn vf_line ln edit uchar * n ulong to ulong
723r = ld_line ln n
724if ne r 0
725ret r
726end
727hp = get gp qhpos
728ev_hash ln hp
729for i ulong 64
730if ne get ln add add hp 9 i get glh i
731ret 4
732end
733end
734if eq get gp qop 0
735q = get gp qid
736for j ulong 16
737put gst j get ln add q j
738put ln add q j 48
739end
740ev_hash ln hp
741for k ulong 16
742if ne get ln add add hp 9 k get gst k
743ret 5
744end
745end
746end
747for a ulong 64
748si_b get glh a
749end
750si_b 32
751s = get gp qsig
752for b ulong 128
753si_b get ln add s b
754end
755si_b 10
756ret 0
757end
758
759note vf_sigs: verify the staged `hash sig` lines; a bad line K sets gx[xbad] = K
760fn vf_sigs to void
761put gx xbad 0
762good = get gx xevn
763if eq good 0
764x = fclose int get gx xmfd
765ret
766end
767pk_reset
768pk_g get gx xsig get gx xsigl
769pk_t tver
770pk_g get gx xppub get gx xppubl
771r = si_run
772n = get gx xson
773if lt n 4
774die 70 mverb
775end
776if and eq r 0 eq get gso 0 111
777ret
778end
779if ne r 3
780die 70 mverb
781end
782v = ulong 0
783for i ulong sub n 4
784c = get gso add i 4
785if or lt c 48 gt c 57
786break
787end
788v = add mul v 10 ulong sub c 48
789end
790if or eq v 0 gt v good
791die 70 mverb
792end
793put gx xbad v
794end
795fn cm_verify ln edit uchar * to void
796lk_take 1
797ld_reset
798fail = ulong 0
799why = ulong 0
800k = ulong 0
801fd = ld_open
802if ge fd 0
803si_open
804r_open fd
805while true bound 999999
806n = r_line ln
807if eq n -1
808break
809end
810k = add k 1
811if eq n -3
812die 70 mio
813end
814if lt n 0
815fail = k
816break
817end
818r = vf_line ln ulong n
819if ne r 0
820fail = k
821why = sub r 1
822break
823end
824end
825c = fclose int fd
826if eq k 0
827x = fclose int get gx xmfd
828end
829if gt k 0
830vf_sigs
831end
832end
833if gt get gx xbad 0
834v_fail get gx xbad 5
835end
836if gt fail 0
837v_fail fail why
838end
839o_t tvok
840o_u get gx xevn
841o_t thead
842for i ulong 64
843o_b get glh i
844end
845o_b 34
846o_b 125
847o_b 10
848end
849