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 / tools / max-interop / contract_check.om
| 1 | note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 2 | note contract_check: native Omega checker for the MAX interop op contracts. |
| 3 | note Protocol: stdin starts with a line mode=id|check|manifest|sha256, then the payload. |
| 4 | note mode=id / mode=sha256 hash the payload; mode=check validates a canonical |
| 5 | note omega-op-contract/1 file; mode=manifest validates an omega-input-manifest/1. |
| 6 | note Output is only written for a decision (OK / REFUSED); exit 0 on OK, 1 on refusal. |
| 7 | note mode=manifest payload (after the mode line): |
| 8 | note expect=<hex64> |
| 9 | note omega-input-manifest/1 |
| 10 | note op=<matmul|gqa-causal-attention> |
| 11 | note contract=<hex64> |
| 12 | note fixture=<name> |
| 13 | note input_sha256=<hex64> |
| 14 | note seed=<u64> |
| 15 | note count=<u32> |
| 16 | note [payload_hex=<even-length lowercase hex>] |
| 17 | note The payload_hex checksum, when present, must match input_sha256, and the |
| 18 | note contract must be the frozen id for op and equal the expect id. |
| 19 | note SHA-256 is adapted from benchmarks-om/build/base/om-sha256.om, itself |
| 20 | note Apache-2.0 WITH LLVM-exception and checked against Omega seed/atlas/atlas.s2 and src/sha256.c. |
| 21 | note No heap: all state is in fixed globals and the fixed input pool. |
| 22 | |
| 23 | global big uchar 1048576 |
| 24 | global irlen ulong 1 |
| 25 | global pstart ulong 1 |
| 26 | global nline ulong 1 |
| 27 | global pofs ulong 256 |
| 28 | global plen ulong 256 |
| 29 | global sofs ulong 256 |
| 30 | global slen ulong 256 |
| 31 | global nsch ulong 1 |
| 32 | |
| 33 | global hk uint 64 |
| 34 | global hs uint 8 |
| 35 | global hm uint 64 |
| 36 | global hb uchar 64 |
| 37 | global hd uchar 64 |
| 38 | global hmeta ulong 3 |
| 39 | |
| 40 | text schm0 "omega-op-contract/1\nop=matmul\nabi=1\ndtype_a=bf16\ndtype_b=bf16\ndtype_c=f32\nlayout_a=row-major\nlayout_b=row-major\nlayout_c=row-major\nstride_a=contiguous\nstride_b=contiguous\nstride_c=contiguous\naccumulation=fp32\nrounding=input-rne-bf16,accumulate-fp32\nalpha=1\nbeta=0\nalias=forbid\nnan=forbid\ninf=forbid\nm_min=1\nm_max=8192\nk_min=1\nk_max=16384\nn_min=1\nn_max=8192\ntail=zero-pad-to-multiple-of-16\ntolerance=abs<=1e-5*max(1,abs(ref))\nreference=fp64-sum-of-exact-bf16-products\n" |
| 41 | text schm1 "omega-op-contract/1\nop=gqa-causal-attention\nabi=1\ndtype_q=f32\ndtype_k=f32\ndtype_v=f32\ndtype_out=f32\nlayout_q=token-head-dim\nlayout_k=token-head-dim\nlayout_v=token-head-dim\nlayout_out=token-head-dim\nhead_dim=128\nq_heads_min=1\nq_heads_max=64\nkv_heads_min=1\nkv_heads_max=64\nhead_map=h_q/(q_heads/kv_heads)\nscale=1/sqrt(128)\nmask=causal-inclusive\nsoftmax=fp32-max-subtracted\nkv_stride=token-major\nzero_length=zero-output\nalias=forbid\nnan=forbid\ninf=forbid\ntolerance_normal=rel<=2e-4,abs<=2e-5\ntolerance_large_scores=rel<=1e-3,abs<=1e-4\nreference=fp64-softmax-fp64-weighted-sum\n" |
| 42 | text mhdr "omega-input-manifest/1" |
| 43 | text omth "matmul" |
| 44 | text ogqa "gqa-causal-attention" |
| 45 | text mid "647116fc1ba1e1394982fc8ba181ce565293f160a6b56afc0f02b2128a675704" |
| 46 | text gid "003696e422bba7274877ba9e01a61076ea0f862f32760cbaa06c60f85a115359" |
| 47 | text okmsg "OK " |
| 48 | text refmsg "REFUSED " |
| 49 | text newln "\n" |
| 50 | text ehead "E_HEADER\n" |
| 51 | text eorder "E_ORDER\n" |
| 52 | text evalue "E_VALUE\n" |
| 53 | text emiss "E_MISSING\n" |
| 54 | text eunknown "E_UNKNOWN\n" |
| 55 | text etol "E_TOLERANCE\n" |
| 56 | text edtype "E_DTYPE\n" |
| 57 | text elayout "E_LAYOUT\n" |
| 58 | text etarget "E_TARGET\n" |
| 59 | |
| 60 | note ---- SHA-256, adapted from the proven om-sha256.om ---- |
| 61 | text hconst "428a2f9871374491b5c0fbcfe9b5dba53956c25b59f111f1923f82a4ab1c5ed5d807aa9812835b01243185be550c7dc372be5d7480deb1fe9bdc06a7c19bf174e49b69c1efbe47860fc19dc6240ca1cc2de92c6f4a7484aa5cb0a9dc76f988da983e5152a831c66db00327c8bf597fc7c6e00bf3d5a7914706ca63511429296727b70a852e1b21384d2c6dfc53380d13650a7354766a0abb81c2c92e92722c85a2bfe8a1a81a664bc24b8b70c76c51a3d192e819d6990624f40e3585106aa07019a4c1161e376c082748774c34b0bcb5391c0cb34ed8aa4a5b9cca4f682e6ff3748f82ee78a5636f84c878148cc7020890befffaa4506cebbef9a3f7c67178f2" |
| 62 | text hseed "6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c1f83d9ab5be0cd19" |
| 63 | text hhex "0123456789abcdef" |
| 64 | |
| 65 | fn hnibble c uchar to uint |
| 66 | if le c 57 |
| 67 | ret uint sub c 48 |
| 68 | end |
| 69 | ret uint sub c 87 |
| 70 | end |
| 71 | |
| 72 | fn hreset to void |
| 73 | for i ulong 64 |
| 74 | v = uint 0 |
| 75 | for j ulong 8 |
| 76 | v = or shl v 4 hnibble get hconst add mul i 8 j |
| 77 | end |
| 78 | put hk i v |
| 79 | end |
| 80 | for i ulong 8 |
| 81 | v = uint 0 |
| 82 | for j ulong 8 |
| 83 | v = or shl v 4 hnibble get hseed add mul i 8 j |
| 84 | end |
| 85 | put hs i v |
| 86 | end |
| 87 | for i ulong 3 |
| 88 | put hmeta i 0 |
| 89 | end |
| 90 | end |
| 91 | |
| 92 | fn hrot x uint n uint to uint |
| 93 | ret or shr x n shl x sub 32 n |
| 94 | end |
| 95 | |
| 96 | fn hsmall0 x uint to uint |
| 97 | ret xor xor hrot x 7 hrot x 18 shr x 3 |
| 98 | end |
| 99 | |
| 100 | fn hsmall1 x uint to uint |
| 101 | ret xor xor hrot x 17 hrot x 19 shr x 10 |
| 102 | end |
| 103 | |
| 104 | fn hbig0 x uint to uint |
| 105 | ret xor xor hrot x 2 hrot x 13 hrot x 22 |
| 106 | end |
| 107 | |
| 108 | fn hbig1 x uint to uint |
| 109 | ret xor xor hrot x 6 hrot x 11 hrot x 25 |
| 110 | end |
| 111 | |
| 112 | fn hblock to void |
| 113 | for i ulong 16 |
| 114 | v = uint 0 |
| 115 | for j ulong 4 |
| 116 | v = or shl v 8 uint get hb add mul i 4 j |
| 117 | end |
| 118 | put hm i v |
| 119 | end |
| 120 | for j ulong 48 |
| 121 | i = add j 16 |
| 122 | v = add hsmall1 get hm sub i 2 get hm sub i 7 |
| 123 | v = add v hsmall0 get hm sub i 15 |
| 124 | put hm i add v get hm sub i 16 |
| 125 | end |
| 126 | a = get hs 0 |
| 127 | b = get hs 1 |
| 128 | c = get hs 2 |
| 129 | d = get hs 3 |
| 130 | e = get hs 4 |
| 131 | f = get hs 5 |
| 132 | g = get hs 6 |
| 133 | h = get hs 7 |
| 134 | for i ulong 64 |
| 135 | t1 = add h hbig1 e |
| 136 | t1 = add t1 xor and e f and not e g |
| 137 | t1 = add t1 get hk i |
| 138 | t1 = add t1 get hm i |
| 139 | t2 = hbig0 a |
| 140 | t2 = add t2 xor xor and a b and a c and b c |
| 141 | h = g |
| 142 | g = f |
| 143 | f = e |
| 144 | e = add d t1 |
| 145 | d = c |
| 146 | c = b |
| 147 | b = a |
| 148 | a = add t1 t2 |
| 149 | end |
| 150 | put hs 0 add get hs 0 a |
| 151 | put hs 1 add get hs 1 b |
| 152 | put hs 2 add get hs 2 c |
| 153 | put hs 3 add get hs 3 d |
| 154 | put hs 4 add get hs 4 e |
| 155 | put hs 5 add get hs 5 f |
| 156 | put hs 6 add get hs 6 g |
| 157 | put hs 7 add get hs 7 h |
| 158 | end |
| 159 | |
| 160 | fn hraw c uchar to void |
| 161 | n = get hmeta 0 |
| 162 | put hb n c |
| 163 | n = add n 1 |
| 164 | if eq n 64 |
| 165 | hblock |
| 166 | n = ulong 0 |
| 167 | end |
| 168 | put hmeta 0 n |
| 169 | end |
| 170 | |
| 171 | fn hpush c uchar to bool |
| 172 | if ne get hmeta 2 0 |
| 173 | ret false |
| 174 | end |
| 175 | if eq get hmeta 1 2305843009213693951 |
| 176 | put hmeta 2 2 |
| 177 | ret false |
| 178 | end |
| 179 | hraw c |
| 180 | put hmeta 1 add get hmeta 1 1 |
| 181 | ret true |
| 182 | end |
| 183 | |
| 184 | fn hfinal to bool |
| 185 | if ne get hmeta 2 0 |
| 186 | ret false |
| 187 | end |
| 188 | bits = mul get hmeta 1 8 |
| 189 | hraw 128 |
| 190 | while ne get hmeta 0 56 |
| 191 | hraw 0 |
| 192 | end |
| 193 | for i ulong 8 |
| 194 | shift = mul sub 7 i 8 |
| 195 | hraw uchar and shr bits shift 255 |
| 196 | end |
| 197 | for i ulong 8 |
| 198 | v = get hs i |
| 199 | for j ulong 8 |
| 200 | shift = uint mul sub 7 j 4 |
| 201 | digit = ulong and shr v shift 15 |
| 202 | put hd add mul i 8 j get hhex digit |
| 203 | end |
| 204 | end |
| 205 | put hmeta 2 1 |
| 206 | ret true |
| 207 | end |
| 208 | |
| 209 | note ---- input, mode, lines ---- |
| 210 | |
| 211 | fn readall to int |
| 212 | more = true |
| 213 | while more |
| 214 | n = read 0 big get irlen 0 sub 1048576 get irlen 0 |
| 215 | if eq n -4 |
| 216 | else |
| 217 | if lt n 0 |
| 218 | ret 74 |
| 219 | end |
| 220 | if eq n 0 |
| 221 | more = false |
| 222 | else |
| 223 | put irlen 0 add get irlen 0 ulong n |
| 224 | if ge get irlen 0 1048576 |
| 225 | n2 = read 0 hb 0 1 |
| 226 | while eq n2 -4 |
| 227 | n2 = read 0 hb 0 1 |
| 228 | end |
| 229 | if gt n2 0 |
| 230 | ret 65 |
| 231 | end |
| 232 | if lt n2 0 |
| 233 | ret 74 |
| 234 | end |
| 235 | more = false |
| 236 | end |
| 237 | end |
| 238 | end |
| 239 | end |
| 240 | ret 0 |
| 241 | end |
| 242 | |
| 243 | fn findnl from ulong to ulong |
| 244 | i = from |
| 245 | while lt i get irlen 0 |
| 246 | if eq get big i 10 |
| 247 | ret i |
| 248 | end |
| 249 | i = add i 1 |
| 250 | end |
| 251 | ret get irlen 0 |
| 252 | end |
| 253 | |
| 254 | fn modeis o ulong n ulong t ulong to bool |
| 255 | if eq t 1 |
| 256 | if ne n 2 |
| 257 | ret false |
| 258 | end |
| 259 | if ne get big o 105 |
| 260 | ret false |
| 261 | end |
| 262 | ret eq get big add o 1 100 |
| 263 | end |
| 264 | if eq t 2 |
| 265 | if ne n 5 |
| 266 | ret false |
| 267 | end |
| 268 | if ne get big o 99 |
| 269 | ret false |
| 270 | end |
| 271 | if ne get big add o 1 104 |
| 272 | ret false |
| 273 | end |
| 274 | if ne get big add o 2 101 |
| 275 | ret false |
| 276 | end |
| 277 | if ne get big add o 3 99 |
| 278 | ret false |
| 279 | end |
| 280 | ret eq get big add o 4 107 |
| 281 | end |
| 282 | if eq t 3 |
| 283 | if ne n 8 |
| 284 | ret false |
| 285 | end |
| 286 | if ne get big o 109 |
| 287 | ret false |
| 288 | end |
| 289 | if ne get big add o 1 97 |
| 290 | ret false |
| 291 | end |
| 292 | if ne get big add o 2 110 |
| 293 | ret false |
| 294 | end |
| 295 | if ne get big add o 3 105 |
| 296 | ret false |
| 297 | end |
| 298 | if ne get big add o 4 102 |
| 299 | ret false |
| 300 | end |
| 301 | if ne get big add o 5 101 |
| 302 | ret false |
| 303 | end |
| 304 | if ne get big add o 6 115 |
| 305 | ret false |
| 306 | end |
| 307 | ret eq get big add o 7 116 |
| 308 | end |
| 309 | if eq t 4 |
| 310 | if ne n 6 |
| 311 | ret false |
| 312 | end |
| 313 | if ne get big o 115 |
| 314 | ret false |
| 315 | end |
| 316 | if ne get big add o 1 104 |
| 317 | ret false |
| 318 | end |
| 319 | if ne get big add o 2 97 |
| 320 | ret false |
| 321 | end |
| 322 | if ne get big add o 3 50 |
| 323 | ret false |
| 324 | end |
| 325 | if ne get big add o 4 53 |
| 326 | ret false |
| 327 | end |
| 328 | ret eq get big add o 5 54 |
| 329 | end |
| 330 | ret false |
| 331 | end |
| 332 | |
| 333 | fn runmode to ulong |
| 334 | e = findnl 0 |
| 335 | if lt e 5 |
| 336 | ret 0 |
| 337 | end |
| 338 | if ne get big 0 109 |
| 339 | ret 0 |
| 340 | end |
| 341 | if ne get big 1 111 |
| 342 | ret 0 |
| 343 | end |
| 344 | if ne get big 2 100 |
| 345 | ret 0 |
| 346 | end |
| 347 | if ne get big 3 101 |
| 348 | ret 0 |
| 349 | end |
| 350 | if ne get big 4 61 |
| 351 | ret 0 |
| 352 | end |
| 353 | v = ulong 5 |
| 354 | n = sub e v |
| 355 | if modeis v n 1 |
| 356 | ret 1 |
| 357 | end |
| 358 | if modeis v n 2 |
| 359 | ret 2 |
| 360 | end |
| 361 | if modeis v n 3 |
| 362 | ret 3 |
| 363 | end |
| 364 | if modeis v n 4 |
| 365 | ret 4 |
| 366 | end |
| 367 | ret 0 |
| 368 | end |
| 369 | |
| 370 | fn splp to void |
| 371 | st = get pstart 0 |
| 372 | n = ulong 0 |
| 373 | i = st |
| 374 | while lt i get irlen 0 |
| 375 | if eq get big i 10 |
| 376 | if ge n 256 |
| 377 | put nline 0 257 |
| 378 | ret |
| 379 | end |
| 380 | put pofs n st |
| 381 | put plen n sub i st |
| 382 | n = add n 1 |
| 383 | st = add i 1 |
| 384 | end |
| 385 | i = add i 1 |
| 386 | end |
| 387 | if lt st get irlen 0 |
| 388 | if ge n 256 |
| 389 | put nline 0 257 |
| 390 | ret |
| 391 | end |
| 392 | put pofs n st |
| 393 | put plen n sub get irlen 0 st |
| 394 | n = add n 1 |
| 395 | end |
| 396 | put nline 0 n |
| 397 | end |
| 398 | |
| 399 | fn schlen s ulong to ulong |
| 400 | if eq s 0 |
| 401 | ret len schm0 |
| 402 | end |
| 403 | ret len schm1 |
| 404 | end |
| 405 | |
| 406 | fn schbyte s ulong i ulong to uchar |
| 407 | if eq s 0 |
| 408 | ret get schm0 i |
| 409 | end |
| 410 | ret get schm1 i |
| 411 | end |
| 412 | |
| 413 | fn schsplit s ulong to void |
| 414 | L = schlen s |
| 415 | st = ulong 0 |
| 416 | n = ulong 0 |
| 417 | i = ulong 0 |
| 418 | while lt i L |
| 419 | if eq schbyte s i 10 |
| 420 | put sofs n st |
| 421 | put slen n sub i st |
| 422 | n = add n 1 |
| 423 | st = add i 1 |
| 424 | end |
| 425 | i = add i 1 |
| 426 | end |
| 427 | if lt st L |
| 428 | put sofs n st |
| 429 | put slen n sub L st |
| 430 | n = add n 1 |
| 431 | end |
| 432 | put nsch 0 n |
| 433 | end |
| 434 | |
| 435 | note ---- payload / schema line helpers ---- |
| 436 | |
| 437 | fn peq i ulong s ulong to bool |
| 438 | if ne get plen i get slen i |
| 439 | ret false |
| 440 | end |
| 441 | j = ulong 0 |
| 442 | while lt j get plen i |
| 443 | if ne get big add get pofs i j schbyte s add get sofs i j |
| 444 | ret false |
| 445 | end |
| 446 | j = add j 1 |
| 447 | end |
| 448 | ret true |
| 449 | end |
| 450 | |
| 451 | fn klen i ulong to ulong |
| 452 | j = ulong 0 |
| 453 | while lt j get plen i |
| 454 | if eq get big add get pofs i j 61 |
| 455 | ret j |
| 456 | end |
| 457 | j = add j 1 |
| 458 | end |
| 459 | ret j |
| 460 | end |
| 461 | |
| 462 | fn haseq i ulong to bool |
| 463 | ret lt klen i get plen i |
| 464 | end |
| 465 | |
| 466 | fn vlen i ulong to ulong |
| 467 | n = klen i |
| 468 | if ge n get plen i |
| 469 | ret 0 |
| 470 | end |
| 471 | ret sub get plen i add n 1 |
| 472 | end |
| 473 | |
| 474 | fn sklen s ulong j ulong to ulong |
| 475 | o = get sofs j |
| 476 | L = get slen j |
| 477 | q = ulong 0 |
| 478 | while lt q L |
| 479 | if eq schbyte s add o q 61 |
| 480 | ret q |
| 481 | end |
| 482 | q = add q 1 |
| 483 | end |
| 484 | ret L |
| 485 | end |
| 486 | |
| 487 | fn keysame i ulong s ulong j ulong to bool |
| 488 | n = klen i |
| 489 | if ne n sklen s j |
| 490 | ret false |
| 491 | end |
| 492 | q = ulong 0 |
| 493 | while lt q n |
| 494 | if ne get big add get pofs i q schbyte s add get sofs j q |
| 495 | ret false |
| 496 | end |
| 497 | q = add q 1 |
| 498 | end |
| 499 | ret true |
| 500 | end |
| 501 | |
| 502 | fn findpk s ulong j ulong to bool |
| 503 | i = ulong 0 |
| 504 | while lt i get nline 0 |
| 505 | if keysame i s j |
| 506 | ret true |
| 507 | end |
| 508 | i = add i 1 |
| 509 | end |
| 510 | ret false |
| 511 | end |
| 512 | |
| 513 | fn findpkey s ulong i ulong to bool |
| 514 | j = ulong 0 |
| 515 | while lt j get nsch 0 |
| 516 | if keysame i s j |
| 517 | ret true |
| 518 | end |
| 519 | j = add j 1 |
| 520 | end |
| 521 | ret false |
| 522 | end |
| 523 | |
| 524 | fn isdtype s ulong i ulong to bool |
| 525 | if lt get slen i 2 |
| 526 | ret false |
| 527 | end |
| 528 | if ne schbyte s get sofs i 100 |
| 529 | ret false |
| 530 | end |
| 531 | ret eq schbyte s add get sofs i 1 116 |
| 532 | end |
| 533 | |
| 534 | fn islayout s ulong i ulong to bool |
| 535 | ret eq schbyte s get sofs i 108 |
| 536 | end |
| 537 | |
| 538 | fn isstride s ulong i ulong to bool |
| 539 | o = get sofs i |
| 540 | L = get slen i |
| 541 | if lt L 6 |
| 542 | ret false |
| 543 | end |
| 544 | q = ulong 0 |
| 545 | while le add q 6 L |
| 546 | if eq schbyte s add o q 115 |
| 547 | if eq schbyte s add o add q 1 116 |
| 548 | if eq schbyte s add o add q 2 114 |
| 549 | if eq schbyte s add o add q 3 105 |
| 550 | if eq schbyte s add o add q 4 100 |
| 551 | if eq schbyte s add o add q 5 101 |
| 552 | ret true |
| 553 | end |
| 554 | end |
| 555 | end |
| 556 | end |
| 557 | end |
| 558 | end |
| 559 | q = add q 1 |
| 560 | end |
| 561 | ret false |
| 562 | end |
| 563 | |
| 564 | fn istol s ulong i ulong to bool |
| 565 | if lt get slen i 2 |
| 566 | ret false |
| 567 | end |
| 568 | if ne schbyte s get sofs i 116 |
| 569 | ret false |
| 570 | end |
| 571 | ret eq schbyte s add get sofs i 1 111 |
| 572 | end |
| 573 | |
| 574 | fn vbyte i ulong q ulong to uchar |
| 575 | ret get big add add get pofs i add klen i 1 q |
| 576 | end |
| 577 | |
| 578 | fn veqt i ulong t ulong to bool |
| 579 | n = vlen i |
| 580 | if eq n 0 |
| 581 | ret false |
| 582 | end |
| 583 | if eq t 0 |
| 584 | if ne n len mid |
| 585 | ret false |
| 586 | end |
| 587 | j = ulong 0 |
| 588 | while lt j n |
| 589 | if ne vbyte i j get mid j |
| 590 | ret false |
| 591 | end |
| 592 | j = add j 1 |
| 593 | end |
| 594 | ret true |
| 595 | end |
| 596 | if eq t 1 |
| 597 | if ne n len gid |
| 598 | ret false |
| 599 | end |
| 600 | j = ulong 0 |
| 601 | while lt j n |
| 602 | if ne vbyte i j get gid j |
| 603 | ret false |
| 604 | end |
| 605 | j = add j 1 |
| 606 | end |
| 607 | ret true |
| 608 | end |
| 609 | if eq t 2 |
| 610 | if ne n len omth |
| 611 | ret false |
| 612 | end |
| 613 | j = ulong 0 |
| 614 | while lt j n |
| 615 | if ne vbyte i j get omth j |
| 616 | ret false |
| 617 | end |
| 618 | j = add j 1 |
| 619 | end |
| 620 | ret true |
| 621 | end |
| 622 | if eq t 3 |
| 623 | if ne n len ogqa |
| 624 | ret false |
| 625 | end |
| 626 | j = ulong 0 |
| 627 | while lt j n |
| 628 | if ne vbyte i j get ogqa j |
| 629 | ret false |
| 630 | end |
| 631 | j = add j 1 |
| 632 | end |
| 633 | ret true |
| 634 | end |
| 635 | ret false |
| 636 | end |
| 637 | |
| 638 | fn valcmp i ulong j ulong to bool |
| 639 | n1 = vlen i |
| 640 | if eq n1 0 |
| 641 | ret false |
| 642 | end |
| 643 | if ne n1 vlen j |
| 644 | ret false |
| 645 | end |
| 646 | q = ulong 0 |
| 647 | while lt q n1 |
| 648 | if ne vbyte i q vbyte j q |
| 649 | ret false |
| 650 | end |
| 651 | q = add q 1 |
| 652 | end |
| 653 | ret true |
| 654 | end |
| 655 | |
| 656 | fn pclass i ulong s ulong to ulong |
| 657 | if peq i s |
| 658 | ret 0 |
| 659 | end |
| 660 | if eq get plen i 0 |
| 661 | ret 4 |
| 662 | end |
| 663 | if not haseq i |
| 664 | ret 2 |
| 665 | end |
| 666 | if keysame i s i |
| 667 | if isdtype s i |
| 668 | ret 7 |
| 669 | end |
| 670 | if islayout s i |
| 671 | ret 8 |
| 672 | end |
| 673 | if isstride s i |
| 674 | ret 8 |
| 675 | end |
| 676 | if istol s i |
| 677 | ret 6 |
| 678 | end |
| 679 | ret 3 |
| 680 | end |
| 681 | if findpkey s i |
| 682 | ret 2 |
| 683 | end |
| 684 | ret 5 |
| 685 | end |
| 686 | |
| 687 | fn docheck to ulong |
| 688 | schsplit 0 |
| 689 | if gt get nline 0 256 |
| 690 | ret 3 |
| 691 | end |
| 692 | if lt get nline 0 1 |
| 693 | ret 1 |
| 694 | end |
| 695 | if eq get irlen 0 get pstart 0 |
| 696 | ret 3 |
| 697 | end |
| 698 | if ne get big sub get irlen 0 1 10 |
| 699 | ret 2 |
| 700 | end |
| 701 | if not peq 0 0 |
| 702 | ret 1 |
| 703 | end |
| 704 | if lt get nline 0 2 |
| 705 | ret 4 |
| 706 | end |
| 707 | if not keysame 1 0 1 |
| 708 | ret 2 |
| 709 | end |
| 710 | s = ulong 9 |
| 711 | if veqt 1 2 |
| 712 | s = ulong 0 |
| 713 | else |
| 714 | if veqt 1 3 |
| 715 | s = ulong 1 |
| 716 | else |
| 717 | ret 3 |
| 718 | end |
| 719 | end |
| 720 | schsplit s |
| 721 | if lt get nline 0 get nsch 0 |
| 722 | ret 4 |
| 723 | end |
| 724 | j = ulong 0 |
| 725 | while lt j get nsch 0 |
| 726 | if not findpk s j |
| 727 | ret 4 |
| 728 | end |
| 729 | j = add j 1 |
| 730 | end |
| 731 | i = ulong 0 |
| 732 | while lt i get nsch 0 |
| 733 | c = pclass i s |
| 734 | if ne c 0 |
| 735 | ret c |
| 736 | end |
| 737 | i = add i 1 |
| 738 | end |
| 739 | while lt i get nline 0 |
| 740 | if eq get plen i 0 |
| 741 | ret 2 |
| 742 | end |
| 743 | if not haseq i |
| 744 | ret 2 |
| 745 | end |
| 746 | if findpkey s i |
| 747 | ret 2 |
| 748 | end |
| 749 | ret 5 |
| 750 | end |
| 751 | ret 0 |
| 752 | end |
| 753 | |
| 754 | note ---- manifest ---- |
| 755 | |
| 756 | fn packn o ulong n ulong to ulong |
| 757 | v = ulong 0 |
| 758 | j = ulong 0 |
| 759 | while lt j n |
| 760 | v = or shl v 8 ulong get big add o j |
| 761 | j = add j 1 |
| 762 | end |
| 763 | ret v |
| 764 | end |
| 765 | |
| 766 | fn keyis i ulong t ulong to bool |
| 767 | n = klen i |
| 768 | o = get pofs i |
| 769 | if eq t 1 |
| 770 | if ne n 6 |
| 771 | ret false |
| 772 | end |
| 773 | ret eq packn o 6 111567956173684 |
| 774 | end |
| 775 | if eq t 2 |
| 776 | if ne n 2 |
| 777 | ret false |
| 778 | end |
| 779 | ret eq packn o 2 28528 |
| 780 | end |
| 781 | if eq t 3 |
| 782 | if ne n 8 |
| 783 | ret false |
| 784 | end |
| 785 | ret eq packn o 8 7165066978583995252 |
| 786 | end |
| 787 | if eq t 4 |
| 788 | if ne n 7 |
| 789 | ret false |
| 790 | end |
| 791 | ret eq packn o 7 28826413695332965 |
| 792 | end |
| 793 | if eq t 5 |
| 794 | if ne n 12 |
| 795 | ret false |
| 796 | end |
| 797 | if ne packn o 8 7597133271186502504 |
| 798 | ret false |
| 799 | end |
| 800 | ret eq packn add o 8 4 1630680374 |
| 801 | end |
| 802 | if eq t 6 |
| 803 | if ne n 4 |
| 804 | ret false |
| 805 | end |
| 806 | ret eq packn o 4 1936024932 |
| 807 | end |
| 808 | if eq t 7 |
| 809 | if ne n 5 |
| 810 | ret false |
| 811 | end |
| 812 | ret eq packn o 5 427071729268 |
| 813 | end |
| 814 | if eq t 8 |
| 815 | if ne n 11 |
| 816 | ret false |
| 817 | end |
| 818 | if ne packn o 8 8097887111620944991 |
| 819 | ret false |
| 820 | end |
| 821 | ret eq packn add o 8 3 6841720 |
| 822 | end |
| 823 | ret false |
| 824 | end |
| 825 | |
| 826 | fn hdigit c uchar to bool |
| 827 | if ge c 48 |
| 828 | if le c 57 |
| 829 | ret true |
| 830 | end |
| 831 | end |
| 832 | if ge c 97 |
| 833 | if le c 102 |
| 834 | ret true |
| 835 | end |
| 836 | end |
| 837 | ret false |
| 838 | end |
| 839 | |
| 840 | fn hval c uchar to uchar |
| 841 | if le c 57 |
| 842 | ret uchar sub c 48 |
| 843 | end |
| 844 | ret uchar sub c 87 |
| 845 | end |
| 846 | |
| 847 | fn hex64 i ulong to bool |
| 848 | if not haseq i |
| 849 | ret false |
| 850 | end |
| 851 | if ne vlen i 64 |
| 852 | ret false |
| 853 | end |
| 854 | j = ulong 0 |
| 855 | while lt j 64 |
| 856 | if not hdigit vbyte i j |
| 857 | ret false |
| 858 | end |
| 859 | j = add j 1 |
| 860 | end |
| 861 | ret true |
| 862 | end |
| 863 | |
| 864 | fn decok i ulong lim ulong to bool |
| 865 | if not haseq i |
| 866 | ret false |
| 867 | end |
| 868 | n = vlen i |
| 869 | if eq n 0 |
| 870 | ret false |
| 871 | end |
| 872 | v = ulong 0 |
| 873 | j = ulong 0 |
| 874 | while lt j n |
| 875 | c = vbyte i j |
| 876 | if or lt c 48 gt c 57 |
| 877 | ret false |
| 878 | end |
| 879 | d = ulong sub c 48 |
| 880 | if gt v div sub lim d 10 |
| 881 | ret false |
| 882 | end |
| 883 | v = add mul v 10 d |
| 884 | j = add j 1 |
| 885 | end |
| 886 | ret true |
| 887 | end |
| 888 | |
| 889 | fn nameok i ulong to bool |
| 890 | if not haseq i |
| 891 | ret false |
| 892 | end |
| 893 | n = vlen i |
| 894 | if eq n 0 |
| 895 | ret false |
| 896 | end |
| 897 | if gt n 64 |
| 898 | ret false |
| 899 | end |
| 900 | j = ulong 0 |
| 901 | while lt j n |
| 902 | c = vbyte i j |
| 903 | ok = false |
| 904 | if ge c 65 |
| 905 | if le c 90 |
| 906 | ok = true |
| 907 | end |
| 908 | end |
| 909 | if ge c 97 |
| 910 | if le c 122 |
| 911 | ok = true |
| 912 | end |
| 913 | end |
| 914 | if ge c 48 |
| 915 | if le c 57 |
| 916 | ok = true |
| 917 | end |
| 918 | end |
| 919 | if eq c 46 |
| 920 | ok = true |
| 921 | end |
| 922 | if eq c 95 |
| 923 | ok = true |
| 924 | end |
| 925 | if eq c 45 |
| 926 | ok = true |
| 927 | end |
| 928 | if not ok |
| 929 | ret false |
| 930 | end |
| 931 | j = add j 1 |
| 932 | end |
| 933 | ret true |
| 934 | end |
| 935 | |
| 936 | fn mfind t ulong to bool |
| 937 | i = ulong 0 |
| 938 | while lt i get nline 0 |
| 939 | if keyis i t |
| 940 | ret true |
| 941 | end |
| 942 | i = add i 1 |
| 943 | end |
| 944 | ret false |
| 945 | end |
| 946 | |
| 947 | fn mhdrline to bool |
| 948 | if ne get plen 1 len mhdr |
| 949 | ret false |
| 950 | end |
| 951 | j = ulong 0 |
| 952 | while lt j len mhdr |
| 953 | if ne get big add get pofs 1 j get mhdr j |
| 954 | ret false |
| 955 | end |
| 956 | j = add j 1 |
| 957 | end |
| 958 | ret true |
| 959 | end |
| 960 | |
| 961 | fn hashhexline i ulong to bool |
| 962 | if not haseq i |
| 963 | ret false |
| 964 | end |
| 965 | n = vlen i |
| 966 | if eq n 0 |
| 967 | ret false |
| 968 | end |
| 969 | if ne and n 1 0 |
| 970 | ret false |
| 971 | end |
| 972 | hreset |
| 973 | j = ulong 0 |
| 974 | while lt j n |
| 975 | c1 = vbyte i j |
| 976 | c2 = vbyte i add j 1 |
| 977 | if not hdigit c1 |
| 978 | ret false |
| 979 | end |
| 980 | if not hdigit c2 |
| 981 | ret false |
| 982 | end |
| 983 | b = uchar add shl hval c1 4 hval c2 |
| 984 | if not hpush b |
| 985 | ret false |
| 986 | end |
| 987 | j = add j 2 |
| 988 | end |
| 989 | ret hfinal |
| 990 | end |
| 991 | |
| 992 | fn hdeqline i ulong to bool |
| 993 | if ne vlen i 64 |
| 994 | ret false |
| 995 | end |
| 996 | j = ulong 0 |
| 997 | while lt j 64 |
| 998 | if ne get hd j vbyte i j |
| 999 | ret false |
| 1000 | end |
| 1001 | j = add j 1 |
| 1002 | end |
| 1003 | ret true |
| 1004 | end |
| 1005 | |
| 1006 | fn domain to ulong |
| 1007 | if lt get nline 0 2 |
| 1008 | ret 1 |
| 1009 | end |
| 1010 | if eq get irlen 0 get pstart 0 |
| 1011 | ret 1 |
| 1012 | end |
| 1013 | if ne get big sub get irlen 0 1 10 |
| 1014 | ret 2 |
| 1015 | end |
| 1016 | if not keyis 0 1 |
| 1017 | ret 1 |
| 1018 | end |
| 1019 | if not mhdrline |
| 1020 | ret 1 |
| 1021 | end |
| 1022 | if lt get nline 0 8 |
| 1023 | ret 4 |
| 1024 | end |
| 1025 | if gt get nline 0 9 |
| 1026 | ret 5 |
| 1027 | end |
| 1028 | t = ulong 2 |
| 1029 | while le t 7 |
| 1030 | if not mfind t |
| 1031 | ret 4 |
| 1032 | end |
| 1033 | t = add t 1 |
| 1034 | end |
| 1035 | t = ulong 2 |
| 1036 | while le t 7 |
| 1037 | if not keyis t t |
| 1038 | ret 2 |
| 1039 | end |
| 1040 | t = add t 1 |
| 1041 | end |
| 1042 | if eq get nline 0 9 |
| 1043 | if not keyis 8 8 |
| 1044 | ret 5 |
| 1045 | end |
| 1046 | end |
| 1047 | if not hex64 0 |
| 1048 | ret 3 |
| 1049 | end |
| 1050 | if not hex64 3 |
| 1051 | ret 3 |
| 1052 | end |
| 1053 | if not hex64 5 |
| 1054 | ret 3 |
| 1055 | end |
| 1056 | if not veqt 2 2 |
| 1057 | if not veqt 2 3 |
| 1058 | ret 3 |
| 1059 | end |
| 1060 | end |
| 1061 | if not veqt 3 0 |
| 1062 | if not veqt 3 1 |
| 1063 | ret 3 |
| 1064 | end |
| 1065 | end |
| 1066 | if not valcmp 3 0 |
| 1067 | ret 9 |
| 1068 | end |
| 1069 | if veqt 2 2 |
| 1070 | if not veqt 3 0 |
| 1071 | ret 9 |
| 1072 | end |
| 1073 | end |
| 1074 | if veqt 2 3 |
| 1075 | if not veqt 3 1 |
| 1076 | ret 9 |
| 1077 | end |
| 1078 | end |
| 1079 | if not nameok 4 |
| 1080 | ret 3 |
| 1081 | end |
| 1082 | if not decok 6 18446744073709551615 |
| 1083 | ret 3 |
| 1084 | end |
| 1085 | if not decok 7 4294967295 |
| 1086 | ret 3 |
| 1087 | end |
| 1088 | if eq get nline 0 9 |
| 1089 | if not hashhexline 8 |
| 1090 | ret 3 |
| 1091 | end |
| 1092 | if not hdeqline 5 |
| 1093 | ret 3 |
| 1094 | end |
| 1095 | end |
| 1096 | ret 0 |
| 1097 | end |
| 1098 | |
| 1099 | note ---- output ---- |
| 1100 | |
| 1101 | fn wokhex to bool |
| 1102 | w = write 1 okmsg 0 len okmsg |
| 1103 | if ne ulong w len okmsg |
| 1104 | ret false |
| 1105 | end |
| 1106 | w = write 1 hd 0 64 |
| 1107 | if ne ulong w 64 |
| 1108 | ret false |
| 1109 | end |
| 1110 | w = write 1 newln 0 1 |
| 1111 | ret eq ulong w 1 |
| 1112 | end |
| 1113 | |
| 1114 | fn whex to bool |
| 1115 | w = write 1 hd 0 64 |
| 1116 | if ne ulong w 64 |
| 1117 | ret false |
| 1118 | end |
| 1119 | w = write 1 newln 0 1 |
| 1120 | ret eq ulong w 1 |
| 1121 | end |
| 1122 | |
| 1123 | fn wslice off ulong n ulong to bool |
| 1124 | p = off |
| 1125 | e = add off n |
| 1126 | while lt p e |
| 1127 | w = write 1 big p sub e p |
| 1128 | if le w 0 |
| 1129 | ret false |
| 1130 | end |
| 1131 | p = add p ulong w |
| 1132 | end |
| 1133 | ret true |
| 1134 | end |
| 1135 | |
| 1136 | fn wmanok to bool |
| 1137 | w = write 1 okmsg 0 len okmsg |
| 1138 | if ne ulong w len okmsg |
| 1139 | ret false |
| 1140 | end |
| 1141 | if not wslice add get pofs 3 add klen 3 1 64 |
| 1142 | ret false |
| 1143 | end |
| 1144 | w = write 1 newln 0 1 |
| 1145 | ret eq ulong w 1 |
| 1146 | end |
| 1147 | |
| 1148 | fn eref c ulong to void |
| 1149 | w = write 1 refmsg 0 len refmsg |
| 1150 | if eq c 1 |
| 1151 | w = write 1 ehead 0 len ehead |
| 1152 | end |
| 1153 | if eq c 2 |
| 1154 | w = write 1 eorder 0 len eorder |
| 1155 | end |
| 1156 | if eq c 3 |
| 1157 | w = write 1 evalue 0 len evalue |
| 1158 | end |
| 1159 | if eq c 4 |
| 1160 | w = write 1 emiss 0 len emiss |
| 1161 | end |
| 1162 | if eq c 5 |
| 1163 | w = write 1 eunknown 0 len eunknown |
| 1164 | end |
| 1165 | if eq c 6 |
| 1166 | w = write 1 etol 0 len etol |
| 1167 | end |
| 1168 | if eq c 7 |
| 1169 | w = write 1 edtype 0 len edtype |
| 1170 | end |
| 1171 | if eq c 8 |
| 1172 | w = write 1 elayout 0 len elayout |
| 1173 | end |
| 1174 | if eq c 9 |
| 1175 | w = write 1 etarget 0 len etarget |
| 1176 | end |
| 1177 | end |
| 1178 | |
| 1179 | fn hashpay to bool |
| 1180 | hreset |
| 1181 | i = get pstart 0 |
| 1182 | while lt i get irlen 0 |
| 1183 | if not hpush get big i |
| 1184 | ret false |
| 1185 | end |
| 1186 | i = add i 1 |
| 1187 | end |
| 1188 | ret hfinal |
| 1189 | end |
| 1190 | |
| 1191 | fn main to int |
| 1192 | r = readall |
| 1193 | if ne r 0 |
| 1194 | ret r |
| 1195 | end |
| 1196 | m = runmode |
| 1197 | if eq m 0 |
| 1198 | eref 1 |
| 1199 | ret 1 |
| 1200 | end |
| 1201 | e = findnl 0 |
| 1202 | if lt e get irlen 0 |
| 1203 | put pstart 0 add e 1 |
| 1204 | else |
| 1205 | put pstart 0 get irlen 0 |
| 1206 | end |
| 1207 | splp |
| 1208 | if eq m 1 |
| 1209 | if not hashpay |
| 1210 | ret 65 |
| 1211 | end |
| 1212 | if not whex |
| 1213 | ret 74 |
| 1214 | end |
| 1215 | ret 0 |
| 1216 | end |
| 1217 | if eq m 4 |
| 1218 | if not hashpay |
| 1219 | ret 65 |
| 1220 | end |
| 1221 | if not whex |
| 1222 | ret 74 |
| 1223 | end |
| 1224 | ret 0 |
| 1225 | end |
| 1226 | if eq m 2 |
| 1227 | c = docheck |
| 1228 | if eq c 0 |
| 1229 | if not hashpay |
| 1230 | ret 65 |
| 1231 | end |
| 1232 | if not wokhex |
| 1233 | ret 74 |
| 1234 | end |
| 1235 | ret 0 |
| 1236 | end |
| 1237 | eref c |
| 1238 | ret 1 |
| 1239 | end |
| 1240 | if eq m 3 |
| 1241 | c = domain |
| 1242 | if eq c 0 |
| 1243 | if not wmanok |
| 1244 | ret 74 |
| 1245 | end |
| 1246 | ret 0 |
| 1247 | end |
| 1248 | eref c |
| 1249 | ret 1 |
| 1250 | end |
| 1251 | eref 1 |
| 1252 | ret 1 |
| 1253 | end |