Skip to content

Commit

Permalink
Cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
samuelburnham committed Oct 18, 2023
1 parent c70213b commit df030a9
Show file tree
Hide file tree
Showing 11 changed files with 183 additions and 177 deletions.
13 changes: 13 additions & 0 deletions benches/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,14 @@
use lurk::config::{Settings, LURK_CONFIG};

pub const PUBLIC_PARAMS_BENCH_PATH: &str = "/var/tmp/lurk_benches/public_params";

/// Sets the config settings with the temporary public parameter cache location
pub fn set_bench_config() -> bool {
let mut settings = Settings::new();
settings.public_params_dir = PUBLIC_PARAMS_BENCH_PATH.into();
// If global config already set, check it has the right param cache
LURK_CONFIG.set(settings).map_or_else(
|preset| preset.public_params_dir == PUBLIC_PARAMS_BENCH_PATH,
|_| true,
)
}
13 changes: 6 additions & 7 deletions benches/end2end.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ use lurk::{
ptr::Ptr,
public_parameters::{
self,
disk_cache::set_public_params_dir,
instance::{Instance, Kind},
},
state::State,
Expand All @@ -27,7 +26,7 @@ use std::time::Duration;
use std::{cell::RefCell, rc::Rc, sync::Arc};

mod common;
use common::PUBLIC_PARAMS_BENCH_PATH;
use common::set_bench_config;

const DEFAULT_REDUCTION_COUNT: usize = 10;

Expand Down Expand Up @@ -71,7 +70,7 @@ fn end2end_benchmark(c: &mut Criterion) {
let prover = NovaProver::new(reduction_count, lang_pallas);

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_pallas_rc.clone(),
Expand Down Expand Up @@ -283,7 +282,7 @@ fn prove_benchmark(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());

let size = (10, 0);
let benchmark_id = BenchmarkId::new("prove_go_base_nova", format!("_{}_{}", size.0, size.1));
Expand Down Expand Up @@ -332,7 +331,7 @@ fn prove_compressed_benchmark(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());

let size = (10, 0);
let benchmark_id = BenchmarkId::new(
Expand Down Expand Up @@ -385,7 +384,7 @@ fn verify_benchmark(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());

let state = State::init_lurk_state().rccell();
let instance = Instance::new(
Expand Down Expand Up @@ -444,7 +443,7 @@ fn verify_compressed_benchmark(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());
let store = Store::default();
let reduction_count = DEFAULT_REDUCTION_COUNT;
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());

let state = State::init_lurk_state().rccell();
let instance = Instance::new(
Expand Down
13 changes: 6 additions & 7 deletions benches/end2end_lem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ use lurk::{
proof::{nova::NovaProver, Prover},
public_parameters::{
self,
disk_cache::set_public_params_dir,
instance::{Instance, Kind},
},
state::State,
};

mod common;
use common::PUBLIC_PARAMS_BENCH_PATH;
use common::set_bench_config;

const DEFAULT_REDUCTION_COUNT: usize = 10;

Expand Down Expand Up @@ -69,7 +68,7 @@ fn end2end_benchmark_lem(c: &mut Criterion) {
NovaProver::new(reduction_count, (*lang_pallas_rc).clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_pallas_rc.clone(),
Expand Down Expand Up @@ -269,7 +268,7 @@ fn prove_benchmark_lem(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_pallas_rc.clone(),
Expand Down Expand Up @@ -317,7 +316,7 @@ fn prove_compressed_benchmark_lem(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_pallas_rc.clone(),
Expand Down Expand Up @@ -359,7 +358,7 @@ fn verify_benchmark_lem(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_pallas_rc.clone(),
Expand Down Expand Up @@ -412,7 +411,7 @@ fn verify_compressed_benchmark_lem(c: &mut Criterion) {
let lang_pallas_rc = Arc::new(lang_pallas.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_pallas_rc.clone(),
Expand Down
7 changes: 3 additions & 4 deletions benches/fibonacci.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ use lurk::{
proof::Prover,
ptr::Ptr,
public_parameters::{
disk_cache::set_public_params_dir,
instance::{Instance, Kind},
public_params,
},
Expand All @@ -27,7 +26,7 @@ use lurk::{
};

mod common;
use common::PUBLIC_PARAMS_BENCH_PATH;
use common::set_bench_config;

fn fib<F: LurkField>(store: &Store<F>, state: Rc<RefCell<State>>, _a: u64) -> Ptr<F> {
let program = r#"
Expand Down Expand Up @@ -80,7 +79,7 @@ fn fibo_prove<M: measurement::Measurement>(
let lang_rc = Arc::new(lang_pallas.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_rc.clone(),
Expand Down Expand Up @@ -121,7 +120,7 @@ fn fibo_prove<M: measurement::Measurement>(

fn fibonacci_prove(c: &mut Criterion) {
tracing::debug!("{:?}", &lurk::config::LURK_CONFIG);
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let reduction_counts = [100, 600, 700, 800, 900];
let batch_sizes = [100, 200];
let mut group: BenchmarkGroup<'_, _> = c.benchmark_group("Prove");
Expand Down
4 changes: 4 additions & 0 deletions benches/fibonacci_lem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ use lurk::{
state::State,
};

mod common;
use common::set_bench_config;

fn fib<F: LurkField>(store: &Store<F>, state: Rc<RefCell<State>>, _a: u64) -> Ptr<F> {
let program = r#"
(letrec ((next (lambda (a b) (next b (+ a b))))
Expand Down Expand Up @@ -69,6 +72,7 @@ fn fibo_prove<M: measurement::Measurement>(
let limit = fib_limit(fib_n, reduction_count);
let lang_pallas = Lang::<pallas::Scalar, Coproc<pallas::Scalar>>::new();
let lang_rc = Arc::new(lang_pallas.clone());
assert!(set_bench_config());

// use cached public params
let instance = Instance::new(
Expand Down
13 changes: 6 additions & 7 deletions benches/sha256.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ use lurk::{
proof::{nova::NovaProver, supernova::SuperNovaProver, Prover},
ptr::Ptr,
public_parameters::{
disk_cache::set_public_params_dir,
instance::{Instance, Kind},
public_params, supernova_public_params,
},
Expand All @@ -29,7 +28,7 @@ use lurk::{
};

mod common;
use common::PUBLIC_PARAMS_BENCH_PATH;
use common::set_bench_config;

fn sha256_ivc<F: LurkField>(
store: &Store<F>,
Expand Down Expand Up @@ -108,7 +107,7 @@ fn sha256_ivc_prove<M: measurement::Measurement>(
let lang_rc = Arc::new(lang.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance: Instance<
'_,
pasta_curves::Fq,
Expand Down Expand Up @@ -155,7 +154,7 @@ fn sha256_ivc_prove<M: measurement::Measurement>(

fn ivc_prove_benchmarks(c: &mut Criterion) {
tracing::debug!("{:?}", &lurk::config::LURK_CONFIG);
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let reduction_counts = [10, 100];
let batch_sizes = [1, 2, 5, 10, 20];
let mut group: BenchmarkGroup<'_, _> = c.benchmark_group("prove");
Expand Down Expand Up @@ -198,7 +197,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(
let lang_rc = Arc::new(lang.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_rc.clone(),
Expand Down Expand Up @@ -242,7 +241,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(

fn ivc_prove_compressed_benchmarks(c: &mut Criterion) {
tracing::debug!("{:?}", &lurk::config::LURK_CONFIG);
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let reduction_counts = [10, 100];
let batch_sizes = [1, 2, 5, 10, 20];
let mut group: BenchmarkGroup<'_, _> = c.benchmark_group("prove_compressed");
Expand Down Expand Up @@ -285,7 +284,7 @@ fn sha256_nivc_prove<M: measurement::Measurement>(
let lang_rc = Arc::new(lang.clone());

// use cached public params
set_public_params_dir(PUBLIC_PARAMS_BENCH_PATH);
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_rc.clone(),
Expand Down
7 changes: 6 additions & 1 deletion benches/sha256_lem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ use lurk::{
state::{user_sym, State},
};

mod common;
use common::set_bench_config;

fn sha256_ivc<F: LurkField>(
store: &Store<F>,
state: Rc<RefCell<State>>,
Expand Down Expand Up @@ -108,7 +111,7 @@ fn sha256_ivc_prove<M: measurement::Measurement>(
let lurk_step = make_eval_step_from_lang(&lang, true);

// use cached public params

assert!(set_bench_config());
let instance: Instance<'_, Fr, Sha256Coproc<Fr>, MultiFrame<'_, _, _>> = Instance::new(
reduction_count,
lang_rc.clone(),
Expand Down Expand Up @@ -191,6 +194,7 @@ fn sha256_ivc_prove_compressed<M: measurement::Measurement>(
let lurk_step = make_eval_step_from_lang(&lang, true);

// use cached public params
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_rc.clone(),
Expand Down Expand Up @@ -275,6 +279,7 @@ fn sha256_nivc_prove<M: measurement::Measurement>(
let lurk_step = make_eval_step_from_lang(&lang, false);

// use cached public params
assert!(set_bench_config());
let instance = Instance::new(
reduction_count,
lang_rc.clone(),
Expand Down
38 changes: 27 additions & 11 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! Global config for the CLI
//! Includes settings for cache locations and proof backend
use std::collections::HashMap;

use crate::config::LURK_CONFIG;
Expand All @@ -10,9 +12,8 @@ use serde::Deserialize;
use super::backend::Backend;
use super::paths::{circom_default_dir, commits_default_dir, proofs_default_dir};

/// Global config varable for CLI `Settings`
pub static CLI_CONFIG: OnceCell<Settings> = OnceCell::new();
//
pub static CLI_PUBLIC_PARAMS_DIR: OnceCell<Utf8PathBuf> = OnceCell::new();

/// Contains the CLI config settings
/// The config file is located in `$HOME/.lurk/lurk.toml` or `<current_dir>/.lurk/lurk.toml` on WASM.
Expand All @@ -26,20 +27,34 @@ pub static CLI_PUBLIC_PARAMS_DIR: OnceCell<Utf8PathBuf> = OnceCell::new();
/// ```
/// NOTE: Config settings share the same `lurk.toml` file for now.
/// It's good practice to avoid duplication of shared settings like `public_params_dir`
/// in specialized structs like this one to prevent conflicts.
/// in downstream configs like these to prevent conflicts.
#[derive(Debug, Deserialize)]

Check warning on line 31 in src/cli/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/config.rs#L31

Added line #L31 was not covered by tests
pub struct Settings {
/// Cache directory for proofs
pub proofs_dir: Utf8PathBuf,
/// Cache directory for commitments
pub commits_dir: Utf8PathBuf,
/// Cache directory for Circom files
pub circom_dir: Utf8PathBuf,
/// Proof generation and verification system
pub backend: Backend,
/// Finite field used for evaluation and proving
pub field: LanguageField,
/// Reduction count, which is the number of circuit reductions per step
pub rc: usize,
/// Iteration limit for the program, which is arbitrary to user preferences
/// Used mainly as a safety check, similar to default stack size
pub limit: usize,
}

impl Settings {
pub fn new(
/// Creates a default config, which can be modified before setting `CLI_CONFIG`
pub fn new() -> Self {
Self::default()
}

Check warning on line 54 in src/cli/config.rs

View check run for this annotation

Codecov / codecov/patch

src/cli/config.rs#L52-L54

Added lines #L52 - L54 were not covered by tests

/// Loads config settings from a file or env var, or CLI arg if applicable
pub fn from_config(
config_file: &Utf8PathBuf,
cli_settings: &HashMap<&str, String>,
) -> Result<Self, ConfigError> {
Expand All @@ -52,9 +67,13 @@ impl Settings {
"rc",
"limit",
);
// TODO: Probably should leave the global init outside of this function
// Silence errors if already set
LURK_CONFIG
.set(crate::config::Settings::new(config_file, cli_settings)?)
.set(crate::config::Settings::from_config(
config_file,
cli_settings,
)?)
.unwrap_or(());
Config::builder()
.set_default(proofs, proofs_default_dir().to_string())?
Expand All @@ -67,7 +86,7 @@ impl Settings {
.add_source(File::with_name(config_file.as_str()).required(false))
// Then overwrite with any `LURK` environment variables
.add_source(Environment::with_prefix("LURK"))
// TODO: Derive config::Source on the Settings struct and use `add_source` instead
// TODO: Derive config::Source for `cli_settings` and use `add_source` instead
.set_override_option(proofs, cli_settings.get(proofs).map(|v| v.to_owned()))?
.set_override_option(commits, cli_settings.get(commits).map(|v| v.to_owned()))?
.set_override_option(circom, cli_settings.get(circom).map(|v| v.to_owned()))?
Expand Down Expand Up @@ -102,7 +121,7 @@ mod tests {
use tempfile::Builder;

use crate::cli::backend::Backend;
use crate::cli::config::{Settings, CLI_CONFIG};
use crate::cli::config::Settings;
use crate::config::LURK_CONFIG;
use crate::field::LanguageField;

Expand Down Expand Up @@ -147,10 +166,7 @@ mod tests {
.write_all(format!("limit = {limit}\n").as_bytes())
.unwrap();

CLI_CONFIG
.set(Settings::new(&config_dir, &HashMap::new()).unwrap())
.unwrap();
let cli_config = CLI_CONFIG.get().unwrap();
let cli_config = Settings::from_config(&config_dir, &HashMap::new()).unwrap();
let lurk_config = LURK_CONFIG.get().unwrap();
assert_eq!(lurk_config.public_params_dir, public_params_dir);
assert_eq!(cli_config.proofs_dir, proofs_dir);
Expand Down
Loading

0 comments on commit df030a9

Please sign in to comment.