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 / tools / turing_verify_indep / src / sha256.om

254 lines · 6675 bytes Raw Blame History
1om2
2note SPDX-License-Identifier: Apache-2.0
3note Independent SHA-256 (FIPS 180-4) in pure seeded OM-2, lane D scorer.
4
5global g_sha_k uint 64
6global g_sha_h uint 16
7global g_sha_buf uchar 128
8global g_sha_len ulong 2
9global g_sha_n ulong 2
10global g_sha_w uint 64
11global g_sha_v uint 8
12global g_sha_io uchar 65536
13global g_sha_kinit uint 1
14
15text t_hexchars "0123456789abcdef"
16
17fn sha_init_k to void
18 if ne get g_sha_kinit 0 uint 0
19 ret
20 end
21 put g_sha_k 0 uint 0x428a2f98
22 put g_sha_k 1 uint 0x71374491
23 put g_sha_k 2 uint 0xb5c0fbcf
24 put g_sha_k 3 uint 0xe9b5dba5
25 put g_sha_k 4 uint 0x3956c25b
26 put g_sha_k 5 uint 0x59f111f1
27 put g_sha_k 6 uint 0x923f82a4
28 put g_sha_k 7 uint 0xab1c5ed5
29 put g_sha_k 8 uint 0xd807aa98
30 put g_sha_k 9 uint 0x12835b01
31 put g_sha_k 10 uint 0x243185be
32 put g_sha_k 11 uint 0x550c7dc3
33 put g_sha_k 12 uint 0x72be5d74
34 put g_sha_k 13 uint 0x80deb1fe
35 put g_sha_k 14 uint 0x9bdc06a7
36 put g_sha_k 15 uint 0xc19bf174
37 put g_sha_k 16 uint 0xe49b69c1
38 put g_sha_k 17 uint 0xefbe4786
39 put g_sha_k 18 uint 0x0fc19dc6
40 put g_sha_k 19 uint 0x240ca1cc
41 put g_sha_k 20 uint 0x2de92c6f
42 put g_sha_k 21 uint 0x4a7484aa
43 put g_sha_k 22 uint 0x5cb0a9dc
44 put g_sha_k 23 uint 0x76f988da
45 put g_sha_k 24 uint 0x983e5152
46 put g_sha_k 25 uint 0xa831c66d
47 put g_sha_k 26 uint 0xb00327c8
48 put g_sha_k 27 uint 0xbf597fc7
49 put g_sha_k 28 uint 0xc6e00bf3
50 put g_sha_k 29 uint 0xd5a79147
51 put g_sha_k 30 uint 0x06ca6351
52 put g_sha_k 31 uint 0x14292967
53 put g_sha_k 32 uint 0x27b70a85
54 put g_sha_k 33 uint 0x2e1b2138
55 put g_sha_k 34 uint 0x4d2c6dfc
56 put g_sha_k 35 uint 0x53380d13
57 put g_sha_k 36 uint 0x650a7354
58 put g_sha_k 37 uint 0x766a0abb
59 put g_sha_k 38 uint 0x81c2c92e
60 put g_sha_k 39 uint 0x92722c85
61 put g_sha_k 40 uint 0xa2bfe8a1
62 put g_sha_k 41 uint 0xa81a664b
63 put g_sha_k 42 uint 0xc24b8b70
64 put g_sha_k 43 uint 0xc76c51a3
65 put g_sha_k 44 uint 0xd192e819
66 put g_sha_k 45 uint 0xd6990624
67 put g_sha_k 46 uint 0xf40e3585
68 put g_sha_k 47 uint 0x106aa070
69 put g_sha_k 48 uint 0x19a4c116
70 put g_sha_k 49 uint 0x1e376c08
71 put g_sha_k 50 uint 0x2748774c
72 put g_sha_k 51 uint 0x34b0bcb5
73 put g_sha_k 52 uint 0x391c0cb3
74 put g_sha_k 53 uint 0x4ed8aa4a
75 put g_sha_k 54 uint 0x5b9cca4f
76 put g_sha_k 55 uint 0x682e6ff3
77 put g_sha_k 56 uint 0x748f82ee
78 put g_sha_k 57 uint 0x78a5636f
79 put g_sha_k 58 uint 0x84c87814
80 put g_sha_k 59 uint 0x8cc70208
81 put g_sha_k 60 uint 0x90befffa
82 put g_sha_k 61 uint 0xa4506ceb
83 put g_sha_k 62 uint 0xbef9a3f7
84 put g_sha_k 63 uint 0xc67178f2
85 put g_sha_kinit 0 uint 1
86end
87
88fn sha_expand ctx ulong to void
89 base = mul ctx ulong 64
90 for i ulong 16
91 p = add base mul i ulong 4
92 w0 = shl uint get g_sha_buf p 24
93 w1 = shl uint get g_sha_buf add p 1 16
94 w2 = shl uint get g_sha_buf add p 2 8
95 w3 = uint get g_sha_buf add p 3
96 put g_sha_w i or or w0 w1 or w2 w3
97 end
98 i = ulong 16
99 while lt i 64 bound 64
100 v15 = get g_sha_w sub i 15
101 s0 = xor xor rotr v15 7 rotr v15 18 shr v15 3
102 v2 = get g_sha_w sub i 2
103 s1 = xor xor rotr v2 17 rotr v2 19 shr v2 10
104 put g_sha_w i add add get g_sha_w sub i 16 s0 add get g_sha_w sub i 7 s1
105 i = add i 1
106 end
107end
108
109fn sha_round r ulong to void
110 ve = get g_sha_v 4
111 t1 = xor rotr ve 6 rotr ve 11
112 t1 = xor t1 rotr ve 25
113 t1 = add t1 get g_sha_v 7
114 t2 = and ve get g_sha_v 5
115 t2 = xor t2 and not ve get g_sha_v 6
116 t1 = add t1 t2
117 t1 = add t1 get g_sha_k r
118 t1 = add t1 get g_sha_w r
119 va = get g_sha_v 0
120 vb = get g_sha_v 1
121 vc = get g_sha_v 2
122 t2 = xor rotr va 2 rotr va 13
123 t2 = xor t2 rotr va 22
124 t2 = add t2 xor xor and va vb and va vc and vb vc
125 put g_sha_v 7 get g_sha_v 6
126 put g_sha_v 6 get g_sha_v 5
127 put g_sha_v 5 ve
128 put g_sha_v 4 add get g_sha_v 3 t1
129 put g_sha_v 3 vc
130 put g_sha_v 2 vb
131 put g_sha_v 1 va
132 put g_sha_v 0 add t1 t2
133end
134
135fn sha_compress ctx ulong to void
136 sha_expand ctx
137 hb = mul ctx ulong 8
138 for i ulong 8
139 put g_sha_v i get g_sha_h add hb i
140 end
141 for r ulong 64
142 sha_round r
143 end
144 for i ulong 8
145 put g_sha_h add hb i add get g_sha_h add hb i get g_sha_v i
146 end
147end
148
149fn sha_init ctx ulong to void
150 sha_init_k
151 hb = mul ctx ulong 8
152 put g_sha_h hb uint 0x6a09e667
153 put g_sha_h add hb 1 uint 0xbb67ae85
154 put g_sha_h add hb 2 uint 0x3c6ef372
155 put g_sha_h add hb 3 uint 0xa54ff53a
156 put g_sha_h add hb 4 uint 0x510e527f
157 put g_sha_h add hb 5 uint 0x9b05688c
158 put g_sha_h add hb 6 uint 0x1f83d9ab
159 put g_sha_h add hb 7 uint 0x5be0cd19
160 put g_sha_len ctx ulong 0
161 put g_sha_n ctx ulong 0
162end
163
164fn sha_upd_byte ctx ulong b uchar to void
165 base = mul ctx ulong 64
166 n = get g_sha_n ctx
167 put g_sha_buf add base n b
168 put g_sha_len ctx add get g_sha_len ctx ulong 1
169 n = add n 1
170 if eq n 64
171 sha_compress ctx
172 put g_sha_n ctx ulong 0
173 else
174 put g_sha_n ctx n
175 end
176end
177
178fn sha_upd ctx ulong buf view uchar * off ulong count ulong to void
179 i = ulong 0
180 while lt i count bound 100000000
181 sha_upd_byte ctx get buf add off i
182 i = add i 1
183 end
184end
185
186fn sha_upd_shaio ctx ulong off ulong count ulong to void
187 i = ulong 0
188 while lt i count bound 100000000
189 sha_upd_byte ctx get g_sha_io add off i
190 i = add i 1
191 end
192end
193
194fn sha_domain_init ctx ulong tag view uchar * to void
195 sha_init ctx
196 sha_upd ctx tag ulong 0 len tag
197 sha_upd_byte ctx uchar 0
198end
199
200fn sha_final ctx ulong dst edit uchar 32 to void
201 bits = mul get g_sha_len ctx ulong 8
202 sha_upd_byte ctx uchar 128
203 while ne get g_sha_n ctx ulong 56 bound 128
204 sha_upd_byte ctx uchar 0
205 end
206 for i ulong 8
207 sh = sub ulong 56 mul i ulong 8
208 sha_upd_byte ctx uchar and shr bits sh ulong 255
209 end
210 hb = mul ctx ulong 8
211 for i ulong 8
212 hv = get g_sha_h add hb i
213 p = mul i ulong 4
214 put dst p uchar and shr hv 24 uint 255
215 put dst add p 1 uchar and shr hv 16 uint 255
216 put dst add p 2 uchar and shr hv 8 uint 255
217 put dst add p 3 uchar and hv uint 255
218 end
219end
220
221fn sha_hex d view uchar 32 dst edit uchar 64 to void
222 for i ulong 32
223 b = ulong get d i
224 put dst mul i 2 get t_hexchars shr b 4
225 put dst add mul i 2 1 get t_hexchars and b 15
226 end
227end
228
229fn sha_file path view uchar * plen ulong dst edit uchar 32 to long
230 fd = fopen path ulong 0 plen int 0 int 0
231 if lt fd long 0
232 ret long -1
233 end
234 sha_init ulong 0
235 total = ulong 0
236 done = false
237 while not done bound 100000000
238 r = read int fd g_sha_io ulong 0 ulong 65536
239 if lt r long 0
240 _fc = fclose int fd
241 ret long -1
242 end
243 if eq r long 0
244 done = true
245 else
246 ur = ulong r
247 sha_upd_shaio ulong 0 ulong 0 ur
248 total = add total ur
249 end
250 end
251 _fc = fclose int fd
252 sha_final ulong 0 dst
253 ret long total
254end