Skip to content
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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from

Conversation

scnale
Copy link
Collaborator

@scnale scnale commented Aug 8, 2024

This adds a few functions that allow the user to hash without extra memory allocations.

Comment on lines +24 to +28
uint256 constant scratchSpacePtr = 0x00;
/**
* @dev We call word the typical unit size for operands of EVM instructions: 32 bytes.
*/
uint256 constant wordSize = 32;
Copy link
Collaborator

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?

Copy link
Collaborator Author

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)
Copy link
Collaborator

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.

Copy link
Collaborator Author

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.

Copy link
Collaborator Author

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants