Skip to content

Commit

Permalink
state mutability plumbing
Browse files Browse the repository at this point in the history
  • Loading branch information
arthurpaulino committed Aug 8, 2023
1 parent 7899075 commit 4b23a1b
Show file tree
Hide file tree
Showing 23 changed files with 604 additions and 559 deletions.
56 changes: 35 additions & 21 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@ use lurk::{
store::Store,
};
use pasta_curves::pallas;
use std::sync::{Arc, Mutex};
use std::time::Duration;
use structopt::lazy_static::lazy_static;
use std::{cell::RefCell, rc::Rc, sync::Arc};

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> {
fn go_base<F: LurkField>(
store: &mut Store<F>,
state: Rc<RefCell<State>>,
a: u64,
b: u64,
) -> Ptr<F> {
let program = format!(
r#"
(let ((foo (lambda (a b)
Expand All @@ -43,9 +43,7 @@ fn go_base<F: LurkField>(store: &mut Store<F>, a: u64, b: u64) -> Ptr<F> {
"#
);

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

/// To run these benchmarks, do `cargo criterion end2end_benchmark`.
Expand Down Expand Up @@ -74,9 +72,11 @@ fn end2end_benchmark(c: &mut Criterion) {
let size = (10, 0);
let benchmark_id = BenchmarkId::new("end2end_go_base_nova", format!("_{}_{}", size.0, size.1));

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

group.bench_with_input(benchmark_id, &size, |b, &s| {
b.iter(|| {
let ptr = go_base::<pallas::Scalar>(&mut store, s.0, s.1);
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())
.unwrap();
Expand All @@ -98,6 +98,8 @@ fn store_benchmark(c: &mut Criterion) {
let mut bls12_store = Store::<Fr>::default();
let mut pallas_store = Store::<pallas::Scalar>::default();

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

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
for size in sizes {
Expand All @@ -106,15 +108,15 @@ fn store_benchmark(c: &mut Criterion) {
let bls12_id = BenchmarkId::new("store_go_base_bls12", &parameter_string);
group.bench_with_input(bls12_id, &size, |b, &s| {
b.iter(|| {
let result = go_base::<Fr>(&mut bls12_store, s.0, s.1);
let result = go_base::<Fr>(&mut bls12_store, state.clone(), s.0, s.1);
black_box(result)
})
});

let pasta_id = BenchmarkId::new("store_go_base_pallas", &parameter_string);
group.bench_with_input(pasta_id, &size, |b, &s| {
b.iter(|| {
let result = go_base::<pallas::Scalar>(&mut pallas_store, s.0, s.1);
let result = go_base::<pallas::Scalar>(&mut pallas_store, state.clone(), s.0, s.1);
black_box(result)
})
});
Expand All @@ -135,6 +137,8 @@ fn hydration_benchmark(c: &mut Criterion) {
let mut bls12_store = Store::<Fr>::default();
let mut pallas_store = Store::<pallas::Scalar>::default();

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

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
for size in sizes {
Expand All @@ -143,15 +147,15 @@ fn hydration_benchmark(c: &mut Criterion) {
{
let benchmark_id = BenchmarkId::new("hydration_go_base_bls12", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let _ptr = go_base::<Fr>(&mut bls12_store, s.0, s.1);
let _ptr = go_base::<Fr>(&mut bls12_store, state.clone(), s.0, s.1);
b.iter(|| bls12_store.hydrate_scalar_cache())
});
}

{
let benchmark_id = BenchmarkId::new("hydration_go_base_pallas", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let _ptr = go_base::<pallas::Scalar>(&mut pallas_store, s.0, s.1);
let _ptr = go_base::<pallas::Scalar>(&mut pallas_store, state.clone(), s.0, s.1);
b.iter(|| pallas_store.hydrate_scalar_cache())
});
}
Expand All @@ -175,6 +179,8 @@ fn eval_benchmark(c: &mut Criterion) {
let mut bls12_store = Store::<Fr>::default();
let mut pallas_store = Store::<pallas::Scalar>::default();

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

// todo!() rfc out into more flexible test cases
let sizes = vec![(10, 16), (10, 160)];
for size in sizes {
Expand All @@ -183,7 +189,7 @@ fn eval_benchmark(c: &mut Criterion) {
{
let benchmark_id = BenchmarkId::new("eval_go_base_bls12", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<Fr>(&mut bls12_store, s.0, s.1);
let ptr = go_base::<Fr>(&mut bls12_store, state.clone(), s.0, s.1);
b.iter(|| {
Evaluator::new(
ptr,
Expand All @@ -200,7 +206,7 @@ fn eval_benchmark(c: &mut Criterion) {
{
let benchmark_id = BenchmarkId::new("eval_go_base_pallas", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pallas::Scalar>(&mut pallas_store, s.0, s.1);
let ptr = go_base::<pallas::Scalar>(&mut pallas_store, state.clone(), s.0, s.1);
b.iter(|| {
Evaluator::new(
ptr,
Expand Down Expand Up @@ -270,8 +276,10 @@ 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::init_lurk_state().mutable();

group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pallas::Scalar>(&mut store, s.0, s.1);
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, lang_pallas_rc.clone()).unwrap();
let frames = prover
Expand Down Expand Up @@ -309,8 +317,10 @@ fn prove_compressed_benchmark(c: &mut Criterion) {
format!("_{}_{}", size.0, size.1),
);

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

group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base::<pallas::Scalar>(&mut store, s.0, s.1);
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, lang_pallas_rc.clone()).unwrap();
let frames = prover
Expand Down Expand Up @@ -343,12 +353,14 @@ fn verify_benchmark(c: &mut Criterion) {
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;

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

let sizes = vec![(10, 0)];
for size in sizes {
let parameter_string = format!("_{}_{}", size.0, size.1);
let benchmark_id = BenchmarkId::new("verify_go_base_nova", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base(&mut store, s.0, s.1);
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(reduction_count, lang_pallas_rc.clone()).unwrap();
Expand Down Expand Up @@ -388,12 +400,14 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
let mut store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;

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

let sizes = vec![(10, 0)];
for size in sizes {
let parameter_string = format!("_{}_{}", size.0, size.1);
let benchmark_id = BenchmarkId::new("verify_compressed_go_base_nova", &parameter_string);
group.bench_with_input(benchmark_id, &size, |b, &s| {
let ptr = go_base(&mut store, s.0, s.1);
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(reduction_count, lang_pallas_rc.clone()).unwrap();
Expand Down
53 changes: 31 additions & 22 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Duration};

use criterion::{
black_box, criterion_group, criterion_main, measurement, BatchSize, BenchmarkGroup,
Expand All @@ -24,15 +21,10 @@ use lurk::{
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> {
fn fib<F: LurkField>(store: &mut Store<F>, state: Rc<RefCell<State>>, a: u64) -> Ptr<F> {
let program = format!(
r#"
(let ((fib (lambda (target)
Expand All @@ -47,13 +39,16 @@ fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
"#
);

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

#[allow(dead_code)]
fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
fn fibo_total<M: measurement::Measurement>(
name: &str,
iterations: u64,
c: &mut BenchmarkGroup<M>,
state: Rc<RefCell<State>>,
) {
let limit: usize = 10_000_000_000;
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
Expand All @@ -68,7 +63,7 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
|b, iterations| {
let mut store = Store::default();
let env = empty_sym_env(&store);
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
let ptr = fib::<pallas::Scalar>(&mut store, state.clone(), black_box(*iterations));
let prover = NovaProver::new(reduction_count, lang_pallas.clone());

b.iter_batched(
Expand All @@ -86,7 +81,12 @@ fn fibo_total<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
}

#[allow(dead_code)]
fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
fn fibo_eval<M: measurement::Measurement>(
name: &str,
iterations: u64,
c: &mut BenchmarkGroup<M>,
state: Rc<RefCell<State>>,
) {
let limit = 10_000_000_000;
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();

Expand All @@ -95,7 +95,7 @@ fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut B
&(iterations),
|b, iterations| {
let mut store = Store::default();
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
let ptr = fib::<pallas::Scalar>(&mut store, state.clone(), black_box(*iterations));
b.iter(|| {
let result =
Evaluator::new(ptr, empty_sym_env(&store), &mut store, limit, &lang_pallas)
Expand All @@ -106,7 +106,12 @@ fn fibo_eval<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut B
);
}

fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut BenchmarkGroup<M>) {
fn fibo_prove<M: measurement::Measurement>(
name: &str,
iterations: u64,
c: &mut BenchmarkGroup<M>,
state: Rc<RefCell<State>>,
) {
let limit = 10_000_000_000;
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
Expand All @@ -119,7 +124,7 @@ fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
|b, iterations| {
let mut store = Store::default();
let env = empty_sym_env(&store);
let ptr = fib::<pallas::Scalar>(&mut store, black_box(*iterations));
let ptr = fib::<pallas::Scalar>(&mut store, state.clone(), black_box(*iterations));
let prover = NovaProver::new(reduction_count, lang_pallas.clone());

let frames = prover
Expand All @@ -142,8 +147,10 @@ fn fibo_prove<M: measurement::Measurement>(name: &str, iterations: u64, c: &mut
fn fibonacci_eval(c: &mut Criterion) {
static BATCH_SIZES: [u64; 2] = [100, 1000];
let mut group: BenchmarkGroup<_> = c.benchmark_group("Evaluate");
let state = State::init_lurk_state().mutable();

for size in BATCH_SIZES.iter() {
fibo_eval("Fibonacci", *size, &mut group);
fibo_eval("Fibonacci", *size, &mut group, state.clone());
}
}

Expand All @@ -152,9 +159,10 @@ fn fibonacci_prove(c: &mut Criterion) {
let mut group: BenchmarkGroup<_> = c.benchmark_group("Prove");
group.sampling_mode(SamplingMode::Flat); // This can take a *while*
group.sample_size(10);
let state = State::init_lurk_state().mutable();

for size in BATCH_SIZES.iter() {
fibo_prove("Fibonacci", *size, &mut group);
fibo_prove("Fibonacci", *size, &mut group, state.clone());
}
}

Expand All @@ -164,9 +172,10 @@ fn fibonacci_total(c: &mut Criterion) {
let mut group: BenchmarkGroup<_> = c.benchmark_group("Total");
group.sampling_mode(SamplingMode::Flat); // This can take a *while*
group.sample_size(10);
let state = State::init_lurk_state().mutable();

for size in BATCH_SIZES.iter() {
fibo_total("Fibonacci", *size, &mut group);
fibo_total("Fibonacci", *size, &mut group, state.clone());
}
}

Expand Down
19 changes: 5 additions & 14 deletions benches/synthesis.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
use std::{
sync::{Arc, Mutex},
time::Duration,
};
use std::{cell::RefCell, rc::Rc, sync::Arc, time::Duration};

use bellperson::{util_cs::test_cs::TestConstraintSystem, Circuit};
use criterion::{
Expand All @@ -22,13 +19,8 @@ use lurk::{
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> {
fn fib<F: LurkField>(store: &mut Store<F>, state: Rc<RefCell<State>>, a: u64) -> Ptr<F> {
let program = format!(
r#"
(let ((fib (lambda (target)
Expand All @@ -43,9 +35,7 @@ fn fib<F: LurkField>(store: &mut Store<F>, a: u64) -> Ptr<F> {
"#
);

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

fn synthesize<M: measurement::Measurement>(
Expand All @@ -56,6 +46,7 @@ fn synthesize<M: measurement::Measurement>(
let limit = 1_000_000;
let lang_pallas = Lang::<pasta_curves::Fq, Coproc<pasta_curves::Fq>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
let state = State::init_lurk_state().mutable();

c.bench_with_input(
BenchmarkId::new(name.to_string(), reduction_count),
Expand All @@ -64,7 +55,7 @@ fn synthesize<M: measurement::Measurement>(
let mut store = Store::default();
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, black_box(fib_n));
let ptr = fib::<pasta_curves::Fq>(&mut store, state.clone(), black_box(fib_n));
let prover = NovaProver::new(*reduction_count, lang_pallas.clone());

let frames = prover
Expand Down
Loading

0 comments on commit 4b23a1b

Please sign in to comment.