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/om/tls/test/mlkem/driver.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 / om / tls / test / mlkem / driver.om` 594 lines ยท 11275 bytes Raw Blame History 1 note SPDX-License-Identifier: Apache-2.0 2 note driver.om: test driver for om/tls/sha3.om and om/tls/mlkem.om (program mode). run.sh writes the 3 note line om2, then sha3.om, mlkem.om and this file. Reads a vector file on stdin, one case per line, 4 note fields separated by one space, byte strings in lowercase hex, - for an empty or unchecked field: 5 note H A MSG OUT one-shot hash: A 1 SHA3-256, 2 SHA3-512, 3 SHAKE128, 4 SHAKE256 (len OUT bytes) 6 note S A MSG OUT the same through init / absorb / squeeze in varying chunk sizes 7 note K D Z EK DK mlkem_keygen d z must return 0 and write EK, DK 8 note Q R SEED EK DK mlkem_keygen_seed seed must return R; when R = 0 it must write EK, DK 9 note E R EK M C K mlkem_encaps ek m must return R; R = 0: writes C, K; else ct, ss all zero 10 note D R DK C K mlkem_decaps dk c must return R; R = 0: writes K; else ss all zero 11 note C R1 SEED EK C R2 K keygen_seed seed returns R1 (EK when 0); then decaps dk c returns R2 (K when 0) 12 note L length checks: six calls with one wrong-sized buffer must return 2 13 note T N timing: N keygen, N encaps, N decaps calls; prints TIME line L OP n N ns T 14 note An unchecked field (-) of an output is skipped; an empty input field (-) is zero bytes. 15 note Lines starting with # and empty lines are skipped. Prints one MISMATCH line per wrong result 16 note and the summary line: cases C calls V mismatches X ns T (T = ns spent in checked calls). 17 note Exit 0 only when at least one checked call ran and none mismatched; 1 otherwise; 65 bad input. 18 19 text MTTMIS "MISMATCH line " 20 text MTTGOT " got rc " 21 text MTTWANT " want rc " 22 text MTTBYTE " bytes " 23 text MTTCASE "cases " 24 text MTTCALL " calls " 25 text MTTMM " mismatches " 26 text MTTNS " ns " 27 text MTTTIME "TIME line " 28 text MTTKG " keygen" 29 text MTTEN " encaps" 30 text MTTDE " decaps" 31 text MTTN " n " 32 text MTTNL "\n" 33 text MTTBAD "driver: bad input\n" 34 35 const MTBUF ulong 65536 36 37 global MTIN uchar `16777216` 38 global MTLEN ulong 1 39 global MTOUT uchar 32 40 global MTCNT ulong 8 41 global MTOK ulong 32 42 43 fn mlkemt_num v ulong to void 44 k = ulong 0 45 x = v 46 while true bound 30 47 put MTOUT sub 31 k uchar add rem x 10 48 48 k = add k 1 49 x = div x 10 50 if eq x 0 51 break 52 end 53 end 54 r = write 1 MTOUT sub 32 k k 55 end 56 57 fn mlkemt_say t view uchar * to void 58 r = write 1 t 0 len t 59 end 60 61 fn mlkemt_bad to void 62 r = write 2 MTTBAD 0 len MTTBAD 63 exit 65 64 end 65 66 fn mlkemt_nib c ulong to ulong 67 if and ge c 48 le c 57 68 ret sub c 48 69 end 70 if and ge c 97 le c 102 71 ret sub c 87 72 end 73 mlkemt_bad 74 ret 0 75 end 76 77 fn mlkemt_bump i ulong v ulong to void 78 put MTCNT i add get MTCNT i v 79 end 80 81 note split MTIN[p..e) at single spaces into MTOK (start, end pairs); n fields required 82 fn mlkemt_tok p ulong e ulong n ulong to void 83 k = ulong 0 84 s = p 85 q = p 86 while le q e bound 999999 87 if or eq q e eq get MTIN q 32 88 if or eq q s ge k 16 89 mlkemt_bad 90 end 91 put MTOK mul k 2 s 92 put MTOK add mul k 2 1 q 93 k = add k 1 94 s = add q 1 95 end 96 q = add q 1 97 end 98 if ne k n 99 mlkemt_bad 100 end 101 end 102 103 note 1 when field k is the single character - 104 fn mlkemt_dash k ulong to ulong 105 s = get MTOK mul k 2 106 if and eq sub get MTOK add mul k 2 1 s 1 eq get MTIN s 45 107 ret 1 108 end 109 ret 0 110 end 111 112 note byte length of hex field k (0 for -) 113 fn mlkemt_tl k ulong to ulong 114 if eq mlkemt_dash k 1 115 ret 0 116 end 117 n = sub get MTOK add mul k 2 1 get MTOK mul k 2 118 if ne and n 1 0 119 mlkemt_bad 120 end 121 ret shr n 1 122 end 123 124 note decimal field k 125 fn mlkemt_dec k ulong to ulong 126 s = get MTOK mul k 2 127 e = get MTOK add mul k 2 1 128 v = ulong 0 129 for i ulong sub e s 130 c = ulong get MTIN add s i 131 if or or lt c 48 gt c 57 ge i 9 132 mlkemt_bad 133 end 134 v = add mul v 10 sub c 48 135 end 136 ret v 137 end 138 139 note hex field k into all of a (its byte length must be len a) 140 fn mlkemt_hex a edit uchar * k ulong to void 141 if ne mlkemt_tl k len a 142 mlkemt_bad 143 end 144 s = get MTOK mul k 2 145 for i ulong len a 146 hi = mlkemt_nib ulong get MTIN add s mul i 2 147 lo = mlkemt_nib ulong get MTIN add add s mul i 2 1 148 put a i uchar or shl hi 4 lo 149 end 150 end 151 152 note 0 when o equals hex field k (or k is -), else 1 153 fn mlkemt_cmp o view uchar * k ulong to ulong 154 if eq mlkemt_dash k 1 155 ret 0 156 end 157 if ne mlkemt_tl k len o 158 ret 1 159 end 160 s = get MTOK mul k 2 161 d = ulong 0 162 for i ulong len o 163 hi = mlkemt_nib ulong get MTIN add s mul i 2 164 lo = mlkemt_nib ulong get MTIN add add s mul i 2 1 165 d = or d xor ulong get o i or shl hi 4 lo 166 end 167 if ne d 0 168 ret 1 169 end 170 ret 0 171 end 172 173 note 0 when every byte of o is zero, else 1 174 fn mlkemt_zero o view uchar * to ulong 175 d = ulong 0 176 for i ulong len o 177 d = or d ulong get o i 178 end 179 if ne d 0 180 ret 1 181 end 182 ret 0 183 end 184 185 note counters in MTCNT: 0 line, 1 cases, 2 calls, 3 mismatches, 4 ns 186 fn mlkemt_check got ulong want ulong d ulong to void 187 mlkemt_bump 2 1 188 if or ne got want ne d 0 189 mlkemt_bump 3 1 190 mlkemt_say MTTMIS 191 mlkemt_num get MTCNT 0 192 mlkemt_say MTTGOT 193 mlkemt_num got 194 mlkemt_say MTTWANT 195 mlkemt_num want 196 mlkemt_say MTTBYTE 197 mlkemt_num d 198 mlkemt_say MTTNL 199 end 200 end 201 202 fn mlkemt_lap t0 long to void 203 mlkemt_bump 4 ulong sub monons t0 204 end 205 206 note one-shot hash of m into o by algorithm a 207 fn mlkemt_hash a ulong m view uchar * o edit uchar * to void 208 if eq a 1 209 sha3_256 m o 210 ret 211 end 212 if eq a 2 213 sha3_512 m o 214 ret 215 end 216 if eq a 3 217 sha3_shake128 m o 218 ret 219 end 220 sha3_shake256 m o 221 end 222 223 note the same hash through the streaming API: absorb chunks 1 + (37 i mod 211), squeeze chunks 224 note 1 + (53 i mod 199) bytes 225 fn mlkemt_strm a ulong m view uchar * o edit uchar * to void 226 c = new ulong S3CN 0 227 sha3_shake256_init c 228 if eq a 1 229 sha3_256_init c 230 end 231 if eq a 2 232 sha3_512_init c 233 end 234 if eq a 3 235 sha3_shake128_init c 236 end 237 q = ulong 0 238 i = ulong 0 239 while lt q len m bound 999999 240 k = add 1 rem mul i 37 211 241 if gt k sub len m q 242 k = sub len m q 243 end 244 sha3_absorb c part m q k 245 q = add q k 246 i = add i 1 247 end 248 q = 0 249 while lt q len o bound 999999 250 k = add 1 rem mul i 53 199 251 if gt k sub len o q 252 k = sub len o q 253 end 254 sha3_squeeze c part o q k 255 q = add q k 256 i = add i 1 257 end 258 end 259 260 fn mlkemt_h p ulong e ulong st ulong to void 261 m = new uchar MTBUF 0 262 o = new uchar 4096 0 263 mlkemt_tok p e 4 264 a = mlkemt_dec 1 265 n = mlkemt_tl 2 266 w = mlkemt_tl 3 267 if or lt a 1 gt a 4 268 mlkemt_bad 269 end 270 if or gt n MTBUF gt w 4096 271 mlkemt_bad 272 end 273 if and eq a 1 ne w 32 274 mlkemt_bad 275 end 276 if and eq a 2 ne w 64 277 mlkemt_bad 278 end 279 mlkemt_hex part m 0 n 2 280 t0 = monons 281 if eq st 0 282 mlkemt_hash a part m 0 n part o 0 w 283 else 284 mlkemt_strm a part m 0 n part o 0 w 285 end 286 mlkemt_lap t0 287 mlkemt_check 0 0 mlkemt_cmp part o 0 w 3 288 end 289 290 fn mlkemt_k to void 291 d = new uchar 32 0 292 z = new uchar 32 0 293 ek = new uchar MKEKSZ 0 294 dk = new uchar MKDKSZ 0 295 mlkemt_hex d 1 296 mlkemt_hex z 2 297 t0 = monons 298 r = mlkem_keygen d z ek dk 299 mlkemt_lap t0 300 mlkemt_check r 0 or mlkemt_cmp ek 3 mlkemt_cmp dk 4 301 end 302 303 fn mlkemt_q to void 304 s = new uchar 128 0 305 ek = new uchar MKEKSZ 0 306 dk = new uchar MKDKSZ 0 307 w = mlkemt_dec 1 308 n = mlkemt_tl 2 309 if gt n 128 310 mlkemt_bad 311 end 312 mlkemt_hex part s 0 n 2 313 t0 = monons 314 r = mlkem_keygen_seed part s 0 n ek dk 315 mlkemt_lap t0 316 d = ulong 0 317 if eq w 0 318 d = or mlkemt_cmp ek 3 mlkemt_cmp dk 4 319 end 320 mlkemt_check r w d 321 end 322 323 fn mlkemt_e to void 324 ek = new uchar 4096 0 325 m = new uchar 32 0 326 ct = new uchar MKCTSZ 0 327 ss = new uchar 32 0 328 w = mlkemt_dec 1 329 n = mlkemt_tl 2 330 if gt n 4096 331 mlkemt_bad 332 end 333 mlkemt_hex part ek 0 n 2 334 mlkemt_hex m 3 335 for i ulong MKCTSZ 336 put ct i 0xa5 337 end 338 t0 = monons 339 r = mlkem_encaps part ek 0 n m ct ss 340 mlkemt_lap t0 341 d = ulong 0 342 if eq w 0 343 d = or mlkemt_cmp ct 4 mlkemt_cmp ss 5 344 else 345 d = or mlkemt_zero ct mlkemt_zero ss 346 end 347 mlkemt_check r w d 348 end 349 350 note decaps of field kd (dk bytes from buffer dk) and field kc into ss, want rc w, K at field kk 351 fn mlkemt_dec2 dk view uchar * kc ulong w ulong kk ulong to void 352 c = new uchar 4096 0 353 ss = new uchar 32 0 354 n = mlkemt_tl kc 355 if gt n 4096 356 mlkemt_bad 357 end 358 mlkemt_hex part c 0 n kc 359 for i ulong 32 360 put ss i 0xa5 361 end 362 t0 = monons 363 r = mlkem_decaps dk part c 0 n ss 364 mlkemt_lap t0 365 d = ulong 0 366 if eq w 0 367 d = mlkemt_cmp ss kk 368 else 369 d = mlkemt_zero ss 370 end 371 mlkemt_check r w d 372 end 373 374 fn mlkemt_d to void 375 dk = new uchar 4096 0 376 n = mlkemt_tl 2 377 if gt n 4096 378 mlkemt_bad 379 end 380 mlkemt_hex part dk 0 n 2 381 mlkemt_dec2 part dk 0 n 3 mlkemt_dec 1 4 382 end 383 384 fn mlkemt_c to void 385 s = new uchar 128 0 386 ek = new uchar MKEKSZ 0 387 dk = new uchar MKDKSZ 0 388 w = mlkemt_dec 1 389 n = mlkemt_tl 2 390 if gt n 128 391 mlkemt_bad 392 end 393 mlkemt_hex part s 0 n 2 394 t0 = monons 395 r = mlkem_keygen_seed part s 0 n ek dk 396 mlkemt_lap t0 397 d = ulong 0 398 if eq w 0 399 d = mlkemt_cmp ek 3 400 end 401 mlkemt_check r w d 402 if and eq w 0 eq r 0 403 mlkemt_dec2 dk 4 mlkemt_dec 5 6 404 end 405 end 406 407 note seven calls: six with one wrong-sized buffer must return 2, one well-sized keygen_seed 0 408 fn mlkemt_l1 to void 409 d = new uchar 32 0 410 z = new uchar 32 0 411 ek = new uchar 1185 0 412 dk = new uchar 2401 0 413 r = mlkem_keygen d z part ek 0 1183 part dk 0 2400 414 mlkemt_check r 2 0 415 r = mlkem_keygen d z part ek 0 1184 dk 416 mlkemt_check r 2 0 417 end 418 419 fn mlkemt_l2 to void 420 s = new uchar 64 0 421 ek = new uchar 1184 0 422 dk = new uchar 2400 0 423 r = mlkem_keygen_seed part s 0 63 ek dk 424 mlkemt_check r 2 0 425 r = mlkem_keygen_seed s ek dk 426 mlkemt_check r 0 0 427 end 428 429 fn mlkemt_l3 to void 430 dk = new uchar 2400 0 431 ct = new uchar 1088 0 432 ss = new uchar 33 0 433 r = mlkem_decaps dk ct part ss 0 31 434 mlkemt_check r 2 0 435 r = mlkem_decaps dk ct ss 436 mlkemt_check r 2 0 437 end 438 439 fn mlkemt_l to void 440 mlkemt_l1 441 mlkemt_l2 442 mlkemt_l3 443 m = new uchar 32 0 444 ek = new uchar 1184 0 445 ct = new uchar 1089 0 446 ss = new uchar 32 0 447 r = mlkem_encaps ek m part ct 0 1087 ss 448 mlkemt_check r 2 0 449 end 450 451 fn mlkemt_time op view uchar * n ulong dt ulong to void 452 mlkemt_say MTTTIME 453 mlkemt_num get MTCNT 0 454 mlkemt_say op 455 mlkemt_say MTTN 456 mlkemt_num n 457 mlkemt_say MTTNS 458 mlkemt_num dt 459 mlkemt_say MTTNL 460 end 461 462 fn mlkemt_t to void 463 s = new uchar 64 0 464 m = new uchar 32 0 465 ek = new uchar MKEKSZ 0 466 dk = new uchar MKDKSZ 0 467 ct = new uchar MKCTSZ 0 468 ss = new uchar 32 0 469 n = mlkemt_dec 1 470 acc = ulong 0 471 t0 = monons 472 for i ulong n 473 put s 0 uchar and i 255 474 acc = or acc mlkem_keygen_seed s ek dk 475 end 476 mlkemt_time MTTKG n ulong sub monons t0 477 t0 = monons 478 for i ulong n 479 put m 0 uchar and i 255 480 acc = or acc mlkem_encaps ek m ct ss 481 end 482 mlkemt_time MTTEN n ulong sub monons t0 483 t0 = monons 484 for i ulong n 485 put ct 0 uchar and i 255 486 acc = or acc mlkem_decaps dk ct ss 487 end 488 mlkemt_time MTTDE n ulong sub monons t0 489 mlkemt_check acc 0 0 490 end 491 492 fn mlkemt_slurp to void 493 t = ulong 0 494 while true bound 999999 495 room = sub len MTIN t 496 if eq room 0 497 mlkemt_bad 498 end 499 r = read 0 MTIN t room 500 if lt r 0 501 mlkemt_bad 502 end 503 if eq r 0 504 break 505 end 506 t = add t ulong r 507 end 508 put MTLEN 0 t 509 end 510 511 note the case on MTIN[p..e), e at its newline 512 fn mlkemt_line p ulong e ulong to void 513 c = get MTIN p 514 if or eq e p eq c 35 515 ret 516 end 517 mlkemt_bump 1 1 518 if eq c 72 519 mlkemt_h p e 0 520 ret 521 end 522 if eq c 83 523 mlkemt_h p e 1 524 ret 525 end 526 if eq c 75 527 mlkemt_tok p e 5 528 mlkemt_k 529 ret 530 end 531 if eq c 81 532 mlkemt_tok p e 5 533 mlkemt_q 534 ret 535 end 536 if eq c 69 537 mlkemt_tok p e 6 538 mlkemt_e 539 ret 540 end 541 if eq c 68 542 mlkemt_tok p e 5 543 mlkemt_d 544 ret 545 end 546 if eq c 67 547 mlkemt_tok p e 7 548 mlkemt_c 549 ret 550 end 551 if eq c 76 552 mlkemt_tok p e 1 553 mlkemt_l 554 ret 555 end 556 if eq c 84 557 mlkemt_bump 1 0 558 mlkemt_tok p e 2 559 mlkemt_t 560 ret 561 end 562 mlkemt_bad 563 end 564 565 fn main to int 566 mlkemt_slurp 567 tot = get MTLEN 0 568 if or eq tot 0 ne get MTIN sub tot 1 10 569 mlkemt_bad 570 end 571 p = ulong 0 572 while lt p tot bound 999999 573 mlkemt_bump 0 1 574 e = p 575 while ne get MTIN e 10 bound 999999 576 e = add e 1 577 end 578 mlkemt_line p e 579 p = add e 1 580 end 581 mlkemt_say MTTCASE 582 mlkemt_num get MTCNT 1 583 mlkemt_say MTTCALL 584 mlkemt_num get MTCNT 2 585 mlkemt_say MTTMM 586 mlkemt_num get MTCNT 3 587 mlkemt_say MTTNS 588 mlkemt_num get MTCNT 4 589 mlkemt_say MTTNL 590 if or gt get MTCNT 3 0 eq get MTCNT 2 0 591 ret 1 592 end 593 ret 0 594 end