Skip to content

Commit

Permalink
refactor: make nova proofs use the generic trait
Browse files Browse the repository at this point in the history
- uses the generic MultiFrame trait introduced in the prior commit to generically prove using nova,
- For references on the latest work defining this trait, see lurk-lab#629 (and its history, incl. lurk-lab#642, lurk-lab#707) and lurk-lab#677.

- make the benches use the generic trait, so LEM can bench similarly,
- make the examples use the generic trait, so LEM can example similarly,
- make Supernova use the generic trait, so LEM can NIVC similarly,
  • Loading branch information
huitseeker committed Sep 27, 2023
1 parent e4cc925 commit 8e3e252
Show file tree
Hide file tree
Showing 30 changed files with 1,567 additions and 581 deletions.
97 changes: 47 additions & 50 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,15 @@ use criterion::{
};

use lurk::{
circuit::circuit_frame::MultiFrame,
eval::{
empty_sym_env,
lang::{Coproc, Lang},
Evaluator,
},
field::LurkField,
proof::nova::NovaProver,
proof::Prover,
proof::{nova::NovaProver, MultiFrameTrait},
ptr::Ptr,
public_parameters,
state::State,
Expand Down Expand Up @@ -70,7 +71,7 @@ fn end2end_benchmark(c: &mut Criterion) {
let prover = NovaProver::new(reduction_count, lang_pallas);

// use cached public params
let pp = public_parameters::public_params(
let pp = public_parameters::public_params::<_, _, MultiFrame<_, Coproc<pallas::Scalar>>>(
reduction_count,
true,
lang_pallas_rc.clone(),
Expand All @@ -87,7 +88,7 @@ fn end2end_benchmark(c: &mut Criterion) {
b.iter(|| {
let ptr = go_base::<pallas::Scalar>(&mut store, state.clone(), s.0, s.1);
let _result = prover
.evaluate_and_prove(&pp, ptr, env, &mut store, limit, lang_pallas_rc.clone())
.evaluate_and_prove(&pp, ptr, env, &mut store, limit, &lang_pallas_rc)
.unwrap();
})
});
Expand Down Expand Up @@ -290,27 +291,25 @@ fn prove_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pallas::Scalar>(&mut store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas.clone());
let pp = public_parameters::public_params(
let pp = public_parameters::public_params::<_, _, MultiFrame<_, Coproc<pallas::Scalar>>>(
reduction_count,
true,
lang_pallas_rc.clone(),
Utf8Path::new(PUBLIC_PARAMS_PATH),
)
.unwrap();
let frames = prover
.get_evaluation_frames(
ptr,
empty_sym_env(&store),
&store,
limit,
lang_pallas_rc.clone(),
)
.unwrap();
let frames = MultiFrame::get_evaluation_frames(
|count| prover.needs_frame_padding(count),
ptr,
empty_sym_env(&store),
&store,
limit,
&lang_pallas,
)
.unwrap();

b.iter(|| {
let result = prover
.prove(&pp, &frames, &store, lang_pallas_rc.clone())
.unwrap();
let result = prover.prove(&pp, &frames, &store, &lang_pallas_rc).unwrap();
black_box(result);
})
});
Expand Down Expand Up @@ -340,30 +339,30 @@ fn prove_compressed_benchmark(c: &mut Criterion) {

let state = State::init_lurk_state().rccell();

let pp = public_parameters::public_params::<_, _, MultiFrame<_, _>>(
reduction_count,
true,
lang_pallas_rc.clone(),
Utf8Path::new(PUBLIC_PARAMS_PATH),
)
.unwrap();

group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pallas::Scalar>(&mut store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas.clone());
let pp = public_parameters::public_params(
reduction_count,
true,
lang_pallas_rc.clone(),
Utf8Path::new(PUBLIC_PARAMS_PATH),

let frames = MultiFrame::get_evaluation_frames(
|count| prover.needs_frame_padding(count),
ptr,
empty_sym_env(&store),
&store,
limit,
&lang_pallas,
)
.unwrap();
let frames = prover
.get_evaluation_frames(
ptr,
empty_sym_env(&store),
&store,
limit,
lang_pallas_rc.clone(),
)
.unwrap();

b.iter(|| {
let (proof, _, _, _) = prover
.prove(&pp, &frames, &store, lang_pallas_rc.clone())
.unwrap();
let (proof, _, _, _) = prover.prove(&pp, &frames, &store, &lang_pallas_rc).unwrap();

let compressed_result = proof.compress(&pp).unwrap();
black_box(compressed_result);
Expand Down Expand Up @@ -395,25 +394,24 @@ fn verify_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base(&mut store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas.clone());
let pp = public_parameters::public_params(
let pp = public_parameters::public_params::<_, _, MultiFrame<_, _>>(
reduction_count,
true,
lang_pallas_rc.clone(),
Utf8Path::new(PUBLIC_PARAMS_PATH),
)
.unwrap();
let frames = prover
.get_evaluation_frames(
ptr,
empty_sym_env(&store),
&store,
limit,
lang_pallas_rc.clone(),
)
.unwrap();
let (proof, z0, zi, num_steps) = prover
.prove(&pp, &frames, &store, lang_pallas_rc.clone())
.unwrap();
let frames = MultiFrame::get_evaluation_frames(
|count| prover.needs_frame_padding(count),
ptr,
empty_sym_env(&store),
&store,
limit,
&lang_pallas,
)
.unwrap();
let (proof, z0, zi, num_steps) =
prover.prove(&pp, &frames, &store, &lang_pallas_rc).unwrap();

b.iter_batched(
|| z0.clone(),
Expand Down Expand Up @@ -453,7 +451,7 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base(&mut store, state.clone(), s.0, s.1);
let prover = NovaProver::new(reduction_count, lang_pallas.clone());
let pp = public_parameters::public_params(
let pp = public_parameters::public_params::<_, _, MultiFrame<_, _>>(
reduction_count,
true,
lang_pallas_rc.clone(),
Expand All @@ -469,9 +467,8 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
lang_pallas_rc.clone(),
)
.unwrap();
let (proof, z0, zi, num_steps) = prover
.prove(&pp, &frames, &store, lang_pallas_rc.clone())
.unwrap();
let (proof, z0, zi, num_steps) =
prover.prove(&pp, &frames, &store, &lang_pallas_rc).unwrap();

let compressed_proof = proof.compress(&pp).unwrap();

Expand Down
5 changes: 3 additions & 2 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ use criterion::{
use pasta_curves::pallas;

use lurk::{
circuit::circuit_frame::MultiFrame,
eval::{
empty_sym_env,
lang::{Coproc, Lang},
Expand Down Expand Up @@ -75,7 +76,7 @@ fn fibo_prove<M: measurement::Measurement>(
let lang_rc = Arc::new(lang_pallas.clone());

// use cached public params
let pp = public_params(
let pp = public_params::<_, _, MultiFrame<_, _>>(
prove_params.reduction_count,
true,
lang_rc.clone(),
Expand Down Expand Up @@ -104,7 +105,7 @@ fn fibo_prove<M: measurement::Measurement>(
b.iter_batched(
|| (frames, lang_rc.clone()),
|(frames, lang_rc)| {
let result = prover.prove(&pp, frames, &store, lang_rc);
let result = prover.prove(&pp, frames, &store, &lang_rc);
let _ = black_box(result);
},
BatchSize::LargeInput,
Expand Down
18 changes: 15 additions & 3 deletions benches/public_params.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use blstrs::Scalar as Fr;
use criterion::{black_box, criterion_group, criterion_main, Criterion, SamplingMode};
use lurk::{
circuit::circuit_frame::MultiFrame,
eval::lang::{Coproc, Lang},
proof::groth16::Groth16Prover,
proof::nova,
Expand All @@ -26,15 +27,26 @@ fn public_params_benchmark(c: &mut Criterion) {

group.bench_function("public_params_nova", |b| {
b.iter(|| {
let result = nova::public_params(reduction_count, lang_pallas_rc.clone());
let result = nova::public_params::<_, _, MultiFrame<_, _>>(
reduction_count,
lang_pallas_rc.clone(),
);
black_box(result)
})
});

group.bench_function("public_params_groth", |b| {
b.iter(|| {
let result =
Groth16Prover::create_groth_params(DEFAULT_REDUCTION_COUNT, lang_bls_rc.clone());
let result = Groth16Prover::<
_,
Coproc<Fr>,
Fr,
MultiFrame<'_, Fr, Coproc<Fr>>,
>::create_groth_params(
DEFAULT_REDUCTION_COUNT,
lang_bls_rc.clone(),
)
.unwrap();
black_box(result)
})
});
Expand Down
9 changes: 5 additions & 4 deletions benches/sha256_ivc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
//! Note: The example [example/sha256_ivc.rs] is this same benchmark but as an example
//! that's easier to play with and run.
use lurk::circuit::circuit_frame::MultiFrame;
use lurk::circuit::gadgets::data::GlobalAllocations;
use lurk::state::user_sym;
use lurk::{circuit::gadgets::pointer::AllocatedContPtr, tag::Tag};
Expand Down Expand Up @@ -226,7 +227,7 @@ fn sha256_ivc_prove<M: measurement::Measurement>(
let lang_rc = Arc::new(lang.clone());

// use cached public params
let pp = public_params(
let pp = public_params::<_, _, MultiFrame<_, _>>(
reduction_count,
true,
lang_rc.clone(),
Expand Down Expand Up @@ -256,7 +257,7 @@ fn sha256_ivc_prove<M: measurement::Measurement>(
b.iter_batched(
|| (frames, lang_rc.clone()),
|(frames, lang_rc)| {
let result = prover.prove(&pp, frames, store, lang_rc);
let result = prover.prove(&pp, frames, store, &lang_rc);
let _ = black_box(result);
},
BatchSize::LargeInput,
Expand Down Expand Up @@ -309,7 +310,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(
let lang_rc = Arc::new(lang.clone());

// use cached public params
let pp = public_params(
let pp = public_params::<_, _, MultiFrame<_, _>>(
reduction_count,
true,
lang_rc.clone(),
Expand Down Expand Up @@ -339,7 +340,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(
b.iter_batched(
|| (frames, lang_rc.clone()),
|(frames, lang_rc)| {
let (proof, _, _, _) = prover.prove(&pp, frames, store, lang_rc).unwrap();
let (proof, _, _, _) = prover.prove(&pp, frames, store, &lang_rc).unwrap();
let compressed_result = proof.compress(&pp).unwrap();

let _ = black_box(compressed_result);
Expand Down
3 changes: 2 additions & 1 deletion benches/synthesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,8 @@ fn synthesize<M: measurement::Measurement>(
let env = empty_sym_env(&store);
let fib_n = (reduction_count / 3) as u64; // Heuristic, since one fib is 35 iterations.
let ptr = fib::<pasta_curves::Fq>(&mut store, state.clone(), black_box(fib_n));
let prover = NovaProver::new(*reduction_count, lang_pallas.clone());
let prover =
NovaProver::<_, _, MultiFrame<_, _>>::new(*reduction_count, lang_pallas.clone());

let frames = prover
.get_evaluation_frames(ptr, env, &store, limit, lang_rc.clone())
Expand Down
15 changes: 13 additions & 2 deletions clutch/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use fcomm::{
CommittedExpression, CommittedExpressionMap, LurkCont, LurkPtr, NovaProofCache, Opening, Proof,
PtrEvaluation,
};
use lurk::circuit::circuit_frame::MultiFrame;
use lurk::lurk_sym_ptr;
use lurk::public_parameters::public_params;

Expand Down Expand Up @@ -147,7 +148,14 @@ impl ReplTrait<F, Coproc<F>> for ClutchState<F, Coproc<F>> {

let lang_rc = Arc::new(lang.clone());
// Load params from disk cache, or generate them in the background.
thread::spawn(move || public_params(reduction_count, true, lang_rc, &public_param_dir()));
thread::spawn(move || {
public_params::<_, _, MultiFrame<_, Coproc<_>>>(
reduction_count,
true,
lang_rc,
&public_param_dir(),
)
});

Self {
repl_state: ReplState::new(s, limit, command, lang),
Expand Down Expand Up @@ -531,7 +539,10 @@ impl ClutchState<F, Coproc<F>> {
fn prove(&mut self, store: &mut Store<F>, rest: Ptr<F>) -> Result<Option<Ptr<F>>> {
let (proof_in_expr, _rest1) = store.car_cdr(&rest)?;

let prover = NovaProver::<F, Coproc<F>>::new(self.reduction_count, (*self.lang()).clone());
let prover = NovaProver::<'_, F, Coproc<F>, MultiFrame<'_, F, Coproc<F>>>::new(
self.reduction_count,
(*self.lang()).clone(),
);
let pp = public_params(self.reduction_count, true, self.lang(), &public_param_dir())?;

let proof = if rest.is_nil() {
Expand Down
8 changes: 5 additions & 3 deletions examples/circom.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ use std::marker::PhantomData;
use std::sync::Arc;
use std::time::Instant;

use lurk::circuit::circuit_frame::MultiFrame;
use lurk::circuit::gadgets::circom::CircomGadget;
use lurk::circuit::gadgets::pointer::AllocatedPtr;

Expand Down Expand Up @@ -114,13 +115,14 @@ fn main() {
let expr = format!("({coproc_expr})");
let ptr = store.read(&expr).unwrap();

let nova_prover = NovaProver::<Fr, Sha256Coproc<Fr>>::new(REDUCTION_COUNT, lang.clone());
let nova_prover =
NovaProver::<Fr, Sha256Coproc<Fr>, MultiFrame<_, _>>::new(REDUCTION_COUNT, lang.clone());
let lang_rc = Arc::new(lang);

println!("Setting up public parameters...");

let pp_start = Instant::now();
let pp = public_params::<_, Sha256Coproc<Fr>>(
let pp = public_params::<_, Sha256Coproc<Fr>, MultiFrame<_, _>>(
REDUCTION_COUNT,
true,
lang_rc.clone(),
Expand All @@ -135,7 +137,7 @@ fn main() {

let proof_start = Instant::now();
let (proof, z0, zi, num_steps) = nova_prover
.evaluate_and_prove(&pp, ptr, empty_sym_env(store), store, 10000, lang_rc)
.evaluate_and_prove(&pp, ptr, empty_sym_env(store), store, 10000, &lang_rc)
.unwrap();
let proof_end = proof_start.elapsed();

Expand Down
Loading

0 comments on commit 8e3e252

Please sign in to comment.