Skip to content

Commit

Permalink
Save zkey and wasm in OUT_DIR
Browse files Browse the repository at this point in the history
  • Loading branch information
Dzejkop committed Apr 12, 2024
1 parent 95fb24f commit 8ef0b3a
Showing 1 changed file with 25 additions and 87 deletions.
112 changes: 25 additions & 87 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,73 +1,54 @@
use color_eyre::eyre::Result;
use std::{
fs::{create_dir, File},
path::{Component, Path, PathBuf},
fs::{create_dir, create_dir_all, File},
path::Path,
};

use color_eyre::eyre::Result;

extern crate reqwest;

const SEMAPHORE_FILES_PATH: &str = "semaphore_files";
const SEMAPHORE_DOWNLOAD_URL: &str = "https://www.trusted-setup-pse.org/semaphore";

// See <https://internals.rust-lang.org/t/path-to-lexical-absolute/14940>
fn absolute(path: PathBuf) -> Result<PathBuf> {
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 download_and_store_binary(url: &str, path: impl AsRef<Path>) -> Result<()> {
let path = path.as_ref();

fn download_and_store_binary(url: &str, path: &Path) -> Result<()> {
let mut resp =
reqwest::blocking::get(url).unwrap_or_else(|_| panic!("Failed to download file: {url}"));
let path_str = path.to_str().unwrap();
let mut file =
File::create(path).unwrap_or_else(|_| panic!("Failed to create file: {path_str}"));
File::create(path).unwrap_or_else(|_| panic!("Failed to create file: {}", path.display()));

resp.copy_to(&mut file)?;
Ok(())
}

fn semaphore_file_path(file_name: &str, depth: usize) -> PathBuf {
Path::new(SEMAPHORE_FILES_PATH)
.join(depth.to_string())
.join(file_name)
}

fn build_circuit(depth: usize) -> Result<()> {
let base_path = Path::new(SEMAPHORE_FILES_PATH);
let out_dir = std::env::var("OUT_DIR").expect("Missing out dir var");
let base_path = Path::new(&out_dir).join(SEMAPHORE_FILES_PATH);

if !base_path.exists() {
create_dir(base_path)?;
create_dir_all(&base_path)?;
}

let depth_str = &depth.to_string();
let extensions = ["wasm", "zkey"];
let depth_str = depth.to_string();

let depth_subfolder = base_path.join(depth_str);
let depth_subfolder = base_path.join(&depth_str);
if !Path::new(&depth_subfolder).exists() {
create_dir(&depth_subfolder)?;
}

for extension in extensions {
let filename = "semaphore";
let download_url = format!("{SEMAPHORE_DOWNLOAD_URL}/{depth_str}/{filename}.{extension}");
let path = Path::new(&depth_subfolder).join(format!("{filename}.{extension}"));
download_and_store_binary(&download_url, &path)?;
}
let depth_subfolder = depth_subfolder.display();
download_and_store_binary(
&format!("{SEMAPHORE_DOWNLOAD_URL}/{depth_str}/semaphore.zkey"),
&format!("{depth_subfolder}/semaphore.zkey"),

Check warning on line 43 in build.rs

View workflow job for this annotation

GitHub Actions / clippy

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> build.rs:43:9 | 43 | &format!("{depth_subfolder}/semaphore.zkey"), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `format!("{depth_subfolder}/semaphore.zkey")` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args = note: `#[warn(clippy::needless_borrows_for_generic_args)]` on by default
)?;
download_and_store_binary(
&format!("{SEMAPHORE_DOWNLOAD_URL}/{depth_str}/semaphore.wasm"),
&format!("{depth_subfolder}/semaphore.wasm"),

Check warning on line 47 in build.rs

View workflow job for this annotation

GitHub Actions / clippy

the borrowed expression implements the required traits

warning: the borrowed expression implements the required traits --> build.rs:47:9 | 47 | &format!("{depth_subfolder}/semaphore.wasm"), | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ help: change this to: `format!("{depth_subfolder}/semaphore.wasm")` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrows_for_generic_args
)?;

// Compute absolute paths
let zkey_file = absolute(semaphore_file_path("semaphore.zkey", depth))?;
let wasm_file = absolute(semaphore_file_path("semaphore.wasm", depth))?;
let zkey_file = base_path.join(&depth_str).join(format!("semaphore.zkey"));

Check warning on line 50 in build.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

warning: useless use of `format!` --> build.rs:50:53 | 50 | let zkey_file = base_path.join(&depth_str).join(format!("semaphore.zkey")); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"semaphore.zkey".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format = note: `#[warn(clippy::useless_format)]` on by default
let wasm_file = base_path.join(&depth_str).join(format!("semaphore.wasm"));

Check warning on line 51 in build.rs

View workflow job for this annotation

GitHub Actions / clippy

useless use of `format!`

warning: useless use of `format!` --> build.rs:51:53 | 51 | let wasm_file = base_path.join(&depth_str).join(format!("semaphore.wasm")); | ^^^^^^^^^^^^^^^^^^^^^^^^^ help: consider using `.to_string()`: `"semaphore.wasm".to_string()` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#useless_format

assert!(zkey_file.exists());
assert!(wasm_file.exists());
Expand All @@ -87,52 +68,9 @@ fn build_circuit(depth: usize) -> Result<()> {
Ok(())
}

#[cfg(feature = "dylib")]
fn build_dylib(depth: usize) -> Result<()> {
use color_eyre::eyre::eyre;
use enumset::{enum_set, EnumSet};
use std::{env, str::FromStr};
use wasmer::{Module, Store, Target, Triple};
use wasmer_compiler_cranelift::Cranelift;
use wasmer_engine_dylib::Dylib;

let wasm_file = absolute(semaphore_file_path("semaphore.wasm", depth))?;
assert!(wasm_file.exists());

let out_dir = env::var("OUT_DIR")?;
let out_dir = Path::new(&out_dir).to_path_buf();
let dylib_file = out_dir.join(format!("semaphore_{depth}.dylib"));
println!(
"cargo:rustc-env=CIRCUIT_WASM_DYLIB_{}={}",
depth,
dylib_file.display()
);

if dylib_file.exists() {
return Ok(());
}

// Create a WASM engine for the target that can compile
let triple = Triple::from_str(&env::var("TARGET")?).map_err(|e| eyre!(e))?;
let cpu_features = enum_set!();
let target = Target::new(triple, cpu_features);
let engine = Dylib::new(Cranelift::default()).target(target).engine();

// Compile the WASM module
let store = Store::new(&engine);
let module = Module::from_file(&store, &wasm_file)?;
module.serialize_to_file(&dylib_file)?;
assert!(dylib_file.exists());
println!("cargo:warning=Circuit dylib is in {}", dylib_file.display());

Ok(())
}

fn main() -> Result<()> {
for depth in semaphore_depth_config::get_supported_depths() {
build_circuit(*depth)?;
#[cfg(feature = "dylib")]
build_dylib(*depth)?;
}
Ok(())
}

0 comments on commit 8ef0b3a

Please sign in to comment.