FEATURE: Allow custom hash functions to Internal library of LeanIMT#59
Open
jimjimvalkema wants to merge 12 commits intozk-kit:mainfrom
Open
FEATURE: Allow custom hash functions to Internal library of LeanIMT#59jimjimvalkema wants to merge 12 commits intozk-kit:mainfrom
jimjimvalkema wants to merge 12 commits intozk-kit:mainfrom
Conversation
…brary calling poseidon through an interface instead of a library to save gas
InternalLeanIMT can now use different hash functions by passing in a hash function as a parameter toevery function that uses the hash function. LeanIMT does this for poseidonT3. BREAKING CHANGE: InternalLeanIMTs functions requires additional inputs. Errors mention hasherLimit instead of SNARK_SCALAR_FIELD.
…d comments of insertMany
7 tasks
…instead of an address Switched to passing the hash function as a function instead of an address and fixed some of the readability of _insertMany. Also added a regular insert to the insertMany test in order to catch issues with self.sideNodes being off. BREAKING CHANGE: switched to passing the hash function as a function instead of an address
Author
|
I managed to fix the readability of insertMany by packing hasherLimit + hasher() into a struct.
I also feel confident now this pr can be reviewed! |
This file contains hidden or 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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
I changed the internal library of LeanIMT so the user can provide their own hash function as a parameter.
The
LeanIMT.sollibrary is setup so it uses the poseidon hash function so everything behaves as usual.I used this to make a poseidon2 IMT (the new poseidon who is faster to prove) here: https://github.com/jimjimvalkema/EIP7503-ERC20/blob/55732aedac7b82e11769f1de7869373736a08734/contracts/leanIMTPoseidon2.sol#L13-L22
I also made some bench marks with hash functions like SHA256, poseidon2, etc:
https://github.com/jimjimvalkema/zkkit-hashfunc-bench-marks/tree/main?tab=readme-ov-file#proof-time-16-depth-tree
changes made
InternalLeanIMT.IHasher(hasher).hash([left,right])(this also saves gas).hasher, SNARK_SCALER_LIMTas added arguments to functions that need them.hasheraddress as a constant and set the hasherLimithere is a snipet:
in
InternalLeanIMT.solThen this is used as follows in
LeanIMT.solinsertMany changes
I ran into a stack too deep error and had to make some minor changes to get around it.
numberOfNewNodesand instead calculated it on the spotleftNoderightNodeand just used an array namedhasherInputtest changes
I changed the deploy-imt-test task to deploy poseidon with the deterministic proxy so it always deploys to the same address. See in the poseidon-solidity readme
Breaking changes
I changed error that mention
SNARK_SCALAR_LIMITto instead sayhasherLimit, since the hash function is now generic. This might break error handling for users that expect the old error.LeanIMT now uses the create2 address of poseidonT3 directly, so users using a chain where it isn't deployed yet (or hardhat tests), will need to deploy it them selfs now.
If an user used the Internal version of the libraries for some reason. Then they do have now pass these new parameters like
_hasher,_defaultZerosandhasherLimit.If an user used the Internal version of the libraries for some reason. Then they do have now pass these new parameters like
hasher,hasherLimitand_defaultZeros.Related Issue(s)
Closes #56
Other information
Calling poseidon with a interface saves from 10k - 100k gas for most functions. But making the libraries generic does add 0~100 gas on average.

In total expect a gas saving of 10k gas on average!
Checklist
yarn stylewithout getting any errorsImportant
We do not accept pull requests for minor grammatical fixes (e.g., correcting typos, rewording sentences) or for fixing broken links, unless they significantly improve clarity or functionality. These contributions, while appreciated, are not a priority for merging. If you notice any of these issues, please create a GitHub Issue to report them so they can be properly tracked and addressed.