benchmarks-mv
Measurement harnesses and evidence bundles for AIEN on the NVIDIA DGX Spark GB10. Speed is reported against the machine's physical limit and my own previous run. Attachment of AienOS.
Brought by drakestapleton
benchmarks-mv / native / neuralese / nl1r / evidence / boundary-independent-source.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Host synthetic one-shot CLI: receiver-context-file expected-op expected-operand. |
| 3 | note This harness cannot authorize effects or reserve a production microjob. |
| 4 | om2 debug |
| 5 | note Closed compiled ordinary Omega operations, never dynamically execute vector bytes. |
| 6 | fn nlinc x uint to uint |
| 7 | requires le x 16 |
| 8 | ensures le result 32 |
| 9 | ret add x 1 |
| 10 | end |
| 11 | fn nldouble x uint to uint |
| 12 | requires le x 16 |
| 13 | ensures le result 32 |
| 14 | ret mul x 2 |
| 15 | end |
| 16 | fn nlident x uint to uint |
| 17 | requires le x 16 |
| 18 | ensures le result 32 |
| 19 | ret x |
| 20 | end |
| 21 | text nlrhead "NL4R1 0 " |
| 22 | text nlrtail " 1 1 0 1 0 " |
| 23 | text nlrno "NL4R1 " |
| 24 | text nlzeros " 0 0 0 0 0 0 0 0 0\n" |
| 25 | text nlendl "\n" |
| 26 | text nlspace " " |
| 27 | fn nlout b view uchar * fd int to void |
| 28 | pos = ulong 0 |
| 29 | while lt pos len b bound 8192 |
| 30 | r = write fd b pos sub len b pos |
| 31 | if le r 0 |
| 32 | exit 81 |
| 33 | end |
| 34 | pos = add pos ulong r |
| 35 | end |
| 36 | end |
| 37 | fn nlnumber v uint fd int to void |
| 38 | b = new uchar 10 0 |
| 39 | pos = ulong 10 |
| 40 | x = v |
| 41 | while true bound 10 |
| 42 | pos = sub pos 1 |
| 43 | put b pos uchar add rem x 10 48 |
| 44 | x = div x 10 |
| 45 | if eq x 0 |
| 46 | break |
| 47 | end |
| 48 | end |
| 49 | nlout part b pos sub 10 pos fd |
| 50 | end |
| 51 | fn nlfail code int to int |
| 52 | nlout nlrno 2 |
| 53 | nlnumber uint code 2 |
| 54 | nlout nlzeros 2 |
| 55 | ret code |
| 56 | end |
| 57 | fn nlread fd int b edit uchar * to long |
| 58 | pos = ulong 0 |
| 59 | while lt pos len b bound 5121 |
| 60 | r = read fd b pos sub len b pos |
| 61 | if lt r 0 |
| 62 | ret r |
| 63 | end |
| 64 | if eq r 0 |
| 65 | break |
| 66 | end |
| 67 | pos = add pos ulong r |
| 68 | end |
| 69 | ret long pos |
| 70 | end |
| 71 | fn nlcontext c edit uchar 1025 to int |
| 72 | path = new uchar 4096 0 |
| 73 | n = argread 1 path 0 4096 |
| 74 | if le n 0 |
| 75 | ret 80 |
| 76 | end |
| 77 | fd = fopen path 0 ulong n 0 0 |
| 78 | if lt fd 0 |
| 79 | ret 80 |
| 80 | end |
| 81 | r = nlread int fd c |
| 82 | cl = fclose int fd |
| 83 | if or ne r 1024 ne cl 0 |
| 84 | ret 80 |
| 85 | end |
| 86 | ret 0 |
| 87 | end |
| 88 | fn nlrequest e edit uint 16 to int |
| 89 | argbuf = new uchar 2 0 |
| 90 | r = argread 2 argbuf 0 2 |
| 91 | if ne r 1 |
| 92 | ret 80 |
| 93 | end |
| 94 | v = get argbuf 0 |
| 95 | if or lt v 49 gt v 51 |
| 96 | ret 80 |
| 97 | end |
| 98 | put e 0 uint sub v 48 |
| 99 | r = argread 3 argbuf 0 2 |
| 100 | if ne r 1 |
| 101 | ret 80 |
| 102 | end |
| 103 | v = get argbuf 0 |
| 104 | if or lt v 48 gt v 51 |
| 105 | ret 80 |
| 106 | end |
| 107 | put e 1 uint sub v 48 |
| 108 | if eq get e 0 1 |
| 109 | put e 2 1 |
| 110 | end |
| 111 | if eq get e 0 2 |
| 112 | put e 2 2 |
| 113 | end |
| 114 | put e 3 1 |
| 115 | put e 4 1 |
| 116 | put e 5 1 |
| 117 | put e 6 4 |
| 118 | put e 7 1 |
| 119 | put e 8 0 |
| 120 | put e 9 1 |
| 121 | ret 0 |
| 122 | end |
| 123 | fn nlexec p view uint 16 to void |
| 124 | z = uint 0 |
| 125 | if eq get p 0 1 |
| 126 | z = nlinc get p 1 |
| 127 | end |
| 128 | if eq get p 0 2 |
| 129 | z = nldouble get p 1 |
| 130 | end |
| 131 | if eq get p 0 3 |
| 132 | z = nlident get p 1 |
| 133 | end |
| 134 | nlout nlrhead 1 |
| 135 | for i ulong 3 |
| 136 | if ne i 0 |
| 137 | nlout nlspace 1 |
| 138 | end |
| 139 | nlnumber get p i 1 |
| 140 | end |
| 141 | nlout nlrtail 1 |
| 142 | nlnumber z 1 |
| 143 | nlout nlendl 1 |
| 144 | end |
| 145 | note SPDX-License-Identifier: AGPL-3.0-only |
| 146 | note Verification adapter, not a receiver or authority. Reuses unchanged NL4 functions. |
| 147 | note Compose after host-proposal.om lines1..144, before pinned h/proposal/v/sha2. |
| 148 | note CLI context-file predicted-bits trusted-target-bits task abstain frame-output-fd. |
| 149 | note Trusted target is evaluator-owned and never flows back to sender or receiver. |
| 150 | global BARG uchar 5 |
| 151 | global BCFG ulong 5 |
| 152 | fn barg ix ulong to ulong |
| 153 | n = argread ix BARG 0 5 |
| 154 | if or le n 0 gt n 4 |
| 155 | exit 80 |
| 156 | end |
| 157 | v = ulong 0 |
| 158 | for i ulong ulong n |
| 159 | c = get BARG i |
| 160 | if or lt c 48 gt c 57 |
| 161 | exit 80 |
| 162 | end |
| 163 | v = add mul v 10 ulong sub c 48 |
| 164 | end |
| 165 | ret v |
| 166 | end |
| 167 | fn bargs to int |
| 168 | put BCFG 0 barg 2 |
| 169 | put BCFG 1 barg 3 |
| 170 | put BCFG 2 barg 4 |
| 171 | put BCFG 3 barg 6 |
| 172 | put BCFG 4 barg 5 |
| 173 | if or gt get BCFG 0 15 gt get BCFG 1 15 |
| 174 | ret 80 |
| 175 | end |
| 176 | if or gt get BCFG 2 2 gt get BCFG 4 1 |
| 177 | ret 80 |
| 178 | end |
| 179 | if or lt get BCFG 3 3 gt get BCFG 3 1023 |
| 180 | ret 80 |
| 181 | end |
| 182 | if eq get BCFG 2 1 |
| 183 | if or gt get BCFG 0 1 gt get BCFG 1 1 |
| 184 | ret 80 |
| 185 | end |
| 186 | end |
| 187 | if eq get BCFG 4 1 |
| 188 | ret 2 |
| 189 | end |
| 190 | if eq get BCFG 2 1 |
| 191 | put BCFG 2 1 |
| 192 | else |
| 193 | put BCFG 2 2 |
| 194 | end |
| 195 | ret 0 |
| 196 | end |
| 197 | fn bheader frame edit uchar 1056 ctx view uchar 1025 to void |
| 198 | for i ulong 1024 |
| 199 | put frame i get ctx i |
| 200 | end |
| 201 | note Fresh one-shot SYNTHETIC sequence, checked with existing lastseq0 rule. |
| 202 | for i ulong 8 |
| 203 | put frame add 80 i uchar 0 |
| 204 | end |
| 205 | put frame 87 uchar 1 |
| 206 | end |
| 207 | fn bexpect e edit uint 16 value uint to void |
| 208 | put e 0 3 |
| 209 | put e 1 value |
| 210 | put e 3 1 |
| 211 | put e 4 1 |
| 212 | put e 5 1 |
| 213 | put e 6 4 |
| 214 | put e 7 1 |
| 215 | put e 9 1 |
| 216 | end |
| 217 | fn bframe frame edit uchar 1056 value ulong to int |
| 218 | f = new f64 4 0.0f64 |
| 219 | put f 0 1.0f64 |
| 220 | put f 1 1.0f64 |
| 221 | put f 2 f64 and value 1 |
| 222 | put f 3 f64 and shr value 1 1 |
| 223 | wire = new uchar 32 0 |
| 224 | rc = nlencode f wire |
| 225 | if ne rc 0 |
| 226 | ret rc |
| 227 | end |
| 228 | for i ulong 32 |
| 229 | put frame add 1024 i get wire i |
| 230 | end |
| 231 | state = new ulong 40 0 |
| 232 | sha256_init state |
| 233 | sha256_update state part frame 0 992 |
| 234 | sha256_update state part frame 1024 32 |
| 235 | sha256_final state part frame 992 32 |
| 236 | note Raw actual envelope bytes are audit output, not an execution receipt. |
| 237 | z = write int get BCFG 3 frame 0 1056 |
| 238 | if ne z 1056 |
| 239 | ret 81 |
| 240 | end |
| 241 | ret 0 |
| 242 | end |
| 243 | fn bverify frame edit uchar 1056 ctx view uchar 1025 p edit uint 16 to int |
| 244 | value = and shr get BCFG 0 mul get BCFG 4 2 3 |
| 245 | rc = bframe frame value |
| 246 | if ne rc 0 |
| 247 | ret rc |
| 248 | end |
| 249 | now = lpbe64 ctx 64 |
| 250 | rc = lpvalid frame part ctx 0 1024 now 0 |
| 251 | if ne rc 0 |
| 252 | ret rc |
| 253 | end |
| 254 | rc = nlproposal frame p |
| 255 | if ne rc 0 |
| 256 | ret rc |
| 257 | end |
| 258 | e = new uint 16 0 |
| 259 | sh = mul get BCFG 4 2 |
| 260 | want = and shr get BCFG 1 sh 3 |
| 261 | bexpect e uint want |
| 262 | if not nlmatch p e |
| 263 | ret 49 |
| 264 | end |
| 265 | ret 0 |
| 266 | end |
| 267 | fn main to int |
| 268 | if ne argcnt 7 |
| 269 | ret nlfail 80 |
| 270 | end |
| 271 | rc = bargs |
| 272 | if ne rc 0 |
| 273 | ret nlfail rc |
| 274 | end |
| 275 | ctx = new uchar 1025 0 |
| 276 | frame = new uchar 1056 0 |
| 277 | props = new uint 32 0 |
| 278 | rc = nlcontext ctx |
| 279 | if ne rc 0 |
| 280 | ret nlfail rc |
| 281 | end |
| 282 | bheader frame ctx |
| 283 | p = new uint 16 0 |
| 284 | q = new uint 16 0 |
| 285 | for chunk ulong get BCFG 2 |
| 286 | put BCFG 4 chunk |
| 287 | for i ulong 16 |
| 288 | put p i uint 0 |
| 289 | end |
| 290 | rc = bverify frame ctx p |
| 291 | if ne rc 0 |
| 292 | ret nlfail rc |
| 293 | end |
| 294 | for i ulong 16 |
| 295 | put props add mul chunk 16 i get p i |
| 296 | end |
| 297 | end |
| 298 | note Every chunk must verify BEFORE any pure execution or execution receipt. |
| 299 | for chunk ulong get BCFG 2 |
| 300 | for i ulong 16 |
| 301 | put q i get props add mul chunk 16 i |
| 302 | end |
| 303 | nlexec q |
| 304 | end |
| 305 | ret 0 |
| 306 | end |
| 307 | note SPDX-License-Identifier: AGPL-3.0-only |
| 308 | note NL1 G0 fixed eight-coordinate mathematical decoder, not a learned channel. |
| 309 | note Input scores propose information only. No effects, threshold, or authority. |
| 310 | global NM f64 8 |
| 311 | global NF f64 4 |
| 312 | global NT f64 4 |
| 313 | |
| 314 | fn ndcol j ulong to ulong |
| 315 | if eq j 0 |
| 316 | ret 1 |
| 317 | end |
| 318 | if eq j 1 |
| 319 | ret 2 |
| 320 | end |
| 321 | if eq j 2 |
| 322 | ret 4 |
| 323 | end |
| 324 | ret 7 |
| 325 | end |
| 326 | |
| 327 | fn ndsign r ulong c ulong to f64 |
| 328 | x = and r c |
| 329 | p = ulong 0 |
| 330 | for b ulong 3 |
| 331 | p = xor p and shr x b 1 |
| 332 | end |
| 333 | if eq p 0 |
| 334 | ret 1.0f64 |
| 335 | end |
| 336 | ret -1.0f64 |
| 337 | end |
| 338 | |
| 339 | fn ndelem r ulong j ulong to f64 |
| 340 | ret div ndsign r ndcol j fsqrt 8.0f64 |
| 341 | end |
| 342 | |
| 343 | fn nddec n ulong to int |
| 344 | if ne n 8 |
| 345 | ret 1 |
| 346 | end |
| 347 | note Ordered comparison rejects NaN, infinities, and outside the frozen range. |
| 348 | for r ulong 8 |
| 349 | if not le fabs get NM r 4.0f64 |
| 350 | ret 2 |
| 351 | end |
| 352 | end |
| 353 | for j ulong 4 |
| 354 | v = 0.0f64 |
| 355 | for r ulong 8 |
| 356 | v = add v mul get NM r ndelem r j |
| 357 | end |
| 358 | put NT j v |
| 359 | end |
| 360 | for j ulong 4 |
| 361 | put NF j get NT j |
| 362 | end |
| 363 | ret 0 |
| 364 | end |
| 365 | note SPDX-License-Identifier: Apache-2.0 |
| 366 | note NL4 consumes NL3 lpvalid and NL1 nddec; neither is execution authority. |
| 367 | note Caller must run lpvalid against receiver-owned context before this decoder. |
| 368 | note No host words, executable payload, capability, allocation from input or effects. |
| 369 | note Concatenate after an om2 debug entry, NL1 hadamard.om and NL3 validator.om. |
| 370 | fn nlencode f view f64 4 wire edit uchar 32 to int |
| 371 | stage = new uint 8 0 |
| 372 | for i ulong 4 |
| 373 | if not and ge get f i 0.0f64 le get f i 1.0f64 |
| 374 | ret 50 |
| 375 | end |
| 376 | end |
| 377 | for i ulong 8 |
| 378 | v = 0.0f64 |
| 379 | for j ulong 4 |
| 380 | v = add v mul get f j ndelem i j |
| 381 | end |
| 382 | put stage i bitcast uint f32 v |
| 383 | end |
| 384 | for i ulong 8 |
| 385 | u = get stage i |
| 386 | off = mul i 4 |
| 387 | for j ulong 4 |
| 388 | sh = mul sub ulong 3 j 8 |
| 389 | c = uchar and shr u sh 255 |
| 390 | put wire add off j c |
| 391 | end |
| 392 | end |
| 393 | ret 0 |
| 394 | end |
| 395 | fn nlbe32 a view uchar * off ulong to uint |
| 396 | v = uint 0 |
| 397 | for i ulong 4 |
| 398 | v = or shl v 8 uint get a add off i |
| 399 | end |
| 400 | ret v |
| 401 | end |
| 402 | fn nlbit v f64 to int |
| 403 | note Closed synthetic codebook tolerance; ambiguous and nonfinite scores refuse. |
| 404 | if and ge v -0.0625f64 le v 0.0625f64 |
| 405 | ret 0 |
| 406 | end |
| 407 | if and ge v 0.9375f64 le v 1.0625f64 |
| 408 | ret 1 |
| 409 | end |
| 410 | ret -1 |
| 411 | end |
| 412 | fn nlproposal a view uchar * p edit uint 16 to int |
| 413 | note This function proposes only; it does not validate context, reserve or execute. |
| 414 | b = new uint 4 0 |
| 415 | if lt len a 1024 |
| 416 | ret 40 |
| 417 | end |
| 418 | if ne get a 5 1 |
| 419 | ret 41 |
| 420 | end |
| 421 | dtype = get a 10 |
| 422 | width = uint 4 |
| 423 | if eq dtype 2 |
| 424 | width = 1 |
| 425 | else |
| 426 | if ne dtype 1 |
| 427 | ret 41 |
| 428 | end |
| 429 | end |
| 430 | if ne len a add ulong 1024 ulong mul width 8 |
| 431 | ret 40 |
| 432 | end |
| 433 | if or ne get a 11 1 ne nlbe32 a 24 8 |
| 434 | ret 42 |
| 435 | end |
| 436 | if or ne nlbe32 a 16 mul width 8 ne nlbe32 a 20 8 |
| 437 | ret 42 |
| 438 | end |
| 439 | if ne get a 101 0 |
| 440 | ret 43 |
| 441 | end |
| 442 | if lt nlbe32 a 104 1 |
| 443 | ret 44 |
| 444 | end |
| 445 | factor = 1.0f64 |
| 446 | zero = int 0 |
| 447 | if eq dtype 2 |
| 448 | scale = nlbe32 a 56 |
| 449 | zero = bitcast int nlbe32 a 60 |
| 450 | if or eq scale 0 ge scale 0x7f800000 |
| 451 | ret 45 |
| 452 | end |
| 453 | if or lt zero -128 gt zero 127 |
| 454 | ret 45 |
| 455 | end |
| 456 | factor = f64 bitcast f32 scale |
| 457 | end |
| 458 | for i ulong 8 |
| 459 | if eq dtype 1 |
| 460 | u = nlbe32 a add 1024 mul i 4 |
| 461 | note Exponent255 includes every NaN and infinity, without floating comparison. |
| 462 | if eq and shr u 23 255 255 |
| 463 | ret 45 |
| 464 | end |
| 465 | put NM i f64 bitcast f32 u |
| 466 | else |
| 467 | q = int get a add 1024 i |
| 468 | if ge q 128 |
| 469 | q = sub q 256 |
| 470 | end |
| 471 | put NM i mul f64 sub q zero factor |
| 472 | end |
| 473 | end |
| 474 | r = nddec 8 |
| 475 | if ne r 0 |
| 476 | ret 46 |
| 477 | end |
| 478 | for i ulong 4 |
| 479 | v = nlbit get NF i |
| 480 | if lt v 0 |
| 481 | ret 47 |
| 482 | end |
| 483 | put b i uint v |
| 484 | end |
| 485 | op = add get b 0 mul get b 1 2 |
| 486 | if eq op 0 |
| 487 | ret 48 |
| 488 | end |
| 489 | arg = add get b 2 mul get b 3 2 |
| 490 | note All checks precede output writes: refusal preserves the caller's proposal. |
| 491 | for i ulong 16 |
| 492 | put p i 0 |
| 493 | end |
| 494 | put p 0 op |
| 495 | put p 1 arg |
| 496 | if eq op 1 |
| 497 | put p 2 1 |
| 498 | end |
| 499 | if eq op 2 |
| 500 | put p 2 2 |
| 501 | end |
| 502 | put p 3 1 |
| 503 | put p 4 1 |
| 504 | put p 5 1 |
| 505 | put p 6 4 |
| 506 | put p 7 1 |
| 507 | put p 8 0 |
| 508 | put p 9 1 |
| 509 | ret 0 |
| 510 | end |
| 511 | fn nlmatch p view uint 16 e view uint 16 to bool |
| 512 | note Independent receiver expectation, never sourced from the numerical message. |
| 513 | for i ulong 16 |
| 514 | if ne get p i get e i |
| 515 | ret false |
| 516 | end |
| 517 | end |
| 518 | ret true |
| 519 | end |
| 520 | note SPDX-License-Identifier: Apache-2.0 |
| 521 | note LTP0 pure admission-template validator. No main, effects or authority. |
| 522 | note Compose after om2 debug and before pinned om/tls/sha2.om. |
| 523 | fn lpbe64 a view uchar * off ulong to ulong |
| 524 | v = ulong 0 |
| 525 | for i ulong 8 |
| 526 | v = or shl v 8 ulong get a add off i |
| 527 | end |
| 528 | ret v |
| 529 | end |
| 530 | fn lpbe32 a view uchar * off ulong to ulong |
| 531 | v = ulong 0 |
| 532 | for i ulong 4 |
| 533 | v = or shl v 8 ulong get a add off i |
| 534 | end |
| 535 | ret v |
| 536 | end |
| 537 | fn lpeq a view uchar * c view uchar * off ulong n ulong to bool |
| 538 | for i ulong n |
| 539 | p = add off i |
| 540 | if ne get a p get c p |
| 541 | ret false |
| 542 | end |
| 543 | end |
| 544 | ret true |
| 545 | end |
| 546 | fn lpnonzero a view uchar * off ulong n ulong to bool |
| 547 | v = uchar 0 |
| 548 | for i ulong n |
| 549 | v = or v get a add off i |
| 550 | end |
| 551 | ret ne v 0 |
| 552 | end |
| 553 | fn lpenums a view uchar * to bool |
| 554 | if or lt get a 5 1 gt get a 5 2 |
| 555 | ret false |
| 556 | end |
| 557 | if or ne get a 6 1 ne get a 7 1 |
| 558 | ret false |
| 559 | end |
| 560 | if or ne get a 14 1 ne get a 15 0 |
| 561 | ret false |
| 562 | end |
| 563 | if or lt get a 100 1 gt get a 100 3 |
| 564 | ret false |
| 565 | end |
| 566 | if gt get a 101 2 |
| 567 | ret false |
| 568 | end |
| 569 | if or lt get a 102 2 gt get a 102 3 |
| 570 | ret false |
| 571 | end |
| 572 | ret true |
| 573 | end |
| 574 | fn lpquant a view uchar * to bool |
| 575 | scale = lpbe32 a 56 |
| 576 | zero = lpbe32 a 60 |
| 577 | if eq get a 10 1 |
| 578 | if or ne get a 12 1 ne get a 13 0 |
| 579 | ret false |
| 580 | end |
| 581 | ret and eq scale 0x3f800000 eq zero 0 |
| 582 | end |
| 583 | if eq get a 10 2 |
| 584 | if or ne get a 12 5 ne get a 13 1 |
| 585 | ret false |
| 586 | end |
| 587 | if or eq scale 0 ge scale 0x7f800000 |
| 588 | ret false |
| 589 | end |
| 590 | ret or le zero 127 ge zero 0xffffff80 |
| 591 | end |
| 592 | ret false |
| 593 | end |
| 594 | fn lpshape a view uchar * to bool |
| 595 | rank = ulong get a 11 |
| 596 | srank = ulong get a 103 |
| 597 | if or lt rank 1 gt rank 4 |
| 598 | ret false |
| 599 | end |
| 600 | if or lt srank 1 gt srank 4 |
| 601 | ret false |
| 602 | end |
| 603 | for i ulong 4 |
| 604 | sdim = lpbe32 a add 112 mul i 4 |
| 605 | if lt i srank |
| 606 | if eq sdim 0 |
| 607 | ret false |
| 608 | end |
| 609 | else |
| 610 | if ne sdim 0 |
| 611 | ret false |
| 612 | end |
| 613 | end |
| 614 | end |
| 615 | width = ulong 1 |
| 616 | if eq get a 10 1 |
| 617 | width = 4 |
| 618 | end |
| 619 | prod = ulong 1 |
| 620 | stride = width |
| 621 | for k ulong 4 |
| 622 | i = sub 3 k |
| 623 | dim = lpbe32 a add 24 mul i 4 |
| 624 | st = lpbe32 a add 40 mul i 4 |
| 625 | if ge i rank |
| 626 | if or ne dim 0 ne st 0 |
| 627 | ret false |
| 628 | end |
| 629 | else |
| 630 | if or eq dim 0 gt dim 4096 |
| 631 | ret false |
| 632 | end |
| 633 | if gt prod div ulong 4096 dim |
| 634 | ret false |
| 635 | end |
| 636 | if ne st stride |
| 637 | ret false |
| 638 | end |
| 639 | prod = mul prod dim |
| 640 | stride = mul prod width |
| 641 | end |
| 642 | end |
| 643 | ret eq prod lpbe32 a 20 |
| 644 | end |
| 645 | fn lpsize a view uchar * to bool |
| 646 | n = lpbe32 a 16 |
| 647 | cap = lpbe32 a 108 |
| 648 | if or eq n 0 gt n 4096 |
| 649 | ret false |
| 650 | end |
| 651 | if or eq cap 0 gt cap 4096 |
| 652 | ret false |
| 653 | end |
| 654 | if gt n cap |
| 655 | ret false |
| 656 | end |
| 657 | if ne len a add 1024 n |
| 658 | ret false |
| 659 | end |
| 660 | width = ulong 1 |
| 661 | if eq get a 10 1 |
| 662 | width = 4 |
| 663 | end |
| 664 | ret eq n mul lpbe32 a 20 width |
| 665 | end |
| 666 | fn lpids a view uchar * to bool |
| 667 | if or eq lpbe32 a 88 0 eq lpbe32 a 96 0 |
| 668 | ret false |
| 669 | end |
| 670 | if eq lpbe32 a 104 0 |
| 671 | ret false |
| 672 | end |
| 673 | for i ulong 2 |
| 674 | if not lpnonzero a add 128 mul i 16 16 |
| 675 | ret false |
| 676 | end |
| 677 | end |
| 678 | for i ulong 18 |
| 679 | if not lpnonzero a add 160 mul i 32 32 |
| 680 | ret false |
| 681 | end |
| 682 | end |
| 683 | for i ulong 2 |
| 684 | if not lpnonzero a add 736 mul i 16 16 |
| 685 | ret false |
| 686 | end |
| 687 | end |
| 688 | for i ulong 3 |
| 689 | if not lpnonzero a add 768 mul i 32 32 |
| 690 | ret false |
| 691 | end |
| 692 | end |
| 693 | ret true |
| 694 | end |
| 695 | fn lpfinite a view uchar * to bool |
| 696 | if eq get a 10 1 |
| 697 | n = lpbe32 a 20 |
| 698 | for i ulong n |
| 699 | bits = lpbe32 a add 1024 mul i 4 |
| 700 | if eq and bits 0x7f800000 0x7f800000 |
| 701 | ret false |
| 702 | end |
| 703 | end |
| 704 | end |
| 705 | ret true |
| 706 | end |
| 707 | fn lphash a view uchar * to bool |
| 708 | state = new ulong 40 0 |
| 709 | digest = new uchar 32 0 |
| 710 | sha256_init state |
| 711 | sha256_update state part a 0 992 |
| 712 | sha256_update state part a 1024 sub len a 1024 |
| 713 | sha256_final state digest |
| 714 | for i ulong 32 |
| 715 | if ne get digest i get a add 992 i |
| 716 | ret false |
| 717 | end |
| 718 | end |
| 719 | ret true |
| 720 | end |
| 721 | fn lpvalid a view uchar * c view uchar * now ulong lastseq ulong to int |
| 722 | if or lt len a 1024 gt len a 5120 |
| 723 | ret 10 |
| 724 | end |
| 725 | if ne len c 1024 |
| 726 | ret 10 |
| 727 | end |
| 728 | if ne lpbe32 a 0 0x4c545030 |
| 729 | ret 11 |
| 730 | end |
| 731 | if or ne get a 4 0 ne get a 8 4 |
| 732 | ret 11 |
| 733 | end |
| 734 | if ne get a 9 0 |
| 735 | ret 11 |
| 736 | end |
| 737 | if not lpenums a |
| 738 | ret 12 |
| 739 | end |
| 740 | if not lpquant a |
| 741 | ret 13 |
| 742 | end |
| 743 | if not lpshape a |
| 744 | ret 14 |
| 745 | end |
| 746 | if not lpsize a |
| 747 | ret 15 |
| 748 | end |
| 749 | for i ulong 128 |
| 750 | if ne get a add 864 i 0 |
| 751 | ret 16 |
| 752 | end |
| 753 | end |
| 754 | if not lpids a |
| 755 | ret 17 |
| 756 | end |
| 757 | issued = lpbe64 a 64 |
| 758 | expiry = lpbe64 a 72 |
| 759 | if or eq issued 0 gt issued now |
| 760 | ret 18 |
| 761 | end |
| 762 | if or le expiry now le expiry issued |
| 763 | ret 18 |
| 764 | end |
| 765 | if gt expiry lpbe64 c 72 |
| 766 | ret 18 |
| 767 | end |
| 768 | if le lpbe64 a 80 lastseq |
| 769 | ret 19 |
| 770 | end |
| 771 | if not lpeq a c 0 16 |
| 772 | ret 20 |
| 773 | end |
| 774 | if not lpeq a c 24 40 |
| 775 | ret 20 |
| 776 | end |
| 777 | if not lpeq a c 88 776 |
| 778 | ret 20 |
| 779 | end |
| 780 | if not lpfinite a |
| 781 | ret 21 |
| 782 | end |
| 783 | if not lphash a |
| 784 | ret 22 |
| 785 | end |
| 786 | ret 0 |
| 787 | end |
| 788 | note SPDX-License-Identifier: Apache-2.0 |
| 789 | note om/tls/sha2.om: SHA-256, SHA-384, SHA-512 (FIPS 180-4) streaming, as an OM-2 library unit |
| 790 | note (no om2 line, no main, no globals). Module code SH; functions sha2_*, sha256_*, sha384_*, sha512_*. |
| 791 | note |
| 792 | note API |
| 793 | note sha256_init c edit ulong * c: 40 ulong cells (caller owns all state) |
| 794 | note sha256_update c edit ulong * m view uchar * any length, any chunking |
| 795 | note sha256_final c edit ulong * o edit uchar * writes 32 bytes (o shorter traps 3) |
| 796 | note sha384_* / sha512_* the same; final writes 48 / 64 bytes |
| 797 | note sha2_copy d edit ulong * s view ulong * copies len s cells (a context snapshot) |
| 798 | note sha2_digest alg ulong m view uchar * o edit uchar * to ulong |
| 799 | note alg 256|384|512: one-shot digest into o, returns 32|48|64; any other alg returns 0, writes nothing |
| 800 | note After final the context is spent (its state cells hold the last chaining value); init it again. |
| 801 | note A context initialised for one algorithm must be used with that algorithm's update/final; the |
| 802 | note common code reads the algorithm from cell 9, so sha256_update on a sha512 context hashes SHA-512. |
| 803 | note |
| 804 | note Context cells: 0..7 chaining value H (32-bit words zero-extended for SHA-256), 8 = bytes absorbed |
| 805 | note mod 2^64, 9 = algorithm (256, 384, 512), 16..31 = pending block bytes packed big-endian (byte i |
| 806 | note in cell 16 + i/8, bits 56 - 8*(i mod 8)), 10..15 and 32..39 = 0. |
| 807 | note Constant time: every branch, loop count and index depends only on lengths and the algorithm, |
| 808 | note never on message or state bytes. Local schedule/state/padding arrays are zeroed before return |
| 809 | note (HMAC keys pass through them). omc emits plain AArch64; MUL/ADD/EOR/ROR/shift data-independent. |
| 810 | note Message length: any length below 2^64 bytes; update cuts its input into runs of 2^25 bytes so no |
| 811 | note loop passes 2^20 times (a rolled for traps 4 after 10^6 passes; the while bound 999999 of runs |
| 812 | note allows inputs up to 999999 * 32 MiB per update call). |
| 813 | |
| 814 | note K{256}[0..64): first 32 bits of the fractional parts of the cube roots of the first 64 primes |
| 815 | note (FIPS 180-4 4.2.2). Same values as om/sha256/sha256-om2.om; recomputed with Wolfram: |
| 816 | note IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^32],16,8] for i = 1..64. |
| 817 | fn sha2_k256 k edit uint 64 to void |
| 818 | put k 0 0x428a2f98 |
| 819 | put k 1 0x71374491 |
| 820 | put k 2 0xb5c0fbcf |
| 821 | put k 3 0xe9b5dba5 |
| 822 | put k 4 0x3956c25b |
| 823 | put k 5 0x59f111f1 |
| 824 | put k 6 0x923f82a4 |
| 825 | put k 7 0xab1c5ed5 |
| 826 | put k 8 0xd807aa98 |
| 827 | put k 9 0x12835b01 |
| 828 | put k 10 0x243185be |
| 829 | put k 11 0x550c7dc3 |
| 830 | put k 12 0x72be5d74 |
| 831 | put k 13 0x80deb1fe |
| 832 | put k 14 0x9bdc06a7 |
| 833 | put k 15 0xc19bf174 |
| 834 | put k 16 0xe49b69c1 |
| 835 | put k 17 0xefbe4786 |
| 836 | put k 18 0x0fc19dc6 |
| 837 | put k 19 0x240ca1cc |
| 838 | put k 20 0x2de92c6f |
| 839 | put k 21 0x4a7484aa |
| 840 | put k 22 0x5cb0a9dc |
| 841 | put k 23 0x76f988da |
| 842 | put k 24 0x983e5152 |
| 843 | put k 25 0xa831c66d |
| 844 | put k 26 0xb00327c8 |
| 845 | put k 27 0xbf597fc7 |
| 846 | put k 28 0xc6e00bf3 |
| 847 | put k 29 0xd5a79147 |
| 848 | put k 30 0x06ca6351 |
| 849 | put k 31 0x14292967 |
| 850 | put k 32 0x27b70a85 |
| 851 | put k 33 0x2e1b2138 |
| 852 | put k 34 0x4d2c6dfc |
| 853 | put k 35 0x53380d13 |
| 854 | put k 36 0x650a7354 |
| 855 | put k 37 0x766a0abb |
| 856 | put k 38 0x81c2c92e |
| 857 | put k 39 0x92722c85 |
| 858 | put k 40 0xa2bfe8a1 |
| 859 | put k 41 0xa81a664b |
| 860 | put k 42 0xc24b8b70 |
| 861 | put k 43 0xc76c51a3 |
| 862 | put k 44 0xd192e819 |
| 863 | put k 45 0xd6990624 |
| 864 | put k 46 0xf40e3585 |
| 865 | put k 47 0x106aa070 |
| 866 | put k 48 0x19a4c116 |
| 867 | put k 49 0x1e376c08 |
| 868 | put k 50 0x2748774c |
| 869 | put k 51 0x34b0bcb5 |
| 870 | put k 52 0x391c0cb3 |
| 871 | put k 53 0x4ed8aa4a |
| 872 | put k 54 0x5b9cca4f |
| 873 | put k 55 0x682e6ff3 |
| 874 | put k 56 0x748f82ee |
| 875 | put k 57 0x78a5636f |
| 876 | put k 58 0x84c87814 |
| 877 | put k 59 0x8cc70208 |
| 878 | put k 60 0x90befffa |
| 879 | put k 61 0xa4506ceb |
| 880 | put k 62 0xbef9a3f7 |
| 881 | put k 63 0xc67178f2 |
| 882 | end |
| 883 | |
| 884 | note K{512}[0..80): first 64 bits of the fractional parts of the cube roots of the first 80 primes |
| 885 | note (FIPS 180-4 4.2.3). Wolfram: IntegerString[Floor[FractionalPart[N[CubeRoot[Prime[i]],60]]*2^64],16,16]; |
| 886 | note equal to the decimal table of omnet om/sha512/sha512.om. |
| 887 | fn sha2_k512 k edit ulong 80 to void |
| 888 | put k 0 0x428a2f98d728ae22 |
| 889 | put k 1 0x7137449123ef65cd |
| 890 | put k 2 0xb5c0fbcfec4d3b2f |
| 891 | put k 3 0xe9b5dba58189dbbc |
| 892 | put k 4 0x3956c25bf348b538 |
| 893 | put k 5 0x59f111f1b605d019 |
| 894 | put k 6 0x923f82a4af194f9b |
| 895 | put k 7 0xab1c5ed5da6d8118 |
| 896 | put k 8 0xd807aa98a3030242 |
| 897 | put k 9 0x12835b0145706fbe |
| 898 | put k 10 0x243185be4ee4b28c |
| 899 | put k 11 0x550c7dc3d5ffb4e2 |
| 900 | put k 12 0x72be5d74f27b896f |
| 901 | put k 13 0x80deb1fe3b1696b1 |
| 902 | put k 14 0x9bdc06a725c71235 |
| 903 | put k 15 0xc19bf174cf692694 |
| 904 | put k 16 0xe49b69c19ef14ad2 |
| 905 | put k 17 0xefbe4786384f25e3 |
| 906 | put k 18 0x0fc19dc68b8cd5b5 |
| 907 | put k 19 0x240ca1cc77ac9c65 |
| 908 | put k 20 0x2de92c6f592b0275 |
| 909 | put k 21 0x4a7484aa6ea6e483 |
| 910 | put k 22 0x5cb0a9dcbd41fbd4 |
| 911 | put k 23 0x76f988da831153b5 |
| 912 | put k 24 0x983e5152ee66dfab |
| 913 | put k 25 0xa831c66d2db43210 |
| 914 | put k 26 0xb00327c898fb213f |
| 915 | put k 27 0xbf597fc7beef0ee4 |
| 916 | put k 28 0xc6e00bf33da88fc2 |
| 917 | put k 29 0xd5a79147930aa725 |
| 918 | put k 30 0x06ca6351e003826f |
| 919 | put k 31 0x142929670a0e6e70 |
| 920 | put k 32 0x27b70a8546d22ffc |
| 921 | put k 33 0x2e1b21385c26c926 |
| 922 | put k 34 0x4d2c6dfc5ac42aed |
| 923 | put k 35 0x53380d139d95b3df |
| 924 | put k 36 0x650a73548baf63de |
| 925 | put k 37 0x766a0abb3c77b2a8 |
| 926 | put k 38 0x81c2c92e47edaee6 |
| 927 | put k 39 0x92722c851482353b |
| 928 | put k 40 0xa2bfe8a14cf10364 |
| 929 | put k 41 0xa81a664bbc423001 |
| 930 | put k 42 0xc24b8b70d0f89791 |
| 931 | put k 43 0xc76c51a30654be30 |
| 932 | put k 44 0xd192e819d6ef5218 |
| 933 | put k 45 0xd69906245565a910 |
| 934 | put k 46 0xf40e35855771202a |
| 935 | put k 47 0x106aa07032bbd1b8 |
| 936 | put k 48 0x19a4c116b8d2d0c8 |
| 937 | put k 49 0x1e376c085141ab53 |
| 938 | put k 50 0x2748774cdf8eeb99 |
| 939 | put k 51 0x34b0bcb5e19b48a8 |
| 940 | put k 52 0x391c0cb3c5c95a63 |
| 941 | put k 53 0x4ed8aa4ae3418acb |
| 942 | put k 54 0x5b9cca4f7763e373 |
| 943 | put k 55 0x682e6ff3d6b2b8a3 |
| 944 | put k 56 0x748f82ee5defb2fc |
| 945 | put k 57 0x78a5636f43172f60 |
| 946 | put k 58 0x84c87814a1f0ab72 |
| 947 | put k 59 0x8cc702081a6439ec |
| 948 | put k 60 0x90befffa23631e28 |
| 949 | put k 61 0xa4506cebde82bde9 |
| 950 | put k 62 0xbef9a3f7b2c67915 |
| 951 | put k 63 0xc67178f2e372532b |
| 952 | put k 64 0xca273eceea26619c |
| 953 | put k 65 0xd186b8c721c0c207 |
| 954 | put k 66 0xeada7dd6cde0eb1e |
| 955 | put k 67 0xf57d4f7fee6ed178 |
| 956 | put k 68 0x06f067aa72176fba |
| 957 | put k 69 0x0a637dc5a2c898a6 |
| 958 | put k 70 0x113f9804bef90dae |
| 959 | put k 71 0x1b710b35131c471b |
| 960 | put k 72 0x28db77f523047d84 |
| 961 | put k 73 0x32caab7b40c72493 |
| 962 | put k 74 0x3c9ebe0a15c9bebc |
| 963 | put k 75 0x431d67c49c100d4c |
| 964 | put k 76 0x4cc5d4becb3e42b6 |
| 965 | put k 77 0x597f299cfc657e2a |
| 966 | put k 78 0x5fcb6fab3ad6faec |
| 967 | put k 79 0x6c44198c4a475817 |
| 968 | end |
| 969 | |
| 970 | note H(0) (FIPS 180-4 5.3.3 SHA-256, 5.3.4 SHA-384, 5.3.5 SHA-512). Wolfram: fractional parts of |
| 971 | note sqrt(Prime[i]) * 2^32 (i 1..8), * 2^64 (i 1..8 for SHA-512, i 9..16 for SHA-384). |
| 972 | note Any other alg traps 9 (caller contract). |
| 973 | fn sha2_init c edit ulong * alg ulong to void |
| 974 | for i ulong 40 |
| 975 | put c i 0 |
| 976 | end |
| 977 | put c 9 alg |
| 978 | if eq alg 256 |
| 979 | put c 0 0x6a09e667 |
| 980 | put c 1 0xbb67ae85 |
| 981 | put c 2 0x3c6ef372 |
| 982 | put c 3 0xa54ff53a |
| 983 | put c 4 0x510e527f |
| 984 | put c 5 0x9b05688c |
| 985 | put c 6 0x1f83d9ab |
| 986 | put c 7 0x5be0cd19 |
| 987 | ret |
| 988 | end |
| 989 | if eq alg 384 |
| 990 | put c 0 0xcbbb9d5dc1059ed8 |
| 991 | put c 1 0x629a292a367cd507 |
| 992 | put c 2 0x9159015a3070dd17 |
| 993 | put c 3 0x152fecd8f70e5939 |
| 994 | put c 4 0x67332667ffc00b31 |
| 995 | put c 5 0x8eb44a8768581511 |
| 996 | put c 6 0xdb0c2e0d64f98fa7 |
| 997 | put c 7 0x47b5481dbefa4fa4 |
| 998 | ret |
| 999 | end |
| 1000 | if eq alg 512 |
| 1001 | put c 0 0x6a09e667f3bcc908 |
| 1002 | put c 1 0xbb67ae8584caa73b |
| 1003 | put c 2 0x3c6ef372fe94f82b |
| 1004 | put c 3 0xa54ff53a5f1d36f1 |
| 1005 | put c 4 0x510e527fade682d1 |
| 1006 | put c 5 0x9b05688c2b3e6c1f |
| 1007 | put c 6 0x1f83d9abfb41bd6b |
| 1008 | put c 7 0x5be0cd19137e2179 |
| 1009 | ret |
| 1010 | end |
| 1011 | trap 9 |
| 1012 | end |
| 1013 | |
| 1014 | note SHA-256 message schedule (FIPS 180-4 6.2.2 step 1) of the block m[o .. o+64). |
| 1015 | fn sha2_s256 w edit uint 64 m view uchar * o ulong to void |
| 1016 | q = ulong 0 |
| 1017 | x = uint 0 |
| 1018 | y = uint 0 |
| 1019 | for i ulong 16 unroll |
| 1020 | q = add o mul i 4 |
| 1021 | x = uint get m q |
| 1022 | x = or shl x 8 uint get m add q 1 |
| 1023 | x = or shl x 8 uint get m add q 2 |
| 1024 | put w i or shl x 8 uint get m add q 3 |
| 1025 | end |
| 1026 | for i ulong 48 unroll |
| 1027 | x = get w add i 1 |
| 1028 | y = get w add i 14 |
| 1029 | x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 1030 | put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 1031 | end |
| 1032 | end |
| 1033 | |
| 1034 | note SHA-256 rounds (6.2.2 steps 2-4). Ch(e,f,g) = g xor (e and (f xor g)); Maj(a,b,c) = |
| 1035 | note (a and b) or (c and (a or b)); Sigma1 = ROTR 6,11,25; Sigma0 = ROTR 2,13,22 (4.1.2). |
| 1036 | fn sha2_r256 s edit uint 8 w view uint 64 kt view uint 64 to void |
| 1037 | a = get s 0 |
| 1038 | b = get s 1 |
| 1039 | c = get s 2 |
| 1040 | d = get s 3 |
| 1041 | e = get s 4 |
| 1042 | f = get s 5 |
| 1043 | g = get s 6 |
| 1044 | h = get s 7 |
| 1045 | t = uint 0 |
| 1046 | for i ulong 64 unroll |
| 1047 | h = add h get kt i |
| 1048 | h = add h get w i |
| 1049 | t = xor rotr e 6 rotr e 11 |
| 1050 | t = xor t rotr e 25 |
| 1051 | h = add h t |
| 1052 | h = add h xor g and e xor f g |
| 1053 | d = add d h |
| 1054 | t = xor rotr a 2 rotr a 13 |
| 1055 | t = xor t rotr a 22 |
| 1056 | h = add h t |
| 1057 | t = and c or a b |
| 1058 | h = add h or and a b t |
| 1059 | t = h |
| 1060 | h = g |
| 1061 | g = f |
| 1062 | f = e |
| 1063 | e = d |
| 1064 | d = c |
| 1065 | c = b |
| 1066 | b = a |
| 1067 | a = t |
| 1068 | end |
| 1069 | put s 0 add get s 0 a |
| 1070 | put s 1 add get s 1 b |
| 1071 | put s 2 add get s 2 c |
| 1072 | put s 3 add get s 3 d |
| 1073 | put s 4 add get s 4 e |
| 1074 | put s 5 add get s 5 f |
| 1075 | put s 6 add get s 6 g |
| 1076 | put s 7 add get s 7 h |
| 1077 | end |
| 1078 | |
| 1079 | note the whole 64-byte blocks of m (len m a multiple of 64, at most 2^25) into c's chaining value |
| 1080 | fn sha2_b256 c edit ulong * m view uchar * to void |
| 1081 | s = new uint 8 0 |
| 1082 | w = new uint 64 0 |
| 1083 | kt = new uint 64 0 |
| 1084 | sha2_k256 kt |
| 1085 | for i ulong 8 unroll |
| 1086 | put s i uint get c i |
| 1087 | end |
| 1088 | for j ulong shr len m 6 |
| 1089 | sha2_s256 w m shl j 6 |
| 1090 | sha2_r256 s w kt |
| 1091 | end |
| 1092 | for i ulong 8 unroll |
| 1093 | put c i ulong get s i |
| 1094 | put s i 0 |
| 1095 | end |
| 1096 | for i ulong 64 |
| 1097 | put w i 0 |
| 1098 | end |
| 1099 | end |
| 1100 | |
| 1101 | note SHA-512 message schedule (6.4.2 step 1) of the block m[o .. o+128); sigma0 = ROTR 1,8 SHR 7, |
| 1102 | note sigma1 = ROTR 19,61 SHR 6 (4.1.3). |
| 1103 | fn sha2_s512 w edit ulong 80 m view uchar * o ulong to void |
| 1104 | q = ulong 0 |
| 1105 | x = ulong 0 |
| 1106 | y = ulong 0 |
| 1107 | for i ulong 16 unroll |
| 1108 | q = add o mul i 8 |
| 1109 | x = ulong get m q |
| 1110 | x = or shl x 8 ulong get m add q 1 |
| 1111 | x = or shl x 8 ulong get m add q 2 |
| 1112 | x = or shl x 8 ulong get m add q 3 |
| 1113 | x = or shl x 8 ulong get m add q 4 |
| 1114 | x = or shl x 8 ulong get m add q 5 |
| 1115 | x = or shl x 8 ulong get m add q 6 |
| 1116 | put w i or shl x 8 ulong get m add q 7 |
| 1117 | end |
| 1118 | for i ulong 64 unroll |
| 1119 | x = get w add i 1 |
| 1120 | y = get w add i 14 |
| 1121 | x = add add get w i get w add i 9 xor xor rotr x 1 rotr x 8 shr x 7 |
| 1122 | put w add i 16 add x xor xor rotr y 19 rotr y 61 shr y 6 |
| 1123 | end |
| 1124 | end |
| 1125 | |
| 1126 | note SHA-512 rounds (6.4.2 steps 2-4), a rolled loop (unroll allows at most 64 copies); Sigma1 = ROTR 14,18,41; Sigma0 = ROTR 28,34,39 (4.1.3). |
| 1127 | fn sha2_r512 s edit ulong 8 w view ulong 80 kt view ulong 80 to void |
| 1128 | a = get s 0 |
| 1129 | b = get s 1 |
| 1130 | c = get s 2 |
| 1131 | d = get s 3 |
| 1132 | e = get s 4 |
| 1133 | f = get s 5 |
| 1134 | g = get s 6 |
| 1135 | h = get s 7 |
| 1136 | t = ulong 0 |
| 1137 | for i ulong 80 |
| 1138 | h = add h get kt i |
| 1139 | h = add h get w i |
| 1140 | t = xor rotr e 14 rotr e 18 |
| 1141 | t = xor t rotr e 41 |
| 1142 | h = add h t |
| 1143 | h = add h xor g and e xor f g |
| 1144 | d = add d h |
| 1145 | t = xor rotr a 28 rotr a 34 |
| 1146 | t = xor t rotr a 39 |
| 1147 | h = add h t |
| 1148 | t = and c or a b |
| 1149 | h = add h or and a b t |
| 1150 | t = h |
| 1151 | h = g |
| 1152 | g = f |
| 1153 | f = e |
| 1154 | e = d |
| 1155 | d = c |
| 1156 | c = b |
| 1157 | b = a |
| 1158 | a = t |
| 1159 | end |
| 1160 | put s 0 add get s 0 a |
| 1161 | put s 1 add get s 1 b |
| 1162 | put s 2 add get s 2 c |
| 1163 | put s 3 add get s 3 d |
| 1164 | put s 4 add get s 4 e |
| 1165 | put s 5 add get s 5 f |
| 1166 | put s 6 add get s 6 g |
| 1167 | put s 7 add get s 7 h |
| 1168 | end |
| 1169 | |
| 1170 | note the whole 128-byte blocks of m (len m a multiple of 128, at most 2^25) into c |
| 1171 | fn sha2_b512 c edit ulong * m view uchar * to void |
| 1172 | s = new ulong 8 0 |
| 1173 | w = new ulong 80 0 |
| 1174 | kt = new ulong 80 0 |
| 1175 | sha2_k512 kt |
| 1176 | for i ulong 8 unroll |
| 1177 | put s i get c i |
| 1178 | end |
| 1179 | for j ulong shr len m 7 |
| 1180 | sha2_s512 w m shl j 7 |
| 1181 | sha2_r512 s w kt |
| 1182 | end |
| 1183 | for i ulong 8 unroll |
| 1184 | put c i get s i |
| 1185 | put s i 0 |
| 1186 | end |
| 1187 | for i ulong 80 |
| 1188 | put w i 0 |
| 1189 | end |
| 1190 | end |
| 1191 | |
| 1192 | note whole blocks of m (len m a multiple of the block size) in runs of at most 2^25 bytes |
| 1193 | fn sha2_run c edit ulong * m view uchar * to void |
| 1194 | n = len m |
| 1195 | q = ulong 0 |
| 1196 | k = ulong 0 |
| 1197 | while lt q n bound 999999 |
| 1198 | k = sub n q |
| 1199 | if gt k 0x2000000 |
| 1200 | k = 0x2000000 |
| 1201 | end |
| 1202 | if eq get c 9 256 |
| 1203 | sha2_b256 c part m q k |
| 1204 | else |
| 1205 | sha2_b512 c part m q k |
| 1206 | end |
| 1207 | q = add q k |
| 1208 | end |
| 1209 | end |
| 1210 | |
| 1211 | note pending byte i (i < 128) of the packed buffer |
| 1212 | fn sha2_bget c view ulong * i ulong to uchar |
| 1213 | x = shr get c add 16 shr i 3 sub 56 shl and i 7 3 |
| 1214 | ret uchar and x 0xff |
| 1215 | end |
| 1216 | |
| 1217 | note set pending byte i of the packed buffer to v |
| 1218 | fn sha2_bput c edit ulong * i ulong v uchar to void |
| 1219 | q = add 16 shr i 3 |
| 1220 | sh = sub 56 shl and i 7 3 |
| 1221 | x = and get c q not shl ulong 0xff sh |
| 1222 | put c q or x shl ulong v sh |
| 1223 | end |
| 1224 | |
| 1225 | note the bytes m[0..k) into the buffer at bi (bi + k <= block size) |
| 1226 | fn sha2_fill c edit ulong * bi ulong m view uchar * k ulong to void |
| 1227 | for i ulong k |
| 1228 | sha2_bput c add bi i get m i |
| 1229 | end |
| 1230 | end |
| 1231 | |
| 1232 | fn sha2_update c edit ulong * m view uchar * to void |
| 1233 | blk = new uchar 128 0 |
| 1234 | n = len m |
| 1235 | cnt = get c 8 |
| 1236 | bs = ulong 128 |
| 1237 | if eq get c 9 256 |
| 1238 | bs = 64 |
| 1239 | end |
| 1240 | bi = and cnt sub bs 1 |
| 1241 | put c 8 add cnt n |
| 1242 | off = ulong 0 |
| 1243 | if gt bi 0 |
| 1244 | off = sub bs bi |
| 1245 | if lt n off |
| 1246 | off = n |
| 1247 | end |
| 1248 | sha2_fill c bi m off |
| 1249 | if eq add bi off bs |
| 1250 | for i ulong bs |
| 1251 | put blk i sha2_bget c i |
| 1252 | end |
| 1253 | sha2_run c part blk 0 bs |
| 1254 | for i ulong bs |
| 1255 | put blk i 0 |
| 1256 | end |
| 1257 | end |
| 1258 | end |
| 1259 | nb = and sub n off not sub bs 1 |
| 1260 | sha2_run c part m off nb |
| 1261 | off = add off nb |
| 1262 | sha2_fill c 0 part m off sub n off sub n off |
| 1263 | end |
| 1264 | |
| 1265 | note H(N) as big-endian bytes: 32 (SHA-256, 8 words of 32 bits), 48 (SHA-384, first 6 words), 64 |
| 1266 | fn sha2_put c view ulong * o edit uchar * to ulong |
| 1267 | alg = get c 9 |
| 1268 | sh = ulong 0 |
| 1269 | if eq alg 256 |
| 1270 | for i ulong 32 |
| 1271 | sh = sub 24 shl and i 3 3 |
| 1272 | put o i uchar and shr get c shr i 2 sh 0xff |
| 1273 | end |
| 1274 | ret 32 |
| 1275 | end |
| 1276 | dl = ulong 64 |
| 1277 | if eq alg 384 |
| 1278 | dl = 48 |
| 1279 | end |
| 1280 | for i ulong dl |
| 1281 | sh = sub 56 shl and i 7 3 |
| 1282 | put o i uchar and shr get c shr i 3 sh 0xff |
| 1283 | end |
| 1284 | ret dl |
| 1285 | end |
| 1286 | |
| 1287 | note padding (FIPS 180-4 5.1.1 / 5.1.2): 0x80, zeros, the bit length big-endian in the last 8 (SHA-256) |
| 1288 | note or 16 (SHA-384/512) bytes of one or two blocks; returns the digest length |
| 1289 | fn sha2_fin c edit ulong * o edit uchar * to ulong |
| 1290 | p = new uchar 256 0 |
| 1291 | bs = ulong 128 |
| 1292 | ls = ulong 16 |
| 1293 | if eq get c 9 256 |
| 1294 | bs = 64 |
| 1295 | ls = 8 |
| 1296 | end |
| 1297 | cnt = get c 8 |
| 1298 | k = and cnt sub bs 1 |
| 1299 | for i ulong k |
| 1300 | put p i sha2_bget c i |
| 1301 | end |
| 1302 | put p k 0x80 |
| 1303 | e = bs |
| 1304 | if ge add k ls bs |
| 1305 | e = shl bs 1 |
| 1306 | end |
| 1307 | x = shl cnt 3 |
| 1308 | for i ulong 8 unroll |
| 1309 | put p sub e add i 1 uchar and shr x mul i 8 0xff |
| 1310 | end |
| 1311 | if eq ls 16 |
| 1312 | put p sub e 9 uchar shr cnt 61 |
| 1313 | end |
| 1314 | sha2_run c part p 0 e |
| 1315 | for i ulong 256 |
| 1316 | put p i 0 |
| 1317 | end |
| 1318 | ret sha2_put c o |
| 1319 | end |
| 1320 | |
| 1321 | fn sha256_init c edit ulong * to void |
| 1322 | sha2_init c 256 |
| 1323 | end |
| 1324 | |
| 1325 | fn sha384_init c edit ulong * to void |
| 1326 | sha2_init c 384 |
| 1327 | end |
| 1328 | |
| 1329 | fn sha512_init c edit ulong * to void |
| 1330 | sha2_init c 512 |
| 1331 | end |
| 1332 | |
| 1333 | fn sha256_update c edit ulong * m view uchar * to void |
| 1334 | sha2_update c m |
| 1335 | end |
| 1336 | |
| 1337 | fn sha384_update c edit ulong * m view uchar * to void |
| 1338 | sha2_update c m |
| 1339 | end |
| 1340 | |
| 1341 | fn sha512_update c edit ulong * m view uchar * to void |
| 1342 | sha2_update c m |
| 1343 | end |
| 1344 | |
| 1345 | fn sha256_final c edit ulong * o edit uchar * to void |
| 1346 | dl = sha2_fin c o |
| 1347 | end |
| 1348 | |
| 1349 | fn sha384_final c edit ulong * o edit uchar * to void |
| 1350 | dl = sha2_fin c o |
| 1351 | end |
| 1352 | |
| 1353 | fn sha512_final c edit ulong * o edit uchar * to void |
| 1354 | dl = sha2_fin c o |
| 1355 | end |
| 1356 | |
| 1357 | fn sha2_copy d edit ulong * s view ulong * to void |
| 1358 | for i ulong len s |
| 1359 | put d i get s i |
| 1360 | end |
| 1361 | end |
| 1362 | |
| 1363 | fn sha2_digest alg ulong m view uchar * o edit uchar * to ulong |
| 1364 | c = new ulong 40 0 |
| 1365 | if ne alg 256 |
| 1366 | if ne alg 384 |
| 1367 | if ne alg 512 |
| 1368 | ret 0 |
| 1369 | end |
| 1370 | end |
| 1371 | end |
| 1372 | sha2_init c alg |
| 1373 | sha2_update c m |
| 1374 | dl = sha2_fin c o |
| 1375 | for i ulong 40 |
| 1376 | put c i 0 |
| 1377 | end |
| 1378 | ret dl |
| 1379 | end |