interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / src / fabric / fab_loopback.om

362 lines · 8394 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note Copyright 2026 Drake Stapleton (AIEN / aien-dev)
3note
4note src/fabric/fab_loopback.om -- in-process loopback transport and HMAC
5note stand-in authenticator for Fabric F5-0 (test and simulation only).
6note Composed after seed/ng/sha256.om, src/runtime/aien_machine_id.om, and
7note src/fabric/fab_hmac.om.
8
9const FAB_LOOP_NODES ulong 8
10const FAB_LOOP_DEPTH ulong 64
11const FAB_MSG_MAX ulong 372
12const FAB_HMAC_MAX_PEERS ulong 64
13
14const FAB_OK int 0
15const FAB_E_ARG int -1
16const FAB_E_FORMAT int -2
17const FAB_E_MISMATCH int -3
18const FAB_E_NOT_ENROLLED int -4
19const FAB_E_AUTH int -5
20const FAB_E_STALE_GEN int -6
21const FAB_E_REPLAY int -7
22const FAB_E_LEASE_EXPIRED int -8
23const FAB_E_NOT_MEMBER int -9
24const FAB_E_ONTOLOGY int -10
25const FAB_E_CAPQ int -11
26const FAB_E_FULL int -12
27const FAB_E_TRANSPORT int -13
28const FAB_E_GENERATION int -14
29const FAB_E_STATE int -15
30
31note Loopback bus state
32global g_fl_n ulong 1
33global g_fl_box_id uchar 264
34global g_fl_box_silent int 8
35global g_fl_box_head ulong 8
36global g_fl_box_count ulong 8
37global g_fl_q_len ulong 512
38global g_fl_q_buf uchar 190464
39global g_fl_tr_buf uchar 65536
40global g_fl_tr_len ulong 1
41global g_fl_sent ulong 1
42global g_fl_dropped ulong 1
43global g_fl_last_len ulong 1
44global g_fl_last_buf uchar 372
45
46note HMAC authenticator tables (up to 8 nodes, up to 64 peers each)
47global g_fa_self uchar 264
48global g_fa_self_key uchar 256
49global g_fa_n ulong 8
50global g_fa_peer_ids uchar 16896
51global g_fa_peer_keys uchar 16384
52
53fn 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
68end
69
70fn 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
86end
87
88fn 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
103end
104
105fn 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
115end
116
117fn 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
131end
132
133fn 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
147end
148
149fn 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
154end
155
156fn 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
162end
163
164fn 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
186end
187
188fn 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
211end
212
213fn 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
220end
221
222fn 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
231end
232
233fn 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
244end
245
246fn 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
253end
254
255fn 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
269end
270
271fn 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
289end
290
291fn 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
304end
305
306fn 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
316end
317
318fn 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
328end
329
330fn 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
339end
340
341fn 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
362end