Skip to content

Commit

Permalink
Circuit cache dir now uses os cache dir (#375)
Browse files Browse the repository at this point in the history
- Now attempts to use the OS's specific cache dir to store circuits.
- This is done by setting the env var `ZK_EVM_CACHE_DIR` if the user has
  not already set it, which allows the user to override the circuit
  cache directory if they want to.
  • Loading branch information
BGluth committed Aug 9, 2024
1 parent cf222f0 commit 09160c1
Show file tree
Hide file tree
Showing 4 changed files with 90 additions and 16 deletions.
50 changes: 50 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions zero_bin/common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ keywords.workspace = true
categories.workspace = true

[dependencies]
directories = "5.0.1"

thiserror = { workspace = true }
tracing = { workspace = true }
proof_gen = { workspace = true }
Expand All @@ -24,6 +26,7 @@ alloy = { workspace = true }
async-stream = { workspace = true }
cargo_metadata = { workspace = true }
vergen = { workspace = true }
once_cell = { workspace = true }

[build-dependencies]
cargo_metadata = { workspace = true }
Expand Down
2 changes: 2 additions & 0 deletions zero_bin/common/src/prover_state/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use clap::ValueEnum;
use evm_arithmetization::{
proof::AllProof, prover::prove, AllStark, GenerationInputs, StarkConfig,
};
use persistence::set_circuit_cache_dir_env_if_not_set;
use plonky2::{
field::goldilocks_field::GoldilocksField, plonk::config::PoseidonGoldilocksConfig,
util::timing::TimingTree,
Expand Down Expand Up @@ -242,6 +243,7 @@ impl ProverStateManager {
/// Initialize global prover state from the configuration.
pub fn initialize(&self) -> anyhow::Result<()> {
info!("initializing prover state...");
set_circuit_cache_dir_env_if_not_set()?;

let state = match self.persistence {
CircuitPersistence::None => {
Expand Down
51 changes: 35 additions & 16 deletions zero_bin/common/src/prover_state/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ use std::{
path::Path,
};

use directories::ProjectDirs;
use once_cell::sync::Lazy;
use plonky2::util::serialization::{
Buffer, DefaultGateSerializer, DefaultGeneratorSerializer, IoError,
};
Expand All @@ -17,10 +19,20 @@ use super::{
Config, RecursiveCircuitsForTableSize, SIZE,
};

const CIRCUITS_DIR: &str = "circuits/";
const PROVER_STATE_FILE_PREFIX: &str = "prover_state";
const VERIFIER_STATE_FILE_PREFIX: &str = "verifier_state";
const CARGO_WORKSPACE_DIR_ENV: &str = "CARGO_WORKSPACE_DIR";
const ZK_EVM_CACHE_DIR_NAME: &str = "zk_evm_circuit_cache";
const ZK_EVM_CACHE_DIR_ENV: &str = "ZK_EVM_CACHE_DIR";

static CIRCUIT_CACHE_DIR: Lazy<String> = Lazy::new(|| {
// Guaranteed to be set by the binary if not set by the user.
std::env::var(ZK_EVM_CACHE_DIR_ENV).unwrap_or_else(|_| {
format!(
"expected the env var \"{}\" to be set",
ZK_EVM_CACHE_DIR_ENV
)
})
});

fn get_serializers() -> (
DefaultGateSerializer,
Expand Down Expand Up @@ -73,11 +85,11 @@ pub(crate) trait DiskResource {
p: &Self::PathConstrutor,
r: &Self::Resource,
) -> Result<(), DiskResourceError<Self::Error>> {
let circuits_dir = relative_circuit_dir_path();
let circuits_dir = &*CIRCUIT_CACHE_DIR;

// Create the base folder if non-existent.
if std::fs::metadata(&circuits_dir).is_err() {
std::fs::create_dir(&circuits_dir).map_err(|_| {
if std::fs::metadata(circuits_dir).is_err() {
std::fs::create_dir(circuits_dir).map_err(|_| {
DiskResourceError::IoError::<Self::Error>(std::io::Error::other(
"Could not create circuits folder",
))
Expand Down Expand Up @@ -107,7 +119,7 @@ impl DiskResource for BaseProverResource {
fn path(p: &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_base_{}_{}",
&relative_circuit_dir_path(),
*CIRCUIT_CACHE_DIR,
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
p.get_configuration_digest()
Expand Down Expand Up @@ -143,7 +155,7 @@ impl DiskResource for MonolithicProverResource {
fn path(p: &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_monolithic_{}_{}",
&relative_circuit_dir_path(),
*CIRCUIT_CACHE_DIR,
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
p.get_configuration_digest()
Expand Down Expand Up @@ -178,7 +190,7 @@ impl DiskResource for RecursiveCircuitResource {
fn path((circuit_type, size): &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_{}_{}_{}",
&relative_circuit_dir_path(),
*CIRCUIT_CACHE_DIR,
PROVER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
circuit_type.as_short_str(),
Expand Down Expand Up @@ -222,7 +234,7 @@ impl DiskResource for VerifierResource {
fn path(p: &Self::PathConstrutor) -> impl AsRef<Path> {
format!(
"{}/{}_{}_{}",
&relative_circuit_dir_path(),
*CIRCUIT_CACHE_DIR,
VERIFIER_STATE_FILE_PREFIX,
env!("EVM_ARITHMETIZATION_PKG_VER"),
p.get_configuration_digest()
Expand Down Expand Up @@ -277,11 +289,18 @@ fn prover_to_disk(
Ok(())
}

/// If we're running in the cargo workspace, then always use the `circuits`
/// directory that lives in `tools/`. Otherwise, just use `circuits` in the
/// current directory.
fn relative_circuit_dir_path() -> String {
env::var(CARGO_WORKSPACE_DIR_ENV)
.map(|p| format!("{}/{}", p, CIRCUITS_DIR))
.unwrap_or_else(|_| CIRCUITS_DIR.to_string())
/// We store serialized circuits inside the cache directory specified by an env
/// variable. If the user does not set this, then we set it base to the OS's
/// standard location for the cache directory.
pub(crate) fn set_circuit_cache_dir_env_if_not_set() -> anyhow::Result<()> {
if std::env::var_os(ZK_EVM_CACHE_DIR_ENV).is_none() {
let circuit_cache_dir = match ProjectDirs::from("", "", ZK_EVM_CACHE_DIR_NAME) {
Some(proj_dir) => proj_dir.cache_dir().to_path_buf(),
None => std::env::current_dir()?,
};

std::env::set_var(ZK_EVM_CACHE_DIR_ENV, circuit_cache_dir);
}

Ok(())
}

0 comments on commit 09160c1

Please sign in to comment.