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

refactor: move machine hash logic to osp #172

Merged
merged 3 commits into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 0 additions & 45 deletions src/challenge/ChallengeLib.sol
Original file line number Diff line number Diff line change
Expand Up @@ -48,51 +48,6 @@ library ChallengeLib {
return challenge.timeUsedSinceLastMove() > challenge.current.timeLeft;
}

function getStartMachineHash(bytes32 globalStateHash, bytes32 wasmModuleRoot)
internal
pure
returns (bytes32)
{
// Start the value stack with the function call ABI for the entrypoint
Value[] memory startingValues = new Value[](3);
startingValues[0] = ValueLib.newRefNull();
startingValues[1] = ValueLib.newI32(0);
startingValues[2] = ValueLib.newI32(0);
ValueArray memory valuesArray = ValueArray({inner: startingValues});
ValueStack memory values = ValueStack({proved: valuesArray, remainingHash: 0});
ValueStack memory internalStack;
StackFrameWindow memory frameStack;

Machine memory mach = Machine({
status: MachineStatus.RUNNING,
valueStack: values,
internalStack: internalStack,
frameStack: frameStack,
globalStateHash: globalStateHash,
moduleIdx: 0,
functionIdx: 0,
functionPc: 0,
modulesRoot: wasmModuleRoot
});
return mach.hash();
}

function getEndMachineHash(MachineStatus status, bytes32 globalStateHash)
internal
pure
returns (bytes32)
{
if (status == MachineStatus.FINISHED) {
return keccak256(abi.encodePacked("Machine finished:", globalStateHash));
} else if (status == MachineStatus.ERRORED) {
return keccak256(abi.encodePacked("Machine errored:"));
} else if (status == MachineStatus.TOO_FAR) {
return keccak256(abi.encodePacked("Machine too far:"));
} else {
revert("BAD_BLOCK_STATUS");
}
}

function extractChallengeSegment(SegmentSelection calldata selection)
internal
pure
Expand Down
7 changes: 2 additions & 5 deletions src/challenge/ChallengeManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,8 @@ contract ChallengeManager is DelegateCallAware, IChallengeManager {
}

bytes32[] memory segments = new bytes32[](2);
segments[0] = ChallengeLib.getStartMachineHash(
globalStateHashes[0],
challenge.wasmModuleRoot
);
segments[1] = ChallengeLib.getEndMachineHash(machineStatuses[1], globalStateHashes[1]);
segments[0] = osp.getStartMachineHash(globalStateHashes[0], challenge.wasmModuleRoot);
segments[1] = osp.getEndMachineHash(machineStatuses[1], globalStateHashes[1]);

challenge.mode = ChallengeLib.ChallengeMode.EXECUTION;

Expand Down
10 changes: 10 additions & 0 deletions src/osp/IOneStepProofEntry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,16 @@ library OneStepProofEntryLib {
}

interface IOneStepProofEntry {
function getStartMachineHash(bytes32 globalStateHash, bytes32 wasmModuleRoot)
external
pure
returns (bytes32);

function getEndMachineHash(MachineStatus status, bytes32 globalStateHash)
external
pure
returns (bytes32);

function proveOneStep(
ExecutionContext calldata execCtx,
uint256 machineStep,
Expand Down
47 changes: 47 additions & 0 deletions src/osp/OneStepProofEntry.sol
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,53 @@
proverHostIo = proverHostIo_;
}

// Copied from ChallengeLib.sol
function getStartMachineHash(bytes32 globalStateHash, bytes32 wasmModuleRoot)
external
pure
returns (bytes32)
{
// Start the value stack with the function call ABI for the entrypoint
Value[] memory startingValues = new Value[](3);
startingValues[0] = ValueLib.newRefNull();
startingValues[1] = ValueLib.newI32(0);
startingValues[2] = ValueLib.newI32(0);
ValueArray memory valuesArray = ValueArray({inner: startingValues});
ValueStack memory values = ValueStack({proved: valuesArray, remainingHash: 0});
ValueStack memory internalStack;

Check warning

Code scanning / Slither

Uninitialized local variables Medium

StackFrameWindow memory frameStack;

Check warning

Code scanning / Slither

Uninitialized local variables Medium


Machine memory mach = Machine({
status: MachineStatus.RUNNING,
valueStack: values,
internalStack: internalStack,
frameStack: frameStack,
globalStateHash: globalStateHash,
moduleIdx: 0,
functionIdx: 0,
functionPc: 0,
modulesRoot: wasmModuleRoot
});
return mach.hash();
}

// Copied from ChallengeLib.sol
function getEndMachineHash(MachineStatus status, bytes32 globalStateHash)
external
pure
returns (bytes32)
{
if (status == MachineStatus.FINISHED) {
return keccak256(abi.encodePacked("Machine finished:", globalStateHash));
} else if (status == MachineStatus.ERRORED) {
return keccak256(abi.encodePacked("Machine errored:"));
} else if (status == MachineStatus.TOO_FAR) {
return keccak256(abi.encodePacked("Machine too far:"));
} else {
revert("BAD_BLOCK_STATUS");
}
}

function proveOneStep(
ExecutionContext calldata execCtx,
uint256 machineStep,
Expand Down
9 changes: 9 additions & 0 deletions test/signatures/OneStepProofEntry
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"getEndMachineHash(uint8,bytes32)": "d8558b87",
"getStartMachineHash(bytes32,bytes32)": "04997be4",
"proveOneStep((uint256,address),uint256,bytes32,bytes)": "5d3adcfb",
"prover0()": "30a5509f",
"proverHostIo()": "5f52fd7c",
"proverMath()": "66e5d9c3",
"proverMem()": "1f128bc0"
}
2 changes: 1 addition & 1 deletion test/signatures/test-sigs.bash
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/bin/bash
output_dir="./test/signatures"
for CONTRACTNAME in Bridge Inbox Outbox RollupCore RollupUserLogic RollupAdminLogic SequencerInbox ChallengeManager ERC20Bridge ERC20Inbox ERC20Outbox BridgeCreator DeployHelper RollupCreator
for CONTRACTNAME in Bridge Inbox Outbox RollupCore RollupUserLogic RollupAdminLogic SequencerInbox ChallengeManager ERC20Bridge ERC20Inbox ERC20Outbox BridgeCreator DeployHelper RollupCreator OneStepProofEntry
do
echo "Checking for signature changes in $CONTRACTNAME"
[ -f "$output_dir/$CONTRACTNAME" ] && mv "$output_dir/$CONTRACTNAME" "$output_dir/$CONTRACTNAME-old"
Expand Down
Loading