interplane.Get Omega
Read in your language

omega-rosetta

aien-dev/omega-rosetta

Brought by drakestapleton

branch main Branches Tags History

omega-rosetta / drafts / rosettacode / sha-256.wiki

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