Skip to content

Commit

Permalink
feat: increase tree leaf log2 size to 5
Browse files Browse the repository at this point in the history
  • Loading branch information
mpernambuco committed Jul 24, 2024
1 parent 88abafa commit 4f9ee32
Show file tree
Hide file tree
Showing 12 changed files with 107 additions and 70 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly-008922d5165c764859bc540d7298045eebf5bc60
version: nightly-fe2acca4e379793539db80e032d76ffe0110298b

- run: forge build
- run: forge fmt --check src test
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
- name: Install Foundry
uses: foundry-rs/foundry-toolchain@v1
with:
version: nightly-008922d5165c764859bc540d7298045eebf5bc60
version: nightly-fe2acca4e379793539db80e032d76ffe0110298b

- name: Run all tests
run: make test-all
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ test/uarch-log
*.log
test/UArchReplay_*.t.sol
.DS_Store
tests/*
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ TEST_DIR := test
DOWNLOADDIR := downloads
SRC_DIR := src

EMULATOR_VERSION ?= v0.17.0
EMULATOR_VERSION ?= v0.18.0-test4

TESTS_DATA_FILE ?= cartesi-machine-tests-data-$(EMULATOR_VERSION).deb
TESTS_DATA_DOWNLOAD_URL := https://github.com/cartesi/machine-emulator/releases/download/$(EMULATOR_VERSION)/$(TESTS_DATA_FILE)
Expand Down
1 change: 1 addition & 0 deletions foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ libs = ["node_modules", "lib"]
gas_reports = ["*"]
memory_limit = 11149934592
fs_permissions = [{access = "read", path = "./test/uarch-log/"}, {access = "read", path = "./test/uarch-bin/"}]
gas_limit = "18446744073709551615"

[fmt]
line_length = 80
Expand Down
4 changes: 2 additions & 2 deletions shasum-download
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
d3b254f274fd6c32e6688365886fbe6b86f91aab733a83cdb73e2461ed7a0d96 downloads/cartesi-machine-tests-data-v0.17.0.deb
8f0cd797b78466b62184257b3276d6c029c5ea5a53672036ad507cbf417d0211 downloads/uarch-riscv-tests-json-logs-v0.17.0.tar.gz
6024a9c7ebf6d10d3721753401c0edd7c3f1392cd26f1cc549fc58d15647a04a downloads/cartesi-machine-tests-data-v0.18.0-test4.deb
994af6de340e7efaf024545ddc44e850761115a9be98b82eeec441c869151525 downloads/uarch-riscv-tests-json-logs-v0.18.0-test4.tar.gz
44 changes: 33 additions & 11 deletions src/AccessLogs.sol
Original file line number Diff line number Diff line change
Expand Up @@ -61,9 +61,8 @@ library AccessLogs {
return end2;
}

/// @dev bytes buffer layouts differently for `readWord` and `writeWord`,
///`readWord` [8 bytes as uint64 value, 32 bytes as drive hash, 61 * 32 bytes as sibling proofs]
///`writeWord` [32 bytes as old drive hash, 32 * 61 bytes as sibling proofs]
/// @dev bytes buffer layout is the same for `readWord` and `writeWord`,
/// [32 bytes as read data], [59 * 32 bytes as sibling hashes]

//
// Read methods
Expand All @@ -86,21 +85,29 @@ library AccessLogs {
returns (bytes32)
{
Memory.Region memory r =
Memory.regionFromStride(readStride, Memory.alignedSizeFromLog2(0));
Memory.regionFromStride(readStride, Memory.alignedSizeFromLog2(2));
return readRegion(a, r);
}

function readWord(
AccessLogs.Context memory a,
Memory.PhysicalAddress readAddress
) internal pure returns (uint64) {
bytes8 b8 = a.buffer.consumeBytes8();
bytes32 valHash = keccak256(abi.encodePacked(b8));
bytes32 readData = a.buffer.consumeBytes32();
bytes32 valHash = keccak256(abi.encodePacked(readData));
Memory.PhysicalAddress leafAddress = Memory.PhysicalAddress.wrap(
Memory.PhysicalAddress.unwrap(readAddress) & ~uint64(31)
);
uint64 offset = Memory.PhysicalAddress.unwrap(readAddress)
- Memory.PhysicalAddress.unwrap(leafAddress);

bytes8 readValue = bytes8(readData << (offset << 3));

bytes32 expectedValHash =
readLeaf(a, Memory.strideFromWordAddress(readAddress));
readLeaf(a, Memory.strideFromWordAddress(leafAddress));

require(valHash == expectedValHash, "Read value doesn't match");
return machineWordToSolidityUint64(b8);
return machineWordToSolidityUint64(readValue);
}

//
Expand Down Expand Up @@ -129,7 +136,7 @@ library AccessLogs {
bytes32 newHash
) internal pure {
Memory.Region memory r =
Memory.regionFromStride(writeStride, Memory.alignedSizeFromLog2(0));
Memory.regionFromStride(writeStride, Memory.alignedSizeFromLog2(2));
writeRegion(a, r, newHash);
}

Expand All @@ -138,10 +145,25 @@ library AccessLogs {
Memory.PhysicalAddress writeAddress,
uint64 newValue
) internal pure {
bytes32 writtenData = a.buffer.consumeBytes32();
Memory.PhysicalAddress leafAddress = Memory.PhysicalAddress.wrap(
Memory.PhysicalAddress.unwrap(writeAddress) & ~uint64(31)
);
uint64 offset = Memory.PhysicalAddress.unwrap(writeAddress)
- Memory.PhysicalAddress.unwrap(leafAddress);

uint64 expectedNewValue =
machineWordToSolidityUint64(bytes8(writtenData << (offset << 3)));

require(
newValue == expectedNewValue,
"Access log value does not contain the expected written value"
);

writeLeaf(
a,
Memory.strideFromWordAddress(writeAddress),
keccak256(abi.encodePacked(solidityUint64ToMachineWord(newValue)))
Memory.strideFromWordAddress(leafAddress),
keccak256(abi.encodePacked(writtenData))
);
}
}
2 changes: 1 addition & 1 deletion src/Memory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ library Memory {
pure
returns (Stride)
{
return strideFromPhysicalAddress(startAddress, alignedSizeFromLog2(0));
return strideFromPhysicalAddress(startAddress, alignedSizeFromLog2(2));
}

function validateStrideLength(Stride stride, AlignedSize alignedSize)
Expand Down
2 changes: 1 addition & 1 deletion src/UArchConstants.sol
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ library UArchConstants {
uint64 constant RESET_POSITION = 0x400000;
uint8 constant RESET_ALIGNED_SIZE = 22;
bytes32 constant PRESTINE_STATE =
0xc2b9cf2658f233f44c403aba6e9f9aa6709e00617b349c0190113517f03d13de;
0xb0a850996464081741f4eefe12160dd2511514b3d606ec5b9c1b183fbe5a248f;
uint64 constant UARCH_ECALL_FN_HALT = 1;
uint64 constant UARCH_ECALL_FN_PUTCHAR = 2;
// END OF AUTO-GENERATED CODE
Expand Down
34 changes: 23 additions & 11 deletions templates/AccessLogs.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,8 @@ library AccessLogs {

//:#ifndef test

/// @dev bytes buffer layouts differently for `readWord` and `writeWord`,
///`readWord` [8 bytes as uint64 value, 32 bytes as drive hash, 61 * 32 bytes as sibling proofs]
///`writeWord` [32 bytes as old drive hash, 32 * 61 bytes as sibling proofs]
/// @dev bytes buffer layout is the same for `readWord` and `writeWord`,
/// [32 bytes as read data], [59 * 32 bytes as sibling hashes]

//
// Read methods
Expand All @@ -99,21 +98,26 @@ library AccessLogs {
returns (bytes32)
{
Memory.Region memory r =
Memory.regionFromStride(readStride, Memory.alignedSizeFromLog2(0));
Memory.regionFromStride(readStride, Memory.alignedSizeFromLog2(2));
return readRegion(a, r);
}

function readWord(
AccessLogs.Context memory a,
Memory.PhysicalAddress readAddress
) internal pure returns (uint64) {
bytes8 b8 = a.buffer.consumeBytes8();
bytes32 valHash = keccak256(abi.encodePacked(b8));
bytes32 readData = a.buffer.consumeBytes32();
bytes32 valHash = keccak256(abi.encodePacked(readData));
Memory.PhysicalAddress leafAddress = Memory.PhysicalAddress.wrap(Memory.PhysicalAddress.unwrap(readAddress) & ~uint64(31));
uint64 offset = Memory.PhysicalAddress.unwrap(readAddress) - Memory.PhysicalAddress.unwrap(leafAddress);

bytes8 readValue = bytes8(readData << (offset << 3));

bytes32 expectedValHash =
readLeaf(a, Memory.strideFromWordAddress(readAddress));
readLeaf(a, Memory.strideFromWordAddress(leafAddress));

require(valHash == expectedValHash, "Read value doesn't match");
return machineWordToSolidityUint64(b8);
return machineWordToSolidityUint64(readValue);
}

//
Expand Down Expand Up @@ -142,7 +146,7 @@ library AccessLogs {
bytes32 newHash
) internal pure {
Memory.Region memory r =
Memory.regionFromStride(writeStride, Memory.alignedSizeFromLog2(0));
Memory.regionFromStride(writeStride, Memory.alignedSizeFromLog2(2));
writeRegion(a, r, newHash);
}

Expand All @@ -151,10 +155,18 @@ library AccessLogs {
Memory.PhysicalAddress writeAddress,
uint64 newValue
) internal pure {
bytes32 writtenData = a.buffer.consumeBytes32();
Memory.PhysicalAddress leafAddress = Memory.PhysicalAddress.wrap(Memory.PhysicalAddress.unwrap(writeAddress) & ~uint64(31));
uint64 offset = Memory.PhysicalAddress.unwrap(writeAddress) - Memory.PhysicalAddress.unwrap(leafAddress);

uint64 expectedNewValue = machineWordToSolidityUint64(bytes8(writtenData << (offset << 3)));

require(newValue == expectedNewValue, "Access log value does not contain the expected written value");

writeLeaf(
a,
Memory.strideFromWordAddress(writeAddress),
keccak256(abi.encodePacked(solidityUint64ToMachineWord(newValue)))
Memory.strideFromWordAddress(leafAddress),
keccak256(abi.encodePacked(writtenData))
);
}

Expand Down
18 changes: 5 additions & 13 deletions templates/UArchReplay.t.sol.template
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ contract UArchReplay_@X@_Test is Test {
string constant JSON_PATH = "./test/uarch-log/";
string constant CATALOG_PATH = "catalog.json";

uint256 constant siblingsLength = 61;
uint256 constant siblingsLength = 59;

struct Entry {
string binaryFilename;
Expand Down Expand Up @@ -84,12 +84,10 @@ contract UArchReplay_@X@_Test is Test {
vm.parseBytes32(string.concat("0x", catalog[i].initialRootHash));
bytes32 finalRootHash =
vm.parseBytes32(string.concat("0x", catalog[i].finalRootHash));

for (uint256 j = 0; j < catalog[i].steps; j++) {
console.log("Replaying step %d ...", j);
// load json log
loadBufferFromRawJson(buffer, rj, j);

AccessLogs.Context memory accessLogs = AccessLogs.Context(
initialRootHash, Buffer.Context(buffer, 0)
);
Expand Down Expand Up @@ -140,20 +138,14 @@ 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("read"))
) {
bytes8 word = bytes8(
vm.parseBytes(string.concat("0x", rawAccesses[i].value))
);
buffer.writeBytes8(word);
}
bytes32 word = bytes32(
vm.parseBytes32(string.concat("0x", rawAccesses[i].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
Loading

0 comments on commit 4f9ee32

Please sign in to comment.