Skip to content

Commit

Permalink
plumbing state
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 6, 2023
1 parent 283dc7f commit ca7fe14
Show file tree
Hide file tree
Showing 24 changed files with 662 additions and 362 deletions.
65 changes: 59 additions & 6 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,21 @@ use lurk::{
proof::Prover,
ptr::Ptr,
public_parameters,
state::State,
store::Store,
};
use pasta_curves::pallas;
use std::sync::Arc;
use std::sync::Mutex;
use std::time::Duration;
use structopt::lazy_static::lazy_static;

const DEFAULT_REDUCTION_COUNT: usize = 10;

lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(State::init_lurk_state());
}

fn go_base<F: LurkField>(store: &mut Store<F>, a: u64, b: u64) -> Ptr<F> {
let program = format!(
r#"
Expand All @@ -37,7 +44,9 @@ fn go_base<F: LurkField>(store: &mut Store<F>, a: u64, b: u64) -> Ptr<F> {
"#
);

store.read(&program).unwrap()
store
.read_with_state(&mut STATE.lock().unwrap(), &program)
.unwrap()
}

/// To run these benchmarks, do `cargo criterion end2end_benchmark`.
Expand Down Expand Up @@ -70,7 +79,15 @@ fn end2end_benchmark(c: &mut Criterion) {
b.iter(|| {
let ptr = go_base::<pallas::Scalar>(&mut store, 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,
&STATE.lock().unwrap(),
limit,
lang_pallas_rc.clone(),
)
.unwrap();
})
});
Expand Down Expand Up @@ -169,6 +186,7 @@ fn eval_benchmark(c: &mut Criterion) {

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
let state = &STATE.lock().unwrap();
for size in sizes {
let parameter_string = format!("_{}_{}", size.0, size.1);

Expand All @@ -181,6 +199,7 @@ fn eval_benchmark(c: &mut Criterion) {
ptr,
empty_sym_env(&bls12_store),
&mut bls12_store,
state,
limit,
&lang_bls12,
)
Expand All @@ -198,6 +217,7 @@ fn eval_benchmark(c: &mut Criterion) {
ptr,
empty_sym_env(&pallas_store),
&mut pallas_store,
state,
limit,
&lang_pallas,
)
Expand Down Expand Up @@ -262,12 +282,21 @@ fn prove_benchmark(c: &mut Criterion) {
let size = (10, 0);
let benchmark_id = BenchmarkId::new("prove_go_base_nova", format!("_{}_{}", size.0, size.1));

let state = &STATE.lock().unwrap();

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

b.iter(|| {
Expand Down Expand Up @@ -300,13 +329,21 @@ fn prove_compressed_benchmark(c: &mut Criterion) {
"prove_compressed_go_base_nova",
format!("_{}_{}", size.0, size.1),
);
let state = &STATE.lock().unwrap();

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

b.iter(|| {
Expand Down Expand Up @@ -334,6 +371,7 @@ fn verify_benchmark(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
let state = &STATE.lock().unwrap();

let sizes = vec![(10, 0)];
for size in sizes {
Expand All @@ -345,7 +383,14 @@ fn verify_benchmark(c: &mut Criterion) {
let pp =
public_parameters::public_params(reduction_count, lang_pallas_rc.clone()).unwrap();
let frames = prover
.get_evaluation_frames(ptr, empty_sym_env(&store), &mut store, limit, &lang_pallas)
.get_evaluation_frames(
ptr,
empty_sym_env(&store),
&mut store,
state,
limit,
&lang_pallas,
)
.unwrap();
let (proof, z0, zi, num_steps) = prover
.prove(&pp, &frames, &mut store, lang_pallas_rc.clone())
Expand Down Expand Up @@ -379,6 +424,7 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
let state = &STATE.lock().unwrap();

let sizes = vec![(10, 0)];
for size in sizes {
Expand All @@ -390,7 +436,14 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
let pp =
public_parameters::public_params(reduction_count, lang_pallas_rc.clone()).unwrap();
let frames = prover
.get_evaluation_frames(ptr, empty_sym_env(&store), &mut store, limit, &lang_pallas)
.get_evaluation_frames(
ptr,
empty_sym_env(&store),
&mut store,
state,
limit,
&lang_pallas,
)
.unwrap();
let (proof, z0, zi, num_steps) = prover
.prove(&pp, &frames, &mut store, lang_pallas_rc.clone())
Expand Down
35 changes: 28 additions & 7 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{sync::Arc, time::Duration};
use std::{
sync::{Arc, Mutex},
time::Duration,
};

use criterion::{
black_box, criterion_group, criterion_main, measurement, BatchSize, BenchmarkGroup,
Expand All @@ -18,10 +21,17 @@ use lurk::{
proof::Prover,
ptr::Ptr,
public_parameters::public_params,
state::State,
store::Store,
};
use structopt::lazy_static::lazy_static;

const DEFAULT_REDUCTION_COUNT: usize = 100;

lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(State::init_lurk_state());
}

fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
let program = format!(
r#"
Expand All @@ -37,7 +47,9 @@ fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
"#
);

store.read(&program).unwrap()
store
.read_with_state(&mut STATE.lock().unwrap(), &program)
.unwrap()
}

#[allow(dead_code)]
Expand All @@ -49,6 +61,7 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut

// use cached public params
let pp = public_params(reduction_count, lang_rc.clone()).unwrap();
let state = &STATE.lock().unwrap();

c.bench_with_input(
BenchmarkId::new(name.to_string(), iterations),
Expand All @@ -63,7 +76,7 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
|| lang_rc.clone(),
|lang_rc| {
let result = prover
.evaluate_and_prove(&pp, ptr, env, &mut store, limit, lang_rc)
.evaluate_and_prove(&pp, ptr, env, &mut store, state, limit, lang_rc)
.unwrap();
black_box(result);
},
Expand All @@ -77,6 +90,7 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
let limit = 10_000_000_000;
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let state = &STATE.lock().unwrap();

c.bench_with_input(
BenchmarkId::new(name.to_string(), iterations),
Expand All @@ -85,9 +99,15 @@ fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut B
let mut store = Store::default();
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
b.iter(|| {
let result =
Evaluator::new(ptr, empty_sym_env(&store), &mut store, limit, &lang_pallas)
.eval();
let result = Evaluator::new(
ptr,
empty_sym_env(&store),
&mut store,
state,
limit,
&lang_pallas,
)
.eval();
black_box(result)
});
},
Expand All @@ -100,6 +120,7 @@ fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
let lang_rc = Arc::new(lang_pallas.clone());
let reduction_count = DEFAULT_REDUCTION_COUNT;
let pp = public_params(reduction_count, lang_rc.clone()).unwrap();
let state = &STATE.lock().unwrap();

c.bench_with_input(
BenchmarkId::new(name.to_string(), iterations),
Expand All @@ -111,7 +132,7 @@ fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
let prover = NovaProver::new(reduction_count, lang_pallas.clone());

let frames = prover
.get_evaluation_frames(ptr, env, &mut store, limit, &lang_pallas)
.get_evaluation_frames(ptr, env, &mut store, state, limit, &lang_pallas)
.unwrap();

b.iter_batched(
Expand Down
18 changes: 15 additions & 3 deletions benches/synthesis.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use std::{sync::Arc, time::Duration};
use std::{
sync::{Arc, Mutex},
time::Duration,
};

use bellperson::{util_cs::test_cs::TestConstraintSystem, Circuit};
use criterion::{
Expand All @@ -16,8 +19,14 @@ use lurk::{
proof::nova::NovaProver,
proof::Prover,
ptr::Ptr,
state::State,
store::Store,
};
use structopt::lazy_static::lazy_static;

lazy_static! {
static ref STATE: Mutex<State> = Mutex::new(State::init_lurk_state());
}

fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
let program = format!(
Expand All @@ -34,7 +43,9 @@ fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
"#
);

store.read(&program).unwrap()
store
.read_with_state(&mut STATE.lock().unwrap(), &program)
.unwrap()
}

fn synthesize<M: measurement::Measurement>(
Expand All @@ -55,9 +66,10 @@ fn synthesize<M: measurement::Measurement>(
let fib_n = (reduction_count / 3) as u64; // Heuristic, since one fib is 35 iterations.
let ptr = fib::<pasta_curves::Fq>(&mut store, black_box(fib_n));
let prover = NovaProver::new(*reduction_count, lang_pallas.clone());
let state = &STATE.lock().unwrap();

let frames = prover
.get_evaluation_frames(ptr, env, &mut store, limit, &lang_pallas)
.get_evaluation_frames(ptr, env, &mut store, state, limit, &lang_pallas)
.unwrap();

let multiframe =
Expand Down
Loading

0 comments on commit ca7fe14

Please sign in to comment.