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 / seed / ng / sha256.om

153 lines · 4004 bytes Raw Blame History
1note SPDX-License-Identifier: Apache-2.0
2note seed/ng/sha256.om: FIPS 180-4 SHA-256 over sh_dat[0..n) for the ng build and witness tools
3note (ngbuild.om, ngcheck.om). Program mode, OM-1, no fn main; prefix sh_. Owner: ng-ir lane.
4note om/sha256/sha256-om2.om cannot be composed into a program unit (its export fn is error 9 there).
5note Constants: the 64 round constants then the 8 initial words, decimal, copied from
6note seed/qualification/om2/fixtures/sha256-k.dec and sha256-iv.dec (bc-derived, FIPS 180-4 s4.2.2, s5.3.3).
7text sh_kiv "1116352408 1899447441 3049323471 3921009573 961987163 1508970993 2453635748 2870763221 3624381080 310598401 607225278 1426881987 1925078388 2162078206 2614888103 3248222580 3835390401 4022224774 264347078 604807628 770255983 1249150122 1555081692 1996064986 2554220882 2821834349 2952996808 3210313671 3336571891 3584528711 113926993 338241895 666307205 773529912 1294757372 1396182291 1695183700 1986661051 2177026350 2456956037 2730485921 2820302411 3259730800 3345764771 3516065817 3600352804 4094571909 275423344 430227734 506948616 659060556 883997877 958139571 1322822218 1537002063 1747873779 1955562222 2024104815 2227730452 2361852424 2428436474 2756734187 3204031479 3329325298 1779033703 3144134277 1013904242 2773480762 1359893119 2600822924 528734635 1541459225"
8global sh_k uint 72
9global sh_w uint 64
10global sh_v uint 8
11global sh_h uint 8
12global sh_pad uchar 128
13global sh_dig uchar 32
14global sh_st ulong 2
15global sh_dat uchar 16777216
16fn sh_init to void
17if ne get sh_st 1 0
18ret
19end
20put sh_st 1 1
21p = ulong 0
22for i ulong 72
23v = ulong 0
24while lt get sh_kiv p 48 bound 4
25p = add p 1
26end
27while ge get sh_kiv p 48 bound 11
28v = add mul v 10 ulong sub get sh_kiv p 48
29p = add p 1
30if ge p len sh_kiv
31put sh_k i uint v
32ret
33end
34end
35put sh_k i uint v
36end
37end
38fn sh_rr x uint n uint to uint
39ret or shr x n shl x sub 32 n
40end
41fn sh_s0 a uint to uint
42ret xor xor sh_rr a 2 sh_rr a 13 sh_rr a 22
43end
44fn sh_s1 e uint to uint
45ret xor xor sh_rr e 6 sh_rr e 11 sh_rr e 25
46end
47fn sh_g0 x uint to uint
48ret xor xor sh_rr x 7 sh_rr x 18 shr x 3
49end
50fn sh_g1 x uint to uint
51ret xor xor sh_rr x 17 sh_rr x 19 shr x 10
52end
53fn sh_maj a uint b uint c uint to uint
54ret xor xor and a b and a c and b c
55end
56note big-endian word at byte o of sh_dat (src 0) or sh_pad (src 1)
57fn sh_be src ulong o ulong to uint
58v = uint 0
59for j ulong 4
60b = uint 0
61if eq src 0
62b = uint get sh_dat add o j
63else
64b = uint get sh_pad add o j
65end
66v = or shl v 8 b
67end
68ret v
69end
70fn sh_rnd i ulong to void
71e = get sh_v 4
72t1 = add get sh_v 7 sh_s1 e
73t1 = add t1 xor and e get sh_v 5 and not e get sh_v 6
74t1 = add t1 add get sh_k i get sh_w i
75a = get sh_v 0
76t2 = add sh_s0 a sh_maj a get sh_v 1 get sh_v 2
77put sh_v 7 get sh_v 6
78put sh_v 6 get sh_v 5
79put sh_v 5 e
80put sh_v 4 add get sh_v 3 t1
81put sh_v 3 get sh_v 2
82put sh_v 2 get sh_v 1
83put sh_v 1 a
84put sh_v 0 add t1 t2
85end
86fn sh_blk src ulong o ulong to void
87for t ulong 16
88put sh_w t sh_be src add o mul t 4
89end
90for t ulong 48
91u = add t 16
92x = add sh_g1 get sh_w sub u 2 get sh_w sub u 7
93put sh_w u add x add sh_g0 get sh_w sub u 15 get sh_w sub u 16
94end
95for j ulong 8
96put sh_v j get sh_h j
97end
98for i ulong 64
99sh_rnd i
100end
101for j ulong 8
102put sh_h j add get sh_h j get sh_v j
103end
104end
105note digest of sh_dat[0..n) into sh_dig
106fn sh_all n ulong to void
107sh_init
108for j ulong 8
109put sh_h j get sh_k add j 64
110end
111nb = div n 64
112for b ulong nb bound 262144
113sh_blk 0 mul b 64
114end
115r = rem n 64
116o = mul nb 64
117for j ulong 128
118put sh_pad j 0
119end
120for j ulong r bound 64
121put sh_pad j get sh_dat add o j
122end
123put sh_pad r 128
124e = ulong 64
125if ge r 56
126e = 128
127end
128bits = mul n 8
129for j ulong 8
130put sh_pad sub sub e 1 j uchar and shr bits mul j 8 255
131end
132sh_blk 1 0
133if eq e 128
134sh_blk 1 64
135end
136sh_out
137end
138fn sh_out to void
139for j ulong 8
140v = get sh_h j
141for q ulong 4
142s = uint mul sub 3 q 8
143put sh_dig add mul j 4 q uchar and shr v s 255
144end
145end
146end
147note lowercase hex digit of the low 4 bits of x
148fn sh_hexd x uint to uchar
149if lt x 10
150ret uchar add x 48
151end
152ret uchar add x 87
153end