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: plonk prover #795

Merged
merged 20 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 19 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
6 changes: 3 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ concurrency:
cancel-in-progress: true

jobs:
groth16:
name: Groth16
plonk:
name: Plonk
runs-on: runs-on,cpu=64,ram=256,family=m7i+m7a,hdd=80,image=ubuntu22-full-x64
env:
CARGO_NET_GIT_FETCH_WITH_CLI: "true"
Expand All @@ -35,7 +35,7 @@ jobs:
with:
command: test
toolchain: nightly-2024-04-17
args: --release -p sp1-sdk --features groth16 -- test_e2e_prove_groth16 --nocapture
args: --release -p sp1-sdk --features plonk_bn254 -- test_e2e_prove_plonk_bn254 --nocapture
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0 -C target-cpu=native
RUST_BACKTRACE: 1
4 changes: 2 additions & 2 deletions .github/workflows/pr.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ jobs:
with:
command: test
toolchain: nightly-2024-04-17
args: --release --features groth16
args: --release --features plonk_bn254
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0 -C target-cpu=native
RUST_BACKTRACE: 1
Expand Down Expand Up @@ -76,7 +76,7 @@ jobs:
with:
command: test
toolchain: nightly-2024-04-17
args: --release --features groth16
args: --release --features plonk_bn254
env:
RUSTFLAGS: -Copt-level=3 -Cdebug-assertions -Coverflow-checks=y -Cdebuginfo=0 -C target-cpu=native
RUST_BACKTRACE: 1
Expand Down
2 changes: 1 addition & 1 deletion book/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,4 +40,4 @@

- [Recommended Settings](./developers/recommended-settings.md)

- [Building Groth16 Artifacts](./developers/building-groth16-artifacts.md)
jtguibas marked this conversation as resolved.
Show resolved Hide resolved
- [Building Plonk Bn254 Artifacts](./developers/building-plonk-artifacts.md)
7 changes: 0 additions & 7 deletions book/developers/building-groth16-artifacts.md
jtguibas marked this conversation as resolved.
Outdated
Show resolved Hide resolved

This file was deleted.

7 changes: 7 additions & 0 deletions book/developers/building-plonk-artifacts.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Building Plonk BN254 Artifacts

To build the Plonk Bn254 artifacts from scratch, you can use the `Makefile` inside the `prover` directory.

```shell,noplayground
RUST_LOG=info make plonk-bn254
```
2 changes: 1 addition & 1 deletion book/developers/recommended-settings.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
For developers contributing to the SP1 project, we recommend the following settings:

