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

feat: SP1VerifierGateway #7

Merged
merged 19 commits into from
Jun 20, 2024
38 changes: 38 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
name: Unit Test

on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:

env:
FOUNDRY_PROFILE: ci

jobs:
sol:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Install Foundry
uses: "foundry-rs/foundry-toolchain@v1"

- name: Build the contracts and print their size
working-directory: ./contracts
run: forge build --sizes

- name: Ensure files are the same as forge fmt
working-directory: ./contracts
run: forge fmt --check

- name: Run the tests
working-directory: ./contracts
run: forge test

- name: Add test summary
run: |
echo "## Tests result" >> $GITHUB_STEP_SUMMARY
echo "✅ Passed" >> $GITHUB_STEP_SUMMARY
5 changes: 5 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
[submodule "contracts/lib/forge-std"]
path = contracts/lib/forge-std
url = https://github.com/foundry-rs/forge-std
tag = v1.8.2
[submodule "contracts/lib/openzeppelin-contracts"]
path = contracts/lib/openzeppelin-contracts
url = https://github.com/openzeppelin/openzeppelin-contracts
tag = v5.0.2
34 changes: 0 additions & 34 deletions contracts/.github/workflows/test.yml

This file was deleted.

21 changes: 21 additions & 0 deletions contracts/.solhint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
{
"extends": "solhint:recommended",
"rules": {
"code-complexity": [
"error",
18
],
"compiler-version": [
"error",
">=0.8.20"
],
"max-line-length": [
"off",
100
],
"no-console": "off",
"var-name-mixedcase": "off",
"func-name-mixedcase": "off",
"avoid-low-level-calls": "off"
}
}
9 changes: 8 additions & 1 deletion contracts/foundry.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,12 @@
src = "src"
out = "out"
libs = ["lib"]
solc = '0.8.20'

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
[fmt]
line_length = 100
tab_width = 4
func_attrs_with_params_multiline = true
ignore = ["lib/**"]

# See more config options https://github.com/foundry-rs/foundry/blob/master/crates/config/README.md#all-options
1 change: 1 addition & 0 deletions contracts/lib/openzeppelin-contracts
Submodule openzeppelin-contracts added at dbb610
4 changes: 2 additions & 2 deletions contracts/script/SP1Verifier.s.sol
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// SPDX-License-Identifier: UNLICENSED
pragma solidity ^0.8.13;
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {Script, console} from "forge-std/Script.sol";

Expand Down
19 changes: 10 additions & 9 deletions contracts/src/ISP1Verifier.sol
Original file line number Diff line number Diff line change
@@ -1,23 +1,24 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.19;
pragma solidity ^0.8.20;
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

note: using sol 0.8.20 because that's what the OZ lib is on


/// @title SP1 Verifier Interface
/// @author Succinct Labs
/// @notice This contract is the interface for the SP1 Verifier.
interface ISP1Verifier {
/// @notice Returns the version of SP1 this verifier corresponds to.
function VERSION() external pure returns (string memory);

/// @notice Returns the hash of the verification key.
function VKEY_HASH() external pure returns (bytes32);

/// @notice Verifies a proof with given public values and vkey.
/// @param vkey The verification key for the RISC-V program.
/// @dev It is expected that the first 4 bytes of proofBytes must match the first 4 bytes of
/// target verifier's VERIFIER_HASH.
/// @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 vkey,
bytes32 programVKey,
bytes calldata publicValues,
bytes calldata proofBytes
) external view;
}

interface ISP1VerifierWithHash is ISP1Verifier {
/// @notice Returns the hash of the verifier.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should note that this is autogenerated by taking sha256 of the vkey file

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

added

function VERIFIER_HASH() external pure returns (bytes32);
}
72 changes: 72 additions & 0 deletions contracts/src/ISP1VerifierGateway.sol
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;

import {ISP1Verifier} from "./ISP1Verifier.sol";

/// @dev A struct containing the address of a verifier and whether the verifier is frozen. A
/// frozen verifier cannot be routed to.
struct VerifierRoute {
address verifier;
bool frozen;
}

interface ISP1VerifierGatewayEvents {
/// @notice Emitted when a verifier route is added.
/// @param selector The verifier selector that was added.
/// @param verifier The address of the verifier contract.
event RouteAdded(bytes4 selector, address verifier);

/// @notice Emitted when a verifier route is frozen.
/// @param selector The verifier selector that was frozen.
/// @param verifier The address of the verifier contract.
event RouteFrozen(bytes4 selector, address verifier);
}

interface ISP1VerifierGatewayErrors {
/// @notice Thrown when the verifier route is not found.
/// @param selector The verifier selector that was specified.
error RouteNotFound(bytes4 selector);

/// @notice Thrown when the verifier route is found, but is frozen.
/// @param selector The verifier selector that was specified.
error RouteIsFrozen(bytes4 selector);

/// @notice Thrown when adding a verifier route and the selector already contains a route.
/// @param verifier The address of the verifier contract in the existing route.
error RouteAlreadyExists(address verifier);

/// @notice Thrown when adding a verifier route and the selector returned by the verifier is
/// zero.
error SelectorCannotBeZero();
}

/// @title SP1 Verifier Gateway Interface
/// @author Succinct Labs
/// @notice This contract is the interface for the SP1 Verifier Gateway.
interface ISP1VerifierGateway is
ISP1VerifierGatewayEvents,
ISP1VerifierGatewayErrors,
ISP1Verifier
{
/// @notice Mapping of 4-byte verifier selectors to verifier routes.
/// @dev Only one verifier route can be added for each selector.
/// @param selector The verifier selector, which is both the first 4 bytes of the VERIFIER_HASH
/// and the first 4 bytes of the proofs designed for that verifier.
/// @return verifier The address of the verifier contract.
/// @return frozen Whether the verifier is frozen.
function routes(bytes4 selector) external view returns (address verifier, bool frozen);

/// @notice Adds a verifier route. This enable proofs to be routed to this verifier.
/// @dev Only callable by the owner. The owner is responsible for ensuring that the specified
/// verifier is correct with a valid VERIFIER_HASH. Once a route to a verifier is added, it
/// cannot be removed.
/// @param verifier The address of the verifier contract. This verifier MUST implement the
/// ISP1VerifierWithHash interface.
function addRoute(address verifier) external;

/// @notice Freezes a verifier route. This prevents proofs from being routed to this verifier.
/// @dev Only callable by the owner. Once a route to a verifier is frozen, it cannot be
/// unfrozen.
/// @param selector The verifier selector to freeze.
function freezeRoute(bytes4 selector) external;
}
Loading
Loading