|
1 | 1 | package poseidon2
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "github.com/consensys/gnark-crypto/ecc" |
5 | 4 | "testing"
|
6 | 5 |
|
| 6 | + "github.com/consensys/gnark-crypto/ecc" |
7 | 7 | "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/poseidon2"
|
8 | 8 | "github.com/consensys/gnark/frontend"
|
9 | 9 | "github.com/consensys/gnark/test"
|
10 |
| - "github.com/stretchr/testify/require" |
11 | 10 | )
|
12 | 11 |
|
| 12 | +type Poseidon2Circuit struct { |
| 13 | + Input []frontend.Variable |
| 14 | + Expected frontend.Variable `gnark:",public"` |
| 15 | +} |
| 16 | + |
| 17 | +func (c *Poseidon2Circuit) Define(api frontend.API) error { |
| 18 | + hsh, err := NewMerkleDamgardHasher(api) |
| 19 | + if err != nil { |
| 20 | + return err |
| 21 | + } |
| 22 | + hsh.Write(c.Input...) |
| 23 | + api.AssertIsEqual(hsh.Sum(), c.Expected) |
| 24 | + return nil |
| 25 | +} |
| 26 | + |
13 | 27 | func TestPoseidon2Hash(t *testing.T) {
|
| 28 | + assert := test.NewAssert(t) |
| 29 | + |
| 30 | + const nbInputs = 5 |
14 | 31 | // prepare expected output
|
15 | 32 | h := poseidon2.NewMerkleDamgardHasher()
|
16 |
| - for i := range 5 { |
| 33 | + circInput := make([]frontend.Variable, nbInputs) |
| 34 | + for i := range nbInputs { |
17 | 35 | _, err := h.Write([]byte{byte(i)})
|
18 |
| - require.NoError(t, err) |
| 36 | + assert.NoError(err) |
| 37 | + circInput[i] = i |
19 | 38 | }
|
20 | 39 | res := h.Sum(nil)
|
21 |
| - |
22 |
| - test.Function(func(api frontend.API) error { |
23 |
| - hsh, err := NewMerkleDamgardHasher(api) |
24 |
| - require.NoError(t, err) |
25 |
| - hsh.Write(0, 1, 2, 3, 4) |
26 |
| - api.AssertIsEqual(hsh.Sum(), res) |
27 |
| - return nil |
28 |
| - }, test.WithCurves(ecc.BLS12_377))(t) |
| 40 | + assert.CheckCircuit(&Poseidon2Circuit{Input: make([]frontend.Variable, nbInputs)}, test.WithValidAssignment(&Poseidon2Circuit{Input: circInput, Expected: res}), test.WithCurves(ecc.BLS12_377)) // we have parametrized currently only for BLS12-377 |
29 | 41 | }
|
0 commit comments