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 / aienux / lm1b / sha256.om

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