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/gpu/checker2/lrat.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 / gpu / checker2 / lrat.om` 634 lines ยท 11713 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note gpu/checker2/lrat.om: pure-Omega DIMACS CNF parser & RUP-only LRAT proof checker 3 note (replaces src/checker2/c2_lrat.{h,c}, trusted kernel of checker2). 4 note Fails closed on every malformed input, RAT negative hint, unknown/deleted clause, 5 note non-unit hint, missing conflict, ID order/range violation, or missing empty clause. 6 note Compose: si/directives/om2.om gpu/checker2/lrat.om gpu/checker2/test.om 7 8 const C2_MAX_VARS int 4096 9 const C2_MAX_CLAUSES ulong 8192 10 const C2_MAX_LITS ulong 65536 11 const C2_MAX_DB ulong 16384 12 const C2_MAX_DB_LITS ulong 131072 13 const C2_LRAT_MAX_ID long `100000000` 14 15 note Stable rule codes matching src/checker2/c2_lrat.c 16 const C2_OK int 0 17 const C2_E_CNF_HEADER int 1 18 const C2_E_CNF_LITERAL int 2 19 const C2_E_CNF_COUNT int 3 20 const C2_E_CNF_TOO_LONG int 4 21 const C2_E_CNF_RESOURCE int 5 22 const C2_E_LRAT_SYNTAX int 10 23 const C2_E_LRAT_DELETE_UNKNOWN int 11 24 const C2_E_LRAT_ID_ORDER int 12 25 const C2_E_LRAT_ID_RANGE int 13 26 const C2_E_LRAT_LITERAL int 14 27 const C2_E_LRAT_RAT_UNSUPPORTED int 15 28 const C2_E_LRAT_HINT_UNKNOWN int 16 29 const C2_E_LRAT_HINT_NOT_UNIT int 17 30 const C2_E_LRAT_NO_CONFLICT int 18 31 const C2_E_LRAT_NO_EMPTY_CLAUSE int 19 32 const C2_E_LRAT_RESOURCE int 20 33 34 struct C2Lx pos ulong line long end 35 struct C2IntRes st int val long end 36 struct C2LratRes ok int rule int line long steps long dels long end 37 38 note Bounded static arenas (zero heap, deterministic) 39 global g_cnf_lits int 65536 40 global g_cnf_start ulong 8192 41 global g_cnf_meta ulong 4 42 global g_db_lits int 131072 43 global g_db_off ulong 16384 44 global g_db_meta ulong 4 45 global g_val byte 4097 46 global g_assigned int 4097 47 global g_cl_buf int 4098 48 49 note ---- lexer helpers ---- 50 51 fn c2_is_ws b uchar to bool 52 if eq b 32 53 ret true 54 end 55 if eq b 9 56 ret true 57 end 58 if eq b 13 59 ret true 60 end 61 if eq b 10 62 ret true 63 end 64 ret false 65 end 66 67 fn c2_skip_ws src view uchar * lx edit C2Lx to void 68 n = len src 69 i = get lx pos 70 ln = get lx line 71 done = false 72 while eq done false bound 65536 73 while lt i n bound 65536 74 ch = get src i 75 if eq c2_is_ws ch false 76 break 77 end 78 if eq ch 10 79 ln = add ln 1 80 end 81 i = add i 1 82 end 83 if lt i n 84 if eq get src i 99 85 note 'c' comment to end of line 86 while lt i n bound 65536 87 if eq get src i 10 88 break 89 end 90 i = add i 1 91 end 92 else 93 done = true 94 end 95 else 96 done = true 97 end 98 end 99 put lx pos i 100 put lx line ln 101 ret 102 end 103 104 note rd_int: st = 0 ok, 1 eof, -1 malformed/overflow 105 fn c2_rd_int src view uchar * lx edit C2Lx res edit C2IntRes to int 106 c2_skip_ws src lx 107 n = len src 108 i = get lx pos 109 if ge i n 110 put res st 1 111 put res val 0 112 ret 1 113 end 114 is_neg = false 115 if eq get src i 45 116 is_neg = true 117 i = add i 1 118 end 119 if ge i n 120 put res st -1 121 ret -1 122 end 123 ch0 = get src i 124 if or lt ch0 48 gt ch0 57 125 put res st -1 126 ret -1 127 end 128 v = long 0 129 while lt i n bound 32 130 ch = get src i 131 if or lt ch 48 gt ch 57 132 break 133 end 134 v = add mul v 10 long sub uint ch 48 135 if gt v C2_LRAT_MAX_ID 136 put res st -1 137 ret -1 138 end 139 i = add i 1 140 end 141 if lt i n 142 if eq c2_is_ws get src i false 143 put res st -1 144 ret -1 145 end 146 end 147 put lx pos i 148 if is_neg 149 put res val neg v 150 else 151 put res val v 152 end 153 put res st 0 154 ret 0 155 end 156 157 note ---- CNF builder & DIMACS parser ---- 158 159 fn c2_cnf_reset to void 160 put g_cnf_meta 0 ulong 0 161 put g_cnf_meta 1 ulong 0 162 put g_cnf_meta 2 ulong 0 163 ret 164 end 165 166 fn c2_cnf_add lits view int * cnt ulong to int 167 nc = get g_cnf_meta 1 168 nl = get g_cnf_meta 2 169 nv = int get g_cnf_meta 0 170 if ge nc C2_MAX_CLAUSES 171 ret C2_E_CNF_RESOURCE 172 end 173 if gt add add nl cnt 1 C2_MAX_LITS 174 ret C2_E_CNF_RESOURCE 175 end 176 put g_cnf_start nc nl 177 k = ulong 0 178 while lt k cnt bound 4096 179 lit = get lits k 180 if eq lit 0 181 ret C2_E_CNF_LITERAL 182 end 183 v = lit 184 if lt v 0 185 v = neg v 186 end 187 if gt v C2_MAX_VARS 188 ret C2_E_CNF_LITERAL 189 end 190 if gt v nv 191 nv = v 192 end 193 put g_cnf_lits add nl k lit 194 k = add k 1 195 end 196 put g_cnf_lits add nl cnt int 0 197 put g_cnf_meta 0 ulong nv 198 put g_cnf_meta 1 add nc 1 199 put g_cnf_meta 2 add add nl cnt 1 200 ret C2_OK 201 end 202 203 fn c2_match_pcnf src view uchar * pos ulong to bool 204 if lt sub len src pos 6 205 ret false 206 end 207 if ne get src pos 112 208 ret false 209 end 210 if ne get src add pos 1 32 211 ret false 212 end 213 if ne get src add pos 2 99 214 ret false 215 end 216 if ne get src add pos 3 110 217 ret false 218 end 219 if ne get src add pos 4 102 220 ret false 221 end 222 if ne get src add pos 5 32 223 ret false 224 end 225 ret true 226 end 227 228 fn c2_cnf_parse src view uchar * to int 229 c2_cnf_reset 230 lx = new C2Lx ulong 0 long 1 231 ir = new C2IntRes 0 0 232 c2_skip_ws src edit lx 233 if eq c2_match_pcnf src get lx pos false 234 ret C2_E_CNF_HEADER 235 end 236 put lx pos add get lx pos 6 237 if ne c2_rd_int src edit lx edit ir 0 238 ret C2_E_CNF_HEADER 239 end 240 nv = get ir val 241 if ne c2_rd_int src edit lx edit ir 0 242 ret C2_E_CNF_HEADER 243 end 244 nc = get ir val 245 if or or lt nv 0 lt nc 0 gt nv long C2_MAX_VARS 246 ret C2_E_CNF_HEADER 247 end 248 buf = new int 4096 0 249 cnt = ulong 0 250 done = false 251 while eq done false bound 65536 252 st = c2_rd_int src edit lx edit ir 253 if eq st 1 254 done = true 255 else 256 if lt st 0 257 c2_cnf_reset 258 ret C2_E_CNF_LITERAL 259 end 260 lv = get ir val 261 if or gt lv nv lt lv neg nv 262 c2_cnf_reset 263 ret C2_E_CNF_LITERAL 264 end 265 if eq lv 0 266 rc = c2_cnf_add view buf cnt 267 if ne rc C2_OK 268 c2_cnf_reset 269 ret rc 270 end 271 cnt = ulong 0 272 else 273 if ge cnt 4096 274 c2_cnf_reset 275 ret C2_E_CNF_TOO_LONG 276 end 277 put buf cnt int lv 278 cnt = add cnt 1 279 end 280 end 281 end 282 if or ne cnt 0 ne get g_cnf_meta 1 ulong nc 283 c2_cnf_reset 284 ret C2_E_CNF_COUNT 285 end 286 put g_cnf_meta 0 ulong nv 287 ret C2_OK 288 end 289 290 note ---- LRAT clause DB & RUP checker ---- 291 292 fn c2_db_reset to void 293 for i ulong 16384 294 put g_db_off i ulong 0 295 end 296 put g_db_meta 0 ulong 0 297 put g_db_meta 1 ulong 0 298 ret 299 end 300 301 fn c2_db_put_clause id ulong src_off ulong cnt ulong to int 302 if or eq id 0 ge id C2_MAX_DB 303 ret C2_E_LRAT_RESOURCE 304 end 305 used = get g_db_meta 0 306 if gt add add used cnt 1 C2_MAX_DB_LITS 307 ret C2_E_LRAT_RESOURCE 308 end 309 k = ulong 0 310 while lt k cnt bound 4096 311 put g_db_lits add used k get g_cl_buf add src_off k 312 k = add k 1 313 end 314 put g_db_lits add used cnt int 0 315 note store 1-based offset so 0 means absent/deleted 316 put g_db_off id add used 1 317 put g_db_meta 0 add add used cnt 1 318 ret C2_OK 319 end 320 321 fn c2_db_load_cnf to int 322 nc = get g_cnf_meta 1 323 i = ulong 0 324 while lt i nc bound 8192 325 st = get g_cnf_start i 326 cnt = ulong 0 327 while ne get g_cnf_lits add st cnt 0 bound 4096 328 put g_cl_buf cnt get g_cnf_lits add st cnt 329 cnt = add cnt 1 330 end 331 rc = c2_db_put_clause add i 1 ulong 0 cnt 332 if ne rc C2_OK 333 ret rc 334 end 335 i = add i 1 336 end 337 ret C2_OK 338 end 339 340 fn c2_lrat_fail res edit C2LratRes rule int ln long to int 341 put res ok int 0 342 put res rule rule 343 put res line ln 344 ret rule 345 end 346 347 fn c2_undo_assign na ulong to void 348 k = ulong 0 349 while lt k na bound 4097 350 v = ulong get g_assigned k 351 put g_val v byte 0 352 k = add k 1 353 end 354 ret 355 end 356 357 note Check a single hint clause h against current g_val assignment. 358 note Returns: 0 = unit (assigns literal, increments na via res_na[0]), 1 = conflict (falsified), 359 note <0 = negative of C2_E_* error code. 360 fn c2_check_hint h long na_ref edit ulong 1 to int 361 if lt h 0 362 ret neg C2_E_LRAT_RAT_UNSUPPORTED 363 end 364 if ge ulong h C2_MAX_DB 365 ret neg C2_E_LRAT_HINT_UNKNOWN 366 end 367 off1 = get g_db_off ulong h 368 if eq off1 0 369 ret neg C2_E_LRAT_HINT_UNKNOWN 370 end 371 off = sub off1 1 372 unit = int 0 373 nfree = int 0 374 sat = false 375 k = ulong 0 376 while ne get g_db_lits add off k 0 bound 4096 377 lit = get g_db_lits add off k 378 v = lit 379 if lt v 0 380 v = neg v 381 end 382 cur = int get g_val ulong v 383 if ne cur 0 384 is_pos = gt lit 0 385 val_pos = gt cur 0 386 if eq is_pos val_pos 387 sat = true 388 break 389 end 390 else 391 nfree = add nfree 1 392 unit = lit 393 end 394 k = add k 1 395 end 396 if or sat gt nfree 1 397 ret neg C2_E_LRAT_HINT_NOT_UNIT 398 end 399 if eq nfree 0 400 ret 1 401 end 402 uv = unit 403 if lt uv 0 404 uv = neg uv 405 end 406 if gt unit 0 407 put g_val ulong uv byte 1 408 else 409 put g_val ulong uv byte -1 410 end 411 na = get na_ref 0 412 put g_assigned na uv 413 put na_ref 0 add na 1 414 ret 0 415 end 416 417 fn c2_handle_delete proof view uchar * lx edit C2Lx res edit C2LratRes ln long to int 418 ir = new C2IntRes 0 0 419 done = false 420 while eq done false bound 8192 421 if ne c2_rd_int proof lx edit ir 0 422 ret c2_lrat_fail res C2_E_LRAT_SYNTAX ln 423 end 424 del = get ir val 425 if eq del 0 426 done = true 427 else 428 if or lt del 0 ge ulong del C2_MAX_DB 429 ret c2_lrat_fail res C2_E_LRAT_DELETE_UNKNOWN ln 430 end 431 if eq get g_db_off ulong del 0 432 ret c2_lrat_fail res C2_E_LRAT_DELETE_UNKNOWN ln 433 end 434 put g_db_off ulong del ulong 0 435 put res dels add get res dels 1 436 end 437 end 438 ret C2_OK 439 end 440 441 note Assign negation of g_cl_buf[0..cnt) into g_val; sets na_ref[0]. Returns 1 if tautology, 0 else. 442 fn c2_assign_neg_clause cnt ulong na_ref edit ulong 1 to int 443 taut = int 0 444 na = ulong 0 445 k = ulong 0 446 while lt k cnt bound 4096 447 lit = get g_cl_buf k 448 v = lit 449 if lt v 0 450 v = neg v 451 end 452 want = int -1 453 if lt lit 0 454 want = int 1 455 end 456 cur = int get g_val ulong v 457 if eq cur neg want 458 taut = int 1 459 end 460 if eq cur 0 461 put g_val ulong v byte want 462 put g_assigned na v 463 na = add na 1 464 end 465 k = add k 1 466 end 467 put na_ref 0 na 468 ret taut 469 end 470 471 fn c2_read_clause_lits proof view uchar * lx edit C2Lx nv long cnt_ref edit ulong 1 to int 472 ir = new C2IntRes 0 0 473 cnt = ulong 0 474 done = false 475 while eq done false bound 4098 476 if ne c2_rd_int proof lx edit ir 0 477 ret C2_E_LRAT_SYNTAX 478 end 479 lv = get ir val 480 if eq lv 0 481 done = true 482 else 483 if or gt lv nv lt lv neg nv 484 ret C2_E_LRAT_LITERAL 485 end 486 if gt cnt ulong nv 487 ret C2_E_LRAT_SYNTAX 488 end 489 put g_cl_buf cnt int lv 490 cnt = add cnt 1 491 end 492 end 493 put cnt_ref 0 cnt 494 ret C2_OK 495 end 496 497 fn c2_read_and_check_hints proof view uchar * lx edit C2Lx taut int na_ref edit ulong 1 to int 498 ir = new C2IntRes 0 0 499 proved = eq taut 1 500 done = false 501 while eq done false bound 8192 502 if ne c2_rd_int proof lx edit ir 0 503 ret C2_E_LRAT_SYNTAX 504 end 505 hv = get ir val 506 if eq hv 0 507 done = true 508 else 509 if eq proved false 510 hr = c2_check_hint hv na_ref 511 if lt hr 0 512 ret neg hr 513 end 514 if eq hr 1 515 proved = true 516 end 517 end 518 end 519 end 520 if eq proved false 521 ret C2_E_LRAT_NO_CONFLICT 522 end 523 ret C2_OK 524 end 525 526 fn c2_count_newlines proof view uchar * to long 527 cnt = long 0 528 i = ulong 0 529 n = len proof 530 while lt i n bound 65536 531 if eq get proof i 10 532 cnt = add cnt 1 533 end 534 i = add i 1 535 end 536 ret cnt 537 end 538 539 note Handle one LRAT clause-addition step; returns 0=ok (continue), 1=empty clause derived, <0=neg(err). 540 fn c2_handle_add_step proof view uchar * lx edit C2Lx id long res edit C2LratRes to int 541 ln = get lx line 542 nv = long get g_cnf_meta 0 543 cnt_ref = new ulong 1 0 544 na_ref = new ulong 1 0 545 rc = c2_read_clause_lits proof lx nv edit cnt_ref 546 if ne rc C2_OK 547 ret neg c2_lrat_fail res rc ln 548 end 549 cnt = get cnt_ref 0 550 taut = c2_assign_neg_clause cnt edit na_ref 551 rc = c2_read_and_check_hints proof lx taut edit na_ref 552 c2_undo_assign get na_ref 0 553 if ne rc C2_OK 554 ret neg c2_lrat_fail res rc ln 555 end 556 rc = c2_db_put_clause ulong id ulong 0 cnt 557 if ne rc C2_OK 558 ret neg c2_lrat_fail res rc ln 559 end 560 put res steps add get res steps 1 561 if eq cnt 0 562 put res ok int 1 563 put res rule C2_OK 564 ret 1 565 end 566 ret 0 567 end 568 569 fn c2_is_del_tok proof view uchar * lx edit C2Lx to bool 570 c2_skip_ws proof lx 571 p = get lx pos 572 if lt p len proof 573 if eq get proof p 100 574 put lx pos add p 1 575 ret true 576 end 577 end 578 ret false 579 end 580 581 fn c2_lrat_check proof view uchar * res edit C2LratRes to int 582 put res ok int 0 583 put res rule C2_OK 584 put res line long 0 585 put res steps long 0 586 put res dels long 0 587 for i ulong 4097 588 put g_val i byte 0 589 end 590 c2_db_reset 591 rc = c2_db_load_cnf 592 if ne rc C2_OK 593 ret c2_lrat_fail res rc long 0 594 end 595 put g_db_meta 1 ulong add add long get g_cnf_meta 1 1 c2_count_newlines proof 596 put g_db_meta 2 get g_cnf_meta 1 597 lx = new C2Lx ulong 0 long 1 598 ir = new C2IntRes 0 0 599 done = false 600 while eq done false bound 16384 601 st = c2_rd_int proof edit lx edit ir 602 if eq st 1 603 done = true 604 else 605 ln = get lx line 606 id = get ir val 607 if or lt st 0 lt id 1 608 ret c2_lrat_fail res C2_E_LRAT_SYNTAX ln 609 end 610 if c2_is_del_tok proof edit lx 611 rc = c2_handle_delete proof edit lx res ln 612 if ne rc C2_OK 613 ret rc 614 end 615 else 616 if le id long get g_db_meta 2 617 ret c2_lrat_fail res C2_E_LRAT_ID_ORDER ln 618 end 619 if gt id long get g_db_meta 1 620 ret c2_lrat_fail res C2_E_LRAT_ID_RANGE ln 621 end 622 put g_db_meta 2 ulong id 623 ar = c2_handle_add_step proof edit lx id res 624 if lt ar 0 625 ret neg ar 626 end 627 if eq ar 1 628 ret C2_OK 629 end 630 end 631 end 632 end 633 ret c2_lrat_fail res C2_E_LRAT_NO_EMPTY_CLAUSE get lx line 634 end