Skip to content

Commit

Permalink
fp contracts: SHA2 preimage type support in preimage oracle (ethereum…
Browse files Browse the repository at this point in the history
…-optimism#9066)

* fp contracts: SHA2 preimage type support in preimage oracle

* contracts-bedrock: fix PreimageOracle.sol comment

Co-authored-by: clabby <[email protected]>

* contracts: IPreimageOracle sha2 extension

* interface

chores

---------

Co-authored-by: clabby <[email protected]>
  • Loading branch information
protolambda and clabby authored Jan 21, 2024
1 parent 5c39917 commit 2d8a7b4
Show file tree
Hide file tree
Showing 7 changed files with 104 additions and 8 deletions.
2 changes: 1 addition & 1 deletion op-bindings/bindings/alphabetvm.go

Large diffs are not rendered by default.

25 changes: 23 additions & 2 deletions op-bindings/bindings/preimageoracle.go

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion op-bindings/bindings/preimageoracle_more.go

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions packages/contracts-bedrock/slither-report.json
Original file line number Diff line number Diff line change
Expand Up @@ -1096,10 +1096,10 @@
"impact": "Medium",
"confidence": "Medium",
"check": "uninitialized-local",
"description": "PreimageOracle.challengeFirstLPP(address,uint256,PreimageOracle.Leaf,bytes32[]).stateMatrix (src/cannon/PreimageOracle.sol#408) is a local variable never initialized\n",
"description": "PreimageOracle.challengeFirstLPP(address,uint256,PreimageOracle.Leaf,bytes32[]).stateMatrix (src/cannon/PreimageOracle.sol#459) is a local variable never initialized\n",
"type": "variable",
"name": "stateMatrix",
"start": 18772,
"start": 20988,
"length": 40,
"filename_relative": "src/cannon/PreimageOracle.sol"
},
Expand Down
18 changes: 18 additions & 0 deletions packages/contracts-bedrock/snapshots/abi/PreimageOracle.json
Original file line number Diff line number Diff line change
Expand Up @@ -315,6 +315,24 @@
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [
{
"internalType": "uint256",
"name": "_partOffset",
"type": "uint256"
},
{
"internalType": "bytes",
"name": "_preimage",
"type": "bytes"
}
],
"name": "loadSha256PreimagePart",
"outputs": [],
"stateMutability": "nonpayable",
"type": "function"
},
{
"inputs": [],
"name": "minProposalSize",
Expand Down
51 changes: 51 additions & 0 deletions packages/contracts-bedrock/src/cannon/PreimageOracle.sol
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,57 @@ contract PreimageOracle is IPreimageOracle {
preimageLengths[key] = size;
}

/// @inheritdoc IPreimageOracle
function loadSha256PreimagePart(uint256 _partOffset, bytes calldata _preimage) external {
uint256 size;
bytes32 key;
bytes32 part;
assembly {
// len(sig) + len(partOffset) + len(preimage offset) = 4 + 32 + 32 = 0x44
size := calldataload(0x44)

// revert if part offset >= size+8 (i.e. parts must be within bounds)
if iszero(lt(_partOffset, add(size, 8))) {
// Store "PartOffsetOOB()"
mstore(0, 0xfe254987)
// Revert with "PartOffsetOOB()"
revert(0x1c, 4)
}
// we leave solidity slots 0x40 and 0x60 untouched,
// and everything after as scratch-memory.
let ptr := 0x80
// put size as big-endian uint64 at start of pre-image
mstore(ptr, shl(192, size))
ptr := add(ptr, 8)
// copy preimage payload into memory so we can hash and read it.
calldatacopy(ptr, _preimage.offset, size)
// Note that it includes the 8-byte big-endian uint64 length prefix.
// this will be zero-padded at the end, since memory at end is clean.
part := mload(add(sub(ptr, 8), _partOffset))

// compute SHA2-256 hash with pre-compile
let success :=
staticcall(
gas(), // Forward all available gas
0x02, // Address of SHA-256 precompile
ptr, // Start of input data in memory
size, // Size of input data
0, // Store output in scratch memory
0x20 // Output is always 32 bytes
)
// Check if the staticcall succeeded
if iszero(success) { revert(0, 0) }
let h := mload(0) // get return data
// mask out prefix byte, replace with type 4 byte
key := or(and(h, not(shl(248, 0xFF))), shl(248, 4))
}
preimagePartOk[key][_partOffset] = true;
preimageParts[key][_partOffset] = part;
preimageLengths[key] = size;
}

// TODO 4844 point-evaluation preimage

////////////////////////////////////////////////////////////////
// Large Preimage Proposals (External) //
////////////////////////////////////////////////////////////////
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,15 @@ interface IPreimageOracle {
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).
/// @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;
}

0 comments on commit 2d8a7b4

Please sign in to comment.