gnark-plonky2-verifier/sha512/sha_test.go
sha512/sha_test.goRaw
1package sha512
2
3import (
4 "testing"
5 "encoding/hex"
6
7 "github.com/consensys/gnark/frontend"
8 "github.com/consensys/gnark/test"
9)
10
11type Sha512Circuit struct {
12 in []frontend.Variable `gnark:"in"`
13 out []frontend.Variable `gnark:"out"`
14}
15
16func (circuit *Sha512Circuit) Define(api frontend.API) error {
17 res := PySha512(api, circuit.in)
18 if len(res) != 512 { panic("bad length") }
19 for i := 0; i < 512; i++ {
20 api.AssertIsEqual(res[i], circuit.out[i])
21 }
22 return nil
23}
24
25func TestSha512(t *testing.T) {
26 assert := test.NewAssert(t)
27
28 testCase := func(input, output string) {
29 in := toBytes(input)
30 out, err := hex.DecodeString(output)
31 if err != nil { panic(err) }
32 if len(out) != 512 / 8 { panic("bad output length") }
33
34 circuit := Sha512Circuit {
35 in: toBits(in),
36 out: toBits(out),
37 }
38 witness := Sha512Circuit {
39 in: toBits(in),
40 out: toBits(out),
41 }
42 err = test.IsSolved(&circuit, &witness, testCurve.ScalarField())
43 assert.NoError(err)
44 }
45
46 testCase("", "cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e")
47 testCase("Succinct Labs", "503ace098aa03f6feec1b5df0a38aee923f744a775508bc81f2b94ad139be297c2e8cd8c44af527b5d3f017a7fc929892c896604047e52e3f518924f52bff0dc")
48}
49
50func toBits(arr []byte) []frontend.Variable {
51 result := make([]frontend.Variable, len(arr) * 8)
52 for i, v := range arr {
53 for j := 0; j < 8; j++ {
54 if (v & (1 << (7-j))) != 0 {
55 result[i*8+j] = 1
56 } else {
57 result[i*8+j] = 0
58 }
59 }
60 }
61 return result
62}
63
64func toBytes(s string) []byte {
65 return []byte(s)
66}