gnark-plonky2-verifier/sha512/sha512.go
sha512/sha512.goRaw
1package sha512
2
3/* Based on https://gist.github.com/illia-v/7883be942da5d416521375004cecb68f */
4
5import (
6 "github.com/consensys/gnark/frontend"
7)
8
9func Sha512(api frontend.API, in []frontend.Variable) [512]frontend.Variable {
10 _not := func(x [64]frontend.Variable) [64]frontend.Variable {
11 return not(api, x)
12 }
13 _and := func(xs ...[64]frontend.Variable) [64]frontend.Variable {
14 return and(api, xs...)
15 }
16 _add := func(xs ...[64]frontend.Variable) [64]frontend.Variable {
17 return add(api, xs...)
18 }
19 _xor := func(xs ...[64]frontend.Variable) [64]frontend.Variable {
20 return xor(api, xs...)
21 }
22 zip_add := func(a, b Array8_64) Array8_64 {
23 a0, a1, a2, a3, a4, a5, a6, a7 := unpack8(a)
24 b0, b1, b2, b3, b4, b5, b6, b7 := unpack8(b)
25 return Array8_64{
26 _add(a0, b0),
27 _add(a1, b1),
28 _add(a2, b2),
29 _add(a3, b3),
30 _add(a4, b4),
31 _add(a5, b5),
32 _add(a6, b6),
33 _add(a7, b7),
34 }
35 }
36 initial_hash := []uint64{
37 0x6a09e667f3bcc908,
38 0xbb67ae8584caa73b,
39 0x3c6ef372fe94f82b,
40 0xa54ff53a5f1d36f1,
41 0x510e527fade682d1,
42 0x9b05688c2b3e6c1f,
43 0x1f83d9abfb41bd6b,
44 0x5be0cd19137e2179,
45 }
46 round_constants := []uint64{
47 0x428a2f98d728ae22, 0x7137449123ef65cd, 0xb5c0fbcfec4d3b2f,
48 0xe9b5dba58189dbbc, 0x3956c25bf348b538, 0x59f111f1b605d019,
49 0x923f82a4af194f9b, 0xab1c5ed5da6d8118, 0xd807aa98a3030242,
50 0x12835b0145706fbe, 0x243185be4ee4b28c, 0x550c7dc3d5ffb4e2,
51 0x72be5d74f27b896f, 0x80deb1fe3b1696b1, 0x9bdc06a725c71235,
52 0xc19bf174cf692694, 0xe49b69c19ef14ad2, 0xefbe4786384f25e3,
53 0x0fc19dc68b8cd5b5, 0x240ca1cc77ac9c65, 0x2de92c6f592b0275,
54 0x4a7484aa6ea6e483, 0x5cb0a9dcbd41fbd4, 0x76f988da831153b5,
55 0x983e5152ee66dfab, 0xa831c66d2db43210, 0xb00327c898fb213f,
56 0xbf597fc7beef0ee4, 0xc6e00bf33da88fc2, 0xd5a79147930aa725,
57 0x06ca6351e003826f, 0x142929670a0e6e70, 0x27b70a8546d22ffc,
58 0x2e1b21385c26c926, 0x4d2c6dfc5ac42aed, 0x53380d139d95b3df,
59 0x650a73548baf63de, 0x766a0abb3c77b2a8, 0x81c2c92e47edaee6,
60 0x92722c851482353b, 0xa2bfe8a14cf10364, 0xa81a664bbc423001,
61 0xc24b8b70d0f89791, 0xc76c51a30654be30, 0xd192e819d6ef5218,
62 0xd69906245565a910, 0xf40e35855771202a, 0x106aa07032bbd1b8,
63 0x19a4c116b8d2d0c8, 0x1e376c085141ab53, 0x2748774cdf8eeb99,
64 0x34b0bcb5e19b48a8, 0x391c0cb3c5c95a63, 0x4ed8aa4ae3418acb,
65 0x5b9cca4f7763e373, 0x682e6ff3d6b2b8a3, 0x748f82ee5defb2fc,
66 0x78a5636f43172f60, 0x84c87814a1f0ab72, 0x8cc702081a6439ec,
67 0x90befffa23631e28, 0xa4506cebde82bde9, 0xbef9a3f7b2c67915,
68 0xc67178f2e372532b, 0xca273eceea26619c, 0xd186b8c721c0c207,
69 0xeada7dd6cde0eb1e, 0xf57d4f7fee6ed178, 0x06f067aa72176fba,
70 0x0a637dc5a2c898a6, 0x113f9804bef90dae, 0x1b710b35131c471b,
71 0x28db77f523047d84, 0x32caab7b40c72493, 0x3c9ebe0a15c9bebc,
72 0x431d67c49c100d4c, 0x4cc5d4becb3e42b6, 0x597f299cfc657e2a,
73 0x5fcb6fab3ad6faec, 0x6c44198c4a475817,
74 }
75 for _, v := range in {
76 api.AssertIsBoolean(v)
77 }
78 mdi := divChecked(len(in), 8) % 128
79 var padding_len int
80 if mdi < 112 {
81 padding_len = 119 - mdi
82 } else {
83 padding_len = 247 - mdi
84 }
85 message_length_bits := uint64ToBits(uint64(len(in)))
86 in = append(in, 1)
87 for i := 0; i < 7; i++ {
88 in = append(in, 0)
89 }
90 for i := 0; i < padding_len*8; i++ {
91 in = append(in, 0)
92 }
93 for i := 0; i < 64; i++ {
94 in = append(in, message_length_bits[i])
95 }
96
97 sha512_hash := Array8_64{
98 uint64ToBits(initial_hash[0]),
99 uint64ToBits(initial_hash[1]),
100 uint64ToBits(initial_hash[2]),
101 uint64ToBits(initial_hash[3]),
102 uint64ToBits(initial_hash[4]),
103 uint64ToBits(initial_hash[5]),
104 uint64ToBits(initial_hash[6]),
105 uint64ToBits(initial_hash[7]),
106 }
107 for chunk_start := 0; chunk_start < divChecked(len(in), 8); chunk_start += 128 {
108 chunk := in[chunk_start*8 : (chunk_start+128)*8]
109 if len(chunk) != 1024 {
110 panic("bad length")
111 }
112 u := make([]frontend.Variable, 80*64)
113 for i, _ := range u {
114 u[i] = 0
115 }
116 copy(u, chunk)
117
118 w := reshape(u)
119
120 for i := 16; i < 80; i++ {
121 s0 := _xor(
122 _right_rotate(w[i-15], 1),
123 _right_rotate(w[i-15], 8),
124 _shr(w[i-15], 7),
125 )
126 s1 := _xor(
127 _right_rotate(w[i-2], 19),
128 _right_rotate(w[i-2], 61),
129 _shr(w[i-2], 6),
130 )
131 w[i] = _add(w[i-16], s0, w[i-7], s1)
132 }
133 a, b, c, d, e, f, g, h := unpack8(sha512_hash)
134 for i := 0; i < 80; i++ {
135 sum1 := _xor(
136 _right_rotate(e, 14),
137 _right_rotate(e, 18),
138 _right_rotate(e, 41),
139 )
140 ch := _xor(_and(e, f), _and(_not(e), g))
141 temp1 := _add(h, sum1, ch, uint64ToBits(round_constants[i]), w[i])
142 sum0 := _xor(
143 _right_rotate(a, 28),
144 _right_rotate(a, 34),
145 _right_rotate(a, 39),
146 )
147 maj := _xor(_and(a, b), _and(a, c), _and(b, c))
148 temp2 := _add(sum0, maj)
149
150 h = g
151 g = f
152 f = e
153 e = _add(d, temp1)
154 d = c
155 c = b
156 b = a
157 a = _add(temp1, temp2)
158 }
159 sha512_hash = zip_add(sha512_hash, Array8_64{a, b, c, d, e, f, g, h})
160 }
161 return flatten8(sha512_hash)
162}
163
164func _right_rotate(n [64]frontend.Variable, bits int) [64]frontend.Variable {
165 var result [64]frontend.Variable
166 for i := 0; i < len(n); i++ {
167 result[(i+bits)%len(n)] = n[i]
168 }
169 return result
170}
171
172func reshape(u []frontend.Variable) [][64]frontend.Variable {
173 l := divChecked(len(u), 64)
174 result := make([][64]frontend.Variable, l)
175 for i := 0; i < l; i++ {
176 var arr [64]frontend.Variable
177 for k := 0; k < 64; k++ {
178 arr[k] = u[i*64+k]
179 }
180 result[i] = arr
181 }
182 return result
183}
184
185type Array8_64 [8][64]frontend.Variable
186
187func unpack8(x Array8_64) ([64]frontend.Variable, [64]frontend.Variable, [64]frontend.Variable, [64]frontend.Variable, [64]frontend.Variable, [64]frontend.Variable, [64]frontend.Variable, [64]frontend.Variable) {
188 return x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]
189}
190
191func flatten8(x Array8_64) [512]frontend.Variable {
192 var result [512]frontend.Variable
193 k := 0
194 for i := 0; i < 8; i++ {
195 for j := 0; j < 64; j++ {
196 result[k] = x[i][j]
197 k++
198 }
199 }
200 return result
201}
202
203func uint64ToBits(value uint64) [64]frontend.Variable {
204 var result [64]frontend.Variable
205 for k := 0; k < 64; k++ {
206 if (value & (1 << (63 - k))) != 0 {
207 result[k] = 1
208 } else {
209 result[k] = 0
210 }
211 }
212 return result
213}
214
215func xor(api frontend.API, args ...[64]frontend.Variable) [64]frontend.Variable {
216 if len(args) == 1 {
217 return args[0]
218 } else {
219 return xor2(api, args[0], xor(api, args[1:]...))
220 }
221}
222func add(api frontend.API, args ...[64]frontend.Variable) [64]frontend.Variable {
223 if len(args) == 1 {
224 return args[0]
225 } else {
226 return add2(api, args[0], add(api, args[1:]...))
227 }
228}
229func and(api frontend.API, args ...[64]frontend.Variable) [64]frontend.Variable {
230 if len(args) == 1 {
231 return args[0]
232 } else {
233 return and2(api, args[0], and(api, args[1:]...))
234 }
235}
236
237func xor2(api frontend.API, a, b [64]frontend.Variable) [64]frontend.Variable {
238 var result [64]frontend.Variable
239 for i := 0; i < 64; i++ {
240 result[i] = api.Xor(a[i], b[i])
241 }
242 return result
243}
244
245func and2(api frontend.API, a, b [64]frontend.Variable) [64]frontend.Variable {
246 var result [64]frontend.Variable
247 for i := 0; i < 64; i++ {
248 result[i] = api.And(a[i], b[i])
249 }
250 return result
251}
252
253func add2(api frontend.API, a, b [64]frontend.Variable) [64]frontend.Variable {
254 var result [64]frontend.Variable
255 var carry frontend.Variable = 0
256 for i := 63; i >= 0; i-- {
257 sum := api.Add(a[i], b[i], carry)
258 sumBin := api.ToBinary(sum, 2)
259 if len(sumBin) != 2 {
260 panic("bad length")
261 }
262 result[i] = sumBin[0]
263 carry = sumBin[1]
264 }
265 return result
266}
267
268func _shr(n [64]frontend.Variable, bits int) [64]frontend.Variable {
269 var result [64]frontend.Variable
270 for i := 0; i < 64; i++ {
271 if i < bits {
272 result[i] = 0
273 } else {
274 result[i] = n[i-bits]
275 }
276 }
277 return result
278}
279
280func not(api frontend.API, n [64]frontend.Variable) [64]frontend.Variable {
281 var result [64]frontend.Variable
282 for i := 0; i < 64; i++ {
283 result[i] = api.Sub(1, n[i])
284 }
285 return result
286}
287
288func divChecked(a, b int) int {
289 if a%b != 0 {
290 panic("divChecked: does not divide evenly")
291 }
292 return a / b
293}