Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add test PolygonVerifierGateway & ConsensusEcdsa #380

Open
wants to merge 3 commits into
base: feature/al-v0.3.0
Choose a base branch
from
Open
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
12 changes: 8 additions & 4 deletions contracts/v2/PolygonVerifierGateway.sol
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ contract PolygonVerifierGateway is ISP1VerifierGateway, Initializable {
}

/**
* @notice Initializer function to set new rollup manager version
* @notice Initializer function to set admin address and pessimistic vkey
* @param _admin The address of the admin
* @param _pessimisticVKey The pessimistic program verification key
*/
Expand All @@ -78,7 +78,9 @@ contract PolygonVerifierGateway is ISP1VerifierGateway, Initializable {
_;
}

/// @inheritdoc ISP1VerifierGateway
/// @notice Verifies a proof with given public values and proofBytes.
/// @param publicValues The public values encoded as bytes.
/// @param proofBytes The proof of the program execution the SP1 zkVM encoded as bytes.
function verifyPessimisticProof(
bytes calldata publicValues,
bytes calldata proofBytes
Expand All @@ -102,7 +104,8 @@ contract PolygonVerifierGateway is ISP1VerifierGateway, Initializable {
// admin functions
//////////////////

/// @inheritdoc ISP1VerifierGateway
/// @notice Add route to verifier
/// @param verifier Verifier address
function addRoute(address verifier) external onlyAdmin {
bytes4 selector = bytes4(
ISP1VerifierWithHash(verifier).VERIFIER_HASH()
Expand All @@ -121,7 +124,8 @@ contract PolygonVerifierGateway is ISP1VerifierGateway, Initializable {
emit RouteAdded(selector, verifier);
}

/// @inheritdoc ISP1VerifierGateway
/// @notice Function to freeze route
/// @param selector Verifier selector
function freezeRoute(bytes4 selector) external onlyAdmin {
VerifierRoute storage route = routes[selector];
if (route.verifier == address(0)) {
Expand Down
4 changes: 2 additions & 2 deletions contracts/v2/lib/ALBaseConsensus.sol
Original file line number Diff line number Diff line change
Expand Up @@ -107,8 +107,8 @@ abstract contract ALBaseConsensus is IALConsensusBase, Initializable {
//////////////////

/**
* @notice Allow the admin to set a new trusted sequencer
* @param newConsensusVKey Address of the new trusted sequencer
* @notice Allow the admin to set a new consensus vkey
* @param newConsensusVKey bytes32, new consensus verification key
*/
function setConsensusVKey(bytes32 newConsensusVKey) external onlyAdmin {
consensusVKey = newConsensusVKey;
Expand Down
62 changes: 62 additions & 0 deletions contracts/verifiers/SP1Verifier_hash0.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

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

// Current deployments: https://github.com/succinctlabs/sp1-contracts/tree/main/contracts/deployments
// Local deployments should deploy this contract. Any existing chain should use already deployed contracts by SP1

/// @title SP1 Verifier
/// @author Succinct Labs
/// @notice This contracts implements a solidity verifier for SP1.
contract SP1Verifier_hash0 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 "v3.0.0";
}

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

/// @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();
}
}
}
165 changes: 165 additions & 0 deletions test/contractsv2/ConsensusEcdsa.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
/* eslint-disable no-plusplus, no-await-in-loop */
import {expect} from "chai";
import {ethers, upgrades} from "hardhat";
import {Address, ConsensusEcdsa} from "../../typechain-types";

describe("ConsensusEcdsa", () => {
let deployer: any;
let trustedSequencer: any;
let admin: any;

let consensusEcdsaContract: ConsensusEcdsa;

const gerManagerAddress = "0xA00000000000000000000000000000000000000A" as unknown as Address;
const polTokenAddress = "0xB00000000000000000000000000000000000000B" as unknown as Address;
const rollupManagerAddress = "0xC00000000000000000000000000000000000000C" as unknown as Address;
const bridgeAddress = "0xD00000000000000000000000000000000000000D" as unknown as Address;

const urlSequencer = "http://zkevm-json-rpc:8123";
const networkName = "zkevm";
const consensusVKey = "0x1122334455667788990011223344556677889900112233445566778899001122";

// Native token will be ether
const gasTokenAddress = ethers.ZeroAddress;

beforeEach("Deploy contract", async () => {
upgrades.silenceWarnings();

// load signers
[deployer, trustedSequencer, admin] = await ethers.getSigners();

// deploy consensus
// create polygonPessimisticConsensus implementation
const consensusEcdsaFactory = await ethers.getContractFactory("ConsensusEcdsa");
consensusEcdsaContract = await upgrades.deployProxy(consensusEcdsaFactory, [], {
initializer: false,
constructorArgs: [rollupManagerAddress],
unsafeAllow: ["constructor", "state-variable-immutable"],
});

await consensusEcdsaContract.waitForDeployment();
});

it("should check the initalized parameters", async () => {

// initialize zkEVM using non admin address
await expect(
consensusEcdsaContract.initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName
)
).to.be.revertedWithCustomError(consensusEcdsaContract, "OnlyRollupManager");

// initialize using rollup manager
await ethers.provider.send("hardhat_impersonateAccount", [rollupManagerAddress]);
const rollupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await consensusEcdsaContract.connect(rollupManagerSigner).initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

expect(await consensusEcdsaContract.admin()).to.be.equal(admin.address);
expect(await consensusEcdsaContract.trustedSequencer()).to.be.equal(trustedSequencer.address);
expect(await consensusEcdsaContract.trustedSequencerURL()).to.be.equal(urlSequencer);
expect(await consensusEcdsaContract.networkName()).to.be.equal(networkName);
expect(await consensusEcdsaContract.gasTokenAddress()).to.be.equal(gasTokenAddress);

// initialize again
await expect(
consensusEcdsaContract.connect(rollupManagerSigner).initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
)
).to.be.revertedWith("Initializable: contract is already initialized");
});

it("should check admin functions", async () => {
// initialize using rollup manager
await ethers.provider.send("hardhat_impersonateAccount", [rollupManagerAddress]);
const rollupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await consensusEcdsaContract.connect(rollupManagerSigner).initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

// setTrustedSequencer
await expect(consensusEcdsaContract.setTrustedSequencer(deployer.address)).to.be.revertedWithCustomError(
consensusEcdsaContract,
"OnlyAdmin"
);

await expect(consensusEcdsaContract.connect(admin).setTrustedSequencer(deployer.address))
.to.emit(consensusEcdsaContract, "SetTrustedSequencer")
.withArgs(deployer.address);

// setTrustedSequencerURL
await expect(consensusEcdsaContract.setTrustedSequencerURL("0x1253")).to.be.revertedWithCustomError(
consensusEcdsaContract,
"OnlyAdmin"
);
await expect(consensusEcdsaContract.connect(admin).setTrustedSequencerURL("0x1253"))
.to.emit(consensusEcdsaContract, "SetTrustedSequencerURL")
.withArgs("0x1253");

// transferAdminRole & acceptAdminRole
await expect(consensusEcdsaContract.connect(admin).transferAdminRole(deployer.address))
.to.emit(consensusEcdsaContract, "TransferAdminRole")
.withArgs(deployer.address);

await expect(consensusEcdsaContract.connect(admin).acceptAdminRole()).to.be.revertedWithCustomError(
consensusEcdsaContract,
"OnlyPendingAdmin"
);

await expect(consensusEcdsaContract.connect(deployer).acceptAdminRole())
.to.emit(consensusEcdsaContract, "AcceptAdminRole")
.withArgs(deployer.address);
});

it("should check getConsensusHash", async () => {
// initialize using rollup manager
await ethers.provider.send("hardhat_impersonateAccount", [rollupManagerAddress]);
const rollupManagerSigner = await ethers.getSigner(rollupManagerAddress as any);
await consensusEcdsaContract.connect(rollupManagerSigner).initialize(
consensusVKey,
admin.address,
trustedSequencer.address,
gasTokenAddress,
urlSequencer,
networkName,
{gasPrice: 0}
);

// pessimistic constant CONSENSUS_TYPE = 0;
const CONSENSUS_TYPE = 1;
const consensusConfig = ethers.solidityPackedKeccak256(["address"], [trustedSequencer.address]);
const consensusHashJs = ethers.solidityPackedKeccak256(
["uint32", "bytes32", "bytes32"],
[CONSENSUS_TYPE, consensusVKey, consensusConfig]
);

// getConsensusHash
const resGetConsensusHash = await consensusEcdsaContract.getConsensusHash("0x");

expect(resGetConsensusHash).to.be.equal(consensusHashJs);
});
});
Loading