Skip to content

Commit

Permalink
fix: groth16 prover issues (#571)
Browse files Browse the repository at this point in the history
  • Loading branch information
jtguibas authored Apr 23, 2024
1 parent 5f0af59 commit 1307b6e
Show file tree
Hide file tree
Showing 15 changed files with 151 additions and 263 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,6 @@ benchmark.csv
# Environment
.env

# Groth16 FFI
recursion/groth16-ffi/build
# Build Artifacts
recursion/groth16-ffi/build
prover/build
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion prover/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ all:
groth16:
RUST_LOG=info RUSTFLAGS='-C target-cpu=native' \
cargo run -p sp1-prover --release --bin e2e -- \
--build-dir=../recursion/groth16-ffi/build
--build-dir=./build

fibonacci-sweep:
mkdir -p scripts/results && \
Expand Down
5 changes: 3 additions & 2 deletions prover/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//! 3. Wrap the shard proof into a SNARK-friendly field.
//! 4. Wrap the last shard proof, proven over the SNARK-friendly field, into a Groth16/PLONK proof.
#![warn(unused_extern_crates)]
#![allow(incomplete_features)]
#![feature(generic_const_exprs)]
#![allow(deprecated)]
Expand Down Expand Up @@ -36,13 +37,13 @@ use sp1_core::{
};
use sp1_recursion_circuit::stark::build_wrap_circuit;
use sp1_recursion_circuit::witness::Witnessable;
use sp1_recursion_compiler::constraints::groth16_ffi;
use sp1_recursion_compiler::ir::Witness;
use sp1_recursion_core::runtime::RecursionProgram;
use sp1_recursion_core::{
runtime::Runtime as RecursionRuntime,
stark::{config::BabyBearPoseidon2Outer, RecursionAir},
};
use sp1_recursion_groth16_ffi::Groth16Prover;
use sp1_recursion_program::reduce::ReduceProgram;
use sp1_recursion_program::{hints::Hintable, stark::EMPTY};

Expand Down Expand Up @@ -425,7 +426,7 @@ impl SP1Prover {
let mut witness = Witness::default();
proof.write(&mut witness);
let constraints = build_wrap_circuit(&self.reduce_vk_outer, proof);
groth16_ffi::test_prove(constraints, witness);
Groth16Prover::test(constraints, witness);
}

// TODO: Get rid of this method by reading it from public values.
Expand Down
2 changes: 1 addition & 1 deletion recursion/circuit/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ sp1-recursion-compiler = { path = "../compiler" }
sp1-recursion-program = { path = "../program" }
p3-bn254-fr = { workspace = true }
p3-baby-bear = { workspace = true }
serial_test = "3.0.0"
bincode = "1.3.3"

[dev-dependencies]
Expand All @@ -33,3 +32,4 @@ p3-merkle-tree = { workspace = true }
p3-poseidon2 = { workspace = true }
zkhash = { git = "https://github.com/HorizenLabs/poseidon2" }
rand = "0.8.4"
sp1-recursion-groth16-ffi = { path = "../groth16-ffi" }
19 changes: 7 additions & 12 deletions recursion/circuit/src/challenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -158,20 +158,19 @@ mod tests {
use p3_field::split_32 as split_32_gt;
use p3_field::AbstractField;
use p3_symmetric::Hash;
use serial_test::serial;
use sp1_recursion_compiler::config::OuterConfig;
use sp1_recursion_compiler::constraints::{groth16_ffi, ConstraintCompiler};
use sp1_recursion_compiler::constraints::ConstraintCompiler;
use sp1_recursion_compiler::ir::SymbolicExt;
use sp1_recursion_compiler::ir::{Builder, Witness};
use sp1_recursion_core::stark::config::{outer_perm, OuterChallenger};
use sp1_recursion_groth16_ffi::Groth16Prover;

use super::reduce_32;
use super::split_32;
use crate::challenger::MultiField32ChallengerVariable;
use crate::DIGEST_SIZE;

#[test]
#[serial]
fn test_num2bits_v() {
let mut builder = Builder::<OuterConfig>::default();
let mut value_u32 = 1345237507;
Expand All @@ -184,11 +183,10 @@ mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_reduce_32() {
let value_1 = BabyBear::from_canonical_u32(1345237507);
let value_2 = BabyBear::from_canonical_u32(1000001);
Expand All @@ -202,11 +200,10 @@ mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_split_32() {
let value = Bn254Fr::from_canonical_u32(1345237507);
let gt: Vec<BabyBear> = split_32_gt(value, 3);
Expand All @@ -221,11 +218,10 @@ mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_challenger() {
let perm = outer_perm();
let mut challenger = OuterChallenger::new(perm).unwrap();
Expand Down Expand Up @@ -259,11 +255,10 @@ mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_challenger_sample_ext() {
let perm = outer_perm();
let mut challenger = OuterChallenger::new(perm).unwrap();
Expand Down Expand Up @@ -302,6 +297,6 @@ mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}
}
7 changes: 3 additions & 4 deletions recursion/circuit/src/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,21 +169,21 @@ mod tests {
use p3_challenger::{CanObserve, FieldChallenger};
use p3_commit::{Pcs, PolynomialSpace};
use serde::{de::DeserializeOwned, Serialize};
use serial_test::serial;
use sp1_core::stark::{
Chip, Com, Dom, LocalProver, OpeningProof, PcsProverData, ShardCommitment, ShardMainData,
ShardProof, StarkGenericConfig, StarkMachine,
};
use sp1_recursion_compiler::{
config::OuterConfig,
constraints::{groth16_ffi, ConstraintCompiler},
constraints::ConstraintCompiler,
ir::{Builder, Witness},
prelude::ExtConst,
};
use sp1_recursion_core::{
runtime::Runtime,
stark::{config::BabyBearPoseidon2Outer, RecursionAir},
};
use sp1_recursion_groth16_ffi::Groth16Prover;

use crate::stark::{tests::basic_program, StarkVerifierCircuit};

Expand Down Expand Up @@ -282,7 +282,6 @@ mod tests {
}

#[test]
#[serial]
fn test_verify_constraints_whole() {
type SC = BabyBearPoseidon2Outer;
type F = <SC as StarkGenericConfig>::Val;
Expand Down Expand Up @@ -361,6 +360,6 @@ mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}
}
11 changes: 4 additions & 7 deletions recursion/circuit/src/fri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ pub fn verify_shape_and_sample_challenges<C: Config>(
}

assert_eq!(proof.query_proofs.len(), config.num_queries);

challenger.check_witness(builder, config.proof_of_work_bits, proof.pow_witness);

let log_max_height = proof.commit_phase_commits.len() + config.log_blowup;
Expand Down Expand Up @@ -242,16 +241,16 @@ pub mod tests {
use p3_fri::{verifier, TwoAdicFriPcsProof};
use p3_matrix::dense::RowMajorMatrix;
use rand::rngs::OsRng;
use serial_test::serial;
use sp1_recursion_compiler::{
config::OuterConfig,
constraints::{groth16_ffi, ConstraintCompiler},
constraints::ConstraintCompiler,
ir::{Builder, Ext, Felt, SymbolicExt, Var, Witness},
};
use sp1_recursion_core::stark::config::{
outer_perm, test_fri_config, OuterChallenge, OuterChallengeMmcs, OuterChallenger,
OuterCompress, OuterDft, OuterFriProof, OuterHash, OuterPcs, OuterVal, OuterValMmcs,
};
use sp1_recursion_groth16_ffi::Groth16Prover;

use super::{verify_shape_and_sample_challenges, verify_two_adic_pcs, TwoAdicPcsRoundVariable};
use crate::{
Expand Down Expand Up @@ -402,7 +401,6 @@ pub mod tests {
}

#[test]
#[serial]
fn test_fri_verify_shape_and_sample_challenges() {
let mut rng = &mut OsRng;
let log_degrees = &[16, 9, 7, 4, 2];
Expand Down Expand Up @@ -485,11 +483,10 @@ pub mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_verify_two_adic_pcs() {
let mut rng = &mut OsRng;
let log_degrees = &[19, 19];
Expand Down Expand Up @@ -559,6 +556,6 @@ pub mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}
}
15 changes: 6 additions & 9 deletions recursion/circuit/src/poseidon2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,17 +58,16 @@ pub mod tests {
use p3_bn254_fr::Bn254Fr;
use p3_field::AbstractField;
use p3_symmetric::{CryptographicHasher, Permutation, PseudoCompressionFunction};
use serial_test::serial;
use sp1_recursion_compiler::config::OuterConfig;
use sp1_recursion_compiler::constraints::{groth16_ffi, ConstraintCompiler};
use sp1_recursion_compiler::constraints::ConstraintCompiler;
use sp1_recursion_compiler::ir::{Builder, Felt, Var, Witness};
use sp1_recursion_core::stark::config::{outer_perm, OuterCompress, OuterHash};
use sp1_recursion_groth16_ffi::Groth16Prover;

use crate::poseidon2::Poseidon2CircuitBuilder;
use crate::types::OuterDigestVariable;

#[test]
#[serial]
fn test_p2_permute_mut() {
let poseidon2 = outer_perm();
let input: [Bn254Fr; 3] = [
Expand All @@ -91,11 +90,10 @@ pub mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_p2_hash() {
let perm = outer_perm();
let hasher = OuterHash::new(perm.clone()).unwrap();
Expand All @@ -109,7 +107,7 @@ pub mod tests {
BabyBear::from_canonical_u32(2),
BabyBear::from_canonical_u32(2),
];
let output = hasher.hash_iter(input.into_iter());
let output = hasher.hash_iter(input);

let mut builder = Builder::<OuterConfig>::default();
let a: Felt<_> = builder.eval(input[0]);
Expand All @@ -125,11 +123,10 @@ pub mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}

#[test]
#[serial]
fn test_p2_compress() {
let perm = outer_perm();
let compressor = OuterCompress::new(perm.clone());
Expand All @@ -146,6 +143,6 @@ pub mod tests {

let mut backend = ConstraintCompiler::<OuterConfig>::default();
let constraints = backend.emit(builder.operations);
groth16_ffi::test_prove::<OuterConfig>(constraints, Witness::default());
Groth16Prover::test::<OuterConfig>(constraints.clone(), Witness::default());
}
}
Loading

0 comments on commit 1307b6e

Please sign in to comment.