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 / src / fabric / fab_loopback.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note src/fabric/fab_loopback.om -- in-process loopback transport and HMAC |
| 5 | note stand-in authenticator for Fabric F5-0 (test and simulation only). |
| 6 | note Composed after seed/ng/sha256.om, src/runtime/aien_machine_id.om, and |
| 7 | note src/fabric/fab_hmac.om. |
| 8 | |
| 9 | const FAB_LOOP_NODES ulong 8 |
| 10 | const FAB_LOOP_DEPTH ulong 64 |
| 11 | const FAB_MSG_MAX ulong 372 |
| 12 | const FAB_HMAC_MAX_PEERS ulong 64 |
| 13 | |
| 14 | const FAB_OK int 0 |
| 15 | const FAB_E_ARG int -1 |
| 16 | const FAB_E_FORMAT int -2 |
| 17 | const FAB_E_MISMATCH int -3 |
| 18 | const FAB_E_NOT_ENROLLED int -4 |
| 19 | const FAB_E_AUTH int -5 |
| 20 | const FAB_E_STALE_GEN int -6 |
| 21 | const FAB_E_REPLAY int -7 |
| 22 | const FAB_E_LEASE_EXPIRED int -8 |
| 23 | const FAB_E_NOT_MEMBER int -9 |
| 24 | const FAB_E_ONTOLOGY int -10 |
| 25 | const FAB_E_CAPQ int -11 |
| 26 | const FAB_E_FULL int -12 |
| 27 | const FAB_E_TRANSPORT int -13 |
| 28 | const FAB_E_GENERATION int -14 |
| 29 | const FAB_E_STATE int -15 |
| 30 | |
| 31 | note Loopback bus state |
| 32 | global g_fl_n ulong 1 |
| 33 | global g_fl_box_id uchar 264 |
| 34 | global g_fl_box_silent int 8 |
| 35 | global g_fl_box_head ulong 8 |
| 36 | global g_fl_box_count ulong 8 |
| 37 | global g_fl_q_len ulong 512 |
| 38 | global g_fl_q_buf uchar 190464 |
| 39 | global g_fl_tr_buf uchar 65536 |
| 40 | global g_fl_tr_len ulong 1 |
| 41 | global g_fl_sent ulong 1 |
| 42 | global g_fl_dropped ulong 1 |
| 43 | global g_fl_last_len ulong 1 |
| 44 | global g_fl_last_buf uchar 372 |
| 45 | |
| 46 | note HMAC authenticator tables (up to 8 nodes, up to 64 peers each) |
| 47 | global g_fa_self uchar 264 |
| 48 | global g_fa_self_key uchar 256 |
| 49 | global g_fa_n ulong 8 |
| 50 | global g_fa_peer_ids uchar 16896 |
| 51 | global g_fa_peer_keys uchar 16384 |
| 52 | |
| 53 | fn fl_box_find mid view uchar 33 to int |
| 54 | n = get g_fl_n 0 |
| 55 | for i ulong n bound 8 |
| 56 | base = mul i 33 |
| 57 | same = int 1 |
| 58 | for j ulong 32 |
| 59 | if ne get g_fl_box_id add add base 1 j get mid add 1 j |
| 60 | same = int 0 |
| 61 | end |
| 62 | end |
| 63 | if ne same 0 |
| 64 | ret int i |
| 65 | end |
| 66 | end |
| 67 | ret int -1 |
| 68 | end |
| 69 | |
| 70 | fn fl_box_push bidx ulong msg view uchar * to int |
| 71 | cnt = get g_fl_box_count bidx |
| 72 | mlen = len msg |
| 73 | if or ge cnt 64 gt mlen 372 |
| 74 | ret FAB_E_TRANSPORT |
| 75 | end |
| 76 | hd = get g_fl_box_head bidx |
| 77 | slot = rem add hd cnt 64 |
| 78 | qidx = add mul bidx 64 slot |
| 79 | put g_fl_q_len qidx mlen |
| 80 | base = mul qidx 372 |
| 81 | for i ulong mlen bound 372 |
| 82 | put g_fl_q_buf add base i get msg i |
| 83 | end |
| 84 | put g_fl_box_count bidx add cnt 1 |
| 85 | ret FAB_OK |
| 86 | end |
| 87 | |
| 88 | fn fl_tr_append_hdr from_id view uchar 33 to_id view uchar 33 mlen ulong to void |
| 89 | pos = get g_fl_tr_len 0 |
| 90 | if gt add pos 68 65536 |
| 91 | ret |
| 92 | end |
| 93 | for i ulong 32 |
| 94 | put g_fl_tr_buf add pos i get from_id add 1 i |
| 95 | end |
| 96 | for i ulong 32 |
| 97 | put g_fl_tr_buf add add pos 32 i get to_id add 1 i |
| 98 | end |
| 99 | for i ulong 4 |
| 100 | put g_fl_tr_buf add add pos 64 i uchar and shr mlen mul i 8 255 |
| 101 | end |
| 102 | put g_fl_tr_len 0 add pos 68 |
| 103 | end |
| 104 | |
| 105 | fn fl_tr_append_msg msg view uchar * to void |
| 106 | pos = get g_fl_tr_len 0 |
| 107 | mlen = len msg |
| 108 | if gt add pos mlen 65536 |
| 109 | ret |
| 110 | end |
| 111 | for i ulong mlen bound 372 |
| 112 | put g_fl_tr_buf add pos i get msg i |
| 113 | end |
| 114 | put g_fl_tr_len 0 add pos mlen |
| 115 | end |
| 116 | |
| 117 | fn fab_loop_init to void |
| 118 | put g_fl_n 0 0 |
| 119 | put g_fl_tr_len 0 0 |
| 120 | put g_fl_sent 0 0 |
| 121 | put g_fl_dropped 0 0 |
| 122 | put g_fl_last_len 0 0 |
| 123 | for i ulong 8 |
| 124 | put g_fl_box_silent i 0 |
| 125 | put g_fl_box_head i 0 |
| 126 | put g_fl_box_count i 0 |
| 127 | end |
| 128 | for i ulong 264 |
| 129 | put g_fl_box_id i uchar 0 |
| 130 | end |
| 131 | end |
| 132 | |
| 133 | fn fab_loop_add mid view uchar 33 to int |
| 134 | if ge fl_box_find view mid 0 |
| 135 | ret FAB_E_ARG |
| 136 | end |
| 137 | n = get g_fl_n 0 |
| 138 | if ge n 8 |
| 139 | ret FAB_E_FULL |
| 140 | end |
| 141 | base = mul n 33 |
| 142 | for i ulong 33 |
| 143 | put g_fl_box_id add base i get mid i |
| 144 | end |
| 145 | put g_fl_n 0 add n 1 |
| 146 | ret FAB_OK |
| 147 | end |
| 148 | |
| 149 | fn fab_loop_silence mid view uchar 33 silent int to void |
| 150 | idx = fl_box_find view mid |
| 151 | if ge idx 0 |
| 152 | put g_fl_box_silent ulong idx silent |
| 153 | end |
| 154 | end |
| 155 | |
| 156 | fn fab_loop_inject to_id view uchar 33 msg view uchar * to int |
| 157 | idx = fl_box_find view to_id |
| 158 | if lt idx 0 |
| 159 | ret FAB_E_TRANSPORT |
| 160 | end |
| 161 | ret fl_box_push ulong idx view msg |
| 162 | end |
| 163 | |
| 164 | fn fab_loop_send from_id view uchar 33 to_id view uchar 33 msg view uchar * to int |
| 165 | sidx = fl_box_find view from_id |
| 166 | didx = fl_box_find view to_id |
| 167 | mlen = len msg |
| 168 | if or lt sidx 0 lt didx 0 |
| 169 | ret FAB_E_TRANSPORT |
| 170 | end |
| 171 | if gt mlen 372 |
| 172 | ret FAB_E_TRANSPORT |
| 173 | end |
| 174 | put g_fl_last_len 0 mlen |
| 175 | for i ulong mlen bound 372 |
| 176 | put g_fl_last_buf i get msg i |
| 177 | end |
| 178 | if ne get g_fl_box_silent ulong sidx 0 |
| 179 | put g_fl_dropped 0 add get g_fl_dropped 0 1 |
| 180 | ret FAB_OK |
| 181 | end |
| 182 | fl_tr_append_hdr view from_id view to_id mlen |
| 183 | fl_tr_append_msg view msg |
| 184 | put g_fl_sent 0 add get g_fl_sent 0 1 |
| 185 | ret fl_box_push ulong didx view msg |
| 186 | end |
| 187 | |
| 188 | fn fab_loop_recv self_id view uchar 33 buf edit uchar * to int |
| 189 | idx = fl_box_find view self_id |
| 190 | if lt idx 0 |
| 191 | ret FAB_E_TRANSPORT |
| 192 | end |
| 193 | bidx = ulong idx |
| 194 | cnt = get g_fl_box_count bidx |
| 195 | if eq cnt 0 |
| 196 | ret 0 |
| 197 | end |
| 198 | hd = get g_fl_box_head bidx |
| 199 | qidx = add mul bidx 64 hd |
| 200 | mlen = get g_fl_q_len qidx |
| 201 | if gt mlen len buf |
| 202 | ret FAB_E_TRANSPORT |
| 203 | end |
| 204 | base = mul qidx 372 |
| 205 | for i ulong mlen bound 372 |
| 206 | put buf i get g_fl_q_buf add base i |
| 207 | end |
| 208 | put g_fl_box_head bidx rem add hd 1 64 |
| 209 | put g_fl_box_count bidx sub cnt 1 |
| 210 | ret int mlen |
| 211 | end |
| 212 | |
| 213 | fn fab_loop_pending to ulong |
| 214 | p = ulong 0 |
| 215 | n = get g_fl_n 0 |
| 216 | for i ulong n bound 8 |
| 217 | p = add p get g_fl_box_count i |
| 218 | end |
| 219 | ret p |
| 220 | end |
| 221 | |
| 222 | fn fab_loop_transcript dst edit uchar 32 to void |
| 223 | tlen = get g_fl_tr_len 0 |
| 224 | for i ulong tlen bound 65536 |
| 225 | put sh_dat i get g_fl_tr_buf i |
| 226 | end |
| 227 | sh_all tlen |
| 228 | for i ulong 32 |
| 229 | put dst i get sh_dig i |
| 230 | end |
| 231 | end |
| 232 | |
| 233 | fn fl_q_match_rec qidx ulong kind ulong rec view uchar 44 to int |
| 234 | base = mul qidx 372 |
| 235 | if ne ulong get g_fl_q_buf add base 5 kind |
| 236 | ret 0 |
| 237 | end |
| 238 | for j ulong 44 |
| 239 | if ne get g_fl_q_buf add add base 8 j get rec j |
| 240 | ret 0 |
| 241 | end |
| 242 | end |
| 243 | ret 1 |
| 244 | end |
| 245 | |
| 246 | fn fl_q_copy qidx ulong dst edit uchar 372 to ulong |
| 247 | mlen = get g_fl_q_len qidx |
| 248 | base = mul qidx 372 |
| 249 | for j ulong mlen bound 372 |
| 250 | put dst j get g_fl_q_buf add base j |
| 251 | end |
| 252 | ret mlen |
| 253 | end |
| 254 | |
| 255 | fn fab_loop_peek bidx ulong from_id view uchar 33 kind ulong dst edit uchar 372 to ulong |
| 256 | rec = new uchar 44 0 |
| 257 | if ne aien_mid_encode view from_id edit rec 0 |
| 258 | ret 0 |
| 259 | end |
| 260 | for k ulong get g_fl_box_count bidx bound 64 |
| 261 | slot = add get g_fl_box_head bidx k |
| 262 | slot = rem slot 64 |
| 263 | qidx = add mul bidx 64 slot |
| 264 | if ne fl_q_match_rec qidx kind view rec 0 |
| 265 | ret fl_q_copy qidx edit dst |
| 266 | end |
| 267 | end |
| 268 | ret 0 |
| 269 | end |
| 270 | |
| 271 | fn fab_hmac_auth_init aid ulong self_id view uchar 33 skey view uchar 32 n ulong to int |
| 272 | if ge aid 8 |
| 273 | ret FAB_E_ARG |
| 274 | end |
| 275 | put g_fa_n aid 0 |
| 276 | if or eq n 0 gt n 64 |
| 277 | ret FAB_E_ARG |
| 278 | end |
| 279 | sbase = mul aid 33 |
| 280 | for i ulong 33 |
| 281 | put g_fa_self add sbase i get self_id i |
| 282 | end |
| 283 | kbase = mul aid 32 |
| 284 | for i ulong 32 |
| 285 | put g_fa_self_key add kbase i get skey i |
| 286 | end |
| 287 | put g_fa_n aid n |
| 288 | ret FAB_OK |
| 289 | end |
| 290 | |
| 291 | fn fab_hmac_auth_set_peer aid ulong pidx ulong pid view uchar 33 pkey view uchar 32 to void |
| 292 | if or ge aid 8 ge pidx 64 |
| 293 | ret |
| 294 | end |
| 295 | slot = add mul aid 64 pidx |
| 296 | ibase = mul slot 33 |
| 297 | for i ulong 33 |
| 298 | put g_fa_peer_ids add ibase i get pid i |
| 299 | end |
| 300 | kbase = mul slot 32 |
| 301 | for i ulong 32 |
| 302 | put g_fa_peer_keys add kbase i get pkey i |
| 303 | end |
| 304 | end |
| 305 | |
| 306 | fn fab_hmac_sign aid ulong msg view uchar * sig edit uchar 64 to int |
| 307 | if ge aid 8 |
| 308 | ret sub int 0 1 |
| 309 | end |
| 310 | skey = new uchar 32 0 |
| 311 | kbase = mul aid 32 |
| 312 | for i ulong 32 |
| 313 | put skey i get g_fa_self_key add kbase i |
| 314 | end |
| 315 | ret fab_hmac_sig64 view skey view msg edit sig |
| 316 | end |
| 317 | |
| 318 | fn fa_peer_eq slot ulong claimed view uchar 33 to int |
| 319 | ibase = add mul slot 33 1 |
| 320 | for j ulong 32 |
| 321 | b1 = get g_fa_peer_ids add ibase j |
| 322 | b2 = get claimed add 1 j |
| 323 | if ne b1 b2 |
| 324 | ret 0 |
| 325 | end |
| 326 | end |
| 327 | ret 1 |
| 328 | end |
| 329 | |
| 330 | fn fa_find_peer aid ulong claimed view uchar 33 to int |
| 331 | n = get g_fa_n aid |
| 332 | for i ulong n bound 64 |
| 333 | slot = add mul aid 64 i |
| 334 | if ne fa_peer_eq slot view claimed 0 |
| 335 | ret int slot |
| 336 | end |
| 337 | end |
| 338 | ret int -1 |
| 339 | end |
| 340 | |
| 341 | fn fab_hmac_verify aid ulong claimed view uchar 33 msg view uchar * sig view uchar 64 to int |
| 342 | if ge aid 8 |
| 343 | ret sub int 0 1 |
| 344 | end |
| 345 | slot = fa_find_peer aid view claimed |
| 346 | if lt slot 0 |
| 347 | ret sub int 0 1 |
| 348 | end |
| 349 | pkey = new uchar 32 0 |
| 350 | t = new uchar 64 0 |
| 351 | kbase = mul ulong slot 32 |
| 352 | for j ulong 32 |
| 353 | put pkey j get g_fa_peer_keys add kbase j |
| 354 | end |
| 355 | if ne fab_hmac_sig64 view pkey view msg edit t 0 |
| 356 | ret sub int 0 1 |
| 357 | end |
| 358 | if ne fab_ct_equal64 view t view sig 0 |
| 359 | ret 0 |
| 360 | end |
| 361 | ret sub int 0 1 |
| 362 | end |