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 / core / sovereign-core / om / vendor / sha256.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note Vendored from aien-dev/benchmarks-mv om/data/sha256.om at 567c5da595ebc9abebcd4dd195b1b8d7fb7507f4 (branch |
| 3 | note refactor/om-rewrite), written before that repository adopted Apache-2.0 tree-wide (d3f3b40). |
| 4 | note Header and hash-state global name differ (hstate avoids the ECDSA hs parameter); algorithm unchanged. |
| 5 | note Incremental SHA-256 in native Omega. No runtime library calls. |
| 6 | note Algorithm and constants checked against Omega seed/atlas/atlas.s2 and src/sha256.c |
| 7 | note at seed ancestor 29b12760. Those files are references, never build inputs. |
| 8 | note hmeta: buffered bytes, message bytes, state (0 active, 1 finalized, 2 error). |
| 9 | note hfinal exposes 64 lowercase hexadecimal bytes in hd; reset before another message. |
| 10 | global hk uint 64 |
| 11 | global hstate uint 8 |
| 12 | global hm uint 64 |
| 13 | global hb uchar 64 |
| 14 | global hd uchar 64 |
| 15 | global hmeta ulong 3 |
| 16 | text hconst "428a2f9871374491b5c0fbcfe9b5dba53956c25b59f111f1923f82a4ab1c5ed5d807aa9812835b01243185be550c7dc372be5d7480deb1fe9bdc06a7c19bf174e49b69c1efbe47860fc19dc6240ca1cc2de92c6f4a7484aa5cb0a9dc76f988da983e5152a831c66db00327c8bf597fc7c6e00bf3d5a7914706ca63511429296727b70a852e1b21384d2c6dfc53380d13650a7354766a0abb81c2c92e92722c85a2bfe8a1a81a664bc24b8b70c76c51a3d192e819d6990624f40e3585106aa07019a4c1161e376c082748774c34b0bcb5391c0cb34ed8aa4a5b9cca4f682e6ff3748f82ee78a5636f84c878148cc7020890befffaa4506cebbef9a3f7c67178f2" |
| 17 | text hseed "6a09e667bb67ae853c6ef372a54ff53a510e527f9b05688c1f83d9ab5be0cd19" |
| 18 | text hhex "0123456789abcdef" |
| 19 | |
| 20 | fn hnibble c uchar to uint |
| 21 | if le c 57 |
| 22 | ret uint sub c 48 |
| 23 | end |
| 24 | ret uint sub c 87 |
| 25 | end |
| 26 | |
| 27 | fn hreset to void |
| 28 | for i ulong 64 |
| 29 | v = uint 0 |
| 30 | for j ulong 8 |
| 31 | v = or shl v 4 hnibble get hconst add mul i 8 j |
| 32 | end |
| 33 | put hk i v |
| 34 | end |
| 35 | for i ulong 8 |
| 36 | v = uint 0 |
| 37 | for j ulong 8 |
| 38 | v = or shl v 4 hnibble get hseed add mul i 8 j |
| 39 | end |
| 40 | put hstate i v |
| 41 | end |
| 42 | for i ulong 3 |
| 43 | put hmeta i 0 |
| 44 | end |
| 45 | end |
| 46 | |
| 47 | fn hrot x uint n uint to uint |
| 48 | ret or shr x n shl x sub 32 n |
| 49 | end |
| 50 | |
| 51 | fn hsmall0 x uint to uint |
| 52 | ret xor xor hrot x 7 hrot x 18 shr x 3 |
| 53 | end |
| 54 | |
| 55 | fn hsmall1 x uint to uint |
| 56 | ret xor xor hrot x 17 hrot x 19 shr x 10 |
| 57 | end |
| 58 | |
| 59 | fn hbig0 x uint to uint |
| 60 | ret xor xor hrot x 2 hrot x 13 hrot x 22 |
| 61 | end |
| 62 | |
| 63 | fn hbig1 x uint to uint |
| 64 | ret xor xor hrot x 6 hrot x 11 hrot x 25 |
| 65 | end |
| 66 | |
| 67 | fn hblock to void |
| 68 | for i ulong 16 |
| 69 | v = uint 0 |
| 70 | for j ulong 4 |
| 71 | v = or shl v 8 uint get hb add mul i 4 j |
| 72 | end |
| 73 | put hm i v |
| 74 | end |
| 75 | for j ulong 48 |
| 76 | i = add j 16 |
| 77 | v = add hsmall1 get hm sub i 2 get hm sub i 7 |
| 78 | v = add v hsmall0 get hm sub i 15 |
| 79 | put hm i add v get hm sub i 16 |
| 80 | end |
| 81 | a = get hstate 0 |
| 82 | b = get hstate 1 |
| 83 | c = get hstate 2 |
| 84 | d = get hstate 3 |
| 85 | e = get hstate 4 |
| 86 | f = get hstate 5 |
| 87 | g = get hstate 6 |
| 88 | h = get hstate 7 |
| 89 | for i ulong 64 |
| 90 | t1 = add h hbig1 e |
| 91 | t1 = add t1 xor and e f and not e g |
| 92 | t1 = add t1 get hk i |
| 93 | t1 = add t1 get hm i |
| 94 | t2 = hbig0 a |
| 95 | t2 = add t2 xor xor and a b and a c and b c |
| 96 | h = g |
| 97 | g = f |
| 98 | f = e |
| 99 | e = add d t1 |
| 100 | d = c |
| 101 | c = b |
| 102 | b = a |
| 103 | a = add t1 t2 |
| 104 | end |
| 105 | put hstate 0 add get hstate 0 a |
| 106 | put hstate 1 add get hstate 1 b |
| 107 | put hstate 2 add get hstate 2 c |
| 108 | put hstate 3 add get hstate 3 d |
| 109 | put hstate 4 add get hstate 4 e |
| 110 | put hstate 5 add get hstate 5 f |
| 111 | put hstate 6 add get hstate 6 g |
| 112 | put hstate 7 add get hstate 7 h |
| 113 | end |
| 114 | |
| 115 | fn hraw c uchar to void |
| 116 | n = get hmeta 0 |
| 117 | put hb n c |
| 118 | n = add n 1 |
| 119 | if eq n 64 |
| 120 | hblock |
| 121 | n = ulong 0 |
| 122 | end |
| 123 | put hmeta 0 n |
| 124 | end |
| 125 | |
| 126 | fn hpush c uchar to bool |
| 127 | if ne get hmeta 2 0 |
| 128 | ret false |
| 129 | end |
| 130 | if eq get hmeta 1 2305843009213693951 |
| 131 | put hmeta 2 2 |
| 132 | ret false |
| 133 | end |
| 134 | hraw c |
| 135 | put hmeta 1 add get hmeta 1 1 |
| 136 | ret true |
| 137 | end |
| 138 | |
| 139 | fn hfinal to bool |
| 140 | if ne get hmeta 2 0 |
| 141 | ret false |
| 142 | end |
| 143 | bits = mul get hmeta 1 8 |
| 144 | hraw 128 |
| 145 | while ne get hmeta 0 56 |
| 146 | hraw 0 |
| 147 | end |
| 148 | for i ulong 8 |
| 149 | shift = mul sub 7 i 8 |
| 150 | hraw uchar and shr bits shift 255 |
| 151 | end |
| 152 | for i ulong 8 |
| 153 | v = get hstate i |
| 154 | for j ulong 8 |
| 155 | shift = uint mul sub 7 j 4 |
| 156 | digit = ulong and shr v shift 15 |
| 157 | put hd add mul i 8 j get hhex digit |
| 158 | end |
| 159 | end |
| 160 | put hmeta 2 1 |
| 161 | ret true |
| 162 | end |