diff --git a/README.md b/README.md new file mode 100644 index 0000000..9064a34 --- /dev/null +++ b/README.md @@ -0,0 +1,16 @@ +This crate uses the external submodules +[pil-stark](git@github.com:powdr-labs/pil-stark.git) and +[zkevm-prover](https://github.com/powdr-labs/zkevm-prover) to generate EStark ZK +proofs from a Rust friendly interface. + +At runtime, it needs to find the contents of cargo's `OUT_DIR` (which is +populated by `build.rs` at build time), otherwise execution will panic. This +means that, if used as a cargo dependency from the same machine it is built, +e.g. with `cargo run` or `cargo test`, it will work out of the box. But if the +binaries are executed from another machine, e.g. from a `nextest` archive, it +will fail unless the original `OUT_DIR` contents are manually provided. + +The library will first search for the dependencies in the path given by +environment variable `PIL_STARK_PROVER_DEPS`, and if not set, it fallbacks to +the compile-time hardcoded path set by cargo's `OUT_DIR`, which is typically +`target//build/pil-stark-prover-/out`. diff --git a/src/lib.rs b/src/lib.rs index 8a69056..35d61b7 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ use std::{ - fs, + borrow::Cow, + env, fs, io::{BufRead, BufReader}, path::{Path, PathBuf}, process::{Command, ExitStatus, Stdio}, @@ -14,7 +15,22 @@ pub struct OutputFiles { } const MAX_NODE_MEM: u32 = 1024 * 16; -const EXTERNALS_DIR: &str = env!("OUT_DIR"); + +fn deps_dir() -> Cow<'static, Path> { + const DEFAULT_DEPS_DIR: &str = env!("OUT_DIR"); + + match env::var_os("PIL_STARK_PROVER_DEPS") { + Some(deps_dir) => Cow::Owned(deps_dir.into()), + None => { + let default_deps_dir = Path::new(DEFAULT_DEPS_DIR); + if default_deps_dir.is_dir() { + Cow::Borrowed(default_deps_dir) + } else { + panic!("pil-stark-prover dependencies directory not found!\nEither set PIL_STARK_PROVER_DEPS environment variable, or build the project locally from source.\nSee README.md for more information.") + } + } + } +} #[derive(thiserror::Error, Debug)] pub enum Error { @@ -72,7 +88,7 @@ pub fn generate_proof( commits_bin: &Path, output_dir: &Path, ) -> Result { - let externals_dir = Path::new(EXTERNALS_DIR); + let externals_dir = deps_dir(); let pil_stark_root = externals_dir.join("pil-stark"); let pil_stark_src = pil_stark_root.join("src"); @@ -217,7 +233,7 @@ pub fn verify_proof( proof_json: &Path, publics_json: &Path, ) -> Result<(), Error> { - let externals_dir = Path::new(EXTERNALS_DIR); + let externals_dir = deps_dir(); let pil_stark_root = externals_dir.join("pil-stark"); let pil_stark_src = pil_stark_root.join("src");