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 / runtime / aien_machine_id.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Copyright 2026 Drake Stapleton (AIEN / aien-dev) |
| 3 | note |
| 4 | note src/runtime/aien_machine_id.om -- canonical AIEN machine identity (M20). |
| 5 | note Composed after seed/ng/sha256.om. Pure seeded OM-2. |
| 6 | note |
| 7 | note AienMachineId layout: 33 bytes: byte 0 = root (1=PROVISIONED, 2=HARDWARE, 3=IMPORTED), |
| 8 | note bytes 1..32 = 32-byte id. |
| 9 | note Persisted 44-byte record: |
| 10 | note 0..3 "AMID" |
| 11 | note 4 0x01 |
| 12 | note 5 root |
| 13 | note 6..7 0x00, 0x00 |
| 14 | note 8..39 id[32] |
| 15 | note 40..43 first 4 bytes of SHA-256(bytes 0..39) |
| 16 | note Text form: 88 lowercase hex digits. |
| 17 | |
| 18 | const AIEN_MID_ROOT_PROVISIONED uchar 1 |
| 19 | const AIEN_MID_ROOT_HARDWARE uchar 2 |
| 20 | const AIEN_MID_ROOT_IMPORTED uchar 3 |
| 21 | |
| 22 | const AIEN_MID_OK int 0 |
| 23 | const AIEN_MID_E_ARG int -1 |
| 24 | const AIEN_MID_E_ZERO int -2 |
| 25 | const AIEN_MID_E_FORMAT int -3 |
| 26 | const AIEN_MID_E_CHECK int -4 |
| 27 | const AIEN_MID_E_IO int -5 |
| 28 | const AIEN_MID_E_FULL int -6 |
| 29 | |
| 30 | text t_mid_dom "AIENOS-MACHINE-ID-V1" |
| 31 | text t_mid_hex "0123456789abcdef" |
| 32 | |
| 33 | global g_mid_tmp_p uchar 4096 |
| 34 | global g_mid_tmp_t uchar 4096 |
| 35 | global g_mid_ren_pkt uchar 8200 |
| 36 | global g_mid_io_buf uchar 48 |
| 37 | global g_mid_rec_tmp uchar 44 |
| 38 | |
| 39 | note Local index table (capacity up to 64 entries) |
| 40 | global g_mid_idx_slots uchar 2112 |
| 41 | global g_mid_idx_meta uint 2 |
| 42 | |
| 43 | fn mid_is_zero id view uchar 32 to int |
| 44 | acc = uint 0 |
| 45 | for i ulong 32 |
| 46 | acc = or acc uint get id i |
| 47 | end |
| 48 | if eq acc 0 |
| 49 | ret 1 |
| 50 | end |
| 51 | ret 0 |
| 52 | end |
| 53 | |
| 54 | fn mid_root_valid r uchar to int |
| 55 | if eq r 1 |
| 56 | ret 1 |
| 57 | end |
| 58 | if eq r 2 |
| 59 | ret 1 |
| 60 | end |
| 61 | if eq r 3 |
| 62 | ret 1 |
| 63 | end |
| 64 | ret 0 |
| 65 | end |
| 66 | |
| 67 | fn mid_path_byte c uchar to int |
| 68 | if ge c 97 |
| 69 | if le c 122 |
| 70 | ret int c |
| 71 | end |
| 72 | end |
| 73 | if ge c 65 |
| 74 | if le c 90 |
| 75 | ret int c |
| 76 | end |
| 77 | end |
| 78 | if ge c 48 |
| 79 | if le c 57 |
| 80 | ret int c |
| 81 | end |
| 82 | end |
| 83 | if eq c 46 |
| 84 | ret int c |
| 85 | end |
| 86 | if eq c 95 |
| 87 | ret int c |
| 88 | end |
| 89 | if eq c 45 |
| 90 | ret int c |
| 91 | end |
| 92 | if eq c 47 |
| 93 | ret int c |
| 94 | end |
| 95 | if eq c 43 |
| 96 | ret int c |
| 97 | end |
| 98 | if eq c 44 |
| 99 | ret int c |
| 100 | end |
| 101 | if eq c 61 |
| 102 | ret int c |
| 103 | end |
| 104 | if eq c 64 |
| 105 | ret int c |
| 106 | end |
| 107 | if eq c 58 |
| 108 | ret int c |
| 109 | end |
| 110 | if eq c 126 |
| 111 | ret int c |
| 112 | end |
| 113 | ret sub int 0 1 |
| 114 | end |
| 115 | |
| 116 | fn mid_path_check_g src view uchar 4096 plen ulong cap ulong to int |
| 117 | if eq plen 0 |
| 118 | ret sub int 0 1 |
| 119 | end |
| 120 | if ge plen cap |
| 121 | ret sub int 0 1 |
| 122 | end |
| 123 | for k ulong plen |
| 124 | c = get src k |
| 125 | if lt mid_path_byte c 0 |
| 126 | ret sub int 0 1 |
| 127 | end |
| 128 | put g_mid_tmp_p k c |
| 129 | end |
| 130 | i = ulong 0 |
| 131 | while lt i plen |
| 132 | j = i |
| 133 | cont = int 1 |
| 134 | while ne cont 0 |
| 135 | if ge j plen |
| 136 | cont = int 0 |
| 137 | else |
| 138 | if eq get g_mid_tmp_p j 47 |
| 139 | cont = int 0 |
| 140 | else |
| 141 | j = add j 1 |
| 142 | end |
| 143 | end |
| 144 | end |
| 145 | if eq sub j i 2 |
| 146 | if eq get g_mid_tmp_p i 46 |
| 147 | if eq get g_mid_tmp_p add i 1 46 |
| 148 | ret sub int 0 1 |
| 149 | end |
| 150 | end |
| 151 | end |
| 152 | i = add j 1 |
| 153 | end |
| 154 | ret 0 |
| 155 | end |
| 156 | |
| 157 | fn aien_mid_derive root_kind uchar root view uchar 4096 rlen ulong dst edit uchar 33 to int |
| 158 | if eq root_kind 2 |
| 159 | if ne rlen 32 |
| 160 | ret sub int 0 1 |
| 161 | end |
| 162 | else |
| 163 | if eq root_kind 1 |
| 164 | if eq rlen 0 |
| 165 | ret sub int 0 1 |
| 166 | end |
| 167 | if gt rlen 4096 |
| 168 | ret sub int 0 1 |
| 169 | end |
| 170 | else |
| 171 | ret sub int 0 1 |
| 172 | end |
| 173 | end |
| 174 | for i ulong 20 |
| 175 | put sh_dat i get t_mid_dom i |
| 176 | end |
| 177 | put sh_dat 20 uchar 0 |
| 178 | for i ulong rlen |
| 179 | put sh_dat add 21 i get root i |
| 180 | end |
| 181 | sh_all add 21 rlen |
| 182 | put dst 0 root_kind |
| 183 | acc = uint 0 |
| 184 | for i ulong 32 |
| 185 | b = get sh_dig i |
| 186 | put dst add 1 i b |
| 187 | acc = or acc uint b |
| 188 | end |
| 189 | if eq acc 0 |
| 190 | ret sub int 0 2 |
| 191 | end |
| 192 | ret 0 |
| 193 | end |
| 194 | |
| 195 | fn aien_mid_from_slot slot view uchar 32 dst edit uchar 33 to int |
| 196 | if ne mid_is_zero view slot 0 |
| 197 | ret sub int 0 2 |
| 198 | end |
| 199 | put dst 0 uchar 3 |
| 200 | for i ulong 32 |
| 201 | put dst add 1 i get slot i |
| 202 | end |
| 203 | ret 0 |
| 204 | end |
| 205 | |
| 206 | fn aien_mid_to_slot m view uchar 33 slot edit uchar 32 to void |
| 207 | for i ulong 32 |
| 208 | put slot i get m add 1 i |
| 209 | end |
| 210 | end |
| 211 | |
| 212 | fn aien_mid_equal a view uchar 33 b view uchar 33 to int |
| 213 | for i ulong 32 |
| 214 | if ne get a add 1 i get b add 1 i |
| 215 | ret 0 |
| 216 | end |
| 217 | end |
| 218 | ret 1 |
| 219 | end |
| 220 | |
| 221 | fn aien_mid_compare a view uchar 33 b view uchar 33 to int |
| 222 | for i ulong 32 |
| 223 | ba = uint get a add 1 i |
| 224 | bb = uint get b add 1 i |
| 225 | if lt ba bb |
| 226 | ret sub int 0 1 |
| 227 | end |
| 228 | if gt ba bb |
| 229 | ret 1 |
| 230 | end |
| 231 | end |
| 232 | ret 0 |
| 233 | end |
| 234 | |
| 235 | fn aien_mid_hash m view uchar 33 to ulong |
| 236 | h = ulong 0 |
| 237 | for i ulong 8 |
| 238 | b = ulong get m add 1 i |
| 239 | h = or shl h 8 b |
| 240 | end |
| 241 | ret h |
| 242 | end |
| 243 | |
| 244 | fn aien_mid_encode m view uchar 33 dst edit uchar 44 to int |
| 245 | r = get m 0 |
| 246 | if eq mid_root_valid r 0 |
| 247 | ret sub int 0 1 |
| 248 | end |
| 249 | acc = uint 0 |
| 250 | for i ulong 32 |
| 251 | b = get m add 1 i |
| 252 | acc = or acc uint b |
| 253 | end |
| 254 | if eq acc 0 |
| 255 | ret sub int 0 2 |
| 256 | end |
| 257 | put dst 0 uchar 65 |
| 258 | put dst 1 uchar 77 |
| 259 | put dst 2 uchar 73 |
| 260 | put dst 3 uchar 68 |
| 261 | put dst 4 uchar 1 |
| 262 | put dst 5 r |
| 263 | put dst 6 uchar 0 |
| 264 | put dst 7 uchar 0 |
| 265 | for i ulong 32 |
| 266 | put dst add 8 i get m add 1 i |
| 267 | end |
| 268 | for i ulong 40 |
| 269 | put sh_dat i get dst i |
| 270 | end |
| 271 | sh_all 40 |
| 272 | for i ulong 4 |
| 273 | put dst add 40 i get sh_dig i |
| 274 | end |
| 275 | ret 0 |
| 276 | end |
| 277 | |
| 278 | fn aien_mid_decode src view uchar 48 slen ulong dst edit uchar 33 to int |
| 279 | if ne slen 44 |
| 280 | ret sub int 0 3 |
| 281 | end |
| 282 | if ne get src 0 65 |
| 283 | ret sub int 0 3 |
| 284 | end |
| 285 | if ne get src 1 77 |
| 286 | ret sub int 0 3 |
| 287 | end |
| 288 | if ne get src 2 73 |
| 289 | ret sub int 0 3 |
| 290 | end |
| 291 | if ne get src 3 68 |
| 292 | ret sub int 0 3 |
| 293 | end |
| 294 | if ne get src 4 1 |
| 295 | ret sub int 0 3 |
| 296 | end |
| 297 | r = get src 5 |
| 298 | if eq mid_root_valid r 0 |
| 299 | ret sub int 0 3 |
| 300 | end |
| 301 | if ne get src 6 0 |
| 302 | ret sub int 0 3 |
| 303 | end |
| 304 | if ne get src 7 0 |
| 305 | ret sub int 0 3 |
| 306 | end |
| 307 | for i ulong 40 |
| 308 | put sh_dat i get src i |
| 309 | end |
| 310 | sh_all 40 |
| 311 | for i ulong 4 |
| 312 | if ne get src add 40 i get sh_dig i |
| 313 | ret sub int 0 4 |
| 314 | end |
| 315 | end |
| 316 | acc = uint 0 |
| 317 | for i ulong 32 |
| 318 | b = get src add 8 i |
| 319 | acc = or acc uint b |
| 320 | end |
| 321 | if eq acc 0 |
| 322 | ret sub int 0 2 |
| 323 | end |
| 324 | put dst 0 r |
| 325 | for i ulong 32 |
| 326 | put dst add 1 i get src add 8 i |
| 327 | end |
| 328 | ret 0 |
| 329 | end |
| 330 | |
| 331 | fn aien_mid_to_text m view uchar 33 dst edit uchar 88 to int |
| 332 | rec = new uchar 44 0 |
| 333 | rc = aien_mid_encode view m edit rec |
| 334 | if ne rc 0 |
| 335 | ret rc |
| 336 | end |
| 337 | for i ulong 44 |
| 338 | b = uint get rec i |
| 339 | hi = ulong shr b 4 |
| 340 | lo = ulong and b 15 |
| 341 | put dst mul i 2 get t_mid_hex hi |
| 342 | put dst add mul i 2 1 get t_mid_hex lo |
| 343 | end |
| 344 | ret 0 |
| 345 | end |
| 346 | |
| 347 | fn mid_hex_nib c uchar to int |
| 348 | if ge c 48 |
| 349 | if le c 57 |
| 350 | ret int sub c 48 |
| 351 | end |
| 352 | end |
| 353 | if ge c 97 |
| 354 | if le c 102 |
| 355 | ret add 10 int sub c 97 |
| 356 | end |
| 357 | end |
| 358 | ret sub int 0 1 |
| 359 | end |
| 360 | |
| 361 | fn aien_mid_from_text src view uchar 96 slen ulong dst edit uchar 33 to int |
| 362 | if ne slen 88 |
| 363 | ret sub int 0 3 |
| 364 | end |
| 365 | io = new uchar 48 0 |
| 366 | for i ulong 44 |
| 367 | hi = mid_hex_nib get src mul i 2 |
| 368 | if lt hi 0 |
| 369 | ret sub int 0 3 |
| 370 | end |
| 371 | lo = mid_hex_nib get src add mul i 2 1 |
| 372 | if lt lo 0 |
| 373 | ret sub int 0 3 |
| 374 | end |
| 375 | put io i uchar or shl uint hi 4 uint lo |
| 376 | end |
| 377 | ret aien_mid_decode view io 44 edit dst |
| 378 | end |
| 379 | |
| 380 | fn mid_put_u16_ren off ulong v ulong to void |
| 381 | put g_mid_ren_pkt off uchar and v 255 |
| 382 | put g_mid_ren_pkt add off 1 uchar and shr v 8 255 |
| 383 | end |
| 384 | |
| 385 | fn mid_store_rename tlen ulong plen ulong to int |
| 386 | mid_put_u16_ren 0 2 |
| 387 | mid_put_u16_ren 2 tlen |
| 388 | for i ulong tlen |
| 389 | put g_mid_ren_pkt add 4 i get g_mid_tmp_t i |
| 390 | end |
| 391 | off2 = add 4 tlen |
| 392 | mid_put_u16_ren off2 plen |
| 393 | for i ulong plen |
| 394 | put g_mid_ren_pkt add add off2 2 i get g_mid_tmp_p i |
| 395 | end |
| 396 | pkt_len = add add off2 2 plen |
| 397 | rr = renames g_mid_ren_pkt 0 pkt_len 0 |
| 398 | if ne rr 0 |
| 399 | ign = unlink g_mid_tmp_t 0 tlen |
| 400 | ret sub int 0 5 |
| 401 | end |
| 402 | ret 0 |
| 403 | end |
| 404 | |
| 405 | fn aien_mid_store path view uchar 4096 plen ulong m view uchar 33 to int |
| 406 | rec = new uchar 44 0 |
| 407 | rc = aien_mid_encode view m edit rec |
| 408 | if ne rc 0 |
| 409 | ret rc |
| 410 | end |
| 411 | for i ulong 44 |
| 412 | put g_mid_rec_tmp i get rec i |
| 413 | end |
| 414 | if ne mid_path_check_g view path plen 4091 0 |
| 415 | ret sub int 0 1 |
| 416 | end |
| 417 | for i ulong plen |
| 418 | put g_mid_tmp_t i get g_mid_tmp_p i |
| 419 | end |
| 420 | put g_mid_tmp_t plen uchar 46 |
| 421 | put g_mid_tmp_t add plen 1 uchar 116 |
| 422 | put g_mid_tmp_t add plen 2 uchar 109 |
| 423 | put g_mid_tmp_t add plen 3 uchar 112 |
| 424 | tlen = add plen 4 |
| 425 | fd = fopen g_mid_tmp_t 0 tlen 577 420 |
| 426 | if lt fd 0 |
| 427 | ret sub int 0 5 |
| 428 | end |
| 429 | fdi = int fd |
| 430 | w = write fdi g_mid_rec_tmp 0 44 |
| 431 | if ne w 44 |
| 432 | ign = fclose fdi |
| 433 | ign = unlink g_mid_tmp_t 0 tlen |
| 434 | ret sub int 0 5 |
| 435 | end |
| 436 | fs = fsync fdi |
| 437 | cl = fclose fdi |
| 438 | if or ne fs 0 ne cl 0 |
| 439 | ign = unlink g_mid_tmp_t 0 tlen |
| 440 | ret sub int 0 5 |
| 441 | end |
| 442 | ret mid_store_rename tlen plen |
| 443 | end |
| 444 | |
| 445 | fn aien_mid_load path view uchar 4096 plen ulong dst edit uchar 33 to int |
| 446 | if ne mid_path_check_g view path plen 4096 0 |
| 447 | ret sub int 0 1 |
| 448 | end |
| 449 | fd = fopen g_mid_tmp_p 0 plen 0 0 |
| 450 | if lt fd 0 |
| 451 | ret sub int 0 5 |
| 452 | end |
| 453 | fdi = int fd |
| 454 | got = ulong 0 |
| 455 | cont = int 1 |
| 456 | while ne cont 0 |
| 457 | r = read fdi g_mid_io_buf got sub 45 got |
| 458 | if lt r 0 |
| 459 | ign = fclose fdi |
| 460 | ret sub int 0 5 |
| 461 | end |
| 462 | if eq r 0 |
| 463 | cont = int 0 |
| 464 | else |
| 465 | got = add got ulong r |
| 466 | if ge got 45 |
| 467 | cont = int 0 |
| 468 | end |
| 469 | end |
| 470 | end |
| 471 | ign2 = fclose fdi |
| 472 | io = new uchar 48 0 |
| 473 | for i ulong 48 |
| 474 | put io i get g_mid_io_buf i |
| 475 | end |
| 476 | ret aien_mid_decode view io got edit dst |
| 477 | end |
| 478 | |
| 479 | fn aien_mid_index_init cap uint to void |
| 480 | c = cap |
| 481 | if gt c 64 |
| 482 | c = uint 64 |
| 483 | end |
| 484 | put g_mid_idx_meta 0 c |
| 485 | put g_mid_idx_meta 1 uint 0 |
| 486 | end |
| 487 | |
| 488 | fn aien_mid_index_find m view uchar 33 to uint |
| 489 | cnt = get g_mid_idx_meta 1 |
| 490 | i = uint 0 |
| 491 | while lt i cnt |
| 492 | base = mul ulong i 33 |
| 493 | same = int 1 |
| 494 | for j ulong 32 |
| 495 | if ne get g_mid_idx_slots add add base 1 j get m add 1 j |
| 496 | same = int 0 |
| 497 | end |
| 498 | end |
| 499 | if ne same 0 |
| 500 | ret add i 1 |
| 501 | end |
| 502 | i = add i 1 |
| 503 | end |
| 504 | ret uint 0 |
| 505 | end |
| 506 | |
| 507 | fn aien_mid_index_bind m view uchar 33 to uint |
| 508 | if eq mid_root_valid get m 0 0 |
| 509 | ret uint 0 |
| 510 | end |
| 511 | acc = uint 0 |
| 512 | for j ulong 32 |
| 513 | acc = or acc uint get m add 1 j |
| 514 | end |
| 515 | if eq acc 0 |
| 516 | ret uint 0 |
| 517 | end |
| 518 | k = aien_mid_index_find view m |
| 519 | if ne k 0 |
| 520 | ret k |
| 521 | end |
| 522 | cnt = get g_mid_idx_meta 1 |
| 523 | cap = get g_mid_idx_meta 0 |
| 524 | if ge cnt cap |
| 525 | ret uint 0 |
| 526 | end |
| 527 | base = mul ulong cnt 33 |
| 528 | for j ulong 33 |
| 529 | put g_mid_idx_slots add base j get m j |
| 530 | end |
| 531 | cnt = add cnt 1 |
| 532 | put g_mid_idx_meta 1 cnt |
| 533 | ret cnt |
| 534 | end |
| 535 | |
| 536 | fn aien_mid_index_get idx uint dst edit uchar 33 to int |
| 537 | cnt = get g_mid_idx_meta 1 |
| 538 | if eq idx 0 |
| 539 | ret sub int 0 1 |
| 540 | end |
| 541 | if gt idx cnt |
| 542 | ret sub int 0 1 |
| 543 | end |
| 544 | base = mul ulong sub idx 1 33 |
| 545 | for j ulong 33 |
| 546 | put dst j get g_mid_idx_slots add base j |
| 547 | end |
| 548 | ret 0 |
| 549 | end |