Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
N
nebula
Project
Project
Details
Activity
Releases
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
exchain
nebula
Commits
ac5f8bc4
Commit
ac5f8bc4
authored
Oct 11, 2021
by
George Hotz
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
make memory use the merkle tree
parent
e37f3c0b
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
29 additions
and
23 deletions
+29
-23
MIPSMemory.sol
contracts/MIPSMemory.sol
+29
-23
No files found.
contracts/MIPSMemory.sol
View file @
ac5f8bc4
...
@@ -5,9 +5,6 @@ import "./lib/Lib_Keccak256.sol";
...
@@ -5,9 +5,6 @@ import "./lib/Lib_Keccak256.sol";
import "./lib/Lib_MerkleTrie.sol";
import "./lib/Lib_MerkleTrie.sol";
contract MIPSMemory {
contract MIPSMemory {
// This state is global
mapping(bytes32 => mapping (uint32 => uint64)) public state;
// TODO: replace with mapping(bytes32 => mapping(uint, bytes4))
// TODO: replace with mapping(bytes32 => mapping(uint, bytes4))
// to only save the part we care about
// to only save the part we care about
mapping(bytes32 => bytes) public preimage;
mapping(bytes32 => bytes) public preimage;
...
@@ -48,7 +45,7 @@ contract MIPSMemory {
...
@@ -48,7 +45,7 @@ contract MIPSMemory {
return Lib_Keccak256.get_hash(c);
return Lib_Keccak256.get_hash(c);
}
}
function tb(uint32 dat) internal returns (bytes memory) {
function tb(uint32 dat) internal
pure
returns (bytes memory) {
bytes memory ret = new bytes(4);
bytes memory ret = new bytes(4);
ret[0] = bytes1(uint8(dat >> 24));
ret[0] = bytes1(uint8(dat >> 24));
ret[1] = bytes1(uint8(dat >> 16));
ret[1] = bytes1(uint8(dat >> 16));
...
@@ -57,29 +54,31 @@ contract MIPSMemory {
...
@@ -57,29 +54,31 @@ contract MIPSMemory {
return ret;
return ret;
}
}
function
AddMerkleState(bytes32 stateHash, uint32 addr, uint32 value, bytes calldata proof) public
{
function
fb(bytes memory dat) internal pure returns (uint32)
{
if (value == 0) {
require(dat.length == 4, "wrong length value");
require(Lib_MerkleTrie.verifyExclusionProof(tb(addr), proof, stateHash) == true, "couldn't verify 0 proof");
uint32 ret = uint32(uint8(dat[0])) << 24 |
} else {
uint32(uint8(dat[1])) << 16 |
require(Lib_MerkleTrie.verifyInclusionProof(tb(addr), tb(value), proof, stateHash) == true, "couldn't verify non 0 proof");
uint32(uint8(dat[2])) << 8 |
}
uint32(uint8(dat[3]));
state[stateHash][addr] = (1 << 32) | value
;
return ret
;
}
}
function WriteMemory(bytes32 stateHash, uint32 addr, uint32 value) public pure returns (bytes32) {
mapping(bytes32 => mapping(uint32 => bytes)) proofs;
require(addr & 3 == 0, "write memory must be 32-bit aligned");
// TODO: do the real stateHash mutation
bytes32 newstateHash = keccak256(abi.encodePacked(stateHash));
// note that zeros are never stored in the trie, so storing a 0 is a delete
function AddMerkleProof(bytes32 stateHash, uint32 addr, bytes calldata proof) public {
// validate proof
Lib_MerkleTrie.get(tb(addr), proof, stateHash);
proofs[stateHash][addr] = proof;
}
// no proof required, this is obviously right
function WriteMemory(bytes32 stateHash, uint32 addr, uint32 value) public view returns (bytes32) {
//state[newstateHash][addr] = (1 << 32) | value
;
require(addr & 3 == 0, "write memory must be 32-bit aligned")
;
return newstateHash;
// TODO: this can't delete nodes. modify the client to never delete
return Lib_MerkleTrie.update(tb(addr), tb(value), proofs[stateHash][addr], stateHash);
}
}
function WriteBytes32(bytes32 stateHash, uint32 addr, bytes32 val) public
pure
returns (bytes32) {
function WriteBytes32(bytes32 stateHash, uint32 addr, bytes32 val) public
view
returns (bytes32) {
for (uint32 i = 0; i < 32; i += 4) {
for (uint32 i = 0; i < 32; i += 4) {
uint256 tv = uint256(val>>(224-(i*8)));
uint256 tv = uint256(val>>(224-(i*8)));
stateHash = WriteMemory(stateHash, addr+i, uint32(tv));
stateHash = WriteMemory(stateHash, addr+i, uint32(tv));
...
@@ -122,8 +121,15 @@ contract MIPSMemory {
...
@@ -122,8 +121,15 @@ contract MIPSMemory {
(uint32(a3) << 0);
(uint32(a3) << 0);
}
}
uint64 ret = state[stateHash][addr];
bool exists;
require((ret >> 32) == 1, "memory was not initialized");
bytes memory value;
return uint32(ret);
(exists, value) = Lib_MerkleTrie.get(tb(addr), proofs[stateHash][addr], stateHash);
if (!exists) {
// this is uninitialized memory
return 0;
} else {
return fb(value);
}
}
}
}
}
\ No newline at end of file
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment