Skip to content

Commit

Permalink
feat: call verifier gateway, add deployment instructions, example env…
Browse files Browse the repository at this point in the history
… for prover network (#13)
  • Loading branch information
mattstam authored Jul 2, 2024
1 parent 13e69ca commit 4b793b4
Show file tree
Hide file tree
Showing 14 changed files with 339 additions and 484 deletions.
5 changes: 5 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# 'mock' for generating mock proofs locally, 'local' for generating proofs locally, 'network' for generating proofs using the proving network.
SP1_PROVER=mock
# If using the proving network, set to your whitelisted private key. For more information, see:
# https://docs.succinct.xyz/prover-network/setup.html#key-setup
SP1_PRIVATE_KEY=
2 changes: 2 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,6 +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/sp1-contracts"]
path = contracts/lib/sp1-contracts
url = https://github.com/succinctlabs/sp1-contracts
tag = main
19 changes: 9 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
# SP1 Project Template

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

## Requirements

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

## Standard Proof Generation

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

```
```sh
cd script
RUST_LOG=info cargo run --bin prove --release
```
Expand All @@ -28,17 +27,17 @@ RUST_LOG=info cargo run --bin prove --release
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.

```
```sh
cd script
RUST_LOG=info cargo run --bin prove --release -- --evm
```

### Solidity Proof Verification
## Using the Prover Network

After generating the verify the proof with the SP1 EVM verifier.
Make a copy of the example environment file:

```
cd ../contracts
forge test -v
```sh
cp .env.example .env
```

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).
61 changes: 61 additions & 0 deletions contracts/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
# SP1 Project Template Contracts

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).

## Requirements

- [Foundry](https://book.getfoundry.sh/getting-started/installation)

## Test

```sh
forge test -v
```

## Deployment

#### Step 1: Set the `VERIFIER` environment variable

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:

```sh
VERIFIER=0x3B6041173B80E77f038f3F2C0f9744f04837185e
```

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.

#### Step 2: Set the `PROGRAM_VKEY` environment variable

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:

> Program Verification Key: 0x00620892344c310c32a74bf0807a5c043964264e4f37c96a10ad12b5c9214e0e
Then set the `PROGRAM_VKEY` environment variable to the output of that command, for example:

```sh
PROGRAM_VKEY=0x00620892344c310c32a74bf0807a5c043964264e4f37c96a10ad12b5c9214e0e
```

#### Step 3: Deploy the contract

Fill out the rest of the details needed for deployment:

```sh
RPC_URL=...
```

```sh
PRIVATE_KEY=...
```

Then deploy the contract to the chain:

```sh
forge create src/Fibonacci.sol:Fibonacci --rpc-url $RPC_URL --private-key $PRIVATE_KEY --constructor-args $VERIFIER $PROGRAM_VKEY
```

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`:

```sh
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
```
32 changes: 19 additions & 13 deletions contracts/src/Fibonacci.sol
Original file line number Diff line number Diff line change
@@ -1,32 +1,38 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.13;
pragma solidity ^0.8.20;

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

/// @title Fibonacci.
/// @author Succinct Labs
/// @notice This contract implements a simple example of verifying the proof of a computing a
/// fibonacci number.
contract Fibonacci is SP1Verifier {
contract Fibonacci {
/// @notice The address of the SP1 verifier contract.
/// @dev This can either be a specific SP1Verifier for a specific version, or the
/// SP1VerifierGateway which can be used to verify proofs for any version of SP1.
/// For the list of supported verifiers on each chain, see:
/// https://github.com/succinctlabs/sp1-contracts/tree/main/contracts/deployments
address public verifier;

/// @notice The verification key for the fibonacci program.
bytes32 public fibonacciProgramVkey;

constructor(bytes32 _fibonacciProgramVkey) {
constructor(address _verifier, bytes32 _fibonacciProgramVkey) {
verifier = _verifier;
fibonacciProgramVkey = _fibonacciProgramVkey;
}

/// @notice The entrypoint for verifying the proof of a fibonacci number.
/// @param proof The encoded proof.
/// @param publicValues The encoded public values.
function verifyFibonacciProof(
bytes memory proof,
bytes memory publicValues
) public view returns (uint32, uint32, uint32) {
this.verifyProof(fibonacciProgramVkey, publicValues, proof);
(uint32 n, uint32 a, uint32 b) = abi.decode(
publicValues,
(uint32, uint32, uint32)
);
function verifyFibonacciProof(bytes calldata proof, bytes calldata publicValues)
public
view
returns (uint32, uint32, uint32)
{
ISP1Verifier(verifier).verifyProof(fibonacciProgramVkey, publicValues, proof);
(uint32 n, uint32 a, uint32 b) = abi.decode(publicValues, (uint32, uint32, uint32));
return (n, a, b);
}
}
19 changes: 11 additions & 8 deletions contracts/test/Fibonacci.t.sol
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.25;
pragma solidity ^0.8.20;

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

struct SP1ProofFixtureJson {
uint32 a;
Expand All @@ -18,6 +18,7 @@ struct SP1ProofFixtureJson {
contract FibonacciTest is Test {
using stdJson for string;

address verifier;
Fibonacci public fibonacci;

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

function setUp() public {
SP1ProofFixtureJson memory fixture = loadFixture();
fibonacci = new Fibonacci(fixture.vkey);

verifier = address(new SP1VerifierGateway(address(1)));
fibonacci = new Fibonacci(verifier, fixture.vkey);
}

function test_ValidFibonacciProof() public view {
function test_ValidFibonacciProof() public {
SP1ProofFixtureJson memory fixture = loadFixture();
(uint32 n, uint32 a, uint32 b) = fibonacci.verifyFibonacciProof(
fixture.proof,
fixture.publicValues
);

vm.mockCall(verifier, abi.encodeWithSelector(SP1VerifierGateway.verifyProof.selector), abi.encode(true));

(uint32 n, uint32 a, uint32 b) = fibonacci.verifyFibonacciProof(fixture.proof, fixture.publicValues);
assert(n == fixture.n);
assert(a == fixture.a);
assert(b == fixture.b);
Expand Down
Loading

0 comments on commit 4b793b4

Please sign in to comment.