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 / apps / omega-mesh / mesh_route.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note apps/omega-mesh/mesh_route.om |
| 3 | note Sovereign Intelligence 2.0 Stage 5 (Pillar 5: Zero-Trust Personal Silicon Mesh, DRA-1073) |
| 4 | note |
| 5 | note Pure seeded OM-2 Compile-on-Arrive Mesh Router, heterogeneous TargetDesc + WattCBF |
| 6 | note offload profitability solver, OMSH v1 signed wire framing, loopback-only TCP socket |
| 7 | note transport, and deterministic pure Compute-on-Arrive worker/client engine. |
| 8 | note |
| 9 | note HARD SAFETY INVARIANTS: |
| 10 | note - Zero GPU device opens, zero UEFI/SPI touches, zero sysfs writes (hw_writes == 0). |
| 11 | note - Wildcard 0.0.0.0 bind/endpoint is STRICTLY REFUSED fail-closed (loopback 127.0.0.1 |
| 12 | note or explicit non-zero WireGuard/LAN unicast endpoints only). |
| 13 | note - Remote outputs enter OMEGA-MEMO ONLY through mesh_verify.om's fail-closed gate. |
| 14 | note |
| 15 | note Library unit (no om2 header, no fn main; prefix mr_ / MR_). |
| 16 | note Link directly after apps/omega-mesh/mesh_verify.om. |
| 17 | |
| 18 | note Heterogeneous personal-mesh TargetDesc IDs (extending target_desc.om slots 10..12) |
| 19 | const MR_TID_MACBOOK ulong 10 |
| 20 | const MR_TID_STEAMDECK ulong 11 |
| 21 | const MR_TID_PC ulong 12 |
| 22 | |
| 23 | note Node roles |
| 24 | const MR_ROLE_MACBOOK ulong 1 |
| 25 | const MR_ROLE_STEAMDECK ulong 2 |
| 26 | const MR_ROLE_PC ulong 3 |
| 27 | const MR_ROLE_SPARK_CPU ulong 4 |
| 28 | const MR_ROLE_SPARK_GPU ulong 5 |
| 29 | |
| 30 | note Capacities, IPv4 constants & protocol framing |
| 31 | const MR_NODE_CAP ulong 16 |
| 32 | const MR_NONE ulong 999999 |
| 33 | const MR_IPV4_LOOPBACK ulong 2130706433 |
| 34 | const MR_OFFLOAD_MARGIN_US ulong 200 |
| 35 | const MR_OMSH_HDR_LEN ulong 32 |
| 36 | const MR_OMSH_VER ulong 1 |
| 37 | const MR_MSG_DISPATCH_REQ ulong 1 |
| 38 | const MR_MSG_DISPATCH_RESP ulong 2 |
| 39 | const MR_MSG_ERROR ulong 3 |
| 40 | const MR_MAX_FRAME_PAYLOAD ulong 32768 |
| 41 | const MR_MAX_WIRE ulong 33024 |
| 42 | const MR_MAX_INPUT_LEN ulong 16384 |
| 43 | const MR_REQ_FIXED_HDR ulong 96 |
| 44 | const MR_RESP_FIXED_HDR ulong 304 |
| 45 | const MR_FLAG_TIER_B ulong 1 |
| 46 | const MR_FLAG_MERKLE ulong 2 |
| 47 | const MR_FLAG_FREIVALDS ulong 4 |
| 48 | |
| 49 | note Pure Compute-on-Arrive workload kinds |
| 50 | const MR_WORK_POLY_PURE ulong 1 |
| 51 | const MR_WORK_MATMUL_INT64 ulong 2 |
| 52 | const MR_WORK_MERKLE_STEPS ulong 3 |
| 53 | |
| 54 | note Routing decisions & fail-closed status codes |
| 55 | const MR_OK ulong 0 |
| 56 | const MR_DEC_MEMO_HIT ulong 1 |
| 57 | const MR_DEC_LOCAL ulong 2 |
| 58 | const MR_DEC_REMOTE ulong 3 |
| 59 | const MR_ERR_NO_NODE ulong 10 |
| 60 | const MR_ERR_BAD_OMPK ulong 11 |
| 61 | const MR_ERR_WILDCARD_BIND ulong 12 |
| 62 | const MR_ERR_BAD_NODE ulong 13 |
| 63 | const MR_ERR_UNAUTH_PEER ulong 14 |
| 64 | const MR_ERR_FRAME_MAGIC ulong 15 |
| 65 | const MR_ERR_FRAME_VER ulong 16 |
| 66 | const MR_ERR_FRAME_LEN ulong 17 |
| 67 | const MR_ERR_FRAME_TYPE ulong 18 |
| 68 | const MR_ERR_FRAME_SIG ulong 19 |
| 69 | const MR_ERR_SOCK ulong 20 |
| 70 | const MR_ERR_IO ulong 21 |
| 71 | const MR_ERR_TIMEOUT ulong 22 |
| 72 | const MR_ERR_REMOTE_REFUSED ulong 23 |
| 73 | const MR_ERR_VERIFY_FAILED ulong 24 |
| 74 | const MR_ERR_MEMO_FAILED ulong 25 |
| 75 | const MR_ERR_IMPURE ulong 26 |
| 76 | |
| 77 | note Worker test fault-injection modes (0 = honest production) |
| 78 | const MR_FAULT_NONE ulong 0 |
| 79 | const MR_FAULT_FLIP_OUT_BEFORE_SIGN ulong 1 |
| 80 | const MR_FAULT_FORGE_SIG ulong 2 |
| 81 | const MR_FAULT_UNKNOWN_KEY ulong 3 |
| 82 | const MR_FAULT_MATMUL_CORRUPT ulong 4 |
| 83 | const MR_FAULT_MERKLE_CHUNK11 ulong 5 |
| 84 | const MR_FAULT_WRONG_INPUT_HASH ulong 6 |
| 85 | |
| 86 | note Heterogeneous mesh node table (slot 0 = local device, slots 1..15 = remote peers) |
| 87 | global mr_n_used ulong 16 |
| 88 | global mr_n_tid ulong 16 |
| 89 | global mr_n_feat ulong 16 |
| 90 | global mr_n_role ulong 16 |
| 91 | global mr_n_tier ulong 16 |
| 92 | global mr_n_ipv4 ulong 16 |
| 93 | global mr_n_port ulong 16 |
| 94 | global mr_n_rtt_us ulong 16 |
| 95 | global mr_n_bw_mbps ulong 16 |
| 96 | global mr_n_speed ulong 16 |
| 97 | global mr_n_online ulong 16 |
| 98 | global mr_n_tdie long 16 |
| 99 | global mr_n_tsink long 16 |
| 100 | global mr_n_tamb long 16 |
| 101 | global mr_n_pbud long 16 |
| 102 | global mr_n_pgbud long 16 |
| 103 | global mr_n_tceil long 16 |
| 104 | global mr_n_tau long 16 |
| 105 | global mr_n_bcpu long 16 |
| 106 | global mr_n_bunc long 16 |
| 107 | global mr_n_pk uchar 512 |
| 108 | |
| 109 | note Staging arrays for node registration |
| 110 | note mr_st_u: [0]=slot [1]=tid [2]=feat [3]=role [4]=tier [5]=ipv4 |
| 111 | note [6]=port [7]=rtt_us [8]=bw_mbps [9]=speed [10]=online |
| 112 | global mr_st_u ulong 12 |
| 113 | note mr_st_i: [0]=tdie [1]=tsink [2]=tamb [3]=pbud [4]=pgbud |
| 114 | note [5]=tceil [6]=tau [7]=bcpu [8]=bunc |
| 115 | global mr_st_i long 10 |
| 116 | |
| 117 | note Routing decision telemetry: |
| 118 | note [0]=decision [1]=sel_slot [2]=sel_tid [3]=local_us [4]=remote_us |
| 119 | note [5]=best_rtt_us [6]=best_xfer_us [7]=local_wc_ok [8]=remote_wc_ok |
| 120 | note [9]=rej_contract [10]=rej_wattcbf [11]=rej_unauth [12]=rej_deadline |
| 121 | note [13]=sel_p_safe_mw [14]=sel_barrier_h |
| 122 | global mr_route_out ulong 16 |
| 123 | |
| 124 | note Last WattCBF probe result: [0]=ok [1]=p_safe_mw [2]=barrier_h [3]=fail_closed |
| 125 | global mr_wc_out long 4 |
| 126 | |
| 127 | note OMSH v1 wire framing, payload, socket, and identity buffers |
| 128 | global mr_wire uchar 33024 |
| 129 | global mr_wire_len ulong 1 |
| 130 | global mr_in_buf uchar 16384 |
| 131 | global mr_in_len ulong 1 |
| 132 | global mr_aux_buf uchar 4096 |
| 133 | global mr_aux_len ulong 1 |
| 134 | global mr_sockaddr uchar 16 |
| 135 | global mr_sopt uchar 4 |
| 136 | note mr_req_meta: [0]=flags [1]=nonce [2]=epoch [3]=ompk_len [4]=in_len |
| 137 | note [5]=work_kind [6]=prec_tier [7]=err_bound [8]=target_tid |
| 138 | note [9]=resp_status [10]=resp_out_len [11]=resp_aux_len |
| 139 | global mr_req_meta ulong 12 |
| 140 | global mr_req_cli_pk uchar 32 |
| 141 | global mr_req_in_sha uchar 32 |
| 142 | global mr_cli_seed uchar 32 |
| 143 | global mr_cli_pk uchar 32 |
| 144 | global mr_wrk_seed uchar 32 |
| 145 | global mr_wrk_pk uchar 32 |
| 146 | note mr_net_stats: [0]=net_dispatches [1]=memo_hits [2]=local_execs [3]=remote_execs |
| 147 | note [4]=frames_sent [5]=frames_recv [6]=last_verify_rc [7]=last_bound_port |
| 148 | global mr_net_stats ulong 8 |
| 149 | global mr_poly_tmp uchar 32 |
| 150 | |
| 151 | text mr_omsh_magic "OMSH" |
| 152 | text mr_poly_tag "OM-MESH-POLY-V1:" |
| 153 | text mr_rogue_tag "OM-MESH-ROGUE-TEST-KEY" |
| 154 | |
| 155 | note ---- Initialization & Heterogeneous TargetDesc Registration ---- |
| 156 | |
| 157 | fn mr_register_mesh_targets to void |
| 158 | tdInitAll |
| 159 | tdCacheInit |
| 160 | note 10: MacBook Apple Silicon (a64macos: 8-wide OOO, 128b NEON, SHA/AES/BF16, battery thermal cap) |
| 161 | tdRegIsa MR_TID_MACBOOK ISA_A64 EXEC_SCALAR_SIMD 64 30 32 |
| 162 | tdRegVec MR_TID_MACBOOK 0 128 0 0 4 |
| 163 | tdRegCache MR_TID_MACBOOK 128 4096 128 0 |
| 164 | tdRegLat MR_TID_MACBOOK 8 2 7 10 |
| 165 | tdRegFeat MR_TID_MACBOOK 399 0 0 0 262143 |
| 166 | note 11: Steam Deck (x86linux: Zen 2 AVX2 APU, 4-wide OOO, 15W handheld thermal cap) |
| 167 | tdRegIsa MR_TID_STEAMDECK ISA_X64 EXEC_SCALAR_SIMD 64 15 16 |
| 168 | tdRegVec MR_TID_STEAMDECK 0 256 0 1 8 |
| 169 | tdRegCache MR_TID_STEAMDECK 32 512 64 0 |
| 170 | tdRegLat MR_TID_STEAMDECK 4 3 14 14 |
| 171 | tdRegFeat MR_TID_STEAMDECK 913 0 0 0 1983 |
| 172 | note 12: Personal PC (x86linux: AVX-512 workstation, 6-wide OOO, 95W thermal cap) |
| 173 | tdRegIsa MR_TID_PC ISA_X64 EXEC_SCALAR_SIMD 64 15 32 |
| 174 | tdRegVec MR_TID_PC 8 512 0 1 16 |
| 175 | tdRegCache MR_TID_PC 48 2048 64 0 |
| 176 | tdRegLat MR_TID_PC 6 3 14 14 |
| 177 | tdRegFeat MR_TID_PC 953 MMA_161616_AMX 0 0 1983 |
| 178 | put tdCount 0 13 |
| 179 | end |
| 180 | |
| 181 | fn mr_init to void |
| 182 | mr_register_mesh_targets |
| 183 | for i ulong MR_NODE_CAP |
| 184 | put mr_n_used i 0 |
| 185 | put mr_n_tid i 0 |
| 186 | put mr_n_feat i 0 |
| 187 | put mr_n_role i 0 |
| 188 | put mr_n_tier i 0 |
| 189 | put mr_n_ipv4 i 0 |
| 190 | put mr_n_port i 0 |
| 191 | put mr_n_rtt_us i 0 |
| 192 | put mr_n_bw_mbps i 0 |
| 193 | put mr_n_speed i 0 |
| 194 | put mr_n_online i 0 |
| 195 | put mr_n_tdie i 0 |
| 196 | put mr_n_tsink i 0 |
| 197 | put mr_n_tamb i 0 |
| 198 | put mr_n_pbud i 0 |
| 199 | put mr_n_pgbud i 0 |
| 200 | put mr_n_tceil i 0 |
| 201 | put mr_n_tau i 0 |
| 202 | put mr_n_bcpu i 0 |
| 203 | put mr_n_bunc i 0 |
| 204 | end |
| 205 | for i ulong 16 |
| 206 | put mr_route_out i 0 |
| 207 | end |
| 208 | for i ulong 12 |
| 209 | put mr_req_meta i 0 |
| 210 | end |
| 211 | for i ulong 8 |
| 212 | put mr_net_stats i 0 |
| 213 | end |
| 214 | put mr_wire_len 0 0 |
| 215 | put mr_in_len 0 0 |
| 216 | put mr_aux_len 0 0 |
| 217 | end |
| 218 | |
| 219 | note ---- Endpoint Safety Guards (0.0.0.0 Wildcard Strictly Refused) ---- |
| 220 | |
| 221 | fn mr_pack_ipv4 a ulong b ulong c ulong d ulong to ulong |
| 222 | p0 = shl and a 255 24 |
| 223 | p1 = shl and b 255 16 |
| 224 | p2 = shl and c 255 8 |
| 225 | p3 = and d 255 |
| 226 | ret or or p0 p1 or p2 p3 |
| 227 | end |
| 228 | |
| 229 | note Refuse 0.0.0.0 wildcard and broadcast; allow 127.0.0.1 or explicit private/WireGuard unicast |
| 230 | fn mr_ip_is_safe_endpoint ipv4 ulong to bool |
| 231 | if eq ipv4 0 |
| 232 | ret false |
| 233 | end |
| 234 | if eq ipv4 4294967295 |
| 235 | ret false |
| 236 | end |
| 237 | if eq ipv4 MR_IPV4_LOOPBACK |
| 238 | ret true |
| 239 | end |
| 240 | o0 = and shr ipv4 24 255 |
| 241 | o1 = and shr ipv4 16 255 |
| 242 | o3 = and ipv4 255 |
| 243 | if or eq o3 0 eq o3 255 |
| 244 | ret false |
| 245 | end |
| 246 | if eq o0 10 |
| 247 | ret true |
| 248 | end |
| 249 | if and eq o0 100 and ge o1 64 le o1 127 |
| 250 | ret true |
| 251 | end |
| 252 | if and eq o0 172 and ge o1 16 le o1 31 |
| 253 | ret true |
| 254 | end |
| 255 | if and eq o0 192 eq o1 168 |
| 256 | ret true |
| 257 | end |
| 258 | ret false |
| 259 | end |
| 260 | |
| 261 | note Bind guard: strictly refuses 0.0.0.0 wildcard; allows 127.0.0.1 or explicit WireGuard IP |
| 262 | fn mr_ip_is_safe_bind ipv4 ulong to bool |
| 263 | if eq ipv4 0 |
| 264 | ret false |
| 265 | end |
| 266 | if eq ipv4 MR_IPV4_LOOPBACK |
| 267 | ret true |
| 268 | end |
| 269 | o0 = and shr ipv4 24 255 |
| 270 | o1 = and shr ipv4 16 255 |
| 271 | o3 = and ipv4 255 |
| 272 | if or eq o3 0 eq o3 255 |
| 273 | ret false |
| 274 | end |
| 275 | if eq o0 10 |
| 276 | ret true |
| 277 | end |
| 278 | if and eq o0 100 and ge o1 64 le o1 127 |
| 279 | ret true |
| 280 | end |
| 281 | ret false |
| 282 | end |
| 283 | |
| 284 | note ---- Node Registration Helpers ---- |
| 285 | |
| 286 | fn mr_stage_node_id slot ulong tid ulong feat ulong role ulong tier ulong to void |
| 287 | put mr_st_u 0 slot |
| 288 | put mr_st_u 1 tid |
| 289 | put mr_st_u 2 feat |
| 290 | put mr_st_u 3 role |
| 291 | put mr_st_u 4 tier |
| 292 | end |
| 293 | |
| 294 | fn mr_stage_node_net ipv4 ulong port ulong rtt_us ulong bw_mbps ulong speed ulong online ulong to void |
| 295 | put mr_st_u 5 ipv4 |
| 296 | put mr_st_u 6 port |
| 297 | put mr_st_u 7 rtt_us |
| 298 | put mr_st_u 8 bw_mbps |
| 299 | put mr_st_u 9 speed |
| 300 | put mr_st_u 10 online |
| 301 | end |
| 302 | |
| 303 | fn mr_stage_node_therm tdie long tsink long tamb long bcpu long bunc long to void |
| 304 | put mr_st_i 0 tdie |
| 305 | put mr_st_i 1 tsink |
| 306 | put mr_st_i 2 tamb |
| 307 | put mr_st_i 7 bcpu |
| 308 | put mr_st_i 8 bunc |
| 309 | end |
| 310 | |
| 311 | fn mr_stage_node_policy pbud long pgbud long tceil long tau long to void |
| 312 | put mr_st_i 3 pbud |
| 313 | put mr_st_i 4 pgbud |
| 314 | put mr_st_i 5 tceil |
| 315 | put mr_st_i 6 tau |
| 316 | end |
| 317 | |
| 318 | note Commit staged node (copies mv_ed_pk[0..32) as the node's Ed25519 identity) |
| 319 | fn mr_commit_node to ulong |
| 320 | slot = get mr_st_u 0 |
| 321 | if ge slot MR_NODE_CAP |
| 322 | ret MR_ERR_BAD_NODE |
| 323 | end |
| 324 | tid = get mr_st_u 1 |
| 325 | if ge tid get tdCount 0 |
| 326 | ret MR_ERR_BAD_NODE |
| 327 | end |
| 328 | ip = get mr_st_u 5 |
| 329 | if not mr_ip_is_safe_endpoint ip |
| 330 | ret MR_ERR_WILDCARD_BIND |
| 331 | end |
| 332 | if eq get mr_st_u 9 0 |
| 333 | ret MR_ERR_BAD_NODE |
| 334 | end |
| 335 | put mr_n_used slot 1 |
| 336 | put mr_n_tid slot tid |
| 337 | put mr_n_feat slot get mr_st_u 2 |
| 338 | put mr_n_role slot get mr_st_u 3 |
| 339 | put mr_n_tier slot get mr_st_u 4 |
| 340 | put mr_n_ipv4 slot ip |
| 341 | put mr_n_port slot get mr_st_u 6 |
| 342 | put mr_n_rtt_us slot get mr_st_u 7 |
| 343 | put mr_n_bw_mbps slot get mr_st_u 8 |
| 344 | put mr_n_speed slot get mr_st_u 9 |
| 345 | put mr_n_online slot get mr_st_u 10 |
| 346 | put mr_n_tdie slot get mr_st_i 0 |
| 347 | put mr_n_tsink slot get mr_st_i 1 |
| 348 | put mr_n_tamb slot get mr_st_i 2 |
| 349 | put mr_n_pbud slot get mr_st_i 3 |
| 350 | put mr_n_pgbud slot get mr_st_i 4 |
| 351 | put mr_n_tceil slot get mr_st_i 5 |
| 352 | put mr_n_tau slot get mr_st_i 6 |
| 353 | put mr_n_bcpu slot get mr_st_i 7 |
| 354 | put mr_n_bunc slot get mr_st_i 8 |
| 355 | base = mul slot 32 |
| 356 | for i ulong 32 |
| 357 | put mr_n_pk add base i get mv_ed_pk i |
| 358 | end |
| 359 | ret MR_OK |
| 360 | end |
| 361 | |
| 362 | note ---- WattCBF Thermal/Power Headroom Evaluation (calls watt_cbf.om UNCHANGED) ---- |
| 363 | |
| 364 | fn mr_tid_is_gpu tid ulong to bool |
| 365 | if eq tid TID_SM121 |
| 366 | ret true |
| 367 | end |
| 368 | if eq tid TID_APU_GB10 |
| 369 | ret true |
| 370 | end |
| 371 | if eq tid TID_SPARK_RING |
| 372 | ret true |
| 373 | end |
| 374 | ret false |
| 375 | end |
| 376 | |
| 377 | fn mr_wc_check_barrier st view WattThermal pol view WattPolicy cmd view WattCmd to bool |
| 378 | dt = wc_next_die_delta st get cmd p_gpu get cmd p_cpu get cmd p_unc |
| 379 | bh = wc_barrier_h get pol t_ceil get st t_die get pol tau dt |
| 380 | put mr_wc_out 2 bh |
| 381 | ret ge bh 0 |
| 382 | end |
| 383 | |
| 384 | fn mr_wc_eval_inner slot ulong req_pw long st view WattThermal pol view WattPolicy to bool |
| 385 | pr = new WattProp 0 0 0 40 |
| 386 | if mr_tid_is_gpu get mr_n_tid slot |
| 387 | put pr p_gpu req_pw |
| 388 | put pr p_cpu get mr_n_bcpu slot |
| 389 | put pr p_unc get mr_n_bunc slot |
| 390 | else |
| 391 | put pr p_gpu 0 |
| 392 | put pr p_cpu add get mr_n_bcpu slot req_pw |
| 393 | put pr p_unc get mr_n_bunc slot |
| 394 | end |
| 395 | cmd = new WattCmd 0 0 0 0 0 0 |
| 396 | wc_cbf_filter st pol pr cmd |
| 397 | put mr_wc_out 3 get cmd fail_closed |
| 398 | if ne get cmd fail_closed 0 |
| 399 | ret false |
| 400 | end |
| 401 | if not mr_wc_check_barrier st pol cmd |
| 402 | ret false |
| 403 | end |
| 404 | if mr_tid_is_gpu get mr_n_tid slot |
| 405 | put mr_wc_out 1 get cmd p_gpu |
| 406 | else |
| 407 | c_ex = sub get cmd p_cpu get mr_n_bcpu slot |
| 408 | if lt c_ex 0 |
| 409 | c_ex = long 0 |
| 410 | end |
| 411 | put mr_wc_out 1 c_ex |
| 412 | end |
| 413 | if ne get cmd p_gpu get pr p_gpu |
| 414 | ret false |
| 415 | end |
| 416 | if ne get cmd p_cpu get pr p_cpu |
| 417 | ret false |
| 418 | end |
| 419 | if ne get cmd p_unc get pr p_unc |
| 420 | ret false |
| 421 | end |
| 422 | put mr_wc_out 0 1 |
| 423 | ret true |
| 424 | end |
| 425 | |
| 426 | fn mr_eval_node_wattcbf slot ulong req_pw long to bool |
| 427 | put mr_wc_out 0 0 |
| 428 | put mr_wc_out 1 0 |
| 429 | put mr_wc_out 2 0 |
| 430 | put mr_wc_out 3 1 |
| 431 | if or ge slot MR_NODE_CAP eq get mr_n_used slot 0 |
| 432 | ret false |
| 433 | end |
| 434 | if lt req_pw 0 |
| 435 | ret false |
| 436 | end |
| 437 | cpw = pk_get_power_mw |
| 438 | if gt cpw 0 |
| 439 | if gt req_pw long cpw |
| 440 | ret false |
| 441 | end |
| 442 | end |
| 443 | st = new WattThermal get mr_n_tdie slot get mr_n_tsink slot get mr_n_tamb slot |
| 444 | pol = new WattPolicy get mr_n_pbud slot get mr_n_pgbud slot get mr_n_tceil slot get mr_n_tau slot |
| 445 | if wc_must_fail_closed st pol |
| 446 | ret false |
| 447 | end |
| 448 | ret mr_wc_eval_inner slot req_pw st pol |
| 449 | end |
| 450 | |
| 451 | note ---- Compile-on-Arrive Profitability & Multi-Target Routing Solver ---- |
| 452 | |
| 453 | fn mr_ceil_div_u a ulong b ulong to ulong |
| 454 | if eq b 0 |
| 455 | ret a |
| 456 | end |
| 457 | q = div a b |
| 458 | if gt rem a b 0 |
| 459 | ret add q 1 |
| 460 | end |
| 461 | ret q |
| 462 | end |
| 463 | |
| 464 | fn mr_node_authorized slot ulong to bool |
| 465 | if eq slot 0 |
| 466 | ret true |
| 467 | end |
| 468 | base = mul slot 32 |
| 469 | for i ulong 32 |
| 470 | put mv_ed_pk i get mr_n_pk add base i |
| 471 | end |
| 472 | ps = mv_peer_find_pk |
| 473 | if ne ps MV_NONE |
| 474 | if ne get mv_peer_quar ps 0 |
| 475 | ret false |
| 476 | end |
| 477 | ret true |
| 478 | end |
| 479 | ret eq get mr_n_tier slot MV_TIER_B_ZEROTRUST |
| 480 | end |
| 481 | |
| 482 | fn mr_calc_remote_us slot ulong work_units ulong est_out ulong to ulong |
| 483 | xb = add get pk_len 0 get mr_in_len 0 |
| 484 | xb = add xb add est_out 352 |
| 485 | bw = get mr_n_bw_mbps slot |
| 486 | if eq bw 0 |
| 487 | bw = ulong 1 |
| 488 | end |
| 489 | tot = mr_ceil_div_u xb bw |
| 490 | tot = add tot get mr_n_rtt_us slot |
| 491 | tot = add tot div tdGet get mr_n_tid slot TDF_COHNS 1000 |
| 492 | tot = add tot mr_ceil_div_u work_units get mr_n_speed slot |
| 493 | if eq get mr_n_tier slot MV_TIER_B_ZEROTRUST |
| 494 | ret add tot 1200 |
| 495 | end |
| 496 | ret add tot 400 |
| 497 | end |
| 498 | |
| 499 | fn mr_route_reset_out to void |
| 500 | for i ulong 16 |
| 501 | put mr_route_out i 0 |
| 502 | end |
| 503 | put mr_route_out 1 MR_NONE |
| 504 | put mr_route_out 3 MR_NONE |
| 505 | put mr_route_out 4 MR_NONE |
| 506 | end |
| 507 | |
| 508 | fn mr_route_eval_local work_units ulong req_pw long to bool |
| 509 | if or eq get mr_n_used 0 0 eq get mr_n_online 0 0 |
| 510 | ret false |
| 511 | end |
| 512 | tid = get mr_n_tid 0 |
| 513 | feat = get mr_n_feat 0 |
| 514 | if ne pk_check_contract tid feat TDK_OK |
| 515 | put mr_route_out 9 add get mr_route_out 9 1 |
| 516 | ret false |
| 517 | end |
| 518 | if not mr_eval_node_wattcbf 0 req_pw |
| 519 | put mr_route_out 10 add get mr_route_out 10 1 |
| 520 | ret false |
| 521 | end |
| 522 | put mr_route_out 7 1 |
| 523 | lus = mr_ceil_div_u work_units get mr_n_speed 0 |
| 524 | dl = pk_get_deadline_us |
| 525 | if and eq pk_get_hard_dl 1 gt dl 0 |
| 526 | if gt lus dl |
| 527 | put mr_route_out 12 add get mr_route_out 12 1 |
| 528 | ret false |
| 529 | end |
| 530 | end |
| 531 | put mr_route_out 3 lus |
| 532 | ret true |
| 533 | end |
| 534 | |
| 535 | fn mr_route_consider_peer s ulong work_units ulong req_pw long est_out ulong to void |
| 536 | if or eq get mr_n_used s 0 eq get mr_n_online s 0 |
| 537 | ret |
| 538 | end |
| 539 | if not mr_node_authorized s |
| 540 | put mr_route_out 11 add get mr_route_out 11 1 |
| 541 | ret |
| 542 | end |
| 543 | tid = get mr_n_tid s |
| 544 | if ne pk_check_contract tid get mr_n_feat s TDK_OK |
| 545 | put mr_route_out 9 add get mr_route_out 9 1 |
| 546 | ret |
| 547 | end |
| 548 | if not mr_eval_node_wattcbf s req_pw |
| 549 | put mr_route_out 10 add get mr_route_out 10 1 |
| 550 | ret |
| 551 | end |
| 552 | put mr_route_out 8 1 |
| 553 | rus = mr_calc_remote_us s work_units est_out |
| 554 | dl = pk_get_deadline_us |
| 555 | if and eq pk_get_hard_dl 1 gt dl 0 |
| 556 | if gt rus dl |
| 557 | put mr_route_out 12 add get mr_route_out 12 1 |
| 558 | ret |
| 559 | end |
| 560 | end |
| 561 | if or eq get mr_route_out 1 MR_NONE lt rus get mr_route_out 4 |
| 562 | put mr_route_out 1 s |
| 563 | put mr_route_out 4 rus |
| 564 | put mr_route_out 5 add get mr_n_rtt_us s div tdGet tid TDF_COHNS 1000 |
| 565 | bw = get mr_n_bw_mbps s |
| 566 | if eq bw 0 |
| 567 | bw = ulong 1 |
| 568 | end |
| 569 | xb = add get pk_len 0 get mr_in_len 0 |
| 570 | xb = add xb add est_out 352 |
| 571 | put mr_route_out 6 mr_ceil_div_u xb bw |
| 572 | if gt get mr_wc_out 1 0 |
| 573 | put mr_route_out 13 ulong get mr_wc_out 1 |
| 574 | else |
| 575 | put mr_route_out 13 0 |
| 576 | end |
| 577 | if gt get mr_wc_out 2 0 |
| 578 | put mr_route_out 14 ulong get mr_wc_out 2 |
| 579 | else |
| 580 | put mr_route_out 14 0 |
| 581 | end |
| 582 | end |
| 583 | end |
| 584 | |
| 585 | fn mr_route_scan_peers work_units ulong req_pw long est_out ulong to ulong |
| 586 | put mr_route_out 1 MR_NONE |
| 587 | put mr_route_out 4 MR_NONE |
| 588 | s = ulong 1 |
| 589 | while lt s MR_NODE_CAP bound 16 |
| 590 | mr_route_consider_peer s work_units req_pw est_out |
| 591 | s = add s 1 |
| 592 | end |
| 593 | ret get mr_route_out 1 |
| 594 | end |
| 595 | |
| 596 | note Primary Compile-on-Arrive Mesh Router: |
| 597 | note 1. Checks OMEGA-MEMO first (when check_memo is true) -> instant MR_DEC_MEMO_HIT. |
| 598 | note 2. On miss, verifies OMPK v1/v2, evaluates local vs peer TargetDesc + WattCBF + RTT/transfer. |
| 599 | note 3. Fails closed (MR_ERR_NO_NODE) if no safe, contract-compatible node exists. |
| 600 | fn mr_route_select work_units ulong req_pw long est_out ulong check_memo bool to ulong |
| 601 | mr_route_reset_out |
| 602 | if check_memo |
| 603 | if eq mm_lookup MM_OK |
| 604 | put mr_route_out 0 MR_DEC_MEMO_HIT |
| 605 | put mr_route_out 1 0 |
| 606 | put mr_route_out 2 get mr_n_tid 0 |
| 607 | put mr_net_stats 1 add get mr_net_stats 1 1 |
| 608 | ret MR_DEC_MEMO_HIT |
| 609 | end |
| 610 | end |
| 611 | if ne pk_verify PK_OK |
| 612 | put mr_route_out 0 MR_ERR_BAD_OMPK |
| 613 | ret MR_ERR_BAD_OMPK |
| 614 | end |
| 615 | loc_ok = mr_route_eval_local work_units req_pw |
| 616 | best_rem = mr_route_scan_peers work_units req_pw est_out |
| 617 | if and not loc_ok eq best_rem MR_NONE |
| 618 | put mr_route_out 0 MR_ERR_NO_NODE |
| 619 | ret MR_ERR_NO_NODE |
| 620 | end |
| 621 | if not loc_ok |
| 622 | put mr_route_out 0 MR_DEC_REMOTE |
| 623 | put mr_route_out 1 best_rem |
| 624 | put mr_route_out 2 get mr_n_tid best_rem |
| 625 | ret MR_DEC_REMOTE |
| 626 | end |
| 627 | if eq best_rem MR_NONE |
| 628 | put mr_route_out 0 MR_DEC_LOCAL |
| 629 | put mr_route_out 1 0 |
| 630 | put mr_route_out 2 get mr_n_tid 0 |
| 631 | ret MR_DEC_LOCAL |
| 632 | end |
| 633 | if lt add get mr_route_out 4 MR_OFFLOAD_MARGIN_US get mr_route_out 3 |
| 634 | put mr_route_out 0 MR_DEC_REMOTE |
| 635 | put mr_route_out 1 best_rem |
| 636 | put mr_route_out 2 get mr_n_tid best_rem |
| 637 | ret MR_DEC_REMOTE |
| 638 | end |
| 639 | put mr_route_out 0 MR_DEC_LOCAL |
| 640 | put mr_route_out 1 0 |
| 641 | put mr_route_out 2 get mr_n_tid 0 |
| 642 | ret MR_DEC_LOCAL |
| 643 | end |
| 644 | |
| 645 | note ---- OMSH v1 Binary Wire Framing (Fail-Closed & Ed25519 Signed) ---- |
| 646 | |
| 647 | fn mr_w_put_u16 off ulong v ulong to void |
| 648 | put mr_wire off uchar and v 255 |
| 649 | put mr_wire add off 1 uchar and shr v 8 255 |
| 650 | end |
| 651 | |
| 652 | fn mr_w_put_u32 off ulong v ulong to void |
| 653 | put mr_wire off uchar and v 255 |
| 654 | put mr_wire add off 1 uchar and shr v 8 255 |
| 655 | put mr_wire add off 2 uchar and shr v 16 255 |
| 656 | put mr_wire add off 3 uchar and shr v 24 255 |
| 657 | end |
| 658 | |
| 659 | fn mr_w_put_u64 off ulong v ulong to void |
| 660 | mr_w_put_u32 off and v 4294967295 |
| 661 | mr_w_put_u32 add off 4 shr v 32 |
| 662 | end |
| 663 | |
| 664 | fn mr_w_get_u16 off ulong to ulong |
| 665 | b0 = ulong get mr_wire off |
| 666 | b1 = shl ulong get mr_wire add off 1 8 |
| 667 | ret or b0 b1 |
| 668 | end |
| 669 | |
| 670 | fn mr_w_get_u32 off ulong to ulong |
| 671 | b0 = ulong get mr_wire off |
| 672 | b1 = shl ulong get mr_wire add off 1 8 |
| 673 | b2 = shl ulong get mr_wire add off 2 16 |
| 674 | b3 = shl ulong get mr_wire add off 3 24 |
| 675 | ret or or b0 b1 or b2 b3 |
| 676 | end |
| 677 | |
| 678 | fn mr_w_get_u64 off ulong to ulong |
| 679 | lo = mr_w_get_u32 off |
| 680 | hi = mr_w_get_u32 add off 4 |
| 681 | ret or lo shl hi 32 |
| 682 | end |
| 683 | |
| 684 | fn mr_wire_check_hdr want_type ulong to ulong |
| 685 | wlen = get mr_wire_len 0 |
| 686 | if or lt wlen MR_OMSH_HDR_LEN gt wlen MR_MAX_WIRE |
| 687 | ret MR_ERR_FRAME_LEN |
| 688 | end |
| 689 | for i ulong 4 |
| 690 | if ne get mr_wire i get mr_omsh_magic i |
| 691 | ret MR_ERR_FRAME_MAGIC |
| 692 | end |
| 693 | end |
| 694 | if ne mr_w_get_u16 4 MR_OMSH_VER |
| 695 | ret MR_ERR_FRAME_VER |
| 696 | end |
| 697 | mtype = mr_w_get_u16 6 |
| 698 | if ne mtype want_type |
| 699 | ret MR_ERR_FRAME_TYPE |
| 700 | end |
| 701 | plen = mr_w_get_u32 8 |
| 702 | if gt plen MR_MAX_FRAME_PAYLOAD |
| 703 | ret MR_ERR_FRAME_LEN |
| 704 | end |
| 705 | if ne wlen add MR_OMSH_HDR_LEN plen |
| 706 | ret MR_ERR_FRAME_LEN |
| 707 | end |
| 708 | ret MR_OK |
| 709 | end |
| 710 | |
| 711 | fn mr_sign_req_wire sig_off ulong to ulong |
| 712 | for i ulong 32 |
| 713 | put mv_ed_seed i get mr_cli_seed i |
| 714 | end |
| 715 | for i ulong sig_off bound 33024 |
| 716 | put mv_ed_msg i get mr_wire i |
| 717 | end |
| 718 | if ne mv_ed_sign_buf sig_off MV_OK |
| 719 | ret MR_ERR_FRAME_SIG |
| 720 | end |
| 721 | for i ulong 64 |
| 722 | put mr_wire add sig_off i get mv_ed_sig i |
| 723 | end |
| 724 | put mr_wire_len 0 add sig_off 64 |
| 725 | ret MR_OK |
| 726 | end |
| 727 | |
| 728 | fn mr_encode_req_copy_body ompk_l ulong in_l ulong to ulong |
| 729 | for i ulong in_l bound 16384 |
| 730 | put sh_dat i get mr_in_buf i |
| 731 | end |
| 732 | sh_all in_l |
| 733 | for i ulong 32 |
| 734 | put mr_req_in_sha i get sh_dig i |
| 735 | put mr_wire add 32 i get mr_cli_pk i |
| 736 | put mr_wire add 64 i get sh_dig i |
| 737 | end |
| 738 | for i ulong ompk_l bound 4768 |
| 739 | put mr_wire add 128 i get pk_buf i |
| 740 | end |
| 741 | p_in = add 128 ompk_l |
| 742 | for i ulong in_l bound 16384 |
| 743 | put mr_wire add p_in i get mr_in_buf i |
| 744 | end |
| 745 | ret mr_sign_req_wire add p_in in_l |
| 746 | end |
| 747 | |
| 748 | note Encode OMSH v1 DISPATCH_REQ into mr_wire and sign with mr_cli_seed |
| 749 | fn mr_encode_req_frame work_kind ulong prec_tier ulong err_b ulong target_tid ulong flags ulong to ulong |
| 750 | ompk_l = get pk_len 0 |
| 751 | in_l = get mr_in_len 0 |
| 752 | if or eq ompk_l 0 gt ompk_l PK_MAX_PKG_LEN |
| 753 | ret MR_ERR_BAD_OMPK |
| 754 | end |
| 755 | if gt in_l MR_MAX_INPUT_LEN |
| 756 | ret MR_ERR_FRAME_LEN |
| 757 | end |
| 758 | plen = add MR_REQ_FIXED_HDR ompk_l |
| 759 | plen = add plen add in_l 64 |
| 760 | if gt plen MR_MAX_FRAME_PAYLOAD |
| 761 | ret MR_ERR_FRAME_LEN |
| 762 | end |
| 763 | put mr_wire 0 get mr_omsh_magic 0 |
| 764 | put mr_wire 1 get mr_omsh_magic 1 |
| 765 | put mr_wire 2 get mr_omsh_magic 2 |
| 766 | put mr_wire 3 get mr_omsh_magic 3 |
| 767 | mr_w_put_u16 4 MR_OMSH_VER |
| 768 | mr_w_put_u16 6 MR_MSG_DISPATCH_REQ |
| 769 | mr_w_put_u32 8 plen |
| 770 | mr_w_put_u32 12 flags |
| 771 | mr_w_put_u64 16 get mr_req_meta 1 |
| 772 | mr_w_put_u64 24 get mr_req_meta 2 |
| 773 | mr_w_put_u32 96 ompk_l |
| 774 | mr_w_put_u32 100 in_l |
| 775 | mr_w_put_u32 104 work_kind |
| 776 | mr_w_put_u32 108 prec_tier |
| 777 | mr_w_put_u64 112 err_b |
| 778 | mr_w_put_u64 120 target_tid |
| 779 | ret mr_encode_req_copy_body ompk_l in_l |
| 780 | end |
| 781 | |
| 782 | fn mr_decode_req_verify_sig sig_off ulong req_cli_allowlist bool to ulong |
| 783 | for i ulong 32 |
| 784 | b = get mr_wire add 32 i |
| 785 | put mr_req_cli_pk i b |
| 786 | put mv_ed_pk i b |
| 787 | end |
| 788 | if req_cli_allowlist |
| 789 | cs = mv_peer_find_pk |
| 790 | if eq cs MV_NONE |
| 791 | ret MR_ERR_UNAUTH_PEER |
| 792 | end |
| 793 | if ne get mv_peer_quar cs 0 |
| 794 | ret MR_ERR_UNAUTH_PEER |
| 795 | end |
| 796 | end |
| 797 | for i ulong sig_off bound 33024 |
| 798 | put mv_ed_msg i get mr_wire i |
| 799 | end |
| 800 | for i ulong 64 |
| 801 | put mv_ed_sig i get mr_wire add sig_off i |
| 802 | end |
| 803 | if not mv_ed_verify_buf sig_off |
| 804 | ret MR_ERR_FRAME_SIG |
| 805 | end |
| 806 | ret MR_OK |
| 807 | end |
| 808 | |
| 809 | fn mr_decode_req_extract ompk_l ulong in_l ulong to ulong |
| 810 | for i ulong 32 |
| 811 | put mr_req_in_sha i get mr_wire add 64 i |
| 812 | end |
| 813 | for i ulong ompk_l bound 4768 |
| 814 | put pk_buf i get mr_wire add 128 i |
| 815 | end |
| 816 | put pk_len 0 ompk_l |
| 817 | p_in = add 128 ompk_l |
| 818 | for i ulong in_l bound 16384 |
| 819 | put mr_in_buf i get mr_wire add p_in i |
| 820 | put sh_dat i get mr_wire add p_in i |
| 821 | end |
| 822 | put mr_in_len 0 in_l |
| 823 | sh_all in_l |
| 824 | for i ulong 32 |
| 825 | if ne get sh_dig i get mr_req_in_sha i |
| 826 | ret MR_ERR_FRAME_SIG |
| 827 | end |
| 828 | end |
| 829 | if ne pk_verify PK_OK |
| 830 | ret MR_ERR_BAD_OMPK |
| 831 | end |
| 832 | if ne pk_get_purity 1 |
| 833 | ret MR_ERR_IMPURE |
| 834 | end |
| 835 | ret MR_OK |
| 836 | end |
| 837 | |
| 838 | note Decode & fail-closed verify OMSH v1 DISPATCH_REQ from mr_wire |
| 839 | fn mr_decode_req_frame req_cli_allowlist bool to ulong |
| 840 | hrc = mr_wire_check_hdr MR_MSG_DISPATCH_REQ |
| 841 | if ne hrc MR_OK |
| 842 | ret hrc |
| 843 | end |
| 844 | plen = mr_w_get_u32 8 |
| 845 | if lt plen add MR_REQ_FIXED_HDR 64 |
| 846 | ret MR_ERR_FRAME_LEN |
| 847 | end |
| 848 | ompk_l = mr_w_get_u32 96 |
| 849 | in_l = mr_w_get_u32 100 |
| 850 | if or eq ompk_l 0 gt ompk_l PK_MAX_PKG_LEN |
| 851 | ret MR_ERR_FRAME_LEN |
| 852 | end |
| 853 | if gt in_l MR_MAX_INPUT_LEN |
| 854 | ret MR_ERR_FRAME_LEN |
| 855 | end |
| 856 | want_p = add MR_REQ_FIXED_HDR ompk_l |
| 857 | want_p = add want_p add in_l 64 |
| 858 | if ne plen want_p |
| 859 | ret MR_ERR_FRAME_LEN |
| 860 | end |
| 861 | hrc = mr_decode_req_verify_sig add add 128 ompk_l in_l req_cli_allowlist |
| 862 | if ne hrc MR_OK |
| 863 | ret hrc |
| 864 | end |
| 865 | put mr_req_meta 0 mr_w_get_u32 12 |
| 866 | put mr_req_meta 1 mr_w_get_u64 16 |
| 867 | put mr_req_meta 2 mr_w_get_u64 24 |
| 868 | put mr_req_meta 3 ompk_l |
| 869 | put mr_req_meta 4 in_l |
| 870 | put mr_req_meta 5 mr_w_get_u32 104 |
| 871 | put mr_req_meta 6 mr_w_get_u32 108 |
| 872 | put mr_req_meta 7 mr_w_get_u64 112 |
| 873 | put mr_req_meta 8 mr_w_get_u64 120 |
| 874 | ret mr_decode_req_extract ompk_l in_l |
| 875 | end |
| 876 | |
| 877 | note Encode OMSH v1 DISPATCH_RESP from mv_rcpt + mm_val_buf + mr_aux_buf into mr_wire |
| 878 | fn mr_encode_resp_frame status ulong olen ulong aux_l ulong to ulong |
| 879 | if gt olen 16384 |
| 880 | ret MR_ERR_FRAME_LEN |
| 881 | end |
| 882 | if gt aux_l 4096 |
| 883 | ret MR_ERR_FRAME_LEN |
| 884 | end |
| 885 | plen = add add MR_RESP_FIXED_HDR olen aux_l |
| 886 | if gt plen MR_MAX_FRAME_PAYLOAD |
| 887 | ret MR_ERR_FRAME_LEN |
| 888 | end |
| 889 | for i ulong 4 |
| 890 | put mr_wire i get mr_omsh_magic i |
| 891 | end |
| 892 | mr_w_put_u16 4 MR_OMSH_VER |
| 893 | mr_w_put_u16 6 MR_MSG_DISPATCH_RESP |
| 894 | mr_w_put_u32 8 plen |
| 895 | mr_w_put_u32 12 get mr_req_meta 0 |
| 896 | mr_w_put_u64 16 get mr_req_meta 1 |
| 897 | mr_w_put_u64 24 get mr_req_meta 2 |
| 898 | mr_w_put_u32 32 status |
| 899 | mr_w_put_u32 36 olen |
| 900 | mr_w_put_u32 40 aux_l |
| 901 | mr_w_put_u32 44 0 |
| 902 | for i ulong MV_RCPT_LEN |
| 903 | put mr_wire add 48 i get mv_rcpt i |
| 904 | end |
| 905 | p_out = add 48 MV_RCPT_LEN |
| 906 | for i ulong olen bound 16384 |
| 907 | put mr_wire add p_out i get mm_val_buf i |
| 908 | end |
| 909 | p_aux = add p_out olen |
| 910 | for i ulong aux_l bound 4096 |
| 911 | put mr_wire add p_aux i get mr_aux_buf i |
| 912 | end |
| 913 | put mr_wire_len 0 add MR_OMSH_HDR_LEN plen |
| 914 | ret MR_OK |
| 915 | end |
| 916 | |
| 917 | fn mr_decode_resp_copy olen ulong aux_l ulong to void |
| 918 | for i ulong MV_RCPT_LEN |
| 919 | put mv_rcpt i get mr_wire add 48 i |
| 920 | end |
| 921 | p_out = add 48 MV_RCPT_LEN |
| 922 | for i ulong olen bound 16384 |
| 923 | put mm_val_buf i get mr_wire add p_out i |
| 924 | end |
| 925 | put mm_val_len 0 olen |
| 926 | p_aux = add p_out olen |
| 927 | for i ulong aux_l bound 4096 |
| 928 | put mr_aux_buf i get mr_wire add p_aux i |
| 929 | end |
| 930 | put mr_aux_len 0 aux_l |
| 931 | end |
| 932 | |
| 933 | note Decode OMSH v1 DISPATCH_RESP from mr_wire into mv_rcpt + mm_val_buf + mr_aux_buf |
| 934 | fn mr_decode_resp_frame to ulong |
| 935 | hrc = mr_wire_check_hdr MR_MSG_DISPATCH_RESP |
| 936 | if ne hrc MR_OK |
| 937 | ret hrc |
| 938 | end |
| 939 | plen = mr_w_get_u32 8 |
| 940 | if lt plen MR_RESP_FIXED_HDR |
| 941 | ret MR_ERR_FRAME_LEN |
| 942 | end |
| 943 | olen = mr_w_get_u32 36 |
| 944 | aux_l = mr_w_get_u32 40 |
| 945 | if ne mr_w_get_u32 44 0 |
| 946 | ret MR_ERR_FRAME_LEN |
| 947 | end |
| 948 | if or gt olen 16384 gt aux_l 4096 |
| 949 | ret MR_ERR_FRAME_LEN |
| 950 | end |
| 951 | if ne plen add add MR_RESP_FIXED_HDR olen aux_l |
| 952 | ret MR_ERR_FRAME_LEN |
| 953 | end |
| 954 | put mr_req_meta 9 mr_w_get_u32 32 |
| 955 | put mr_req_meta 10 olen |
| 956 | put mr_req_meta 11 aux_l |
| 957 | mr_decode_resp_copy olen aux_l |
| 958 | ret MR_OK |
| 959 | end |
| 960 | |
| 961 | note ---- Loopback-Safe Native Pure-OM-2 TCP Socket Engine ---- |
| 962 | |
| 963 | fn mr_fill_sockaddr ipv4 ulong port ulong to void |
| 964 | for i ulong 16 |
| 965 | put mr_sockaddr i 0 |
| 966 | end |
| 967 | note AF_INET = 2 (LE u16), port (BE u16), ipv4 (BE u32) |
| 968 | put mr_sockaddr 0 2 |
| 969 | put mr_sockaddr 1 0 |
| 970 | put mr_sockaddr 2 uchar and shr port 8 255 |
| 971 | put mr_sockaddr 3 uchar and port 255 |
| 972 | put mr_sockaddr 4 uchar and shr ipv4 24 255 |
| 973 | put mr_sockaddr 5 uchar and shr ipv4 16 255 |
| 974 | put mr_sockaddr 6 uchar and shr ipv4 8 255 |
| 975 | put mr_sockaddr 7 uchar and ipv4 255 |
| 976 | end |
| 977 | |
| 978 | fn mr_tcp_listen ipv4 ulong port ulong to long |
| 979 | if not mr_ip_is_safe_bind ipv4 |
| 980 | ret neg long MR_ERR_WILDCARD_BIND |
| 981 | end |
| 982 | fd = socknew 2 1 0 |
| 983 | if lt fd 0 |
| 984 | ret neg long MR_ERR_SOCK |
| 985 | end |
| 986 | put mr_sopt 0 1 |
| 987 | put mr_sopt 1 0 |
| 988 | put mr_sopt 2 0 |
| 989 | put mr_sopt 3 0 |
| 990 | sr = sockopt int fd 1 2 mr_sopt 0 4 |
| 991 | mr_fill_sockaddr ipv4 port |
| 992 | br = bind int fd mr_sockaddr 0 16 |
| 993 | if ne br 0 |
| 994 | cf = fclose int fd |
| 995 | ret neg long MR_ERR_SOCK |
| 996 | end |
| 997 | snr = sockname int fd mr_sockaddr 0 16 |
| 998 | if ne snr 16 |
| 999 | cf = fclose int fd |
| 1000 | ret neg long MR_ERR_SOCK |
| 1001 | end |
| 1002 | bport = or shl ulong get mr_sockaddr 2 8 ulong get mr_sockaddr 3 |
| 1003 | put mr_net_stats 7 bport |
| 1004 | lr = listen int fd 8 |
| 1005 | if ne lr 0 |
| 1006 | cf = fclose int fd |
| 1007 | ret neg long MR_ERR_SOCK |
| 1008 | end |
| 1009 | ret fd |
| 1010 | end |
| 1011 | |
| 1012 | fn mr_tcp_connect ipv4 ulong port ulong to long |
| 1013 | if not mr_ip_is_safe_endpoint ipv4 |
| 1014 | ret neg long MR_ERR_WILDCARD_BIND |
| 1015 | end |
| 1016 | fd = socknew 2 1 0 |
| 1017 | if lt fd 0 |
| 1018 | ret neg long MR_ERR_SOCK |
| 1019 | end |
| 1020 | mr_fill_sockaddr ipv4 port |
| 1021 | cr = connect int fd mr_sockaddr 0 16 |
| 1022 | if ne cr 0 |
| 1023 | cf = fclose int fd |
| 1024 | ret neg long MR_ERR_SOCK |
| 1025 | end |
| 1026 | ret fd |
| 1027 | end |
| 1028 | |
| 1029 | fn mr_tcp_send_wire fd int to ulong |
| 1030 | tot = get mr_wire_len 0 |
| 1031 | if or eq tot 0 gt tot MR_MAX_WIRE |
| 1032 | ret MR_ERR_FRAME_LEN |
| 1033 | end |
| 1034 | off = ulong 0 |
| 1035 | while lt off tot bound 4096 |
| 1036 | k = send fd mr_wire off sub tot off 0 |
| 1037 | if le k 0 |
| 1038 | ret MR_ERR_IO |
| 1039 | end |
| 1040 | off = add off ulong k |
| 1041 | end |
| 1042 | put mr_net_stats 4 add get mr_net_stats 4 1 |
| 1043 | ret MR_OK |
| 1044 | end |
| 1045 | |
| 1046 | fn mr_tcp_recv_exact fd int start ulong need ulong to ulong |
| 1047 | off = ulong 0 |
| 1048 | while lt off need bound 4096 |
| 1049 | wr = waitio fd 1 3000000000 |
| 1050 | if lt wr 0 |
| 1051 | ret MR_ERR_IO |
| 1052 | end |
| 1053 | if eq and wr 1 0 |
| 1054 | ret MR_ERR_TIMEOUT |
| 1055 | end |
| 1056 | k = recv fd mr_wire add start off sub need off 0 |
| 1057 | if le k 0 |
| 1058 | ret MR_ERR_IO |
| 1059 | end |
| 1060 | off = add off ulong k |
| 1061 | end |
| 1062 | ret MR_OK |
| 1063 | end |
| 1064 | |
| 1065 | fn mr_tcp_recv_wire fd int to ulong |
| 1066 | rc = mr_tcp_recv_exact fd 0 MR_OMSH_HDR_LEN |
| 1067 | if ne rc MR_OK |
| 1068 | ret rc |
| 1069 | end |
| 1070 | for i ulong 4 |
| 1071 | if ne get mr_wire i get mr_omsh_magic i |
| 1072 | ret MR_ERR_FRAME_MAGIC |
| 1073 | end |
| 1074 | end |
| 1075 | if ne mr_w_get_u16 4 MR_OMSH_VER |
| 1076 | ret MR_ERR_FRAME_VER |
| 1077 | end |
| 1078 | plen = mr_w_get_u32 8 |
| 1079 | if gt plen MR_MAX_FRAME_PAYLOAD |
| 1080 | ret MR_ERR_FRAME_LEN |
| 1081 | end |
| 1082 | if gt plen 0 |
| 1083 | rc2 = mr_tcp_recv_exact fd MR_OMSH_HDR_LEN plen |
| 1084 | if ne rc2 MR_OK |
| 1085 | ret rc2 |
| 1086 | end |
| 1087 | end |
| 1088 | put mr_wire_len 0 add MR_OMSH_HDR_LEN plen |
| 1089 | put mr_net_stats 5 add get mr_net_stats 5 1 |
| 1090 | ret MR_OK |
| 1091 | end |
| 1092 | |
| 1093 | note ---- Pure Compute-on-Arrive Execution Kernels ---- |
| 1094 | |
| 1095 | fn mr_in_get_u64 off ulong to ulong |
| 1096 | v = ulong 0 |
| 1097 | for j ulong 8 |
| 1098 | v = or v shl ulong get mr_in_buf add off j mul j 8 |
| 1099 | end |
| 1100 | ret v |
| 1101 | end |
| 1102 | |
| 1103 | fn mr_exec_poly_core to void |
| 1104 | tl = len mr_poly_tag |
| 1105 | for i ulong tl bound 32 |
| 1106 | put sh_dat i get mr_poly_tag i |
| 1107 | end |
| 1108 | for i ulong 32 |
| 1109 | put sh_dat add tl i get pk_buf add 40 i |
| 1110 | end |
| 1111 | inl = get mr_in_len 0 |
| 1112 | if gt inl 4096 |
| 1113 | inl = ulong 4096 |
| 1114 | end |
| 1115 | for i ulong inl bound 4096 |
| 1116 | put sh_dat add add tl 32 i get mr_in_buf i |
| 1117 | end |
| 1118 | sh_all add add tl 32 inl |
| 1119 | for i ulong 32 |
| 1120 | put mr_poly_tmp i get sh_dig i |
| 1121 | end |
| 1122 | for r ulong 16 |
| 1123 | for i ulong 32 |
| 1124 | put sh_dat i get mr_poly_tmp i |
| 1125 | end |
| 1126 | mv_sh_put_u64 32 r |
| 1127 | sh_all 40 |
| 1128 | for i ulong 32 |
| 1129 | put mr_poly_tmp i get sh_dig i |
| 1130 | end |
| 1131 | end |
| 1132 | end |
| 1133 | |
| 1134 | fn mr_exec_poly_to_valbuf to ulong |
| 1135 | mr_exec_poly_core |
| 1136 | for i ulong 32 |
| 1137 | put mm_val_buf i get mr_poly_tmp i |
| 1138 | end |
| 1139 | put mm_val_len 0 32 |
| 1140 | ret MR_OK |
| 1141 | end |
| 1142 | |
| 1143 | fn mr_exec_poly_to_spot to void |
| 1144 | mr_exec_poly_core |
| 1145 | for i ulong 32 |
| 1146 | put mv_spot_local i get mr_poly_tmp i |
| 1147 | end |
| 1148 | put mv_spot_llen 0 32 |
| 1149 | end |
| 1150 | |
| 1151 | fn mr_exec_matmul_from_in fault_mode ulong to ulong |
| 1152 | inl = get mr_in_len 0 |
| 1153 | if lt inl 8 |
| 1154 | ret MR_ERR_FRAME_LEN |
| 1155 | end |
| 1156 | n = mr_in_get_u64 0 |
| 1157 | if or eq n 0 gt n 32 |
| 1158 | ret MR_ERR_FRAME_LEN |
| 1159 | end |
| 1160 | elems = mul n n |
| 1161 | need = add 8 mul elems 16 |
| 1162 | if ne inl need |
| 1163 | ret MR_ERR_FRAME_LEN |
| 1164 | end |
| 1165 | for idx ulong elems bound 1024 |
| 1166 | put mv_mat_a idx mr_in_get_u64 add 8 mul idx 8 |
| 1167 | put mv_mat_b idx mr_in_get_u64 add add 8 mul elems 8 mul idx 8 |
| 1168 | end |
| 1169 | crc = mv_matmul_compute n |
| 1170 | if ne crc MV_OK |
| 1171 | ret MR_ERR_FRAME_LEN |
| 1172 | end |
| 1173 | if eq fault_mode MR_FAULT_MATMUL_CORRUPT |
| 1174 | put mv_mat_c 5 add get mv_mat_c 5 1 |
| 1175 | end |
| 1176 | erc = mv_mat_c_encode_to_valbuf n |
| 1177 | if ne erc MV_OK |
| 1178 | ret MR_ERR_FRAME_LEN |
| 1179 | end |
| 1180 | ret MR_OK |
| 1181 | end |
| 1182 | |
| 1183 | fn mr_exec_merkle_from_in fault_mode ulong to ulong |
| 1184 | if ne get mr_in_len 0 16 |
| 1185 | ret MR_ERR_FRAME_LEN |
| 1186 | end |
| 1187 | m = mr_in_get_u64 0 |
| 1188 | sseed = mr_in_get_u64 8 |
| 1189 | bad_c = MV_NONE |
| 1190 | if eq fault_mode MR_FAULT_MERKLE_CHUNK11 |
| 1191 | bad_c = ulong 11 |
| 1192 | end |
| 1193 | mrc = mv_merkle_run_trace true m sseed bad_c |
| 1194 | if ne mrc MV_OK |
| 1195 | ret MR_ERR_FRAME_LEN |
| 1196 | end |
| 1197 | for i ulong 32 |
| 1198 | put mm_val_buf i get mv_mk_state i |
| 1199 | end |
| 1200 | put mm_val_len 0 32 |
| 1201 | tbytes = mul mul m 2 32 |
| 1202 | for i ulong tbytes bound 4096 |
| 1203 | put mr_aux_buf i get mv_mk_rem i |
| 1204 | end |
| 1205 | put mr_aux_len 0 tbytes |
| 1206 | ret MR_OK |
| 1207 | end |
| 1208 | |
| 1209 | note ---- Real TCP Loopback Worker & Client Dispatch Engine ---- |
| 1210 | |
| 1211 | fn mr_worker_apply_fault fault_mode ulong to void |
| 1212 | if eq fault_mode MR_FAULT_FLIP_OUT_BEFORE_SIGN |
| 1213 | if gt get mm_val_len 0 0 |
| 1214 | put mm_val_buf 0 xor get mm_val_buf 0 uchar 1 |
| 1215 | end |
| 1216 | end |
| 1217 | if eq fault_mode MR_FAULT_WRONG_INPUT_HASH |
| 1218 | put mv_exp_in_sha 0 xor get mv_exp_in_sha 0 uchar 1 |
| 1219 | end |
| 1220 | end |
| 1221 | |
| 1222 | fn mr_worker_post_sign_fault fault_mode ulong to void |
| 1223 | if eq fault_mode MR_FAULT_FORGE_SIG |
| 1224 | put mv_rcpt 224 xor get mv_rcpt 224 uchar 1 |
| 1225 | end |
| 1226 | end |
| 1227 | |
| 1228 | fn mr_worker_sign_resp worker_tid ulong fault_mode ulong cns ulong to ulong |
| 1229 | for i ulong 32 |
| 1230 | put mv_exp_ompk_sem i get pk_buf add 8 i |
| 1231 | put mv_exp_fid i get pk_buf add 40 i |
| 1232 | put mv_exp_in_sha i get mr_req_in_sha i |
| 1233 | end |
| 1234 | vtier = MV_TIER_A_SOVEREIGN |
| 1235 | if ne and get mr_req_meta 0 MR_FLAG_TIER_B 0 |
| 1236 | vtier = MV_TIER_B_ZEROTRUST |
| 1237 | end |
| 1238 | mv_set_expected_meta get mr_req_meta 6 get mr_req_meta 7 get mr_req_meta 1 get mr_req_meta 2 vtier |
| 1239 | mr_worker_apply_fault fault_mode |
| 1240 | if eq fault_mode MR_FAULT_UNKNOWN_KEY |
| 1241 | mv_ed_derive_seed mr_rogue_tag 777 |
| 1242 | else |
| 1243 | for i ulong 32 |
| 1244 | put mv_ed_seed i get mr_wrk_seed i |
| 1245 | end |
| 1246 | end |
| 1247 | if ne mv_rcpt_sign worker_tid get mr_req_meta 6 vtier get mr_req_meta 7 cns MV_OK |
| 1248 | ret MR_ERR_FRAME_SIG |
| 1249 | end |
| 1250 | mr_worker_post_sign_fault fault_mode |
| 1251 | ret MR_OK |
| 1252 | end |
| 1253 | |
| 1254 | fn mr_worker_exec_and_sign worker_tid ulong worker_feat ulong fault_mode ulong to ulong |
| 1255 | if ne pk_realize_on_arrive worker_tid worker_feat 1 7 PK_OK |
| 1256 | ret MR_ERR_REMOTE_REFUSED |
| 1257 | end |
| 1258 | t0 = monons |
| 1259 | put mr_aux_len 0 0 |
| 1260 | wk = get mr_req_meta 5 |
| 1261 | xrc = ulong MR_ERR_FRAME_TYPE |
| 1262 | if eq wk MR_WORK_POLY_PURE |
| 1263 | xrc = mr_exec_poly_to_valbuf |
| 1264 | else |
| 1265 | if eq wk MR_WORK_MATMUL_INT64 |
| 1266 | xrc = mr_exec_matmul_from_in fault_mode |
| 1267 | else |
| 1268 | if eq wk MR_WORK_MERKLE_STEPS |
| 1269 | xrc = mr_exec_merkle_from_in fault_mode |
| 1270 | end |
| 1271 | end |
| 1272 | end |
| 1273 | t1 = monons |
| 1274 | cns = ulong 10000 |
| 1275 | if gt t1 t0 |
| 1276 | cns = ulong sub t1 t0 |
| 1277 | if lt cns 10000 |
| 1278 | cns = ulong 10000 |
| 1279 | end |
| 1280 | end |
| 1281 | if ne xrc MR_OK |
| 1282 | ret xrc |
| 1283 | end |
| 1284 | ret mr_worker_sign_resp worker_tid fault_mode cns |
| 1285 | end |
| 1286 | |
| 1287 | note Serve one Compile-on-Arrive OMSH v1 request over an accepted TCP connection |
| 1288 | fn mr_worker_serve_one lfd int worker_tid ulong worker_feat ulong fault_mode ulong req_cli_auth bool to ulong |
| 1289 | wr = waitio lfd 1 5000000000 |
| 1290 | if lt wr 0 |
| 1291 | ret MR_ERR_IO |
| 1292 | end |
| 1293 | if eq and wr 1 0 |
| 1294 | ret MR_ERR_TIMEOUT |
| 1295 | end |
| 1296 | cfd = accept lfd 0 |
| 1297 | if lt cfd 0 |
| 1298 | ret MR_ERR_SOCK |
| 1299 | end |
| 1300 | rc = mr_tcp_recv_wire int cfd |
| 1301 | if ne rc MR_OK |
| 1302 | cf = fclose int cfd |
| 1303 | ret rc |
| 1304 | end |
| 1305 | rc = mr_decode_req_frame req_cli_auth |
| 1306 | if eq rc MR_OK |
| 1307 | rc = mr_worker_exec_and_sign worker_tid worker_feat fault_mode |
| 1308 | end |
| 1309 | if ne rc MR_OK |
| 1310 | put mr_aux_len 0 0 |
| 1311 | put mm_val_len 0 0 |
| 1312 | if eq mr_encode_resp_frame rc 0 0 MR_OK |
| 1313 | sr = mr_tcp_send_wire int cfd |
| 1314 | end |
| 1315 | cf = fclose int cfd |
| 1316 | ret rc |
| 1317 | end |
| 1318 | rc = mr_encode_resp_frame MR_OK get mm_val_len 0 get mr_aux_len 0 |
| 1319 | if eq rc MR_OK |
| 1320 | rc = mr_tcp_send_wire int cfd |
| 1321 | end |
| 1322 | cf = fclose int cfd |
| 1323 | ret rc |
| 1324 | end |
| 1325 | |
| 1326 | note Client-side single TCP roundtrip to ipv4:port (expects pk_buf, mr_in_buf, mr_req_meta[1..2] staged) |
| 1327 | fn mr_client_tcp_call ipv4 ulong port ulong work_kind ulong prec_tier ulong err_b ulong flags ulong to ulong |
| 1328 | rc = mr_encode_req_frame work_kind prec_tier err_b get mr_req_meta 8 flags |
| 1329 | if ne rc MR_OK |
| 1330 | ret rc |
| 1331 | end |
| 1332 | fd = mr_tcp_connect ipv4 port |
| 1333 | if lt fd 0 |
| 1334 | ret MR_ERR_SOCK |
| 1335 | end |
| 1336 | rc = mr_tcp_send_wire int fd |
| 1337 | if eq rc MR_OK |
| 1338 | rc = mr_tcp_recv_wire int fd |
| 1339 | end |
| 1340 | cf = fclose int fd |
| 1341 | if ne rc MR_OK |
| 1342 | ret rc |
| 1343 | end |
| 1344 | rc = mr_decode_resp_frame |
| 1345 | if ne rc MR_OK |
| 1346 | ret rc |
| 1347 | end |
| 1348 | if ne get mr_req_meta 9 MR_OK |
| 1349 | ret MR_ERR_REMOTE_REFUSED |
| 1350 | end |
| 1351 | put mr_net_stats 0 add get mr_net_stats 0 1 |
| 1352 | ret MR_OK |
| 1353 | end |
| 1354 | |
| 1355 | note Sync mm_req_* and mv_exp_* from current pk_buf (OMPK v2) + mr_in_buf |
| 1356 | fn mr_stage_memo_and_exp prec_tier ulong err_b ulong nonce ulong epoch ulong vtier ulong to ulong |
| 1357 | if ne pk_verify PK_OK |
| 1358 | ret MR_ERR_BAD_OMPK |
| 1359 | end |
| 1360 | if ne pk_get_purity 1 |
| 1361 | ret MR_ERR_IMPURE |
| 1362 | end |
| 1363 | inl = get mr_in_len 0 |
| 1364 | if gt inl MR_MAX_INPUT_LEN |
| 1365 | ret MR_ERR_FRAME_LEN |
| 1366 | end |
| 1367 | for i ulong inl bound 16384 |
| 1368 | put sh_dat i get mr_in_buf i |
| 1369 | end |
| 1370 | sh_all inl |
| 1371 | for i ulong 32 |
| 1372 | put mv_exp_ompk_sem i get pk_buf add 8 i |
| 1373 | put mv_exp_fid i get pk_buf add 40 i |
| 1374 | put mv_exp_in_sha i get sh_dig i |
| 1375 | put mm_req_ir_sha i get pk_buf add 8 i |
| 1376 | put mm_req_fid i get pk_buf add 40 i |
| 1377 | put mm_req_in_sha i get sh_dig i |
| 1378 | end |
| 1379 | mm_set_contract 1 1 prec_tier err_b |
| 1380 | mv_set_expected_meta prec_tier err_b nonce epoch vtier |
| 1381 | mv_set_expected_purity 1 1 get mr_req_meta 8 |
| 1382 | ret MR_OK |
| 1383 | end |
| 1384 | |
| 1385 | fn mr_client_verify_resp work_kind ulong vtier ulong nonce ulong to ulong |
| 1386 | olen = get mm_val_len 0 |
| 1387 | if eq vtier MV_TIER_A_SOVEREIGN |
| 1388 | has_spot = false |
| 1389 | if mv_should_spot_check nonce |
| 1390 | if eq work_kind MR_WORK_POLY_PURE |
| 1391 | mr_exec_poly_to_spot |
| 1392 | has_spot = true |
| 1393 | end |
| 1394 | end |
| 1395 | ret mv_tier_a_verify olen has_spot |
| 1396 | end |
| 1397 | if eq work_kind MR_WORK_MATMUL_INT64 |
| 1398 | ret mv_tier_b_verify_freivalds mr_in_get_u64 0 32 0 |
| 1399 | end |
| 1400 | if eq work_kind MR_WORK_MERKLE_STEPS |
| 1401 | m = mr_in_get_u64 0 |
| 1402 | tbytes = mul mul m 2 32 |
| 1403 | if ne get mr_aux_len 0 tbytes |
| 1404 | ret MV_ERR_FORMAT |
| 1405 | end |
| 1406 | for i ulong tbytes bound 4096 |
| 1407 | put mv_mk_rem i get mr_aux_buf i |
| 1408 | end |
| 1409 | ret mv_tier_b_verify_merkle olen m |
| 1410 | end |
| 1411 | ret MV_ERR_UNVERIFIED |
| 1412 | end |
| 1413 | |
| 1414 | note Full transparent Client Compile-on-Arrive + Verification + OMEGA-MEMO path: |
| 1415 | note 1. Checks OMEGA-MEMO first -> instant 0-recompute MR_DEC_MEMO_HIT. |
| 1416 | note 2. On miss, dispatches OMSH v1 over TCP to peer slot. |
| 1417 | note 3. Verifies via Tier A (Ed25519 + optional spot-check) or Tier B (Freivalds / Merkle). |
| 1418 | note 4. Admits to OMEGA-MEMO ONLY AFTER verification passes! |
| 1419 | fn mr_client_dispatch_and_memo slot ulong work_kind ulong prec_tier ulong err_b ulong vtier ulong flags ulong to ulong |
| 1420 | if ge slot MR_NODE_CAP |
| 1421 | ret MR_ERR_BAD_NODE |
| 1422 | end |
| 1423 | put mr_req_meta 8 get mr_n_tid slot |
| 1424 | rc = mr_stage_memo_and_exp prec_tier err_b get mr_req_meta 1 get mr_req_meta 2 vtier |
| 1425 | if ne rc MR_OK |
| 1426 | ret rc |
| 1427 | end |
| 1428 | if eq mm_lookup MM_OK |
| 1429 | put mr_net_stats 1 add get mr_net_stats 1 1 |
| 1430 | ret MR_DEC_MEMO_HIT |
| 1431 | end |
| 1432 | rc = mr_client_tcp_call get mr_n_ipv4 slot get mr_n_port slot work_kind prec_tier err_b flags |
| 1433 | if ne rc MR_OK |
| 1434 | ret rc |
| 1435 | end |
| 1436 | rc = mr_client_verify_resp work_kind vtier get mr_req_meta 1 |
| 1437 | put mr_net_stats 6 rc |
| 1438 | if ne rc MV_OK |
| 1439 | ret MR_ERR_VERIFY_FAILED |
| 1440 | end |
| 1441 | if ne mv_admit_to_memo get mv_parsed 7 1000 MV_OK |
| 1442 | ret MR_ERR_MEMO_FAILED |
| 1443 | end |
| 1444 | put mr_net_stats 3 add get mr_net_stats 3 1 |
| 1445 | ret MR_DEC_REMOTE |
| 1446 | end |