Skip to content

Commit 1a4701e

Browse files
committed
feat: auto-download .wasm and .zkey while building
1 parent 7d341d4 commit 1a4701e

9 files changed

+1365
-30
lines changed

Cargo.lock

Lines changed: 1303 additions & 30 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ ruint = "1.14.0"
1212
num-traits = "0.2.19"
1313
ethers-core = "2.0.14"
1414
anyhow = "1.0.97"
15+
reqwest = { version = "0.12.15", features = ["blocking", "json"] }
16+
tokio = { version = "1", features = ["full"] }
1517

1618
# arkworks
1719
ark-ec = { version = "=0.5.0", default-features = false }
@@ -40,3 +42,4 @@ serde = [
4042

4143
[build-dependencies]
4244
rust-witness = "0.1"
45+
reqwest = { version = "0.12.15", features = ["blocking", "json"] }

build.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,62 @@
1+
use reqwest::blocking::Client;
2+
use std::error::Error;
3+
use std::fs::{File, create_dir_all};
4+
use std::io::copy;
5+
use std::path::{Path, PathBuf};
6+
7+
/// download semaphore artifacts by required tree depth
8+
fn download_semaphore_artifacts(depth: usize) -> Result<(), Box<dyn Error>> {
9+
let base_url = "https://snark-artifacts.pse.dev/semaphore/latest/";
10+
let remote_filenames = [
11+
format!("semaphore-{}.wasm", depth),
12+
format!("semaphore-{}.zkey", depth),
13+
];
14+
let local_filenames = ["semaphore.wasm", "semaphore.zkey"];
15+
16+
let client = Client::new();
17+
let target_dir = Path::new("./zkey");
18+
19+
// Verify if those files have been downloaded or not. Skip downloading if yes.
20+
let version_path = target_dir.join("depth");
21+
if version_path.exists() {
22+
let current_version = std::fs::read_to_string(&version_path)?.trim().to_string();
23+
if current_version == depth.to_string() {
24+
println!(
25+
"Artifacts for depth {} already downloaded, skipping.",
26+
depth
27+
);
28+
return Ok(());
29+
}
30+
}
31+
// create ./zkey folder
32+
create_dir_all(&target_dir)?;
33+
34+
// download artifacts
35+
for (remote, local) in remote_filenames.iter().zip(local_filenames.iter()) {
36+
let url = format!("{}{}", base_url, remote);
37+
let dest_path: PathBuf = target_dir.join(local);
38+
39+
eprintln!("Downloading {}...", url);
40+
let mut resp = client.get(&url).send()?.error_for_status()?;
41+
let mut out = File::create(&dest_path)?;
42+
copy(&mut resp, &mut out)?;
43+
eprintln!("Saved as {}", dest_path.display());
44+
}
45+
46+
// update depth info
47+
std::fs::write(&version_path, depth.to_string())?;
48+
49+
Ok(())
50+
}
51+
152
fn main() {
53+
// Default depth is 10 for testing purposes; can be overridden via SEMAPHORE_DEPTH environment variable
54+
let depth: usize = std::env::var("SEMAPHORE_DEPTH")
55+
.unwrap_or_else(|_| "10".to_string())
56+
.parse()
57+
.expect("SEMAPHORE_DEPTH must be a valid usize");
58+
59+
download_semaphore_artifacts(depth).expect("Failed to download artifacts");
60+
261
rust_witness::transpile::transpile_wasm("./zkey".to_string());
362
}

test-vector/semaphore-1.wasm

-1.71 MB
Binary file not shown.

test-vector/semaphore-1.zkey

-1.13 MB
Binary file not shown.

test-vector/semaphore-10.wasm

-1.72 MB
Binary file not shown.

test-vector/semaphore-10.zkey

-2.28 MB
Binary file not shown.

zkey/semaphore.wasm

-1.72 MB
Binary file not shown.

zkey/semaphore.zkey

-2.28 MB
Binary file not shown.

0 commit comments

Comments
 (0)