Skip to content

Commit

Permalink
fix: change access logs
Browse files Browse the repository at this point in the history
  • Loading branch information
GCdePaula committed Aug 6, 2024
1 parent 1285a45 commit 34741cf
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 79 deletions.
25 changes: 14 additions & 11 deletions src/AccessLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -94,18 +94,21 @@ library AccessLogs {
AccessLogs.Context memory a,
Memory.PhysicalAddress readAddress
) internal pure returns (uint64) {
bytes32 readData = a.buffer.consumeBytes32();
bytes32 valHash = keccak256(abi.encodePacked(readData));

(Memory.PhysicalAddress leafAddress, uint64 offset) =
(Memory.PhysicalAddress leafAddress, uint64 wordOffset) =
readAddress.truncateToLeaf();
bytes8 readValue = getBytes8FromBytes32AtOffset(readData, offset);

bytes32 expectedValHash =
readLeaf(a, Memory.strideFromLeafAddress(leafAddress));
Memory.Region memory region = Memory.regionFromStride(
Memory.strideFromLeafAddress(leafAddress),
Memory.alignedSizeFromLog2(0)
);

bytes32 leaf = a.buffer.consumeBytes32();
bytes32 rootHash =
a.buffer.getRoot(region, keccak256(abi.encodePacked(leaf)));
require(a.currentRootHash == rootHash, "Read word root doesn't match");

require(valHash == expectedValHash, "Read value doesn't match");
return machineWordToSolidityUint64(readValue);
bytes8 word = getBytes8FromBytes32AtOffset(leaf, wordOffset);
return machineWordToSolidityUint64(word);
}

