Skip to content

Commit

Permalink
Merge pull request #322 from 0xPolygonHermez/feature/sp1-e2e
Browse files Browse the repository at this point in the history
e2e testing sp1
  • Loading branch information
invocamanman committed Sep 13, 2024
2 parents f0f4d7c + bb8c169 commit f2600ed
Show file tree
Hide file tree
Showing 8 changed files with 1,882 additions and 1 deletion.
1 change: 0 additions & 1 deletion contracts/v2/PolygonRollupManager.sol
Original file line number Diff line number Diff line change
Expand Up @@ -1079,7 +1079,6 @@ contract PolygonRollupManager is
inputPessimisticBytes,
proof
);

// TODO: Since there are no batches we could have either:
// A pool of POL for pessimistic, or make the fee system offchain, since there are already a
// dependency with the trusted aggregator ( or pessimistic aggregator)
Expand Down
10 changes: 10 additions & 0 deletions contracts/v2/mocks/PolygonRollupManagerMock.sol
Original file line number Diff line number Diff line change
Expand Up @@ -84,4 +84,14 @@ contract PolygonRollupManagerMock is PolygonRollupManager {
) public pure returns (bool) {
return _checkStateRootInsidePrime(newStateRoot);
}

function setRollupData(
uint32 rollupID,
bytes32 lastLocalExitRoot,
bytes32 lastPessimisticRoot
) external {
RollupData storage rollup = _rollupIDToRollupData[rollupID];
rollup.lastLocalExitRoot = lastLocalExitRoot;
rollup.lastPessimisticRoot = lastPessimisticRoot;
}
}
22 changes: 22 additions & 0 deletions contracts/v2/mocks/PolygonZkEVMGlobalExitRootV2Mock.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// SPDX-License-Identifier: AGPL-3.0
pragma solidity 0.8.20;
import "../PolygonZkEVMGlobalExitRootV2.sol";

/**
* PolygonRollupManager mock
*/
contract PolygonZkEVMGlobalExitRootV2Mock is PolygonZkEVMGlobalExitRootV2 {
/**
* @param _rollupManager Rollup manager contract address
* @param _bridgeAddress PolygonZkEVMBridge contract address
*/
constructor(
address _rollupManager,
address _bridgeAddress
) PolygonZkEVMGlobalExitRootV2(_rollupManager, _bridgeAddress) {}

function injectGER(bytes32 _root, uint32 depositCount) external {
globalExitRootMap[_root] = block.timestamp;
l1InfoRootMap[depositCount] = _root;
}
}
1,375 changes: 1,375 additions & 0 deletions contracts/verifiers/PlonkVerifier.sol

Large diffs are not rendered by default.

61 changes: 61 additions & 0 deletions contracts/verifiers/SP1Verifier.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ISP1Verifier, ISP1VerifierWithHash} from "../v2/interfaces/ISP1Verifier.sol";
import {PlonkVerifier} from "./PlonkVerifier.sol";

/// @title SP1 Verifier
/// @author Succinct Labs
/// @notice This contracts implements a solidity verifier for SP1.
contract SP1Verifier is PlonkVerifier, ISP1VerifierWithHash {
/// @notice Thrown when the verifier selector from this proof does not match the one in this
/// verifier. This indicates that this proof was sent to the wrong verifier.
/// @param received The verifier selector from the first 4 bytes of the proof.
/// @param expected The verifier selector from the first 4 bytes of the VERIFIER_HASH().
error WrongVerifierSelector(bytes4 received, bytes4 expected);

/// @notice Thrown when the proof is invalid.
error InvalidProof();

function VERSION() external pure returns (string memory) {
return "v1.1.0";
}

/// @inheritdoc ISP1VerifierWithHash
function VERIFIER_HASH() public pure returns (bytes32) {
return 0xc430ff7f31a22c5f7607f3ed2a2f5621af340bc45a44179319cba5761664e1f0;
}

/// @notice Hashes the public values to a field elements inside Bn254.
/// @param publicValues The public values.
function hashPublicValues(
bytes calldata publicValues
) public pure returns (bytes32) {
return sha256(publicValues) & bytes32(uint256((1 << 253) - 1));
}

/// @notice Verifies a proof with given public values and vkey.
/// @param programVKey The verification key for the RISC-V program.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyProof(
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) external view {
bytes4 receivedSelector = bytes4(proofBytes[:4]);
bytes4 expectedSelector = bytes4(VERIFIER_HASH());
if (receivedSelector != expectedSelector) {
revert WrongVerifierSelector(receivedSelector, expectedSelector);
}

bytes32 publicValuesDigest = hashPublicValues(publicValues);
uint256[] memory inputs = new uint256[](2);
inputs[0] = uint256(programVKey);
inputs[1] = uint256(publicValuesDigest);
bool success = this.Verify(proofBytes[4:], inputs);
if (!success) {
revert InvalidProof();
}
}
}
Loading

0 comments on commit f2600ed

Please sign in to comment.