omega-mv
Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.
Brought by drakestapleton
omega-mv / src / estimation / est_math.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Pure seeded OM-2 IEEE-754 double-precision math, SHA-256, and printing helpers |
| 3 | note for src/estimation/ and tests/estimation/. |
| 4 | |
| 5 | const EST_PI f64 3.1415926535897932 |
| 6 | const EST_TWO_PI f64 6.2831853071795865 |
| 7 | const EST_HALF_PI f64 1.5707963267948966 |
| 8 | const EST_LN2_HI f64 0.69314718036912382 |
| 9 | const EST_LN2_LO f64 1.9082149292705877e-10 |
| 10 | const EST_INV_LN2 f64 1.4426950408889634 |
| 11 | const EST_SQRT2 f64 1.4142135623730950 |
| 12 | const EST_SQRT1_2 f64 0.70710678118654752 |
| 13 | const EST_INV_SQRT_PI f64 0.56418958354775629 |
| 14 | const EST_TWO_INV_SQRT_PI f64 1.1283791670955126 |
| 15 | const EST_HALF_LOG_2PI f64 0.91893853320467274 |
| 16 | |
| 17 | fn est_isfinite x f64 to bool |
| 18 | bits = bitcast ulong x |
| 19 | ret lt and bits 0x7ff0000000000000 0x7ff0000000000000 |
| 20 | end |
| 21 | |
| 22 | fn est_isnan x f64 to bool |
| 23 | ret ne x x |
| 24 | end |
| 25 | |
| 26 | fn est_isinf x f64 to bool |
| 27 | bits = bitcast ulong x |
| 28 | ret eq and bits 0x7fffffffffffffff 0x7ff0000000000000 |
| 29 | end |
| 30 | |
| 31 | fn est_nan to f64 |
| 32 | ret bitcast f64 ulong 0x7ff8000000000000 |
| 33 | end |
| 34 | |
| 35 | fn est_inf to f64 |
| 36 | ret bitcast f64 ulong 0x7ff0000000000000 |
| 37 | end |
| 38 | |
| 39 | fn est_ninf to f64 |
| 40 | ret bitcast f64 ulong 0xfff0000000000000 |
| 41 | end |
| 42 | |
| 43 | fn est_neg_zero to f64 |
| 44 | ret bitcast f64 ulong 0x8000000000000000 |
| 45 | end |
| 46 | |
| 47 | fn est_nextafter_up x f64 to f64 |
| 48 | ret bitcast f64 add bitcast ulong x 1 |
| 49 | end |
| 50 | |
| 51 | fn est_floor x f64 to f64 |
| 52 | if not est_isfinite x |
| 53 | ret x |
| 54 | end |
| 55 | if ge fabs x 4503599627370496.0f64 |
| 56 | ret x |
| 57 | end |
| 58 | i = long x |
| 59 | f = f64 i |
| 60 | if gt f x |
| 61 | f = sub f 1.0f64 |
| 62 | end |
| 63 | ret f |
| 64 | end |
| 65 | |
| 66 | fn est_nearbyint x f64 to f64 |
| 67 | if not est_isfinite x |
| 68 | ret x |
| 69 | end |
| 70 | if ge fabs x 4503599627370496.0f64 |
| 71 | ret x |
| 72 | end |
| 73 | fl = est_floor x |
| 74 | fr = sub x fl |
| 75 | if lt fr 0.5f64 |
| 76 | ret fl |
| 77 | end |
| 78 | if gt fr 0.5f64 |
| 79 | ret add fl 1.0f64 |
| 80 | end |
| 81 | k = long fl |
| 82 | if eq and k 1 0 |
| 83 | ret fl |
| 84 | end |
| 85 | ret add fl 1.0f64 |
| 86 | end |
| 87 | |
| 88 | fn est_ldexp_pow2 k long to f64 |
| 89 | e = add k 1023 |
| 90 | ret bitcast f64 shl ulong e 52 |
| 91 | end |
| 92 | |
| 93 | fn est_exp_poly r f64 to f64 |
| 94 | p = f64 1.6059043836821615e-10 |
| 95 | p = add mul p r 2.0876756987868099e-9 |
| 96 | p = add mul p r 2.5052108385441719e-8 |
| 97 | p = add mul p r 2.7557319223985891e-7 |
| 98 | p = add mul p r 2.7557319223985891e-6 |
| 99 | p = add mul p r 2.4801587301587302e-5 |
| 100 | p = add mul p r 1.9841269841269841e-4 |
| 101 | p = add mul p r 1.3888888888888889e-3 |
| 102 | p = add mul p r 8.3333333333333333e-3 |
| 103 | p = add mul p r 4.1666666666666667e-2 |
| 104 | p = add mul p r 1.6666666666666667e-1 |
| 105 | p = add mul p r 0.5 |
| 106 | p = add mul p r 1.0 |
| 107 | p = add mul p r 1.0 |
| 108 | ret p |
| 109 | end |
| 110 | |
| 111 | fn est_exp x f64 to f64 |
| 112 | if est_isnan x |
| 113 | ret x |
| 114 | end |
| 115 | if gt x 709.78271289338397 |
| 116 | ret est_inf |
| 117 | end |
| 118 | if lt x -745.13321910194122 |
| 119 | ret 0.0f64 |
| 120 | end |
| 121 | kf = est_nearbyint mul x EST_INV_LN2 |
| 122 | k = long kf |
| 123 | r = sub sub x mul kf EST_LN2_HI mul kf EST_LN2_LO |
| 124 | p = est_exp_poly r |
| 125 | if ge k -1022 |
| 126 | ret mul p est_ldexp_pow2 k |
| 127 | end |
| 128 | p = mul p est_ldexp_pow2 -1022 |
| 129 | ret mul p est_ldexp_pow2 add k 1022 |
| 130 | end |
| 131 | |
| 132 | fn est_log_poly t2 f64 to f64 |
| 133 | p = div 1.0f64 19.0f64 |
| 134 | p = add mul p t2 div 1.0f64 17.0f64 |
| 135 | p = add mul p t2 div 1.0f64 15.0f64 |
| 136 | p = add mul p t2 div 1.0f64 13.0f64 |
| 137 | p = add mul p t2 div 1.0f64 11.0f64 |
| 138 | p = add mul p t2 div 1.0f64 9.0f64 |
| 139 | p = add mul p t2 div 1.0f64 7.0f64 |
| 140 | p = add mul p t2 div 1.0f64 5.0f64 |
| 141 | p = add mul p t2 div 1.0f64 3.0f64 |
| 142 | p = add mul p t2 1.0f64 |
| 143 | ret p |
| 144 | end |
| 145 | |
| 146 | fn est_log x f64 to f64 |
| 147 | if est_isnan x |
| 148 | ret x |
| 149 | end |
| 150 | if lt x 0.0f64 |
| 151 | ret est_nan |
| 152 | end |
| 153 | if eq x 0.0f64 |
| 154 | ret est_ninf |
| 155 | end |
| 156 | if est_isinf x |
| 157 | ret est_inf |
| 158 | end |
| 159 | xv = x |
| 160 | ebias = long 0 |
| 161 | if lt xv 2.2250738585072014e-308 |
| 162 | xv = mul xv 18014398509481984.0 |
| 163 | ebias = -54 |
| 164 | end |
| 165 | bits = bitcast ulong xv |
| 166 | e = add sub long shr bits 52 1023 ebias |
| 167 | m = bitcast f64 or and bits 0x000fffffffffffff 0x3ff0000000000000 |
| 168 | if gt m EST_SQRT2 |
| 169 | m = mul m 0.5f64 |
| 170 | e = add e 1 |
| 171 | end |
| 172 | t = div sub m 1.0f64 add m 1.0f64 |
| 173 | lnm = mul mul 2.0f64 t est_log_poly mul t t |
| 174 | ef = f64 e |
| 175 | ret add add mul ef EST_LN2_HI lnm mul ef EST_LN2_LO |
| 176 | end |
| 177 | |
| 178 | fn est_pow a f64 b f64 to f64 |
| 179 | if eq b 0.0f64 |
| 180 | ret 1.0f64 |
| 181 | end |
| 182 | if eq a 1.0f64 |
| 183 | ret 1.0f64 |
| 184 | end |
| 185 | if eq a 0.0f64 |
| 186 | if gt b 0.0f64 |
| 187 | ret 0.0f64 |
| 188 | end |
| 189 | ret est_inf |
| 190 | end |
| 191 | if lt a 0.0f64 |
| 192 | bi = est_nearbyint b |
| 193 | if eq bi b |
| 194 | res = est_exp mul b est_log neg a |
| 195 | if ne and long bi 1 0 |
| 196 | ret neg res |
| 197 | end |
| 198 | ret res |
| 199 | end |
| 200 | ret est_nan |
| 201 | end |
| 202 | ret est_exp mul b est_log a |
| 203 | end |
| 204 | |
| 205 | fn est_powu a f64 n ulong to f64 |
| 206 | res = f64 1.0f64 |
| 207 | base = a |
| 208 | r = n |
| 209 | while gt r 0 bound 64 |
| 210 | if ne and r 1 0 |
| 211 | res = mul res base |
| 212 | end |
| 213 | r = shr r 1 |
| 214 | if gt r 0 |
| 215 | base = mul base base |
| 216 | end |
| 217 | end |
| 218 | ret res |
| 219 | end |
| 220 | |
| 221 | fn est_lgamma_lanczos z f64 to f64 |
| 222 | s = f64 0.99999999999980993f64 |
| 223 | s = add s div 676.5203681218851f64 add z 1.0f64 |
| 224 | s = add s div -1259.1392167224028f64 add z 2.0f64 |
| 225 | s = add s div 771.32342877765313f64 add z 3.0f64 |
| 226 | s = add s div -176.61502916214059f64 add z 4.0f64 |
| 227 | s = add s div 12.507343278686905f64 add z 5.0f64 |
| 228 | s = add s div -0.13857109526572012f64 add z 6.0f64 |
| 229 | s = add s div 9.9843695780195716e-6f64 add z 7.0f64 |
| 230 | s = add s div 1.5056327351493116e-7f64 add z 8.0f64 |
| 231 | t = add z 7.5f64 |
| 232 | ret add sub add EST_HALF_LOG_2PI mul add z 0.5f64 est_log t t est_log s |
| 233 | end |
| 234 | |
| 235 | fn est_lgamma x f64 to f64 |
| 236 | if not est_isfinite x |
| 237 | ret est_nan |
| 238 | end |
| 239 | if le x 0.0f64 |
| 240 | ret est_nan |
| 241 | end |
| 242 | if lt x 1.0f64 |
| 243 | ret sub est_lgamma_lanczos x est_log x |
| 244 | end |
| 245 | ret est_lgamma_lanczos sub x 1.0f64 |
| 246 | end |
| 247 | |
| 248 | fn est_erf_series x f64 to f64 |
| 249 | x2 = mul x x |
| 250 | term = x |
| 251 | s = term |
| 252 | k = ulong 1 |
| 253 | while lt k 120 bound 120 |
| 254 | term = div mul term mul 2.0f64 x2 f64 add mul 2 k 1 |
| 255 | s = add s term |
| 256 | if le fabs term mul 1.0e-17f64 fabs s |
| 257 | break |
| 258 | end |
| 259 | k = add k 1 |
| 260 | end |
| 261 | ret mul mul EST_TWO_INV_SQRT_PI est_exp neg x2 s |
| 262 | end |
| 263 | |
| 264 | fn est_erfc_cf x f64 to f64 |
| 265 | fpmin = f64 1.0e-300f64 |
| 266 | b = x |
| 267 | c = div 1.0f64 fpmin |
| 268 | d = div 1.0f64 b |
| 269 | h = d |
| 270 | m = ulong 1 |
| 271 | while le m 200 bound 200 |
| 272 | an = mul 0.5f64 f64 m |
| 273 | d = add x mul an d |
| 274 | if lt fabs d fpmin |
| 275 | d = fpmin |
| 276 | end |
| 277 | c = add x div an c |
| 278 | if lt fabs c fpmin |
| 279 | c = fpmin |
| 280 | end |
| 281 | d = div 1.0f64 d |
| 282 | del = mul d c |
| 283 | h = mul h del |
| 284 | if le fabs sub del 1.0f64 1.0e-15f64 |
| 285 | break |
| 286 | end |
| 287 | m = add m 1 |
| 288 | end |
| 289 | ret mul mul EST_INV_SQRT_PI est_exp neg mul x x h |
| 290 | end |
| 291 | |
| 292 | fn est_erfc x f64 to f64 |
| 293 | if est_isnan x |
| 294 | ret x |
| 295 | end |
| 296 | if lt x 0.0f64 |
| 297 | ret sub 2.0f64 est_erfc neg x |
| 298 | end |
| 299 | if eq x 0.0f64 |
| 300 | ret 1.0f64 |
| 301 | end |
| 302 | if gt x 27.3f64 |
| 303 | ret 0.0f64 |
| 304 | end |
| 305 | if lt x 2.0f64 |
| 306 | ret sub 1.0f64 est_erf_series x |
| 307 | end |
| 308 | ret est_erfc_cf x |
| 309 | end |
| 310 | |
| 311 | fn est_erf x f64 to f64 |
| 312 | if est_isnan x |
| 313 | ret x |
| 314 | end |
| 315 | ax = fabs x |
| 316 | r = f64 0.0f64 |
| 317 | if gt ax 6.5f64 |
| 318 | r = 1.0f64 |
| 319 | else |
| 320 | if lt ax 2.0f64 |
| 321 | r = est_erf_series ax |
| 322 | else |
| 323 | r = sub 1.0f64 est_erfc_cf ax |
| 324 | end |
| 325 | end |
| 326 | if lt x 0.0f64 |
| 327 | ret neg r |
| 328 | end |
| 329 | ret r |
| 330 | end |
| 331 | |
| 332 | fn est_sin_poly r f64 to f64 |
| 333 | r2 = mul r r |
| 334 | p = f64 -2.5052108385441719e-8 |
| 335 | p = add mul p r2 2.7557319223985891e-6 |
| 336 | p = add mul p r2 -1.9841269841269841e-4 |
| 337 | p = add mul p r2 8.3333333333333333e-3 |
| 338 | p = add mul p r2 -1.6666666666666667e-1 |
| 339 | p = add mul p r2 1.0 |
| 340 | ret mul r p |
| 341 | end |
| 342 | |
| 343 | fn est_cos_poly r f64 to f64 |
| 344 | r2 = mul r r |
| 345 | p = f64 2.0876756987868099e-9 |
| 346 | p = add mul p r2 -2.7557319223985891e-7 |
| 347 | p = add mul p r2 2.4801587301587302e-5 |
| 348 | p = add mul p r2 -1.3888888888888889e-3 |
| 349 | p = add mul p r2 4.1666666666666667e-2 |
| 350 | p = add mul p r2 -0.5 |
| 351 | p = add mul p r2 1.0 |
| 352 | ret p |
| 353 | end |
| 354 | |
| 355 | fn est_sin x f64 to f64 |
| 356 | if not est_isfinite x |
| 357 | ret est_nan |
| 358 | end |
| 359 | kf = est_nearbyint div x EST_HALF_PI |
| 360 | r = sub x mul kf EST_HALF_PI |
| 361 | q = and long kf 3 |
| 362 | if eq q 0 |
| 363 | ret est_sin_poly r |
| 364 | end |
| 365 | if eq q 1 |
| 366 | ret est_cos_poly r |
| 367 | end |
| 368 | if eq q 2 |
| 369 | ret neg est_sin_poly r |
| 370 | end |
| 371 | ret neg est_cos_poly r |
| 372 | end |
| 373 | |
| 374 | fn est_cos x f64 to f64 |
| 375 | if not est_isfinite x |
| 376 | ret est_nan |
| 377 | end |
| 378 | kf = est_nearbyint div x EST_HALF_PI |
| 379 | r = sub x mul kf EST_HALF_PI |
| 380 | q = and long kf 3 |
| 381 | if eq q 0 |
| 382 | ret est_cos_poly r |
| 383 | end |
| 384 | if eq q 1 |
| 385 | ret neg est_sin_poly r |
| 386 | end |
| 387 | if eq q 2 |
| 388 | ret neg est_cos_poly r |
| 389 | end |
| 390 | ret est_sin_poly r |
| 391 | end |
| 392 | |
| 393 | fn est_atan_small t f64 to f64 |
| 394 | t2 = mul t t |
| 395 | term = t |
| 396 | s = t |
| 397 | k = ulong 1 |
| 398 | while lt k 30 bound 30 |
| 399 | term = mul neg term t2 |
| 400 | addv = div term f64 add mul 2 k 1 |
| 401 | s = add s addv |
| 402 | if le fabs addv mul 1.0e-17f64 fabs s |
| 403 | break |
| 404 | end |
| 405 | k = add k 1 |
| 406 | end |
| 407 | ret s |
| 408 | end |
| 409 | |
| 410 | fn est_atan x f64 to f64 |
| 411 | if est_isnan x |
| 412 | ret x |
| 413 | end |
| 414 | if lt x 0.0f64 |
| 415 | ret neg est_atan neg x |
| 416 | end |
| 417 | if gt x 1.0f64 |
| 418 | ret sub EST_HALF_PI est_atan div 1.0f64 x |
| 419 | end |
| 420 | if gt x 0.2679491924311227 |
| 421 | sq3 = f64 1.7320508075688773 |
| 422 | t = div sub mul sq3 x 1.0 add sq3 x |
| 423 | ret add div EST_PI 6.0f64 est_atan_small t |
| 424 | end |
| 425 | ret est_atan_small x |
| 426 | end |
| 427 | |
| 428 | fn est_asin x f64 to f64 |
| 429 | if or lt x -1.0f64 gt x 1.0f64 |
| 430 | ret est_nan |
| 431 | end |
| 432 | if eq x 1.0f64 |
| 433 | ret EST_HALF_PI |
| 434 | end |
| 435 | if eq x -1.0f64 |
| 436 | ret neg EST_HALF_PI |
| 437 | end |
| 438 | ret est_atan div x fsqrt sub 1.0f64 mul x x |
| 439 | end |
| 440 | |
| 441 | note ---- Pure OM-2 SHA-256 (FIPS 180-4) ---- |
| 442 | |
| 443 | fn est_sha_kinit k edit uint 64 to void |
| 444 | put k 0 0x428a2f98 |
| 445 | put k 1 0x71374491 |
| 446 | put k 2 0xb5c0fbcf |
| 447 | put k 3 0xe9b5dba5 |
| 448 | put k 4 0x3956c25b |
| 449 | put k 5 0x59f111f1 |
| 450 | put k 6 0x923f82a4 |
| 451 | put k 7 0xab1c5ed5 |
| 452 | put k 8 0xd807aa98 |
| 453 | put k 9 0x12835b01 |
| 454 | put k 10 0x243185be |
| 455 | put k 11 0x550c7dc3 |
| 456 | put k 12 0x72be5d74 |
| 457 | put k 13 0x80deb1fe |
| 458 | put k 14 0x9bdc06a7 |
| 459 | put k 15 0xc19bf174 |
| 460 | put k 16 0xe49b69c1 |
| 461 | put k 17 0xefbe4786 |
| 462 | put k 18 0x0fc19dc6 |
| 463 | put k 19 0x240ca1cc |
| 464 | put k 20 0x2de92c6f |
| 465 | put k 21 0x4a7484aa |
| 466 | put k 22 0x5cb0a9dc |
| 467 | put k 23 0x76f988da |
| 468 | put k 24 0x983e5152 |
| 469 | put k 25 0xa831c66d |
| 470 | put k 26 0xb00327c8 |
| 471 | put k 27 0xbf597fc7 |
| 472 | put k 28 0xc6e00bf3 |
| 473 | put k 29 0xd5a79147 |
| 474 | put k 30 0x06ca6351 |
| 475 | put k 31 0x14292967 |
| 476 | put k 32 0x27b70a85 |
| 477 | put k 33 0x2e1b2138 |
| 478 | put k 34 0x4d2c6dfc |
| 479 | put k 35 0x53380d13 |
| 480 | put k 36 0x650a7354 |
| 481 | put k 37 0x766a0abb |
| 482 | put k 38 0x81c2c92e |
| 483 | put k 39 0x92722c85 |
| 484 | put k 40 0xa2bfe8a1 |
| 485 | put k 41 0xa81a664b |
| 486 | put k 42 0xc24b8b70 |
| 487 | put k 43 0xc76c51a3 |
| 488 | put k 44 0xd192e819 |
| 489 | put k 45 0xd6990624 |
| 490 | put k 46 0xf40e3585 |
| 491 | put k 47 0x106aa070 |
| 492 | put k 48 0x19a4c116 |
| 493 | put k 49 0x1e376c08 |
| 494 | put k 50 0x2748774c |
| 495 | put k 51 0x34b0bcb5 |
| 496 | put k 52 0x391c0cb3 |
| 497 | put k 53 0x4ed8aa4a |
| 498 | put k 54 0x5b9cca4f |
| 499 | put k 55 0x682e6ff3 |
| 500 | put k 56 0x748f82ee |
| 501 | put k 57 0x78a5636f |
| 502 | put k 58 0x84c87814 |
| 503 | put k 59 0x8cc70208 |
| 504 | put k 60 0x90befffa |
| 505 | put k 61 0xa4506ceb |
| 506 | put k 62 0xbef9a3f7 |
| 507 | put k 63 0xc67178f2 |
| 508 | end |
| 509 | |
| 510 | fn est_sha_iv s edit uint 8 to void |
| 511 | put s 0 0x6a09e667 |
| 512 | put s 1 0xbb67ae85 |
| 513 | put s 2 0x3c6ef372 |
| 514 | put s 3 0xa54ff53a |
| 515 | put s 4 0x510e527f |
| 516 | put s 5 0x9b05688c |
| 517 | put s 6 0x1f83d9ab |
| 518 | put s 7 0x5be0cd19 |
| 519 | end |
| 520 | |
| 521 | fn est_sha_sched w edit uint 64 fresh m view uchar * o ulong to void |
| 522 | q = ulong 0 |
| 523 | x = uint 0 |
| 524 | y = uint 0 |
| 525 | for i ulong 16 unroll |
| 526 | q = add o mul i 4 |
| 527 | x = uint get m q |
| 528 | x = or shl x 8 uint get m add q 1 |
| 529 | x = or shl x 8 uint get m add q 2 |
| 530 | put w i or shl x 8 uint get m add q 3 |
| 531 | end |
| 532 | for i ulong 48 unroll |
| 533 | x = get w add i 1 |
| 534 | y = get w add i 14 |
| 535 | x = add add get w i get w add i 9 xor xor rotr x 7 rotr x 18 shr x 3 |
| 536 | put w add i 16 add x xor xor rotr y 17 rotr y 19 shr y 10 |
| 537 | end |
| 538 | end |
| 539 | |
| 540 | fn est_sha_rounds s edit uint 8 w view uint 64 kt view uint 64 to void |
| 541 | a = get s 0 |
| 542 | b = get s 1 |
| 543 | c = get s 2 |
| 544 | d = get s 3 |
| 545 | e = get s 4 |
| 546 | f = get s 5 |
| 547 | g = get s 6 |
| 548 | h = get s 7 |
| 549 | t = uint 0 |
| 550 | for i ulong 64 unroll |
| 551 | h = add h get kt i |
| 552 | h = add h get w i |
| 553 | t = xor rotr e 6 rotr e 11 |
| 554 | t = xor t rotr e 25 |
| 555 | h = add h t |
| 556 | h = add h xor g and e xor f g |
| 557 | d = add d h |
| 558 | t = xor rotr a 2 rotr a 13 |
| 559 | t = xor t rotr a 22 |
| 560 | h = add h t |
| 561 | t = and c or a b |
| 562 | h = add h or and a b t |
| 563 | t = h |
| 564 | h = g |
| 565 | g = f |
| 566 | f = e |
| 567 | e = d |
| 568 | d = c |
| 569 | c = b |
| 570 | b = a |
| 571 | a = t |
| 572 | end |
| 573 | put s 0 add get s 0 a |
| 574 | put s 1 add get s 1 b |
| 575 | put s 2 add get s 2 c |
| 576 | put s 3 add get s 3 d |
| 577 | put s 4 add get s 4 e |
| 578 | put s 5 add get s 5 f |
| 579 | put s 6 add get s 6 g |
| 580 | put s 7 add get s 7 h |
| 581 | end |
| 582 | |
| 583 | fn est_sha_compress s edit uint 8 w edit uint 64 kt view uint 64 m view uchar * o ulong to void |
| 584 | est_sha_sched w m o |
| 585 | est_sha_rounds s w kt |
| 586 | end |
| 587 | |
| 588 | fn est_sha_finish s edit uint 8 w edit uint 64 kt view uint 64 p edit uchar 128 k ulong n ulong to void |
| 589 | put p k 0x80 |
| 590 | e = ulong 64 |
| 591 | if ge k 56 |
| 592 | e = 128 |
| 593 | end |
| 594 | bits = shl n 3 |
| 595 | for i ulong 8 unroll |
| 596 | put p sub e add i 1 uchar and shr bits mul i 8 0xff |
| 597 | end |
| 598 | est_sha_compress s w kt view p 0 |
| 599 | if eq e 128 |
| 600 | est_sha_compress s w kt view p 64 |
| 601 | end |
| 602 | end |
| 603 | |
| 604 | fn est_sha256_hash d view uchar * n ulong dst edit uchar 32 to void |
| 605 | s = new uint 8 0 |
| 606 | w = new uint 64 0 |
| 607 | kt = new uint 64 0 |
| 608 | p = new uchar 128 0 |
| 609 | est_sha_kinit kt |
| 610 | est_sha_iv s |
| 611 | nb = shr n 6 |
| 612 | for j ulong nb |
| 613 | est_sha_compress s w kt d shl j 6 |
| 614 | end |
| 615 | off = shl nb 6 |
| 616 | k = sub n off |
| 617 | for i ulong k |
| 618 | put p i get d add off i |
| 619 | end |
| 620 | est_sha_finish s w kt p k n |
| 621 | for i ulong 8 unroll |
| 622 | v = ulong get s i |
| 623 | for j ulong 4 unroll |
| 624 | put dst add mul i 4 j uchar and shr v sub 24 mul j 8 0xff |
| 625 | end |
| 626 | end |
| 627 | end |
| 628 | |
| 629 | fn est_sha256_domain dom view uchar * enc view uchar * enclen ulong dst edit uchar 32 to void |
| 630 | dlen = len dom |
| 631 | buf = new uchar 2400 0 |
| 632 | for i ulong dlen |
| 633 | put buf i get dom i |
| 634 | end |
| 635 | put buf dlen uchar 0 |
| 636 | base = add dlen 1 |
| 637 | for i ulong enclen |
| 638 | put buf add base i get enc i |
| 639 | end |
| 640 | est_sha256_hash view buf add base enclen dst |
| 641 | end |
| 642 | |
| 643 | note ---- Minimal stdout/stderr printing helpers for test suites ---- |
| 644 | |
| 645 | fn est_write_all fd int s view uchar * n ulong to void |
| 646 | off = ulong 0 |
| 647 | while lt off n bound 65536 |
| 648 | k = write fd s off sub n off |
| 649 | if le k 0 |
| 650 | ret |
| 651 | end |
| 652 | off = add off ulong k |
| 653 | end |
| 654 | end |
| 655 | |
| 656 | fn est_puts s view uchar * to void |
| 657 | est_write_all 1 s len s |
| 658 | end |
| 659 | |
| 660 | fn est_eputs s view uchar * to void |
| 661 | est_write_all 2 s len s |
| 662 | end |
| 663 | |
| 664 | fn est_putu_fd fd int v ulong to void |
| 665 | buf = new uchar 32 0 |
| 666 | if eq v 0 |
| 667 | put buf 0 uchar 48 |
| 668 | est_write_all fd view buf 1 |
| 669 | ret |
| 670 | end |
| 671 | n = ulong 0 |
| 672 | x = v |
| 673 | while gt x 0 bound 32 |
| 674 | put buf n uchar add 48 rem x 10 |
| 675 | x = div x 10 |
| 676 | n = add n 1 |
| 677 | end |
| 678 | rev = new uchar 32 0 |
| 679 | for i ulong n |
| 680 | put rev i get buf sub sub n 1 i |
| 681 | end |
| 682 | est_write_all fd view rev n |
| 683 | end |
| 684 | |
| 685 | fn est_putu v ulong to void |
| 686 | est_putu_fd 1 v |
| 687 | end |
| 688 | |
| 689 | fn est_puti_fd fd int v long to void |
| 690 | m = new uchar 1 45 |
| 691 | if lt v 0 |
| 692 | est_write_all fd view m 1 |
| 693 | est_putu_fd fd ulong neg v |
| 694 | ret |
| 695 | end |
| 696 | est_putu_fd fd ulong v |
| 697 | end |