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 / seed / ng / sha256.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note seed/ng/sha256.om: FIPS 180-4 SHA-256 over sh_dat[0..n) for the ng build and witness tools |
| 3 | note (ngbuild.om, ngcheck.om). Program mode, OM-1, no fn main; prefix sh_. Owner: ng-ir lane. |
| 4 | note om/sha256/sha256-om2.om cannot be composed into a program unit (its export fn is error 9 there). |
| 5 | note Constants: the 64 round constants then the 8 initial words, decimal, copied from |
| 6 | note seed/qualification/om2/fixtures/sha256-k.dec and sha256-iv.dec (bc-derived, FIPS 180-4 s4.2.2, s5.3.3). |
| 7 | text sh_kiv "1116352408 1899447441 3049323471 3921009573 961987163 1508970993 2453635748 2870763221 3624381080 310598401 607225278 1426881987 1925078388 2162078206 2614888103 3248222580 3835390401 4022224774 264347078 604807628 770255983 1249150122 1555081692 1996064986 2554220882 2821834349 2952996808 3210313671 3336571891 3584528711 113926993 338241895 666307205 773529912 1294757372 1396182291 1695183700 1986661051 2177026350 2456956037 2730485921 2820302411 3259730800 3345764771 3516065817 3600352804 4094571909 275423344 430227734 506948616 659060556 883997877 958139571 1322822218 1537002063 1747873779 1955562222 2024104815 2227730452 2361852424 2428436474 2756734187 3204031479 3329325298 1779033703 3144134277 1013904242 2773480762 1359893119 2600822924 528734635 1541459225" |
| 8 | global sh_k uint 72 |
| 9 | global sh_w uint 64 |
| 10 | global sh_v uint 8 |
| 11 | global sh_h uint 8 |
| 12 | global sh_pad uchar 128 |
| 13 | global sh_dig uchar 32 |
| 14 | global sh_st ulong 2 |
| 15 | global sh_dat uchar 16777216 |
| 16 | fn sh_init to void |
| 17 | if ne get sh_st 1 0 |
| 18 | ret |
| 19 | end |
| 20 | put sh_st 1 1 |
| 21 | p = ulong 0 |
| 22 | for i ulong 72 |
| 23 | v = ulong 0 |
| 24 | while lt get sh_kiv p 48 bound 4 |
| 25 | p = add p 1 |
| 26 | end |
| 27 | while ge get sh_kiv p 48 bound 11 |
| 28 | v = add mul v 10 ulong sub get sh_kiv p 48 |
| 29 | p = add p 1 |
| 30 | if ge p len sh_kiv |
| 31 | put sh_k i uint v |
| 32 | ret |
| 33 | end |
| 34 | end |
| 35 | put sh_k i uint v |
| 36 | end |
| 37 | end |
| 38 | fn sh_rr x uint n uint to uint |
| 39 | ret or shr x n shl x sub 32 n |
| 40 | end |
| 41 | fn sh_s0 a uint to uint |
| 42 | ret xor xor sh_rr a 2 sh_rr a 13 sh_rr a 22 |
| 43 | end |
| 44 | fn sh_s1 e uint to uint |
| 45 | ret xor xor sh_rr e 6 sh_rr e 11 sh_rr e 25 |
| 46 | end |
| 47 | fn sh_g0 x uint to uint |
| 48 | ret xor xor sh_rr x 7 sh_rr x 18 shr x 3 |
| 49 | end |
| 50 | fn sh_g1 x uint to uint |
| 51 | ret xor xor sh_rr x 17 sh_rr x 19 shr x 10 |
| 52 | end |
| 53 | fn sh_maj a uint b uint c uint to uint |
| 54 | ret xor xor and a b and a c and b c |
| 55 | end |
| 56 | note big-endian word at byte o of sh_dat (src 0) or sh_pad (src 1) |
| 57 | fn sh_be src ulong o ulong to uint |
| 58 | v = uint 0 |
| 59 | for j ulong 4 |
| 60 | b = uint 0 |
| 61 | if eq src 0 |
| 62 | b = uint get sh_dat add o j |
| 63 | else |
| 64 | b = uint get sh_pad add o j |
| 65 | end |
| 66 | v = or shl v 8 b |
| 67 | end |
| 68 | ret v |
| 69 | end |
| 70 | fn sh_rnd i ulong to void |
| 71 | e = get sh_v 4 |
| 72 | t1 = add get sh_v 7 sh_s1 e |
| 73 | t1 = add t1 xor and e get sh_v 5 and not e get sh_v 6 |
| 74 | t1 = add t1 add get sh_k i get sh_w i |
| 75 | a = get sh_v 0 |
| 76 | t2 = add sh_s0 a sh_maj a get sh_v 1 get sh_v 2 |
| 77 | put sh_v 7 get sh_v 6 |
| 78 | put sh_v 6 get sh_v 5 |
| 79 | put sh_v 5 e |
| 80 | put sh_v 4 add get sh_v 3 t1 |
| 81 | put sh_v 3 get sh_v 2 |
| 82 | put sh_v 2 get sh_v 1 |
| 83 | put sh_v 1 a |
| 84 | put sh_v 0 add t1 t2 |
| 85 | end |
| 86 | fn sh_blk src ulong o ulong to void |
| 87 | for t ulong 16 |
| 88 | put sh_w t sh_be src add o mul t 4 |
| 89 | end |
| 90 | for t ulong 48 |
| 91 | u = add t 16 |
| 92 | x = add sh_g1 get sh_w sub u 2 get sh_w sub u 7 |
| 93 | put sh_w u add x add sh_g0 get sh_w sub u 15 get sh_w sub u 16 |
| 94 | end |
| 95 | for j ulong 8 |
| 96 | put sh_v j get sh_h j |
| 97 | end |
| 98 | for i ulong 64 |
| 99 | sh_rnd i |
| 100 | end |
| 101 | for j ulong 8 |
| 102 | put sh_h j add get sh_h j get sh_v j |
| 103 | end |
| 104 | end |
| 105 | note digest of sh_dat[0..n) into sh_dig |
| 106 | fn sh_all n ulong to void |
| 107 | sh_init |
| 108 | for j ulong 8 |
| 109 | put sh_h j get sh_k add j 64 |
| 110 | end |
| 111 | nb = div n 64 |
| 112 | for b ulong nb bound 262144 |
| 113 | sh_blk 0 mul b 64 |
| 114 | end |
| 115 | r = rem n 64 |
| 116 | o = mul nb 64 |
| 117 | for j ulong 128 |
| 118 | put sh_pad j 0 |
| 119 | end |
| 120 | for j ulong r bound 64 |
| 121 | put sh_pad j get sh_dat add o j |
| 122 | end |
| 123 | put sh_pad r 128 |
| 124 | e = ulong 64 |
| 125 | if ge r 56 |
| 126 | e = 128 |
| 127 | end |
| 128 | bits = mul n 8 |
| 129 | for j ulong 8 |
| 130 | put sh_pad sub sub e 1 j uchar and shr bits mul j 8 255 |
| 131 | end |
| 132 | sh_blk 1 0 |
| 133 | if eq e 128 |
| 134 | sh_blk 1 64 |
| 135 | end |
| 136 | sh_out |
| 137 | end |
| 138 | fn sh_out to void |
| 139 | for j ulong 8 |
| 140 | v = get sh_h j |
| 141 | for q ulong 4 |
| 142 | s = uint mul sub 3 q 8 |
| 143 | put sh_dig add mul j 4 q uchar and shr v s 255 |
| 144 | end |
| 145 | end |
| 146 | end |
| 147 | note lowercase hex digit of the low 4 bits of x |
| 148 | fn sh_hexd x uint to uchar |
| 149 | if lt x 10 |
| 150 | ret uchar add x 48 |
| 151 | end |
| 152 | ret uchar add x 87 |
| 153 | end |