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
omega-mv / protocol / synapse / claims / src / 50_event.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note Event serialisation, hashing, batched signing through synapse-claims-sig, and the locked append. |
| 3 | |
| 4 | text mkid "AIEN_CLAIMS_EVENT_V1\n" |
| 5 | text tbrace "}" |
| 6 | text msig "signing failed (synapse-claims-sig)" |
| 7 | text mapp "ledger append failed" |
| 8 | text mlock "lock failed" |
| 9 | text mhome "home not initialised; run init" |
| 10 | text tsign "sign" |
| 11 | text tver "verify" |
| 12 | text tkeyg "keygen" |
| 13 | |
| 14 | note gev cells: the event being built |
| 15 | const eop ulong 0 |
| 16 | const eid ulong 1 |
| 17 | const eag ulong 2 |
| 18 | const eagl ulong 3 |
| 19 | const ese ulong 4 |
| 20 | const esel ulong 5 |
| 21 | const ekind ulong 6 |
| 22 | const etg ulong 7 |
| 23 | const etgl ulong 8 |
| 24 | const emode ulong 9 |
| 25 | const ettl ulong 10 |
| 26 | const eexp ulong 11 |
| 27 | const est ulong 12 |
| 28 | const ein ulong 13 |
| 29 | const einl ulong 14 |
| 30 | const eev ulong 15 |
| 31 | const eevl ulong 16 |
| 32 | const eor ulong 17 |
| 33 | const eorl ulong 18 |
| 34 | const eidp ulong 19 |
| 35 | |
| 36 | note ======== line writer: s[gx xlp ..] ======== |
| 37 | |
| 38 | fn l_b s edit uchar * c uchar to void |
| 39 | p = get gx xlp |
| 40 | put s p c |
| 41 | put gx xlp add p 1 |
| 42 | end |
| 43 | |
| 44 | fn l_t s edit uchar * t view uchar * to void |
| 45 | for i ulong len t |
| 46 | l_b s get t i |
| 47 | end |
| 48 | end |
| 49 | |
| 50 | fn l_ar s edit uchar * off ulong n ulong to void |
| 51 | for i ulong n |
| 52 | l_b s ar_g add off i |
| 53 | end |
| 54 | end |
| 55 | |
| 56 | fn l_arq s edit uchar * off ulong n ulong to void |
| 57 | l_b s 34 |
| 58 | l_ar s off n |
| 59 | l_b s 34 |
| 60 | end |
| 61 | |
| 62 | fn l_u s edit uchar * v ulong to void |
| 63 | x = v |
| 64 | k = ulong 0 |
| 65 | for i ulong 20 |
| 66 | put gst i uchar add 48 rem x 10 |
| 67 | x = div x 10 |
| 68 | k = add k 1 |
| 69 | if eq x 0 |
| 70 | break |
| 71 | end |
| 72 | end |
| 73 | for j ulong k |
| 74 | l_b s get gst sub sub k j 1 |
| 75 | end |
| 76 | end |
| 77 | |
| 78 | fn l_ts s edit uchar * v ulong to void |
| 79 | ts_fmt v |
| 80 | l_b s 34 |
| 81 | for i ulong 20 |
| 82 | l_b s get gtb i |
| 83 | end |
| 84 | l_b s 34 |
| 85 | end |
| 86 | |
| 87 | fn l_wd s edit uchar * w ulong to void |
| 88 | l_b s 34 |
| 89 | p = tw_off wall w |
| 90 | for i ulong tw_len wall w |
| 91 | l_b s get wall add p i |
| 92 | end |
| 93 | l_b s 34 |
| 94 | end |
| 95 | |
| 96 | fn l_id s edit uchar * v ulong to void |
| 97 | for i ulong 16 |
| 98 | l_b s hexd and shr v sub 60 mul i 4 15 |
| 99 | end |
| 100 | end |
| 101 | |
| 102 | note ev_hash: sha256(tag ++ s[0..hp) ++ "}") as lowercase hex into s[hp+9 .. hp+73) |
| 103 | fn ev_hash s edit uchar * hp ulong to void |
| 104 | c = new ulong 40 0 |
| 105 | d = new uchar 32 0 |
| 106 | sha256_init c |
| 107 | sha256_update c mkid |
| 108 | sha256_update c view part s 0 hp |
| 109 | sha256_update c tbrace |
| 110 | sha256_final c d |
| 111 | q = add hp 9 |
| 112 | for i ulong 32 |
| 113 | put s add q mul i 2 hexd ulong shr get d i 4 |
| 114 | put s add add q mul i 2 1 hexd ulong and get d i 15 |
| 115 | end |
| 116 | end |
| 117 | |
| 118 | note ev_body: writes the event body (through prev) at s[0..); returns hp. seq = xseq + 1. |
| 119 | fn ev_body s edit uchar * to ulong |
| 120 | put gx xlp 0 |
| 121 | l_t s kv |
| 122 | l_u s add get gx xseq 1 |
| 123 | l_t s kts |
| 124 | l_ts s get gx xnow |
| 125 | l_t s kop |
| 126 | l_wd s get gev eop |
| 127 | l_t s kid |
| 128 | l_b s 34 |
| 129 | put gev eidp get gx xlp |
| 130 | l_id s get gev eid |
| 131 | l_b s 34 |
| 132 | l_t s kag |
| 133 | l_arq s get gev eag get gev eagl |
| 134 | l_t s kse |
| 135 | l_arq s get gev ese get gev esel |
| 136 | l_t s kkind |
| 137 | l_wd s add get gev ekind 13 |
| 138 | l_t s ktg |
| 139 | l_arq s get gev etg get gev etgl |
| 140 | l_t s kmode |
| 141 | l_wd s add get gev emode 18 |
| 142 | l_t s kttl |
| 143 | l_u s get gev ettl |
| 144 | l_t s kexp |
| 145 | l_ts s get gev eexp |
| 146 | l_t s kst |
| 147 | l_wd s add get gev est 5 |
| 148 | l_t s kin |
| 149 | l_arq s get gev ein get gev einl |
| 150 | l_t s kev |
| 151 | l_ar s get gev eev get gev eevl |
| 152 | l_b s 93 |
| 153 | l_t s kor |
| 154 | l_arq s get gev eor get gev eorl |
| 155 | l_t s kprev |
| 156 | l_b s 34 |
| 157 | for i ulong 64 |
| 158 | l_b s get glh i |
| 159 | end |
| 160 | l_b s 34 |
| 161 | ret get gx xlp |
| 162 | end |
| 163 | |
| 164 | note ev_build: complete line (sig = 128 zeros, patched at flush); for op=claim the id is the first 16 |
| 165 | note hex of the hash of the body with id 0000000000000000. Returns the line length. |
| 166 | fn ev_build s edit uchar * to ulong |
| 167 | if eq get gev eop 0 |
| 168 | put gev eid 0 |
| 169 | end |
| 170 | hp = ev_body s |
| 171 | put gx xlp hp |
| 172 | l_t s khash |
| 173 | l_b s 34 |
| 174 | for i ulong 64 |
| 175 | l_b s 48 |
| 176 | end |
| 177 | l_b s 34 |
| 178 | l_t s ksig |
| 179 | l_b s 34 |
| 180 | for j ulong 128 |
| 181 | l_b s 48 |
| 182 | end |
| 183 | l_b s 34 |
| 184 | l_b s 125 |
| 185 | n = get gx xlp |
| 186 | if eq get gev eop 0 |
| 187 | ev_hash s hp |
| 188 | put gev eid hx_u s add hp 9 |
| 189 | p = get gev eidp |
| 190 | for k ulong 16 |
| 191 | put s add p k get s add add hp 9 k |
| 192 | end |
| 193 | end |
| 194 | ev_hash s hp |
| 195 | ret n |
| 196 | end |
| 197 | |
| 198 | note ======== pending batch (gpend, ends in gpe) ======== |
| 199 | |
| 200 | fn pend_add s edit uchar * n ulong to void |
| 201 | p = get gx xpen |
| 202 | k = get gx xpec |
| 203 | if or gt add add p n 1 pecap ge k 8192 |
| 204 | die 70 mapp |
| 205 | end |
| 206 | for i ulong n |
| 207 | put gpend add p i get s i |
| 208 | end |
| 209 | put gpend add p n 10 |
| 210 | e = add add p n 1 |
| 211 | put gpe k e |
| 212 | put gx xpen e |
| 213 | put gx xpec add k 1 |
| 214 | h = sub e 204 |
| 215 | for j ulong 64 |
| 216 | put glh j get gpend add h j |
| 217 | end |
| 218 | put gx xseq add get gx xseq 1 |
| 219 | end |
| 220 | |
| 221 | fn pe_room to bool |
| 222 | if ge get gx xpec 8192 |
| 223 | ret false |
| 224 | end |
| 225 | ret le add get gx xpen 70000 pecap |
| 226 | end |
| 227 | |
| 228 | fn pk_g off ulong n ulong to void |
| 229 | p = pk_rec n |
| 230 | for i ulong n |
| 231 | put gpk add p i get gpth add off i |
| 232 | end |
| 233 | end |
| 234 | |
| 235 | note pe_flush: sign every pending hash in one helper run, patch the signatures, then append each line |
| 236 | note with a single write() and fdsync. echo true also prints each appended line on stdout. |
| 237 | fn pe_flush echo bool to void |
| 238 | k = get gx xpec |
| 239 | if eq k 0 |
| 240 | ret |
| 241 | end |
| 242 | si_open |
| 243 | for i ulong k |
| 244 | h = sub get gpe i 204 |
| 245 | for j ulong 64 |
| 246 | si_b get gpend add h j |
| 247 | end |
| 248 | si_b 10 |
| 249 | end |
| 250 | pk_reset |
| 251 | pk_g get gx xsig get gx xsigl |
| 252 | pk_t tsign |
| 253 | pk_g get gx xpkey get gx xpkeyl |
| 254 | r = si_run |
| 255 | if ne r 0 |
| 256 | die 70 msig |
| 257 | end |
| 258 | if ne get gx xson mul k 129 |
| 259 | die 70 msig |
| 260 | end |
| 261 | for a ulong k |
| 262 | sp = sub get gpe a 131 |
| 263 | for b ulong 128 |
| 264 | c = get gso add mul a 129 b |
| 265 | if eq hexv c 16 |
| 266 | die 70 msig |
| 267 | end |
| 268 | put gpend add sp b c |
| 269 | end |
| 270 | if ne get gso add mul a 129 128 10 |
| 271 | die 70 msig |
| 272 | end |
| 273 | end |
| 274 | fd = fopen gpth get gx xpled get gx xpledl 525377 420 |
| 275 | if lt fd 0 |
| 276 | die 70 mapp |
| 277 | end |
| 278 | st = ulong 0 |
| 279 | for e ulong k |
| 280 | en = get gpe e |
| 281 | p = st |
| 282 | while lt p en bound 999999 |
| 283 | w = write int fd gpend p sub en p |
| 284 | if le w 0 |
| 285 | die 70 mapp |
| 286 | end |
| 287 | p = add p ulong w |
| 288 | end |
| 289 | if echo |
| 290 | for x ulong sub en st |
| 291 | o_b get gpend add st x |
| 292 | end |
| 293 | end |
| 294 | st = en |
| 295 | end |
| 296 | y = fdsync int fd |
| 297 | if ne y 0 |
| 298 | die 70 mapp |
| 299 | end |
| 300 | z = fclose int fd |
| 301 | put gx xpen 0 |
| 302 | put gx xpec 0 |
| 303 | end |
| 304 | |
| 305 | note lk_take: open claims.lock and flock it (1 shared, 2 exclusive); held until exit |
| 306 | fn lk_take op int to void |
| 307 | fd = fopen gpth get gx xplk get gx xplkl 524354 420 |
| 308 | if eq fd -2 |
| 309 | if eq op 1 |
| 310 | ret |
| 311 | end |
| 312 | die 70 mhome |
| 313 | end |
| 314 | if lt fd 0 |
| 315 | die 70 mlock |
| 316 | end |
| 317 | r = flock int fd op |
| 318 | if ne r 0 |
| 319 | die 70 mlock |
| 320 | end |
| 321 | put gx xlkfd ulong fd |
| 322 | end |