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 / tools / codebook / codebook.om

1838 lines · 28380 bytes Raw Blame History
1note SPDX-License-Identifier: AGPL-3.0-only
2note codebook: T-002 items 7-10, the signed layered digit codebook, in OM-2 on the seeded SHA-256 core.
3note Built after the core of seed/trace/sha256.om (up to "fn sha256"); see tools/codebook/build.sh.
4note Wire form: a code is (9xx)^k then one terminal 000..899, each group exactly 3 decimal digits; a
5note message is codes concatenated with no separator. The model and proofs are proof/CodebookProof.lean.
6note Usage (batch, one stdin line in, one stdout line out):
7note codebook enc "901.12 345" -> "901012345" (or ERR syntax|range)
8note codebook dec "901012345" -> "901.12 345" (or ERR len|digit|esc)
9note codebook sha CB prints the codebook id, the sha256 of the TSV file
10note codebook sign CB KEY "lane|ts|from|to|tag|text|ref|counter|digits" ->
11note "ocb2:<cbsha>:<counter>:<digits>:<mac>"
12note codebook verify CB KEY STATE [LANE] inbox line -> "<verdict> <trit>" (LANE is n or e, default n)
13note MAC = HMAC-SHA256(key, ocb2|cbsha|counter|lane|ts|from|to|tag|text|ref|digits). A signed line must
14note be the 6-field form ts|from|to|tag|text|ref plus the ocb2 field. verify checks, in order: unsigned
15note (no ocb2 field or no MAC), legacy (ocb1, caps at trit 1), badsender, badfmt (including a missing
16note field or a bad timestamp), badcb, badlen/baddigit/badesc, unknown, badmac, badto (to != receiving
17note bot, when one is set), stale (counter not strictly above STATE/<sender>.ctr, or ts older than the
18note last accepted ts). Else ok: the counter and ts are stored atomically as "<counter> <ts>\n". Trit 2
19note only for ok plus a 3xx emergency code; every other line is 1. Exit: 2 usage, 4 bad codebook, 5 bad
20note key (unreadable, empty, mode & 077 != 0, or a path with a boards/ segment). The key is never printed;
21note key buffers are zeroed before exit. Keep the key file outside boards/.
22const BIG ulong 9223372036854775807
23abstract trit uint
24global fb uchar *
25global fbn ulong 1
26global cbk uchar *
27global cbn ulong 1
28global mb uchar *
29global mbn ulong 1
30global ms uchar *
31global msn ulong 1
32global ob uchar *
33global obn ulong 1
34global gr ulong *
35global ng ulong 1
36global ce ulong *
37global nc ulong 1
38global eo ulong *
39global nen ulong 1
40global pb uchar 4096
41global pn ulong 1
42global av uchar 64
43global avn ulong 1
44global sd uchar 2048
45global sdn ulong 1
46global kr uchar 4096
47global krn ulong 1
48global kz uchar 64
49global dg uchar 32
50global hx uchar 64
51global cs uchar 64
52global cb uchar 128
53global fs ulong 2
54global pk uchar 4096
55global pkn ulong 1
56global mo ulong 1
57global vq ulong 1
58global vt ulong 1
59global vm ulong 1
60global vf ulong 1
61global ve ulong 1
62global vc ulong 1
63global ssep ulong 1
64global vtb uchar 4096
65global vtn ulong 1
66global vti ulong 384
67global vtc ulong 1
68global vln ulong 1
69global stt uchar 128
70global nts uchar 32
71global ntn ulong 1
72global lts uchar 32
73global ltn ulong 1
74global fa ulong 12
75global rbot uchar 256
76global rbn ulong 1
77global vc0 ulong 1
78global vcl ulong 1
79global vd0 ulong 1
80global vdl ulong 1
81text thex "0123456789abcdef"
82text tenc "enc"
83text tdec "dec"
84text tsha "sha"
85text tsign "sign"
86text tver "verify"
87text tr45 "r45"
88text tins "inst"
89text tsig "sig"
90text temg "emerg"
91text tocb "ocb2:"
92text tocb4 "ocb2"
93text tocbo "ocb1:"
94text tocb1 "ocb1"
95text tctr ".ctr"
96text ttmp ".tmp"
97text tlock "ctr.lock"
98text eesyn "ERR syntax"
99text eerng "ERR range"
100text eelen "ERR len"
101text eedig "ERR digit"
102text eeesc "ERR esc"
103text eesnd "ERR sender"
104text eectr "ERR counter"
105text eeunk "ERR unknown"
106text eefmt "ERR fmt"
107text vok "ok"
108text vuns "unsigned"
109text vsnd "badsender"
110text vfmt "badfmt"
111text vcb "badcb"
112text vlen "badlen"
113text vdig "baddigit"
114text vesc "badesc"
115text vunk "unknown"
116text vmac "badmac"
117text vstl "stale"
118text vleg "legacy"
119text vto "badto"
120
121note The trit type: only tritmk makes one, so no value outside 0..2 exists.
122fn tritmk x uint to trit
123if gt x 2
124exit 70
125end
126ret seal trit x
127end
128
129note Lean lineTrit: 2 only for a verified line that carries an emergency code, else 1.
130fn ltrit ok bool em bool to trit
131if and ok em
132ret tritmk 2
133end
134ret tritmk 1
135end
136
137fn obyte c uchar to void
138n = get obn 0
139if ge n len ob
140e1 = enlarge ob add mul n 2 4096
141if ne e1 0
142exit 72
143end
144end
145put ob n c
146put obn 0 add n 1
147end
148
149fn otext t view uchar * to void
150for i ulong len t bound BIG
151obyte get t i
152end
153end
154
155fn mbyte c uchar to void
156n = get mbn 0
157if ge n len mb
158e1 = enlarge mb add mul n 2 4096
159if ne e1 0
160exit 72
161end
162end
163put mb n c
164put mbn 0 add n 1
165end
166
167fn msbyte c uchar to void
168n = get msn 0
169if ge n len ms
170e1 = enlarge ms add mul n 2 4096
171if ne e1 0
172exit 72
173end
174end
175put ms n c
176put msn 0 add n 1
177end
178
179fn gpush v ulong to void
180n = get ng 0
181if ge n len gr
182e1 = enlarge gr add mul n 2 64
183if ne e1 0
184exit 72
185end
186end
187put gr n v
188put ng 0 add n 1
189end
190
191fn cpush v ulong to void
192n = get nc 0
193if ge n len ce
194e1 = enlarge ce add mul n 2 64
195if ne e1 0
196exit 72
197end
198end
199put ce n v
200put nc 0 add n 1
201end
202
203fn wrall fd int to void
204i = ulong 0
205n = get obn 0
206while lt i n bound BIG
207r = write fd ob i sub n i
208if le r 0
209exit 75
210end
211i = add i ulong r
212end
213put obn 0 0
214end
215
216fn odec v ulong to void
217n = ulong 1
218x = v
219while ge x 10 bound BIG
220x = div x 10
221n = add n 1
222end
223x = v
224for i ulong n bound BIG
225put cb sub sub n 1 i add uchar rem x 10 48
226x = div x 10
227end
228for j ulong n bound BIG
229obyte get cb j
230end
231end
232
233fn o3 v ulong to void
234obyte add uchar rem div v 100 10 48
235obyte add uchar rem div v 10 10 48
236obyte add uchar rem v 10 48
237end
238
239note Read all of fd into fb (grows as needed; no size limit).
240fn ldfd fd int to void
241n = ulong 0
242while true bound BIG
243if lt len fb add n 4096
244e1 = enlarge fb add mul n 2 8192
245if ne e1 0
246exit 72
247end
248end
249r = read int fd fb n 4096
250if lt r 0
251exit 73
252end
253if eq r 0
254break
255end
256n = add n ulong r
257end
258put fbn 0 n
259end
260
261fn argpb a ulong to void
262p = argread a pb 0 len pb
263if lt p 1
264exit 2
265end
266put pn 0 ulong p
267end
268
269fn loadp to bool
270put fbn 0 0
271fd = fopen pb 0 get pn 0 0 0
272if lt fd 0
273ret false
274end
275ldfd int fd
276e2 = fclose int fd
277ret true
278end
279
280fn isdig c uchar to bool
281if lt c 48
282ret false
283end
284if gt c 57
285ret false
286end
287ret true
288end
289
290fn ishex c uchar to bool
291if isdig c
292ret true
293end
294if lt c 97
295ret false
296end
297if gt c 102
298ret false
299end
300ret true
301end
302
303fn okch c uchar to bool
304if isdig c
305ret true
306end
307if eq c 45
308ret true
309end
310if lt c 97
311ret false
312end
313if gt c 122
314ret false
315end
316ret true
317end
318
319fn lnend p ulong to ulong
320i = p
321while lt i get fbn 0 bound BIG
322if eq get fb i 10
323ret i
324end
325i = add i 1
326end
327ret i
328end
329
330note Field kk of fb[p..e) split on byte sp, into fs 0 (start) and fs 1 (end).
331fn splitf p ulong e ulong kk ulong sp uchar to bool
332s = p
333f = ulong 0
334i = p
335while lt i e bound BIG
336if eq get fb i sp
337if eq f kk
338put fs 0 s
339put fs 1 i
340ret true
341end
342f = add f 1
343s = add i 1
344end
345i = add i 1
346end
347if eq f kk
348put fs 0 s
349put fs 1 e
350ret true
351end
352ret false
353end
354
355fn feq t view uchar * to bool
356s = get fs 0
357n = sub get fs 1 s
358if ne n len t
359ret false
360end
361for i ulong n bound BIG
362if ne get fb add s i get t i
363ret false
364end
365end
366ret true
367end
368
369fn dv q ulong to ulong
370ret ulong sub get fb q 48
371end
372
373note Decode fb[p..e) into groups gr and code ends ce (index of each code's last group).
374note 0 ok, 1 length not a multiple of 3, 2 non-digit, 3 dangling escape (Lean groups/parse).
375fn decl p ulong e ulong to ulong
376put ng 0 0
377put nc 0 0
378n = sub e p
379if ne rem n 3 0
380ret 1
381end
382for i ulong n bound BIG
383if eq isdig get fb add p i false
384ret 2
385end
386end
387m = div n 3
388for j ulong m bound BIG
389q = add p mul j 3
390g = add add mul dv q 100 mul dv add q 1 10 dv add q 2
391gpush g
392if lt g 900
393cpush sub get ng 0 1
394end
395end
396if gt get ng 0 0
397if ge get gr sub get ng 0 1 900
398ret 3
399end
400end
401ret 0
402end
403
404note Canonical text of the decoded codes: groups in decimal joined by '.', codes joined by ' '.
405fn otcode to void
406s = ulong 0
407for j ulong get nc 0 bound BIG
408if gt j 0
409obyte 32
410end
411en = get ce j
412i = s
413while le i en bound BIG
414if gt i s
415obyte 46
416end
417odec get gr i
418i = add i 1
419end
420s = add en 1
421end
422end
423
424note Parse the text form fb[p..e) into gr/ce: 0 ok, 4 syntax, 5 range (escape 900..999 before a
425note terminal 0..899, Lean Valid).
426fn encp p ulong e ulong to ulong
427put ng 0 0
428put nc 0 0
429if eq p e
430ret 0
431end
432i = p
433while true bound BIG
434v = ulong 0
435d = ulong 0
436while lt i e bound BIG
437if eq isdig get fb i false
438break
439end
440v = add mul v 10 dv i
441d = add d 1
442i = add i 1
443end
444if or eq d 0 gt d 3
445ret 4
446end
447gpush v
448if eq i e
449cpush sub get ng 0 1
450break
451end
452c = get fb i
453if eq c 32
454cpush sub get ng 0 1
455else
456if ne c 46
457ret 4
458end
459end
460i = add i 1
461end
462ret encv
463end
464
465fn encv to ulong
466s = ulong 0
467for j ulong get nc 0 bound BIG
468en = get ce j
469i = s
470while le i en bound BIG
471g = get gr i
472if lt i en
473if lt g 900
474ret 5
475end
476else
477if ge g 900
478ret 5
479end
480end
481i = add i 1
482end
483s = add en 1
484end
485ret 0
486end
487
488fn doenc to void
489p = ulong 0
490while lt p get fbn 0 bound BIG
491e = lnend p
492r = encp p e
493if eq r 0
494for i ulong get ng 0 bound BIG
495o3 get gr i
496end
497else
498if eq r 4
499otext eesyn
500else
501otext eerng
502end
503end
504obyte 10
505p = add e 1
506end
507end
508
509fn dodec to void
510p = ulong 0
511while lt p get fbn 0 bound BIG
512e = lnend p
513r = decl p e
514if eq r 0
515otcode
516else
517if eq r 1
518otext eelen
519end
520if eq r 2
521otext eedig
522end
523if eq r 3
524otext eeesc
525end
526end
527obyte 10
528p = add e 1
529end
530end
531
532note SHA-256 of mb[0..mbn) into h, streamed block by block over the seeded hinit/compress.
533note seed/trace/sha256.om:175 "fn sha256 data view uchar 4096" stops at 4096 bytes and a growable
534note global cannot be passed to it, so this driver pads and hashes buffers of any length.
535fn hmb to void
536hinit
537n = get mbn 0
538full = div n 64
539for b ulong full bound BIG
540o = mul b 64
541for i ulong 64
542put blk i get mb add o i
543end
544compress
545end
546hpad mul full 64 rem n 64 n
547end
548
549fn hpad o ulong left ulong n ulong to void
550for i ulong 64
551put blk i 0
552end
553for j ulong left bound BIG
554put blk j get mb add o j
555end
556put blk left 128
557if ge left 56
558compress
559for i ulong 64
560put blk i 0
561end
562end
563bits = mul n 8
564for q ulong 8
565v = and shr bits mul sub 7 q 8 255
566put blk add 56 q uchar v
567end
568compress
569end
570
571fn hdig to void
572for i ulong 8
573v = ulong get h i
574for j ulong 4
575put dg add mul i 4 j uchar and shr v mul sub 3 j 8 255
576end
577end
578end
579
580fn hhex to void
581for i ulong 8
582v = ulong get h i
583for j ulong 4
584x = and shr v mul sub 3 j 8 255
585o = add mul i 8 mul j 2
586put hx o get thex shr x 4
587put hx add o 1 get thex and x 15
588end
589end
590end
591
592note HMAC key block K0 (RFC 2104): the key, or its sha256 when longer than 64 bytes, zero padded.
593fn mkk0 to void
594for i ulong 64
595put kz i 0
596end
597n = get krn 0
598if gt n 64
599put mbn 0 0
600for j ulong n bound BIG
601mbyte get kr j
602end
603hmb
604hdig
605for q ulong 32
606put kz q get dg q
607end
608ret
609end
610for j ulong n bound BIG
611put kz j get kr j
612end
613end
614
615note HMAC-SHA256(K0, ms) as 64 hex bytes in hx: H((K0^ipad) || H((K0^opad)... per RFC 2104.
616fn hmac to void
617put mbn 0 0
618for i ulong 64
619mbyte uchar xor get kz i 54
620end
621for j ulong get msn 0 bound BIG
622mbyte get ms j
623end
624hmb
625hdig
626put mbn 0 0
627for i ulong 64
628mbyte uchar xor get kz i 92
629end
630for q ulong 32
631mbyte get dg q
632end
633hmb
634hhex
635for i ulong 64
636put mb i 0
637end
638end
639
640fn zap to void
641for i ulong len kr
642put kr i 0
643end
644for j ulong 64
645put kz j 0
646end
647if ge len mb 64
648for q ulong 64
649put mb q 0
650end
651end
652end
653
654fn isty p ulong e ulong to ulong
655if feq tr45
656ret 1
657end
658if feq tins
659ret 2
660end
661if feq tsig
662ret 3
663end
664if feq temg
665ret 4
666end
667ret 0
668end
669
670fn sameb a ulong b ulong n ulong to bool
671for i ulong n bound BIG
672if ne get fb add a i get fb add b i
673ret false
674end
675end
676ret true
677end
678
679note One TSV entry fb[p..e): code (one valid layered code), type (r45|inst|sig|emerg), meaning
680note (1..200 printable bytes). 3xx codes and the emerg type go together. A code may appear once.
681fn cbent p ulong e ulong to bool
682if eq splitf p e 3 9 true
683ret false
684end
685if eq splitf p e 2 9 false
686ret false
687end
688ms0 = get fs 0
689ml = sub get fs 1 ms0
690if or eq ml 0 gt ml 200
691ret false
692end
693for i ulong ml bound BIG
694c = get fb add ms0 i
695if or lt c 32 gt c 126
696ret false
697end
698end
699if eq splitf p e 1 9 false
700ret false
701end
702ty = isty p e
703if eq ty 0
704ret false
705end
706if eq splitf p e 0 9 false
707ret false
708end
709cs0 = get fs 0
710cl = sub get fs 1 cs0
711r = decl cs0 add cs0 cl
712if or ne r 0 ne get nc 0 1
713ret false
714end
715ret cbent2 cs0 cl ty
716end
717
718fn cbent2 cs0 ulong cl ulong ty ulong to bool
719em = false
720if eq get ng 0 1
721g = get gr 0
722if and ge g 300 lt g 400
723em = true
724end
725end
726if ne em eq ty 4
727ret false
728end
729for i ulong get nen 0 bound BIG
730b = mul i 2
731if eq get eo add b 1 cl
732if sameb get eo b cs0 cl
733ret false
734end
735end
736end
737n = get nen 0
738if lt len eo add mul n 2 2
739e1 = enlarge eo add mul n 4 64
740if ne e1 0
741exit 72
742end
743end
744put eo mul n 2 cs0
745put eo add mul n 2 1 cl
746put nen 0 add n 1
747ret true
748end
749
750note Validate and index the codebook in fb. Every line ends in LF; '#' lines are comments.
751fn cbval to bool
752put nen 0 0
753n = get fbn 0
754if eq n 0
755ret false
756end
757if ne get fb sub n 1 10
758ret false
759end
760p = ulong 0
761while lt p n bound BIG
762e = lnend p
763if ne get fb p 35
764if eq cbent p e false
765ret false
766end
767end
768p = add e 1
769end
770ret true
771end
772
773note Load, validate and index the codebook named by argument a; its sha256 (hex) is the id in cs.
774fn ldcb a ulong to void
775argpb a
776ldcbq
777end
778
779note Load and validate the codebook whose path is already in pb (exit 4 when unreadable or invalid).
780fn ldcbq to void
781if eq loadp false
782exit 4
783end
784if eq cbval false
785exit 4
786end
787n = get fbn 0
788put mbn 0 0
789for i ulong n bound BIG
790mbyte get fb i
791end
792hmb
793hhex
794for j ulong 64
795put cs j get hx j
796end
797if lt len cbk n
798e1 = enlarge cbk add n 64
799if ne e1 0
800exit 72
801end
802end
803for q ulong n bound BIG
804put cbk q get fb q
805end
806put cbn 0 n
807end
808
809fn ldkey a ulong to void
810argpb a
811ldkeyq
812end
813
814note True when pb[0..pn) has a path segment exactly equal to boards (so a key inside
815note boards/ is refused; a name like boards-key is not).
816fn bdseg to bool
817n = get pn 0
818i = ulong 0
819while le i n bound BIG
820e = i
821while lt e n bound BIG
822if eq get pb e 47
823break
824end
825e = add e 1
826end
827if eq sub e i 6
828q = ulong 0
829if eq get pb i 98
830if eq get pb add i 1 111
831if eq get pb add i 2 97
832if eq get pb add i 3 114
833if eq get pb add i 4 100
834if eq get pb add i 5 115
835q = 1
836end
837end
838end
839end
840end
841end
842if eq q 1
843ret true
844end
845end
846if eq e n
847break
848end
849i = add e 1
850end
851ret false
852end
853
854fn ldkeyq to void
855if eq bdseg true
856exit 5
857end
858fd = fopen pb 0 get pn 0 0 0
859if lt fd 0
860exit 5
861end
862fsr = fstat int fd stt 0 128
863e2 = fclose int fd
864if or lt fsr 0 ne and get stt 16 63 0
865exit 5
866end
867if eq loadp false
868exit 5
869end
870n = get fbn 0
871if or eq n 0 gt n len kr
872exit 5
873end
874for i ulong n bound BIG
875put kr i get fb i
876put fb i 0
877end
878put krn 0 n
879mkk0
880end
881
882fn rdin to void
883put fbn 0 0
884ldfd int 0
885end
886
887note fb[a..a+n) equals cbk[s..s+n).
888fn cbeq a ulong s ulong n ulong to bool
889for j ulong n bound BIG
890if ne get fb add a j get cbk add s j
891ret false
892end
893end
894ret true
895end
896
897note Is the code fb[a..a+n) an entry of the loaded codebook?
898fn lookup a ulong n ulong to bool
899for i ulong get nen 0 bound BIG
900b = mul i 2
901if eq get eo add b 1 n
902if cbeq a get eo b n
903ret true
904end
905end
906end
907ret false
908end
909
910note All codes decoded from digits at p are in the codebook.
911fn known p ulong to bool
912s = ulong 0
913for j ulong get nc 0 bound BIG
914en = get ce j
915a = add p mul s 3
916n = mul sub add en 1 s 3
917if eq lookup a n false
918ret false
919end
920s = add en 1
921end
922ret true
923end
924
925note Lean emerg: some code is the single group 300..399.
926fn hasem to bool
927s = ulong 0
928for j ulong get nc 0 bound BIG
929en = get ce j
930if eq en s
931g = get gr s
932if and ge g 300 lt g 400
933ret true
934end
935end
936s = add en 1
937end
938ret false
939end
940
941fn slugok s ulong n ulong to bool
942if or eq n 0 gt n 64
943ret false
944end
945for i ulong n bound BIG
946if eq okch get fb add s i false
947ret false
948end
949end
950ret true
951end
952
953note Canonical counter: 1..18 digits, no leading zero; returns the value or 0 when malformed.
954fn ctrval s ulong n ulong to ulong
955if or eq n 0 gt n 18
956ret 0
957end
958if eq get fb s 48
959ret 0
960end
961v = ulong 0
962for i ulong n bound BIG
963c = get fb add s i
964if eq isdig c false
965ret 0
966end
967v = add mul v 10 ulong sub c 48
968end
969ret v
970end
971
972fn msrng s ulong n ulong to void
973for i ulong n bound BIG
974msbyte get fb add s i
975end
976end
977
978note tsok: YYYY-MM-DDTHH:MM, 16 bytes, so lexicographic compare matches time order.
979fn tsok s ulong n ulong to bool
980if ne n 16
981ret false
982end
983for i ulong 16
984c = get fb add s i
985if or eq i 4 eq i 7
986if ne c 45
987ret false
988end
989else
990if eq i 10
991if ne c 84
992ret false
993end
994else
995if eq i 13
996if ne c 58
997ret false
998end
999else
1000if eq isdig c false
1001ret false
1002end
1003end
1004end
1005end
1006end
1007ret true
1008end
1009
1010note tsok on a copy in nts (the line's ts is saved there before wrctr).
1011fn ntsok to bool
1012if ne get ntn 0 16
1013ret false
1014end
1015for i ulong 16
1016c = get nts i
1017if or eq i 4 eq i 7
1018if ne c 45
1019ret false
1020end
1021else
1022if eq i 10
1023if ne c 84
1024ret false
1025end
1026else
1027if eq i 13
1028if ne c 58
1029ret false
1030end
1031else
1032if eq isdig c false
1033ret false
1034end
1035end
1036end
1037end
1038end
1039ret true
1040end
1041
1042note Last accepted ts (lts) is <= the new ts in nts. An empty last ts always passes.
1043fn tsge to bool
1044if eq get ltn 0 0
1045ret true
1046end
1047if ne get ltn 0 get ntn 0
1048ret false
1049end
1050for i ulong get ntn 0 bound BIG
1051a = get lts i
1052b = get nts i
1053if lt b a
1054ret false
1055end
1056if lt a b
1057ret true
1058end
1059end
1060ret true
1061end
1062
1063note Fill fa[0..11] with start/end of the 6 content fields in fb[p..q).
1064fn setfa p ulong q ulong to bool
1065kk = ulong 0
1066s = p
1067i = p
1068while lt i q bound BIG
1069if eq get fb i 124
1070if eq kk 5
1071ret false
1072end
1073put fa mul kk 2 s
1074put fa add mul kk 2 1 i
1075kk = add kk 1
1076s = add i 1
1077end
1078i = add i 1
1079end
1080if ne kk 5
1081ret false
1082end
1083put fa 10 s
1084put fa 11 q
1085ret true
1086end
1087
1088fn savets to void
1089a = get fa 0
1090b = get fa 1
1091n = sub b a
1092if gt n 32
1093n = 32
1094end
1095for i ulong n bound BIG
1096put nts i get fb add a i
1097end
1098put ntn 0 n
1099end
1100
1101note ms = ocb2|cbsha|counter|lane|ts|from|to|tag|text|ref|digits.
1102fn mkocb to void
1103put msn 0 0
1104msbyte 111
1105msbyte 99
1106msbyte 98
1107msbyte 50
1108msbyte 124
1109for i ulong 64
1110msbyte get cs i
1111end
1112msbyte 124
1113msrng get vc0 0 get vcl 0
1114msbyte 124
1115msbyte uchar get vln 0
1116for kk ulong 6
1117msbyte 124
1118a = get fa mul kk 2
1119b = get fa add mul kk 2 1
1120msrng a sub b a
1121end
1122msbyte 124
1123msrng get vd0 0 get vdl 0
1124end
1125
1126note ms = sender|cbsha|counter|digits (kept for the ocb1 path, which never verifies).
1127fn mkmsg s ulong sn ulong c ulong cn ulong d ulong dn ulong to void
1128put msn 0 0
1129msrng s sn
1130msbyte 124
1131for i ulong 64
1132msbyte get cs i
1133end
1134msbyte 124
1135msrng c cn
1136msbyte 124
1137msrng d dn
1138end
1139
1140fn dosign to void
1141p = ulong 0
1142while lt p get fbn 0 bound BIG
1143e = lnend p
1144signl p e
1145obyte 10
1146p = add e 1
1147end
1148end
1149
1150fn signl p ulong e ulong to void
1151c = ulong 0
1152i = p
1153while lt i e bound BIG
1154if eq get fb i 124
1155c = add c 1
1156end
1157i = add i 1
1158end
1159if ne c 8
1160otext eefmt
1161ret
1162end
1163if eq splitf p e 0 124 false
1164otext eefmt
1165ret
1166end
1167if ne sub get fs 1 get fs 0 1
1168otext eefmt
1169ret
1170end
1171o = get fs 0
1172if and ne get fb o 110 ne get fb o 101
1173otext eefmt
1174ret
1175end
1176put vln 0 ulong get fb o
1177if eq splitf p e 1 124 false
1178otext eefmt
1179ret
1180end
1181if eq tsok get fs 0 sub get fs 1 get fs 0 false
1182otext eefmt
1183ret
1184end
1185if eq splitf p e 2 124 false
1186otext eefmt
1187ret
1188end
1189if eq slugok get fs 0 sub get fs 1 get fs 0 false
1190otext eesnd
1191ret
1192end
1193if eq splitf p e 3 124 false
1194otext eefmt
1195ret
1196end
1197if eq slugok get fs 0 sub get fs 1 get fs 0 false
1198otext eefmt
1199ret
1200end
1201if eq splitf p e 7 124 false
1202otext eefmt
1203ret
1204end
1205put vc0 0 get fs 0
1206put vcl 0 sub get fs 1 get fs 0
1207if eq ctrval get vc0 0 get vcl 0 0
1208otext eectr
1209ret
1210end
1211if eq splitf p e 8 124 false
1212otext eefmt
1213ret
1214end
1215put vd0 0 get fs 0
1216put vdl 0 sub get fs 1 get fs 0
1217if eq splitf p e 1 124 false
1218otext eefmt
1219ret
1220end
1221a = get fs 0
1222if eq splitf p e 7 124 false
1223otext eefmt
1224ret
1225end
1226if eq setfa a sub get fs 0 1 false
1227otext eefmt
1228ret
1229end
1230savets
1231if eq ntsok false
1232otext eefmt
1233ret
1234end
1235r = decl get vd0 0 add get vd0 0 get vdl 0
1236if ne r 0
1237if eq r 1
1238otext eelen
1239end
1240if eq r 2
1241otext eedig
1242end
1243if eq r 3
1244otext eeesc
1245end
1246ret
1247end
1248if eq known get vd0 0 false
1249otext eeunk
1250ret
1251end
1252mkocb
1253hmac
1254emitsig
1255end
1256
1257fn emitsig to void
1258otext tocb
1259for i ulong 64
1260obyte get cs i
1261end
1262obyte 58
1263b = get vc0 0
1264for j ulong get vcl 0 bound BIG
1265obyte get fb add b j
1266end
1267obyte 58
1268d = get vd0 0
1269for q ulong get vdl 0 bound BIG
1270obyte get fb add d q
1271end
1272obyte 58
1273for t ulong 64
1274obyte get hx t
1275end
1276end
1277
1278fn pbyte c uchar to void
1279n = get pn 0
1280if ge n len pb
1281exit 71
1282end
1283put pb n c
1284put pn 0 add n 1
1285end
1286
1287fn ptext t view uchar * to void
1288for i ulong len t bound BIG
1289pbyte get t i
1290end
1291end
1292
1293note Counter file path STATE/<sender>.ctr into pb.
1294fn ctrpath s ulong n ulong to void
1295sdsep
1296for j ulong n bound BIG
1297pbyte get fb add s j
1298end
1299ptext tctr
1300end
1301
1302note The sender's last accepted counter; 0 when no file. A malformed file returns BIG (fail closed).
1303note File form is "<counter> <ts>\n"; a lone counter (old form) is accepted with an empty last ts.
1304fn rdctf s ulong n ulong to ulong
1305put ltn 0 0
1306ctrpath s n
1307fd = fopen pb 0 get pn 0 0 0
1308if lt fd 0
1309ret 0
1310end
1311r = read int fd cb 0 len cb
1312e2 = fclose int fd
1313if lt r 1
1314ret BIG
1315end
1316m = ulong r
1317if eq get cb sub m 1 10
1318m = sub m 1
1319end
1320if eq m 0
1321ret BIG
1322end
1323sp = BIG
1324for i ulong m bound BIG
1325if eq get cb i 32
1326sp = i
1327break
1328end
1329end
1330ww = m
1331if ne sp BIG
1332ww = sp
1333end
1334if or eq ww 0 gt ww 18
1335ret BIG
1336end
1337v = ulong 0
1338for j ulong ww bound BIG
1339c = get cb j
1340if eq isdig c false
1341ret BIG
1342end
1343v = add mul v 10 ulong sub c 48
1344end
1345if ne sp BIG
1346if eq takets m sp false
1347ret BIG
1348end
1349end
1350ret v
1351end
1352
1353fn takets m ulong sp ulong to bool
1354tn = sub m add sp 1
1355if or eq tn 0 gt tn 32
1356ret false
1357end
1358x = add sp 1
1359for kk ulong tn bound BIG
1360put lts kk get cb add x kk
1361end
1362put ltn 0 tn
1363ret true
1364end
1365
1366fn pkrec to void
1367n = get pn 0
1368o = get pkn 0
1369if gt add add o n 2 len pk
1370exit 78
1371end
1372put pk o uchar and n 255
1373put pk add o 1 uchar shr n 8
1374for i ulong n bound BIG
1375put pk add add o 2 i get pb i
1376end
1377put pkn 0 add add o n 2
1378end
1379
1380note pb = STATE then the separator: '/' for codebook verify, '.' for trit (DIR/inbox/<bot>.<sender>.ctr).
1381fn sdsep to void
1382put pn 0 0
1383for i ulong get sdn 0 bound BIG
1384pbyte get sd i
1385end
1386if eq get ssep 0 0
1387pbyte 47
1388else
1389pbyte uchar get ssep 0
1390end
1391end
1392
1393note The last accepted counter: the counter file, raised by the in-memory table (trit read mode).
1394fn rdts i ulong n ulong to void
1395o = get vti mul i 3
1396for j ulong 16
1397put lts j get vtb add add o n j
1398end
1399put ltn 0 16
1400end
1401
1402fn rdctr s ulong n ulong to ulong
1403v = rdctf s n
1404t = vtget s n
1405if gt t v
1406i = vtfind s n
1407if ne i BIG
1408rdts i n
1409end
1410ret t
1411end
1412ret v
1413end
1414
1415note In-memory counter table (sender bytes in vtb, entries off/len/ctr in vti): used when vt = 1,
1416note so a read-only pass (trit without --ack) still refuses a replay later in the same pass.
1417fn vtfind s ulong n ulong to ulong
1418for i ulong get vtc 0 bound BIG
1419b = mul i 3
1420if eq get vti add b 1 n
1421if vteq get vti b s n
1422ret i
1423end
1424end
1425end
1426ret BIG
1427end
1428
1429fn vteq o ulong s ulong n ulong to bool
1430for j ulong n bound BIG
1431if ne get vtb add o j get fb add s j
1432ret false
1433end
1434end
1435ret true
1436end
1437
1438fn vtget s ulong n ulong to ulong
1439i = vtfind s n
1440if eq i BIG
1441ret 0
1442end
1443ret get vti add mul i 3 2
1444end
1445
1446fn vtput s ulong n ulong c ulong to void
1447i = vtfind s n
1448if ne i BIG
1449b = mul i 3
1450if gt c get vti add b 2
1451put vti add b 2 c
1452o2 = get vti b
1453for j ulong get ntn 0 bound BIG
1454put vtb add add o2 n j get nts j
1455end
1456end
1457ret
1458end
1459o = get vtn 0
1460kq = get vtc 0
1461tn = get ntn 0
1462if or gt add add o n tn len vtb ge mul add kq 1 3 len vti
1463exit 79
1464end
1465for j ulong n bound BIG
1466put vtb add o j get fb add s j
1467end
1468for kk ulong tn bound BIG
1469put vtb add add o n kk get nts kk
1470end
1471b2 = mul kq 3
1472put vti b2 o
1473put vti add b2 1 n
1474put vti add b2 2 c
1475put vtn 0 add add o n tn
1476put vtc 0 add kq 1
1477end
1478
1479note fsync the directory holding the path in pb, so a rename survives a crash (opened O_RDONLY).
1480fn syncdir to void
1481n = get pn 0
1482i = n
1483while gt i 0 bound BIG
1484if eq get pb sub i 1 47
1485break
1486end
1487i = sub i 1
1488end
1489if eq i 0
1490put pn 0 0
1491pbyte 46
1492else
1493if eq i 1
1494put pn 0 1
1495else
1496put pn 0 sub i 1
1497end
1498end
1499fd = fopen pb 0 get pn 0 0 0
1500if lt fd 0
1501exit 77
1502end
1503s1 = fsync int fd
1504e2 = fclose int fd
1505if ne s1 0
1506exit 77
1507end
1508end
1509
1510note Exclusive flock on STATE<sep>ctr.lock for one verify-and-advance pass. The lock file is never
1511note renamed, so it stays the same inode while the .ctr files are replaced by rename.
1512fn lockst to int
1513sdsep
1514ptext tlock
1515fd = fopen pb 0 get pn 0 66 384
1516if lt fd 0
1517exit 76
1518end
1519r = flock int fd 2
1520if ne r 0
1521exit 76
1522end
1523ret int fd
1524end
1525
1526fn unlockst fd int to void
1527r = flock fd 8
1528e2 = fclose fd
1529end
1530
1531note Verify fb[p..e) with the verdict in globals: vm (MAC good), vf (ok: MAC good and fresh),
1532note ve (ok and some code is 3xx), vc (the counter when vm). Prints only when vq = 0.
1533fn verx p ulong e ulong to void
1534put vm 0 0
1535put vf 0 0
1536put ve 0 0
1537put vc 0 0
1538if eq get vln 0 0
1539put vln 0 110
1540end
1541verl p e
1542end
1543
1544note Atomic counter write: "<counter> <ts>\n" into <file>.tmp, fsync, then renameat2 over the file.
1545fn wrctr s ulong n ulong c ulong cn ulong to void
1546for i ulong cn bound BIG
1547put cb i get fb add c i
1548end
1549put cb cn 32
1550for j ulong get ntn 0 bound BIG
1551put cb add add cn 1 j get nts j
1552end
1553put cb add add cn get ntn 0 1 10
1554ctrpath s n
1555ptext ttmp
1556fd = fopen pb 0 get pn 0 577 384
1557if lt fd 0
1558exit 76
1559end
1560w2 = ulong 0
1561m = add add cn get ntn 0 2
1562while lt w2 m bound BIG
1563r = write int fd cb w2 sub m w2
1564if le r 0
1565exit 77
1566end
1567w2 = add w2 ulong r
1568end
1569s1 = fsync int fd
1570e2 = fclose int fd
1571if ne s1 0
1572exit 77
1573end
1574put pk 0 2
1575put pk 1 0
1576put pkn 0 2
1577pkrec
1578ctrpath s n
1579pkrec
1580rn = renames pk 0 get pkn 0 0
1581if ne rn 0
1582exit 77
1583end
1584ctrpath s n
1585syncdir
1586end
1587
1588fn verdict v view uchar * t trit to void
1589if eq get vq 0 1
1590ret
1591end
1592otext v
1593obyte 32
1594obyte add uchar open t 48
1595end
1596
1597fn bad v view uchar * to void
1598verdict v ltrit false false
1599end
1600
1601note A refused line that still decodes keeps its emergency request, and ltrit demotes it to 1.
1602fn bademd v view uchar * to void
1603verdict v ltrit false hasem
1604end
1605
1606note Unsigned with an ocb1 field: decode its digits (part 3) only to pass the request to ltrit.
1607fn baduns f ulong e ulong to void
1608if splitf f e 3 58
1609d = get fs 0
1610if eq decl d get fs 1 0
1611bademd vuns
1612ret
1613end
1614end
1615bad vuns
1616end
1617
1618fn dover to void
1619p = ulong 0
1620while lt p get fbn 0 bound BIG
1621e = lnend p
1622verx p e
1623obyte 10
1624p = add e 1
1625end
1626end
1627
1628note Last '|' field of fb[p..e) into fs; false when the line has no '|'.
1629fn lastf p ulong e ulong to bool
1630i = e
1631while gt i p bound BIG
1632if eq get fb sub i 1 124
1633put fs 0 i
1634put fs 1 e
1635ret true
1636end
1637i = sub i 1
1638end
1639ret false
1640end
1641
1642note Verify one inbox line; the check order is Lean `status`.
1643fn verl p ulong e ulong to void
1644if eq lastf p e false
1645bad vuns
1646ret
1647end
1648f = get fs 0
1649if or lt sub e f 5 eq splitf f e 0 58 false
1650bad vuns
1651ret
1652end
1653if eq feq tocb1 true
1654bad vleg
1655ret
1656end
1657if eq feq tocb4 false
1658bad vuns
1659ret
1660end
1661if eq splitf f e 4 58 false
1662baduns f e
1663ret
1664end
1665if eq get fs 0 get fs 1
1666baduns f e
1667ret
1668end
1669verl2 p e f
1670end
1671
1672note toeq: when a receiving bot is set (rbn > 0), field 2 (to) must equal rbot.
1673fn toeq to bool
1674if eq get rbn 0 0
1675ret true
1676end
1677a = get fa 4
1678b = get fa 5
1679if ne sub b a get rbn 0
1680ret false
1681end
1682for i ulong get rbn 0 bound BIG
1683if ne get fb add a i get rbot i
1684ret false
1685end
1686end
1687ret true
1688end
1689
1690fn verl2 p ulong e ulong f ulong to void
1691if eq splitf p e 1 124 false
1692bad vsnd
1693ret
1694end
1695s = get fs 0
1696sn = sub get fs 1 s
1697if eq slugok s sn false
1698bad vsnd
1699ret
1700end
1701if eq f p
1702bad vfmt
1703ret
1704end
1705if eq setfa p sub f 1 false
1706bad vfmt
1707ret
1708end
1709savets
1710if eq ntsok false
1711bad vfmt
1712ret
1713end
1714if eq toeq false
1715bad vto
1716ret
1717end
1718if splitf f e 5 58
1719bad vfmt
1720ret
1721end
1722if eq splitf f e 4 58 false
1723bad vfmt
1724ret
1725end
1726mac = get fs 0
1727if ne sub get fs 1 mac 64
1728bad vfmt
1729ret
1730end
1731for i ulong 64
1732if eq ishex get fb add mac i false
1733bad vfmt
1734ret
1735end
1736end
1737verl3 s sn f e mac
1738end
1739
1740fn verl3 s ulong sn ulong f ulong e ulong mac ulong to void
1741if eq splitf f e 1 58 false
1742bad vfmt
1743ret
1744end
1745hs = get fs 0
1746if ne sub get fs 1 hs 64
1747bad vfmt
1748ret
1749end
1750if eq splitf f e 2 58 false
1751bad vfmt
1752ret
1753end
1754c = get fs 0
1755cn = sub get fs 1 c
1756ctr = ctrval c cn
1757if eq ctr 0
1758bad vfmt
1759ret
1760end
1761for i ulong 64
1762if ne get fb add hs i get cs i
1763bad vcb
1764ret
1765end
1766end
1767if eq splitf f e 3 58 false
1768bad vfmt
1769ret
1770end
1771d = get fs 0
1772dn = sub get fs 1 d
1773put mo 0 mac
1774verl4 s sn c cn d dn
1775end
1776
1777note The computed MAC (hx) equals the line's MAC at fb[mo]; all 64 bytes are compared every time.
1778fn maceq to bool
1779mac = get mo 0
1780x = ulong 0
1781for i ulong 64
1782y = xor get hx i get fb add mac i
1783x = or x ulong y
1784end
1785ret eq x 0
1786end
1787
1788note At most 6 parameters per function (OMEGESE.md:492), so the MAC offset rides in global mo.
1789fn verl4 s ulong sn ulong c ulong cn ulong d ulong dn ulong to void
1790r = decl d add d dn
1791if eq r 1
1792bad vlen
1793ret
1794end
1795if eq r 2
1796bad vdig
1797ret
1798end
1799if eq r 3
1800bad vesc
1801ret
1802end
1803if eq known d false
1804bad vunk
1805ret
1806end
1807put vc0 0 c
1808put vcl 0 cn
1809put vd0 0 d
1810put vdl 0 dn
1811mkocb
1812hmac
1813if eq maceq false
1814bad vmac
1815ret
1816end
1817ctr = ctrval c cn
1818put vm 0 1
1819put vc 0 ctr
1820if le ctr rdctr s sn
1821bad vstl
1822ret
1823end
1824if eq tsge false
1825bad vstl
1826ret
1827end
1828if eq get vt 0 0
1829wrctr s sn c cn
1830else
1831vtput s sn ctr
1832end
1833put vf 0 1
1834if hasem
1835put ve 0 1
1836end
1837verdict vok ltrit true hasem
1838end