-
Notifications
You must be signed in to change notification settings - Fork 7
FEATURE: Allow custom hash functions to Internal libraries of: IMT, LeanIMT, LazyIMT #57
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
Conversation
internalBinaryIMT can use different hash functions by passing in a hash function as a parameter to every function that uses the hash function. BinaryIMT does this for poseidonT3.
renamed _hash to hasher for a more consistent naming
renamed _hash to hasher
InternalQuinaryIMT can use different hash functions by passing in a hash function as a parameter to
…er field checks SNARK_SCALAR_FIELD checks to outside of the internal library to allow non snark based
…se the hasher was pure fixed non solidity hash functions not being by making the hash function view instead of pure. This allows use cases where the developer needs to call a contract with .staticcall() directly to do a hash
initial measuremnt gas so we can see what changes after each commit
…ction InternalBinaryIMT can now be used with different hash functions given that the hash functions contract has the expected interface BREAKING CHANGE: InternalBinaryIMTs functions now needs additional parameters
quinary IMT can now be used with different hash functions given that the hash functions contract has the expected interface BREAKING CHANGE: InternalQuinaryIMTs functions now require additional parameters
Removed poseidon from the quinary and BinaryIMT and made the deploy:imt-test task deploy poseidon through nicks methond instead of a linked library BREAKING CHANGE: gas reports no longer include poseidon hashes
…ent hash functions moved to a new approach using an address to support different hash functions
…terThanHasherLimit renamed ValueGreaterThanSnarkScalarField to ValueGreaterThanHasherLimit since not every hash function is snark based BREAKING CHANGE: breaks error handeling that expects ValueGreaterThanSnarkScalarField
initial gas test
internalLazyImt can use different hash functions by passing in a hash function as a parameter to every function that uses the hash function. LazyIMT does this for poseidonT3. BREAKING CHANGE: InternalLazyIMT requires additional inputs. Errors mention hasherLimit instead of SNARK_SCALAR_FIELD
…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
…interfaces folder
| library LazyIMT { | ||
| using InternalLazyIMT for *; | ||
|
|
||
| address internal constant hasher = 0x3333333C0A88F9BE4fd23ed0536F9B6c427e3B93; |
Check warning
Code scanning / Slither
Conformance to Solidity naming conventions Warning
| import {SNARK_SCALAR_FIELD} from "./Constants.sol"; | ||
|
|
||
| library LeanIMT { | ||
| address internal constant hasher = 0x3333333C0A88F9BE4fd23ed0536F9B6c427e3B93; |
Check warning
Code scanning / Slither
Conformance to Solidity naming conventions Warning
packages/imt/contracts/BinaryIMT.sol
Outdated
| using InternalBinaryIMT for *; | ||
|
|
||
| // This is poseidonT3 from: https://github.com/chancehudson/poseidon-solidity | ||
| address internal constant hasher = 0x3333333C0A88F9BE4fd23ed0536F9B6c427e3B93; |
Check warning
Code scanning / Slither
Conformance to Solidity naming conventions Warning
|
|
||
| library QuinaryIMT { | ||
| using InternalQuinaryIMT for *; | ||
| address internal constant hasher = 0x666333F371685334CdD69bdDdaFBABc87CE7c7Db; |
Check warning
Code scanning / Slither
Conformance to Solidity naming conventions Warning
|
Sorry didnt catch that slither error! I renamed the constant |
Description
I changed the internal libraries of IMT, LeanIMT and LazyIMT so the user can provide their own hash function with an address that implements their hash function with the interface that is the same as PoseidonT3(T6 for QuinaryIMT). And also their own default zeros for IMT and LazyIMT.
I made the libraries that use the internal libraries use the poseidon so they behave like usual.
changes made
InternalBinaryIMT.IHasher(hasher).hash([left,right])(this also saves gas).hasher, _defaultZero, SNARK_SCALER_LIMTas added arguments to functions that need them.hasheraddress as a constant, add _defaultZero, and set the hasherLimithere is a snipet:
in
InternalBinaryIMTThen this is used as follows in
BinaryIMTtest changes
I changed the deploy-imt-test task to deploy poseidon with the determanistic proxy so it always deploys to the same address. See in the poseidon-solidity readme
Stack too deep error
My changes added a stack too deep error to
InternalLeanImt_insertMany. Which i resolved by deletingnumberOfNewNodesand calculating it on the spot instead. And also deletingnextLevelNewNodesand usingcurrentLevelNewNodesinstead. This reduced the readability but it did save a tiny bit of gas!You can see it here at line 158-178
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.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.