Skip to content

Commit

Permalink
Loading dependencies from an env var.
Browse files Browse the repository at this point in the history
  • Loading branch information
lvella committed Apr 16, 2024
1 parent 2e7f405 commit 684cb2b
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 4 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
This crate uses the external submodules
[pil-stark]([email protected]: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/<profile>/build/pil-stark-prover-<hash>/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`.
22 changes: 18 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::{
fs,
borrow::Cow,
env, fs,
io::{BufRead, BufReader},
path::{Path, PathBuf},
process::{Command, ExitStatus, Stdio},
Expand All @@ -14,7 +15,20 @@ 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");

let default_deps_dir = Path::new(DEFAULT_DEPS_DIR);
if default_deps_dir.is_dir() {
Cow::Borrowed(default_deps_dir)
} else {
Cow::Owned(env::var("PIL_STARK_PROVER_DEPS").map_or_else(|err| match err {
env::VarError::NotPresent => 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."),
env::VarError::NotUnicode(os_str) => os_str.into(),
}, PathBuf::from))
}
}

#[derive(thiserror::Error, Debug)]
pub enum Error {
Expand Down Expand Up @@ -72,7 +86,7 @@ pub fn generate_proof(
commits_bin: &Path,
output_dir: &Path,
) -> Result<OutputFiles, 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");

Expand Down Expand Up @@ -217,7 +231,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");

Expand Down

0 comments on commit 684cb2b

Please sign in to comment.