- `FRI_QUERIES=1`: Makes the prover use less bits of security to generate proofs more quickly.
- `SP1_DEV=1`: This will rebuild the Groth16 artifacts everytime they are necessary.
- `SP1_DEV=1`: This will rebuild the Plonk Bn254 artifacts everytime they are necessary.
16 changes: 8 additions & 8 deletions book/verifying-proofs/solidity-and-evm.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
SP1 recently added support for verifying proofs for onchain usecases. To see an end-to-end example
of using SP1 for on-chain usecases, refer to the [SP1 Project Template](https://github.com/succinctlabs/sp1-project-template/tree/main).

## Generating a Groth16 Proof
## Generating a Plonk Bn254 Proof

By default, the proofs generated by SP1 are not verifiable onchain, as they are non-constant size and STARK verification on Ethereum is very expensive. To generate a proof that can be verified onchain, we use performant STARK recursion to combine SP1 shard proofs into a single STARK proof and then wrap that in a SNARK proof. Our `ProverClient` has a function for this called `prove_groth16`. Behind the scenes, this function will first generate a normal SP1 proof, then recursively combine all of them into a single proof using the STARK recursion protocol. Finally, the proof is wrapped in a SNARK proof using Groth16.
By default, the proofs generated by SP1 are not verifiable onchain, as they are non-constant size and STARK verification on Ethereum is very expensive. To generate a proof that can be verified onchain, we use performant STARK recursion to combine SP1 shard proofs into a single STARK proof and then wrap that in a SNARK proof. Our `ProverClient` has a function for this called `prove_plonk_bn254`. Behind the scenes, this function will first generate a normal SP1 proof, then recursively combine all of them into a single proof using the STARK recursion protocol. Finally, the proof is wrapped in a SNARK proof using PLONK.

**Due to the fact that Groth16 proofs require a trusted setup, the Groth16 prover is only guaranteed to work on official releases of SP1 (i.e., `v.1.0.0-testnet`).**
**Due to the fact that PLONK Bn254 proofs require a trusted setup, the PLONK Bn254 prover is only guaranteed to work on official releases of SP1.**
jtguibas marked this conversation as resolved.
Show resolved Hide resolved

To use Groth16 proving & verification locally, enable the `groth16` feature flag in the sp1-sdk and ensure that Go >1.22.1 is installed.
To use PLONK proving & verification locally, enable the `plonk_bn254` feature flag in the sp1-sdk and ensure that Go >1.22.1 is installed.
```toml
sp1-sdk = { features = ["groth16"] }
sp1-sdk = { features = ["plonk_bn254"] }
```


### Example

```rust,noplayground
{{#include ../../examples/fibonacci/script/bin/groth16.rs}}
{{#include ../../examples/fibonacci/script/bin/plonk_bn254.rs}}
```

You can run the above script with `RUST_LOG=info cargo run --bin groth16 --release` in `examples/fibonacci/script`.
You can run the above script with `RUST_LOG=info cargo run --bin plonk_bn254 --release` in `examples/fibonacci/script`.

## Exporting the Verifier Contract

Expand All @@ -41,7 +41,7 @@ use std::path::PathBuf;

fn main() {
let contracts_src_dir = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("../contracts/src");
sp1_sdk::artifacts::export_solidity_groth16_verifier(contracts_src_dir)
sp1_sdk::artifacts::export_solidity_plonk_bn254_verifier(contracts_src_dir)
.expect("failed to export verifier");
}
```
14 changes: 7 additions & 7 deletions core/src/stark/machine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl<SC: StarkGenericConfig, A: MachineAir<Val<SC>>> StarkMachine<SC, A> {

// Display some statistics about the workload.
let stats = record.stats();
log::info!("Shard: {:?}", stats);
log::info!("shard: {:?}", stats);

// For each chip, shard the events into segments.
record.shard(config)
Expand Down Expand Up @@ -423,12 +423,12 @@ impl<SC: StarkGenericConfig, A: MachineAir<Val<SC>>> StarkMachine<SC, A> {
let permutation_width = permutation_traces[i].width();
let total_width = trace_width + permutation_width;
tracing::debug!(
"{:<11} | Main Cols = {:<5} | Perm Cols = {:<5} | Rows = {:<10} | Cells = {:<10}",
chips[i].name(),
trace_width,
permutation_width,
traces[i].0.height(),
total_width * traces[i].0.height(),
"{:<11} | Main Cols = {:<5} | Perm Cols = {:<5} | Rows = {:<10} | Cells = {:<10}",
chips[i].name(),
trace_width,
permutation_width,
traces[i].0.height(),
total_width * traces[i].0.height(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion examples/aggregation/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ default-run = "sp1-aggregation-example-script"

[dependencies]
hex = "0.4.3"
sp1-sdk = { path = "../../../sdk", features = ["groth16"] }
sp1-sdk = { path = "../../../sdk", features = ["plonk_bn254"] }
tracing = "0.1.40"

[build-dependencies]
Expand Down
8 changes: 4 additions & 4 deletions examples/aggregation/script/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const FIBONACCI_ELF: &[u8] =
include_bytes!("../../programs/fibonacci/elf/riscv32im-succinct-zkvm-elf");

/// An input to the aggregation program.
///
///
/// Consists of a proof and a verification key.
struct AggregationInput {
pub proof: SP1CompressedProof,
Expand Down Expand Up @@ -93,9 +93,9 @@ fn main() {
stdin.write_proof(input.proof.proof, input.vk.vk);
}

// Generate the groth16 proof.
// Generate the plonk bn254 proof.
client
.prove_groth16(&aggregation_pk, stdin)
.prove_plonk_bn254(&aggregation_pk, stdin)
.expect("proving failed");
});
});
}
4 changes: 2 additions & 2 deletions examples/fibonacci/script/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ sha2 = "0.10.8"
sp1-helper = { path = "../../../helper" }

[[bin]]
name = "groth16"
path = "bin/groth16.rs"
name = "plonk_bn254"
path = "bin/plonk_bn254.rs"

[[bin]]
name = "compressed"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ fn main() {
// Generate the proof for the given program and input.
let client = ProverClient::new();
let (pk, vk) = client.setup(ELF);
let mut proof = client.prove_groth16(&pk, stdin).unwrap();
let mut proof = client.prove_plonk_bn254(&pk, stdin).unwrap();

println!("generated proof");

Expand All @@ -29,7 +29,7 @@ fn main() {

// Verify proof and public values
client
.verify_groth16(&proof, &vk)
.verify_plonk_bn254(&proof, &vk)
.expect("verification failed");

// Save the proof.
Expand Down
8 changes: 4 additions & 4 deletions prover/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -48,15 +48,15 @@ num-bigint = "0.4.5"
thiserror = "1.0.60"

[[bin]]
name = "build_groth16"
path = "scripts/build_groth16.rs"
name = "build_plonk_bn254"
path = "scripts/build_plonk_bn254.rs"

[[bin]]
name = "e2e"
path = "scripts/e2e.rs"

[features]
default = ["groth16"]
default = ["plonk_bn254"]

neon = ["sp1-core/neon"]
groth16 = ["sp1-recursion-gnark-ffi/groth16"]
plonk_bn254 = ["sp1-recursion-gnark-ffi/plonk_bn254"]
33 changes: 6 additions & 27 deletions prover/Makefile
Original file line number Diff line number Diff line change
@@ -1,37 +1,16 @@
all:
make fibonacci-sweep
make tendermint-sweep
make fibonacci-groth16
make build-plonk-bn254
make release-plonk-bn254

build-groth16:
build-plonk-bn254:
mkdir -p build && \
RUSTFLAGS='-C target-cpu=native' \
cargo run -p sp1-prover --release --bin build_groth16 -- \
cargo run -p sp1-prover --release --bin build_plonk_bn254 -- \
--build-dir=./build

release-groth16:
release-plonk-bn254:
bash release.sh

test-e2e:
RUSTFLAGS='-C target-cpu=native' \
cargo test --package sp1-prover --lib --release -- tests::test_e2e --exact --show-output

fibonacci-sweep:
mkdir -p scripts/results && \
rm -f scripts/results/fibonacci_sweep.log && \
RUSTFLAGS='-C target-cpu=native -C target_feature=+avx512ifma,+avx512vl' \
RUST_LOG=info \
cargo run --package sp1-prover --release --bin fibonacci_sweep

tendermint-sweep:
mkdir -p scripts/results && \
rm -f scripts/results/tendermint_sweep.log && \
RUSTFLAGS='-C target-cpu=native -C target_feature=+avx512ifma,+avx512vl' \
RUST_LOG=info \
cargo run --package sp1-prover --release --bin tendermint_sweep

fibonacci-groth16:
mkdir -p scripts/results && \
RUSTFLAGS='-C target-cpu=native -C target_feature=+avx512ifma,+avx512vl' \
RUST_LOG=info \
cargo run --package sp1-prover --release --bin fibonacci_groth16
cargo test --package sp1-prover --lib --release -- tests::test_e2e --exact --show-output
2 changes: 1 addition & 1 deletion prover/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ fi
# Create archive named after the commit hash
ARCHIVE_NAME="${COMMIT_HASH}.tar.gz"
cd $FILE_TO_UPLOAD
tar -czvf "../$ARCHIVE_NAME" .
tar --exclude='srs.bin' --exclude='srs_lagrange.bin' -czvf "../$ARCHIVE_NAME" .
cd -
if [ $? -ne 0 ]; then
echo "Failed to create archive."
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use std::path::PathBuf;

use clap::Parser;
use sp1_core::utils::setup_logger;
use sp1_prover::build::build_groth16_artifacts_with_dummy;
use sp1_prover::build::build_plonk_bn254_artifacts_with_dummy;

#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand All @@ -17,5 +17,5 @@ struct Args {
pub fn main() {
setup_logger();
let args = Args::parse();
build_groth16_artifacts_with_dummy(args.build_dir);
build_plonk_bn254_artifacts_with_dummy(args.build_dir);
}
12 changes: 6 additions & 6 deletions prover/scripts/e2e.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use sp1_recursion_circuit::stark::build_wrap_circuit;
use sp1_recursion_circuit::witness::Witnessable;
use sp1_recursion_compiler::ir::Witness;
use sp1_recursion_core::air::RecursionPublicValues;
use sp1_recursion_gnark_ffi::Groth16Prover;
use sp1_recursion_gnark_ffi::PlonkBn254Prover;
use subtle_encoding::hex;

#[derive(Parser, Debug)]
Expand Down Expand Up @@ -70,19 +70,19 @@ pub fn main() {
witness.write_vkey_hash(vkey_hash);

tracing::info!("sanity check gnark test");
Groth16Prover::test(constraints.clone(), witness.clone());
PlonkBn254Prover::test(constraints.clone(), witness.clone());

tracing::info!("sanity check gnark build");
Groth16Prover::build(constraints.clone(), witness.clone(), build_dir.clone());
PlonkBn254Prover::build(constraints.clone(), witness.clone(), build_dir.clone());

tracing::info!("sanity check gnark prove");
let groth16_prover = Groth16Prover::new();
let plonk_bn254_prover = PlonkBn254Prover::new();

tracing::info!("gnark prove");
let proof = groth16_prover.prove(witness.clone(), build_dir.clone());
let proof = plonk_bn254_prover.prove(witness.clone(), build_dir.clone());

tracing::info!("verify gnark proof");
groth16_prover.verify(
plonk_bn254_prover.verify(
&proof,
&vkey_hash.as_canonical_biguint(),
&committed_values_digest.as_canonical_biguint(),
Expand Down
2 changes: 1 addition & 1 deletion prover/scripts/fibonacci_groth16.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//! Tests end-to-end performance of wrapping a recursion proof to Groth16.
//! Tests end-to-end performance of wrapping a recursion proof to PLONK.

#![feature(generic_const_exprs)]
#![allow(incomplete_features)]
Expand Down
Loading
Loading