Skip to content

Commit

Permalink
Merge pull request #6 from powdr-labs/runtime-env-var
Browse files Browse the repository at this point in the history
Loading dependencies from an env var.
  • Loading branch information
lvella committed Apr 16, 2024
2 parents 2e7f405 + a6ce729 commit d119f0b
Show file tree
Hide file tree
Showing 2 changed files with 36 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](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/<profile>/build/pil-stark-prover-<hash>/out`.
24 changes: 20 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,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 {
Expand Down Expand Up @@ -72,7 +88,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 +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");

Expand Down

0 comments on commit d119f0b

Please sign in to comment.