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