-
Notifications
You must be signed in to change notification settings - Fork 1
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
Hashing without memory allocations #2
base: main
Are you sure you want to change the base?
Conversation
uint256 constant scratchSpacePtr = 0x00; | ||
/** | ||
* @dev We call word the typical unit size for operands of EVM instructions: 32 bytes. | ||
*/ | ||
uint256 constant wordSize = 32; |
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 typically CAPITALIZE constants, don't we?
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.
Hm, yeah, I guess.
) internal pure returns (bytes32 hash) { | ||
assembly ("memory-safe") { | ||
// The length of the bytes type `length` field is that of a word in memory | ||
let data := add(add(encoded, offset), 0x20) |
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.
Should use the word size constant.
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 was actually about to use the memoryWord
(or MEMORY_WORD
?) constant instead since this refers to size of a type in memory while the other one is about the typical size of an operand. The distinction is very small but it could bite us in the future if we're not careful with these constants 🤔
Ah, in any case, I ended up not including that constant because it was defined by the pay
function which I didn't include here but I could just add that constant by itself instead.
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 mean, at some point the memory could be accessed in sizes other than 32 bytes; in fact, you can access it byte by byte, although I don't think the compiler ever does that.
This adds a few functions that allow the user to hash without extra memory allocations.