11// SPDX-License-Identifier: MIT
22pragma solidity ^ 0.8.13 ;
3-
3+ import { IPreimageOracle } from "./interfaces/IPreimageOracle.sol " ;
4+ import { PreimageKeyLib } from "./PreimageKeyLib.sol " ;
45
56contract Step {
67
@@ -10,6 +11,11 @@ contract Step {
1011 preimageOracle = _preimageOracle;
1112 }
1213
14+ // Use public method because compiler optimizes out if not
15+ function localize (bytes32 _key , bytes32 _localContext ) public view returns (bytes32 localizedKey_ ) {
16+ return PreimageKeyLib.localize (_key, _localContext);
17+ }
18+
1319 // Executes a single RISC-V instruction, starting from
1420 function step (bytes calldata stateData , bytes calldata proof , bytes32 localContext ) public returns (bytes32 ) {
1521 assembly {
@@ -774,17 +780,22 @@ contract Step {
774780 }
775781
776782 function localize (preImageKey, localContext_) -> localizedKey {
777- // TODO: deduplicate definition of localize using lib
778- // Grab the current free memory pointer to restore later.
779- let ptr := mload (0x40 )
780- // Store the local data key and caller next to each other in memory for hashing.
781- mstore (0 , preImageKey)
782- mstore (0x20 , caller ())
783- mstore (0x40 , localContext_)
784- // Localize the key with the above `localize` operation.
785- localizedKey := or (and (keccak256 (0 , 0x60 ), not (shl (248 , 0xFF ))), shl (248 , 1 ))
786- // Restore the free memory pointer.
787- mstore (0x40 , ptr)
783+ // calling address(this).localize(bytes32,bytes32)
784+ // eventually calling PreimageKeyLib.localize(bytes32,bytes32)
785+ let memPtr := mload (0x40 ) // get pointer to free memory for preimage interactions
786+ mstore (memPtr, shl (224 , 0x1aae47f0 )) // (32-4)*8=224: right-pad the function selector, and then store it as prefix
787+ mstore (add (memPtr, 0x04 ), preImageKey)
788+ mstore (add (memPtr, 0x24 ), localContext_)
789+ let cgas := 100000 // TODO change call gas
790+
791+ // use delegatecall to follow same behavior with library method call
792+ let res := delegatecall (cgas, address (), memPtr, 0x44 , 0x00 , 0x20 ) // output into scratch space
793+ if res {
794+ // 1 on success
795+ localizedKey := mload (0x00 )
796+ leave
797+ }
798+ revertWithCode (0xbadf00d0 )
788799 }
789800
790801 function readPreimageValue (addr, count, localContext_) -> out {
0 commit comments