2021-11-01 11:30:35 +00:00
|
|
|
// SPDX-License-Identifier: MIT
|
2024-01-23 09:24:02 +00:00
|
|
|
pragma solidity 0.8.23;
|
2021-11-01 11:30:35 +00:00
|
|
|
|
2023-01-17 12:55:58 +00:00
|
|
|
import "./Configuration.sol";
|
2023-01-09 13:20:59 +00:00
|
|
|
import "./Requests.sol";
|
2023-01-10 13:16:32 +00:00
|
|
|
import "./Periods.sol";
|
2024-01-18 12:50:54 +00:00
|
|
|
import "./Groth16.sol";
|
2024-01-05 11:27:53 +00:00
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @title Proofs
|
|
|
|
* @notice Abstract contract that handles proofs tracking, validation and reporting functionality
|
|
|
|
*/
|
2023-01-10 13:16:32 +00:00
|
|
|
abstract contract Proofs is Periods {
|
2023-01-23 10:57:10 +00:00
|
|
|
ProofConfig private _config;
|
2024-01-23 12:28:53 +00:00
|
|
|
IGroth16Verifier private _verifier;
|
2023-01-17 12:55:58 +00:00
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* Creation of the contract requires at least 256 mined blocks!
|
|
|
|
* @param config Proving configuration
|
|
|
|
*/
|
2024-01-10 14:12:18 +00:00
|
|
|
constructor(
|
|
|
|
ProofConfig memory config,
|
2024-01-23 12:28:53 +00:00
|
|
|
IGroth16Verifier verifier
|
2024-01-10 14:12:18 +00:00
|
|
|
) Periods(config.period) {
|
2022-03-09 10:21:19 +00:00
|
|
|
require(block.number > 256, "Insufficient block height");
|
2023-01-23 10:57:10 +00:00
|
|
|
_config = config;
|
2024-01-10 14:12:06 +00:00
|
|
|
_verifier = verifier;
|
2022-03-02 14:44:58 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
mapping(SlotId => uint256) private _slotStarts; // TODO: Should be smaller than uint256
|
2023-01-23 10:57:10 +00:00
|
|
|
mapping(SlotId => uint256) private _probabilities;
|
2024-08-08 09:35:35 +00:00
|
|
|
mapping(SlotId => uint256) private _missed; // TODO: Should be smaller than uint256
|
2023-01-23 10:57:10 +00:00
|
|
|
mapping(SlotId => mapping(Period => bool)) private _received;
|
|
|
|
mapping(SlotId => mapping(Period => bool)) private _missing;
|
2021-11-01 11:30:35 +00:00
|
|
|
|
2023-01-18 14:26:21 +00:00
|
|
|
function slotState(SlotId id) public view virtual returns (SlotState);
|
2021-11-03 12:24:50 +00:00
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @return Number of missed proofs since Slot was Filled
|
|
|
|
*/
|
2023-01-09 13:41:28 +00:00
|
|
|
function missingProofs(SlotId slotId) public view returns (uint256) {
|
2023-01-23 10:57:10 +00:00
|
|
|
return _missed[slotId];
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param slotId Slot's ID for which the proofs should be reset
|
|
|
|
* @notice Resets the missing proofs counter to zero
|
|
|
|
*/
|
2023-05-30 12:42:59 +00:00
|
|
|
function _resetMissingProofs(SlotId slotId) internal {
|
2023-03-08 16:19:49 +00:00
|
|
|
_missed[slotId] = 0;
|
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the proofs should be started to require
|
|
|
|
* @param probability Integer which specifies the probability of how often the proofs will be required. Lower number means higher probability.
|
|
|
|
* @notice Notes down the block's timestamp as Slot's starting time for requiring proofs
|
|
|
|
* and saves the required probability.
|
|
|
|
*/
|
2023-01-10 11:51:26 +00:00
|
|
|
function _startRequiringProofs(SlotId id, uint256 probability) internal {
|
2023-01-23 10:57:10 +00:00
|
|
|
_slotStarts[id] = block.timestamp;
|
|
|
|
_probabilities[id] = probability;
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the pointer should be calculated
|
|
|
|
* @param period Period for which the pointer should be calculated
|
|
|
|
* @return Uint8 pointer that is stable over current Period, ie an integer offset [0-255] of the last 256 blocks, pointing to a block that remains constant for the entire Period's duration.
|
|
|
|
* @dev For more information see [timing of storage proofs](https://github.com/codex-storage/codex-research/blob/41c4b4409d2092d0a5475aca0f28995034e58d14/design/storage-proof-timing.md)
|
|
|
|
*/
|
2023-01-19 15:47:29 +00:00
|
|
|
function _getPointer(SlotId id, Period period) internal view returns (uint8) {
|
2022-03-10 09:12:03 +00:00
|
|
|
uint256 blockNumber = block.number % 256;
|
2023-12-14 23:02:03 +00:00
|
|
|
// To ensure the pointer does not remain in downtime for many consecutive
|
|
|
|
// periods, for each period increase, move the pointer 67 blocks. We've
|
|
|
|
// chosen a prime number to ensure that we don't get cycles.
|
|
|
|
uint256 periodNumber = (Period.unwrap(period) * 67) % 256;
|
2023-01-09 13:20:59 +00:00
|
|
|
uint256 idOffset = uint256(SlotId.unwrap(id)) % 256;
|
2022-03-10 09:12:03 +00:00
|
|
|
uint256 pointer = (blockNumber + periodNumber + idOffset) % 256;
|
|
|
|
return uint8(pointer);
|
|
|
|
}
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the pointer should be calculated
|
|
|
|
* @return Uint8 pointer that is stable over current Period, ie an integer offset [0-255] of the last 256 blocks, pointing to a block that remains constant for the entire Period's duration.
|
|
|
|
* @dev For more information see [timing of storage proofs](https://github.com/codex-storage/codex-research/blob/41c4b4409d2092d0a5475aca0f28995034e58d14/design/storage-proof-timing.md)
|
|
|
|
*/
|
2023-01-17 07:37:59 +00:00
|
|
|
function getPointer(SlotId id) public view returns (uint8) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _getPointer(id, _blockPeriod());
|
2022-03-10 12:35:41 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param pointer Integer [0-255] that indicates an offset of the last 256 blocks, pointing to a block that remains constant for the entire Period's duration.
|
|
|
|
* @return Challenge that should be used for generation of proofs
|
|
|
|
*/
|
2023-01-19 15:47:29 +00:00
|
|
|
function _getChallenge(uint8 pointer) internal view returns (bytes32) {
|
2022-03-10 09:12:03 +00:00
|
|
|
bytes32 hash = blockhash(block.number - 1 - pointer);
|
|
|
|
assert(uint256(hash) != 0);
|
|
|
|
return keccak256(abi.encode(hash));
|
|
|
|
}
|
2022-03-08 14:58:08 +00:00
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the challenge should be calculated
|
|
|
|
* @param period Period for which the challenge should be calculated
|
|
|
|
* @return Challenge that should be used for generation of proofs
|
|
|
|
*/
|
2023-01-19 15:47:29 +00:00
|
|
|
function _getChallenge(
|
2023-01-10 10:04:04 +00:00
|
|
|
SlotId id,
|
2023-01-17 08:12:15 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) internal view returns (bytes32) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _getChallenge(_getPointer(id, period));
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the challenge should be calculated
|
|
|
|
* @return Challenge for current Period that should be used for generation of proofs
|
|
|
|
*/
|
2023-01-17 07:37:59 +00:00
|
|
|
function getChallenge(SlotId id) public view returns (bytes32) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _getChallenge(id, _blockPeriod());
|
2022-03-10 12:04:46 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the requirements are gathered. If the Slot's state is other than Filled, `false` is always returned.
|
|
|
|
* @param period Period for which the requirements are gathered.
|
|
|
|
*/
|
2023-01-10 10:04:04 +00:00
|
|
|
function _getProofRequirement(
|
|
|
|
SlotId id,
|
2023-01-17 08:12:15 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) internal view returns (bool isRequired, uint8 pointer) {
|
2023-01-16 15:31:04 +00:00
|
|
|
SlotState state = slotState(id);
|
2023-01-23 10:57:10 +00:00
|
|
|
Period start = _periodOf(_slotStarts[id]);
|
2023-01-19 15:47:29 +00:00
|
|
|
if (state != SlotState.Filled || !_isAfter(period, start)) {
|
2022-04-05 09:27:02 +00:00
|
|
|
return (false, 0);
|
2022-03-10 09:12:03 +00:00
|
|
|
}
|
2023-01-19 15:47:29 +00:00
|
|
|
pointer = _getPointer(id, period);
|
|
|
|
bytes32 challenge = _getChallenge(pointer);
|
2024-08-08 09:35:35 +00:00
|
|
|
|
|
|
|
/// Scaling of the probability according the downtime configuration
|
|
|
|
/// See: https://github.com/codex-storage/codex-research/blob/41c4b4409d2092d0a5475aca0f28995034e58d14/design/storage-proof-timing.md#pointer-downtime
|
2023-01-23 10:57:10 +00:00
|
|
|
uint256 probability = (_probabilities[id] * (256 - _config.downtime)) / 256;
|
2023-01-18 15:05:32 +00:00
|
|
|
isRequired = probability == 0 || uint256(challenge) % probability == 0;
|
2022-04-05 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* See isProofRequired
|
|
|
|
*/
|
2023-01-19 15:47:29 +00:00
|
|
|
function _isProofRequired(
|
2023-01-10 10:04:04 +00:00
|
|
|
SlotId id,
|
2023-01-17 08:12:15 +00:00
|
|
|
Period period
|
2023-01-10 10:04:04 +00:00
|
|
|
) internal view returns (bool) {
|
2022-04-05 09:27:02 +00:00
|
|
|
bool isRequired;
|
|
|
|
uint8 pointer;
|
2023-01-17 08:12:15 +00:00
|
|
|
(isRequired, pointer) = _getProofRequirement(id, period);
|
2023-01-23 10:57:10 +00:00
|
|
|
return isRequired && pointer >= _config.downtime;
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* @param id Slot's ID for which the proof requirements should be checked. If the Slot's state is other than Filled, `false` is always returned.
|
|
|
|
* @return bool indicating if proof is required for current period
|
|
|
|
*/
|
2023-01-17 07:37:59 +00:00
|
|
|
function isProofRequired(SlotId id) public view returns (bool) {
|
2023-01-19 15:47:29 +00:00
|
|
|
return _isProofRequired(id, _blockPeriod());
|
2022-03-08 14:58:08 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* Proof Downtime specifies part of the Period when the proof is not required even
|
|
|
|
* if the proof should be required. This function returns true if the pointer is
|
|
|
|
* in downtime (hence no proof required now) and at the same time the proof
|
|
|
|
* will be required later on in the Period.
|
|
|
|
*
|
|
|
|
* @dev for more info about downtime see [timing of storage proofs](https://github.com/codex-storage/codex-research/blob/41c4b4409d2092d0a5475aca0f28995034e58d14/design/storage-proof-timing.md#pointer-downtime)
|
|
|
|
* @param id SlotId for which the proof requirements should be checked. If the Slot's state is other than Filled, `false` is always returned.
|
|
|
|
* @return bool
|
|
|
|
*/
|
2023-01-17 07:37:59 +00:00
|
|
|
function willProofBeRequired(SlotId id) public view returns (bool) {
|
2022-04-05 09:27:02 +00:00
|
|
|
bool isRequired;
|
|
|
|
uint8 pointer;
|
2023-01-19 15:47:29 +00:00
|
|
|
(isRequired, pointer) = _getProofRequirement(id, _blockPeriod());
|
2023-01-23 10:57:10 +00:00
|
|
|
return isRequired && pointer < _config.downtime;
|
2022-04-05 09:27:02 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* Function used for submitting and verification of the proofs.
|
|
|
|
*
|
|
|
|
* @dev Reverts when proof is invalid or had been already submitted.
|
|
|
|
* @dev Emits ProofSubmitted event.
|
|
|
|
* @param id Slot's ID for which the proof requirements should be checked
|
|
|
|
* @param proof Groth16 proof
|
|
|
|
* @param pubSignals Proofs public input
|
|
|
|
*/
|
2024-01-22 15:43:03 +00:00
|
|
|
function _proofReceived(
|
|
|
|
SlotId id,
|
|
|
|
Groth16Proof calldata proof,
|
2024-01-23 11:50:14 +00:00
|
|
|
uint[] memory pubSignals
|
2024-01-22 15:43:03 +00:00
|
|
|
) internal {
|
2023-01-23 10:57:10 +00:00
|
|
|
require(!_received[id][_blockPeriod()], "Proof already submitted");
|
2024-01-23 12:28:53 +00:00
|
|
|
require(_verifier.verify(proof, pubSignals), "Invalid proof");
|
2023-01-23 10:57:10 +00:00
|
|
|
_received[id][_blockPeriod()] = true;
|
2024-01-15 15:25:30 +00:00
|
|
|
emit ProofSubmitted(id);
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
|
|
|
|
2024-08-08 09:35:35 +00:00
|
|
|
/**
|
|
|
|
* Function used to mark proof as missing.
|
|
|
|
*
|
|
|
|
* @param id Slot's ID for which the proof is missing
|
|
|
|
* @param missedPeriod Period for which the proof was missed
|
|
|
|
* @dev Reverts when:
|
|
|
|
* - missedPeriod has not ended yet ended
|
|
|
|
* - missing proof was time-barred
|
|
|
|
* - proof was not required for missedPeriod period
|
|
|
|
* - proof was already marked as missing
|
|
|
|
*/
|
2023-01-10 13:16:32 +00:00
|
|
|
function _markProofAsMissing(SlotId id, Period missedPeriod) internal {
|
2023-01-19 15:47:29 +00:00
|
|
|
uint256 end = _periodEnd(missedPeriod);
|
2023-01-17 12:55:58 +00:00
|
|
|
require(end < block.timestamp, "Period has not ended yet");
|
2023-01-23 10:57:10 +00:00
|
|
|
require(block.timestamp < end + _config.timeout, "Validation timed out");
|
|
|
|
require(!_received[id][missedPeriod], "Proof was submitted, not missing");
|
2023-01-19 15:47:29 +00:00
|
|
|
require(_isProofRequired(id, missedPeriod), "Proof was not required");
|
2023-01-23 10:57:10 +00:00
|
|
|
require(!_missing[id][missedPeriod], "Proof already marked as missing");
|
|
|
|
_missing[id][missedPeriod] = true;
|
|
|
|
_missed[id] += 1;
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|
2022-04-12 06:43:47 +00:00
|
|
|
|
2024-01-15 15:25:30 +00:00
|
|
|
event ProofSubmitted(SlotId id);
|
2021-11-01 11:30:35 +00:00
|
|
|
}
|