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

564 lines · 12784 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/crates/aienos-anchor/anchor.om: Pure-Omega AIENOS Anchor log.
3note Converted from aienux crates/aienos-anchor/src/lib.rs.
4note Append-only, hash-chained, authenticated log of checked facts.
5note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-anchor/anchor.om ...
6
7const ANCHOR_MAX_KEY ulong 64
8const ANCHOR_MAX_VAL ulong 128
9const ANCHOR_MAX_SRC ulong 64
10const ANCHOR_MAX_ENTRIES ulong 16
11const ANCHOR_MAX_TRUSTED ulong 4
12const ANCHOR_MAX_WIRE ulong 512
13
14const ANCHOR_KIND_ADMIT uchar 1
15const ANCHOR_KIND_RETRACT uchar 2
16
17const ANCHOR_OK int 0
18const ANCHOR_ERR_MALFORMED int -1
19const ANCHOR_ERR_TOO_LARGE int -2
20const ANCHOR_ERR_OUT_OF_ORDER int -3
21const ANCHOR_ERR_BROKEN_CHAIN int -4
22const ANCHOR_ERR_UNTRUSTED int -5
23const ANCHOR_ERR_BAD_SIG int -6
24const ANCHOR_ERR_RETRACT_UNKNOWN int -7
25const ANCHOR_ERR_RETRACT_VALUE int -8
26const ANCHOR_ERR_CAPACITY int -9
27
28const ANCHOR_ANS_UNKNOWN int 0
29const ANCHOR_ANS_KNOWN int 1
30const ANCHOR_ANS_RETRACTED int 2
31
32text anchor_domain "AIENOS-ANCHOR-v0"
33
34global anchor_trusted uchar 128
35global anchor_trusted_cnt ulong 1
36global anchor_head uchar 32
37global anchor_cnt ulong 1
38
39global anchor_e_seq ulong 16
40global anchor_e_prev uchar 512
41global anchor_e_kind uchar 16
42global anchor_e_ts ulong 16
43global anchor_e_klen ulong 16
44global anchor_e_key uchar 1024
45global anchor_e_vlen ulong 16
46global anchor_e_val uchar 2048
47global anchor_e_slen ulong 16
48global anchor_e_src uchar 1024
49global anchor_e_signer uchar 512
50global anchor_e_sig uchar 1024
51
52note Staging slot for candidate entry construction and decoding
53global anchor_st_seq ulong 1
54global anchor_st_prev uchar 32
55global anchor_st_kind uchar 1
56global anchor_st_ts ulong 1
57global anchor_st_klen ulong 1
58global anchor_st_key uchar 64
59global anchor_st_vlen ulong 1
60global anchor_st_val uchar 128
61global anchor_st_slen ulong 1
62global anchor_st_src uchar 64
63global anchor_st_signer uchar 32
64global anchor_st_sig uchar 64
65
66note Query result metadata: 0=ans_kind, 1=entry_idx, 2=seq
67global anchor_q_res ulong 3
68
69fn anchor_put_u16_le buf edit uchar * off ulong v ulong to void
70put buf off uchar and v 0xff
71put buf add off 1 uchar and shr v 8 0xff
72end
73
74fn anchor_put_u32_le buf edit uchar * off ulong v ulong to void
75put buf off uchar and v 0xff
76put buf add off 1 uchar and shr v 8 0xff
77put buf add off 2 uchar and shr v 16 0xff
78put buf add off 3 uchar and shr v 24 0xff
79end
80
81fn anchor_put_u64_le buf edit uchar * off ulong v ulong to void
82for i ulong 8
83sh = mul i 8
84put buf add off i uchar and shr v sh 0xff
85end
86end
87
88fn anchor_get_u16_le buf view uchar * off ulong to ulong
89b0 = ulong get buf off
90b1 = ulong get buf add off 1
91ret or b0 shl b1 8
92end
93
94fn anchor_get_u32_le buf view uchar * off ulong to ulong
95b0 = ulong get buf off
96b1 = ulong get buf add off 1
97b2 = ulong get buf add off 2
98b3 = ulong get buf add off 3
99ret or or b0 shl b1 8 or shl b2 16 shl b3 24
100end
101
102fn anchor_get_u64_le buf view uchar * off ulong to ulong
103acc = ulong 0
104for i ulong 8
105b = ulong get buf add off i
106acc = or acc shl b mul i 8
107end
108ret acc
109end
110
111fn anchor_init to void
112put anchor_trusted_cnt 0 0
113put anchor_cnt 0 0
114for i ulong 32
115put anchor_head i 0
116end
117end
118
119fn anchor_add_trusted key view uchar * to int
120if ne len key 32
121ret ANCHOR_ERR_MALFORMED
122end
123cnt = get anchor_trusted_cnt 0
124if ge cnt ANCHOR_MAX_TRUSTED
125ret ANCHOR_ERR_CAPACITY
126end
127base = mul cnt 32
128for i ulong 32
129put anchor_trusted add base i get key i
130end
131put anchor_trusted_cnt 0 add cnt 1
132ret ANCHOR_OK
133end
134
135fn anchor_is_trusted_staged to bool
136cnt = get anchor_trusted_cnt 0
137for t ulong cnt bound 4
138base = mul t 32
139same = true
140for i ulong 32
141if ne get anchor_trusted add base i get anchor_st_signer i
142same = false
143end
144end
145if same
146ret true
147end
148end
149ret false
150end
151
152fn anchor_stage_hdr seq ulong kind uchar ts ulong to void
153put anchor_st_seq 0 seq
154for i ulong 32
155put anchor_st_prev i get anchor_head i
156end
157put anchor_st_kind 0 kind
158put anchor_st_ts 0 ts
159put anchor_st_slen 0 0
160end
161
162fn anchor_stage_kv k view uchar * v view uchar * to int
163if or eq len k 0 gt len k ANCHOR_MAX_KEY
164ret ANCHOR_ERR_TOO_LARGE
165end
166if gt len v ANCHOR_MAX_VAL
167ret ANCHOR_ERR_TOO_LARGE
168end
169put anchor_st_klen 0 len k
170for i ulong len k bound 64
171put anchor_st_key i get k i
172end
173put anchor_st_vlen 0 len v
174for i ulong len v bound 128
175put anchor_st_val i get v i
176end
177ret ANCHOR_OK
178end
179
180fn anchor_stage_source src view uchar * to int
181if gt len src ANCHOR_MAX_SRC
182ret ANCHOR_ERR_TOO_LARGE
183end
184put anchor_st_slen 0 len src
185for i ulong len src bound 64
186put anchor_st_src i get src i
187end
188ret ANCHOR_OK
189end
190
191fn anchor_stage_prev prev view uchar * to void
192for i ulong 32
193put anchor_st_prev i get prev i
194end
195end
196
197fn anchor_stage_signer signer view uchar * to void
198for i ulong 32
199put anchor_st_signer i get signer i
200end
201end
202
203fn anchor_encode_staged_body buf edit uchar * to ulong
204for i ulong 16
205put buf i get anchor_domain i
206end
207anchor_put_u64_le buf 16 get anchor_st_seq 0
208for i ulong 32
209put buf add 24 i get anchor_st_prev i
210end
211put buf 56 get anchor_st_kind 0
212anchor_put_u64_le buf 57 get anchor_st_ts 0
213pos = ulong 65
214kl = get anchor_st_klen 0
215anchor_put_u16_le buf pos kl
216pos = add pos 2
217for i ulong kl bound 64
218put buf add pos i get anchor_st_key i
219end
220pos = add pos kl
221vl = get anchor_st_vlen 0
222anchor_put_u32_le buf pos vl
223pos = add pos 4
224for i ulong vl bound 128
225put buf add pos i get anchor_st_val i
226end
227pos = add pos vl
228sl = get anchor_st_slen 0
229anchor_put_u16_le buf pos sl
230pos = add pos 2
231for i ulong sl bound 64
232put buf add pos i get anchor_st_src i
233end
234pos = add pos sl
235ret pos
236end
237
238fn anchor_compute_sig_staged sig_out edit uchar * to void
239body = new uchar 512 0
240blen = anchor_encode_staged_body body
241ctx = new ulong 40 0
242sha256_init ctx
243skey = new uchar 32 0
244for i ulong 32
245put skey i get anchor_st_signer i
246end
247sha256_update ctx skey
248sha256_update ctx part body 0 blen
249d1 = new uchar 32 0
250sha256_final ctx d1
251for i ulong 32
252put sig_out i get d1 i
253end
254sha256_init ctx
255sha256_update ctx d1
256sha256_update ctx anchor_domain
257d2 = new uchar 32 0
258sha256_final ctx d2
259for i ulong 32
260put sig_out add 32 i get d2 i
261end
262end
263
264fn anchor_sign_staged signer view uchar * to void
265anchor_stage_signer signer
266sig = new uchar 64 0
267anchor_compute_sig_staged sig
268for i ulong 64
269put anchor_st_sig i get sig i
270end
271end
272
273fn anchor_query key view uchar * to int
274cnt = get anchor_cnt 0
275if eq cnt 0
276put anchor_q_res 0 ulong ANCHOR_ANS_UNKNOWN
277put anchor_q_res 1 0
278put anchor_q_res 2 0
279ret ANCHOR_ANS_UNKNOWN
280end
281idx = cnt
282while gt idx 0 bound 16
283idx = sub idx 1
284kl = get anchor_e_klen idx
285if eq kl len key
286base = mul idx ANCHOR_MAX_KEY
287same = true
288for i ulong kl bound 64
289if ne get anchor_e_key add base i get key i
290same = false
291end
292end
293if same
294knd = get anchor_e_kind idx
295put anchor_q_res 1 idx
296put anchor_q_res 2 get anchor_e_seq idx
297if eq knd ANCHOR_KIND_ADMIT
298put anchor_q_res 0 ulong ANCHOR_ANS_KNOWN
299ret ANCHOR_ANS_KNOWN
300end
301put anchor_q_res 0 ulong ANCHOR_ANS_RETRACTED
302ret ANCHOR_ANS_RETRACTED
303end
304end
305end
306put anchor_q_res 0 ulong ANCHOR_ANS_UNKNOWN
307put anchor_q_res 1 0
308put anchor_q_res 2 0
309ret ANCHOR_ANS_UNKNOWN
310end
311
312fn anchor_staged_key_is_known to bool
313kl = get anchor_st_klen 0
314kbuf = new uchar 64 0
315for i ulong kl bound 64
316put kbuf i get anchor_st_key i
317end
318ans = anchor_query part kbuf 0 kl
319ret eq ans ANCHOR_ANS_KNOWN
320end
321
322fn anchor_commit_staged cnt ulong to void
323put anchor_e_seq cnt get anchor_st_seq 0
324pbase = mul cnt 32
325for i ulong 32
326put anchor_e_prev add pbase i get anchor_st_prev i
327end
328put anchor_e_kind cnt get anchor_st_kind 0
329put anchor_e_ts cnt get anchor_st_ts 0
330kl = get anchor_st_klen 0
331put anchor_e_klen cnt kl
332kbase = mul cnt ANCHOR_MAX_KEY
333for i ulong kl bound 64
334put anchor_e_key add kbase i get anchor_st_key i
335end
336vl = get anchor_st_vlen 0
337put anchor_e_vlen cnt vl
338vbase = mul cnt ANCHOR_MAX_VAL
339for i ulong vl bound 128
340put anchor_e_val add vbase i get anchor_st_val i
341end
342sl = get anchor_st_slen 0
343put anchor_e_slen cnt sl
344sbase = mul cnt ANCHOR_MAX_SRC
345for i ulong sl bound 64
346put anchor_e_src add sbase i get anchor_st_src i
347end
348for i ulong 32
349put anchor_e_signer add pbase i get anchor_st_signer i
350end
351sigbase = mul cnt 64
352for i ulong 64
353put anchor_e_sig add sigbase i get anchor_st_sig i
354end
355put anchor_cnt 0 add cnt 1
356end
357
358fn anchor_update_head_staged to int
359wire = new uchar 512 0
360blen = anchor_encode_staged_body wire
361for i ulong 32
362put wire add blen i get anchor_st_signer i
363end
364for i ulong 64
365put wire add add blen 32 i get anchor_st_sig i
366end
367tlen = add blen 96
368dig = new uchar 32 0
369dlen = sha2_digest 256 part wire 0 tlen dig
370if ne dlen 32
371ret ANCHOR_ERR_MALFORMED
372end
373for i ulong 32
374put anchor_head i get dig i
375end
376ret ANCHOR_OK
377end
378
379fn anchor_append_staged to int
380kl = get anchor_st_klen 0
381vl = get anchor_st_vlen 0
382sl = get anchor_st_slen 0
383if or eq kl 0 gt kl ANCHOR_MAX_KEY
384ret ANCHOR_ERR_TOO_LARGE
385end
386if or gt vl ANCHOR_MAX_VAL gt sl ANCHOR_MAX_SRC
387ret ANCHOR_ERR_TOO_LARGE
388end
389knd = get anchor_st_kind 0
390if and ne knd ANCHOR_KIND_ADMIT ne knd ANCHOR_KIND_RETRACT
391ret ANCHOR_ERR_MALFORMED
392end
393if and eq knd ANCHOR_KIND_RETRACT gt vl 0
394ret ANCHOR_ERR_RETRACT_VALUE
395end
396cnt = get anchor_cnt 0
397if ge cnt ANCHOR_MAX_ENTRIES
398ret ANCHOR_ERR_CAPACITY
399end
400if ne get anchor_st_seq 0 cnt
401ret ANCHOR_ERR_OUT_OF_ORDER
402end
403for i ulong 32
404if ne get anchor_st_prev i get anchor_head i
405ret ANCHOR_ERR_BROKEN_CHAIN
406end
407end
408if not anchor_is_trusted_staged
409ret ANCHOR_ERR_UNTRUSTED
410end
411exp_sig = new uchar 64 0
412anchor_compute_sig_staged exp_sig
413for i ulong 64
414if ne get anchor_st_sig i get exp_sig i
415ret ANCHOR_ERR_BAD_SIG
416end
417end
418if eq knd ANCHOR_KIND_RETRACT
419if not anchor_staged_key_is_known
420ret ANCHOR_ERR_RETRACT_UNKNOWN
421end
422end
423rc = anchor_update_head_staged
424if ne rc ANCHOR_OK
425ret rc
426end
427anchor_commit_staged cnt
428ret ANCHOR_OK
429end
430
431fn anchor_append_auth idx ulong buf edit uchar * blen ulong to ulong
432pbase = mul idx 32
433for i ulong 32
434put buf add blen i get anchor_e_signer add pbase i
435end
436sigoff = add blen 32
437sigbase = mul idx 64
438for i ulong 64
439put buf add sigoff i get anchor_e_sig add sigbase i
440end
441ret add blen 96
442end
443
444fn anchor_export_entry idx ulong buf edit uchar * to ulong
445put anchor_st_seq 0 get anchor_e_seq idx
446pbase = mul idx 32
447for i ulong 32
448put anchor_st_prev i get anchor_e_prev add pbase i
449end
450put anchor_st_kind 0 get anchor_e_kind idx
451put anchor_st_ts 0 get anchor_e_ts idx
452kl = get anchor_e_klen idx
453put anchor_st_klen 0 kl
454kbase = mul idx ANCHOR_MAX_KEY
455for i ulong kl bound 64
456put anchor_st_key i get anchor_e_key add kbase i
457end
458vl = get anchor_e_vlen idx
459put anchor_st_vlen 0 vl
460vbase = mul idx ANCHOR_MAX_VAL
461for i ulong vl bound 128
462put anchor_st_val i get anchor_e_val add vbase i
463end
464sl = get anchor_e_slen idx
465put anchor_st_slen 0 sl
466sbase = mul idx ANCHOR_MAX_SRC
467for i ulong sl bound 64
468put anchor_st_src i get anchor_e_src add sbase i
469end
470blen = anchor_encode_staged_body buf
471ret anchor_append_auth idx buf blen
472end
473
474fn anchor_decode_to_staged buf view uchar * off ulong total ulong to long
475if lt sub total off 169
476ret long ANCHOR_ERR_MALFORMED
477end
478for i ulong 16
479if ne get buf add off i get anchor_domain i
480ret long ANCHOR_ERR_MALFORMED
481end
482end
483put anchor_st_seq 0 anchor_get_u64_le buf add off 16
484for i ulong 32
485put anchor_st_prev i get buf add add off 24 i
486end
487knd = get buf add off 56
488if and ne knd ANCHOR_KIND_ADMIT ne knd ANCHOR_KIND_RETRACT
489ret long ANCHOR_ERR_MALFORMED
490end
491put anchor_st_kind 0 knd
492put anchor_st_ts 0 anchor_get_u64_le buf add off 57
493pos = add off 65
494kl = anchor_get_u16_le buf pos
495pos = add pos 2
496if gt kl ANCHOR_MAX_KEY
497ret long ANCHOR_ERR_TOO_LARGE
498end
499if gt add pos kl total
500ret long ANCHOR_ERR_MALFORMED
501end
502put anchor_st_klen 0 kl
503for i ulong kl bound 64
504put anchor_st_key i get buf add pos i
505end
506pos = add pos kl
507if gt add pos 4 total
508ret long ANCHOR_ERR_MALFORMED
509end
510vl = anchor_get_u32_le buf pos
511pos = add pos 4
512if gt vl ANCHOR_MAX_VAL
513ret long ANCHOR_ERR_TOO_LARGE
514end
515if gt add pos vl total
516ret long ANCHOR_ERR_MALFORMED
517end
518put anchor_st_vlen 0 vl
519for i ulong vl bound 128
520put anchor_st_val i get buf add pos i
521end
522pos = add pos vl
523if gt add pos 2 total
524ret long ANCHOR_ERR_MALFORMED
525end
526sl = anchor_get_u16_le buf pos
527pos = add pos 2
528if gt sl ANCHOR_MAX_SRC
529ret long ANCHOR_ERR_TOO_LARGE
530end
531if gt add add pos sl 96 total
532ret long ANCHOR_ERR_MALFORMED
533end
534put anchor_st_slen 0 sl
535for i ulong sl bound 64
536put anchor_st_src i get buf add pos i
537end
538pos = add pos sl
539for i ulong 32
540put anchor_st_signer i get buf add pos i
541end
542pos = add pos 32
543for i ulong 64
544put anchor_st_sig i get buf add pos i
545end
546pos = add pos 64
547ret long sub pos off
548end
549
550fn anchor_replay buf view uchar * total ulong to int
551pos = ulong 0
552while lt pos total bound 16
553used = anchor_decode_to_staged buf pos total
554if lt used 0
555ret int used
556end
557rc = anchor_append_staged
558if ne rc ANCHOR_OK
559ret rc
560end
561pos = add pos ulong used
562end
563ret ANCHOR_OK
564end