interplane.Get Omega
Read in your language

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

branch main Branches Tags History

omega-mv / om / blake3 / blake3.om

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