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 / store / store_v1.om

625 lines · 15169 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note aienux/store/store_v1.om: Pure-Omega System Store v1 format primitives (ADR 0015).
3note Faithful OM-2 library (no om2 line, no fn main) converted from upstream aienux
4note native/store/store_v1.{h,c} (byte-identical to crates/aienos-kernel/src/store/v1.rs).
5note Compose with: si/directives/om2.om aienux/store/store_v1.om ... om/tls/sha2.om
6
7const SV1_UNIT ulong 4096
8const SV1_MAX_CATALOG_ENTRIES ulong 4096
9const SV1_CATALOG_HEADER ulong 16
10const SV1_CATALOG_ENTRY ulong 64
11const SV1_MAX_OBJECT_BYTES ulong 67108864
12const SV1_MAX_OBJECT_UNITS ulong 16384
13const SV1_MAX_TX_OBJECTS ulong 256
14const SV1_MAX_TX_UNITS ulong 32768
15const SV1_MAX_REGION_UNITS ulong 4294967296
16const SV1_KIND_CATALOG ushort 1
17const SV1_KIND_COMMIT ushort 2
18const SV1_VERSION ushort 1
19const SV1_COMMIT_BYTES ulong 200
20const SV1_SB_CRC_OFFSET ulong 168
21const SV1_SB_RESERVED_OFFSET ulong 172
22const SV1_MAX_CATALOG_UNITS ulong 65
23const SV1_U64_MAX ulong 0xffffffffffffffff
24
25note FormatError codes (matching store_v1.h / v1.rs)
26const SV1_OK int 0
27const SV1_E_INVALID_LENGTH int -1
28const SV1_E_INVALID_OBJECT int -2
29const SV1_E_OBJECT_TOO_LARGE int -3
30const SV1_E_LENGTH_OVERFLOW int -4
31const SV1_E_MALFORMED_DESCRIPTOR int -5
32const SV1_E_OUT_OF_BOUNDS int -6
33const SV1_E_OVERLAP int -7
34const SV1_E_CATALOG_TOO_LARGE int -8
35const SV1_E_CATALOG_ORDER int -9
36const SV1_E_DUPLICATE_OBJECT int -10
37const SV1_E_BAD_CATALOG_MAGIC int -11
38const SV1_E_UNSUPPORTED_VERSION int -12
39const SV1_E_MALFORMED_COMMIT int -13
40const SV1_E_MALFORMED_SUPERBLOCK int -14
41const SV1_E_BAD_SUPERBLOCK_MAGIC int -15
42const SV1_E_BAD_SUPERBLOCK_CRC int -16
43const SV1_E_NONZERO_RESERVED int -17
44const SV1_E_WRONG_SUPERBLOCK_SLOT int -18
45const SV1_E_UNSUPPORTED_FEATURES int -19
46const SV1_E_INTEGRITY int -20
47const SV1_E_INVALID_GENERATION int -21
48const SV1_E_CONFLICTING_ROOTS int -22
49const SV1_E_INCONSISTENT_HISTORY int -23
50const SV1_E_ARG int -24
51
52text sv1_obj_dom "AIENOS-STORE-OBJECT-V1"
53text sv1_cat_magic "AIENCAT1"
54text sv1_cmt_magic "AIENCMT1"
55text sv1_sb_magic "AIENSTR1"
56
57note ---- Byte and comparison helpers ----
58
59fn sv1_put16 buf edit uchar * off ulong v ushort to void
60put buf off uchar and v 0xff
61put buf add off 1 uchar and shr v 8 0xff
62end
63
64fn sv1_put32 buf edit uchar * off ulong v uint to void
65put buf off uchar and v 0xff
66put buf add off 1 uchar and shr v 8 0xff
67put buf add off 2 uchar and shr v 16 0xff
68put buf add off 3 uchar and shr v 24 0xff
69end
70
71fn sv1_put64 buf edit uchar * off ulong v ulong to void
72for i ulong 8 unroll
73put buf add off i uchar and shr v mul i 8 0xff
74end
75end
76
77fn sv1_get16 buf view uchar * off ulong to ushort
78b0 = ushort get buf off
79b1 = ushort get buf add off 1
80ret or b0 shl b1 8
81end
82
83fn sv1_get32 buf view uchar * off ulong to uint
84b0 = uint get buf off
85b1 = uint get buf add off 1
86b2 = uint get buf add off 2
87b3 = uint get buf add off 3
88ret or or b0 shl b1 8 or shl b2 16 shl b3 24
89end
90
91fn sv1_get64 buf view uchar * off ulong to ulong
92v = ulong 0
93for i ulong 8 unroll
94b = ulong get buf add off i
95v = or v shl b mul i 8
96end
97ret v
98end
99
100fn sv1_equal a view uchar * b view uchar * to bool
101if ne len a len b
102ret false
103end
104cnt = len a
105i = ulong 0
106while lt i cnt bound 266240
107if ne get a i get b i
108ret false
109end
110i = add i 1
111end
112ret true
113end
114
115fn sv1_all_zero buf view uchar * off ulong cnt ulong to bool
116i = ulong 0
117while lt i cnt bound 266240
118if ne get buf add off i 0
119ret false
120end
121i = add i 1
122end
123ret true
124end
125
126fn sv1_cmp_id a view uchar * aoff ulong b view uchar * boff ulong to int
127for i ulong 32
128va = get a add aoff i
129vb = get b add boff i
130if lt va vb
131ret -1
132end
133if gt va vb
134ret 1
135end
136end
137ret 0
138end
139
140note ---- CRC-32C (Castagnoli, reflected 0x82f63b78) ----
141
142fn sv1_crc32c buf view uchar * off ulong cnt ulong to uint
143crc = uint 0xffffffff
144i = ulong 0
145while lt i cnt bound 65536
146crc = xor crc uint get buf add off i
147for k ulong 8 unroll
148bit = and crc 1
149mask = sub uint 0 bit
150crc = xor shr crc 1 and 0x82f63b78 mask
151end
152i = add i 1
153end
154ret xor crc 0xffffffff
155end
156
157fn sv1_sb_crc buf view uchar * off ulong to uint
158crc = uint 0xffffffff
159for i ulong 4096
160b = uint 0
161if or lt i SV1_SB_CRC_OFFSET ge i add SV1_SB_CRC_OFFSET 4
162b = uint get buf add off i
163end
164crc = xor crc b
165for k ulong 8 unroll
166bit = and crc 1
167mask = sub uint 0 bit
168crc = xor shr crc 1 and 0x82f63b78 mask
169end
170end
171ret xor crc 0xffffffff
172end
173
174fn sv1_superblock_crc_ok buf view uchar * off ulong to bool
175ret eq sv1_sb_crc buf off sv1_get32 buf add off SV1_SB_CRC_OFFSET
176end
177
178note ---- ObjectId and unit count ----
179
180fn sv1_unit_count byte_len ulong dst edit uint 1 to int
181if eq byte_len 0
182ret SV1_E_INVALID_OBJECT
183end
184if gt byte_len SV1_MAX_OBJECT_BYTES
185ret SV1_E_OBJECT_TOO_LARGE
186end
187u = div add byte_len sub SV1_UNIT 1 SV1_UNIT
188if gt u SV1_MAX_OBJECT_UNITS
189ret SV1_E_OBJECT_TOO_LARGE
190end
191put dst 0 uint u
192ret SV1_OK
193end
194
195fn sv1_object_id_begin ctx edit ulong 40 kind ushort ver ushort byte_len ulong to int
196if or eq kind 0 or eq ver 0 eq byte_len 0
197ret SV1_E_INVALID_OBJECT
198end
199if gt byte_len SV1_MAX_OBJECT_BYTES
200ret SV1_E_OBJECT_TOO_LARGE
201end
202z1 = new uchar 1 0
203d = new uchar 12 0
204sv1_put16 d 0 kind
205sv1_put16 d 2 ver
206sv1_put64 d 4 byte_len
207sha256_init ctx
208sha256_update ctx sv1_obj_dom
209sha256_update ctx z1
210sha256_update ctx d
211ret SV1_OK
212end
213
214fn sv1_object_id kind ushort ver ushort data view uchar * dst edit uchar 32 to int
215ctx = new ulong 40 0
216e = sv1_object_id_begin ctx kind ver len data
217if ne e SV1_OK
218ret e
219end
220sha256_update ctx data
221sha256_final ctx dst
222ret SV1_OK
223end
224
225fn sv1_validate_extent id view uchar 32 kind ushort ver ushort byte_len ulong ext view uchar * to int
226u = new uint 1 0
227e = sv1_unit_count byte_len u
228if ne e SV1_OK
229ret e
230end
231if ne len ext mul ulong get u 0 SV1_UNIT
232ret SV1_E_INVALID_LENGTH
233end
234if not sv1_all_zero ext byte_len sub len ext byte_len
235ret SV1_E_NONZERO_RESERVED
236end
237got = new uchar 32 0
238e = sv1_object_id kind ver part ext 0 byte_len got
239if ne e SV1_OK
240ret e
241end
242if not sv1_equal got id
243ret SV1_E_INTEGRITY
244end
245ret SV1_OK
246end
247
248note ---- CatalogEntry (canonical 64-byte wire record) ----
249note Layout: 0..32 object_id | 32 kind u16 | 34 version u16 | 36 first_unit u64
250note 44 byte_length u64 | 52 unit_count u32 | 56 flags u16 | 58..64 zero
251
252fn sv1_entry_validate_shape ent view uchar * off ulong to int
253kind = sv1_get16 ent add off 32
254ver = sv1_get16 ent add off 34
255blen = sv1_get64 ent add off 44
256ucnt = sv1_get32 ent add off 52
257fl = sv1_get16 ent add off 56
258if or lt kind 3 or eq ver 0 ne fl 0
259ret SV1_E_MALFORMED_DESCRIPTOR
260end
261if not sv1_all_zero ent add off 58 6
262ret SV1_E_NONZERO_RESERVED
263end
264u = new uint 1 0
265r = sv1_unit_count blen u
266if ne r SV1_OK
267ret r
268end
269if ne get u 0 ucnt
270ret SV1_E_MALFORMED_DESCRIPTOR
271end
272ret SV1_OK
273end
274
275fn sv1_entry_validate_bounds ent view uchar * off ulong reg_units ulong cat_first ulong hw ulong to int
276r = sv1_entry_validate_shape ent off
277if ne r SV1_OK
278ret r
279end
280if or lt reg_units 4 or gt reg_units SV1_MAX_REGION_UNITS or lt hw 4 gt hw reg_units
281ret SV1_E_OUT_OF_BOUNDS
282end
283fu = sv1_get64 ent add off 36
284uc = ulong sv1_get32 ent add off 52
285if gt fu sub SV1_U64_MAX uc
286ret SV1_E_LENGTH_OVERFLOW
287end
288eu = add fu uc
289if or lt fu 2 gt eu cat_first
290ret SV1_E_OUT_OF_BOUNDS
291end
292if or gt eu hw gt eu reg_units
293ret SV1_E_OUT_OF_BOUNDS
294end
295ret SV1_OK
296end
297
298note ---- Catalog helpers ----
299
300fn sv1_catalog_len n ulong to ulong
301ret add SV1_CATALOG_HEADER mul n SV1_CATALOG_ENTRY
302end
303
304fn sv1_commit_validate_gen buf view uchar * off ulong gen ulong pgen ulong to int
305if eq gen 1
306if ne pgen 0
307ret SV1_E_INVALID_GENERATION
308end
309if not sv1_all_zero buf add off 72 32
310ret SV1_E_INVALID_GENERATION
311end
312if not sv1_all_zero buf add off 104 32
313ret SV1_E_INVALID_GENERATION
314end
315ret SV1_OK
316end
317if eq pgen SV1_U64_MAX
318ret SV1_E_INVALID_GENERATION
319end
320if ne add pgen 1 gen
321ret SV1_E_INVALID_GENERATION
322end
323if sv1_all_zero buf add off 72 32
324ret SV1_E_INVALID_GENERATION
325end
326if sv1_all_zero buf add off 104 32
327ret SV1_E_INVALID_GENERATION
328end
329ret SV1_OK
330end
331
332fn sv1_commit_validate_cat buf view uchar * off ulong reg_u ulong to int
333cat_fu = sv1_get64 buf add off 168
334cat_bl = sv1_get64 buf add off 176
335cat_uc = sv1_get32 buf add off 184
336cat_ec = sv1_get32 buf add off 188
337if or gt ulong cat_ec SV1_MAX_CATALOG_ENTRIES ne cat_bl sv1_catalog_len ulong cat_ec
338ret SV1_E_MALFORMED_COMMIT
339end
340u = new uint 1 0
341r = sv1_unit_count cat_bl u
342if ne r SV1_OK
343ret r
344end
345if ne get u 0 cat_uc
346ret SV1_E_MALFORMED_COMMIT
347end
348if gt cat_fu sub SV1_U64_MAX ulong cat_uc
349ret SV1_E_LENGTH_OVERFLOW
350end
351eu = add cat_fu ulong cat_uc
352hw = sv1_get64 buf add off 192
353if or lt cat_fu 2 ge eu reg_u
354ret SV1_E_MALFORMED_COMMIT
355end
356if or ne hw add eu 1 gt hw reg_u
357ret SV1_E_MALFORMED_COMMIT
358end
359ret SV1_OK
360end
361
362fn sv1_commit_validate buf view uchar * off ulong to int
363if not sv1_equal part buf off 8 sv1_cmt_magic
364ret SV1_E_MALFORMED_COMMIT
365end
366if or ne sv1_get16 buf add off 8 1 ne sv1_get16 buf add off 10 ushort SV1_COMMIT_BYTES
367ret SV1_E_UNSUPPORTED_VERSION
368end
369if or ne sv1_get16 buf add off 12 1 ne sv1_get16 buf add off 14 0
370ret SV1_E_UNSUPPORTED_VERSION
371end
372if or ne sv1_get64 buf add off 16 0 ne sv1_get64 buf add off 24 0
373ret SV1_E_UNSUPPORTED_FEATURES
374end
375reg_u = sv1_get64 buf add off 48
376gen = sv1_get64 buf add off 56
377if or lt reg_u 4 or gt reg_u SV1_MAX_REGION_UNITS eq gen 0
378ret SV1_E_MALFORMED_COMMIT
379end
380rc = sv1_commit_validate_cat buf off reg_u
381if ne rc SV1_OK
382ret rc
383end
384pgen = sv1_get64 buf add off 64
385ret sv1_commit_validate_gen buf off gen pgen
386end
387
388fn sv1_commit_identifies_pred cur view uchar 200 prev view uchar 200 prev_id view uchar 32 to bool
389gen = sv1_get64 cur 56
390pgen = sv1_get64 prev 56
391if or eq gen 0 ne sub gen 1 pgen
392ret false
393end
394if ne sv1_get64 cur 64 pgen
395ret false
396end
397if not sv1_equal part cur 72 32 prev_id
398ret false
399end
400if not sv1_equal part cur 104 32 part prev 136 32
401ret false
402end
403if not sv1_equal part cur 32 16 part prev 32 16
404ret false
405end
406if ne sv1_get64 cur 48 sv1_get64 prev 48
407ret false
408end
409if lt sv1_get64 cur 168 sv1_get64 prev 192
410ret false
411end
412ret true
413end
414
415note ---- Superblock (4096-byte canonical wire format) ----
416
417fn sv1_sb_validate_cat buf view uchar * off ulong hw ulong to int
418cmu = sv1_get64 buf add off 96
419cat_fu = sv1_get64 buf add off 136
420cat_bl = sv1_get64 buf add off 144
421cat_uc = sv1_get32 buf add off 152
422cat_ec = sv1_get32 buf add off 156
423if gt cat_fu sub SV1_U64_MAX ulong cat_uc
424ret SV1_E_LENGTH_OVERFLOW
425end
426eu = add cat_fu ulong cat_uc
427if or gt ulong cat_ec SV1_MAX_CATALOG_ENTRIES ne cat_bl sv1_catalog_len ulong cat_ec
428ret SV1_E_MALFORMED_SUPERBLOCK
429end
430u = new uint 1 0
431r = sv1_unit_count cat_bl u
432if ne r SV1_OK
433ret r
434end
435if or ne get u 0 cat_uc lt cat_fu 2
436ret SV1_E_MALFORMED_SUPERBLOCK
437end
438if ne eu cmu
439ret SV1_E_MALFORMED_SUPERBLOCK
440end
441if eq cmu SV1_U64_MAX
442ret SV1_E_MALFORMED_SUPERBLOCK
443end
444if ne add cmu 1 hw
445ret SV1_E_MALFORMED_SUPERBLOCK
446end
447ret SV1_OK
448end
449
450fn sv1_sb_validate_shape buf view uchar * off ulong to int
451slot = sv1_get32 buf add off 44
452reg_u = sv1_get64 buf add off 48
453gen = sv1_get64 buf add off 56
454hw = sv1_get64 buf add off 160
455if or gt slot 1 or lt reg_u 4 gt reg_u SV1_MAX_REGION_UNITS
456ret SV1_E_MALFORMED_SUPERBLOCK
457end
458if or eq gen 0 or gt hw reg_u lt hw 4
459ret SV1_E_MALFORMED_SUPERBLOCK
460end
461ret sv1_sb_validate_cat buf off hw
462end
463
464fn sv1_superblock_decode buf view uchar * off ulong exp_slot uint to int
465if not sv1_equal part buf off 8 sv1_sb_magic
466ret SV1_E_BAD_SUPERBLOCK_MAGIC
467end
468if not sv1_superblock_crc_ok buf off
469ret SV1_E_BAD_SUPERBLOCK_CRC
470end
471if not sv1_all_zero buf add off SV1_SB_RESERVED_OFFSET sub SV1_UNIT SV1_SB_RESERVED_OFFSET
472ret SV1_E_NONZERO_RESERVED
473end
474if or ne sv1_get16 buf add off 8 1 ne sv1_get16 buf add off 10 0
475ret SV1_E_UNSUPPORTED_VERSION
476end
477if or ne sv1_get64 buf add off 12 0 ne sv1_get64 buf add off 20 0
478ret SV1_E_UNSUPPORTED_FEATURES
479end
480slot = sv1_get32 buf add off 44
481if or ne slot exp_slot gt slot 1
482ret SV1_E_WRONG_SUPERBLOCK_SLOT
483end
484ret sv1_sb_validate_shape buf off
485end
486
487fn sv1_superblock_validate_commit sb view uchar 4096 cid view uchar 32 cmt view uchar 200 to int
488if not sv1_equal part sb 64 32 cid
489ret SV1_E_MALFORMED_SUPERBLOCK
490end
491if not sv1_equal part sb 28 16 part cmt 32 16
492ret SV1_E_MALFORMED_SUPERBLOCK
493end
494if ne sv1_get64 sb 48 sv1_get64 cmt 48
495ret SV1_E_MALFORMED_SUPERBLOCK
496end
497if ne sv1_get64 sb 56 sv1_get64 cmt 56
498ret SV1_E_MALFORMED_SUPERBLOCK
499end
500if not sv1_equal part sb 104 32 part cmt 136 32
501ret SV1_E_MALFORMED_SUPERBLOCK
502end
503if ne sv1_get64 sb 136 sv1_get64 cmt 168
504ret SV1_E_MALFORMED_SUPERBLOCK
505end
506if ne sv1_get64 sb 144 sv1_get64 cmt 176
507ret SV1_E_MALFORMED_SUPERBLOCK
508end
509if ne sv1_get32 sb 152 sv1_get32 cmt 184
510ret SV1_E_MALFORMED_SUPERBLOCK
511end
512if ne sv1_get32 sb 156 sv1_get32 cmt 188
513ret SV1_E_MALFORMED_SUPERBLOCK
514end
515hw = sv1_get64 sb 160
516if ne hw sv1_get64 cmt 192
517ret SV1_E_MALFORMED_SUPERBLOCK
518end
519cmu = sv1_get64 sb 96
520cat_fu = sv1_get64 sb 136
521cat_uc = ulong sv1_get32 sb 152
522if or eq cmu SV1_U64_MAX ne add cmu 1 hw
523ret SV1_E_MALFORMED_SUPERBLOCK
524end
525if or gt cat_fu sub SV1_U64_MAX cat_uc ne add cat_fu cat_uc cmu
526ret SV1_E_MALFORMED_SUPERBLOCK
527end
528ret SV1_OK
529end
530
531fn sv1_superblock_equivalent a view uchar 4096 b view uchar 4096 to bool
532if not sv1_equal part a 28 16 part b 28 16
533ret false
534end
535if ne sv1_get64 a 48 sv1_get64 b 48
536ret false
537end
538if ne sv1_get64 a 56 sv1_get64 b 56
539ret false
540end
541ret sv1_equal part a 64 32 part b 64 32
542end
543
544note ---- Genesis units 0..3 (16384 bytes) ----
545
546fn sv1_genesis_units uuid view uchar 16 reg_units ulong dst edit uchar 16384 to int
547if or lt reg_units 4 gt reg_units SV1_MAX_REGION_UNITS
548ret SV1_E_MALFORMED_COMMIT
549end
550for i ulong 16384
551put dst i 0
552end
553note Unit 2: empty catalog (16 bytes)
554u2 = ulong 8192
555for i ulong 8
556put dst add u2 i get sv1_cat_magic i
557end
558sv1_put16 dst add u2 8 1
559sv1_put16 dst add u2 10 ushort SV1_CATALOG_ENTRY
560sv1_put32 dst add u2 12 0
561cat_id = new uchar 32 0
562r = sv1_object_id SV1_KIND_CATALOG SV1_VERSION part dst u2 16 cat_id
563if ne r SV1_OK
564ret r
565end
566note Unit 3: genesis commit record (200 bytes)
567u3 = ulong 12288
568for i ulong 8
569put dst add u3 i get sv1_cmt_magic i
570end
571sv1_put16 dst add u3 8 1
572sv1_put16 dst add u3 10 ushort SV1_COMMIT_BYTES
573sv1_put16 dst add u3 12 1
574sv1_put16 dst add u3 14 0
575for i ulong 16
576put dst add u3 add 32 i get uuid i
577end
578sv1_put64 dst add u3 48 reg_units
579sv1_put64 dst add u3 56 1
580sv1_put64 dst add u3 64 0
581for i ulong 32
582put dst add u3 add 136 i get cat_id i
583end
584sv1_put64 dst add u3 168 2
585sv1_put64 dst add u3 176 16
586sv1_put32 dst add u3 184 1
587sv1_put32 dst add u3 188 0
588sv1_put64 dst add u3 192 4
589r = sv1_commit_validate dst u3
590if ne r SV1_OK
591ret r
592end
593cmt_id = new uchar 32 0
594r = sv1_object_id SV1_KIND_COMMIT SV1_VERSION part dst u3 SV1_COMMIT_BYTES cmt_id
595if ne r SV1_OK
596ret r
597end
598note Unit 0: superblock A (slot 0)
599for i ulong 8
600put dst i get sv1_sb_magic i
601end
602sv1_put16 dst 8 1
603sv1_put16 dst 10 0
604for i ulong 16
605put dst add 28 i get uuid i
606end
607sv1_put32 dst 44 0
608sv1_put64 dst 48 reg_units
609sv1_put64 dst 56 1
610for i ulong 32
611put dst add 64 i get cmt_id i
612end
613sv1_put64 dst 96 3
614for i ulong 32
615put dst add 104 i get cat_id i
616end
617sv1_put64 dst 136 2
618sv1_put64 dst 144 16
619sv1_put32 dst 152 1
620sv1_put32 dst 156 0
621sv1_put64 dst 160 4
622crc = sv1_sb_crc dst 0
623sv1_put32 dst SV1_SB_CRC_OFFSET crc
624ret sv1_superblock_decode dst 0 0
625end