arnaucube
arnaucube/gnark-plonky2-verifierPublic Self-hosted repository
gnark-plonky2-verifier/sha512/sha_test.go
sha512/sha_test.goRaw
 1package sha512
 2
 3import (
 4	"testing"
 5
 6	"github.com/consensys/gnark/frontend"
 7	"github.com/consensys/gnark/test"
 8)
 9
10type Sha512Circuit struct {
11	in []frontend.Variable `gnark:"in"`
12	out []frontend.Variable `gnark:"out"`
13}
14
15func (circuit *Sha512Circuit) Define(api frontend.API) error {
16	res := Sha512(api, circuit.in)
17	for i := 0; i < 512; i++ {
18		api.AssertIsEqual(res[i], circuit.out[i])
19	}
20	return nil
21}
22
23func TestSha512(t *testing.T) {
24	assert := test.NewAssert(t)
25	in := toBytes("Succinct Labs")
26	out := toBytes("503ace098aa03f6feec1b5df0a38aee923f744a775508bc81f2b94ad139be297c2e8cd8c44af527b5d3f017a7fc929892c896604047e52e3f518924f52bff0dc")
27	circuit := Sha512Circuit {
28		in: toVariables(make([]byte, len(in))),
29		out: toVariables(make([]byte, len(out))),
30	}
31	witness := Sha512Circuit {
32		in: toVariables(in),
33		out: toVariables(out),
34	}
35	err := test.IsSolved(&circuit, &witness, testCurve.ScalarField())
36	assert.NoError(err)
37}
38
39func toVariables(arr []byte) []frontend.Variable {
40	result := make([]frontend.Variable, len(arr))
41	for i, v := range arr {
42		result[i] = v
43	}
44	return result
45}
46
47func toBytes(s string) []byte {
48	return []byte(s)
49}