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

feat: increase tree leaf log2 size to 5 #68

Merged
merged 6 commits into from
Aug 11, 2024
Merged

Conversation

mpernambuco
Copy link
Contributor

@mpernambuco mpernambuco commented Jul 22, 2024

This branch updates access logs handling and tests according to the emulator changes introduced in this PR:

cartesi/machine-emulator#259

In the emulator, the Merkle tree word size is increased from 8 to 32 bytes.

The access log format has changed as follows:

  • The value field is now 32 bytes long and contains the Merkle tree leaf where the accessed word is located.
  • To locate the relevant 64-bit word inside data, we compute the offset of address relative to the leaf-aligned address.
  • The sibling_hashes field now has 59 entries.

@vfusco vfusco added this to the v0.11.0 milestone Jul 23, 2024
@GCdePaula
Copy link
Contributor

Hey @mpernambuco, let me know if I can help in any way!

Current implementation has 64-bit words as the unit requested by step, and also the unit that we read from our access log. With the change in this PR, it seems there's a break: step requests words, but internally our memory model has larger blocks.

My suggestion is that we make better distinctions between "words" (64-bit) and "blocks"/"leaves" (currently 256-bit), and that we extend the Memory module to handle "words" vs "blocks" (and move some of the address manipulation there). I can help here if you want!

@diegonehab
Copy link
Contributor

Hey @mpernambuco, let me know if I can help in any way!

Current implementation has 64-bit words as the unit requested by step, and also the unit that we read from our access log. With the change in this PR, it seems there's a break: step requests words, but internally our memory model has larger blocks.

My suggestion is that we make better distinctions between "words" (64-bit) and "blocks"/"leaves" (currently 256-bit), and that we extend the Memory module to handle "words" vs "blocks" (and move some of the address manipulation there). I can help here if you want!

I don't understand the point you are trying to make. We are changing everything to consistently deal with 256-bit words and forget we ever dealt with 64-bit words. The machine emulator is changing, so the log will change, and the memory module will change, and the solidity step will change.

@mpernambuco mpernambuco force-pushed the increase-word-log2-size branch from 09baaae to 4f9ee32 Compare July 24, 2024 15:12
@GCdePaula
Copy link
Contributor

GCdePaula commented Jul 25, 2024

@diegonehab

I don't understand the point you are trying to make.

It's not a point, it's a Solidity code structure suggestion and an offer for help. I didn't want to barge in.

The current Solidity code is such that words are requested from step, words are read from the log, and words are the leafs of the Merkle tree. We're changing some of that. My suggestion is that there's a nicer way to structure this that makes the code clearer.

As an example, there's a function called strideFromWordAddress, which reading the PR appears to be changed to read a 256-bit memory block. Perhaps it's clearer if we renamed it to strideFromBlockAddress or strideFromLeafAddress. This is minor.

A change that perhaps is more important is understanding the meaning of Region to be the size of a leaf (which used to be words, so no difference before). Then AlignedSize becomes a BlockAlignedSize or LeafAlignedSize (which used to be words, so no difference before).

This way, the function strideFromPhysicalAddress should be updated such that the address alignment verification takes into consideration the correct size of the leaf (which used to be words, so no difference before).

The way I'd do it is start by changing what AlignedSize means, to match the leaf. Probably even change the name to indicate that. We can make this "configurable" (uint8 constant LOG2_LEAF = 5;). Then I guess it's just dealing with the cascading changes.

@mpernambuco
Copy link
Contributor Author

Hey @mpernambuco, let me know if I can help in any way!

Current implementation has 64-bit words as the unit requested by step, and also the unit that we read from our access log. With the change in this PR, it seems there's a break: step requests words, but internally our memory model has larger blocks.

My suggestion is that we make better distinctions between "words" (64-bit) and "blocks"/"leaves" (currently 256-bit), and that we extend the Memory module to handle "words" vs "blocks" (and move some of the address manipulation there). I can help here if you want!

Hey @GCdePaula,
Yes, thank you! I will need your help to improve these changes.
This is still a draft.
I wanted to ensure that the new access logs format can be handled by the Solidity code.

@mpernambuco mpernambuco marked this pull request as ready for review July 31, 2024 11:37
Makefile Outdated Show resolved Hide resolved
Copy link
Contributor

@stephenctw stephenctw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is looking great! I only have a few minor comments.

src/Memory.sol Outdated Show resolved Hide resolved
templates/AccessLogs.sol.template Outdated Show resolved Hide resolved
templates/AccessLogs.sol.template Outdated Show resolved Hide resolved
test/AccessLogs.t.sol Outdated Show resolved Hide resolved
test/AccessLogs.t.sol Outdated Show resolved Hide resolved
test/AccessLogs.t.sol Outdated Show resolved Hide resolved
@mpernambuco mpernambuco force-pushed the increase-word-log2-size branch from fb79fe8 to 5340fa7 Compare August 1, 2024 22:47
@mpernambuco mpernambuco requested a review from stephenctw August 1, 2024 23:41
stephenctw
stephenctw previously approved these changes Aug 2, 2024
Copy link
Contributor

@GCdePaula GCdePaula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think there's an exploit. Let me know what you think.

src/AccessLogs.sol Outdated Show resolved Hide resolved
@GCdePaula
Copy link
Contributor

I've opened #70 (a quick draft) of what I had in mind for reading. Let me know what you think.

@diegonehab @mpernambuco @stephenctw

Copy link
Contributor

@stephenctw stephenctw left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! @GCdePaula is there anything else you'd like to add/change?

Copy link
Contributor

@GCdePaula GCdePaula left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🚀🚀🚀

@mpernambuco mpernambuco merged commit 8c4bc18 into main Aug 11, 2024
2 checks passed
@mpernambuco mpernambuco deleted the increase-word-log2-size branch August 11, 2024 15:01
@vfusco vfusco modified the milestones: v0.11.0, v0.12.0 Aug 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: Done
Development

Successfully merging this pull request may close these issues.

5 participants