Commit 2d8a7b41 authored by protolambda's avatar protolambda Committed by GitHub

fp contracts: SHA2 preimage type support in preimage oracle (#9066)

* fp contracts: SHA2 preimage type support in preimage oracle

* contracts-bedrock: fix PreimageOracle.sol comment
Co-authored-by: default avatarclabby <ben@clab.by>

* contracts: IPreimageOracle sha2 extension

* interface

chores

---------
Co-authored-by: default avatarclabby <ben@clab.by>
parent 5c399172
This diff is collapsed.
This diff is collapsed.
This diff is collapsed.
......@@ -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"
},
......
......@@ -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",
......
......@@ -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) //
////////////////////////////////////////////////////////////////
......
......@@ -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;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment