Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions src/libraries/verifier/IOpenVmHalo2Verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// SPDX-License-Identifier: MIT

pragma solidity ^0.8.24;

/// @notice Typed entry point of the OpenVM Halo2 verifier wrapper
/// (`OpenVmHalo2Verifier`, selector `0x24270d54`).
/// @dev The wrapper formats the payload and self-calls its inherited
/// `Halo2Verifier` fallback. Reaching it through this typed function means the
/// call selector is fixed by the compiler, never taken from caller bytes.
interface IOpenVmHalo2Verifier {
/// @param publicValues The public values revealed by the guest program. For
/// Scroll this is the 32-byte `publicInputHash`.
/// @param proofData `abi.encodePacked(kzgAccumulator, proofSuffix)`, i.e. the
/// bundle proof exactly as `ScrollChain` forwards it.
/// @param appExeCommit Commitment to the application executable. For Scroll
/// this is the fixed `verifierDigest1`.
/// @param appVmCommit Commitment to the VM configuration. For Scroll this is
/// the fixed `verifierDigest2`.
function verify(
bytes calldata publicValues,
bytes calldata proofData,
bytes32 appExeCommit,
bytes32 appVmCommit
) external view;
}
69 changes: 28 additions & 41 deletions src/libraries/verifier/ZkEvmVerifierPostFeynman.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
pragma solidity =0.8.24;

import {IZkEvmVerifierV2} from "./IZkEvmVerifier.sol";
import {IOpenVmHalo2Verifier} from "./IOpenVmHalo2Verifier.sol";

// solhint-disable no-inline-assembly
// solhint-disable no-empty-blocks

contract ZkEvmVerifierPostFeynman is IZkEvmVerifierV2 {
/**********
Expand All @@ -14,10 +15,19 @@ contract ZkEvmVerifierPostFeynman is IZkEvmVerifierV2 {
/// @dev Thrown when bundle recursion zk proof verification is failed.
error VerificationFailed();

/// @dev Thrown when a verifier digest is not an element of the BN254 scalar field.
error InvalidVerifierDigest();

/// @dev Thrown when the plonk verifier address has no code.
error InvalidPlonkVerifier();

/*************
* Constants *
*************/

/// @dev The BN254 scalar field modulus. The wrapper rejects commitments at or above it.
uint256 private constant BN254_SCALAR_MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001;

/// @notice The address of highly optimized plonk verifier contract.
address public immutable plonkVerifier;

Expand All @@ -42,6 +52,10 @@ contract ZkEvmVerifierPostFeynman is IZkEvmVerifierV2 {
bytes32 _verifierDigest2,
uint256 _protocolVersion
) {
if (_verifier.code.length == 0) revert InvalidPlonkVerifier();
if (uint256(_verifierDigest1) >= BN254_SCALAR_MODULUS) revert InvalidVerifierDigest();
if (uint256(_verifierDigest2) >= BN254_SCALAR_MODULUS) revert InvalidVerifierDigest();

plonkVerifier = _verifier;
verifierDigest1 = _verifierDigest1;
verifierDigest2 = _verifierDigest2;
Expand All @@ -60,47 +74,20 @@ contract ZkEvmVerifierPostFeynman is IZkEvmVerifierV2 {
/// | 8 bytes | 32 bytes | 4 bytes | 32 bytes | 32 bytes | 32 bytes | 32 bytes | 32 bytes | 33 bytes |
/// ```
function verify(bytes calldata bundleProof, bytes calldata publicInput) external view override {
address _verifier = plonkVerifier;
bytes32 _verifierDigest1 = verifierDigest1;
bytes32 _verifierDigest2 = verifierDigest2;
bytes32 publicInputHash = keccak256(abi.encodePacked(protocolVersion, publicInput));
bool success;

// 1. the first 12 * 32 (0x180) bytes of `bundleProof` is `accumulator`
// 2. the rest bytes of `bundleProof` is the actual `bundle_proof`
// 3. Inserted between `accumulator` and `bundle_proof` are
// 32 * 34 (0x440) bytes, such that:
// | start | end | field |
// |---------------|---------------|-------------------------|
// | 0x00 | 0x180 | bundleProof[0x00:0x180] |
// | 0x180 | 0x180 + 0x20 | verifierDigest1 |
// | 0x180 + 0x20 | 0x180 + 0x40 | verifierDigest2 |
// | 0x180 + 0x40 | 0x180 + 0x60 | publicInputHash[0] |
// | 0x180 + 0x60 | 0x180 + 0x80 | publicInputHash[1] |
// ...
// | 0x180 + 0x420 | 0x180 + 0x440 | publicInputHash[31] |
// | 0x180 + 0x440 | dynamic | bundleProof[0x180:] |
assembly {
let p := mload(0x40)
// 1. copy the accumulator's 0x180 bytes
calldatacopy(p, bundleProof.offset, 0x180)
// 2. insert the public input's 0x440 bytes
mstore(add(p, 0x180), _verifierDigest1) // verifierDigest1
mstore(add(p, 0x1a0), _verifierDigest2) // verifierDigest2
for {
let i := 0
} lt(i, 0x400) {
i := add(i, 0x20)
} {
mstore(add(p, sub(0x5a0, i)), and(publicInputHash, 0xff))
publicInputHash := shr(8, publicInputHash)
}
// 3. copy all remaining bytes from bundleProof
calldatacopy(add(p, 0x5c0), add(bundleProof.offset, 0x180), sub(bundleProof.length, 0x180))
// 4. call plonk verifier
success := staticcall(gas(), _verifier, p, add(bundleProof.length, 0x440), 0x00, 0x00)
}
if (!success) {

// Use the wrapper's typed entry point, never a raw call: the wrapper has a function
// dispatcher, so raw calldata led by `bundleProof` could select `verify` itself with
// attacker-chosen public values. The wrapper lays out the same payload for its
// Halo2 fallback that this contract used to build by hand.
try
IOpenVmHalo2Verifier(plonkVerifier).verify(
abi.encodePacked(publicInputHash),
bundleProof,
verifierDigest1,
verifierDigest2
)
{} catch {
revert VerificationFailed();
}
}
Expand Down
164 changes: 164 additions & 0 deletions src/test/ZkEvmVerifierPostFeynman.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
// SPDX-License-Identifier: MIT

pragma solidity =0.8.24;

import {DSTestPlus} from "solmate/test/utils/DSTestPlus.sol";
import {Vm} from "forge-std/Vm.sol";

import {ZkEvmVerifierPostFeynman} from "../libraries/verifier/ZkEvmVerifierPostFeynman.sol";
import {IOpenVmHalo2Verifier} from "../libraries/verifier/IOpenVmHalo2Verifier.sol";

/// @dev Faithful stand-in for the OpenVM 2.0 `OpenVmHalo2Verifier` wrapper. It is
/// `view`, like the real one, and reproduces its external surface: the typed
/// `verify(...)` with selector 0x24270d54, the same length and BN254-modulus checks,
/// and the `proofData[0:4] == 0` assertion. A proof "verifies" only when the tuple
/// (publicValues, proofData, appExeCommit, appVmCommit) equals the one the honest
/// prover produced; this models that the wrapper binds the values it is given, the
/// property Option A' relies on.
contract MockOpenVmWrapper {
uint256 internal constant BN254_SCALAR_MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001;
uint256 internal constant PROOF_DATA_LENGTH = (12 + 43) * 32; // 1760
uint256 internal constant PUBLIC_VALUES_LENGTH = 32;

error InvalidPublicValuesLength(uint256 expected, uint256 actual);
error InvalidProofDataLength(uint256 expected, uint256 actual);
error InvalidAppExeCommit(bytes32 actual);
error InvalidAppVmCommit(bytes32 actual);
error ProofVerificationFailed();

bytes32 public immutable expExe;
bytes32 public immutable expVm;
bytes32 public immutable expPublicValuesHash;
bytes32 public immutable expProofHash;

constructor(
bytes32 _expExe,
bytes32 _expVm,
bytes32 _expPublicValuesHash,
bytes32 _expProofHash
) {
expExe = _expExe;
expVm = _expVm;
expPublicValuesHash = _expPublicValuesHash;
expProofHash = _expProofHash;
}

function verify(
bytes calldata publicValues,
bytes calldata proofData,
bytes32 appExeCommit,
bytes32 appVmCommit
) external view {
if (publicValues.length != PUBLIC_VALUES_LENGTH) {
revert InvalidPublicValuesLength(PUBLIC_VALUES_LENGTH, publicValues.length);
}
if (proofData.length != PROOF_DATA_LENGTH) revert InvalidProofDataLength(PROOF_DATA_LENGTH, proofData.length);
if (uint256(appExeCommit) >= BN254_SCALAR_MODULUS) revert InvalidAppExeCommit(appExeCommit);
if (uint256(appVmCommit) >= BN254_SCALAR_MODULUS) revert InvalidAppVmCommit(appVmCommit);
assert(bytes4(proofData[0:4]) == bytes4(0x00000000));

bool ok = appExeCommit == expExe &&
appVmCommit == expVm &&
keccak256(publicValues) == expPublicValuesHash &&
keccak256(proofData) == expProofHash;
if (!ok) revert ProofVerificationFailed();
}

fallback() external {}
}

contract ZkEvmVerifierPostFeynmanTest is DSTestPlus {
Vm internal constant vm = Vm(address(uint160(uint256(keccak256("hevm cheat code")))));

uint256 internal constant PROOF_DATA_LENGTH = (12 + 43) * 32; // 1760
uint256 internal constant PROTOCOL_VERSION = 10;

bytes32 internal constant DIGEST1 = bytes32(uint256(0x1111));
bytes32 internal constant DIGEST2 = bytes32(uint256(0x2222));

MockOpenVmWrapper internal wrapper;
ZkEvmVerifierPostFeynman internal verifier;

bytes internal honestProof;
bytes internal honestPublicInput;
bytes32 internal honestPiHash;

function setUp() public {
// A well-formed bundle proof: 1760 bytes, first 4 bytes zero (accumulator limb).
honestProof = new bytes(PROOF_DATA_LENGTH);
for (uint256 i = 4; i < PROOF_DATA_LENGTH; i++) honestProof[i] = bytes1(uint8(((i * 7) % 251) + 1));

honestPublicInput = abi.encodePacked(uint64(534352), keccak256("pi"), uint32(1));
honestPiHash = keccak256(abi.encodePacked(uint256(PROTOCOL_VERSION), honestPublicInput));

wrapper = new MockOpenVmWrapper(
DIGEST1,
DIGEST2,
keccak256(abi.encodePacked(honestPiHash)),
keccak256(honestProof)
);
verifier = new ZkEvmVerifierPostFeynman(address(wrapper), DIGEST1, DIGEST2, PROTOCOL_VERSION);
}

/// The call must reach the wrapper through selector 0x24270d54, carrying the
/// public-input hash as `publicValues` and the fixed digests as the commitments.
/// `bundleProof` is passed as the `proofData` argument, so its bytes can never
/// become the call selector — the old attack is structurally impossible.
function test_typedCall_dispatchAndArgs() external {
bytes memory expectedCalldata = abi.encodeWithSelector(
IOpenVmHalo2Verifier.verify.selector,
abi.encodePacked(honestPiHash),
honestProof,
DIGEST1,
DIGEST2
);
vm.expectCall(address(wrapper), expectedCalldata);
verifier.verify(honestProof, honestPublicInput); // must not revert
}

/// Public-input binding: the same proof is rejected for a different public input,
/// because `publicInputHash` (passed as publicValues) no longer matches. This is
/// the property the vulnerable raw path lost.
function test_forgedPublicInputRejected() external {
bytes memory forged = abi.encodePacked(honestPublicInput, uint8(1));
hevm.expectRevert(ZkEvmVerifierPostFeynman.VerificationFailed.selector);
verifier.verify(honestProof, forged);
}

/// A wrapper-side revert (wrong length, bad commitment, failed proof) surfaces as
/// VerificationFailed rather than bubbling the raw error.
function test_wrapperRevertSurfacedAsVerificationFailed() external {
bytes memory wrongLen = new bytes(PROOF_DATA_LENGTH - 32); // fails the length check
hevm.expectRevert(ZkEvmVerifierPostFeynman.VerificationFailed.selector);
verifier.verify(wrongLen, honestPublicInput);
}

/// The constructor accepts a digest if and only if it is below the BN254 scalar field modulus.
function testFuzz_constructorDigestRange(uint256 digest1, uint256 digest2) external {
uint256 modulus = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001;
if (digest1 >= modulus || digest2 >= modulus) {
hevm.expectRevert(ZkEvmVerifierPostFeynman.InvalidVerifierDigest.selector);
}
new ZkEvmVerifierPostFeynman(address(wrapper), bytes32(digest1), bytes32(digest2), PROTOCOL_VERSION);
}

function test_constructorDigestBoundary() external {
uint256 modulus = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001;
new ZkEvmVerifierPostFeynman(address(wrapper), bytes32(modulus - 1), bytes32(modulus - 1), PROTOCOL_VERSION);

hevm.expectRevert(ZkEvmVerifierPostFeynman.InvalidVerifierDigest.selector);
new ZkEvmVerifierPostFeynman(address(wrapper), bytes32(modulus), DIGEST2, PROTOCOL_VERSION);

hevm.expectRevert(ZkEvmVerifierPostFeynman.InvalidVerifierDigest.selector);
new ZkEvmVerifierPostFeynman(address(wrapper), DIGEST1, bytes32(modulus), PROTOCOL_VERSION);
}

/// The constructor rejects a plonk verifier without code, including the zero address.
function test_constructorRejectsVerifierWithoutCode() external {
hevm.expectRevert(ZkEvmVerifierPostFeynman.InvalidPlonkVerifier.selector);
new ZkEvmVerifierPostFeynman(address(0), DIGEST1, DIGEST2, PROTOCOL_VERSION);

hevm.expectRevert(ZkEvmVerifierPostFeynman.InvalidPlonkVerifier.selector);
new ZkEvmVerifierPostFeynman(address(0xBEEF), DIGEST1, DIGEST2, PROTOCOL_VERSION);
}
}
97 changes: 97 additions & 0 deletions src/test/ZkEvmVerifierPostFeynmanEncoding.t.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,97 @@
// SPDX-License-Identifier: MIT

pragma solidity =0.8.24;

import {Test} from "forge-std/Test.sol";
import {Vm, VmSafe} from "forge-std/Vm.sol";

import {ZkEvmVerifierPostFeynmanLegacy} from "./legacy/ZkEvmVerifierPostFeynmanLegacy.sol";
import {ZkEvmVerifierPostFeynman} from "../libraries/verifier/ZkEvmVerifierPostFeynman.sol";
// Vendored verbatim from scroll-tech/zkvm-prover@bf88715 releases/dev/verifier/verifier.sol
// (identical to openvm-solidity-sdk src/v2.0-base); only the pragma is relaxed. Its
// `Halo2Verifier` base is a stub that accepts every payload.
import {OpenVmHalo2Verifier} from "./openvm/OpenVmHalo2Verifier.sol";

/// Differential test: the payload that reaches the Halo2 fallback must be byte-identical
/// whether it is built by the old raw path (`ZkEvmVerifierPostFeynmanLegacy`) or by the OpenVM
/// wrapper's `_constructProof` behind the typed call (`ZkEvmVerifierPostFeynman`).
contract ZkEvmVerifierPostFeynmanEncodingTest is Test {
uint256 internal constant BN254_SCALAR_MODULUS = 0x30644e72e131a029b85045b68181585d2833e84879b9709143e1f593f0000001;
uint256 internal constant ACCUMULATOR_WORDS = 12;
uint256 internal constant SUFFIX_WORDS = 43;

OpenVmHalo2Verifier internal wrapper;

function setUp() public {
wrapper = new OpenVmHalo2Verifier();
}

function testFuzz_oldAndTypedPayloadsMatch(
bytes32 seed,
bytes calldata publicInput,
uint256 digest1,
uint256 digest2,
uint256 protocolVersion
) external {
// The wrapper rejects commitments outside the scalar field; the live digests are inside.
bytes32 d1 = bytes32(bound(digest1, 0, BN254_SCALAR_MODULUS - 1));
bytes32 d2 = bytes32(bound(digest2, 0, BN254_SCALAR_MODULUS - 1));
bytes memory bundleProof = _bundleProof(seed);

ZkEvmVerifierPostFeynmanLegacy oldVerifier = new ZkEvmVerifierPostFeynmanLegacy(
address(wrapper),
d1,
d2,
protocolVersion
);
ZkEvmVerifierPostFeynman newVerifier = new ZkEvmVerifierPostFeynman(address(wrapper), d1, d2, protocolVersion);

// Old path: the raw staticcall from the verifier into the wrapper's fallback.
vm.startStateDiffRecording();
oldVerifier.verify(bundleProof, publicInput);
bytes memory oldPayload = _calldataOf(vm.stopAndReturnStateDiff(), address(oldVerifier));

// New path: the wrapper's self-call into its own fallback.
vm.startStateDiffRecording();
newVerifier.verify(bundleProof, publicInput);
bytes memory newPayload = _calldataOf(vm.stopAndReturnStateDiff(), address(wrapper));

assertEq(oldPayload.length, (ACCUMULATOR_WORDS + 2 + 32 + SUFFIX_WORDS) * 32);
assertEq(newPayload, oldPayload);
}

/// The single call from `accessor` into the wrapper, which must be a successful staticcall.
function _calldataOf(VmSafe.AccountAccess[] memory accesses, address accessor)
internal
view
returns (bytes memory)
{
bytes memory data;
uint256 found;
for (uint256 i = 0; i < accesses.length; i++) {
VmSafe.AccountAccess memory a = accesses[i];
if (a.account != address(wrapper) || a.accessor != accessor) continue;
assertEq(uint256(a.kind), uint256(VmSafe.AccountAccessKind.StaticCall));
assertFalse(a.reverted);
data = a.data;
found++;
}
assertEq(found, 1);
return data;
}

/// A 1760-byte bundle proof. Every word is random except that the first accumulator
/// word has its top 4 bytes cleared: a valid proof always has this (88-bit limbs), and
/// it is what makes the old raw call reach the fallback.
function _bundleProof(bytes32 seed) internal pure returns (bytes memory proof) {
uint256 words = ACCUMULATOR_WORDS + SUFFIX_WORDS;
proof = new bytes(words * 32);
for (uint256 i = 0; i < words; i++) {
uint256 word = uint256(keccak256(abi.encode(seed, i)));
if (i == 0) word >>= 32;
assembly {
mstore(add(add(proof, 0x20), mul(i, 0x20)), word)
}
}
}
}
Loading
Loading