Skip to content

Commit

Permalink
docs: verifier crate docs (#1764)
Browse files Browse the repository at this point in the history
  • Loading branch information
yuwen01 authored Nov 7, 2024
1 parent 80f7cd4 commit 1a25bc4
Show file tree
Hide file tree
Showing 8 changed files with 75 additions and 13 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -417,7 +417,14 @@ jobs:

lock-files:
name: "Check lock files"
runs-on: ["runs-on", "runner=8cpu-linux-x64", "hdd=150", "run-id=${{ github.run_id }}"]
runs-on:
[
runs-on,
runner=64cpu-linux-x64,
spot=false,
hdd=150,
"run-id=${{ github.run_id }}",
]
env:
CARGO_NET_GIT_FETCH_WITH_CLI: "true"
steps:
Expand Down
9 changes: 5 additions & 4 deletions book/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@

- [FAQ](./generating-proofs/sp1-sdk-faq.md)

# Onchain Verification
# Verification

- [Setup](./onchain-verification/getting-started.md)
- [On-chain Verification](./verification/onchain/getting-started.md)

- [Solidity Verifier](./onchain-verification/solidity-sdk.md)
- [Solidity Verifier](./verification/onchain/solidity-sdk.md)
- [Contract Addresses](./verification/onchain/contract-addresses.md)

- [Contract Addresses](./onchain-verification/contract-addresses.md)
- [Off-chain Verification](./verification/off-chain-verification.md)

# Developers

Expand Down
49 changes: 49 additions & 0 deletions book/verification/off-chain-verification.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
# Offchain Verification

## Rust `no_std` Verification

You can verify SP1 Groth16 and Plonk proofs in `no_std` environments with [`sp1-verifier`](https://docs.rs/sp1-verifier/latest/sp1_verifier/).

`sp1-verifier` is also patched to verify Groth16 and Plonk proofs within the SP1 ZKVM, using
[bn254](https://blog.succinct.xyz/succinctshipsprecompiles/) precompiles. For an example of this, see
the [Groth16 Example](https://github.com/succinctlabs/sp1/tree/main/examples/groth16/).

### Installation

Import the following dependency in your `Cargo.toml`:

```toml
sp1-verifier = {version = "3.0.0", default-features = false}
```

### Usage

`sp1-verifier`'s interface is very similar to the solidity verifier's. It exposes two public functions:
[`Groth16Verifier::verify_proof`](https://docs.rs/sp1-verifier/latest/src/sp1_verifier/groth16.rs.html)
and [`PlonkVerifier::verify_proof`](https://docs.rs/sp1-verifier/latest/src/sp1_verifier/plonk.rs.html).

`sp1-verifier` also exposes the Groth16 and Plonk verifying keys as constants, `GROTH16_VK_BYTES` and `PLONK_VK_BYTES`. These
keys correspond to the current SP1 version's official Groth16 and Plonk verifying keys, which are used for verifying proofs generated
using docker or the prover network.

First, generate your groth16/plonk proof with the SP1 SDK. See [here](./onchain/getting-started.md#generating-sp1-proofs-for-onchain-verification)
for more information -- `sp1-verifier` and the solidity verifier expect inputs in the same format.

Next, verify the proof with `sp1-verifier`. The following snippet is from the [Groth16 example program](https://github.com/succinctlabs/sp1/tree/dev/examples/groth16/), which verifies a Groth16 proof within SP1 using `sp1-verifier`.

```rust,noplayground
{{#include ../../examples/groth16/program/src/main.rs}}
```

Here, the proof, public inputs, and vkey hash are read from stdin. See the following snippet to see how these values are generated.

```rust,noplayground
{{#include ../../examples/groth16/script/src/main.rs:12:34}}
```

> Note that the SP1 SDK itself is *not* `no_std` compatible.
## Wasm Verification

The [`example-sp1-wasm-verifier`](https://github.com/succinctlabs/example-sp1-wasm-verifier) demonstrates how to
verify SP1 proofs in wasm. For a more detailed explanation of the process, please see the [README](https://github.com/succinctlabs/example-sp1-wasm-verifier/blob/main/README.md).
File renamed without changes.
Binary file removed examples/elf/riscv32im-succinct-zkvm-elf
Binary file not shown.
21 changes: 13 additions & 8 deletions examples/groth16/script/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
//! A script that generates a Groth16 proof for the Fibonacci program, and verifies the
//! Groth16 proof in SP1.

use sp1_sdk::{include_elf, utils, HashableKey, ProverClient, SP1ProofWithPublicValues, SP1Stdin};
use sp1_sdk::{include_elf, utils, HashableKey, ProverClient, SP1Stdin};

/// The ELF for the Groth16 verifier program.
const GROTH16_ELF: &[u8] = include_elf!("groth16-verifier-program");

/// The ELF for the Fibonacci program.
const FIBONACCI_ELF: &[u8] = include_elf!("fibonacci-program");

fn generate_fibonacci_proof() -> (SP1ProofWithPublicValues, String) {
/// Generates the proof, public values, and vkey hash for the Fibonacci program in a format that
/// can be read by `sp1-verifier`.
///
/// Returns the proof bytes, public values, and vkey hash.
fn generate_fibonacci_proof() -> (Vec<u8>, Vec<u8>, String) {
// Create an input stream and write '20' to it.
let n = 20u32;

Expand All @@ -21,23 +25,24 @@ fn generate_fibonacci_proof() -> (SP1ProofWithPublicValues, String) {
// Create a `ProverClient`.
let client = ProverClient::new();

// Generate the proof for the fibonacci program..
// Generate the groth16 proof for the Fibonacci program.
let (pk, vk) = client.setup(FIBONACCI_ELF);
println!("vk: {:?}", vk.bytes32());
(client.prove(&pk, stdin).groth16().run().unwrap(), vk.bytes32())
let proof = client.prove(&pk, stdin).groth16().run().unwrap();
(proof.bytes(), proof.public_values.to_vec(), vk.bytes32())
}

fn main() {
// Setup logging.
utils::setup_logger();

// Generate the Fibonacci proof.
let (fibonacci_proof, vk) = generate_fibonacci_proof();
// Generate the Fibonacci proof, public values, and vkey hash.
let (fibonacci_proof, fibonacci_public_values, vk) = generate_fibonacci_proof();

// Write the proof, public values, and vkey hash to the input stream.
let mut stdin = SP1Stdin::new();
stdin.write_vec(fibonacci_proof.bytes());
stdin.write_vec(fibonacci_proof.public_values.to_vec());
stdin.write_vec(fibonacci_proof);
stdin.write_vec(fibonacci_public_values);
stdin.write(&vk);

// Create a `ProverClient`.
Expand Down

0 comments on commit 1a25bc4

Please sign in to comment.