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-1 / sha-1.om

142 lines · 2791 bytes Raw Blame History
1note Rosetta Code task "SHA-1": https://rosettacode.org/wiki/SHA-1
2note SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
3om2
4text nl "\n"
5text hexd "0123456789abcdef"
6text m0 "abc"
7text m1 "Rosetta Code"
8text m2 "The quick brown fox jumps over the lazy dog"
9text m3 "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
10global hs uint 5
11global ww uint 80
12global buf uchar 128
13note emit writes every byte of a to stdout; a short write is retried, an error exits 1
14fn emit a view uchar * to void
15o = ulong 0
16while lt o len a bound 4096
17k = write 1 a o sub len a o
18if le k 0
19exit 1
20end
21o = add o ulong k
22end
23end
24note emitc writes one byte
25fn emitc c uchar to void
26b = new uchar 1 0
27put b 0 c
28emit b
29end
30note emith writes the byte v as two lower-case hex digits
31fn emith v uchar to void
32emitc get hexd shr v 4
33emitc get hexd and v 15
34end
35note rnd is step i of 80 on the state (a, b, c, d, e) in hs, with the expanded schedule in ww
36fn rnd i ulong to void
37a = get hs 0
38b = get hs 1
39c = get hs 2
40d = get hs 3
41f = uint 0
42k = uint 0
43if lt i 20
44f = or and b c and not b d
45k = 1518500249
46else
47if lt i 40
48f = xor xor b c d
49k = 1859775393
50else
51if lt i 60
52f = or or and b c and b d and c d
53k = 2400959708
54else
55f = xor xor b c d
56k = 3395469782
57end
58end
59end
60t = add add add add rotl a uint 5 f get hs 4 k get ww i
61put hs 4 d
62put hs 3 c
63put hs 2 rotl b uint 30
64put hs 1 a
65put hs 0 t
66end
67note block loads the 64 bytes of buf at p as 16 big-endian words, extends them to 80 and runs the steps
68fn block p ulong to void
69for w ulong 16
70v = uint 0
71for j ulong 4
72v = or shl v 8 uint get buf add add p mul w 4 j
73end
74put ww w v
75end
76for w ulong 64
77x = add w 16
78v = xor get ww sub x 3 get ww sub x 8
79v = xor v get ww sub x 14
80v = xor v get ww w
81put ww x rotl v uint 1
82end
83sv = new uint 5 0
84for k ulong 5
85put sv k get hs k
86end
87for i ulong 80
88rnd i
89end
90for k ulong 5
91put hs k add get hs k get sv k
92end
93end
94note digest prints the five state words big-endian in hex
95fn digest to void
96for k ulong 5
97v = get hs k
98for j ulong 4
99s = mul uint sub 3 j 8
100emith uchar and shr v s 255
101end
102end
103emit nl
104end
105note sha1 pads a (up to 119 bytes) with 0x80, zeros and the bit length (big-endian) to one or two
106note 64-byte blocks, hashes them and prints the digest in hex
107fn sha1 a view uchar * to void
108put hs 0 uint 1732584193
109put hs 1 uint 4023233417
110put hs 2 uint 2562383102
111put hs 3 uint 271733878
112put hs 4 uint 3285377520
113n = len a
114for i ulong 128
115put buf i 0
116end
117for i ulong n
118put buf i get a i
119end
120put buf n 128
121t = ulong 64
122if ge n 56
123t = 128
124end
125bits = mul n 8
126for j ulong 8
127put buf sub sub t 1 j uchar and shr bits mul j 8 255
128end
129for b ulong div t 64
130block mul b 64
131end
132digest
133end
134fn main to int
135note the empty message, as a slice of length 0
136sha1 part m0 0 0
137sha1 m0
138sha1 m1
139sha1 m2
140sha1 m3
141ret 0
142end