Skip to content

Commit

Permalink
Add dependency for PreimageKeyLib
Browse files Browse the repository at this point in the history
  • Loading branch information
pcw109550 committed Feb 8, 2024
1 parent 83cb772 commit 6bf2840
Showing 1 changed file with 23 additions and 12 deletions.
35 changes: 23 additions & 12 deletions rvsol/src/Step.sol
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;

import { IPreimageOracle } from "./interfaces/IPreimageOracle.sol";
import { PreimageKeyLib } from "./PreimageKeyLib.sol";

contract Step {

Expand All @@ -10,6 +11,11 @@ contract Step {
preimageOracle = _preimageOracle;
}

// Use public method because compiler optimizes out if not
function localize(bytes32 _key, bytes32 _localContext) public view returns (bytes32 localizedKey_) {
return PreimageKeyLib.localize(_key, _localContext);
}

// Executes a single RISC-V instruction, starting from
function step(bytes calldata stateData, bytes calldata proof, bytes32 localContext) public returns (bytes32) {
assembly {
Expand Down Expand Up @@ -774,17 +780,22 @@ contract Step {
}

function localize(preImageKey, localContext_) -> localizedKey {
// TODO: deduplicate definition of localize using lib
// Grab the current free memory pointer to restore later.
let ptr := mload(0x40)
// Store the local data key and caller next to each other in memory for hashing.
mstore(0, preImageKey)
mstore(0x20, caller())
mstore(0x40, localContext_)
// Localize the key with the above `localize` operation.
localizedKey := or(and(keccak256(0, 0x60), not(shl(248, 0xFF))), shl(248, 1))
// Restore the free memory pointer.
mstore(0x40, ptr)
// calling address(this).localize(bytes32,bytes32)
// eventually calling PreimageKeyLib.localize(bytes32,bytes32)
let memPtr := mload(0x40) // get pointer to free memory for preimage interactions
mstore(memPtr, shl(224, 0x1aae47f0)) // (32-4)*8=224: right-pad the function selector, and then store it as prefix
mstore(add(memPtr, 0x04), preImageKey)
mstore(add(memPtr, 0x24), localContext_)
let cgas := 100000 // TODO change call gas

// use delegatecall to follow same behavior with library method call
let res := delegatecall(cgas, address(), memPtr, 0x44, 0x00, 0x20) // output into scratch space
if res {
// 1 on success
localizedKey := mload(0x00)
leave
}
revertWithCode(0xbadf00d0)
}

function readPreimageValue(addr, count, localContext_) -> out {
Expand Down

0 comments on commit 6bf2840

Please sign in to comment.