-
Notifications
You must be signed in to change notification settings - Fork 20
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10 from ethereum-optimism/tip/pcw109550/local-pre…
…image-support feat: Local Preimage Support
- Loading branch information
Showing
9 changed files
with
330 additions
and
45 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[submodule "rvsol/lib/forge-std"] | ||
path = rvsol/lib/forge-std | ||
url = https://github.com/foundry-rs/forge-std |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
/// @title PreimageKeyLib | ||
/// @notice Shared utilities for localizing local keys in the preimage oracle. | ||
library PreimageKeyLib { | ||
/// @notice Generates a context-specific local key for the given local data identifier. | ||
/// @dev See `localize` for a description of the localization operation. | ||
/// @param _ident The identifier of the local data. [0, 32) bytes in size. | ||
/// @param _localContext The local context for the key. | ||
/// @return key_ The context-specific local key. | ||
function localizeIdent(uint256 _ident, bytes32 _localContext) internal view returns (bytes32 key_) { | ||
assembly { | ||
// Set the type byte in the given identifier to `1` (Local). We only care about | ||
// the [1, 32) bytes in this value. | ||
key_ := or(shl(248, 1), and(_ident, not(shl(248, 0xFF)))) | ||
} | ||
// Localize the key with the given local context. | ||
key_ = localize(key_, _localContext); | ||
} | ||
|
||
/// @notice Localizes a given local data key for the caller's context. | ||
/// @dev The localization operation is defined as: | ||
/// localize(k) = H(k .. sender .. local_context) & ~(0xFF << 248) | (0x01 << 248) | ||
/// where H is the Keccak-256 hash function. | ||
/// @param _key The local data key to localize. | ||
/// @param _localContext The local context for the key. | ||
/// @return localizedKey_ The localized local data key. | ||
function localize(bytes32 _key, bytes32 _localContext) internal view returns (bytes32 localizedKey_) { | ||
assembly { | ||
// 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, _key) | ||
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) | ||
} | ||
} | ||
|
||
/// @notice Computes and returns the key for a global keccak pre-image. | ||
/// @param _preimage The pre-image. | ||
/// @return key_ The pre-image key. | ||
function keccak256PreimageKey(bytes memory _preimage) internal pure returns (bytes32 key_) { | ||
assembly { | ||
// Grab the size of the `_preimage` | ||
let size := mload(_preimage) | ||
|
||
// Compute the pre-image keccak256 hash (aka the pre-image key) | ||
let h := keccak256(add(_preimage, 0x20), size) | ||
|
||
// Mask out prefix byte, replace with type 2 byte | ||
key_ := or(and(h, not(shl(248, 0xFF))), shl(248, 2)) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
// SPDX-License-Identifier: MIT | ||
pragma solidity 0.8.15; | ||
|
||
/// @title IPreimageOracle | ||
/// @notice Interface for a preimage oracle. | ||
interface IPreimageOracle { | ||
/// @notice Reads a preimage from the oracle. | ||
/// @param _key The key of the preimage to read. | ||
/// @param _offset The offset of the preimage to read. | ||
/// @return dat_ The preimage data. | ||
/// @return datLen_ The length of the preimage data. | ||
function readPreimage(bytes32 _key, uint256 _offset) external view returns (bytes32 dat_, uint256 datLen_); | ||
|
||
/// @notice Loads of local data part into the preimage oracle. | ||
/// @param _ident The identifier of the local data. | ||
/// @param _localContext The local key context for the preimage oracle. Optionally, can be set as a constant | ||
/// if the caller only requires one set of local keys. | ||
/// @param _word The local data word. | ||
/// @param _size The number of bytes in `_word` to load. | ||
/// @param _partOffset The offset of the local data part to write to the oracle. | ||
/// @dev The local data parts are loaded into the preimage oracle under the context | ||
/// of the caller - no other account can write to the caller's context | ||
/// specific data. | ||
/// | ||
/// There are 5 local data identifiers: | ||
/// ┌────────────┬────────────────────────┐ | ||
/// │ Identifier │ Data │ | ||
/// ├────────────┼────────────────────────┤ | ||
/// │ 1 │ L1 Head Hash (bytes32) │ | ||
/// │ 2 │ Output Root (bytes32) │ | ||
/// │ 3 │ Root Claim (bytes32) │ | ||
/// │ 4 │ L2 Block Number (u64) │ | ||
/// │ 5 │ Chain ID (u64) │ | ||
/// └────────────┴────────────────────────┘ | ||
function loadLocalData(uint256 _ident, bytes32 _localContext, bytes32 _word, uint256 _size, uint256 _partOffset) | ||
external | ||
returns (bytes32 key_); | ||
|
||
/// @notice Prepares a preimage to be read by keccak256 key, starting at the given offset and up to 32 bytes | ||
/// (clipped at preimage length, if out of data). | ||
/// @param _partOffset The offset of the preimage to read. | ||
/// @param _preimage The preimage data. | ||
function loadKeccak256PreimagePart(uint256 _partOffset, bytes calldata _preimage) external; | ||
|
||
/// @notice Prepares a preimage to be read by sha256 key, starting at the given offset and up to 32 bytes | ||
/// (clipped at preimage length, if out of data). | ||
/// @param _partOffset The offset of the preimage to read. | ||
/// @param _preimage The preimage data. | ||
function loadSha256PreimagePart(uint256 _partOffset, bytes calldata _preimage) external; | ||
} |
Oops, something went wrong.