//
Expand Down Expand Up @@ -143,7 +146,7 @@ library AccessLogs {
Memory.PhysicalAddress writeAddress,
uint64 newValue
) internal pure {
(Memory.PhysicalAddress leafAddress, uint64 offset) =
(Memory.PhysicalAddress leafAddress, uint64 wordOffset) =
writeAddress.truncateToLeaf();

Memory.Region memory region = Memory.regionFromStride(
Expand All @@ -158,7 +161,7 @@ library AccessLogs {
require(a.currentRootHash == rootHash, "Write word root doesn't match");

bytes32 newLeaf = setBytes8ToBytes32AtOffset(
solidityUint64ToMachineWord(newValue), oldLeaf, offset
solidityUint64ToMachineWord(newValue), oldLeaf, wordOffset
);

bytes32 newRootHash =
Expand Down
25 changes: 15 additions & 10 deletions templates/AccessLogs.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,22 @@ library AccessLogs {
AccessLogs.Context memory a,
Memory.PhysicalAddress readAddress
) internal pure returns (uint64) {
bytes32 readData = a.buffer.consumeBytes32();
bytes32 valHash = keccak256(abi.encodePacked(readData));
(Memory.PhysicalAddress leafAddress, uint64 wordOffset) =
readAddress.truncateToLeaf();

(Memory.PhysicalAddress leafAddress, uint64 offset) = readAddress.truncateToLeaf();
bytes8 readValue = getBytes8FromBytes32AtOffset(readData, offset);
Memory.Region memory region = Memory.regionFromStride(
Memory.strideFromLeafAddress(leafAddress),
Memory.alignedSizeFromLog2(0)
);

bytes32 expectedValHash =
readLeaf(a, Memory.strideFromLeafAddress(leafAddress));
bytes32 leaf = a.buffer.consumeBytes32();
bytes32 rootHash = a.buffer.getRoot(region, keccak256(abi.encodePacked(leaf)));
require(
a.currentRootHash == rootHash, "Read word root doesn't match"
);

require(valHash == expectedValHash, "Read value doesn't match");
return machineWordToSolidityUint64(readValue);
bytes8 word = getBytes8FromBytes32AtOffset(leaf, wordOffset);
return machineWordToSolidityUint64(word);
}

//
Expand Down Expand Up @@ -155,7 +160,7 @@ library AccessLogs {
Memory.PhysicalAddress writeAddress,
uint64 newValue
) internal pure {
(Memory.PhysicalAddress leafAddress, uint64 offset) =
(Memory.PhysicalAddress leafAddress, uint64 wordOffset) =
writeAddress.truncateToLeaf();

Memory.Region memory region = Memory.regionFromStride(
Expand All @@ -171,7 +176,7 @@ library AccessLogs {
);

bytes32 newLeaf = setBytes8ToBytes32AtOffset(
solidityUint64ToMachineWord(newValue), oldLeaf, offset
solidityUint64ToMachineWord(newValue), oldLeaf, wordOffset
);

bytes32 newRootHash = a.buffer.getRoot(region, keccak256(abi.encodePacked(newLeaf)));
Expand Down
18 changes: 6 additions & 12 deletions templates/UArchReplay.t.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -137,22 +137,16 @@ contract UArchReplay_@X@_Test is Test {
Buffer.Context memory buffer = Buffer.Context(data, 0);

for (uint256 i = 0; i < arrayLength; i++) {
if (
keccak256(abi.encodePacked(rawAccesses[i].typeAccess))
== keccak256(abi.encodePacked("write"))
) {
if (rawAccesses[i].log2_size == 3) {
buffer.writeBytes32(
vm.parseBytes32(string.concat("0x", rawAccesses[i].read_value))
);
} else {
buffer.writeBytes32(
vm.parseBytes32(string.concat("0x", rawAccesses[i].written_hash))
vm.parseBytes32(string.concat("0x", rawAccesses[i].read_hash))
);
}
bytes32 word = bytes32(
vm.parseBytes32(string.concat("0x", rawAccesses[i].read_value))
);
buffer.writeBytes32(word);

buffer.writeBytes32(
vm.parseBytes32(string.concat("0x", rawAccesses[i].read_hash))
);
for (uint256 j = 0; j < siblingsLength; j++) {
buffer.writeBytes32(
vm.parseBytes32(
Expand Down
76 changes: 30 additions & 46 deletions test/AccessLogs.t.sol
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ contract AccessLogsTest is Test {
}
}

function verifyWord(bytes32 h, uint64 p, uint64 w) internal {
(Buffer.Context memory buffer,) =
makeReadBuffer(bytes8(w).swapEndian(), false);
AccessLogs.Context memory accessLogs = AccessLogs.Context(h, buffer);
assertEq(accessLogs.readWord(p.toPhysicalAddress()), w);
}

function testReadWordHappyPath() public {
(Buffer.Context memory buffer, bytes32 rootHash) = makeReadBuffer(
bytes8(0x0000000000000001).swapEndian(),
Expand All @@ -62,7 +69,7 @@ contract AccessLogsTest is Test {
);
AccessLogs.Context memory accessLogs =
AccessLogs.Context(rootHash, buffer);
vm.expectRevert("Read region root doesn't match");
vm.expectRevert("Read word root doesn't match");
accessLogs.readWord((position + 32).toPhysicalAddress());
}

Expand All @@ -75,67 +82,51 @@ contract AccessLogsTest is Test {
AccessLogs.Context memory accessLogs =
AccessLogs.Context(rootHash, buffer);

vm.expectRevert("Read value doesn't match");
vm.expectRevert("Read word root doesn't match");
accessLogs.readWord(position.toPhysicalAddress());
}

function testWriteWordHappyPath() public view {
uint64 wordWritten = 1;
function testWriteWordHappyPath() public {
uint64 wordWritten = 3;
(Buffer.Context memory buffer, bytes32 rootHash) = makeWriteBuffer(
initialReadLeaf,
bytes8(wordWritten).swapEndian(), /* withReadValueMismatch= */
false,
/* withWrittenValueMismatch= */
// bytes8(wordWritten).swapEndian(),
/* withReadValueMismatch= */
false
);
AccessLogs.Context memory accessLogs =
AccessLogs.Context(rootHash, buffer);
accessLogs.writeWord(position.toPhysicalAddress(), wordWritten);
verifyWord(accessLogs.currentRootHash, position, wordWritten);
}

function testWriteWordBadRegion() public {
uint64 wordWritten = 1;
uint64 wordWritten = 3;
(Buffer.Context memory buffer, bytes32 rootHash) = makeWriteBuffer(
initialReadLeaf,
bytes8(wordWritten).swapEndian(), /* withReadValueMismatch= */
false,
/* withWrittenValueMismatch= */
/* withReadValueMismatch= */
false
);
AccessLogs.Context memory accessLogs =
AccessLogs.Context(rootHash, buffer);
vm.expectRevert("Write region root doesn't match");
vm.expectRevert("Write word root doesn't match");
accessLogs.writeWord((position + 32).toPhysicalAddress(), wordWritten);
verifyWord(accessLogs.currentRootHash, position, wordWritten);
}

function testWriteWordReadMismatch() public {
uint64 wordWritten = 1;
(Buffer.Context memory buffer, bytes32 rootHash) = makeWriteBuffer(
initialReadLeaf,
bytes8(wordWritten).swapEndian(), /* withReadValueMismatch= */
true,
/* withWrittenValueMismatch= */
false
);
AccessLogs.Context memory accessLogs =
AccessLogs.Context(rootHash, buffer);
vm.expectRevert("logged and computed read hashes mismatch");
accessLogs.writeWord(position.toPhysicalAddress(), wordWritten);
}

function testWriteWordWriteMismatch() public {
uint64 wordWritten = 1;
uint64 wordWritten = 3;
(Buffer.Context memory buffer, bytes32 rootHash) = makeWriteBuffer(
initialReadLeaf,
bytes8(wordWritten).swapEndian(), /* withReadValueMismatch= */
false,
/* withWrittenValueMismatch= */
// bytes8(wordWritten).swapEndian(),
/* withReadValueMismatch= */
true
);
AccessLogs.Context memory accessLogs =
AccessLogs.Context(rootHash, buffer);
vm.expectRevert("Written hash mismatch");
vm.expectRevert("Write word root doesn't match");
accessLogs.writeWord(position.toPhysicalAddress(), wordWritten);
verifyWord(accessLogs.currentRootHash, position, wordWritten);
}

function testEndianSwap() public {
Expand Down Expand Up @@ -196,7 +187,7 @@ contract AccessLogsTest is Test {
if (withReadValueMismatch) {
readHash = keccak256(abi.encodePacked(bytes8(readHash)));
}
buffer.writeBytes32(readHash);

for (uint256 i = 0; i < 59; i++) {
buffer.writeBytes32(siblingHashes[i]);
}
Expand All @@ -206,29 +197,22 @@ contract AccessLogsTest is Test {
return (buffer, rootHash);
}

function makeWriteBuffer(
bytes32 readLeaf,
bytes8 writtenWord,
bool withReadValueMismatch,
bool withWrittenValueMismatch
) private view returns (Buffer.Context memory, bytes32) {
function makeWriteBuffer(bytes32 readLeaf, bool withReadValueMismatch)
private
view
returns (Buffer.Context memory, bytes32)
{
Buffer.Context memory buffer = Buffer.Context(
new bytes((59 << Memory.LOG2_LEAF) + 32 + 32 + 32), 0
);
bytes32 writtenLeaf = patchLeaf(readLeaf, writtenWord, position);
bytes32 writtenHash = keccak256(abi.encodePacked(writtenLeaf));
if (withWrittenValueMismatch) {
writtenHash = keccak256(abi.encodePacked(bytes8(writtenHash)));
}
buffer.writeBytes32(writtenHash);

// write leaf data, leaf hash and sibling hashes
buffer.writeBytes32(readLeaf);
bytes32 readHash = keccak256(abi.encodePacked(readLeaf));
if (withReadValueMismatch) {
readHash = keccak256(abi.encodePacked(bytes8(readHash)));
}
buffer.writeBytes32(readHash);

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

0 comments on commit 34741cf

Please sign in to comment.