Skip to content

Commit 4b793b4

Browse files
authored
feat: call verifier gateway, add deployment instructions, example env for prover network (#13)
1 parent 13e69ca commit 4b793b4

File tree

14 files changed

+339
-484
lines changed

14 files changed

+339
-484
lines changed

.env.example

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# 'mock' for generating mock proofs locally, 'local' for generating proofs locally, 'network' for generating proofs using the proving network.
2+
SP1_PROVER=mock
3+
# If using the proving network, set to your whitelisted private key. For more information, see:
4+
# https://docs.succinct.xyz/prover-network/setup.html#key-setup
5+
SP1_PRIVATE_KEY=

.gitmodules

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
[submodule "contracts/lib/forge-std"]
22
path = contracts/lib/forge-std
33
url = https://github.com/foundry-rs/forge-std
4+
tag = v1.8.2
45
[submodule "contracts/lib/sp1-contracts"]
56
path = contracts/lib/sp1-contracts
67
url = https://github.com/succinctlabs/sp1-contracts
8+
tag = main

README.md

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,12 @@
11
# SP1 Project Template
22

3-
This is a template for creating an end-to-end [SP1](https://github.com/succinctlabs/sp1) project
4-
that can generate a proof of any RISC-V program and verify the proof onchain.
3+
This is a template for creating an end-to-end [SP1](https://github.com/succinctlabs/sp1) project
4+
that can generate a proof of any RISC-V program.
55

66
## Requirements
77

88
- [Rust](https://rustup.rs/)
99
- [SP1](https://succinctlabs.github.io/sp1/getting-started/install.html)
10-
- [Foundry](https://book.getfoundry.sh/getting-started/installation)
1110

1211
## Standard Proof Generation
1312

@@ -16,7 +15,7 @@ that can generate a proof of any RISC-V program and verify the proof onchain.
1615
1716
Generate the proof for your program using the standard prover.
1817

19-
```
18+
```sh
2019
cd script
2120
RUST_LOG=info cargo run --bin prove --release
2221
```
@@ -28,17 +27,17 @@ RUST_LOG=info cargo run --bin prove --release
2827
2928
Generate the proof that is small enough to be verified on-chain and verifiable by the EVM. This command also generates a fixture that can be used to test the verification of SP1 zkVM proofs inside Solidity.
3029

31-
```
30+
```sh
3231
cd script
3332
RUST_LOG=info cargo run --bin prove --release -- --evm
3433
```
3534

36-
### Solidity Proof Verification
35+
## Using the Prover Network
3736

38-
After generating the verify the proof with the SP1 EVM verifier.
37+
Make a copy of the example environment file:
3938

40-
```
41-
cd ../contracts
42-
forge test -v
39+
```sh
40+
cp .env.example .env
4341
```
4442

43+
Then, set the `SP1_PROVER` environment variable to `network` and set the `SP1_PRIVATE_KEY` environment variable to your whitelisted private key. For more information, see the [setup guide](https://docs.succinct.xyz/prover-network/setup.html).

contracts/README.md

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
# SP1 Project Template Contracts
2+
3+
This is a template for writing a contract that uses verification of [SP1](https://github.com/succinctlabs/sp1) PlonK proofs onchain using the [SP1VerifierGateway](https://github.com/succinctlabs/sp1-contracts/blob/main/contracts/src/SP1VerifierGateway.sol).
4+
5+
## Requirements
6+
7+
- [Foundry](https://book.getfoundry.sh/getting-started/installation)
8+
9+
## Test
10+
11+
```sh
12+
forge test -v
13+
```
14+
15+
## Deployment
16+
17+
#### Step 1: Set the `VERIFIER` environment variable
18+
19+
Find the address of the `verifer` to use from the [deployments](https://github.com/succinctlabs/sp1-contracts/tree/main/contracts/deployments) list for the chain you are deploying to. Set it to the `VERIFIER` environment variable, for example:
20+
21+
```sh
22+
VERIFIER=0x3B6041173B80E77f038f3F2C0f9744f04837185e
23+
```
24+
25+
Note: you can use either the [SP1VerifierGateway](https://github.com/succinctlabs/sp1-contracts/blob/main/contracts/src/SP1VerifierGateway.sol) or a specific version, but it is highly recommended to use the gateway as this will allow you to use different versions of SP1.
26+
27+
#### Step 2: Set the `PROGRAM_VKEY` environment variable
28+
29+
Find your program verification key by going into the `../script` directory and running `RUST_LOG=info cargo run --package fibonacci-script --bin vkey --release`, which will print an output like:
30+
31+
> Program Verification Key: 0x00620892344c310c32a74bf0807a5c043964264e4f37c96a10ad12b5c9214e0e
32+
33+
Then set the `PROGRAM_VKEY` environment variable to the output of that command, for example:
34+
35+
```sh
36+
PROGRAM_VKEY=0x00620892344c310c32a74bf0807a5c043964264e4f37c96a10ad12b5c9214e0e
37+
```
38+
39+
#### Step 3: Deploy the contract
40+
41+
Fill out the rest of the details needed for deployment:
42+
43+
```sh
44+
RPC_URL=...
45+
```
46+
47+
```sh
48+
PRIVATE_KEY=...
49+
```
50+
51+
Then deploy the contract to the chain:
52+
53+
```sh
54+
forge create src/Fibonacci.sol:Fibonacci --rpc-url $RPC_URL --private-key $PRIVATE_KEY --constructor-args $VERIFIER $PROGRAM_VKEY
55+
```
56+
57+
It can also be a good idea to verify the contract when you deploy, in which case you would also need to set `ETHERSCAN_API_KEY`:
58+
59+
```sh
60+
forge create src/Fibonacci.sol:Fibonacci --rpc-url $RPC_URL --private-key $PRIVATE_KEY --constructor-args $VERIFIER $PROGRAM_VKEY --verify --verifier etherscan --etherscan-api-key $ETHERSCAN_API_KEY
61+
```

contracts/src/Fibonacci.sol

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,38 @@
11
// SPDX-License-Identifier: MIT
2-
pragma solidity ^0.8.13;
2+
pragma solidity ^0.8.20;
33

4-
import {SP1Verifier} from "@sp1-contracts/SP1Verifier.sol";
4+
import {ISP1Verifier} from "@sp1-contracts/ISP1Verifier.sol";
55

66
/// @title Fibonacci.
77
/// @author Succinct Labs
88
/// @notice This contract implements a simple example of verifying the proof of a computing a
99
/// fibonacci number.
10-
contract Fibonacci is SP1Verifier {
10+
contract Fibonacci {
11+
/// @notice The address of the SP1 verifier contract.
12+
/// @dev This can either be a specific SP1Verifier for a specific version, or the
13+
/// SP1VerifierGateway which can be used to verify proofs for any version of SP1.
14+
/// For the list of supported verifiers on each chain, see:
15+
/// https://github.com/succinctlabs/sp1-contracts/tree/main/contracts/deployments
16+
address public verifier;
17+
1118
/// @notice The verification key for the fibonacci program.
1219
bytes32 public fibonacciProgramVkey;
1320

14-
constructor(bytes32 _fibonacciProgramVkey) {
21+
constructor(address _verifier, bytes32 _fibonacciProgramVkey) {
22+
verifier = _verifier;
1523
fibonacciProgramVkey = _fibonacciProgramVkey;
1624
}
1725

1826
/// @notice The entrypoint for verifying the proof of a fibonacci number.
1927
/// @param proof The encoded proof.
2028
/// @param publicValues The encoded public values.
21-
function verifyFibonacciProof(
22-
bytes memory proof,
23-
bytes memory publicValues
24-
) public view returns (uint32, uint32, uint32) {
25-
this.verifyProof(fibonacciProgramVkey, publicValues, proof);
26-
(uint32 n, uint32 a, uint32 b) = abi.decode(
27-
publicValues,
28-
(uint32, uint32, uint32)
29-
);
29+
function verifyFibonacciProof(bytes calldata proof, bytes calldata publicValues)
30+
public
31+
view
32+
returns (uint32, uint32, uint32)
33+
{
34+
ISP1Verifier(verifier).verifyProof(fibonacciProgramVkey, publicValues, proof);
35+
(uint32 n, uint32 a, uint32 b) = abi.decode(publicValues, (uint32, uint32, uint32));
3036
return (n, a, b);
3137
}
3238
}

contracts/test/Fibonacci.t.sol

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
// SPDX-License-Identifier: MIT
2-
pragma solidity ^0.8.25;
2+
pragma solidity ^0.8.20;
33

44
import {Test, console} from "forge-std/Test.sol";
55
import {stdJson} from "forge-std/StdJson.sol";
66
import {Fibonacci} from "../src/Fibonacci.sol";
7-
import {SP1Verifier} from "@sp1-contracts/SP1Verifier.sol";
7+
import {SP1VerifierGateway} from "@sp1-contracts/SP1VerifierGateway.sol";
88

99
struct SP1ProofFixtureJson {
1010
uint32 a;
@@ -18,6 +18,7 @@ struct SP1ProofFixtureJson {
1818
contract FibonacciTest is Test {
1919
using stdJson for string;
2020

21+
address verifier;
2122
Fibonacci public fibonacci;
2223

2324
function loadFixture() public view returns (SP1ProofFixtureJson memory) {
@@ -30,15 +31,17 @@ contract FibonacciTest is Test {
3031

3132
function setUp() public {
3233
SP1ProofFixtureJson memory fixture = loadFixture();
33-
fibonacci = new Fibonacci(fixture.vkey);
34+
35+
verifier = address(new SP1VerifierGateway(address(1)));
36+
fibonacci = new Fibonacci(verifier, fixture.vkey);
3437
}
3538

36-
function test_ValidFibonacciProof() public view {
39+
function test_ValidFibonacciProof() public {
3740
SP1ProofFixtureJson memory fixture = loadFixture();
38-
(uint32 n, uint32 a, uint32 b) = fibonacci.verifyFibonacciProof(
39-
fixture.proof,
40-
fixture.publicValues
41-
);
41+
42+
vm.mockCall(verifier, abi.encodeWithSelector(SP1VerifierGateway.verifyProof.selector), abi.encode(true));
43+
44+
(uint32 n, uint32 a, uint32 b) = fibonacci.verifyFibonacciProof(fixture.proof, fixture.publicValues);
4245
assert(n == fixture.n);
4346
assert(a == fixture.a);
4447
assert(b == fixture.b);

0 commit comments

Comments
 (0)