Interplane page as plain text, for reading tools and agents. English is the reference text: translate it with your own tool. Text in backticks and in ``` blocks (commands, fingerprints, file names) and link targets in <...> are exact: copy them, never translate them. Page: /drakestapleton/omega-mv/blob/main/aienux/crates/aienos-anchor/anchor.om # 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 Sign in to star 1 stars 0 watching About Files History Releases Proposed changes Issues For developers branch `main` Branches Tags History `omega-mv / aienux / crates / aienos-anchor / anchor.om` 564 lines ยท 12784 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note aienux/crates/aienos-anchor/anchor.om: Pure-Omega AIENOS Anchor log. 3 note Converted from aienux crates/aienos-anchor/src/lib.rs. 4 note Append-only, hash-chained, authenticated log of checked facts. 5 note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-anchor/anchor.om ... 6 7 const ANCHOR_MAX_KEY ulong 64 8 const ANCHOR_MAX_VAL ulong 128 9 const ANCHOR_MAX_SRC ulong 64 10 const ANCHOR_MAX_ENTRIES ulong 16 11 const ANCHOR_MAX_TRUSTED ulong 4 12 const ANCHOR_MAX_WIRE ulong 512 13 14 const ANCHOR_KIND_ADMIT uchar 1 15 const ANCHOR_KIND_RETRACT uchar 2 16 17 const ANCHOR_OK int 0 18 const ANCHOR_ERR_MALFORMED int -1 19 const ANCHOR_ERR_TOO_LARGE int -2 20 const ANCHOR_ERR_OUT_OF_ORDER int -3 21 const ANCHOR_ERR_BROKEN_CHAIN int -4 22 const ANCHOR_ERR_UNTRUSTED int -5 23 const ANCHOR_ERR_BAD_SIG int -6 24 const ANCHOR_ERR_RETRACT_UNKNOWN int -7 25 const ANCHOR_ERR_RETRACT_VALUE int -8 26 const ANCHOR_ERR_CAPACITY int -9 27 28 const ANCHOR_ANS_UNKNOWN int 0 29 const ANCHOR_ANS_KNOWN int 1 30 const ANCHOR_ANS_RETRACTED int 2 31 32 text anchor_domain "AIENOS-ANCHOR-v0" 33 34 global anchor_trusted uchar 128 35 global anchor_trusted_cnt ulong 1 36 global anchor_head uchar 32 37 global anchor_cnt ulong 1 38 39 global anchor_e_seq ulong 16 40 global anchor_e_prev uchar 512 41 global anchor_e_kind uchar 16 42 global anchor_e_ts ulong 16 43 global anchor_e_klen ulong 16 44 global anchor_e_key uchar 1024 45 global anchor_e_vlen ulong 16 46 global anchor_e_val uchar 2048 47 global anchor_e_slen ulong 16 48 global anchor_e_src uchar 1024 49 global anchor_e_signer uchar 512 50 global anchor_e_sig uchar 1024 51 52 note Staging slot for candidate entry construction and decoding 53 global anchor_st_seq ulong 1 54 global anchor_st_prev uchar 32 55 global anchor_st_kind uchar 1 56 global anchor_st_ts ulong 1 57 global anchor_st_klen ulong 1 58 global anchor_st_key uchar 64 59 global anchor_st_vlen ulong 1 60 global anchor_st_val uchar 128 61 global anchor_st_slen ulong 1 62 global anchor_st_src uchar 64 63 global anchor_st_signer uchar 32 64 global anchor_st_sig uchar 64 65 66 note Query result metadata: 0=ans_kind, 1=entry_idx, 2=seq 67 global anchor_q_res ulong 3 68 69 fn anchor_put_u16_le buf edit uchar * off ulong v ulong to void 70 put buf off uchar and v 0xff 71 put buf add off 1 uchar and shr v 8 0xff 72 end 73 74 fn anchor_put_u32_le buf edit uchar * off ulong v ulong to void 75 put buf off uchar and v 0xff 76 put buf add off 1 uchar and shr v 8 0xff 77 put buf add off 2 uchar and shr v 16 0xff 78 put buf add off 3 uchar and shr v 24 0xff 79 end 80 81 fn anchor_put_u64_le buf edit uchar * off ulong v ulong to void 82 for i ulong 8 83 sh = mul i 8 84 put buf add off i uchar and shr v sh 0xff 85 end 86 end 87 88 fn anchor_get_u16_le buf view uchar * off ulong to ulong 89 b0 = ulong get buf off 90 b1 = ulong get buf add off 1 91 ret or b0 shl b1 8 92 end 93 94 fn anchor_get_u32_le buf view uchar * off ulong to ulong 95 b0 = ulong get buf off 96 b1 = ulong get buf add off 1 97 b2 = ulong get buf add off 2 98 b3 = ulong get buf add off 3 99 ret or or b0 shl b1 8 or shl b2 16 shl b3 24 100 end 101 102 fn anchor_get_u64_le buf view uchar * off ulong to ulong 103 acc = ulong 0 104 for i ulong 8 105 b = ulong get buf add off i 106 acc = or acc shl b mul i 8 107 end 108 ret acc 109 end 110 111 fn anchor_init to void 112 put anchor_trusted_cnt 0 0 113 put anchor_cnt 0 0 114 for i ulong 32 115 put anchor_head i 0 116 end 117 end 118 119 fn anchor_add_trusted key view uchar * to int 120 if ne len key 32 121 ret ANCHOR_ERR_MALFORMED 122 end 123 cnt = get anchor_trusted_cnt 0 124 if ge cnt ANCHOR_MAX_TRUSTED 125 ret ANCHOR_ERR_CAPACITY 126 end 127 base = mul cnt 32 128 for i ulong 32 129 put anchor_trusted add base i get key i 130 end 131 put anchor_trusted_cnt 0 add cnt 1 132 ret ANCHOR_OK 133 end 134 135 fn anchor_is_trusted_staged to bool 136 cnt = get anchor_trusted_cnt 0 137 for t ulong cnt bound 4 138 base = mul t 32 139 same = true 140 for i ulong 32 141 if ne get anchor_trusted add base i get anchor_st_signer i 142 same = false 143 end 144 end 145 if same 146 ret true 147 end 148 end 149 ret false 150 end 151 152 fn anchor_stage_hdr seq ulong kind uchar ts ulong to void 153 put anchor_st_seq 0 seq 154 for i ulong 32 155 put anchor_st_prev i get anchor_head i 156 end 157 put anchor_st_kind 0 kind 158 put anchor_st_ts 0 ts 159 put anchor_st_slen 0 0 160 end 161 162 fn anchor_stage_kv k view uchar * v view uchar * to int 163 if or eq len k 0 gt len k ANCHOR_MAX_KEY 164 ret ANCHOR_ERR_TOO_LARGE 165 end 166 if gt len v ANCHOR_MAX_VAL 167 ret ANCHOR_ERR_TOO_LARGE 168 end 169 put anchor_st_klen 0 len k 170 for i ulong len k bound 64 171 put anchor_st_key i get k i 172 end 173 put anchor_st_vlen 0 len v 174 for i ulong len v bound 128 175 put anchor_st_val i get v i 176 end 177 ret ANCHOR_OK 178 end 179 180 fn anchor_stage_source src view uchar * to int 181 if gt len src ANCHOR_MAX_SRC 182 ret ANCHOR_ERR_TOO_LARGE 183 end 184 put anchor_st_slen 0 len src 185 for i ulong len src bound 64 186 put anchor_st_src i get src i 187 end 188 ret ANCHOR_OK 189 end 190 191 fn anchor_stage_prev prev view uchar * to void 192 for i ulong 32 193 put anchor_st_prev i get prev i 194 end 195 end 196 197 fn anchor_stage_signer signer view uchar * to void 198 for i ulong 32 199 put anchor_st_signer i get signer i 200 end 201 end 202 203 fn anchor_encode_staged_body buf edit uchar * to ulong 204 for i ulong 16 205 put buf i get anchor_domain i 206 end 207 anchor_put_u64_le buf 16 get anchor_st_seq 0 208 for i ulong 32 209 put buf add 24 i get anchor_st_prev i 210 end 211 put buf 56 get anchor_st_kind 0 212 anchor_put_u64_le buf 57 get anchor_st_ts 0 213 pos = ulong 65 214 kl = get anchor_st_klen 0 215 anchor_put_u16_le buf pos kl 216 pos = add pos 2 217 for i ulong kl bound 64 218 put buf add pos i get anchor_st_key i 219 end 220 pos = add pos kl 221 vl = get anchor_st_vlen 0 222 anchor_put_u32_le buf pos vl 223 pos = add pos 4 224 for i ulong vl bound 128 225 put buf add pos i get anchor_st_val i 226 end 227 pos = add pos vl 228 sl = get anchor_st_slen 0 229 anchor_put_u16_le buf pos sl 230 pos = add pos 2 231 for i ulong sl bound 64 232 put buf add pos i get anchor_st_src i 233 end 234 pos = add pos sl 235 ret pos 236 end 237 238 fn anchor_compute_sig_staged sig_out edit uchar * to void 239 body = new uchar 512 0 240 blen = anchor_encode_staged_body body 241 ctx = new ulong 40 0 242 sha256_init ctx 243 skey = new uchar 32 0 244 for i ulong 32 245 put skey i get anchor_st_signer i 246 end 247 sha256_update ctx skey 248 sha256_update ctx part body 0 blen 249 d1 = new uchar 32 0 250 sha256_final ctx d1 251 for i ulong 32 252 put sig_out i get d1 i 253 end 254 sha256_init ctx 255 sha256_update ctx d1 256 sha256_update ctx anchor_domain 257 d2 = new uchar 32 0 258 sha256_final ctx d2 259 for i ulong 32 260 put sig_out add 32 i get d2 i 261 end 262 end 263 264 fn anchor_sign_staged signer view uchar * to void 265 anchor_stage_signer signer 266 sig = new uchar 64 0 267 anchor_compute_sig_staged sig 268 for i ulong 64 269 put anchor_st_sig i get sig i 270 end 271 end 272 273 fn anchor_query key view uchar * to int 274 cnt = get anchor_cnt 0 275 if eq cnt 0 276 put anchor_q_res 0 ulong ANCHOR_ANS_UNKNOWN 277 put anchor_q_res 1 0 278 put anchor_q_res 2 0 279 ret ANCHOR_ANS_UNKNOWN 280 end 281 idx = cnt 282 while gt idx 0 bound 16 283 idx = sub idx 1 284 kl = get anchor_e_klen idx 285 if eq kl len key 286 base = mul idx ANCHOR_MAX_KEY 287 same = true 288 for i ulong kl bound 64 289 if ne get anchor_e_key add base i get key i 290 same = false 291 end 292 end 293 if same 294 knd = get anchor_e_kind idx 295 put anchor_q_res 1 idx 296 put anchor_q_res 2 get anchor_e_seq idx 297 if eq knd ANCHOR_KIND_ADMIT 298 put anchor_q_res 0 ulong ANCHOR_ANS_KNOWN 299 ret ANCHOR_ANS_KNOWN 300 end 301 put anchor_q_res 0 ulong ANCHOR_ANS_RETRACTED 302 ret ANCHOR_ANS_RETRACTED 303 end 304 end 305 end 306 put anchor_q_res 0 ulong ANCHOR_ANS_UNKNOWN 307 put anchor_q_res 1 0 308 put anchor_q_res 2 0 309 ret ANCHOR_ANS_UNKNOWN 310 end 311 312 fn anchor_staged_key_is_known to bool 313 kl = get anchor_st_klen 0 314 kbuf = new uchar 64 0 315 for i ulong kl bound 64 316 put kbuf i get anchor_st_key i 317 end 318 ans = anchor_query part kbuf 0 kl 319 ret eq ans ANCHOR_ANS_KNOWN 320 end 321 322 fn anchor_commit_staged cnt ulong to void 323 put anchor_e_seq cnt get anchor_st_seq 0 324 pbase = mul cnt 32 325 for i ulong 32 326 put anchor_e_prev add pbase i get anchor_st_prev i 327 end 328 put anchor_e_kind cnt get anchor_st_kind 0 329 put anchor_e_ts cnt get anchor_st_ts 0 330 kl = get anchor_st_klen 0 331 put anchor_e_klen cnt kl 332 kbase = mul cnt ANCHOR_MAX_KEY 333 for i ulong kl bound 64 334 put anchor_e_key add kbase i get anchor_st_key i 335 end 336 vl = get anchor_st_vlen 0 337 put anchor_e_vlen cnt vl 338 vbase = mul cnt ANCHOR_MAX_VAL 339 for i ulong vl bound 128 340 put anchor_e_val add vbase i get anchor_st_val i 341 end 342 sl = get anchor_st_slen 0 343 put anchor_e_slen cnt sl 344 sbase = mul cnt ANCHOR_MAX_SRC 345 for i ulong sl bound 64 346 put anchor_e_src add sbase i get anchor_st_src i 347 end 348 for i ulong 32 349 put anchor_e_signer add pbase i get anchor_st_signer i 350 end 351 sigbase = mul cnt 64 352 for i ulong 64 353 put anchor_e_sig add sigbase i get anchor_st_sig i 354 end 355 put anchor_cnt 0 add cnt 1 356 end 357 358 fn anchor_update_head_staged to int 359 wire = new uchar 512 0 360 blen = anchor_encode_staged_body wire 361 for i ulong 32 362 put wire add blen i get anchor_st_signer i 363 end 364 for i ulong 64 365 put wire add add blen 32 i get anchor_st_sig i 366 end 367 tlen = add blen 96 368 dig = new uchar 32 0 369 dlen = sha2_digest 256 part wire 0 tlen dig 370 if ne dlen 32 371 ret ANCHOR_ERR_MALFORMED 372 end 373 for i ulong 32 374 put anchor_head i get dig i 375 end 376 ret ANCHOR_OK 377 end 378 379 fn anchor_append_staged to int 380 kl = get anchor_st_klen 0 381 vl = get anchor_st_vlen 0 382 sl = get anchor_st_slen 0 383 if or eq kl 0 gt kl ANCHOR_MAX_KEY 384 ret ANCHOR_ERR_TOO_LARGE 385 end 386 if or gt vl ANCHOR_MAX_VAL gt sl ANCHOR_MAX_SRC 387 ret ANCHOR_ERR_TOO_LARGE 388 end 389 knd = get anchor_st_kind 0 390 if and ne knd ANCHOR_KIND_ADMIT ne knd ANCHOR_KIND_RETRACT 391 ret ANCHOR_ERR_MALFORMED 392 end 393 if and eq knd ANCHOR_KIND_RETRACT gt vl 0 394 ret ANCHOR_ERR_RETRACT_VALUE 395 end 396 cnt = get anchor_cnt 0 397 if ge cnt ANCHOR_MAX_ENTRIES 398 ret ANCHOR_ERR_CAPACITY 399 end 400 if ne get anchor_st_seq 0 cnt 401 ret ANCHOR_ERR_OUT_OF_ORDER 402 end 403 for i ulong 32 404 if ne get anchor_st_prev i get anchor_head i 405 ret ANCHOR_ERR_BROKEN_CHAIN 406 end 407 end 408 if not anchor_is_trusted_staged 409 ret ANCHOR_ERR_UNTRUSTED 410 end 411 exp_sig = new uchar 64 0 412 anchor_compute_sig_staged exp_sig 413 for i ulong 64 414 if ne get anchor_st_sig i get exp_sig i 415 ret ANCHOR_ERR_BAD_SIG 416 end 417 end 418 if eq knd ANCHOR_KIND_RETRACT 419 if not anchor_staged_key_is_known 420 ret ANCHOR_ERR_RETRACT_UNKNOWN 421 end 422 end 423 rc = anchor_update_head_staged 424 if ne rc ANCHOR_OK 425 ret rc 426 end 427 anchor_commit_staged cnt 428 ret ANCHOR_OK 429 end 430 431 fn anchor_append_auth idx ulong buf edit uchar * blen ulong to ulong 432 pbase = mul idx 32 433 for i ulong 32 434 put buf add blen i get anchor_e_signer add pbase i 435 end 436 sigoff = add blen 32 437 sigbase = mul idx 64 438 for i ulong 64 439 put buf add sigoff i get anchor_e_sig add sigbase i 440 end 441 ret add blen 96 442 end 443 444 fn anchor_export_entry idx ulong buf edit uchar * to ulong 445 put anchor_st_seq 0 get anchor_e_seq idx 446 pbase = mul idx 32 447 for i ulong 32 448 put anchor_st_prev i get anchor_e_prev add pbase i 449 end 450 put anchor_st_kind 0 get anchor_e_kind idx 451 put anchor_st_ts 0 get anchor_e_ts idx 452 kl = get anchor_e_klen idx 453 put anchor_st_klen 0 kl 454 kbase = mul idx ANCHOR_MAX_KEY 455 for i ulong kl bound 64 456 put anchor_st_key i get anchor_e_key add kbase i 457 end 458 vl = get anchor_e_vlen idx 459 put anchor_st_vlen 0 vl 460 vbase = mul idx ANCHOR_MAX_VAL 461 for i ulong vl bound 128 462 put anchor_st_val i get anchor_e_val add vbase i 463 end 464 sl = get anchor_e_slen idx 465 put anchor_st_slen 0 sl 466 sbase = mul idx ANCHOR_MAX_SRC 467 for i ulong sl bound 64 468 put anchor_st_src i get anchor_e_src add sbase i 469 end 470 blen = anchor_encode_staged_body buf 471 ret anchor_append_auth idx buf blen 472 end 473 474 fn anchor_decode_to_staged buf view uchar * off ulong total ulong to long 475 if lt sub total off 169 476 ret long ANCHOR_ERR_MALFORMED 477 end 478 for i ulong 16 479 if ne get buf add off i get anchor_domain i 480 ret long ANCHOR_ERR_MALFORMED 481 end 482 end 483 put anchor_st_seq 0 anchor_get_u64_le buf add off 16 484 for i ulong 32 485 put anchor_st_prev i get buf add add off 24 i 486 end 487 knd = get buf add off 56 488 if and ne knd ANCHOR_KIND_ADMIT ne knd ANCHOR_KIND_RETRACT 489 ret long ANCHOR_ERR_MALFORMED 490 end 491 put anchor_st_kind 0 knd 492 put anchor_st_ts 0 anchor_get_u64_le buf add off 57 493 pos = add off 65 494 kl = anchor_get_u16_le buf pos 495 pos = add pos 2 496 if gt kl ANCHOR_MAX_KEY 497 ret long ANCHOR_ERR_TOO_LARGE 498 end 499 if gt add pos kl total 500 ret long ANCHOR_ERR_MALFORMED 501 end 502 put anchor_st_klen 0 kl 503 for i ulong kl bound 64 504 put anchor_st_key i get buf add pos i 505 end 506 pos = add pos kl 507 if gt add pos 4 total 508 ret long ANCHOR_ERR_MALFORMED 509 end 510 vl = anchor_get_u32_le buf pos 511 pos = add pos 4 512 if gt vl ANCHOR_MAX_VAL 513 ret long ANCHOR_ERR_TOO_LARGE 514 end 515 if gt add pos vl total 516 ret long ANCHOR_ERR_MALFORMED 517 end 518 put anchor_st_vlen 0 vl 519 for i ulong vl bound 128 520 put anchor_st_val i get buf add pos i 521 end 522 pos = add pos vl 523 if gt add pos 2 total 524 ret long ANCHOR_ERR_MALFORMED 525 end 526 sl = anchor_get_u16_le buf pos 527 pos = add pos 2 528 if gt sl ANCHOR_MAX_SRC 529 ret long ANCHOR_ERR_TOO_LARGE 530 end 531 if gt add add pos sl 96 total 532 ret long ANCHOR_ERR_MALFORMED 533 end 534 put anchor_st_slen 0 sl 535 for i ulong sl bound 64 536 put anchor_st_src i get buf add pos i 537 end 538 pos = add pos sl 539 for i ulong 32 540 put anchor_st_signer i get buf add pos i 541 end 542 pos = add pos 32 543 for i ulong 64 544 put anchor_st_sig i get buf add pos i 545 end 546 pos = add pos 64 547 ret long sub pos off 548 end 549 550 fn anchor_replay buf view uchar * total ulong to int 551 pos = ulong 0 552 while lt pos total bound 16 553 used = anchor_decode_to_staged buf pos total 554 if lt used 0 555 ret int used 556 end 557 rc = anchor_append_staged 558 if ne rc ANCHOR_OK 559 ret rc 560 end 561 pos = add pos ulong used 562 end 563 ret ANCHOR_OK 564 end