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