feat: add typed field hasher interface in MiMC package#752
Conversation
There was a problem hiding this comment.
Pull Request Overview
This PR adds a new FieldHasher interface to MiMC packages to enable direct work with field elements instead of requiring byte conversion. This provides a more convenient API for cryptographic operations that naturally work with field elements.
- Introduces the
FieldHasherinterface withWriteElementandSumElementmethods - Updates
NewMiMC()to returnFieldHasherinstead ofhash.StateStorer - Improves memory efficiency by using slice reslicing instead of setting to nil
Reviewed Changes
Copilot reviewed 18 out of 18 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| internal/generator/crypto/hash/mimc/template/mimc.go.tmpl | Template for MiMC implementation with new FieldHasher interface and methods |
| internal/generator/crypto/hash/mimc/template/tests/mimc_test.go.tmpl | Template for tests verifying FieldHasher functionality |
| ecc/*/fr/mimc/mimc.go | Generated MiMC implementations for various elliptic curves with FieldHasher interface |
| ecc/*/fr/mimc/mimc_test.go | Generated tests for FieldHasher functionality across different curves |
Tip: Customize your code reviews with copilot-instructions.md. Create the file or learn how to get started.
ivokub
left a comment
There was a problem hiding this comment.
I think the PR makes sense and I think the implementation is good.
I'm just not 100% sure if the interfaces make sense. The problem could be if someone has some kind of switch to initialize the hasher, then because NewMiMC in every pacakge now returns different interfaces, then it wouldn't work anymore.
I'd rather instead would define new constructor NewFieldHasher which returns FieldHasher interface (which we can now simplify to be Interface {Write(fr.Element) Sum() fr.Element} and for clarity maybe NewBinaryHasher which is the same as NewMiMC (and this we keep around).
This also should cover potential pain-point when people would try to do mixed hashing (i.e. bytes and field elements).
Adds a interface in
mimcpackages to enable callers to directly work with field elements instead of dancing with bytes (e.g. linea monorepo).Note
Introduce a typed field-element MiMC hasher with WriteElement/SumElement/SumElements and supporting factories, update internals to reuse buffers, and add tests/templates for all curves.
FieldHasherinterface withWriteElement,SumElement, andSumElements.NewFieldHasher(field elements) andNewBinaryHasher(bytes).WriteElement,SumElement, andSumElementsindigestforbls12-377,bls12-381,bls24-315,bls24-317,bn254,bw6-633,bw6-761, andgrumpkin.Sumto reset internal buffer withd.data = d.data[:0].TestFieldHashervalidating equivalence of byte-based and element-based hashing, andSumElement/SumElementsbehavior for all curves.internal/generator/.../mimc.go.tmpl, tests) to generate the new API and behavior across curves.Written by Cursor Bugbot for commit f87fe4a. This will update automatically on new commits. Configure here.