-
Notifications
You must be signed in to change notification settings - Fork 518
feat: SumMerkleDamgardDynamicLength
#1634
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 7 commits
5e3c670
ead8711
1ae9c91
976f197
c9dd1c3
49c11f4
ae54e28
fad6bbb
e899bb5
f58211d
92b24a3
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,79 @@ | ||
| package poseidon2_test | ||
|
|
||
| import ( | ||
| "fmt" | ||
| "testing" | ||
|
|
||
| "github.com/consensys/gnark-crypto/ecc" | ||
| "github.com/consensys/gnark-crypto/ecc/bls12-377/fr" | ||
| poseidonbls12377 "github.com/consensys/gnark-crypto/ecc/bls12-377/fr/poseidon2" | ||
| "github.com/consensys/gnark/frontend" | ||
| "github.com/consensys/gnark/std/hash" | ||
| "github.com/consensys/gnark/std/hash/poseidon2" | ||
| gkr_poseidon2 "github.com/consensys/gnark/std/hash/poseidon2/gkr-poseidon2" | ||
| permutation "github.com/consensys/gnark/std/permutation/poseidon2/gkr-poseidon2" | ||
| "github.com/consensys/gnark/test" | ||
| ) | ||
|
|
||
| type poseidon2Circuit struct { | ||
| Input []frontend.Variable | ||
| Expected frontend.Variable `gnark:",public"` | ||
| Expected []frontend.Variable `gnark:",public"` // Expected[i] = H(Input[:i+1]) | ||
| } | ||
|
|
||
| func (c *poseidon2Circuit) Define(api frontend.API) error { | ||
| if len(c.Input) != len(c.Expected) { | ||
| return fmt.Errorf("length mismatch") | ||
| } | ||
| hsh, err := poseidon2.New(api) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| compressor, err := permutation.NewCompressor(api) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| gkr, err := gkr_poseidon2.New(api) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| hsh.Write(c.Input...) | ||
| api.AssertIsEqual(hsh.Sum(), c.Expected) | ||
| gkr.Write(c.Input...) | ||
| api.AssertIsEqual(gkr.Sum(), c.Expected) | ||
|
|
||
| for i := range c.Input { | ||
| hsh.Write(c.Input[i]) | ||
| api.AssertIsEqual(c.Expected[i], hsh.Sum()) | ||
| gkr.Write(c.Input[i]) | ||
| api.AssertIsEqual(c.Expected[i], gkr.Sum()) | ||
| api.AssertIsEqual(c.Expected[i], hash.SumMerkleDamgardDynamicLength(api, compressor, 0, i+1, c.Input)) | ||
| } | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'l also add test if you're trying to read hash digest not from the end, but from earlier. A la |
||
| return nil | ||
| } | ||
|
|
||
| func TestPoseidon2Hash(t *testing.T) { | ||
| assert := test.NewAssert(t) | ||
|
|
||
| var buf [fr.Bytes]byte | ||
| const nbInputs = 5 | ||
| // prepare expected output | ||
| h := poseidonbls12377.NewMerkleDamgardHasher() | ||
| circInput := make([]frontend.Variable, nbInputs) | ||
| for i := range nbInputs { | ||
| _, err := h.Write([]byte{byte(i)}) | ||
| expected := make([]frontend.Variable, nbInputs) | ||
| input := make([]frontend.Variable, nbInputs) | ||
| for i := range input { | ||
| buf[fr.Bytes-1] = byte(i) | ||
| _, err := h.Write(buf[:]) | ||
| assert.NoError(err) | ||
| circInput[i] = i | ||
| input[i] = i | ||
| expected[i] = h.Sum(nil) | ||
| } | ||
| 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 | ||
|
|
||
| assert.CheckCircuit( | ||
| &poseidon2Circuit{ | ||
| Input: make([]frontend.Variable, nbInputs), | ||
| Expected: make([]frontend.Variable, nbInputs), | ||
| }, test.WithValidAssignment(&poseidon2Circuit{ | ||
| Input: input, | ||
| Expected: expected, | ||
| }), test.WithCurves(ecc.BLS12_377)) | ||
| } | ||
|
|
||
| func TestStateStorer(t *testing.T) { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would also add SumWithLen test to the state storer circuit. |
||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please comment what is
stateTableLen: "// stateTableLen indicates the length of ofstateentries written tostateTable. It will be updated with every read toDynamicSum()" etc.