|
| 1 | +// Copyright 2025 RISC Zero, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | +// |
| 15 | +// SPDX-License-Identifier: Apache-2.0 |
| 16 | + |
| 17 | +pragma solidity ^0.8.17; |
| 18 | + |
| 19 | +import {Test} from "forge-std/Test.sol"; |
| 20 | + |
| 21 | +import {Steel, Beacon, Encoding} from "../steel/Steel.sol"; |
| 22 | + |
| 23 | +contract SteelVerifier { |
| 24 | + function validateCommitment(Steel.Commitment memory commitment) external view returns (bool) { |
| 25 | + return Steel.validateCommitment(commitment); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +contract SteelTest is Test { |
| 30 | + SteelVerifier internal verifier; |
| 31 | + |
| 32 | + function setUp() public { |
| 33 | + verifier = new SteelVerifier(); |
| 34 | + } |
| 35 | + |
| 36 | + function createCommitment(uint240 claimID, uint16 version, bytes32 digest) |
| 37 | + internal |
| 38 | + pure |
| 39 | + returns (Steel.Commitment memory) |
| 40 | + { |
| 41 | + return |
| 42 | + Steel.Commitment({id: Encoding.encodeVersionedID(claimID, version), digest: digest, configID: bytes32(0)}); |
| 43 | + } |
| 44 | + |
| 45 | + function test_ValidateCommitment_V0_Block_Success() public { |
| 46 | + vm.roll(block.number + 10); |
| 47 | + uint256 targetBlockNumber = block.number - 5; |
| 48 | + bytes32 targetBlockHash = blockhash(targetBlockNumber); |
| 49 | + assertTrue(targetBlockHash != bytes32(0), "Test setup: blockhash(target) is zero"); |
| 50 | + |
| 51 | + Steel.Commitment memory c = createCommitment(uint240(targetBlockNumber), 0, targetBlockHash); |
| 52 | + assertTrue(verifier.validateCommitment(c), "V0 valid block commitment failed"); |
| 53 | + } |
| 54 | + |
| 55 | + function test_ValidateCommitment_V0_Block_WrongHash() public { |
| 56 | + vm.roll(block.number + 10); |
| 57 | + uint256 targetBlockNumber = block.number - 5; |
| 58 | + bytes32 wrongHash = keccak256(abi.encodePacked("wrong_hash")); |
| 59 | + assertTrue(blockhash(targetBlockNumber) != bytes32(0), "Test setup: blockhash(target) is zero"); |
| 60 | + |
| 61 | + Steel.Commitment memory c = createCommitment(uint240(targetBlockNumber), 0, wrongHash); |
| 62 | + assertFalse(verifier.validateCommitment(c), "V0 wrong block hash should be invalid"); |
| 63 | + } |
| 64 | + |
| 65 | + function test_ValidateCommitment_V0_Block_TooOld() public { |
| 66 | + vm.roll(block.number + 300); |
| 67 | + uint256 oldBlockNumber = 1; |
| 68 | + bytes32 someHash = keccak256(abi.encodePacked("some_hash")); |
| 69 | + |
| 70 | + Steel.Commitment memory c = createCommitment(uint240(oldBlockNumber), 0, someHash); |
| 71 | + vm.expectPartialRevert(Steel.CommitmentTooOld.selector); |
| 72 | + verifier.validateCommitment(c); |
| 73 | + } |
| 74 | + |
| 75 | + function test_ValidateCommitment_V1_Beacon_Success() public { |
| 76 | + uint256 timestamp = 1700000000; |
| 77 | + bytes32 expectedRoot = keccak256(abi.encodePacked("beacon_root_v1")); |
| 78 | + |
| 79 | + // Mock the call to Beacon.BEACON_ROOTS_ADDRESS |
| 80 | + vm.mockCall(Beacon.BEACON_ROOTS_ADDRESS, abi.encode(timestamp), abi.encode(expectedRoot)); |
| 81 | + |
| 82 | + Steel.Commitment memory c = createCommitment( |
| 83 | + uint240(timestamp), // claimID is timestamp for V1 |
| 84 | + 1, |
| 85 | + expectedRoot |
| 86 | + ); |
| 87 | + assertTrue(verifier.validateCommitment(c), "V1 valid beacon commitment failed"); |
| 88 | + } |
| 89 | + |
| 90 | + function test_ValidateCommitment_V1_Beacon_WrongRoot() public { |
| 91 | + uint256 timestamp = 1700000000; |
| 92 | + bytes32 correctRoot = keccak256(abi.encodePacked("beacon_root_v1")); |
| 93 | + bytes32 wrongRootInCommitment = keccak256(abi.encodePacked("wrong_root")); |
| 94 | + |
| 95 | + // Mock the call to Beacon.BEACON_ROOTS_ADDRESS to return the correctRoot |
| 96 | + vm.mockCall(Beacon.BEACON_ROOTS_ADDRESS, abi.encode(timestamp), abi.encode(correctRoot)); |
| 97 | + |
| 98 | + Steel.Commitment memory c = createCommitment( |
| 99 | + uint240(timestamp), |
| 100 | + 1, |
| 101 | + wrongRootInCommitment // Commitment has the wrong root |
| 102 | + ); |
| 103 | + assertFalse(verifier.validateCommitment(c), "V1 wrong beacon root should be invalid"); |
| 104 | + } |
| 105 | + |
| 106 | + function test_ValidateCommitment_V1_Beacon_InvalidTimestamp() public { |
| 107 | + uint256 invalidTimestamp = 1700000001; |
| 108 | + |
| 109 | + // Mock the call to Beacon.BEACON_ROOTS_ADDRESS to revert |
| 110 | + vm.mockCallRevert( |
| 111 | + Beacon.BEACON_ROOTS_ADDRESS, |
| 112 | + abi.encode(invalidTimestamp), |
| 113 | + bytes("Mocked EIP-4788 Revert for invalid timestamp") |
| 114 | + ); |
| 115 | + |
| 116 | + Steel.Commitment memory c = |
| 117 | + createCommitment(uint240(invalidTimestamp), 1, keccak256(abi.encodePacked("any_root"))); |
| 118 | + vm.expectRevert(Beacon.InvalidBlockTimestamp.selector); |
| 119 | + verifier.validateCommitment(c); |
| 120 | + } |
| 121 | + |
| 122 | + function test_ValidateCommitment_V2_Reverts_ConsensusSlotNotSupported() public { |
| 123 | + uint240 claimID = 999; |
| 124 | + Steel.Commitment memory c = createCommitment(claimID, 2, keccak256(abi.encodePacked("any_digest_v2"))); |
| 125 | + vm.expectRevert(Steel.ConsensusSlotCommitmentNotSupported.selector); |
| 126 | + verifier.validateCommitment(c); |
| 127 | + } |
| 128 | + |
| 129 | + function test_ValidateCommitment_V3_Reverts_InvalidCommitmentVersion() public { |
| 130 | + uint240 claimID = 1000; |
| 131 | + Steel.Commitment memory c = createCommitment( |
| 132 | + claimID, |
| 133 | + 3, // Any version > 2 not explicitly handled |
| 134 | + keccak256(abi.encodePacked("any_digest_v3")) |
| 135 | + ); |
| 136 | + vm.expectRevert(Steel.InvalidCommitmentVersion.selector); |
| 137 | + verifier.validateCommitment(c); |
| 138 | + } |
| 139 | +} |
0 commit comments