-
Notifications
You must be signed in to change notification settings - Fork 421
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: merkle damgard and poseidon2 (#1407)
Co-authored-by: Arya Tabaie <15056835+Tabaie@users.noreply.github.com> Co-authored-by: Ivo Kubjas <ivo.kubjas@consensys.net>
- Loading branch information
1 parent
9388128
commit e55bdc2
Showing
7 changed files
with
353 additions
and
198 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
package poseidon2 | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/std/hash" | ||
poseidon2 "github.com/consensys/gnark/std/permutation/poseidon2" | ||
) | ||
|
||
// NewMerkleDamgardHasher returns a Poseidon2 hasher using the Merkle-Damgard | ||
// construction with the default parameters. | ||
func NewMerkleDamgardHasher(api frontend.API) (hash.FieldHasher, error) { | ||
f, err := poseidon2.NewPoseidon2(api) | ||
if err != nil { | ||
return nil, fmt.Errorf("could not create poseidon2 hasher: %w", err) | ||
} | ||
return hash.NewMerkleDamgardHasher(api, f, 0), nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
package poseidon2 | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/consensys/gnark-crypto/ecc" | ||
"github.com/consensys/gnark-crypto/ecc/bls12-377/fr/poseidon2" | ||
"github.com/consensys/gnark/frontend" | ||
"github.com/consensys/gnark/test" | ||
) | ||
|
||
type Poseidon2Circuit struct { | ||
Input []frontend.Variable | ||
Expected frontend.Variable `gnark:",public"` | ||
} | ||
|
||
func (c *Poseidon2Circuit) Define(api frontend.API) error { | ||
hsh, err := NewMerkleDamgardHasher(api) | ||
if err != nil { | ||
return err | ||
} | ||
hsh.Write(c.Input...) | ||
api.AssertIsEqual(hsh.Sum(), c.Expected) | ||
return nil | ||
} | ||
|
||
func TestPoseidon2Hash(t *testing.T) { | ||
assert := test.NewAssert(t) | ||
|
||
const nbInputs = 5 | ||
// prepare expected output | ||
h := poseidon2.NewMerkleDamgardHasher() | ||
circInput := make([]frontend.Variable, nbInputs) | ||
for i := range nbInputs { | ||
_, err := h.Write([]byte{byte(i)}) | ||
assert.NoError(err) | ||
circInput[i] = i | ||
} | ||
res := h.Sum(nil) | ||
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 | ||
} |
Oops, something went wrong.