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 / blake3.om
| 1 | note SPDX-License-Identifier: Apache-2.0 |
| 2 | note blake3: BLAKE3 default (unkeyed, 32-byte) hash of one file (or stdin when no argument), |
| 3 | note 64 hex digits + newline on stdout. Pure seeded OM-2 streaming implementation. |
| 4 | note Uses 32-bit uint state words with native OM-2 rotr and a 64 KiB I/O read buffer, |
| 5 | note removing the legacy 16 MiB static BSS buffer and file-size cap. |
| 6 | note 1024-byte chunks of 64-byte blocks, a binary tree of parents (stack depth <= 54, up to 16 EiB), |
| 7 | note the ROOT flag on the last compression only. Exit 65 when the file cannot be read, 74 on stdout error. |
| 8 | note usage: blake3 [FILE] |
| 9 | om2 |
| 10 | |
| 11 | text tbad "blake3: cannot read input\n" |
| 12 | |
| 13 | global io uchar 65536 |
| 14 | global cbuf uchar 1024 |
| 15 | global clen ulong 1 |
| 16 | global cctr ulong 1 |
| 17 | global iv uint 8 |
| 18 | global perm ulong 16 |
| 19 | global m uint 16 |
| 20 | global mt uint 16 |
| 21 | global s uint 16 |
| 22 | global co uint 16 |
| 23 | global ncv uint 8 |
| 24 | global blk uchar 64 |
| 25 | global nb ulong 3 |
| 26 | global cvr uint 8 |
| 27 | global stk uint 512 |
| 28 | global sp ulong 1 |
| 29 | global name uchar 4096 |
| 30 | global hex uchar 65 |
| 31 | |
| 32 | const cstart ulong 1 |
| 33 | const cend ulong 2 |
| 34 | const parent ulong 4 |
| 35 | const root ulong 8 |
| 36 | |
| 37 | fn initiv to void |
| 38 | put iv 0 0x6a09e667 |
| 39 | put iv 1 0xbb67ae85 |
| 40 | put iv 2 0x3c6ef372 |
| 41 | put iv 3 0xa54ff53a |
| 42 | put iv 4 0x510e527f |
| 43 | put iv 5 0x9b05688c |
| 44 | put iv 6 0x1f83d9ab |
| 45 | put iv 7 0x5be0cd19 |
| 46 | end |
| 47 | |
| 48 | fn initperm to void |
| 49 | put perm 0 2 |
| 50 | put perm 1 6 |
| 51 | put perm 2 3 |
| 52 | put perm 3 10 |
| 53 | put perm 4 7 |
| 54 | put perm 5 0 |
| 55 | put perm 6 4 |
| 56 | put perm 7 13 |
| 57 | put perm 8 1 |
| 58 | put perm 9 11 |
| 59 | put perm 10 12 |
| 60 | put perm 11 5 |
| 61 | put perm 12 9 |
| 62 | put perm 13 14 |
| 63 | put perm 14 15 |
| 64 | put perm 15 8 |
| 65 | end |
| 66 | |
| 67 | note the G mixing function on state words a b c d with message words mx my (native OM-2 uint rotr) |
| 68 | fn g a ulong b ulong c ulong d ulong mx uint my uint to void |
| 69 | put s a add add get s a get s b mx |
| 70 | put s d rotr xor get s d get s a uint 16 |
| 71 | put s c add get s c get s d |
| 72 | put s b rotr xor get s b get s c uint 12 |
| 73 | put s a add add get s a get s b my |
| 74 | put s d rotr xor get s d get s a uint 8 |
| 75 | put s c add get s c get s d |
| 76 | put s b rotr xor get s b get s c uint 7 |
| 77 | end |
| 78 | |
| 79 | fn round to void |
| 80 | g 0 4 8 12 get m 0 get m 1 |
| 81 | g 1 5 9 13 get m 2 get m 3 |
| 82 | g 2 6 10 14 get m 4 get m 5 |
| 83 | g 3 7 11 15 get m 6 get m 7 |
| 84 | g 0 5 10 15 get m 8 get m 9 |
| 85 | g 1 6 11 12 get m 10 get m 11 |
| 86 | g 2 7 8 13 get m 12 get m 13 |
| 87 | g 3 4 9 14 get m 14 get m 15 |
| 88 | end |
| 89 | |
| 90 | fn permute to void |
| 91 | for i ulong 16 unroll |
| 92 | put mt i get m get perm i |
| 93 | end |
| 94 | for i ulong 16 unroll |
| 95 | put m i get mt i |
| 96 | end |
| 97 | end |
| 98 | |
| 99 | note load the 16 little-endian words of blk into m |
| 100 | fn loadm to void |
| 101 | for i ulong 16 unroll |
| 102 | o = mul i 4 |
| 103 | v = uint get blk add o 3 |
| 104 | v = or shl v 8 uint get blk add o 2 |
| 105 | v = or shl v 8 uint get blk add o 1 |
| 106 | v = or shl v 8 uint get blk o |
| 107 | put m i v |
| 108 | end |
| 109 | end |
| 110 | |
| 111 | note compress the node (ncv, blk, nb = block_len counter flags) into co (16 words); |
| 112 | note co 0..7 is the chaining value |
| 113 | fn compress to void |
| 114 | loadm |
| 115 | for i ulong 8 unroll |
| 116 | put s i get ncv i |
| 117 | put s add i 8 get iv i |
| 118 | end |
| 119 | ctr = get nb 1 |
| 120 | put s 12 uint and ctr 0xffffffff |
| 121 | put s 13 uint shr ctr 32 |
| 122 | put s 14 uint get nb 0 |
| 123 | put s 15 uint get nb 2 |
| 124 | for r ulong 7 |
| 125 | round |
| 126 | permute |
| 127 | end |
| 128 | for i ulong 8 unroll |
| 129 | put co i xor get s i get s add i 8 |
| 130 | put co add i 8 xor get s add i 8 get ncv i |
| 131 | end |
| 132 | end |
| 133 | |
| 134 | note chaining value of the node into cvr |
| 135 | fn nodecv to void |
| 136 | compress |
| 137 | for i ulong 8 unroll |
| 138 | put cvr i get co i |
| 139 | end |
| 140 | end |
| 141 | |
| 142 | note copy cnt bytes of cbuf from offset o into blk, zero the rest |
| 143 | fn loadblk o ulong cnt ulong to void |
| 144 | for i ulong 64 |
| 145 | if lt i cnt |
| 146 | put blk i get cbuf add o i |
| 147 | else |
| 148 | put blk i 0 |
| 149 | end |
| 150 | end |
| 151 | end |
| 152 | |
| 153 | note prepare the unfinished last compression of chunk ctr holding cbuf[0 .. cnt) |
| 154 | fn chunk cnt ulong ctr ulong to void |
| 155 | for i ulong 8 unroll |
| 156 | put ncv i get iv i |
| 157 | end |
| 158 | nblk = ulong 1 |
| 159 | if gt cnt 0 |
| 160 | nblk = div add cnt 63 64 |
| 161 | end |
| 162 | b = ulong 0 |
| 163 | while lt add b 1 nblk bound 16 |
| 164 | loadblk mul b 64 64 |
| 165 | put nb 0 64 |
| 166 | put nb 1 ctr |
| 167 | put nb 2 0 |
| 168 | if eq b 0 |
| 169 | put nb 2 cstart |
| 170 | end |
| 171 | compress |
| 172 | for i ulong 8 unroll |
| 173 | put ncv i get co i |
| 174 | end |
| 175 | b = add b 1 |
| 176 | end |
| 177 | bo = mul sub nblk 1 64 |
| 178 | rest = sub cnt bo |
| 179 | loadblk bo rest |
| 180 | put nb 0 rest |
| 181 | put nb 1 ctr |
| 182 | put nb 2 cend |
| 183 | if eq nblk 1 |
| 184 | put nb 2 or cend cstart |
| 185 | end |
| 186 | end |
| 187 | |
| 188 | note parent node of stk row r (left) and cvr (right) |
| 189 | fn parentn r ulong to void |
| 190 | for i ulong 8 unroll |
| 191 | put ncv i get iv i |
| 192 | end |
| 193 | for i ulong 8 |
| 194 | l = get stk add mul r 8 i |
| 195 | v = get cvr i |
| 196 | for k ulong 4 unroll |
| 197 | sh = uint mul k 8 |
| 198 | put blk add mul i 4 k uchar and shr l sh 255 |
| 199 | put blk add 32 add mul i 4 k uchar and shr v sh 255 |
| 200 | end |
| 201 | end |
| 202 | put nb 0 64 |
| 203 | put nb 1 0 |
| 204 | put nb 2 parent |
| 205 | end |
| 206 | |
| 207 | fn push to void |
| 208 | p = get sp 0 |
| 209 | for i ulong 8 unroll |
| 210 | put stk add mul p 8 i get cvr i |
| 211 | end |
| 212 | put sp 0 add p 1 |
| 213 | end |
| 214 | |
| 215 | note flush a full non-final 1024-byte chunk in cbuf and merge completed subtrees |
| 216 | fn flushchunk to void |
| 217 | c = get cctr 0 |
| 218 | chunk 1024 c |
| 219 | nodecv |
| 220 | tot = add c 1 |
| 221 | put cctr 0 tot |
| 222 | while eq and tot 1 0 bound 64 |
| 223 | put sp 0 sub get sp 0 1 |
| 224 | parentn get sp 0 |
| 225 | nodecv |
| 226 | tot = shr tot 1 |
| 227 | end |
| 228 | push |
| 229 | put clen 0 0 |
| 230 | end |
| 231 | |
| 232 | note feed io[0 .. cnt) into the streaming chunker (never flushes the last chunk before EOF) |
| 233 | fn feed cnt ulong to void |
| 234 | pos = ulong 0 |
| 235 | while lt pos cnt bound 65536 |
| 236 | if eq get clen 0 1024 |
| 237 | flushchunk |
| 238 | end |
| 239 | cur = get clen 0 |
| 240 | take = sub 1024 cur |
| 241 | rem_b = sub cnt pos |
| 242 | if lt rem_b take |
| 243 | take = rem_b |
| 244 | end |
| 245 | for i ulong take bound 1024 |
| 246 | put cbuf add cur i get io add pos i |
| 247 | end |
| 248 | put clen 0 add cur take |
| 249 | pos = add pos take |
| 250 | end |
| 251 | end |
| 252 | |
| 253 | note stream the entire descriptor in 64 KiB reads; 0 ok, 65 read error |
| 254 | fn streamfd fd int to int |
| 255 | while true bound 1000000000 |
| 256 | r = read fd io 0 len io |
| 257 | if lt r 0 |
| 258 | ret 65 |
| 259 | end |
| 260 | if eq r 0 |
| 261 | break |
| 262 | end |
| 263 | feed ulong r |
| 264 | end |
| 265 | ret 0 |
| 266 | end |
| 267 | |
| 268 | note finalize the last chunk in cbuf[0 .. clen) and fold all pending stack CVs with ROOT on the last node |
| 269 | fn finalize to void |
| 270 | chunk get clen 0 get cctr 0 |
| 271 | while gt get sp 0 0 bound 64 |
| 272 | nodecv |
| 273 | put sp 0 sub get sp 0 1 |
| 274 | parentn get sp 0 |
| 275 | end |
| 276 | put nb 2 or get nb 2 root |
| 277 | compress |
| 278 | end |
| 279 | |
| 280 | fn hexd v ulong to uchar |
| 281 | if lt v 10 |
| 282 | ret uchar add v 48 |
| 283 | end |
| 284 | ret uchar add v 87 |
| 285 | end |
| 286 | |
| 287 | note little-endian bytes of co 0..7 as 64 hex digits |
| 288 | fn emit to int |
| 289 | for i ulong 8 |
| 290 | v = ulong get co i |
| 291 | for k ulong 4 |
| 292 | bt = and shr v mul k 8 255 |
| 293 | p = add mul i 8 mul k 2 |
| 294 | put hex p hexd shr bt 4 |
| 295 | put hex add p 1 hexd and bt 15 |
| 296 | end |
| 297 | end |
| 298 | put hex 64 10 |
| 299 | off = ulong 0 |
| 300 | while lt off 65 bound 65 |
| 301 | r = write 1 hex off sub 65 off |
| 302 | if le r 0 |
| 303 | ret 74 |
| 304 | end |
| 305 | off = add off ulong r |
| 306 | end |
| 307 | ret 0 |
| 308 | end |
| 309 | |
| 310 | fn main to int |
| 311 | initiv |
| 312 | initperm |
| 313 | fd = int 0 |
| 314 | if ge argcnt 2 |
| 315 | l = argread 1 name 0 4095 |
| 316 | if lt l 1 |
| 317 | r = write 2 tbad 0 len tbad |
| 318 | ret 65 |
| 319 | end |
| 320 | fd = int fopen name 0 ulong l 0 0 |
| 321 | if lt fd 0 |
| 322 | r = write 2 tbad 0 len tbad |
| 323 | ret 65 |
| 324 | end |
| 325 | end |
| 326 | e = streamfd fd |
| 327 | if ne e 0 |
| 328 | r = write 2 tbad 0 len tbad |
| 329 | ret 65 |
| 330 | end |
| 331 | finalize |
| 332 | ret emit |
| 333 | end |