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 / om / blake3 / b3unit.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note b3unit.om: BLAKE3 (default unkeyed hash, 32-byte output) as one function-mode unit, bound |
| 3 | note through the omega-bridge native call ABI and exported as omega_omb3. It replaces the algorithm of |
| 4 | note src/omega_blake3.c, which keeps only omega_blake3_hash: that cuts the input into 1024-byte chunks. |
| 5 | note The tree logic follows om/blake3/blake3.om (the program form, kept for files and stdin). |
| 6 | note |
| 7 | note omb3 st b n fin o: |
| 8 | note st caller state, zeroed before the first call: rows 0..53 of 8 words are the chaining-value |
| 9 | note stack, word 440 its depth, word 441 the number of chunks absorbed |
| 10 | note fin 0 b holds a whole chunk that is not the last one (n must be 1024): its chaining value is |
| 11 | note merged into the stack |
| 12 | note fin 1 b[0..n) is the last chunk (n 0..1024): the tree is finished and the root hash goes to o |
| 13 | note A wrong n traps 3. |
| 14 | note |
| 15 | note Working words w (one array because globals are program-mode only): |
| 16 | note 0..15 state s, 16..31 message m, 32..47 permute scratch, 48..55 input chaining value, |
| 17 | note 56..71 compression output, 72 block length, 73 counter, 74 flags, 75..82 node chaining value, |
| 18 | note 83..90 IV, 91..106 message permutation |
| 19 | |
| 20 | fn initw w edit ulong 128 to void |
| 21 | put w 83 1779033703 |
| 22 | put w 84 3144134277 |
| 23 | put w 85 1013904242 |
| 24 | put w 86 2773480762 |
| 25 | put w 87 1359893119 |
| 26 | put w 88 2600822924 |
| 27 | put w 89 528734635 |
| 28 | put w 90 1541459225 |
| 29 | put w 91 2 |
| 30 | put w 92 6 |
| 31 | put w 93 3 |
| 32 | put w 94 10 |
| 33 | put w 95 7 |
| 34 | put w 96 0 |
| 35 | put w 97 4 |
| 36 | put w 98 13 |
| 37 | put w 99 1 |
| 38 | put w 100 11 |
| 39 | put w 101 12 |
| 40 | put w 102 5 |
| 41 | put w 103 9 |
| 42 | put w 104 14 |
| 43 | put w 105 15 |
| 44 | put w 106 8 |
| 45 | end |
| 46 | |
| 47 | note the G mixing function on state words a b c d with message words 2j and 2j+1 |
| 48 | fn g w edit ulong 128 a ulong b ulong c ulong d ulong j ulong to void |
| 49 | x = get w add 16 mul j 2 |
| 50 | t = add get w a get w b |
| 51 | put w a and add t x 4294967295 |
| 52 | t = xor get w d get w a |
| 53 | put w d and or shr t 16 shl t 16 4294967295 |
| 54 | put w c and add get w c get w d 4294967295 |
| 55 | t = xor get w b get w c |
| 56 | put w b and or shr t 12 shl t 20 4294967295 |
| 57 | x = get w add 17 mul j 2 |
| 58 | t = add get w a get w b |
| 59 | put w a and add t x 4294967295 |
| 60 | t = xor get w d get w a |
| 61 | put w d and or shr t 8 shl t 24 4294967295 |
| 62 | put w c and add get w c get w d 4294967295 |
| 63 | t = xor get w b get w c |
| 64 | put w b and or shr t 7 shl t 25 4294967295 |
| 65 | end |
| 66 | |
| 67 | fn round w edit ulong 128 to void |
| 68 | g w 0 4 8 12 0 |
| 69 | g w 1 5 9 13 1 |
| 70 | g w 2 6 10 14 2 |
| 71 | g w 3 7 11 15 3 |
| 72 | g w 0 5 10 15 4 |
| 73 | g w 1 6 11 12 5 |
| 74 | g w 2 7 8 13 6 |
| 75 | g w 3 4 9 14 7 |
| 76 | end |
| 77 | |
| 78 | fn permute w edit ulong 128 to void |
| 79 | for i ulong 16 |
| 80 | put w add 32 i get w add 16 get w add 91 i |
| 81 | end |
| 82 | for i ulong 16 |
| 83 | put w add 16 i get w add 32 i |
| 84 | end |
| 85 | end |
| 86 | |
| 87 | note the 16 little-endian words of blk into m |
| 88 | fn loadm w edit ulong 128 blk edit uchar 64 to void |
| 89 | for i ulong 16 |
| 90 | o = mul i 4 |
| 91 | v = ulong get blk add o 3 |
| 92 | v = or shl v 8 ulong get blk add o 2 |
| 93 | v = or shl v 8 ulong get blk add o 1 |
| 94 | v = or shl v 8 ulong get blk o |
| 95 | put w add 16 i v |
| 96 | end |
| 97 | end |
| 98 | |
| 99 | note compress (input chaining value, blk, block length, counter, flags) into the output words |
| 100 | fn compress w edit ulong 128 blk edit uchar 64 to void |
| 101 | loadm w blk |
| 102 | for i ulong 8 |
| 103 | put w i get w add 48 i |
| 104 | put w add i 8 get w add 83 i |
| 105 | end |
| 106 | put w 12 and get w 73 4294967295 |
| 107 | put w 13 and shr get w 73 32 4294967295 |
| 108 | put w 14 get w 72 |
| 109 | put w 15 get w 74 |
| 110 | for r ulong 7 |
| 111 | round w |
| 112 | permute w |
| 113 | end |
| 114 | for i ulong 8 |
| 115 | put w add 56 i xor get w i get w add i 8 |
| 116 | put w add 64 i xor get w add i 8 get w add 48 i |
| 117 | end |
| 118 | end |
| 119 | |
| 120 | note compress, then the node chaining value is the first 8 output words |
| 121 | fn nodecv w edit ulong 128 blk edit uchar 64 to void |
| 122 | compress w blk |
| 123 | for i ulong 8 |
| 124 | put w add 75 i get w add 56 i |
| 125 | end |
| 126 | end |
| 127 | |
| 128 | note copy cnt bytes of b from offset o into blk, zero the rest |
| 129 | fn loadblk blk edit uchar 64 b view uchar 1024 o ulong cnt ulong to void |
| 130 | for i ulong 64 |
| 131 | if lt i cnt |
| 132 | put blk i get b add o i |
| 133 | else |
| 134 | put blk i 0 |
| 135 | end |
| 136 | end |
| 137 | end |
| 138 | |
| 139 | fn setnb w edit ulong 128 bl ulong ctr ulong fl ulong to void |
| 140 | put w 72 bl |
| 141 | put w 73 ctr |
| 142 | put w 74 fl |
| 143 | end |
| 144 | |
| 145 | note chunk ctr holding b[0..cnt): every block but the last compressed, the last left set up |
| 146 | fn chunk w edit ulong 128 blk edit uchar 64 b view uchar 1024 cnt ulong ctr ulong to void |
| 147 | for i ulong 8 |
| 148 | put w add 48 i get w add 83 i |
| 149 | end |
| 150 | nblk = ulong 1 |
| 151 | if gt cnt 0 |
| 152 | nblk = div add cnt 63 64 |
| 153 | end |
| 154 | k = ulong 0 |
| 155 | while lt add k 1 nblk bound 16 |
| 156 | loadblk blk b mul k 64 64 |
| 157 | fl = ulong 0 |
| 158 | if eq k 0 |
| 159 | fl = 1 |
| 160 | end |
| 161 | setnb w 64 ctr fl |
| 162 | compress w blk |
| 163 | for i ulong 8 |
| 164 | put w add 48 i get w add 56 i |
| 165 | end |
| 166 | k = add k 1 |
| 167 | end |
| 168 | bo = mul sub nblk 1 64 |
| 169 | loadblk blk b bo sub cnt bo |
| 170 | fl = ulong 2 |
| 171 | if eq nblk 1 |
| 172 | fl = 3 |
| 173 | end |
| 174 | setnb w sub cnt bo ctr fl |
| 175 | end |
| 176 | |
| 177 | note parent node of stack row r (left) and the node chaining value (right), set up to compress |
| 178 | fn parentn w edit ulong 128 blk edit uchar 64 st edit ulong 448 r ulong to void |
| 179 | for i ulong 8 |
| 180 | put w add 48 i get w add 83 i |
| 181 | end |
| 182 | for i ulong 8 |
| 183 | l = get st add mul r 8 i |
| 184 | v = get w add 75 i |
| 185 | for k ulong 4 |
| 186 | sh = mul k 8 |
| 187 | q = add mul i 4 k |
| 188 | put blk q uchar and shr l sh 255 |
| 189 | put blk add 32 q uchar and shr v sh 255 |
| 190 | end |
| 191 | end |
| 192 | setnb w 64 0 4 |
| 193 | end |
| 194 | |
| 195 | fn push w edit ulong 128 st edit ulong 448 to void |
| 196 | p = get st 440 |
| 197 | for i ulong 8 |
| 198 | put st add mul p 8 i get w add 75 i |
| 199 | end |
| 200 | put st 440 add p 1 |
| 201 | end |
| 202 | |
| 203 | note a whole chunk that is not the last: merge completed subtrees, then push its chaining value |
| 204 | fn midchk w edit ulong 128 blk edit uchar 64 st edit ulong 448 b view uchar 1024 to void |
| 205 | ctr = get st 441 |
| 206 | chunk w blk b 1024 ctr |
| 207 | nodecv w blk |
| 208 | tot = add ctr 1 |
| 209 | while eq and tot 1 0 bound 64 |
| 210 | d = sub get st 440 1 |
| 211 | put st 440 d |
| 212 | parentn w blk st d |
| 213 | nodecv w blk |
| 214 | tot = shr tot 1 |
| 215 | end |
| 216 | push w st |
| 217 | put st 441 add ctr 1 |
| 218 | end |
| 219 | |
| 220 | note the last chunk: fold the stack into the root and write its 32 little-endian bytes into o |
| 221 | fn lastchk w edit ulong 128 blk edit uchar 64 st edit ulong 448 b view uchar 1024 n ulong o edit uchar 32 to void |
| 222 | chunk w blk b n get st 441 |
| 223 | while gt get st 440 0 bound 64 |
| 224 | nodecv w blk |
| 225 | d = sub get st 440 1 |
| 226 | put st 440 d |
| 227 | parentn w blk st d |
| 228 | end |
| 229 | put w 74 or get w 74 8 |
| 230 | compress w blk |
| 231 | outle w o |
| 232 | end |
| 233 | |
| 234 | note the first 8 output words, little endian, into o |
| 235 | fn outle w edit ulong 128 o edit uchar 32 to void |
| 236 | for i ulong 8 |
| 237 | v = get w add 56 i |
| 238 | for k ulong 4 |
| 239 | q = add mul i 4 k |
| 240 | put o q uchar and shr v mul k 8 255 |
| 241 | end |
| 242 | end |
| 243 | end |
| 244 | |
| 245 | fn omb3 st edit ulong 448 b view uchar 1024 n ulong fin ulong o edit uchar 32 to void |
| 246 | w = new ulong 128 0 |
| 247 | blk = new uchar 64 0 |
| 248 | initw w |
| 249 | if gt n 1024 |
| 250 | trap 3 |
| 251 | end |
| 252 | if eq fin 0 |
| 253 | if ne n 1024 |
| 254 | trap 3 |
| 255 | end |
| 256 | midchk w blk st b |
| 257 | ret |
| 258 | end |
| 259 | lastchk w blk st b n o |
| 260 | end |