Commit 626b3454 authored by George Hotz's avatar George Hotz

deal with misaligned end of keccaks

parent 85dbf8ea
......@@ -29,10 +29,10 @@ contract MIPSMemory {
require(p.length == 0 || p.length == len, "length is somehow wrong");
p.length = len;
p.data[offset] = (1 << 32) |
(uint32(uint8(anything[offset+0])) << 24) |
(uint32(uint8(anything[offset+1])) << 16) |
(uint32(uint8(anything[offset+2])) << 8) |
(uint32(uint8(anything[offset+3])) << 0);
((len <= (offset+0) ? 0 : uint32(uint8(anything[offset+0]))) << 24) |
((len <= (offset+1) ? 0 : uint32(uint8(anything[offset+1]))) << 16) |
((len <= (offset+2) ? 0 : uint32(uint8(anything[offset+2]))) << 8) |
((len <= (offset+3) ? 0 : uint32(uint8(anything[offset+3]))) << 0);
}
// one per owner (at a time)
......@@ -131,12 +131,16 @@ contract MIPSMemory {
// MMIO preimage oracle
if (addr >= 0x31000000 && addr < 0x32000000) {
bytes32 pihash = ReadBytes32(stateHash, 0x30001000);
if (pihash == keccak256("")) {
// both the length and any data are 0
return 0;
}
if (addr == 0x31000000) {
return uint32(preimage[pihash].length);
}
uint offset = addr-0x31000004;
uint64 data = preimage[pihash].data[offset];
require(data > 0, "offset must be loaded in");
require(data > 0, "offset not loaded");
return uint32(data);
}
......
const { keccak256 } = require("@ethersproject/keccak256");
const { expect } = require("chai");
const chai = require("chai");
const { solidity } = require("ethereum-waffle");
chai.use(solidity);
const { writeMemory } = require("../scripts/lib")
async function loadPreimageAndSelect(mm, data, offset) {
// add in the preimage at offset 4
const hash = keccak256(data)
await mm.AddPreimage(data, offset)
// write the oracle selection address
let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
root = await writeMemory(mm, root, 0x30001000, hash, true)
return root
}
describe("MIPSMemory oracle", function () {
beforeEach(async function () {
const MIPSMemory = await ethers.getContractFactory("MIPSMemory")
......@@ -9,19 +25,21 @@ describe("MIPSMemory oracle", function () {
await mm.AddTrieNode(new Uint8Array([0x80]))
})
it("simple oracle", async function() {
// add in the preimage at offset 4
const data = [0x11,0x22,0x33,0x44,0xaa,0xbb,0xcc,0xdd]
const hash = keccak256(data)
await mm.AddPreimage(data, 4)
// write the oracle selection address
let root = "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421"
root = await writeMemory(mm, root, 0x30001000, hash, true)
root = await loadPreimageAndSelect(mm, [0x11,0x22,0x33,0x44,0xaa,0xbb,0xcc,0xdd], 4)
// length is 8
expect(await mm.ReadMemory(root, 0x31000000)).to.equal(8)
// offset 4 is 0xaabbccdd
expect(await mm.ReadMemory(root, 0x31000008)).to.equal(0xaabbccdd)
// offset 0 isn't loaded
await expect(mm.ReadMemory(root, 0x31000004)).to.be.revertedWith("offset not loaded")
})
it("misaligned oracle", async function() {
root = await loadPreimageAndSelect(mm, [0x11,0x22,0x33,0x44,0xaa,0xbb,0xcc], 4)
expect(await mm.ReadMemory(root, 0x31000000)).to.equal(7)
expect(await mm.ReadMemory(root, 0x31000008)).to.equal(0xaabbcc00)
})
})
\ No newline at end of file
This diff is collapsed.
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