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