|
| 1 | +// SPDX-License-Identifier: UNLICENSED |
| 2 | +pragma solidity ^0.8.12; |
| 3 | + |
| 4 | +import {Initializable} from "@openzeppelin-upgrades/contracts/proxy/utils/Initializable.sol"; |
| 5 | +import {OwnableUpgradeable} from "@openzeppelin-upgrades/contracts/access/OwnableUpgradeable.sol"; |
| 6 | +import {UUPSUpgradeable} from "@openzeppelin-upgrades/contracts/proxy/utils/UUPSUpgradeable.sol"; |
| 7 | +import {IAlignedProofAggregationService} from "./IAlignedProofAggregationService.sol"; |
| 8 | +import {ISP1Verifier} from "@sp1-contracts/ISP1Verifier.sol"; |
| 9 | + |
| 10 | +contract AlignedProofAggregationService is |
| 11 | + IAlignedProofAggregationService, |
| 12 | + Initializable, |
| 13 | + OwnableUpgradeable, |
| 14 | + UUPSUpgradeable |
| 15 | +{ |
| 16 | + /// @notice Maps the aggregated verification merkle root with the blob transaction hash that contains the leaves |
| 17 | + uint64 public currentAggregatedProofNumber; |
| 18 | + mapping(uint64 => AggregatedProof) public aggregatedProofs; |
| 19 | + |
| 20 | + /// @notice The address of the SP1 verifier contract. |
| 21 | + /// @dev This can either be a specific SP1Verifier for a specific version, or the |
| 22 | + /// SP1VerifierGateway which can be used to verify proofs for any version of SP1. |
| 23 | + /// For the list of supported verifiers on each chain, see: |
| 24 | + /// https://docs.succinct.xyz/onchain-verification/contract-addresses |
| 25 | + address public sp1VerifierAddress; |
| 26 | + |
| 27 | + address public alignedAggregatorAddress; |
| 28 | + |
| 29 | + /// @notice whether we are in dev mode or not |
| 30 | + /// if the sp1 verifier address is set to this address, then we skip verification |
| 31 | + address public constant VERIFIER_MOCK_ADDRESS = address(0xFF); |
| 32 | + |
| 33 | + constructor() { |
| 34 | + _disableInitializers(); |
| 35 | + } |
| 36 | + |
| 37 | + function initialize(address newOwner, address _alignedAggregatorAddress, address _sp1VerifierAddress) |
| 38 | + public |
| 39 | + initializer |
| 40 | + { |
| 41 | + __Ownable_init(); |
| 42 | + __UUPSUpgradeable_init(); |
| 43 | + _transferOwnership(newOwner); |
| 44 | + alignedAggregatorAddress = _alignedAggregatorAddress; |
| 45 | + sp1VerifierAddress = _sp1VerifierAddress; |
| 46 | + } |
| 47 | + |
| 48 | + function verify( |
| 49 | + bytes32 blobTransactionHash, |
| 50 | + bytes32 sp1ProgramVKey, |
| 51 | + bytes calldata sp1PublicValues, |
| 52 | + bytes calldata sp1ProofBytes |
| 53 | + ) public onlyAlignedAggregator { |
| 54 | + // In dev mode, poofs are mocked, so we skip the verification part |
| 55 | + if (sp1VerifierAddress == VERIFIER_MOCK_ADDRESS) { |
| 56 | + (bytes32 merkleRoot) = abi.decode(sp1PublicValues, (bytes32)); |
| 57 | + _newAggregatedProof(merkleRoot, blobTransactionHash); |
| 58 | + return; |
| 59 | + } |
| 60 | + |
| 61 | + try ISP1Verifier(sp1VerifierAddress).verifyProof(sp1ProgramVKey, sp1PublicValues, sp1ProofBytes) { |
| 62 | + (bytes32 merkleRoot) = abi.decode(sp1PublicValues, (bytes32)); |
| 63 | + _newAggregatedProof(merkleRoot, blobTransactionHash); |
| 64 | + } catch { |
| 65 | + emit AggregatedProofFailed(currentAggregatedProofNumber); |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + function markCurrentAggregatedProofAsMissed() public onlyAlignedAggregator { |
| 70 | + emit AggregatedProofMissed(currentAggregatedProofNumber); |
| 71 | + currentAggregatedProofNumber += 1; |
| 72 | + } |
| 73 | + |
| 74 | + function _newAggregatedProof(bytes32 merkleRoot, bytes32 blobHash) internal { |
| 75 | + AggregatedProof storage proof = aggregatedProofs[currentAggregatedProofNumber]; |
| 76 | + proof.merkleRoot = merkleRoot; |
| 77 | + proof.blobHash = blobHash; |
| 78 | + proof.status = AggregatedProofStatus.Verified; |
| 79 | + emit NewAggregatedProofVerified(currentAggregatedProofNumber, merkleRoot, blobHash); |
| 80 | + currentAggregatedProofNumber += 1; |
| 81 | + } |
| 82 | + |
| 83 | + function _authorizeUpgrade(address newImplementation) |
| 84 | + internal |
| 85 | + override |
| 86 | + onlyOwner // solhint-disable-next-line no-empty-blocks |
| 87 | + {} |
| 88 | + |
| 89 | + modifier onlyAlignedAggregator() { |
| 90 | + if (msg.sender != alignedAggregatorAddress) { |
| 91 | + revert OnlyAlignedAggregator(msg.sender); |
| 92 | + } |
| 93 | + _; |
| 94 | + } |
| 95 | +} |
0 commit comments