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