diff --git a/Cargo.toml b/Cargo.toml index f869ff1..cbe0140 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -64,6 +64,9 @@ tempfile = "3.0" tiny-keccak = "2.0.2" tracing-test = "0.2" +[build-dependencies] +wasmer = { version = "2.0", features = [ "dylib" ] } +color-eyre = "0.5" wasmer-engine-dylib = "2.2.1" wasmer-compiler-cranelift = "2.2.1" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..70a7d8e --- /dev/null +++ b/build.rs @@ -0,0 +1,67 @@ +// TODO: Use ExitCode::exit_ok() when stable. + +use std::{process::Command, fs::canonicalize, path::{Path, PathBuf, Component}}; +use color_eyre::eyre::{Result, eyre}; + +const ZKEY_FILE: &str = "./semaphore/build/snark/semaphore_final.zkey"; +const WASM_FILE: &str = "./semaphore/build/snark/semaphore.wasm"; +const DYLIB_FILE: &str = "./semaphore/build/snark/semaphore.dylib"; + +// See +fn absolute(path: &str) -> Result { + let path = Path::new(path); + let mut absolute = if path.is_absolute() { + PathBuf::new() + } else { + std::env::current_dir()? + }; + for component in path.components() { + match component { + Component::CurDir => {}, + Component::ParentDir => { absolute.pop(); }, + component @ _ => absolute.push(component.as_os_str()), + } + } + Ok(absolute) +} + +fn build_circuit() -> Result<()> { + println!("cargo:rerun-if-changed=./semaphore"); + let run = |cmd: &[&str]| -> Result<()> { + Command::new(cmd[0]) + .args(cmd[1..].iter()) + .current_dir("./semaphore") + .status()? + .success() + .then(|| ()) + .ok_or(eyre!("procees returned failure"))?; + Ok(()) + }; + + // Compute absolute paths + let zkey_file = absolute(ZKEY_FILE)?; + let wasm_file = absolute(WASM_FILE)?; + + // Build circuits if not exists + // TODO: This does not rebuild if the semaphore submodule is changed. + // NOTE: This requires npm / nodejs to be installed. + if !(zkey_file.exists() && wasm_file.exists()) { + run(&["npm", "install"])?; + run(&["npm", "exec", "ts-node", "./scripts/compile-circuits.ts"])?; + } + assert!(zkey_file.exists()); + assert!(wasm_file.exists()); + + // Export generated paths + println!("cargo:rustc-env=ZKEY_FILE={}", zkey_file.display()); + println!("cargo:rustc-env=WASM_FILE={}", wasm_file.display()); + + Ok(()) +} + +fn main() -> Result<()> { + build_circuit()?; + + + Ok(()) +}