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 / crumb-spec / ledger.om

253 lines · 6351 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/crumb-spec/ledger.om: pure-Omega port of crumb-spec (src/ledger.rs)
3note Cryptographic BLAKE3 hash-chained ledger for archived/legacy crumb schema verification.
4note Standing rule (DRA-97 / 2026-11-08): active workflow .crumb files remain retired;
5note no .crumb files are created or read in active workflows. This unit locks the pure-Omega
6note wire/hash contract for historical/archived crumb ledger verification.
7note Compose after si/directives/om2.om and om/blake3/b3unit.om.
8
9const CRUMB_OK ulong 0
10const CRUMB_ERR_HASH_MISMATCH ulong 1
11const CRUMB_ERR_PARENT_MISMATCH ulong 2
12const CRUMB_ERR_CHRONO ulong 3
13const CRUMB_ERR_INVALID_INDEX ulong 4
14const CRUMB_ERR_MISSING_PARENT ulong 5
15const CRUMB_ERR_INVALID_GENESIS ulong 6
16const CRUMB_ERR_FULL ulong 7
17const CRUMB_ERR_OVERFLOW ulong 8
18
19const CRUMB_ACT_CREATE ulong 0
20const CRUMB_ACT_MODIFY ulong 1
21const CRUMB_ACT_DELETE ulong 2
22const CRUMB_ACT_AUDIT ulong 3
23const CRUMB_ACT_TEST ulong 4
24const CRUMB_ACT_BUILD ulong 5
25
26const CRUMB_MAX_EVENTS ulong 32
27
28text cr_domain "CRUMB_LEDGER_EVENT_V1"
29text cr_act_create "create"
30text cr_act_modify "modify"
31text cr_act_delete "delete"
32text cr_act_audit "audit"
33text cr_act_test "test"
34text cr_act_build "build"
35
36fn cr_put_u32_be dst edit uchar * pos ulong v uint to ulong
37if gt add pos ulong 4 len dst
38ret ulong 99999
39end
40put dst pos uchar and shr v uint 24 uint 255
41put dst add pos ulong 1 uchar and shr v uint 16 uint 255
42put dst add pos ulong 2 uchar and shr v uint 8 uint 255
43put dst add pos ulong 3 uchar and v uint 255
44ret add pos ulong 4
45end
46
47fn cr_put_u64_be dst edit uchar * pos ulong v ulong to ulong
48if gt add pos ulong 8 len dst
49ret ulong 99999
50end
51for i ulong 8
52sh = mul sub ulong 7 i ulong 8
53put dst add pos i uchar and shr v sh ulong 255
54end
55ret add pos ulong 8
56end
57
58fn cr_copy_bytes dst edit uchar * pos ulong src view uchar * to ulong
59n = len src
60if gt add pos n len dst
61ret ulong 99999
62end
63if gt n ulong 1024
64ret ulong 99999
65end
66i = ulong 0
67while lt i n bound 1024
68put dst add pos i get src i
69i = add i ulong 1
70end
71ret add pos n
72end
73
74fn cr_put_lp_str dst edit uchar * pos ulong src view uchar * to ulong
75p = cr_put_u32_be dst pos uint len src
76if eq p ulong 99999
77ret ulong 99999
78end
79ret cr_copy_bytes dst p src
80end
81
82fn cr_put_action dst edit uchar * pos ulong act ulong to ulong
83if eq act CRUMB_ACT_CREATE
84ret cr_put_lp_str dst pos cr_act_create
85end
86if eq act CRUMB_ACT_MODIFY
87ret cr_put_lp_str dst pos cr_act_modify
88end
89if eq act CRUMB_ACT_DELETE
90ret cr_put_lp_str dst pos cr_act_delete
91end
92if eq act CRUMB_ACT_AUDIT
93ret cr_put_lp_str dst pos cr_act_audit
94end
95if eq act CRUMB_ACT_TEST
96ret cr_put_lp_str dst pos cr_act_test
97end
98if eq act CRUMB_ACT_BUILD
99ret cr_put_lp_str dst pos cr_act_build
100end
101ret ulong 99999
102end
103
104fn cr_hash_eq a view uchar 32 b view uchar 32 to bool
105for i ulong 32
106if ne get a i get b i
107ret false
108end
109end
110ret true
111end
112
113fn cr_is_zero_hash h view uchar 32 to bool
114for i ulong 32
115if ne get h i uchar 0
116ret false
117end
118end
119ret true
120end
121
122note Single-shot BLAKE3-256 over up to 1024 bytes using om/blake3/b3unit.om
123fn cr_blake3_buf data view uchar * digest edit uchar 32 to ulong
124n = len data
125if gt n ulong 1024
126ret CRUMB_ERR_OVERFLOW
127end
128st = new ulong 448 0
129blk = new uchar 1024 0
130i = ulong 0
131while lt i n bound 1024
132put blk i get data i
133i = add i ulong 1
134end
135omb3 st blk n ulong 1 digest
136ret CRUMB_OK
137end
138
139note Encode canonical LedgerEvent preimage matching crumb-spec src/ledger.rs:
140note "CRUMB_LEDGER_EVENT_V1" || be64(index) || be64(timestamp) ||
141note be32(len(agent)) || agent || be32(len(action)) || action ||
142note be32(len(target)) || target || be32(len(intent)) || intent ||
143note payload_hash[32] || parent_hash[32]
144note Split into two helpers to respect 6-parameter-slot limit.
145fn cr_event_preimage_head buf edit uchar 1024 idx ulong ts ulong act ulong agent view uchar * to ulong
146p = cr_copy_bytes buf ulong 0 cr_domain
147if eq p ulong 99999
148ret ulong 99999
149end
150p = cr_put_u64_be buf p idx
151if eq p ulong 99999
152ret ulong 99999
153end
154p = cr_put_u64_be buf p ts
155if eq p ulong 99999
156ret ulong 99999
157end
158p = cr_put_lp_str buf p agent
159if eq p ulong 99999
160ret ulong 99999
161end
162ret cr_put_action buf p act
163end
164
165fn cr_event_preimage_tail buf edit uchar 1024 pos ulong target view uchar * intent view uchar * to ulong
166p = cr_put_lp_str buf pos target
167if eq p ulong 99999
168ret ulong 99999
169end
170ret cr_put_lp_str buf p intent
171end
172
173fn cr_event_finalize buf edit uchar 1024 pos ulong ph view uchar 32 par view uchar 32 dig edit uchar 32 to ulong
174p = cr_copy_bytes buf pos ph
175if eq p ulong 99999
176ret CRUMB_ERR_OVERFLOW
177end
178p = cr_copy_bytes buf p par
179if eq p ulong 99999
180ret CRUMB_ERR_OVERFLOW
181end
182st = new ulong 448 0
183omb3 st buf p ulong 1 dig
184ret CRUMB_OK
185end
186
187note Verify an event chain stored in parallel arrays:
188note indices[n], timestamps[n], hashes[n*32], parents[n*32], recomputed[n*32]
189note Each slot i has 32 bytes at offset i*32.
190fn cr_copy_slot32 flat view uchar 1024 idx ulong dst edit uchar 32 to void
191base = mul idx ulong 32
192for i ulong 32
193put dst i get flat add base i
194end
195end
196
197fn cr_put_slot32 flat edit uchar 1024 idx ulong src view uchar 32 to void
198base = mul idx ulong 32
199for i ulong 32
200put flat add base i get src i
201end
202end
203
204fn cr_verify_chain_meta count ulong indices view ulong 32 timestamps view ulong 32 hashes view uchar 1024 parents view uchar 1024 to ulong
205if gt count CRUMB_MAX_EVENTS
206ret CRUMB_ERR_FULL
207end
208hcur = new uchar 32 0
209hprev = new uchar 32 0
210pcur = new uchar 32 0
211i = ulong 0
212while lt i count bound 32
213if ne get indices i i
214ret CRUMB_ERR_INVALID_INDEX
215end
216cr_copy_slot32 parents i pcur
217if eq i ulong 0
218if not cr_is_zero_hash pcur
219ret CRUMB_ERR_INVALID_GENESIS
220end
221else
222prev_i = sub i ulong 1
223cr_copy_slot32 hashes prev_i hprev
224if not cr_hash_eq pcur hprev
225ret CRUMB_ERR_PARENT_MISMATCH
226end
227if lt get timestamps i get timestamps prev_i
228ret CRUMB_ERR_CHRONO
229end
230end
231i = add i ulong 1
232end
233ret CRUMB_OK
234end
235
236fn cr_verify_parent count ulong ev_idx ulong ev_parent view uchar 32 hashes view uchar 1024 to ulong
237if eq ev_idx ulong 0
238if not cr_is_zero_hash ev_parent
239ret CRUMB_ERR_INVALID_GENESIS
240end
241ret CRUMB_OK
242end
243par_i = sub ev_idx ulong 1
244if ge par_i count
245ret CRUMB_ERR_MISSING_PARENT
246end
247exp = new uchar 32 0
248cr_copy_slot32 hashes par_i exp
249if not cr_hash_eq ev_parent exp
250ret CRUMB_ERR_PARENT_MISMATCH
251end
252ret CRUMB_OK
253end