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 / core / sovereign-core / om / vendor / sha256.om

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