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