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 / aienux / crates / aienos-cortex / cortex.om

881 lines · 18285 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-cortex/cortex.om: Pure-Omega AIENOS Cortex Epistemic Store.
3note Converted from aienux crates/aienos-cortex/src/{status,record,verification,store,lib}.rs.
4note Preserves durable epistemic memory with verifiable SHA-256 provenance and rejects
5note ungrounded hallucinations, missing parents, and circular assertions.
6note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-cortex/cortex.om ...
7
8const CORTEX_DIRECT_OBSERVATION uchar 1
9const CORTEX_VERIFIED_FACT uchar 2
10const CORTEX_INFERENCE uchar 3
11const CORTEX_HYPOTHESIS uchar 4
12const CORTEX_CONTRADICTION uchar 5
13const CORTEX_OPERATOR_DECISION uchar 6
14
15const CORTEX_OK int 0
16const CORTEX_ERR_UNGROUNDED int -1
17const CORTEX_ERR_CIRCULAR int -2
18const CORTEX_ERR_NOT_FOUND int -3
19const CORTEX_ERR_CONTRADICTION int -4
20const CORTEX_ERR_CORRUPT_JOURNAL int -5
21const CORTEX_ERR_CHECKSUM int -6
22const CORTEX_ERR_CAPACITY int -7
23
24const CORTEX_MAX_RECS ulong 32
25const CORTEX_MAX_STMT ulong 96
26const CORTEX_MAX_PROV ulong 64
27const CORTEX_MAX_VERIF ulong 48
28const CORTEX_MAX_PARENTS ulong 4
29const CORTEX_ID_LEN ulong 16
30const CORTEX_HASH_LEN ulong 32
31
32note Epistemic store arrays (up to 32 records)
33global cx_cnt ulong 1
34global cx_id uchar 512
35global cx_status uchar 32
36global cx_stmt uchar 3072
37global cx_stmt_len ulong 32
38global cx_prov uchar 2048
39global cx_prov_len ulong 32
40global cx_ev_hash uchar 1024
41global cx_ts ulong 32
42global cx_conf f32 32
43global cx_verif uchar 1536
44global cx_verif_len ulong 32
45global cx_has_verif uchar 32
46global cx_pcnt ulong 32
47global cx_parents uchar 2048
48
49note Staging slot for record construction and grounding check
50global cx_st_id uchar 16
51global cx_st_status uchar 1
52global cx_st_stmt uchar 96
53global cx_st_stmt_len ulong 1
54global cx_st_prov uchar 64
55global cx_st_prov_len ulong 1
56global cx_st_payload uchar 256
57global cx_st_pay_len ulong 1
58global cx_st_ev_hash uchar 32
59global cx_st_ts ulong 1
60global cx_st_conf f32 1
61global cx_st_verif uchar 48
62global cx_st_verif_len ulong 1
63global cx_st_has_verif uchar 1
64global cx_st_pcnt ulong 1
65global cx_st_parents uchar 64
66
67note Scratch buffers for DFS grounding check and journal sort
68global cx_grounded uchar 32
69global cx_on_path uchar 32
70global cx_order ulong 32
71
72text cx_op_prefix "operator:"
73text cx_sentinel "cortex_sentinel"
74text cx_contra_prefix "Contradiction detected: "
75text cx_jmagic "CXJ1"
76
77fn cx_is_ground_truth st uchar to bool
78if eq st CORTEX_DIRECT_OBSERVATION
79ret true
80end
81if eq st CORTEX_VERIFIED_FACT
82ret true
83end
84if eq st CORTEX_OPERATOR_DECISION
85ret true
86end
87ret false
88end
89
90fn cx_requires_grounding st uchar to bool
91if eq st CORTEX_INFERENCE
92ret true
93end
94if eq st CORTEX_HYPOTHESIS
95ret true
96end
97ret false
98end
99
100fn cx_clamp01 v f32 to f32
101if lt v 0.0f32
102ret 0.0f32
103end
104if gt v 1.0f32
105ret 1.0f32
106end
107ret v
108end
109
110fn cx_put_u16_be buf edit uchar * off ulong v ulong to void
111put buf off uchar and shr v 8 0xff
112put buf add off 1 uchar and v 0xff
113end
114
115fn cx_put_u32_be buf edit uchar * off ulong v ulong to void
116put buf off uchar and shr v 24 0xff
117put buf add off 1 uchar and shr v 16 0xff
118put buf add off 2 uchar and shr v 8 0xff
119put buf add off 3 uchar and v 0xff
120end
121
122fn cx_put_u64_be buf edit uchar * off ulong v ulong to void
123for i ulong 8
124sh = mul sub 7 i 8
125put buf add off i uchar and shr v sh 0xff
126end
127end
128
129fn cx_get_u16_be buf view uchar * off ulong to ulong
130b0 = ulong get buf off
131b1 = ulong get buf add off 1
132ret or shl b0 8 b1
133end
134
135fn cx_get_u32_be buf view uchar * off ulong to ulong
136b0 = ulong get buf off
137b1 = ulong get buf add off 1
138b2 = ulong get buf add off 2
139b3 = ulong get buf add off 3
140ret or or shl b0 24 shl b1 16 or shl b2 8 b3
141end
142
143fn cx_get_u64_be buf view uchar * off ulong to ulong
144acc = ulong 0
145for i ulong 8
146b = ulong get buf add off i
147acc = or shl acc 8 b
148end
149ret acc
150end
151
152fn cx_init to void
153put cx_cnt 0 0
154put cx_st_pcnt 0 0
155put cx_st_has_verif 0 0
156put cx_st_verif_len 0 0
157put cx_st_pay_len 0 0
158end
159
160fn cx_id_eq_slot idx ulong id view uchar 16 to bool
161base = mul idx CORTEX_ID_LEN
162for i ulong 16
163if ne get cx_id add base i get id i
164ret false
165end
166end
167ret true
168end
169
170fn cx_find_by_id id view uchar 16 to long
171n = get cx_cnt 0
172for i ulong n bound 32
173if cx_id_eq_slot i id
174ret long i
175end
176end
177ret -1
178end
179
180fn cx_count_by_status st uchar to ulong
181n = get cx_cnt 0
182c = ulong 0
183for i ulong n bound 32
184if eq get cx_status i st
185c = add c 1
186end
187end
188ret c
189end
190
191fn cx_stage_reset to void
192put cx_st_status 0 0
193put cx_st_stmt_len 0 0
194put cx_st_prov_len 0 0
195put cx_st_pay_len 0 0
196put cx_st_ts 0 0
197put cx_st_conf 0 1.0f32
198put cx_st_verif_len 0 0
199put cx_st_has_verif 0 0
200put cx_st_pcnt 0 0
201end
202
203fn cx_stage_add_parent pid view uchar 16 to int
204pc = get cx_st_pcnt 0
205if ge pc CORTEX_MAX_PARENTS
206ret CORTEX_ERR_CAPACITY
207end
208base = mul pc CORTEX_ID_LEN
209for i ulong 16
210put cx_st_parents add base i get pid i
211end
212put cx_st_pcnt 0 add pc 1
213ret CORTEX_OK
214end
215
216fn cx_stage_set_verifier v view uchar * to int
217if gt len v CORTEX_MAX_VERIF
218ret CORTEX_ERR_CAPACITY
219end
220for i ulong len v bound 48
221put cx_st_verif i get v i
222end
223put cx_st_verif_len 0 len v
224put cx_st_has_verif 0 1
225ret CORTEX_OK
226end
227
228fn cx_stage_set_payload payload view uchar * to int
229if gt len payload 256
230ret CORTEX_ERR_CAPACITY
231end
232for i ulong len payload bound 256
233put cx_st_payload i get payload i
234end
235put cx_st_pay_len 0 len payload
236ret CORTEX_OK
237end
238
239fn cx_stage_set_conf conf f32 to void
240put cx_st_conf 0 cx_clamp01 conf
241end
242
243note Compute evidence_hash and id from staged globals
244fn cx_stage_compute_hashes to void
245hbuf = new uchar 512 0
246pos = ulong 0
247put hbuf 0 get cx_st_status 0
248pos = 1
249sl = get cx_st_stmt_len 0
250for i ulong sl bound 96
251put hbuf add pos i get cx_st_stmt i
252end
253pos = add pos sl
254pl = get cx_st_prov_len 0
255for i ulong pl bound 64
256put hbuf add pos i get cx_st_prov i
257end
258pos = add pos pl
259payl = get cx_st_pay_len 0
260for i ulong payl bound 256
261put hbuf add pos i get cx_st_payload i
262end
263pos = add pos payl
264pc = get cx_st_pcnt 0
265for i ulong mul pc 16 bound 64
266put hbuf add pos i get cx_st_parents i
267end
268pos = add pos mul pc 16
269
270dig = new uchar 32 0
271dl = sha2_digest 256 part hbuf 0 pos dig
272for i ulong 32
273put cx_st_ev_hash i get dig i
274end
275
276id_in = new uchar 40 0
277for i ulong 32
278put id_in i get dig i
279end
280cx_put_u64_be id_in 32 get cx_st_ts 0
281dl = sha2_digest 256 id_in dig
282for i ulong 16
283put cx_st_id i get dig i
284end
285end
286
287fn cx_stage_build st uchar stmt view uchar * prov view uchar * ts ulong to int
288if or gt len stmt CORTEX_MAX_STMT gt len prov CORTEX_MAX_PROV
289ret CORTEX_ERR_CAPACITY
290end
291pc = get cx_st_pcnt 0
292payl = get cx_st_pay_len 0
293need = add add add 1 len stmt len prov add payl mul pc 16
294if gt need 512
295ret CORTEX_ERR_CAPACITY
296end
297put cx_st_status 0 st
298put cx_st_stmt_len 0 len stmt
299for i ulong len stmt bound 96
300put cx_st_stmt i get stmt i
301end
302put cx_st_prov_len 0 len prov
303for i ulong len prov bound 64
304put cx_st_prov i get prov i
305end
306put cx_st_ts 0 ts
307cx_stage_compute_hashes
308ret CORTEX_OK
309end
310
311fn cx_copy_staged_to_slot slot ulong to void
312base = mul slot CORTEX_ID_LEN
313for i ulong 16
314put cx_id add base i get cx_st_id i
315end
316put cx_status slot get cx_st_status 0
317nlen = get cx_st_stmt_len 0
318put cx_stmt_len slot nlen
319base = mul slot CORTEX_MAX_STMT
320for i ulong nlen bound 96
321put cx_stmt add base i get cx_st_stmt i
322end
323nlen = get cx_st_prov_len 0
324put cx_prov_len slot nlen
325base = mul slot CORTEX_MAX_PROV
326for i ulong nlen bound 64
327put cx_prov add base i get cx_st_prov i
328end
329base = mul slot CORTEX_HASH_LEN
330for i ulong 32
331put cx_ev_hash add base i get cx_st_ev_hash i
332end
333put cx_ts slot get cx_st_ts 0
334put cx_conf slot get cx_st_conf 0
335nlen = get cx_st_verif_len 0
336put cx_verif_len slot nlen
337put cx_has_verif slot get cx_st_has_verif 0
338base = mul slot CORTEX_MAX_VERIF
339for i ulong nlen bound 48
340put cx_verif add base i get cx_st_verif i
341end
342nlen = get cx_st_pcnt 0
343put cx_pcnt slot nlen
344base = mul slot mul CORTEX_MAX_PARENTS CORTEX_ID_LEN
345for i ulong mul nlen 16 bound 64
346put cx_parents add base i get cx_st_parents i
347end
348end
349
350note Copy staged record into store slot idx (or overwrite if ID already exists)
351fn cx_insert_staged to long
352st_id = new uchar 16 0
353for i ulong 16
354put st_id i get cx_st_id i
355end
356existing = cx_find_by_id view st_id
357slot = ulong 0
358if ge existing 0
359slot = ulong existing
360else
361n = get cx_cnt 0
362if ge n CORTEX_MAX_RECS
363ret long CORTEX_ERR_CAPACITY
364end
365slot = n
366put cx_cnt 0 add n 1
367end
368cx_copy_staged_to_slot slot
369ret long slot
370end
371
372fn cx_check_node_slot idx ulong depth ulong to int
373if ge depth CORTEX_MAX_RECS
374ret CORTEX_ERR_CIRCULAR
375end
376if eq get cx_on_path idx 1
377ret CORTEX_ERR_CIRCULAR
378end
379if eq get cx_grounded idx 1
380ret CORTEX_OK
381end
382st = get cx_status idx
383if cx_is_ground_truth st
384put cx_grounded idx 1
385ret CORTEX_OK
386end
387pc = get cx_pcnt idx
388if eq pc 0
389ret CORTEX_ERR_UNGROUNDED
390end
391put cx_on_path idx 1
392par_base = mul idx mul CORTEX_MAX_PARENTS CORTEX_ID_LEN
393pid = new uchar 16 0
394for p ulong pc bound 4
395off = add par_base mul p 16
396for j ulong 16
397put pid j get cx_parents add off j
398end
399pidx = cx_find_by_id view pid
400if lt pidx 0
401put cx_on_path idx 0
402ret CORTEX_ERR_NOT_FOUND
403end
404rc = cx_check_node_slot ulong pidx add depth 1
405if ne rc CORTEX_OK
406put cx_on_path idx 0
407ret rc
408end
409end
410put cx_on_path idx 0
411put cx_grounded idx 1
412ret CORTEX_OK
413end
414
415fn cx_clear_dfs to void
416for i ulong 32
417put cx_grounded i 0
418put cx_on_path i 0
419end
420end
421
422fn cx_verify_slot idx ulong to int
423if ge idx get cx_cnt 0
424ret CORTEX_ERR_NOT_FOUND
425end
426st = get cx_status idx
427if cx_is_ground_truth st
428ret CORTEX_OK
429end
430if eq get cx_pcnt idx 0
431ret CORTEX_ERR_UNGROUNDED
432end
433cx_clear_dfs
434ret cx_check_node_slot idx 0
435end
436
437fn cx_verify_staged to int
438st = get cx_st_status 0
439if cx_is_ground_truth st
440ret CORTEX_OK
441end
442pc = get cx_st_pcnt 0
443if eq pc 0
444ret CORTEX_ERR_UNGROUNDED
445end
446cx_clear_dfs
447st_id = new uchar 16 0
448for i ulong 16
449put st_id i get cx_st_id i
450end
451existing = cx_find_by_id view st_id
452if ge existing 0
453put cx_on_path ulong existing 1
454end
455pid = new uchar 16 0
456for p ulong pc bound 4
457off = mul p 16
458for j ulong 16
459put pid j get cx_st_parents add off j
460end
461same = true
462for j ulong 16
463if ne get pid j get st_id j
464same = false
465end
466end
467if same
468ret CORTEX_ERR_CIRCULAR
469end
470pidx = cx_find_by_id view pid
471if lt pidx 0
472ret CORTEX_ERR_NOT_FOUND
473end
474rc = cx_check_node_slot ulong pidx 1
475if ne rc CORTEX_OK
476ret rc
477end
478end
479ret CORTEX_OK
480end
481
482note High-level APIs mirroring CortexStore methods (all <= 6 parameter slots)
483
484fn cx_record_observation stmt view uchar * prov view uchar * ts ulong id_out edit uchar 16 to int
485rc = cx_stage_build CORTEX_DIRECT_OBSERVATION stmt prov ts
486if ne rc CORTEX_OK
487ret rc
488end
489slot = cx_insert_staged
490if lt slot 0
491ret int slot
492end
493for i ulong 16
494put id_out i get cx_st_id i
495end
496ret CORTEX_OK
497end
498
499fn cx_record_verified_fact stmt view uchar * prov view uchar * ts ulong id_out edit uchar 16 to int
500rc = cx_stage_build CORTEX_VERIFIED_FACT stmt prov ts
501if ne rc CORTEX_OK
502ret rc
503end
504slot = cx_insert_staged
505if lt slot 0
506ret int slot
507end
508for i ulong 16
509put id_out i get cx_st_id i
510end
511ret CORTEX_OK
512end
513
514fn cx_record_operator_decision stmt view uchar * sig view uchar * ts ulong id_out edit uchar 16 to int
515cx_stage_reset
516plen = add len cx_op_prefix len sig
517if gt plen CORTEX_MAX_PROV
518ret CORTEX_ERR_CAPACITY
519end
520prov = new uchar 64 0
521for i ulong len cx_op_prefix bound 16
522put prov i get cx_op_prefix i
523end
524for i ulong len sig bound 48
525put prov add len cx_op_prefix i get sig i
526end
527rc = cx_stage_set_verifier sig
528if ne rc CORTEX_OK
529ret rc
530end
531rc = cx_stage_set_payload sig
532if ne rc CORTEX_OK
533ret rc
534end
535rc = cx_stage_build CORTEX_OPERATOR_DECISION stmt part prov 0 plen ts
536if ne rc CORTEX_OK
537ret rc
538end
539slot = cx_insert_staged
540if lt slot 0
541ret int slot
542end
543for i ulong 16
544put id_out i get cx_st_id i
545end
546ret CORTEX_OK
547end
548
549fn cx_record_inference stmt view uchar * model view uchar * ts ulong id_out edit uchar 16 to int
550put cx_st_pay_len 0 0
551rc = cx_stage_build CORTEX_INFERENCE stmt model ts
552if ne rc CORTEX_OK
553ret rc
554end
555rc = cx_verify_staged
556if ne rc CORTEX_OK
557ret rc
558end
559slot = cx_insert_staged
560if lt slot 0
561ret int slot
562end
563for i ulong 16
564put id_out i get cx_st_id i
565end
566ret CORTEX_OK
567end
568
569fn cx_record_hypothesis stmt view uchar * agent view uchar * ts ulong id_out edit uchar 16 to int
570put cx_st_pay_len 0 0
571put cx_st_conf 0 0.5f32
572rc = cx_stage_build CORTEX_HYPOTHESIS stmt agent ts
573if ne rc CORTEX_OK
574ret rc
575end
576rc = cx_verify_staged
577if ne rc CORTEX_OK
578ret rc
579end
580slot = cx_insert_staged
581if lt slot 0
582ret int slot
583end
584for i ulong 16
585put id_out i get cx_st_id i
586end
587ret CORTEX_OK
588end
589
590fn cx_flag_contradiction id_a view uchar 16 id_b view uchar 16 reason view uchar * ts ulong id_out edit uchar 16 to int
591if lt cx_find_by_id id_a 0
592ret CORTEX_ERR_NOT_FOUND
593end
594if lt cx_find_by_id id_b 0
595ret CORTEX_ERR_NOT_FOUND
596end
597slen = add len cx_contra_prefix len reason
598if gt slen CORTEX_MAX_STMT
599ret CORTEX_ERR_CAPACITY
600end
601sbuf = new uchar 96 0
602for i ulong len cx_contra_prefix bound 32
603put sbuf i get cx_contra_prefix i
604end
605for i ulong len reason bound 64
606put sbuf add len cx_contra_prefix i get reason i
607end
608cx_stage_reset
609rc = cx_stage_add_parent id_a
610if ne rc CORTEX_OK
611ret rc
612end
613rc = cx_stage_add_parent id_b
614if ne rc CORTEX_OK
615ret rc
616end
617rc = cx_stage_set_payload reason
618if ne rc CORTEX_OK
619ret rc
620end
621rc = cx_stage_build CORTEX_CONTRADICTION part sbuf 0 slen cx_sentinel ts
622if ne rc CORTEX_OK
623ret rc
624end
625slot = cx_insert_staged
626if lt slot 0
627ret int slot
628end
629for i ulong 16
630put id_out i get cx_st_id i
631end
632ret CORTEX_OK
633end
634
635fn cx_slot_gt a ulong b ulong to bool
636ta = get cx_ts a
637tb = get cx_ts b
638if gt ta tb
639ret true
640end
641if lt ta tb
642ret false
643end
644ba = mul a 16
645bb = mul b 16
646for i ulong 16
647va = get cx_id add ba i
648vb = get cx_id add bb i
649if gt va vb
650ret true
651end
652if lt va vb
653ret false
654end
655end
656ret false
657end
658
659fn cx_export_one_rec idx ulong dst edit uchar * pos ulong to long
660nlen = get cx_stmt_len idx
661nlen = add nlen get cx_prov_len idx
662nlen = add nlen get cx_verif_len idx
663nlen = add nlen mul get cx_pcnt idx 16
664if gt add add pos add 69 nlen 32 len dst
665ret long CORTEX_ERR_CAPACITY
666end
667p = pos
668base = mul idx 16
669for i ulong 16
670put dst add p i get cx_id add base i
671end
672p = add p 16
673put dst p get cx_status idx
674p = add p 1
675cx_put_u64_be dst p get cx_ts idx
676p = add p 8
677cx_put_u32_be dst p ulong bitcast uint get cx_conf idx
678p = add p 4
679base = mul idx 32
680for i ulong 32
681put dst add p i get cx_ev_hash add base i
682end
683p = add p 32
684put dst p get cx_has_verif idx
685p = add p 1
686nlen = get cx_stmt_len idx
687cx_put_u16_be dst p nlen
688p = add p 2
689base = mul idx CORTEX_MAX_STMT
690for i ulong nlen bound 96
691put dst add p i get cx_stmt add base i
692end
693p = add p nlen
694nlen = get cx_prov_len idx
695cx_put_u16_be dst p nlen
696p = add p 2
697base = mul idx CORTEX_MAX_PROV
698for i ulong nlen bound 64
699put dst add p i get cx_prov add base i
700end
701p = add p nlen
702nlen = get cx_verif_len idx
703cx_put_u16_be dst p nlen
704p = add p 2
705base = mul idx CORTEX_MAX_VERIF
706for i ulong nlen bound 48
707put dst add p i get cx_verif add base i
708end
709p = add p nlen
710nlen = get cx_pcnt idx
711put dst p uchar nlen
712p = add p 1
713base = mul idx mul CORTEX_MAX_PARENTS CORTEX_ID_LEN
714for i ulong mul nlen 16 bound 64
715put dst add p i get cx_parents add base i
716end
717ret long add p mul nlen 16
718end
719
720fn cx_export_journal dst edit uchar * to long
721n = get cx_cnt 0
722for i ulong n bound 32
723put cx_order i i
724end
725if gt n 1
726for i ulong sub n 1 bound 32
727key_idx = get cx_order add i 1
728j = add i 1
729while gt j 0 bound 32
730prev = get cx_order sub j 1
731if cx_slot_gt prev key_idx
732put cx_order j prev
733j = sub j 1
734else
735break
736end
737end
738put cx_order j key_idx
739end
740end
741
742if lt len dst 38
743ret long CORTEX_ERR_CAPACITY
744end
745for i ulong 4
746put dst i get cx_jmagic i
747end
748cx_put_u16_be dst 4 n
749pos = ulong 6
750
751for k ulong n bound 32
752idx = get cx_order k
753nxt = cx_export_one_rec idx dst pos
754if lt nxt 0
755ret nxt
756end
757pos = ulong nxt
758end
759
760jsum = new uchar 32 0
761dl = sha2_digest 256 part dst 0 pos jsum
762for i ulong 32
763put dst add pos i get jsum i
764end
765ret long add pos 32
766end
767
768fn cx_import_one_rec src view uchar * pos ulong body_end ulong to long
769if gt add pos 69 body_end
770ret long CORTEX_ERR_CORRUPT_JOURNAL
771end
772cx_stage_reset
773p = pos
774for i ulong 16
775put cx_st_id i get src add p i
776end
777p = add p 16
778put cx_st_status 0 get src p
779p = add p 1
780put cx_st_ts 0 cx_get_u64_be src p
781p = add p 8
782put cx_st_conf 0 bitcast f32 uint cx_get_u32_be src p
783p = add p 4
784for i ulong 32
785put cx_st_ev_hash i get src add p i
786end
787p = add p 32
788put cx_st_has_verif 0 get src p
789p = add p 1
790nlen = cx_get_u16_be src p
791p = add p 2
792if or gt nlen CORTEX_MAX_STMT gt add p nlen body_end
793ret long CORTEX_ERR_CORRUPT_JOURNAL
794end
795put cx_st_stmt_len 0 nlen
796for i ulong nlen bound 96
797put cx_st_stmt i get src add p i
798end
799p = add p nlen
800if gt add p 2 body_end
801ret long CORTEX_ERR_CORRUPT_JOURNAL
802end
803nlen = cx_get_u16_be src p
804p = add p 2
805if or gt nlen CORTEX_MAX_PROV gt add p nlen body_end
806ret long CORTEX_ERR_CORRUPT_JOURNAL
807end
808put cx_st_prov_len 0 nlen
809for i ulong nlen bound 64
810put cx_st_prov i get src add p i
811end
812p = add p nlen
813if gt add p 2 body_end
814ret long CORTEX_ERR_CORRUPT_JOURNAL
815end
816nlen = cx_get_u16_be src p
817p = add p 2
818if or gt nlen CORTEX_MAX_VERIF gt add p nlen body_end
819ret long CORTEX_ERR_CORRUPT_JOURNAL
820end
821put cx_st_verif_len 0 nlen
822for i ulong nlen bound 48
823put cx_st_verif i get src add p i
824end
825p = add p nlen
826if gt add p 1 body_end
827ret long CORTEX_ERR_CORRUPT_JOURNAL
828end
829nlen = ulong get src p
830p = add p 1
831if or gt nlen CORTEX_MAX_PARENTS gt add p mul nlen 16 body_end
832ret long CORTEX_ERR_CORRUPT_JOURNAL
833end
834put cx_st_pcnt 0 nlen
835for i ulong mul nlen 16 bound 64
836put cx_st_parents i get src add p i
837end
838p = add p mul nlen 16
839if lt cx_insert_staged 0
840ret long CORTEX_ERR_CAPACITY
841end
842ret long p
843end
844
845fn cx_import_journal src view uchar * slen_in ulong to long
846if lt slen_in 38
847ret long CORTEX_ERR_CORRUPT_JOURNAL
848end
849if gt slen_in len src
850ret long CORTEX_ERR_CORRUPT_JOURNAL
851end
852for i ulong 4
853if ne get src i get cx_jmagic i
854ret long CORTEX_ERR_CORRUPT_JOURNAL
855end
856end
857body_end = sub slen_in 32
858computed = new uchar 32 0
859dl = sha2_digest 256 part src 0 body_end computed
860for i ulong 32
861if ne get computed i get src add body_end i
862ret long CORTEX_ERR_CHECKSUM
863end
864end
865cnt = cx_get_u16_be src 4
866if gt cnt CORTEX_MAX_RECS
867ret long CORTEX_ERR_CAPACITY
868end
869pos = ulong 6
870for k ulong cnt bound 32
871nxt = cx_import_one_rec src pos body_end
872if lt nxt 0
873ret nxt
874end
875pos = ulong nxt
876end
877if ne pos body_end
878ret long CORTEX_ERR_CORRUPT_JOURNAL
879end
880ret long cnt
881end