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 / b3unit.om

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