Commit e37f3c0b authored by George Hotz's avatar George Hotz

check proof in AddMerkleState

parent 41bff486
...@@ -49,9 +49,6 @@ contract Challenge { ...@@ -49,9 +49,6 @@ contract Challenge {
owner.transfer(address(this).balance); owner.transfer(address(this).balance);
} }
// memory helpers
// create challenge // create challenge
uint256 public lastChallengeId = 0; uint256 public lastChallengeId = 0;
...@@ -103,10 +100,9 @@ contract Challenge { ...@@ -103,10 +100,9 @@ contract Challenge {
// confirm the finalSystemHash asserts the state you claim (in $t0-$t7) and the machine is stopped // confirm the finalSystemHash asserts the state you claim (in $t0-$t7) and the machine is stopped
// you must load these proofs into MIPS before calling this // you must load these proofs into MIPS before calling this
// we disagree at the end // we disagree at the end
require(mem.ReadMemory(finalSystemState, 0xC0000080) == 0x5EAD0000, "machine is not stopped in final state (PC == 0x5EAD0000)");
require(mem.ReadMemory(finalSystemState, 0x30000800) == 0x1337f00d, "state is not outputted"); require(mem.ReadMemory(finalSystemState, 0x30000800) == 0x1337f00d, "state is not outputted");
require(mem.ReadBytes32(finalSystemState, 0x30000804) == assertionRoot, "you are claiming a different state root in machine"); require(mem.ReadBytes32(finalSystemState, 0x30000804) == assertionRoot, "you are claiming a different state root in machine");
require(mem.ReadMemory(finalSystemState, 0xC0000080) == 0x5EAD0000, "machine is not stopped in final state (PC == 0x5EAD0000)");
return newChallengeTrusted(startState, finalSystemState, stepCount); return newChallengeTrusted(startState, finalSystemState, stepCount);
} }
...@@ -167,21 +163,4 @@ contract Challenge { ...@@ -167,21 +163,4 @@ contract Challenge {
emit ChallengerWins(challengeId); emit ChallengerWins(challengeId);
} }
function HumiliateChallengerStateTransition(uint256 challengeId, bytes32 finalRiscState) external {
Chal storage c = challenges[challengeId];
require(c.challenger != address(0), "invalid challenge");
require(owner == msg.sender, "must be owner");
require(c.L + 1 == c.R, "binary search not finished");
// it's 0 if you agree with all attacker states except the final one
// in which case, you get a free pass to submit now
require(c.defendedState[c.R] == finalRiscState || c.defendedState[c.R] == bytes32(0), "must be consistent with state");
require(mips.Step(c.defendedState[c.L]) == finalRiscState, "wrong asserted state");
// consider the challenger mocked
// if they staked a bounty, you could claim it here
emit ChallengerLoses(challengeId);
}
} }
...@@ -2,6 +2,7 @@ ...@@ -2,6 +2,7 @@
pragma solidity ^0.7.3; pragma solidity ^0.7.3;
import "./lib/Lib_Keccak256.sol"; import "./lib/Lib_Keccak256.sol";
import "./lib/Lib_MerkleTrie.sol";
contract MIPSMemory { contract MIPSMemory {
// This state is global // This state is global
...@@ -47,8 +48,21 @@ contract MIPSMemory { ...@@ -47,8 +48,21 @@ contract MIPSMemory {
return Lib_Keccak256.get_hash(c); return Lib_Keccak256.get_hash(c);
} }
function AddMerkleState(bytes32 stateHash, uint32 addr, uint32 value, string calldata proof) public { function tb(uint32 dat) internal returns (bytes memory) {
// TODO: check proof bytes memory ret = new bytes(4);
ret[0] = bytes1(uint8(dat >> 24));
ret[1] = bytes1(uint8(dat >> 16));
ret[2] = bytes1(uint8(dat >> 8));
ret[3] = bytes1(uint8(dat >> 0));
return ret;
}
function AddMerkleState(bytes32 stateHash, uint32 addr, uint32 value, bytes calldata proof) public {
if (value == 0) {
require(Lib_MerkleTrie.verifyExclusionProof(tb(addr), proof, stateHash) == true, "couldn't verify 0 proof");
} else {
require(Lib_MerkleTrie.verifyInclusionProof(tb(addr), tb(value), proof, stateHash) == true, "couldn't verify non 0 proof");
}
state[stateHash][addr] = (1 << 32) | value; state[stateHash][addr] = (1 << 32) | value;
} }
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment