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