11// SPDX-License-Identifier: MIT
22pragma solidity 0.8.15 ;
33
4- contract PreimageOracle {
4+ import {IPreimageOracle} from "./interfaces/IPreimageOracle.sol " ;
5+ import {PreimageKeyLib} from "./PreimageKeyLib.sol " ;
6+
7+ contract PreimageOracle is IPreimageOracle {
58 mapping (bytes32 => uint256 ) public preimageLengths;
69 mapping (bytes32 => mapping (uint256 => bytes32 )) public preimageParts;
710 mapping (bytes32 => mapping (uint256 => bool )) public preimagePartOk;
@@ -21,42 +24,44 @@ contract PreimageOracle {
2124 dat_ = preimageParts[_key][_offset];
2225 }
2326
24- // TODO(CLI-4104):
25- // we need to mix-in the ID of the dispute for local-type keys to avoid collisions,
26- // and restrict local pre-image insertion to the dispute-managing contract.
27- // For now we permit anyone to write any pre-image unchecked, to make testing easy.
28- // This method is DANGEROUS. And NOT FOR PRODUCTION.
29- function cheat (uint256 partOffset , bytes32 key , bytes32 part , uint256 size ) external {
30- preimagePartOk[key][partOffset] = true ;
31- preimageParts[key][partOffset] = part;
32- preimageLengths[key] = size;
33- }
27+ function loadLocalData (uint256 _ident , bytes32 _localContext , bytes32 _word , uint256 _size , uint256 _partOffset )
28+ external
29+ returns (bytes32 key_ )
30+ {
31+ // Compute the localized key from the given local identifier.
32+ key_ = PreimageKeyLib.localizeIdent (_ident, _localContext);
33+
34+ // Revert if the given part offset is not within bounds.
35+ if (_partOffset > _size + 8 || _size > 32 ) {
36+ // Revert with "PartOffsetOOB()"
37+ assembly {
38+ // Store "PartOffsetOOB()"
39+ mstore (0 , 0xfe254987 )
40+ // Revert with "PartOffsetOOB()"
41+ revert (0x1c , 4 )
42+ }
43+ // TODO: remove with revert PartOffsetOOB();
44+ }
3445
35- // temporary method for localization. Will be removed to PreimageKeyLib.sol
36- function localize ( bytes32 _key , bytes32 _localContext ) internal view returns ( bytes32 localizedKey_ ) {
46+ // Prepare the local data part at the given offset
47+ bytes32 part;
3748 assembly {
38- // Grab the current free memory pointer to restore later.
39- let ptr := mload (0x40 )
40- // Store the local data key and caller next to each other in memory for hashing.
41- mstore (0 , _key)
42- mstore (0x20 , caller ())
43- mstore (0x40 , _localContext)
44- // Localize the key with the above `localize` operation.
45- localizedKey_ := or (and (keccak256 (0 , 0x60 ), not (shl (248 , 0xFF ))), shl (248 , 1 ))
46- // Restore the free memory pointer.
47- mstore (0x40 , ptr)
49+ // Clean the memory in [0x20, 0x40)
50+ mstore (0x20 , 0x00 )
51+
52+ // Store the full local data in scratch space.
53+ mstore (0x00 , shl (192 , _size))
54+ mstore (0x08 , _word)
55+
56+ // Prepare the local data part at the requested offset.
57+ part := mload (_partOffset)
4858 }
49- }
5059
51- // temporary method for localization. Will be removed to PreimageKeyLib.sol
52- function cheatLocalKey (uint256 partOffset , bytes32 key , bytes32 part , uint256 size , bytes32 localContext ) external {
53- // sanity check key is local key using prefix
54- require (uint8 (key[0 ]) == 1 , "must be used for local key " );
55-
56- bytes32 localizedKey = localize (key, localContext);
57- preimagePartOk[localizedKey][partOffset] = true ;
58- preimageParts[localizedKey][partOffset] = part;
59- preimageLengths[localizedKey] = size;
60+ // Store the first part with `_partOffset`.
61+ preimagePartOk[key_][_partOffset] = true ;
62+ preimageParts[key_][_partOffset] = part;
63+ // Assign the length of the preimage at the localized key.
64+ preimageLengths[key_] = _size;
6065 }
6166
6267 // loadKeccak256PreimagePart prepares the pre-image to be read by keccak256 key,
@@ -95,4 +100,8 @@ contract PreimageOracle {
95100 preimageParts[key][_partOffset] = part;
96101 preimageLengths[key] = size;
97102 }
103+
104+ function loadSha256PreimagePart (uint256 _partOffset , bytes calldata _preimage ) external {
105+ // TODO: fetch diff from cannon. Currently for only satisfying interface
106+ }
98107}
0 commit comments