Skip to content

Commit

Permalink
fixup! feat: increase leaf size
Browse files Browse the repository at this point in the history
  • Loading branch information
mpernambuco committed Aug 1, 2024
1 parent 4ff6d68 commit 1ba38d9
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 13 deletions.
8 changes: 4 additions & 4 deletions src/AccessLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -97,8 +97,8 @@ library AccessLogs {
bytes32 readData = a.buffer.consumeBytes32();
bytes32 valHash = keccak256(abi.encodePacked(readData));

Memory.PhysicalAddress leafAddress = readAddress.truncateToLeaf();
uint64 offset = readAddress.minus(leafAddress);
(Memory.PhysicalAddress leafAddress, uint64 offset) =
readAddress.truncateToLeaf();
bytes8 readValue = bytes8(readData << (offset << 3));

bytes32 expectedValHash =
Expand Down Expand Up @@ -145,8 +145,8 @@ library AccessLogs {
) internal pure {
bytes32 writtenData = a.buffer.consumeBytes32();

Memory.PhysicalAddress leafAddress = writeAddress.truncateToLeaf();
uint64 offset = writeAddress.minus(leafAddress);
(Memory.PhysicalAddress leafAddress, uint64 offset) =
writeAddress.truncateToLeaf();
uint64 expectedNewValue =
machineWordToSolidityUint64(bytes8(writtenData << (offset << 3)));

Expand Down
7 changes: 5 additions & 2 deletions src/Memory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ library Memory {

type AlignedSize is uint8;

uint8 constant LOG2_WORD = 3;
uint8 constant LOG2_LEAF = 5;
uint8 constant LOG2_MAX_SIZE = 64 - LOG2_LEAF;
uint64 constant LEAF_MASK = uint64(1 << LOG2_LEAF) - 1;
Expand Down Expand Up @@ -142,10 +143,12 @@ library Memory {
function truncateToLeaf(PhysicalAddress addr)
internal
pure
returns (PhysicalAddress)
returns (PhysicalAddress, uint64)
{
uint64 r = Memory.PhysicalAddress.unwrap(addr) & ~LEAF_MASK;
return Memory.PhysicalAddress.wrap(r);
PhysicalAddress truncated = Memory.PhysicalAddress.wrap(r);
uint64 offset = minus(addr, truncated);
return (truncated, offset);
}

function minus(PhysicalAddress lhs, PhysicalAddress rhs)
Expand Down
6 changes: 2 additions & 4 deletions templates/AccessLogs.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,7 @@ library AccessLogs {
bytes32 readData = a.buffer.consumeBytes32();
bytes32 valHash = keccak256(abi.encodePacked(readData));

Memory.PhysicalAddress leafAddress = readAddress.truncateToLeaf();
uint64 offset = readAddress.minus(leafAddress);
(Memory.PhysicalAddress leafAddress, uint64 offset) = readAddress.truncateToLeaf();
bytes8 readValue = bytes8(readData << (offset << 3));

bytes32 expectedValHash =
Expand Down Expand Up @@ -158,8 +157,7 @@ library AccessLogs {
) internal pure {
bytes32 writtenData = a.buffer.consumeBytes32();

Memory.PhysicalAddress leafAddress = writeAddress.truncateToLeaf();
uint64 offset = writeAddress.minus(leafAddress);
(Memory.PhysicalAddress leafAddress, uint64 offset) = writeAddress.truncateToLeaf();
uint64 expectedNewValue = machineWordToSolidityUint64(bytes8(writtenData << (offset << 3)));

require(newValue == expectedNewValue, "Access log value does not contain the expected written value");
Expand Down
7 changes: 4 additions & 3 deletions test/AccessLogs.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ contract AccessLogsTest is Test {
{
bytes32 b32 = makeLeaf(word, position);
Buffer.Context memory buffer =
Buffer.Context(new bytes((59 << 5) + 32 + 32), 0);
buffer.writeBytes32(b32); // leaf containing the readd word
Buffer.Context(new bytes((59 << Memory.LOG2_LEAF) + 32 + 32), 0);
buffer.writeBytes32(b32); // leaf containing the read word

for (uint256 i = 0; i < 60; i++) {
buffer.writeBytes32(hashes[i]);
Expand All @@ -170,7 +170,8 @@ contract AccessLogsTest is Test {
}

function rootFromHashes(bytes32 drive) private view returns (bytes32) {
Buffer.Context memory buffer = Buffer.Context(new bytes(59 << 5), 0);
Buffer.Context memory buffer =
Buffer.Context(new bytes(59 << Memory.LOG2_LEAF), 0);

for (uint256 i = 0; i < 59; i++) {
buffer.writeBytes32(hashes[i + 1]);
Expand Down

0 comments on commit 1ba38d9

Please sign in to comment.