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 / aienux / crates / aienos-cortex / cortex.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note aienux/crates/aienos-cortex/cortex.om: Pure-Omega AIENOS Cortex Epistemic Store. |
| 3 | note Converted from aienux crates/aienos-cortex/src/{status,record,verification,store,lib}.rs. |
| 4 | note Preserves durable epistemic memory with verifiable SHA-256 provenance and rejects |
| 5 | note ungrounded hallucinations, missing parents, and circular assertions. |
| 6 | note Compose with: si/directives/om2.om om/tls/sha2.om aienux/crates/aienos-cortex/cortex.om ... |
| 7 | |
| 8 | const CORTEX_DIRECT_OBSERVATION uchar 1 |
| 9 | const CORTEX_VERIFIED_FACT uchar 2 |
| 10 | const CORTEX_INFERENCE uchar 3 |
| 11 | const CORTEX_HYPOTHESIS uchar 4 |
| 12 | const CORTEX_CONTRADICTION uchar 5 |
| 13 | const CORTEX_OPERATOR_DECISION uchar 6 |
| 14 | |
| 15 | const CORTEX_OK int 0 |
| 16 | const CORTEX_ERR_UNGROUNDED int -1 |
| 17 | const CORTEX_ERR_CIRCULAR int -2 |
| 18 | const CORTEX_ERR_NOT_FOUND int -3 |
| 19 | const CORTEX_ERR_CONTRADICTION int -4 |
| 20 | const CORTEX_ERR_CORRUPT_JOURNAL int -5 |
| 21 | const CORTEX_ERR_CHECKSUM int -6 |
| 22 | const CORTEX_ERR_CAPACITY int -7 |
| 23 | |
| 24 | const CORTEX_MAX_RECS ulong 32 |
| 25 | const CORTEX_MAX_STMT ulong 96 |
| 26 | const CORTEX_MAX_PROV ulong 64 |
| 27 | const CORTEX_MAX_VERIF ulong 48 |
| 28 | const CORTEX_MAX_PARENTS ulong 4 |
| 29 | const CORTEX_ID_LEN ulong 16 |
| 30 | const CORTEX_HASH_LEN ulong 32 |
| 31 | |
| 32 | note Epistemic store arrays (up to 32 records) |
| 33 | global cx_cnt ulong 1 |
| 34 | global cx_id uchar 512 |
| 35 | global cx_status uchar 32 |
| 36 | global cx_stmt uchar 3072 |
| 37 | global cx_stmt_len ulong 32 |
| 38 | global cx_prov uchar 2048 |
| 39 | global cx_prov_len ulong 32 |
| 40 | global cx_ev_hash uchar 1024 |
| 41 | global cx_ts ulong 32 |
| 42 | global cx_conf f32 32 |
| 43 | global cx_verif uchar 1536 |
| 44 | global cx_verif_len ulong 32 |
| 45 | global cx_has_verif uchar 32 |
| 46 | global cx_pcnt ulong 32 |
| 47 | global cx_parents uchar 2048 |
| 48 | |
| 49 | note Staging slot for record construction and grounding check |
| 50 | global cx_st_id uchar 16 |
| 51 | global cx_st_status uchar 1 |
| 52 | global cx_st_stmt uchar 96 |
| 53 | global cx_st_stmt_len ulong 1 |
| 54 | global cx_st_prov uchar 64 |
| 55 | global cx_st_prov_len ulong 1 |
| 56 | global cx_st_payload uchar 256 |
| 57 | global cx_st_pay_len ulong 1 |
| 58 | global cx_st_ev_hash uchar 32 |
| 59 | global cx_st_ts ulong 1 |
| 60 | global cx_st_conf f32 1 |
| 61 | global cx_st_verif uchar 48 |
| 62 | global cx_st_verif_len ulong 1 |
| 63 | global cx_st_has_verif uchar 1 |
| 64 | global cx_st_pcnt ulong 1 |
| 65 | global cx_st_parents uchar 64 |
| 66 | |
| 67 | note Scratch buffers for DFS grounding check and journal sort |
| 68 | global cx_grounded uchar 32 |
| 69 | global cx_on_path uchar 32 |
| 70 | global cx_order ulong 32 |
| 71 | |
| 72 | text cx_op_prefix "operator:" |
| 73 | text cx_sentinel "cortex_sentinel" |
| 74 | text cx_contra_prefix "Contradiction detected: " |
| 75 | text cx_jmagic "CXJ1" |
| 76 | |
| 77 | fn cx_is_ground_truth st uchar to bool |
| 78 | if eq st CORTEX_DIRECT_OBSERVATION |
| 79 | ret true |
| 80 | end |
| 81 | if eq st CORTEX_VERIFIED_FACT |
| 82 | ret true |
| 83 | end |
| 84 | if eq st CORTEX_OPERATOR_DECISION |
| 85 | ret true |
| 86 | end |
| 87 | ret false |
| 88 | end |
| 89 | |
| 90 | fn cx_requires_grounding st uchar to bool |
| 91 | if eq st CORTEX_INFERENCE |
| 92 | ret true |
| 93 | end |
| 94 | if eq st CORTEX_HYPOTHESIS |
| 95 | ret true |
| 96 | end |
| 97 | ret false |
| 98 | end |
| 99 | |
| 100 | fn cx_clamp01 v f32 to f32 |
| 101 | if lt v 0.0f32 |
| 102 | ret 0.0f32 |
| 103 | end |
| 104 | if gt v 1.0f32 |
| 105 | ret 1.0f32 |
| 106 | end |
| 107 | ret v |
| 108 | end |
| 109 | |
| 110 | fn cx_put_u16_be buf edit uchar * off ulong v ulong to void |
| 111 | put buf off uchar and shr v 8 0xff |
| 112 | put buf add off 1 uchar and v 0xff |
| 113 | end |
| 114 | |
| 115 | fn cx_put_u32_be buf edit uchar * off ulong v ulong to void |
| 116 | put buf off uchar and shr v 24 0xff |
| 117 | put buf add off 1 uchar and shr v 16 0xff |
| 118 | put buf add off 2 uchar and shr v 8 0xff |
| 119 | put buf add off 3 uchar and v 0xff |
| 120 | end |
| 121 | |
| 122 | fn cx_put_u64_be buf edit uchar * off ulong v ulong to void |
| 123 | for i ulong 8 |
| 124 | sh = mul sub 7 i 8 |
| 125 | put buf add off i uchar and shr v sh 0xff |
| 126 | end |
| 127 | end |
| 128 | |
| 129 | fn cx_get_u16_be buf view uchar * off ulong to ulong |
| 130 | b0 = ulong get buf off |
| 131 | b1 = ulong get buf add off 1 |
| 132 | ret or shl b0 8 b1 |
| 133 | end |
| 134 | |
| 135 | fn cx_get_u32_be buf view uchar * off ulong to ulong |
| 136 | b0 = ulong get buf off |
| 137 | b1 = ulong get buf add off 1 |
| 138 | b2 = ulong get buf add off 2 |
| 139 | b3 = ulong get buf add off 3 |
| 140 | ret or or shl b0 24 shl b1 16 or shl b2 8 b3 |
| 141 | end |
| 142 | |
| 143 | fn cx_get_u64_be buf view uchar * off ulong to ulong |
| 144 | acc = ulong 0 |
| 145 | for i ulong 8 |
| 146 | b = ulong get buf add off i |
| 147 | acc = or shl acc 8 b |
| 148 | end |
| 149 | ret acc |
| 150 | end |
| 151 | |
| 152 | fn cx_init to void |
| 153 | put cx_cnt 0 0 |
| 154 | put cx_st_pcnt 0 0 |
| 155 | put cx_st_has_verif 0 0 |
| 156 | put cx_st_verif_len 0 0 |
| 157 | put cx_st_pay_len 0 0 |
| 158 | end |
| 159 | |
| 160 | fn cx_id_eq_slot idx ulong id view uchar 16 to bool |
| 161 | base = mul idx CORTEX_ID_LEN |
| 162 | for i ulong 16 |
| 163 | if ne get cx_id add base i get id i |
| 164 | ret false |
| 165 | end |
| 166 | end |
| 167 | ret true |
| 168 | end |
| 169 | |
| 170 | fn cx_find_by_id id view uchar 16 to long |
| 171 | n = get cx_cnt 0 |
| 172 | for i ulong n bound 32 |
| 173 | if cx_id_eq_slot i id |
| 174 | ret long i |
| 175 | end |
| 176 | end |
| 177 | ret -1 |
| 178 | end |
| 179 | |
| 180 | fn cx_count_by_status st uchar to ulong |
| 181 | n = get cx_cnt 0 |
| 182 | c = ulong 0 |
| 183 | for i ulong n bound 32 |
| 184 | if eq get cx_status i st |
| 185 | c = add c 1 |
| 186 | end |
| 187 | end |
| 188 | ret c |
| 189 | end |
| 190 | |
| 191 | fn cx_stage_reset to void |
| 192 | put cx_st_status 0 0 |
| 193 | put cx_st_stmt_len 0 0 |
| 194 | put cx_st_prov_len 0 0 |
| 195 | put cx_st_pay_len 0 0 |
| 196 | put cx_st_ts 0 0 |
| 197 | put cx_st_conf 0 1.0f32 |
| 198 | put cx_st_verif_len 0 0 |
| 199 | put cx_st_has_verif 0 0 |
| 200 | put cx_st_pcnt 0 0 |
| 201 | end |
| 202 | |
| 203 | fn cx_stage_add_parent pid view uchar 16 to int |
| 204 | pc = get cx_st_pcnt 0 |
| 205 | if ge pc CORTEX_MAX_PARENTS |
| 206 | ret CORTEX_ERR_CAPACITY |
| 207 | end |
| 208 | base = mul pc CORTEX_ID_LEN |
| 209 | for i ulong 16 |
| 210 | put cx_st_parents add base i get pid i |
| 211 | end |
| 212 | put cx_st_pcnt 0 add pc 1 |
| 213 | ret CORTEX_OK |
| 214 | end |
| 215 | |
| 216 | fn cx_stage_set_verifier v view uchar * to int |
| 217 | if gt len v CORTEX_MAX_VERIF |
| 218 | ret CORTEX_ERR_CAPACITY |
| 219 | end |
| 220 | for i ulong len v bound 48 |
| 221 | put cx_st_verif i get v i |
| 222 | end |
| 223 | put cx_st_verif_len 0 len v |
| 224 | put cx_st_has_verif 0 1 |
| 225 | ret CORTEX_OK |
| 226 | end |
| 227 | |
| 228 | fn cx_stage_set_payload payload view uchar * to int |
| 229 | if gt len payload 256 |
| 230 | ret CORTEX_ERR_CAPACITY |
| 231 | end |
| 232 | for i ulong len payload bound 256 |
| 233 | put cx_st_payload i get payload i |
| 234 | end |
| 235 | put cx_st_pay_len 0 len payload |
| 236 | ret CORTEX_OK |
| 237 | end |
| 238 | |
| 239 | fn cx_stage_set_conf conf f32 to void |
| 240 | put cx_st_conf 0 cx_clamp01 conf |
| 241 | end |
| 242 | |
| 243 | note Compute evidence_hash and id from staged globals |
| 244 | fn cx_stage_compute_hashes to void |
| 245 | hbuf = new uchar 512 0 |
| 246 | pos = ulong 0 |
| 247 | put hbuf 0 get cx_st_status 0 |
| 248 | pos = 1 |
| 249 | sl = get cx_st_stmt_len 0 |
| 250 | for i ulong sl bound 96 |
| 251 | put hbuf add pos i get cx_st_stmt i |
| 252 | end |
| 253 | pos = add pos sl |
| 254 | pl = get cx_st_prov_len 0 |
| 255 | for i ulong pl bound 64 |
| 256 | put hbuf add pos i get cx_st_prov i |
| 257 | end |
| 258 | pos = add pos pl |
| 259 | payl = get cx_st_pay_len 0 |
| 260 | for i ulong payl bound 256 |
| 261 | put hbuf add pos i get cx_st_payload i |
| 262 | end |
| 263 | pos = add pos payl |
| 264 | pc = get cx_st_pcnt 0 |
| 265 | for i ulong mul pc 16 bound 64 |
| 266 | put hbuf add pos i get cx_st_parents i |
| 267 | end |
| 268 | pos = add pos mul pc 16 |
| 269 | |
| 270 | dig = new uchar 32 0 |
| 271 | dl = sha2_digest 256 part hbuf 0 pos dig |
| 272 | for i ulong 32 |
| 273 | put cx_st_ev_hash i get dig i |
| 274 | end |
| 275 | |
| 276 | id_in = new uchar 40 0 |
| 277 | for i ulong 32 |
| 278 | put id_in i get dig i |
| 279 | end |
| 280 | cx_put_u64_be id_in 32 get cx_st_ts 0 |
| 281 | dl = sha2_digest 256 id_in dig |
| 282 | for i ulong 16 |
| 283 | put cx_st_id i get dig i |
| 284 | end |
| 285 | end |
| 286 | |
| 287 | fn cx_stage_build st uchar stmt view uchar * prov view uchar * ts ulong to int |
| 288 | if or gt len stmt CORTEX_MAX_STMT gt len prov CORTEX_MAX_PROV |
| 289 | ret CORTEX_ERR_CAPACITY |
| 290 | end |
| 291 | pc = get cx_st_pcnt 0 |
| 292 | payl = get cx_st_pay_len 0 |
| 293 | need = add add add 1 len stmt len prov add payl mul pc 16 |
| 294 | if gt need 512 |
| 295 | ret CORTEX_ERR_CAPACITY |
| 296 | end |
| 297 | put cx_st_status 0 st |
| 298 | put cx_st_stmt_len 0 len stmt |
| 299 | for i ulong len stmt bound 96 |
| 300 | put cx_st_stmt i get stmt i |
| 301 | end |
| 302 | put cx_st_prov_len 0 len prov |
| 303 | for i ulong len prov bound 64 |
| 304 | put cx_st_prov i get prov i |
| 305 | end |
| 306 | put cx_st_ts 0 ts |
| 307 | cx_stage_compute_hashes |
| 308 | ret CORTEX_OK |
| 309 | end |
| 310 | |
| 311 | fn cx_copy_staged_to_slot slot ulong to void |
| 312 | base = mul slot CORTEX_ID_LEN |
| 313 | for i ulong 16 |
| 314 | put cx_id add base i get cx_st_id i |
| 315 | end |
| 316 | put cx_status slot get cx_st_status 0 |
| 317 | nlen = get cx_st_stmt_len 0 |
| 318 | put cx_stmt_len slot nlen |
| 319 | base = mul slot CORTEX_MAX_STMT |
| 320 | for i ulong nlen bound 96 |
| 321 | put cx_stmt add base i get cx_st_stmt i |
| 322 | end |
| 323 | nlen = get cx_st_prov_len 0 |
| 324 | put cx_prov_len slot nlen |
| 325 | base = mul slot CORTEX_MAX_PROV |
| 326 | for i ulong nlen bound 64 |
| 327 | put cx_prov add base i get cx_st_prov i |
| 328 | end |
| 329 | base = mul slot CORTEX_HASH_LEN |
| 330 | for i ulong 32 |
| 331 | put cx_ev_hash add base i get cx_st_ev_hash i |
| 332 | end |
| 333 | put cx_ts slot get cx_st_ts 0 |
| 334 | put cx_conf slot get cx_st_conf 0 |
| 335 | nlen = get cx_st_verif_len 0 |
| 336 | put cx_verif_len slot nlen |
| 337 | put cx_has_verif slot get cx_st_has_verif 0 |
| 338 | base = mul slot CORTEX_MAX_VERIF |
| 339 | for i ulong nlen bound 48 |
| 340 | put cx_verif add base i get cx_st_verif i |
| 341 | end |
| 342 | nlen = get cx_st_pcnt 0 |
| 343 | put cx_pcnt slot nlen |
| 344 | base = mul slot mul CORTEX_MAX_PARENTS CORTEX_ID_LEN |
| 345 | for i ulong mul nlen 16 bound 64 |
| 346 | put cx_parents add base i get cx_st_parents i |
| 347 | end |
| 348 | end |
| 349 | |
| 350 | note Copy staged record into store slot idx (or overwrite if ID already exists) |
| 351 | fn cx_insert_staged to long |
| 352 | st_id = new uchar 16 0 |
| 353 | for i ulong 16 |
| 354 | put st_id i get cx_st_id i |
| 355 | end |
| 356 | existing = cx_find_by_id view st_id |
| 357 | slot = ulong 0 |
| 358 | if ge existing 0 |
| 359 | slot = ulong existing |
| 360 | else |
| 361 | n = get cx_cnt 0 |
| 362 | if ge n CORTEX_MAX_RECS |
| 363 | ret long CORTEX_ERR_CAPACITY |
| 364 | end |
| 365 | slot = n |
| 366 | put cx_cnt 0 add n 1 |
| 367 | end |
| 368 | cx_copy_staged_to_slot slot |
| 369 | ret long slot |
| 370 | end |
| 371 | |
| 372 | fn cx_check_node_slot idx ulong depth ulong to int |
| 373 | if ge depth CORTEX_MAX_RECS |
| 374 | ret CORTEX_ERR_CIRCULAR |
| 375 | end |
| 376 | if eq get cx_on_path idx 1 |
| 377 | ret CORTEX_ERR_CIRCULAR |
| 378 | end |
| 379 | if eq get cx_grounded idx 1 |
| 380 | ret CORTEX_OK |
| 381 | end |
| 382 | st = get cx_status idx |
| 383 | if cx_is_ground_truth st |
| 384 | put cx_grounded idx 1 |
| 385 | ret CORTEX_OK |
| 386 | end |
| 387 | pc = get cx_pcnt idx |
| 388 | if eq pc 0 |
| 389 | ret CORTEX_ERR_UNGROUNDED |
| 390 | end |
| 391 | put cx_on_path idx 1 |
| 392 | par_base = mul idx mul CORTEX_MAX_PARENTS CORTEX_ID_LEN |
| 393 | pid = new uchar 16 0 |
| 394 | for p ulong pc bound 4 |
| 395 | off = add par_base mul p 16 |
| 396 | for j ulong 16 |
| 397 | put pid j get cx_parents add off j |
| 398 | end |
| 399 | pidx = cx_find_by_id view pid |
| 400 | if lt pidx 0 |
| 401 | put cx_on_path idx 0 |
| 402 | ret CORTEX_ERR_NOT_FOUND |
| 403 | end |
| 404 | rc = cx_check_node_slot ulong pidx add depth 1 |
| 405 | if ne rc CORTEX_OK |
| 406 | put cx_on_path idx 0 |
| 407 | ret rc |
| 408 | end |
| 409 | end |
| 410 | put cx_on_path idx 0 |
| 411 | put cx_grounded idx 1 |
| 412 | ret CORTEX_OK |
| 413 | end |
| 414 | |
| 415 | fn cx_clear_dfs to void |
| 416 | for i ulong 32 |
| 417 | put cx_grounded i 0 |
| 418 | put cx_on_path i 0 |
| 419 | end |
| 420 | end |
| 421 | |
| 422 | fn cx_verify_slot idx ulong to int |
| 423 | if ge idx get cx_cnt 0 |
| 424 | ret CORTEX_ERR_NOT_FOUND |
| 425 | end |
| 426 | st = get cx_status idx |
| 427 | if cx_is_ground_truth st |
| 428 | ret CORTEX_OK |
| 429 | end |
| 430 | if eq get cx_pcnt idx 0 |
| 431 | ret CORTEX_ERR_UNGROUNDED |
| 432 | end |
| 433 | cx_clear_dfs |
| 434 | ret cx_check_node_slot idx 0 |
| 435 | end |
| 436 | |
| 437 | fn cx_verify_staged to int |
| 438 | st = get cx_st_status 0 |
| 439 | if cx_is_ground_truth st |
| 440 | ret CORTEX_OK |
| 441 | end |
| 442 | pc = get cx_st_pcnt 0 |
| 443 | if eq pc 0 |
| 444 | ret CORTEX_ERR_UNGROUNDED |
| 445 | end |
| 446 | cx_clear_dfs |
| 447 | st_id = new uchar 16 0 |
| 448 | for i ulong 16 |
| 449 | put st_id i get cx_st_id i |
| 450 | end |
| 451 | existing = cx_find_by_id view st_id |
| 452 | if ge existing 0 |
| 453 | put cx_on_path ulong existing 1 |
| 454 | end |
| 455 | pid = new uchar 16 0 |
| 456 | for p ulong pc bound 4 |
| 457 | off = mul p 16 |
| 458 | for j ulong 16 |
| 459 | put pid j get cx_st_parents add off j |
| 460 | end |
| 461 | same = true |
| 462 | for j ulong 16 |
| 463 | if ne get pid j get st_id j |
| 464 | same = false |
| 465 | end |
| 466 | end |
| 467 | if same |
| 468 | ret CORTEX_ERR_CIRCULAR |
| 469 | end |
| 470 | pidx = cx_find_by_id view pid |
| 471 | if lt pidx 0 |
| 472 | ret CORTEX_ERR_NOT_FOUND |
| 473 | end |
| 474 | rc = cx_check_node_slot ulong pidx 1 |
| 475 | if ne rc CORTEX_OK |
| 476 | ret rc |
| 477 | end |
| 478 | end |
| 479 | ret CORTEX_OK |
| 480 | end |
| 481 | |
| 482 | note High-level APIs mirroring CortexStore methods (all <= 6 parameter slots) |
| 483 | |
| 484 | fn cx_record_observation stmt view uchar * prov view uchar * ts ulong id_out edit uchar 16 to int |
| 485 | rc = cx_stage_build CORTEX_DIRECT_OBSERVATION stmt prov ts |
| 486 | if ne rc CORTEX_OK |
| 487 | ret rc |
| 488 | end |
| 489 | slot = cx_insert_staged |
| 490 | if lt slot 0 |
| 491 | ret int slot |
| 492 | end |
| 493 | for i ulong 16 |
| 494 | put id_out i get cx_st_id i |
| 495 | end |
| 496 | ret CORTEX_OK |
| 497 | end |
| 498 | |
| 499 | fn cx_record_verified_fact stmt view uchar * prov view uchar * ts ulong id_out edit uchar 16 to int |
| 500 | rc = cx_stage_build CORTEX_VERIFIED_FACT stmt prov ts |
| 501 | if ne rc CORTEX_OK |
| 502 | ret rc |
| 503 | end |
| 504 | slot = cx_insert_staged |
| 505 | if lt slot 0 |
| 506 | ret int slot |
| 507 | end |
| 508 | for i ulong 16 |
| 509 | put id_out i get cx_st_id i |
| 510 | end |
| 511 | ret CORTEX_OK |
| 512 | end |
| 513 | |
| 514 | fn cx_record_operator_decision stmt view uchar * sig view uchar * ts ulong id_out edit uchar 16 to int |
| 515 | cx_stage_reset |
| 516 | plen = add len cx_op_prefix len sig |
| 517 | if gt plen CORTEX_MAX_PROV |
| 518 | ret CORTEX_ERR_CAPACITY |
| 519 | end |
| 520 | prov = new uchar 64 0 |
| 521 | for i ulong len cx_op_prefix bound 16 |
| 522 | put prov i get cx_op_prefix i |
| 523 | end |
| 524 | for i ulong len sig bound 48 |
| 525 | put prov add len cx_op_prefix i get sig i |
| 526 | end |
| 527 | rc = cx_stage_set_verifier sig |
| 528 | if ne rc CORTEX_OK |
| 529 | ret rc |
| 530 | end |
| 531 | rc = cx_stage_set_payload sig |
| 532 | if ne rc CORTEX_OK |
| 533 | ret rc |
| 534 | end |
| 535 | rc = cx_stage_build CORTEX_OPERATOR_DECISION stmt part prov 0 plen ts |
| 536 | if ne rc CORTEX_OK |
| 537 | ret rc |
| 538 | end |
| 539 | slot = cx_insert_staged |
| 540 | if lt slot 0 |
| 541 | ret int slot |
| 542 | end |
| 543 | for i ulong 16 |
| 544 | put id_out i get cx_st_id i |
| 545 | end |
| 546 | ret CORTEX_OK |
| 547 | end |
| 548 | |
| 549 | fn cx_record_inference stmt view uchar * model view uchar * ts ulong id_out edit uchar 16 to int |
| 550 | put cx_st_pay_len 0 0 |
| 551 | rc = cx_stage_build CORTEX_INFERENCE stmt model ts |
| 552 | if ne rc CORTEX_OK |
| 553 | ret rc |
| 554 | end |
| 555 | rc = cx_verify_staged |
| 556 | if ne rc CORTEX_OK |
| 557 | ret rc |
| 558 | end |
| 559 | slot = cx_insert_staged |
| 560 | if lt slot 0 |
| 561 | ret int slot |
| 562 | end |
| 563 | for i ulong 16 |
| 564 | put id_out i get cx_st_id i |
| 565 | end |
| 566 | ret CORTEX_OK |
| 567 | end |
| 568 | |
| 569 | fn cx_record_hypothesis stmt view uchar * agent view uchar * ts ulong id_out edit uchar 16 to int |
| 570 | put cx_st_pay_len 0 0 |
| 571 | put cx_st_conf 0 0.5f32 |
| 572 | rc = cx_stage_build CORTEX_HYPOTHESIS stmt agent ts |
| 573 | if ne rc CORTEX_OK |
| 574 | ret rc |
| 575 | end |
| 576 | rc = cx_verify_staged |
| 577 | if ne rc CORTEX_OK |
| 578 | ret rc |
| 579 | end |
| 580 | slot = cx_insert_staged |
| 581 | if lt slot 0 |
| 582 | ret int slot |
| 583 | end |
| 584 | for i ulong 16 |
| 585 | put id_out i get cx_st_id i |
| 586 | end |
| 587 | ret CORTEX_OK |
| 588 | end |
| 589 | |
| 590 | fn cx_flag_contradiction id_a view uchar 16 id_b view uchar 16 reason view uchar * ts ulong id_out edit uchar 16 to int |
| 591 | if lt cx_find_by_id id_a 0 |
| 592 | ret CORTEX_ERR_NOT_FOUND |
| 593 | end |
| 594 | if lt cx_find_by_id id_b 0 |
| 595 | ret CORTEX_ERR_NOT_FOUND |
| 596 | end |
| 597 | slen = add len cx_contra_prefix len reason |
| 598 | if gt slen CORTEX_MAX_STMT |
| 599 | ret CORTEX_ERR_CAPACITY |
| 600 | end |
| 601 | sbuf = new uchar 96 0 |
| 602 | for i ulong len cx_contra_prefix bound 32 |
| 603 | put sbuf i get cx_contra_prefix i |
| 604 | end |
| 605 | for i ulong len reason bound 64 |
| 606 | put sbuf add len cx_contra_prefix i get reason i |
| 607 | end |
| 608 | cx_stage_reset |
| 609 | rc = cx_stage_add_parent id_a |
| 610 | if ne rc CORTEX_OK |
| 611 | ret rc |
| 612 | end |
| 613 | rc = cx_stage_add_parent id_b |
| 614 | if ne rc CORTEX_OK |
| 615 | ret rc |
| 616 | end |
| 617 | rc = cx_stage_set_payload reason |
| 618 | if ne rc CORTEX_OK |
| 619 | ret rc |
| 620 | end |
| 621 | rc = cx_stage_build CORTEX_CONTRADICTION part sbuf 0 slen cx_sentinel ts |
| 622 | if ne rc CORTEX_OK |
| 623 | ret rc |
| 624 | end |
| 625 | slot = cx_insert_staged |
| 626 | if lt slot 0 |
| 627 | ret int slot |
| 628 | end |
| 629 | for i ulong 16 |
| 630 | put id_out i get cx_st_id i |
| 631 | end |
| 632 | ret CORTEX_OK |
| 633 | end |
| 634 | |
| 635 | fn cx_slot_gt a ulong b ulong to bool |
| 636 | ta = get cx_ts a |
| 637 | tb = get cx_ts b |
| 638 | if gt ta tb |
| 639 | ret true |
| 640 | end |
| 641 | if lt ta tb |
| 642 | ret false |
| 643 | end |
| 644 | ba = mul a 16 |
| 645 | bb = mul b 16 |
| 646 | for i ulong 16 |
| 647 | va = get cx_id add ba i |
| 648 | vb = get cx_id add bb i |
| 649 | if gt va vb |
| 650 | ret true |
| 651 | end |
| 652 | if lt va vb |
| 653 | ret false |
| 654 | end |
| 655 | end |
| 656 | ret false |
| 657 | end |
| 658 | |
| 659 | fn cx_export_one_rec idx ulong dst edit uchar * pos ulong to long |
| 660 | nlen = get cx_stmt_len idx |
| 661 | nlen = add nlen get cx_prov_len idx |
| 662 | nlen = add nlen get cx_verif_len idx |
| 663 | nlen = add nlen mul get cx_pcnt idx 16 |
| 664 | if gt add add pos add 69 nlen 32 len dst |
| 665 | ret long CORTEX_ERR_CAPACITY |
| 666 | end |
| 667 | p = pos |
| 668 | base = mul idx 16 |
| 669 | for i ulong 16 |
| 670 | put dst add p i get cx_id add base i |
| 671 | end |
| 672 | p = add p 16 |
| 673 | put dst p get cx_status idx |
| 674 | p = add p 1 |
| 675 | cx_put_u64_be dst p get cx_ts idx |
| 676 | p = add p 8 |
| 677 | cx_put_u32_be dst p ulong bitcast uint get cx_conf idx |
| 678 | p = add p 4 |
| 679 | base = mul idx 32 |
| 680 | for i ulong 32 |
| 681 | put dst add p i get cx_ev_hash add base i |
| 682 | end |
| 683 | p = add p 32 |
| 684 | put dst p get cx_has_verif idx |
| 685 | p = add p 1 |
| 686 | nlen = get cx_stmt_len idx |
| 687 | cx_put_u16_be dst p nlen |
| 688 | p = add p 2 |
| 689 | base = mul idx CORTEX_MAX_STMT |
| 690 | for i ulong nlen bound 96 |
| 691 | put dst add p i get cx_stmt add base i |
| 692 | end |
| 693 | p = add p nlen |
| 694 | nlen = get cx_prov_len idx |
| 695 | cx_put_u16_be dst p nlen |
| 696 | p = add p 2 |
| 697 | base = mul idx CORTEX_MAX_PROV |
| 698 | for i ulong nlen bound 64 |
| 699 | put dst add p i get cx_prov add base i |
| 700 | end |
| 701 | p = add p nlen |
| 702 | nlen = get cx_verif_len idx |
| 703 | cx_put_u16_be dst p nlen |
| 704 | p = add p 2 |
| 705 | base = mul idx CORTEX_MAX_VERIF |
| 706 | for i ulong nlen bound 48 |
| 707 | put dst add p i get cx_verif add base i |
| 708 | end |
| 709 | p = add p nlen |
| 710 | nlen = get cx_pcnt idx |
| 711 | put dst p uchar nlen |
| 712 | p = add p 1 |
| 713 | base = mul idx mul CORTEX_MAX_PARENTS CORTEX_ID_LEN |
| 714 | for i ulong mul nlen 16 bound 64 |
| 715 | put dst add p i get cx_parents add base i |
| 716 | end |
| 717 | ret long add p mul nlen 16 |
| 718 | end |
| 719 | |
| 720 | fn cx_export_journal dst edit uchar * to long |
| 721 | n = get cx_cnt 0 |
| 722 | for i ulong n bound 32 |
| 723 | put cx_order i i |
| 724 | end |
| 725 | if gt n 1 |
| 726 | for i ulong sub n 1 bound 32 |
| 727 | key_idx = get cx_order add i 1 |
| 728 | j = add i 1 |
| 729 | while gt j 0 bound 32 |
| 730 | prev = get cx_order sub j 1 |
| 731 | if cx_slot_gt prev key_idx |
| 732 | put cx_order j prev |
| 733 | j = sub j 1 |
| 734 | else |
| 735 | break |
| 736 | end |
| 737 | end |
| 738 | put cx_order j key_idx |
| 739 | end |
| 740 | end |
| 741 | |
| 742 | if lt len dst 38 |
| 743 | ret long CORTEX_ERR_CAPACITY |
| 744 | end |
| 745 | for i ulong 4 |
| 746 | put dst i get cx_jmagic i |
| 747 | end |
| 748 | cx_put_u16_be dst 4 n |
| 749 | pos = ulong 6 |
| 750 | |
| 751 | for k ulong n bound 32 |
| 752 | idx = get cx_order k |
| 753 | nxt = cx_export_one_rec idx dst pos |
| 754 | if lt nxt 0 |
| 755 | ret nxt |
| 756 | end |
| 757 | pos = ulong nxt |
| 758 | end |
| 759 | |
| 760 | jsum = new uchar 32 0 |
| 761 | dl = sha2_digest 256 part dst 0 pos jsum |
| 762 | for i ulong 32 |
| 763 | put dst add pos i get jsum i |
| 764 | end |
| 765 | ret long add pos 32 |
| 766 | end |
| 767 | |
| 768 | fn cx_import_one_rec src view uchar * pos ulong body_end ulong to long |
| 769 | if gt add pos 69 body_end |
| 770 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 771 | end |
| 772 | cx_stage_reset |
| 773 | p = pos |
| 774 | for i ulong 16 |
| 775 | put cx_st_id i get src add p i |
| 776 | end |
| 777 | p = add p 16 |
| 778 | put cx_st_status 0 get src p |
| 779 | p = add p 1 |
| 780 | put cx_st_ts 0 cx_get_u64_be src p |
| 781 | p = add p 8 |
| 782 | put cx_st_conf 0 bitcast f32 uint cx_get_u32_be src p |
| 783 | p = add p 4 |
| 784 | for i ulong 32 |
| 785 | put cx_st_ev_hash i get src add p i |
| 786 | end |
| 787 | p = add p 32 |
| 788 | put cx_st_has_verif 0 get src p |
| 789 | p = add p 1 |
| 790 | nlen = cx_get_u16_be src p |
| 791 | p = add p 2 |
| 792 | if or gt nlen CORTEX_MAX_STMT gt add p nlen body_end |
| 793 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 794 | end |
| 795 | put cx_st_stmt_len 0 nlen |
| 796 | for i ulong nlen bound 96 |
| 797 | put cx_st_stmt i get src add p i |
| 798 | end |
| 799 | p = add p nlen |
| 800 | if gt add p 2 body_end |
| 801 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 802 | end |
| 803 | nlen = cx_get_u16_be src p |
| 804 | p = add p 2 |
| 805 | if or gt nlen CORTEX_MAX_PROV gt add p nlen body_end |
| 806 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 807 | end |
| 808 | put cx_st_prov_len 0 nlen |
| 809 | for i ulong nlen bound 64 |
| 810 | put cx_st_prov i get src add p i |
| 811 | end |
| 812 | p = add p nlen |
| 813 | if gt add p 2 body_end |
| 814 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 815 | end |
| 816 | nlen = cx_get_u16_be src p |
| 817 | p = add p 2 |
| 818 | if or gt nlen CORTEX_MAX_VERIF gt add p nlen body_end |
| 819 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 820 | end |
| 821 | put cx_st_verif_len 0 nlen |
| 822 | for i ulong nlen bound 48 |
| 823 | put cx_st_verif i get src add p i |
| 824 | end |
| 825 | p = add p nlen |
| 826 | if gt add p 1 body_end |
| 827 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 828 | end |
| 829 | nlen = ulong get src p |
| 830 | p = add p 1 |
| 831 | if or gt nlen CORTEX_MAX_PARENTS gt add p mul nlen 16 body_end |
| 832 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 833 | end |
| 834 | put cx_st_pcnt 0 nlen |
| 835 | for i ulong mul nlen 16 bound 64 |
| 836 | put cx_st_parents i get src add p i |
| 837 | end |
| 838 | p = add p mul nlen 16 |
| 839 | if lt cx_insert_staged 0 |
| 840 | ret long CORTEX_ERR_CAPACITY |
| 841 | end |
| 842 | ret long p |
| 843 | end |
| 844 | |
| 845 | fn cx_import_journal src view uchar * slen_in ulong to long |
| 846 | if lt slen_in 38 |
| 847 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 848 | end |
| 849 | if gt slen_in len src |
| 850 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 851 | end |
| 852 | for i ulong 4 |
| 853 | if ne get src i get cx_jmagic i |
| 854 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 855 | end |
| 856 | end |
| 857 | body_end = sub slen_in 32 |
| 858 | computed = new uchar 32 0 |
| 859 | dl = sha2_digest 256 part src 0 body_end computed |
| 860 | for i ulong 32 |
| 861 | if ne get computed i get src add body_end i |
| 862 | ret long CORTEX_ERR_CHECKSUM |
| 863 | end |
| 864 | end |
| 865 | cnt = cx_get_u16_be src 4 |
| 866 | if gt cnt CORTEX_MAX_RECS |
| 867 | ret long CORTEX_ERR_CAPACITY |
| 868 | end |
| 869 | pos = ulong 6 |
| 870 | for k ulong cnt bound 32 |
| 871 | nxt = cx_import_one_rec src pos body_end |
| 872 | if lt nxt 0 |
| 873 | ret nxt |
| 874 | end |
| 875 | pos = ulong nxt |
| 876 | end |
| 877 | if ne pos body_end |
| 878 | ret long CORTEX_ERR_CORRUPT_JOURNAL |
| 879 | end |
| 880 | ret long cnt |
| 881 | end |