Skip to content

Commit

Permalink
Tigher visibility conditions for Lurk (#900)
Browse files Browse the repository at this point in the history
* fix: make z_data::serde private

* refactor: restrict visibility of CLI backend/config

* make circuit::data pub(crate)

* refactor: make gadgets from #849 explicitly unused

* make interpreter pub(crate)

* refactor: make top-level  expr, cont, hash, package private

* refactor: make ptr pub(crate)

* refactor: make symbol crate-private

* refactor: make syntax crate-private

* refactor: make tag crate-private

* refactor: make uint crate-private

* refactor: make writer crate-private

* refactor: limit visibility in public_params

* refactor: Refactor variant naming in public parameters module

- Renamed `Error::IOError` to `Error::IO` and `Error::CacheError` to `Error::Cache` across multiple files for consistency.

* fix: repair doctest
  • Loading branch information
huitseeker authored Nov 16, 2023
1 parent 8e9e8b5 commit 9781652
Show file tree
Hide file tree
Showing 21 changed files with 83 additions and 74 deletions.
1 change: 1 addition & 0 deletions src/circuit/gadgets/constraints.rs
Original file line number Diff line number Diff line change
Expand Up @@ -404,6 +404,7 @@ pub(crate) fn pick_const<F: PrimeField, CS: ConstraintSystem<F>>(
}

/// Convert from Boolean to AllocatedNum.
#[allow(dead_code)]
pub(crate) fn boolean_to_num<F: PrimeField, CS: ConstraintSystem<F>>(
mut cs: CS,
bit: &Boolean,
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/gadgets/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -286,7 +286,7 @@ pub(crate) fn hash_poseidon<CS: ConstraintSystem<F>, F: LurkField, A: Arity<F>>(
}
}

pub fn allocate_constant<F: LurkField, CS: ConstraintSystem<F>>(
pub(crate) fn allocate_constant<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
val: F,
) -> AllocatedNum<F> {
Expand Down
2 changes: 1 addition & 1 deletion src/circuit/gadgets/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ pub(crate) mod macros;
pub(crate) mod case;
pub mod circom;
pub mod constraints;
pub mod data;
pub(crate) mod data;
pub(crate) mod hashes;
pub mod pointer;
2 changes: 1 addition & 1 deletion src/cli/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use serde::Deserialize;
use crate::field::LanguageField;

#[derive(Clone, Default, Debug, Deserialize, ValueEnum, PartialEq, Eq)]
pub enum Backend {
pub(crate) enum Backend {
#[default]
Nova,
}
Expand Down
22 changes: 11 additions & 11 deletions src/cli/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use super::backend::Backend;
use super::paths::{circom_default_dir, commits_default_dir, proofs_default_dir};

/// Global config varable for `CliSettings`
pub static CLI_CONFIG: OnceCell<CliSettings> = OnceCell::new();
pub(crate) static CLI_CONFIG: OnceCell<CliSettings> = OnceCell::new();

/// Gets the `CLI_CONFIG` settings. If uninitialized, sets the global variable
/// in the following order (greatest to least precedence):
Expand All @@ -27,7 +27,7 @@ pub static CLI_CONFIG: OnceCell<CliSettings> = OnceCell::new();
/// ```
/// Other file formats are supported by the `config` crate, but only TOML is tested
/// - Default values, e.g. `$HOME/.lurk/proofs`
pub fn cli_config(
pub(crate) fn cli_config(
config_file: Option<&Utf8PathBuf>,
settings: Option<&HashMap<&str, String>>,
) -> &'static CliSettings {
Expand All @@ -44,27 +44,27 @@ pub fn cli_config(
// It's good practice to avoid duplication of shared settings like `public_params_dir`
// in downstream configs like these to prevent conflicts.
#[derive(Debug, Deserialize)]
pub struct CliSettings {
pub(crate) struct CliSettings {
/// Cache directory for proofs
pub proofs_dir: Utf8PathBuf,
pub(crate) proofs_dir: Utf8PathBuf,
/// Cache directory for commitments
pub commits_dir: Utf8PathBuf,
pub(crate) commits_dir: Utf8PathBuf,
/// Cache directory for Circom files
pub circom_dir: Utf8PathBuf,
pub(crate) circom_dir: Utf8PathBuf,
/// Proof generation and verification system
pub backend: Backend,
pub(crate) backend: Backend,
/// Finite field used for evaluation and proving
pub field: LanguageField,
pub(crate) field: LanguageField,
/// Reduction count, which is the number of circuit reductions per step
pub rc: usize,
pub(crate) 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,
pub(crate) limit: usize,
}

impl CliSettings {
/// Loads config settings from a file or env var, or CLI arg if applicable
pub fn from_config(
pub(crate) fn from_config(
config_file: &Utf8PathBuf,
cli_settings: Option<&HashMap<&str, String>>,
) -> Result<Self, ConfigError> {
Expand Down
6 changes: 3 additions & 3 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
pub mod backend;
mod backend;
mod circom;
mod commitment;
pub mod config;
mod config;
mod field_data;
mod lurk_proof;
pub mod paths;
pub mod repl;
mod repl;
mod zstore;

use anyhow::{bail, Context, Result};
Expand Down
8 changes: 4 additions & 4 deletions src/cli/repl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl<F: LurkField> Evaluation<F> {
}

#[allow(dead_code)]
pub struct Repl<F: LurkField> {
pub(crate) struct Repl<F: LurkField> {
store: Store<F>,
state: Rc<RefCell<State>>,
env: Ptr<F>,
Expand Down Expand Up @@ -144,7 +144,7 @@ impl<F: LurkField> Repl<F> {
type F = pasta_curves::pallas::Scalar; // TODO: generalize this

impl Repl<F> {
pub fn new(store: Store<F>, rc: usize, limit: usize, backend: Backend) -> Repl<F> {
pub(crate) fn new(store: Store<F>, rc: usize, limit: usize, backend: Backend) -> Repl<F> {
let limit = pad(limit, rc);
info!(
"Launching REPL with backend {backend}, field {}, rc {rc} and limit {limit}",
Expand Down Expand Up @@ -394,7 +394,7 @@ impl Repl<F> {
Ok(hash)
}

pub fn handle_non_meta(&mut self, expr_ptr: Ptr<F>) -> Result<()> {
pub(crate) fn handle_non_meta(&mut self, expr_ptr: Ptr<F>) -> Result<()> {
let (output, iterations) = self.eval_expr_and_memoize(expr_ptr)?;
let iterations_display = Self::pretty_iterations_display(iterations);
match output[2].tag() {
Expand Down Expand Up @@ -472,7 +472,7 @@ impl Repl<F> {
Ok(new_input)
}

pub fn load_file(&mut self, file_path: &Utf8Path, demo: bool) -> Result<()> {
pub(crate) fn load_file(&mut self, file_path: &Utf8Path, demo: bool) -> Result<()> {
let input = read_to_string(file_path)?;
if demo {
println!("Loading {file_path} in demo mode");
Expand Down
30 changes: 20 additions & 10 deletions src/coprocessor/gadgets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ use crate::{
};

/// Constructs an `AllocatedPtr` compound by two others
pub fn construct_tuple2<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
#[allow(dead_code)]
pub(crate) fn construct_tuple2<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
cs: CS,
g: &GlobalAllocator<F>,
store: &Store<F>,
Expand All @@ -43,7 +44,8 @@ pub fn construct_tuple2<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
}

/// Constructs an `AllocatedPtr` compound by three others
pub fn construct_tuple3<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
#[allow(dead_code)]
pub(crate) fn construct_tuple3<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
cs: CS,
g: &GlobalAllocator<F>,
store: &Store<F>,
Expand Down Expand Up @@ -71,7 +73,8 @@ pub fn construct_tuple3<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
}

/// Constructs an `AllocatedPtr` compound by four others
pub fn construct_tuple4<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
#[allow(dead_code)]
pub(crate) fn construct_tuple4<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
cs: CS,
g: &GlobalAllocator<F>,
store: &Store<F>,
Expand Down Expand Up @@ -102,8 +105,9 @@ pub fn construct_tuple4<F: LurkField, CS: ConstraintSystem<F>, T: Tag>(
}

/// Constructs a `Cons` pointer
#[allow(dead_code)]
#[inline]
pub fn construct_cons<F: LurkField, CS: ConstraintSystem<F>>(
pub(crate) fn construct_cons<F: LurkField, CS: ConstraintSystem<F>>(
cs: CS,
g: &GlobalAllocator<F>,
store: &Store<F>,
Expand All @@ -115,7 +119,8 @@ pub fn construct_cons<F: LurkField, CS: ConstraintSystem<F>>(

/// Constructs a cons-list with the provided `elts`. The terminating value defaults
/// to `nil` when `last` is `None`
pub fn construct_list<F: LurkField, CS: ConstraintSystem<F>>(
#[allow(dead_code)]
pub(crate) fn construct_list<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
g: &GlobalAllocator<F>,
store: &Store<F>,
Expand All @@ -136,6 +141,7 @@ pub fn construct_list<F: LurkField, CS: ConstraintSystem<F>>(

/// Retrieves the `Ptr` that corresponds to `a_ptr` by using the `Store` as the
/// hint provider
#[allow(dead_code)]
fn get_ptr<F: LurkField>(
a_ptr: &AllocatedPtr<F>,
store: &Store<F>,
Expand All @@ -160,7 +166,8 @@ fn get_ptr<F: LurkField>(
///
/// # Panics
/// Panics if the store can't deconstruct the tuple pointer
pub fn deconstruct_tuple2<F: LurkField, CS: ConstraintSystem<F>>(
#[allow(dead_code)]
pub(crate) fn deconstruct_tuple2<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
store: &Store<F>,
not_dummy: &Boolean,
Expand Down Expand Up @@ -202,7 +209,8 @@ pub fn deconstruct_tuple2<F: LurkField, CS: ConstraintSystem<F>>(
///
/// # Panics
/// Panics if the store can't deconstruct the tuple pointer
pub fn deconstruct_tuple3<F: LurkField, CS: ConstraintSystem<F>>(
#[allow(dead_code)]
pub(crate) fn deconstruct_tuple3<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
store: &Store<F>,
not_dummy: &Boolean,
Expand Down Expand Up @@ -247,7 +255,8 @@ pub fn deconstruct_tuple3<F: LurkField, CS: ConstraintSystem<F>>(
///
/// # Panics
/// Panics if the store can't deconstruct the tuple pointer
pub fn deconstruct_tuple4<F: LurkField, CS: ConstraintSystem<F>>(
#[allow(dead_code)]
pub(crate) fn deconstruct_tuple4<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
store: &Store<F>,
not_dummy: &Boolean,
Expand Down Expand Up @@ -308,7 +317,8 @@ pub fn deconstruct_tuple4<F: LurkField, CS: ConstraintSystem<F>>(
///
/// # Panics
/// Panics if the store can't deconstruct `data` with `car_cdr`
pub fn car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
#[allow(dead_code)]
pub(crate) fn car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
g: &GlobalAllocator<F>,
store: &Store<F>,
Expand Down Expand Up @@ -469,6 +479,7 @@ pub fn car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
/// assert_eq!(a_ptr_as_z_ptr(&cdr), Some(z_empty_str));
/// assert_eq!(length.get_value(), Some(Fq::from_u64(2)));
/// ```
#[allow(dead_code)]
pub fn chain_car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
cs: &mut CS,
g: &GlobalAllocator<F>,
Expand Down Expand Up @@ -500,7 +511,6 @@ pub fn chain_car_cdr<F: LurkField, CS: ConstraintSystem<F>>(
}

#[inline]
#[allow(dead_code)]
pub fn a_ptr_as_z_ptr<T: Tag, F: LurkField>(
a: &AllocatedPtr<F>,
) -> Option<crate::z_ptr::ZPtr<T, F>> {
Expand Down
2 changes: 1 addition & 1 deletion src/lem/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@
pub mod circuit;
pub mod eval;
pub mod interpreter;
pub(crate) mod interpreter;
mod macros;
pub mod multiframe;
mod path;
Expand Down
20 changes: 10 additions & 10 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,29 +5,29 @@
pub mod circuit;
pub mod cli;
pub mod config;
pub mod cont;
mod cont;
pub mod coprocessor;
pub mod error;
pub mod eval;
pub mod expr;
mod expr;
pub mod field;
pub mod hash;
mod hash;
pub mod hash_witness;
pub mod lem;
mod num;
pub mod package;
mod package;
pub mod parser;
pub mod proof;
pub mod ptr;
mod ptr;
pub mod public_parameters;
pub mod state;
pub mod store;
pub mod symbol;
pub mod syntax;
mod symbol;
mod syntax;
mod syntax_macros;
pub mod tag;
pub mod uint;
pub mod writer;
mod tag;
mod uint;
mod writer;
pub mod z_data;
pub use num::Num;
pub use symbol::Symbol;
Expand Down
2 changes: 1 addition & 1 deletion src/package.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use std::{

use crate::Symbol;

pub type SymbolRef = Arc<Symbol>;
pub(crate) type SymbolRef = Arc<Symbol>;

#[derive(Debug)]
pub struct Package {
Expand Down
6 changes: 3 additions & 3 deletions src/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,8 @@ impl<F: LurkField, T: Tag> Hash for GPtr<F, T> {
}
}

pub type Ptr<F> = GPtr<F, ExprTag>;
pub type ContPtr<F> = GPtr<F, ContTag>;
pub(crate) type Ptr<F> = GPtr<F, ExprTag>;
pub(crate) type ContPtr<F> = GPtr<F, ContTag>;

impl<F: LurkField, T: Tag> GPtr<F, T> {
/// check if a Ptr is an opaque pointer
Expand Down Expand Up @@ -184,7 +184,7 @@ impl<F: LurkField> ContPtr<F> {
}
}

pub trait TypePredicates {
pub(crate) trait TypePredicates {
fn is_fun(&self) -> bool;
fn is_self_evaluating(&self) -> bool;
fn is_potentially(&self, tag: ExprTag) -> bool;
Expand Down
10 changes: 4 additions & 6 deletions src/public_parameters/disk_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ where
) -> Result<PublicParams<F, M>, Error> {
let file = instance.open(&self.dir)?;
let reader = BufReader::new(file);
bincode::deserialize_from(reader).map_err(|e| {
Error::CacheError(format!("Public param cache deserialization error: {}", e))
})
bincode::deserialize_from(reader)
.map_err(|e| Error::Cache(format!("Public param cache deserialization error: {}", e)))
}

pub(crate) fn read_bytes(
Expand All @@ -75,9 +74,8 @@ where
) -> Result<(), Error> {
let file = instance.create(&self.dir)?;
let writer = BufWriter::new(&file);
bincode::serialize_into(writer, &data).map_err(|e| {
Error::CacheError(format!("Public param cache serialization error: {}", e))
})
bincode::serialize_into(writer, &data)
.map_err(|e| Error::Cache(format!("Public param cache serialization error: {}", e)))
}

pub(crate) fn write_abomonated<V: Abomonation>(
Expand Down
6 changes: 3 additions & 3 deletions src/public_parameters/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use thiserror::Error;
#[derive(Error, Debug)]
pub enum Error {
#[error("IO error: {0}")]
IOError(#[from] io::Error),
IO(#[from] io::Error),
#[error("Cache error: {0}")]
CacheError(String),
Cache(String),
#[error("JSON error: {0}")]
JsonError(#[from] serde_json::Error),
Json(#[from] serde_json::Error),
}
6 changes: 3 additions & 3 deletions src/public_parameters/mem_cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ impl PublicParamMemCache {
assert!(rest.is_empty());
Ok(Arc::new(pp.clone())) // this clone is VERY expensive
}
Err(Error::IOError(e)) => {
Err(Error::IO(e)) => {
warn!("{e}");
info!("Generating fresh public parameters");
let pp = default(instance);
Expand All @@ -77,7 +77,7 @@ impl PublicParamMemCache {
.tap_ok(|_| {
info!("writing public params to disk-cache: {}", instance.key())
})
.map_err(|e| Error::CacheError(format!("Disk write error: {e}")))?;
.map_err(|e| Error::Cache(format!("Disk write error: {e}")))?;
Ok(pp)
}
_ => unreachable!(),
Expand All @@ -92,7 +92,7 @@ impl PublicParamMemCache {
disk_cache
.write(instance, &*pp)
.tap_ok(|_| info!("writing public params to disk-cache: {}", instance.key()))
.map_err(|e| Error::CacheError(format!("Disk write error: {e}")))?;
.map_err(|e| Error::Cache(format!("Disk write error: {e}")))?;
Ok(pp)
}
}
Expand Down
Loading

1 comment on commit 9781652

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Benchmarks

Table of Contents

Overview

This benchmark report shows the Fibonacci GPU benchmark.
NVIDIA L4
Intel(R) Xeon(R) CPU @ 2.20GHz
125.78 GB RAM

Benchmark Results

LEM Fibonacci Prove - rc = 100

fib-ref=8e9e8b56cbfb9ff086481f820eda33062a5473fc fib-ref=97816523d371179170a829992f8862123f91e1db
num-100 4.79 s (✅ 1.00x) 4.82 s (✅ 1.01x slower)
num-200 9.83 s (✅ 1.00x) 9.73 s (✅ 1.01x faster)

LEM Fibonacci Prove - rc = 600

fib-ref=8e9e8b56cbfb9ff086481f820eda33062a5473fc fib-ref=97816523d371179170a829992f8862123f91e1db
num-100 3.99 s (✅ 1.00x) 3.98 s (✅ 1.00x faster)
num-200 9.05 s (✅ 1.00x) 9.06 s (✅ 1.00x slower)

Made with criterion-table

Please sign in to comment.