-
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 all 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 |
|---|---|---|
|
|
@@ -8,6 +8,7 @@ import ( | |
| "fmt" | ||
|
|
||
| "github.com/consensys/gnark/frontend" | ||
| "github.com/consensys/gnark/std/lookup/logderivlookup" | ||
| "github.com/consensys/gnark/std/math/uints" | ||
| ) | ||
|
|
||
|
|
@@ -26,6 +27,13 @@ type FieldHasher interface { | |
| Reset() | ||
| } | ||
|
|
||
| // DynamicLengthFieldHasher can compute hashes of lengths unknown at compile time. | ||
| type DynamicLengthFieldHasher interface { | ||
| FieldHasher | ||
| // SumWithLength computes the hash of the first l inputs written into the hash. | ||
| SumWithLength(l frontend.Variable) frontend.Variable | ||
| } | ||
|
|
||
| // StateStorer allows to store and retrieve the state of a hash function. | ||
| type StateStorer interface { | ||
| FieldHasher | ||
|
|
@@ -108,48 +116,68 @@ type Compressor interface { | |
| } | ||
|
|
||
| type merkleDamgardHasher struct { | ||
| state frontend.Variable | ||
| iv frontend.Variable | ||
| f Compressor | ||
| api frontend.API | ||
| state []frontend.Variable // state after being updated with each written element | ||
| stateTable logderivlookup.Table // stateTable always contains a prefix of h.state | ||
| stateTableLen int | ||
|
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. Please comment what is |
||
| f Compressor | ||
| api frontend.API | ||
| } | ||
|
|
||
| // NewMerkleDamgardHasher transforms a 2-1 one-way function into a hash | ||
| // initialState is a value whose preimage is not known | ||
| // NewMerkleDamgardHasher range-extends a 2-1 one-way hash compression function into a hash by way of the Merkle-Damgård construction. | ||
| // Parameters: | ||
| // - api: constraint builder | ||
| // - f: 2-1 hash compression (one-way) function | ||
| // - initialState: the initialization vector (IV) in the Merkle-Damgård chain. It must be a value whose preimage is not known. | ||
|
Tabaie marked this conversation as resolved.
|
||
| func NewMerkleDamgardHasher(api frontend.API, f Compressor, initialState frontend.Variable) StateStorer { | ||
|
Tabaie marked this conversation as resolved.
|
||
| return &merkleDamgardHasher{ | ||
| state: initialState, | ||
| iv: initialState, | ||
| state: []frontend.Variable{initialState}, | ||
| f: f, | ||
| api: api, | ||
| } | ||
| } | ||
|
|
||
| func (h *merkleDamgardHasher) Reset() { | ||
| h.state = h.iv | ||
| h.state = h.state[:1] | ||
| h.stateTableLen = 0 | ||
| h.stateTable = nil | ||
| } | ||
|
|
||
| func (h *merkleDamgardHasher) Write(data ...frontend.Variable) { | ||
| for _, d := range data { | ||
| h.state = h.f.Compress(h.state, d) | ||
| h.state = append(h.state, h.f.Compress(h.state[len(h.state)-1], d)) | ||
| } | ||
| } | ||
|
|
||
| func (h *merkleDamgardHasher) Sum() frontend.Variable { | ||
| return h.state | ||
| return h.state[len(h.state)-1] | ||
| } | ||
|
|
||
| // SumWithLength computes the Merkle-Damgård hash of the input data, truncated at the given length. | ||
| // Parameters: | ||
| // - length: length of the prefix of data to be hashed. The verifier will not accept a value outside the range {0, 1, ..., len(data)}. | ||
| // The gnark prover will refuse to attempt to generate such an unsuccessful proof. | ||
| func (h *merkleDamgardHasher) SumWithLength(length frontend.Variable) frontend.Variable { | ||
| if h.stateTable == nil { | ||
| h.stateTable = logderivlookup.New(h.api) | ||
| } | ||
| for h.stateTableLen < len(h.state) { | ||
| h.stateTable.Insert(h.state[h.stateTableLen]) | ||
| h.stateTableLen++ | ||
| } | ||
| return h.stateTable.Lookup(length)[0] | ||
| } | ||
|
|
||
| func (h *merkleDamgardHasher) State() []frontend.Variable { | ||
| return []frontend.Variable{h.state} | ||
| return []frontend.Variable{h.state[len(h.state)-1]} | ||
| } | ||
|
|
||
| func (h *merkleDamgardHasher) SetState(state []frontend.Variable) error { | ||
| if h.state != h.iv { | ||
| return fmt.Errorf("the hasher is not in an initial state; reset before attempting to set the state") | ||
| } | ||
| if len(state) != 1 { | ||
| return fmt.Errorf("expected one state variable, got %d", len(state)) | ||
| } | ||
| h.state = state[0] | ||
| if len(h.state) != 1 { | ||
| return fmt.Errorf("the hasher is not in an initial state; reset before attempting to set the state") | ||
| } | ||
| h.state = append(h.state, state[0]) | ||
|
cursor[bot] marked this conversation as resolved.
|
||
| return nil | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,51 +1,81 @@ | ||
| 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" | ||
| "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 | ||
| } | ||
| varlen := hsh.(hash.DynamicLengthFieldHasher) | ||
|
|
||
| hsh, err = poseidon2.New(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) | ||
|
|
||
| varlen.Write(c.Input...) | ||
|
|
||
| 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], varlen.SumWithLength(i+1)) | ||
| } | ||
|
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.
I would keep consistency with
BinaryFixedLengthHasher. Also see the interface description there which imo is more thorough.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.
So to call it "fixed length"? That seems like the exact opposite of what it does.
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.
We fixed the input length at solving time? Dunno, perhaps it could be better, but imo it also is not wrong.
My point is that we already have an existing interface which does the same thing (but when we get the inputs as bytes instead of field elements), so for me it is logical that the new interface has similar naming. And for boolean hashes the "dynamic length" is also confusing as it could refer to the output length of the hash (as in SHAKE). What is important is that the methods are well documented and explained, we shouldn't rely on the method name only to pass on all information.