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 / si / rosetta / tasks / sha-256 / sha-256.om

215 lines · 4207 bytes Raw Blame History
1note Rosetta Code task "SHA-256": https://rosettacode.org/wiki/SHA-256
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text m1 "abc"
6text m2 "Rosetta code"
7text m3 "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
8global kt uint 64
9global hs uint 8
10global w uint 64
11global blk uchar 128
12note emit writes every byte of a to stdout; a short write is retried, an error exits 1
13fn emit a view uchar * to void
14o = ulong 0
15while lt o len a bound 4096
16k = write 1 a o sub len a o
17if le k 0
18exit 1
19end
20o = add o ulong k
21end
22end
23note emitx writes n as 8 lowercase hex digits
24fn emitx n uint to void
25d = new uchar 8 0
26v = n
27for i ulong 8
28q = uchar and v 15
29if lt q 10
30put d sub 7 i add q 48
31else
32put d sub 7 i add q 87
33end
34v = shr v 4
35end
36emit d
37end
38note FIPS 180-4 constants, copied mechanically from aien-dev/omega-mv om/sha256/sha256-om2.om
39fn kinit to void
40put kt 0 0x428a2f98
41put kt 1 0x71374491
42put kt 2 0xb5c0fbcf
43put kt 3 0xe9b5dba5
44put kt 4 0x3956c25b
45put kt 5 0x59f111f1
46put kt 6 0x923f82a4
47put kt 7 0xab1c5ed5
48put kt 8 0xd807aa98
49put kt 9 0x12835b01
50put kt 10 0x243185be
51put kt 11 0x550c7dc3
52put kt 12 0x72be5d74
53put kt 13 0x80deb1fe
54put kt 14 0x9bdc06a7
55put kt 15 0xc19bf174
56put kt 16 0xe49b69c1
57put kt 17 0xefbe4786
58put kt 18 0x0fc19dc6
59put kt 19 0x240ca1cc
60put kt 20 0x2de92c6f
61put kt 21 0x4a7484aa
62put kt 22 0x5cb0a9dc
63put kt 23 0x76f988da
64put kt 24 0x983e5152
65put kt 25 0xa831c66d
66put kt 26 0xb00327c8
67put kt 27 0xbf597fc7
68put kt 28 0xc6e00bf3
69put kt 29 0xd5a79147
70put kt 30 0x06ca6351
71put kt 31 0x14292967
72put kt 32 0x27b70a85
73put kt 33 0x2e1b2138
74put kt 34 0x4d2c6dfc
75put kt 35 0x53380d13
76put kt 36 0x650a7354
77put kt 37 0x766a0abb
78put kt 38 0x81c2c92e
79put kt 39 0x92722c85
80put kt 40 0xa2bfe8a1
81put kt 41 0xa81a664b
82put kt 42 0xc24b8b70
83put kt 43 0xc76c51a3
84put kt 44 0xd192e819
85put kt 45 0xd6990624
86put kt 46 0xf40e3585
87put kt 47 0x106aa070
88put kt 48 0x19a4c116
89put kt 49 0x1e376c08
90put kt 50 0x2748774c
91put kt 51 0x34b0bcb5
92put kt 52 0x391c0cb3
93put kt 53 0x4ed8aa4a
94put kt 54 0x5b9cca4f
95put kt 55 0x682e6ff3
96put kt 56 0x748f82ee
97put kt 57 0x78a5636f
98put kt 58 0x84c87814
99put kt 59 0x8cc70208
100put kt 60 0x90befffa
101put kt 61 0xa4506ceb
102put kt 62 0xbef9a3f7
103put kt 63 0xc67178f2
104end
105fn iv to void
106put hs 0 0x6a09e667
107put hs 1 0xbb67ae85
108put hs 2 0x3c6ef372
109put hs 3 0xa54ff53a
110put hs 4 0x510e527f
111put hs 5 0x9b05688c
112put hs 6 0x1f83d9ab
113put hs 7 0x5be0cd19
114end
115fn bs0 x uint to uint
116ret xor xor rotr x 2 rotr x 13 rotr x 22
117end
118fn bs1 x uint to uint
119ret xor xor rotr x 6 rotr x 11 rotr x 25
120end
121fn ss0 x uint to uint
122ret xor xor rotr x 7 rotr x 18 shr x 3
123end
124fn ss1 x uint to uint
125ret xor xor rotr x 17 rotr x 19 shr x 10
126end
127fn ch e uint f uint g uint to uint
128ret xor and e f and not e g
129end
130fn maj a uint b uint c uint to uint
131ret xor xor and a b and a c and b c
132end
133note compress the 64-byte block blk[o .. o+64) into hs
134fn compress o ulong to void
135for i ulong 16
136j = add o mul i 4
137x = shl uint get blk j 24
138x = or x shl uint get blk add j 1 16
139x = or x shl uint get blk add j 2 8
140x = or x uint get blk add j 3
141put w i x
142end
143for i ulong 48
144x = add get w i ss0 get w add i 1
145x = add x get w add i 9
146x = add x ss1 get w add i 14
147put w add i 16 x
148end
149a = get hs 0
150b = get hs 1
151c = get hs 2
152d = get hs 3
153e = get hs 4
154f = get hs 5
155g = get hs 6
156h = get hs 7
157for i ulong 64
158t1 = add h bs1 e
159t1 = add t1 ch e f g
160t1 = add t1 get kt i
161t1 = add t1 get w i
162t2 = add bs0 a maj a b c
163h = g
164g = f
165f = e
166e = add d t1
167d = c
168c = b
169b = a
170a = add t1 t2
171end
172put hs 0 add get hs 0 a
173put hs 1 add get hs 1 b
174put hs 2 add get hs 2 c
175put hs 3 add get hs 3 d
176put hs 4 add get hs 4 e
177put hs 5 add get hs 5 f
178put hs 6 add get hs 6 g
179put hs 7 add get hs 7 h
180end
181note one or two blocks: messages up to 119 bytes (longer traps 3 at the first put past blk)
182fn sha a view uchar * to void
183n = len a
184for i ulong 128
185put blk i 0
186end
187for i ulong n
188put blk i get a i
189end
190put blk n 128
191total = ulong 64
192if gt n 55
193total = 128
194end
195bits = mul n 8
196for i ulong 8
197put blk sub total add i 1 uchar and shr bits mul i 8 255
198end
199iv
200compress 0
201if eq total 128
202compress 64
203end
204for i ulong 8
205emitx get hs i
206end
207emit nl
208end
209fn main to int
210kinit
211sha m1
212sha m2
213sha m3
214ret 0
215end