From 34741cf333f65789e4d303f1a2e7b8516c4c0d0d Mon Sep 17 00:00:00 2001 From: Gabriel Coutinho de Paula Date: Mon, 5 Aug 2024 10:31:17 -0300 Subject: [PATCH] fix: change access logs --- src/AccessLogs.sol | 25 +++++---- templates/AccessLogs.sol.template | 25 +++++---- templates/UArchReplay.t.sol.template | 18 +++---- test/AccessLogs.t.sol | 76 +++++++++++----------------- 4 files changed, 65 insertions(+), 79 deletions(-) diff --git a/src/AccessLogs.sol b/src/AccessLogs.sol index f8457fa..131ab2c 100644 --- a/src/AccessLogs.sol +++ b/src/AccessLogs.sol @@ -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); } // @@ -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( @@ -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 = diff --git a/templates/AccessLogs.sol.template b/templates/AccessLogs.sol.template index e95f310..b25a9dc 100644 --- a/templates/AccessLogs.sol.template +++ b/templates/AccessLogs.sol.template @@ -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); } // @@ -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( @@ -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))); diff --git a/templates/UArchReplay.t.sol.template b/templates/UArchReplay.t.sol.template index ee82376..6bc22a9 100644 --- a/templates/UArchReplay.t.sol.template +++ b/templates/UArchReplay.t.sol.template @@ -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( diff --git a/test/AccessLogs.t.sol b/test/AccessLogs.t.sol index 30d9691..dd95aab 100644 --- a/test/AccessLogs.t.sol +++ b/test/AccessLogs.t.sol @@ -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(), @@ -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()); } @@ -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 { @@ -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]); } @@ -206,21 +197,14 @@ 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); @@ -228,7 +212,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]); }