|
| 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 | + |
1 | 52 | 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 | + |
2 | 61 | rust_witness::transpile::transpile_wasm("./zkey".to_string()); |
3 | 62 | } |
0 commit comments