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 / synapse / core / ledger.om

307 lines · 6965 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note protocol/synapse/core/ledger.om: append-only SHA-256 hash-chained event ledger
3note recording every changeset, lease, review, and landing transition with chain-integrity
4note verification, tamper refusal, and JSONL serialization.
5note Composed OM-2 library unit (no om2 directive, no main).
6
7const LG_MAX ulong 1024
8const LG_FIELDS ulong 7
9
10const LG_EV_CS_CREATE ulong 1
11const LG_EV_CS_STATE ulong 2
12const LG_EV_LEASE_CLAIM ulong 3
13const LG_EV_LEASE_RENEW ulong 4
14const LG_EV_LEASE_RELEASE ulong 5
15const LG_EV_REVIEW ulong 6
16const LG_EV_LAND ulong 7
17
18const LGF_SEQ ulong 0
19const LGF_KIND ulong 1
20const LGF_TS ulong 2
21const LGF_ACT_OFF ulong 3
22const LGF_ACT_LEN ulong 4
23const LGF_DET_OFF ulong 5
24const LGF_DET_LEN ulong 6
25
26global g_lg_meta ulong 7168
27global g_lg_subj uchar 32768
28global g_lg_prev uchar 32768
29global g_lg_hash uchar 32768
30global g_lg_head uchar 32
31global g_lg_stg uchar 32
32global g_lg_hdr ulong 8
33
34text t_lg_dom "SYNAPSE_LEDGER_ENTRY_V1"
35text t_ev_csc "CS_CREATE"
36text t_ev_css "CS_STATE"
37text t_ev_lcl "LEASE_CLAIM"
38text t_ev_lrn "LEASE_RENEW"
39text t_ev_lrl "LEASE_RELEASE"
40text t_ev_rev "REVIEW"
41text t_ev_lnd "LAND"
42text t_ev_unk "UNKNOWN"
43
44text t_lj1 "{\"seq\":"
45text t_lj2 ",\"kind\":\""
46text t_lj3 "\",\"ts\":"
47text t_lj4 ",\"actor\":\""
48text t_lj5 "\",\"detail\":\""
49text t_lj6 "\",\"subject\":\""
50text t_lj7 "\",\"prev\":\""
51text t_lj8 "\",\"hash\":\""
52text t_lj9 "\"}\n"
53
54fn lg_reset to void
55put g_lg_hdr 0 0
56put g_lg_hdr 1 0
57for i ulong 32
58put g_lg_head i 0
59put g_lg_stg i 0
60end
61end
62
63fn syn_reset_all to void
64syn_arena_reset
65cs_reset
66gr_reset
67ls_reset
68rv_reset
69lg_reset
70end
71
72fn lg_count to ulong
73ret get g_lg_hdr 0
74end
75
76fn lg_bad_seq to ulong
77ret get g_lg_hdr 1
78end
79
80fn lg_head dst edit uchar * to void
81for i ulong 32
82put dst i get g_lg_head i
83end
84end
85
86fn lg_mget idx ulong f ulong to ulong
87ret get g_lg_meta add mul idx LG_FIELDS f
88end
89
90fn lg_mput idx ulong f ulong v ulong to void
91put g_lg_meta add mul idx LG_FIELDS f v
92end
93
94fn lg_set_subject subj view uchar * to void
95n = len subj
96if gt n 32
97n = 32
98end
99for i ulong 32
100if lt i n
101put g_lg_stg i get subj i
102else
103put g_lg_stg i 0
104end
105end
106end
107
108note Computes domain-separated SHA-256 of ledger entry idx into dst[32].
109fn lg_compute_entry_hash idx ulong dst edit uchar * to void
110ctx = new ulong 40 0
111blk = new uchar 32 0
112nul = new uchar 1 0
113sha256_init ctx
114sha256_update ctx t_lg_dom
115sha256_update ctx nul
116syn_sha_u64 ctx lg_mget idx LGF_SEQ
117syn_sha_u64 ctx lg_mget idx LGF_KIND
118syn_sha_u64 ctx lg_mget idx LGF_TS
119base = mul idx 32
120for i ulong 32
121put blk i get g_lg_subj add base i
122end
123sha256_update ctx blk
124syn_sha_arena_str ctx lg_mget idx LGF_ACT_OFF lg_mget idx LGF_ACT_LEN
125syn_sha_arena_str ctx lg_mget idx LGF_DET_OFF lg_mget idx LGF_DET_LEN
126for i ulong 32
127put blk i get g_lg_prev add base i
128end
129sha256_update ctx blk
130sha256_final ctx dst
131end
132
133note Appends one hash-chained ledger event using staged subject and arena actor/detail slices.
134note Returns monotonic sequence number (1..), or 0 if ledger is at capacity.
135fn lg_append_arena kind ulong ts ulong ao ulong al ulong doff ulong dlen ulong to ulong
136h = new uchar 32 0
137cnt = get g_lg_hdr 0
138if ge cnt LG_MAX
139ret 0
140end
141seq = add cnt 1
142base = mul cnt 32
143lg_mput cnt LGF_SEQ seq
144lg_mput cnt LGF_KIND kind
145lg_mput cnt LGF_TS ts
146lg_mput cnt LGF_ACT_OFF ao
147lg_mput cnt LGF_ACT_LEN al
148lg_mput cnt LGF_DET_OFF doff
149lg_mput cnt LGF_DET_LEN dlen
150for i ulong 32
151put g_lg_subj add base i get g_lg_stg i
152put g_lg_prev add base i get g_lg_head i
153end
154lg_compute_entry_hash cnt h
155for i ulong 32
156put g_lg_hash add base i get h i
157put g_lg_head i get h i
158end
159put g_lg_hdr 0 seq
160ret seq
161end
162
163note Convenience wrapper: arena actor + uchar view detail.
164fn lg_append_view kind ulong ts ulong ao ulong al ulong det view uchar * to ulong
165doff = syn_arena_put det
166if eq doff SYN_NONE
167ret 0
168end
169ret lg_append_arena kind ts ao al doff len det
170end
171
172note Verifies entire hash-chained ledger from seq 1 to head.
173note Checks sequence continuity, prev_hash chaining from [0; 32], recomputed SHA-256 of every entry,
174note and final head digest match. Returns SYN_OK (0) or SYN_ERR_LEDGER_TAMPER (12).
175fn lg_verify to ulong
176exp_prev = new uchar 32 0
177calc = new uchar 32 0
178put g_lg_hdr 1 0
179cnt = get g_lg_hdr 0
180for i ulong cnt
181exp_seq = add i 1
182if ne lg_mget i LGF_SEQ exp_seq
183put g_lg_hdr 1 exp_seq
184ret SYN_ERR_LEDGER_TAMPER
185end
186base = mul i 32
187for j ulong 32
188if ne get g_lg_prev add base j get exp_prev j
189put g_lg_hdr 1 exp_seq
190ret SYN_ERR_LEDGER_TAMPER
191end
192end
193lg_compute_entry_hash i calc
194for j ulong 32
195if ne get g_lg_hash add base j get calc j
196put g_lg_hdr 1 exp_seq
197ret SYN_ERR_LEDGER_TAMPER
198end
199put exp_prev j get calc j
200end
201end
202for j ulong 32
203if ne get g_lg_head j get exp_prev j
204put g_lg_hdr 1 cnt
205ret SYN_ERR_LEDGER_TAMPER
206end
207end
208ret SYN_OK
209end
210
211note Controlled tamper-injection helpers for negative-control verification.
212fn lg_tamper_xor_subj idx ulong byte_pos ulong mask uchar to void
213if and lt idx get g_lg_hdr 0 lt byte_pos 32
214pos = add mul idx 32 byte_pos
215put g_lg_subj pos xor get g_lg_subj pos mask
216end
217end
218
219fn lg_tamper_xor_prev idx ulong byte_pos ulong mask uchar to void
220if and lt idx get g_lg_hdr 0 lt byte_pos 32
221pos = add mul idx 32 byte_pos
222put g_lg_prev pos xor get g_lg_prev pos mask
223end
224end
225
226fn lg_tamper_xor_hash idx ulong byte_pos ulong mask uchar to void
227if and lt idx get g_lg_hdr 0 lt byte_pos 32
228pos = add mul idx 32 byte_pos
229put g_lg_hash pos xor get g_lg_hash pos mask
230end
231end
232
233fn lg_tamper_set_seq idx ulong new_seq ulong to void
234if lt idx get g_lg_hdr 0
235lg_mput idx LGF_SEQ new_seq
236end
237end
238
239fn lg_write_kind fd int kind ulong to void
240if eq kind LG_EV_CS_CREATE
241syn_write_text fd t_ev_csc
242ret
243end
244if eq kind LG_EV_CS_STATE
245syn_write_text fd t_ev_css
246ret
247end
248if eq kind LG_EV_LEASE_CLAIM
249syn_write_text fd t_ev_lcl
250ret
251end
252if eq kind LG_EV_LEASE_RENEW
253syn_write_text fd t_ev_lrn
254ret
255end
256if eq kind LG_EV_LEASE_RELEASE
257syn_write_text fd t_ev_lrl
258ret
259end
260if eq kind LG_EV_REVIEW
261syn_write_text fd t_ev_rev
262ret
263end
264if eq kind LG_EV_LAND
265syn_write_text fd t_ev_lnd
266ret
267end
268syn_write_text fd t_ev_unk
269end
270
271fn lg_dump_entry_jsonl fd int idx ulong to void
272buf = new uchar 32 0
273base = mul idx 32
274syn_write_text fd t_lj1
275syn_write_u64 fd lg_mget idx LGF_SEQ
276syn_write_text fd t_lj2
277lg_write_kind fd lg_mget idx LGF_KIND
278syn_write_text fd t_lj3
279syn_write_u64 fd lg_mget idx LGF_TS
280syn_write_text fd t_lj4
281syn_write_arena fd lg_mget idx LGF_ACT_OFF lg_mget idx LGF_ACT_LEN
282syn_write_text fd t_lj5
283syn_write_arena fd lg_mget idx LGF_DET_OFF lg_mget idx LGF_DET_LEN
284syn_write_text fd t_lj6
285for j ulong 32
286put buf j get g_lg_subj add base j
287end
288syn_write_hex32 fd buf
289syn_write_text fd t_lj7
290for j ulong 32
291put buf j get g_lg_prev add base j
292end
293syn_write_hex32 fd buf
294syn_write_text fd t_lj8
295for j ulong 32
296put buf j get g_lg_hash add base j
297end
298syn_write_hex32 fd buf
299syn_write_text fd t_lj9
300end
301
302fn lg_dump_jsonl fd int to void
303cnt = get g_lg_hdr 0
304for i ulong cnt
305lg_dump_entry_jsonl fd i
306end
307end