1
1
// SPDX-License-Identifier: MIT
2
2
pragma solidity ^ 0.8.13 ;
3
-
3
+ import { IPreimageOracle } from "./interfaces/IPreimageOracle.sol " ;
4
+ import { PreimageKeyLib } from "./PreimageKeyLib.sol " ;
4
5
5
6
contract Step {
6
7
@@ -10,6 +11,11 @@ contract Step {
10
11
preimageOracle = _preimageOracle;
11
12
}
12
13
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
+
13
19
// Executes a single RISC-V instruction, starting from
14
20
function step (bytes calldata stateData , bytes calldata proof , bytes32 localContext ) public returns (bytes32 ) {
15
21
assembly {
@@ -774,17 +780,22 @@ contract Step {
774
780
}
775
781
776
782
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 )
788
799
}
789
800
790
801
function readPreimageValue (addr, count, localContext_) -> out {
0 commit comments