From c883df747fd934da04c67ba8726b128da6100da5 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Vella Date: Tue, 16 Apr 2024 13:43:05 +0100 Subject: [PATCH 1/3] Loading dependencies from an env var. --- README.md | 16 ++++++++++++++++ src/lib.rs | 24 ++++++++++++++++++++---- 2 files changed, 36 insertions(+), 4 deletions(-) create mode 100644 README.md diff --git a/README.md b/README.md new file mode 100644 index 0000000..690e326 --- /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 the 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. + +Cargo's `OUT_DIR` will typically be +`target//build/pil-stark-prover-/out`. If the original directory +is not found at runtime, the library will search for its contents in the path +pointed by environment variable `PIL_STARK_PROVER_DEPS`. 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"); From 764fb422fd086a7a07cd4a05aa84522718a91b21 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Vella Date: Tue, 16 Apr 2024 16:15:09 +0100 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Leo --- README.md | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 690e326..e001ce1 100644 --- a/README.md +++ b/README.md @@ -1,10 +1,10 @@ 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. +proofs from a Rust friendly interface. -At runtime, it needs to find the contents of the cargo's `OUT_DIR` (which is -populated by `build.rs` at build time) otherwise execution will panic. This +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 From a6ce729deb1aa81ad068b6a3321ed3deb59ede41 Mon Sep 17 00:00:00 2001 From: Lucas Clemente Vella Date: Tue, 16 Apr 2024 16:41:20 +0100 Subject: [PATCH 3/3] More precise description of the dependency search mechanism. --- README.md | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index e001ce1..9064a34 100644 --- a/README.md +++ b/README.md @@ -10,7 +10,7 @@ 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. -Cargo's `OUT_DIR` will typically be -`target//build/pil-stark-prover-/out`. If the original directory -is not found at runtime, the library will search for its contents in the path -pointed by environment variable `PIL_STARK_PROVER_DEPS`. +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`.