arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
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
  9
 10func _right_rotate(n [64]frontend.Variable, bits int) [64]frontend.Variable {
 11	var result [64]frontend.Variable
 12	for i := 0; i < len(n); i++ {
 13		result[(i+bits)%len(n)] = n[i]
 14	}
 15	return result
 16}
 17
 18func Sha512(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	var padding_len int
 89	if mdi < 112 {
 90		padding_len = 119 - mdi
 91	} else {
 92		padding_len = 247 - mdi
 93	}
 94	message_length_bits := uint64ToBits(uint64(len(in)))
 95	in = append(in, 1)
 96	for i := 0; i < 7; i++ {
 97		in = append(in, 0)
 98	}
 99	for i := 0; i < padding_len * 8; i++ {
100		in = append(in, 0)
101	}
102	for i := 0; i < 64; i++ {
103		in = append(in, message_length_bits[i])
104	}
105	
106	sha512_hash := Array8_64{
107		uint64ToBits(initial_hash[0]),
108		uint64ToBits(initial_hash[1]),
109		uint64ToBits(initial_hash[2]),
110		uint64ToBits(initial_hash[3]),
111		uint64ToBits(initial_hash[4]),
112		uint64ToBits(initial_hash[5]),
113		uint64ToBits(initial_hash[6]),
114		uint64ToBits(initial_hash[7]),
115	}
116	for chunk_start := 0; chunk_start < divChecked(len(in), 8); chunk_start += 128 {
117		chunk := in[chunk_start * 8 : (chunk_start+128) * 8]
118		if len(chunk) != 1024 { panic("bad length") }
119		u := make([]frontend.Variable, 80 * 64)
120		for i, _ := range u {
121			u[i] = 0
122		}
123		copy(u, chunk)
124		
125		w := reshape(u)
126		
127		for i := 16; i < 80; i++ {
128			s0 := _xor(
129                _right_rotate(w[i - 15], 1),
130                _right_rotate(w[i - 15], 8),
131                _shr(w[i - 15], 7),
132			)
133			s1 := _xor(
134                _right_rotate(w[i - 2], 19),
135                _right_rotate(w[i - 2], 61),
136                _shr(w[i - 2], 6),
137			)
138			w[i] = _add(w[i - 16], s0, w[i - 7], s1)
139		}
140		a, b, c, d, e, f, g, h := unpack8(sha512_hash)
141		for i := 0; i < 80; i++ {
142            sum1 := _xor(
143                _right_rotate(e, 14),
144                _right_rotate(e, 18),
145                _right_rotate(e, 41),
146            )
147			ch := _xor(_and(e, f), _and(_not(e), g))
148			temp1 := _add(h, sum1, ch, uint64ToBits(round_constants[i]), w[i])
149            sum0 := _xor(
150                _right_rotate(a, 28),
151                _right_rotate(a, 34),
152                _right_rotate(a, 39),
153            )
154			maj := _xor(_and(a, b), _and(a, c), _and(b, c))
155			temp2 := _add(sum0, maj)
156			
157            h = g
158            g = f
159            f = e
160            e = _add(d, temp1)
161            d = c
162            c = b
163            b = a
164            a = _add(temp1, temp2)
165		}
166		sha512_hash = zip_add(sha512_hash, Array8_64{a, b, c, d, e, f, g, h})
167	}
168	return flatten8(sha512_hash)
169}
170
171func reshape(u []frontend.Variable) [][64]frontend.Variable {
172	l := divChecked(len(u), 64)
173	result := make([][64]frontend.Variable, l)
174	for i := 0; i < l; i++ {
175		var arr [64]frontend.Variable
176		for k := 0; k < 64; k++ {
177			arr[k] = u[i*64+k]
178		}
179		result[i] = arr
180	}
181	return result
182}
183
184type Array8_64 [8][64]frontend.Variable
185
186func 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) {
187	return x[0], x[1], x[2], x[3], x[4], x[5], x[6], x[7]
188}
189
190func flatten8(x Array8_64) [512]frontend.Variable {
191	var result [512]frontend.Variable
192	k := 0
193	for i := 0; i < 8; i++ {
194		for j := 0; j < 64; j++ {
195			result[k] = x[i][j]
196			k++
197		}
198	}
199	return result
200}
201
202func uint64ToBits(value uint64) [64]frontend.Variable {
203	var result [64]frontend.Variable
204	for k := 0; k < 64; k++ {
205		if (value & (1 << (63-k))) != 0 {
206			result[k] = 1
207		} else {
208			result[k] = 0
209		}
210	}
211	return result
212}
213
214func xor(api frontend.API, args ...[64]frontend.Variable) [64]frontend.Variable {
215	if len(args) == 1 {
216		return args[0]
217	} else {
218		return xor2(api, args[0], xor(api, args[1:]...))
219	}
220}
221func add(api frontend.API, args ...[64]frontend.Variable) [64]frontend.Variable {
222	if len(args) == 1 {
223		return args[0]
224	} else {
225		return add2(api, args[0], add(api, args[1:]...))
226	}
227}
228func and(api frontend.API, args ...[64]frontend.Variable) [64]frontend.Variable {
229	if len(args) == 1 {
230		return args[0]
231	} else {
232		return and2(api, args[0], and(api, args[1:]...))
233	}
234}
235
236func xor2(api frontend.API, a, b [64]frontend.Variable) [64]frontend.Variable {
237	var result [64]frontend.Variable
238	for i := 0; i < 64; i++ {
239		result[i] = api.Xor(a[i], b[i])
240	}
241	return result
242}
243
244func and2(api frontend.API, a, b [64]frontend.Variable) [64]frontend.Variable {
245	var result [64]frontend.Variable
246	for i := 0; i < 64; i++ {
247		result[i] = api.And(a[i], b[i])
248	}
249	return result
250}
251
252func add2(api frontend.API, a, b [64]frontend.Variable) [64]frontend.Variable {
253	var result [64]frontend.Variable
254	var carry frontend.Variable = 0
255	for i := 63; i >= 0; i-- {
256		sum := api.Add(a[i], b[i], carry)
257		sumBin := api.ToBinary(sum, 2)
258		if len(sumBin) != 2 {
259			panic("bad length")
260		}
261		result[i] = sumBin[0]
262		carry = sumBin[1]
263	}
264	return result
265}
266
267func _shr(n [64]frontend.Variable, bits int) [64]frontend.Variable {
268	var result [64]frontend.Variable
269	for i := 0; i < 64; i++ {
270		if i < bits {
271			result[i] = 0
272		} else {
273			result[i] = n[i - bits]
274		}
275	}
276	return result
277}
278
279func not(api frontend.API, n [64]frontend.Variable) [64]frontend.Variable {
280	var result [64]frontend.Variable
281	for i := 0; i < 64; i++ {
282		result[i] = api.Sub(1, n[i])
283	}
284	return result
285}
286
287func divChecked(a, b int) int {
288	if a % b != 0 {
289		panic("divChecked: does not divide evenly")
290	}
291	return a / b
292}
293