interplane.Get Omega
Read in your language

spark-rsi-mv

Native Rust recursive self-improvement engine with paired bootstrap evaluation and cryptographic ledger

Brought by drakestapleton

branch main Branches Tags History

spark-rsi-mv / om / governance / ledger.om

314 lines · 7882 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note core/rsi/om/governance/ledger.om
3note Pure OM-2 conversion of spark-rsi-mv/src/ledger/record.rs and src/ledger/merkle.rs.
4note Cryptographic SHA-256 hash-chained improvement ledger and domain-separated binary
5note Merkle tree root + leaf inclusion verification.
6note Compose after si/directives/om2.om and om/tls/sha2.om.
7
8const RSI_LDG_CAP ulong 64
9const RSI_LDG_HASH_BYTES ulong 32
10const RSI_LDG_HASH_TOTAL ulong 2048
11
12const RSI_LDG_OK ulong 0
13const RSI_LDG_ERR_FULL ulong 1
14const RSI_LDG_ERR_SEQ ulong 2
15const RSI_LDG_ERR_PREV_HASH ulong 3
16const RSI_LDG_ERR_HASH ulong 4
17const RSI_LDG_ERR_EMPTY ulong 5
18const RSI_LDG_ERR_OOB ulong 6
19
20text rsi_ldg_dom "RSI-LEDGER-V1"
21text rsi_leaf_dom "RSI-LEAF-V1"
22text rsi_node_dom "RSI-NODE-V1"
23
24note Ledger metadata: [0]=count, [1]=last_seq
25global rldg_meta ulong 4
26global rldg_seq ulong 64
27global rldg_cand_id ulong 64
28global rldg_tier ulong 64
29global rldg_verdict ulong 64
30global rldg_perf_delta long 64
31global rldg_corr_bps ulong 64
32global rldg_trust_src ulong 64
33global rldg_prev_hash uchar 2048
34global rldg_hash uchar 2048
35
36note Working Merkle level buffers (2 levels of up to 64 * 32 = 2048 bytes each)
37global rldg_m_cur uchar 2048
38global rldg_m_nxt uchar 2048
39
40fn rsi_ledger_reset to void
41for i ulong 4
42put rldg_meta i 0
43end
44for i ulong RSI_LDG_CAP
45put rldg_seq i 0
46put rldg_cand_id i 0
47put rldg_tier i 0
48put rldg_verdict i 0
49put rldg_perf_delta i long 0
50put rldg_corr_bps i 0
51put rldg_trust_src i 0
52end
53for i ulong RSI_LDG_HASH_TOTAL
54put rldg_prev_hash i uchar 0
55put rldg_hash i uchar 0
56put rldg_m_cur i uchar 0
57put rldg_m_nxt i uchar 0
58end
59end
60
61fn rsi_ledger_count to ulong
62ret get rldg_meta 0
63end
64
65note Write a u64 in little-endian into buf[off..off+8).
66fn rldg_put_u64 buf edit uchar * off ulong v ulong to void
67for i ulong 8
68sh = mul i 8
69put buf add off i uchar and shr v sh ulong 255
70end
71end
72
73note Compute canonical SHA-256 hash of ledger record at index idx into out_h[0..32).
74fn rldg_compute_rec_hash idx ulong out_h edit uchar 32 to void
75ctx = new ulong 40 0
76sha256_init ctx
77sha256_update ctx rsi_ldg_dom
78hdr = new uchar 56 0
79rldg_put_u64 hdr 0 get rldg_seq idx
80rldg_put_u64 hdr 8 get rldg_cand_id idx
81rldg_put_u64 hdr 16 get rldg_tier idx
82rldg_put_u64 hdr 24 get rldg_verdict idx
83rldg_put_u64 hdr 32 bitcast ulong get rldg_perf_delta idx
84rldg_put_u64 hdr 40 get rldg_corr_bps idx
85rldg_put_u64 hdr 48 get rldg_trust_src idx
86sha256_update ctx hdr
87prev = new uchar 32 0
88pbase = mul idx RSI_LDG_HASH_BYTES
89for i ulong 32
90put prev i get rldg_prev_hash add pbase i
91end
92sha256_update ctx prev
93sha256_final ctx out_h
94end
95
96note Constant-time 32-byte equality comparison.
97fn rldg_hash_eq a view uchar 32 b view uchar 32 to bool
98diff = uchar 0
99for i ulong 32
100diff = or diff xor get a i get b i
101end
102ret eq diff uchar 0
103end
104
105note Append an evaluated improvement record to the cryptographic hash-chained ledger.
106fn rsi_ledger_append cand_id ulong tier ulong verdict ulong perf_delta long corr_bps ulong trust_src ulong to ulong
107cnt = get rldg_meta 0
108if ge cnt RSI_LDG_CAP
109ret RSI_LDG_ERR_FULL
110end
111nseq = add get rldg_meta 1 1
112put rldg_seq cnt nseq
113put rldg_cand_id cnt cand_id
114put rldg_tier cnt tier
115put rldg_verdict cnt verdict
116put rldg_perf_delta cnt perf_delta
117put rldg_corr_bps cnt corr_bps
118put rldg_trust_src cnt trust_src
119pbase = mul cnt RSI_LDG_HASH_BYTES
120if eq cnt 0
121for i ulong 32
122put rldg_prev_hash add pbase i uchar 0
123end
124else
125lbase = mul sub cnt 1 RSI_LDG_HASH_BYTES
126for i ulong 32
127put rldg_prev_hash add pbase i get rldg_hash add lbase i
128end
129end
130h = new uchar 32 0
131rldg_compute_rec_hash cnt h
132for i ulong 32
133put rldg_hash add pbase i get h i
134end
135put rldg_meta 0 add cnt 1
136put rldg_meta 1 nseq
137ret RSI_LDG_OK
138end
139
140note Verify the entire ledger chain from genesis: strict sequence monotonicity, prev_hash links, and SHA-256 hashes.
141fn rsi_ledger_verify_chain to ulong
142cnt = get rldg_meta 0
143exp_prev = new uchar 32 0
144cur_prev = new uchar 32 0
145stored_h = new uchar 32 0
146comp_h = new uchar 32 0
147for i ulong cnt bound 64
148if ne get rldg_seq i add i 1
149ret RSI_LDG_ERR_SEQ
150end
151pbase = mul i RSI_LDG_HASH_BYTES
152for j ulong 32
153put cur_prev j get rldg_prev_hash add pbase j
154put stored_h j get rldg_hash add pbase j
155end
156if not rldg_hash_eq cur_prev exp_prev
157ret RSI_LDG_ERR_PREV_HASH
158end
159rldg_compute_rec_hash i comp_h
160if not rldg_hash_eq stored_h comp_h
161ret RSI_LDG_ERR_HASH
162end
163for j ulong 32
164put exp_prev j get stored_h j
165end
166end
167ret RSI_LDG_OK
168end
169
170note Hash one Merkle leaf: SHA-256("RSI-LEAF-V1" || record_hash).
171fn rldg_hash_leaf rec_idx ulong dst_idx ulong to void
172ctx = new ulong 40 0
173leaf_in = new uchar 32 0
174leaf_out = new uchar 32 0
175sbase = mul rec_idx 32
176for j ulong 32
177put leaf_in j get rldg_hash add sbase j
178end
179sha256_init ctx
180sha256_update ctx rsi_leaf_dom
181sha256_update ctx leaf_in
182sha256_final ctx leaf_out
183dbase = mul dst_idx 32
184for j ulong 32
185put rldg_m_cur add dbase j get leaf_out j
186end
187end
188
189note Hash two Merkle children (left, right) in rldg_m_cur into rldg_m_nxt[dst_idx].
190fn rldg_hash_pair left_idx ulong right_idx ulong dst_idx ulong to void
191ctx = new ulong 40 0
192pair = new uchar 64 0
193lbase = mul left_idx 32
194rbase = mul right_idx 32
195for j ulong 32
196put pair j get rldg_m_cur add lbase j
197put pair add j 32 get rldg_m_cur add rbase j
198end
199node_out = new uchar 32 0
200sha256_init ctx
201sha256_update ctx rsi_node_dom
202sha256_update ctx pair
203sha256_final ctx node_out
204dbase = mul dst_idx 32
205for j ulong 32
206put rldg_m_nxt add dbase j get node_out j
207end
208end
209
210note Advance rldg_m_cur one Merkle level up and return the new width.
211fn rldg_merkle_step_level width ulong to ulong
212nwidth = div add width 1 2
213for p ulong nwidth bound 32
214lidx = mul p 2
215ridx = add lidx 1
216if ge ridx width
217ridx = lidx
218end
219rldg_hash_pair lidx ridx p
220end
221nbytes = mul nwidth 32
222for b ulong nbytes bound 2048
223put rldg_m_cur b get rldg_m_nxt b
224end
225ret nwidth
226end
227
228note Combine cur_h with sibling at sib_idx in rldg_m_cur into cur_h.
229fn rldg_merkle_combine cur_h edit uchar 32 sib_idx ulong is_right bool to void
230pair = new uchar 64 0
231ctx = new ulong 40 0
232sbase = mul sib_idx 32
233if is_right
234for j ulong 32
235put pair j get rldg_m_cur add sbase j
236put pair add j 32 get cur_h j
237end
238else
239for j ulong 32
240put pair j get cur_h j
241put pair add j 32 get rldg_m_cur add sbase j
242end
243end
244sha256_init ctx
245sha256_update ctx rsi_node_dom
246sha256_update ctx pair
247sha256_final ctx cur_h
248end
249
250note Compute binary Merkle root over all ledger record hashes (odd levels duplicate last node).
251fn rsi_merkle_root out_root edit uchar 32 to ulong
252cnt = get rldg_meta 0
253if eq cnt 0
254ret RSI_LDG_ERR_EMPTY
255end
256for i ulong cnt bound 64
257rldg_hash_leaf i i
258end
259width = cnt
260while gt width 1 bound 8
261width = rldg_merkle_step_level width
262end
263for j ulong 32
264put out_root j get rldg_m_cur j
265end
266ret RSI_LDG_OK
267end
268
269note Compute Merkle inclusion proof for leaf_idx and verify it matches expected_root.
270fn rsi_merkle_verify_leaf leaf_idx ulong expected_root view uchar 32 to bool
271cnt = get rldg_meta 0
272if or eq cnt 0 ge leaf_idx cnt
273ret false
274end
275for i ulong cnt bound 64
276rldg_hash_leaf i i
277end
278cur_h = new uchar 32 0
279lbase = mul leaf_idx 32
280for j ulong 32
281put cur_h j get rldg_m_cur add lbase j
282end
283pos = leaf_idx
284width = cnt
285while gt width 1 bound 8
286is_right = eq rem pos 2 1
287sib_idx = add pos 1
288if is_right
289sib_idx = sub pos 1
290else
291if ge sib_idx width
292sib_idx = pos
293end
294end
295rldg_merkle_combine cur_h sib_idx is_right
296width = rldg_merkle_step_level width
297pos = div pos 2
298end
299ret rldg_hash_eq cur_h expected_root
300end
301
302note Test-only tamper helper (used by negative controls to simulate on-disk corruption).
303fn rsi_ledger_tamper_perf idx ulong bad_val long to void
304if lt idx get rldg_meta 0
305put rldg_perf_delta idx bad_val
306end
307end
308
309fn rsi_ledger_tamper_prev idx ulong byte_idx ulong xor_mask uchar to void
310if and lt idx get rldg_meta 0 lt byte_idx 32
311off = add mul idx 32 byte_idx
312put rldg_prev_hash off xor get rldg_prev_hash off xor_mask
313end
314end