Skip to content

Commit

Permalink
bump all ark dependencies to 4.0 and adjust the code
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMenko committed Nov 30, 2023
1 parent 420aacc commit 68f290a
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 137 deletions.
17 changes: 10 additions & 7 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,16 @@ path = "criterion.rs"
required-features = ["bench", "proptest"]

[dependencies]
ark-bn254 = { version = "0.3.0" }
ark-circom = { git = "https://github.com/gakonst/ark-circom", rev = "a93c8b0", features = ["circom-2"] }
ark-ec = { version = "0.3.0", default-features = false, features = ["parallel"] }
ark-ff = { version = "0.3.0", default-features = false, features = ["parallel", "asm"] }
ark-groth16 = { git = "https://github.com/arkworks-rs/groth16", rev = "765817f", features = ["parallel"] }
ark-relations = { version = "0.3.0", default-features = false }
ark-std = { version = "0.3.0", default-features = false, features = ["parallel"] }
ark-bn254 = { version = "=0.4.0" }
ark-circom = { git = "https://github.com/gakonst/ark-circom", features = ["circom-2"] }
ark-ec = { version = "0.4.1", default-features = false, features = ["parallel"] }
ark-ff = { version = "0.4.1", default-features = false, features = ["parallel", "asm"] }
ark-groth16 = { version = "=0.4.0", features = ["parallel"] }
ark-relations = { version = "=0.4.0", default-features = false }
ark-std = { version = "0.4.0", default-features = false, features = ["parallel"] }
# TODO: Need to give mopro a example zkey for satisfying its tests
# for now i just commented them out
# ark-zkey = { git = "https://github.com/worldcoin/mopro" }
ark-zkey = { path = "crates/ark-zkey" }
color-eyre = "0.6"
criterion = { version = "0.3", optional = true, features = ["async_tokio"] }
Expand Down
7 changes: 4 additions & 3 deletions build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ fn semaphore_file_path(file_name: &str, depth: usize) -> PathBuf {
}

fn create_arkzkey(path: PathBuf) -> Result<PathBuf> {
let ark_zkey_path = path.join("-arkzkey");
let mut ark_zkey_path = path.clone();
ark_zkey_path.set_extension("arkzkey");

let (original_proving_key, original_constraint_matrices) =
ark_zkey::read_proving_key_and_matrices_from_zkey(
Expand All @@ -57,7 +58,7 @@ fn create_arkzkey(path: PathBuf) -> Result<PathBuf> {
ark_zkey::convert_zkey(
original_proving_key,
original_constraint_matrices,
&ark_zkey_path.to_str().unwrap(),
ark_zkey_path.to_str().unwrap(),
)?;

Ok(ark_zkey_path)
Expand Down Expand Up @@ -87,7 +88,7 @@ fn build_circuit(depth: usize) -> Result<()> {

// Compute absolute paths
let zkey_file = absolute(semaphore_file_path("semaphore.zkey", depth))?;
let arkzkey_file = absolute(semaphore_file_path("semaphore.zkey-arkzkey", depth))?;
let arkzkey_file = absolute(semaphore_file_path("semaphore.arkzkey", depth))?;
let graph_file = absolute(
Path::new("graphs")
.join(depth.to_string())
Expand Down
203 changes: 96 additions & 107 deletions crates/ark-zkey/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub fn deserialize_proving_key(data: Vec<u8>) -> SerializableProvingKey {
}

// takes bytes from .zkey files
const ZKEY_DATA: &[u8] = &[0u8];
const ZKEY_DATA: &[u8] = &[];

pub fn read_proving_key_and_matrices(
) -> Result<(SerializableProvingKey, SerializableConstraintMatrices<Fr>)> {
Expand Down Expand Up @@ -220,110 +220,99 @@ pub fn convert_zkey(
Ok(())
}

// fn read_zkey_with_mmap(zkey_path: &str) -> Result<(ProvingKey<Bn254>, ConstraintMatrices<Fr>)> {
// let file = File::open(zkey_path)?;

// let mmap = unsafe { Mmap::map(&file)? };

// let cursor = Cursor::new(&mmap);
// let (proving_key, matrices) = read_zkey(&mut cursor.clone())?;

// Ok((proving_key, matrices))
// #[cfg(test)]
// mod tests {
// use super::*;
// use std::time::Instant;

// fn circuit_naive_read() -> Result<()> {
// println!("Reading zkey...");
// let now = Instant::now();

// let mut cursor = Cursor::new(ZKEY_DATA);

// let (_proving_key, _matrices) =
// read_zkey(&mut cursor).wrap_err("Failed to process zkey data")?;
// println!("Time to read zkey data: {:?}", now.elapsed());

// Ok(())
// }

// fn test_circuit_serialization_deserialization(dir: &str, circuit: &str) -> Result<()> {
// let _zkey_path = format!("{}/target/{}_final.zkey", dir, circuit);
// let arkzkey_path = format!("{}/target/{}_final.arkzkey", dir, circuit);

// let (original_proving_key, original_constraint_matrices) = read_proving_key_and_matrices()?;

// println!("[build] Writing arkzkey to: {}", arkzkey_path);
// let now = Instant::now();
// convert_zkey(
// original_proving_key.clone(),
// original_constraint_matrices.clone(),
// &arkzkey_path,
// )?;
// println!("[build] Time to write arkzkey: {:?}", now.elapsed());

// println!("Reading arkzkey from: {}", arkzkey_path);
// let now = Instant::now();
// let (deserialized_proving_key, deserialized_constraint_matrices) =
// read_arkzkey(&arkzkey_path)?;
// println!("Time to read arkzkey: {:?}", now.elapsed());

// assert_eq!(
// original_proving_key, deserialized_proving_key,
// "Original and deserialized proving keys do not match"
// );

// assert_eq!(
// original_constraint_matrices, deserialized_constraint_matrices,
// "Original and deserialized constraint matrices do not match"
// );

// //flame::dump_html(&mut std::fs::File::create("flame-graph.html").unwrap()).unwrap();

// Ok(())
// }

// #[test]
// fn test_multiplier2_serialization_deserialization() -> Result<()> {
// test_circuit_serialization_deserialization(
// "../mopro-core/examples/circom/multiplier2",
// "multiplier2",
// )
// }

// #[test]
// fn test_keccak256_serialization_deserialization() -> Result<()> {
// test_circuit_serialization_deserialization(
// "../mopro-core/examples/circom/keccak256",
// "keccak256_256_test",
// )
// }

// #[test]
// fn test_rsa_serialization_deserialization() -> Result<()> {
// test_circuit_serialization_deserialization("../mopro-core/examples/circom/rsa", "main")
// }

// // XXX: We do include_bytes for zkey data, so need to manually change this
// #[test]
// fn test_circuit_naive_read() -> Result<()> {
// circuit_naive_read()
// }

// // #[test]
// // fn test_read_arkzkey_from_bytes() -> Result<()> {
// // const ARKZKEY_BYTES: &[u8] = include_bytes!(
// // "../../mopro-core/examples/circom/keccak256/target/keccak256_256_test_final.arkzkey"
// // );

// // println!("Reading arkzkey from bytes (keccak)");
// // let now = Instant::now();
// // let (_deserialized_proving_key, _deserialized_constraint_matrices) =
// // read_arkzkey_from_bytes(ARKZKEY_BYTES)?;
// // println!("Time to read arkzkey: {:?}", now.elapsed());

// // Ok(())
// // }
// }

#[cfg(test)]
mod tests {
use super::*;
use std::time::Instant;

fn circuit_naive_read() -> Result<()> {
println!("Reading zkey...");
let now = Instant::now();

let mut cursor = Cursor::new(ZKEY_DATA);

let (_proving_key, _matrices) =
read_zkey(&mut cursor).wrap_err("Failed to process zkey data")?;
println!("Time to read zkey data: {:?}", now.elapsed());

Ok(())
}

fn test_circuit_serialization_deserialization(dir: &str, circuit: &str) -> Result<()> {
let _zkey_path = format!("{}/target/{}_final.zkey", dir, circuit);
let arkzkey_path = format!("{}/target/{}_final.arkzkey", dir, circuit);

let (original_proving_key, original_constraint_matrices) = read_proving_key_and_matrices()?;

println!("[build] Writing arkzkey to: {}", arkzkey_path);
let now = Instant::now();
convert_zkey(
original_proving_key.clone(),
original_constraint_matrices.clone(),
&arkzkey_path,
)?;
println!("[build] Time to write arkzkey: {:?}", now.elapsed());

println!("Reading arkzkey from: {}", arkzkey_path);
let now = Instant::now();
let (deserialized_proving_key, deserialized_constraint_matrices) =
read_arkzkey(&arkzkey_path)?;
println!("Time to read arkzkey: {:?}", now.elapsed());

assert_eq!(
original_proving_key, deserialized_proving_key,
"Original and deserialized proving keys do not match"
);

assert_eq!(
original_constraint_matrices, deserialized_constraint_matrices,
"Original and deserialized constraint matrices do not match"
);

//flame::dump_html(&mut std::fs::File::create("flame-graph.html").unwrap()).unwrap();

Ok(())
}

#[test]
fn test_multiplier2_serialization_deserialization() -> Result<()> {
test_circuit_serialization_deserialization(
"../mopro-core/examples/circom/multiplier2",
"multiplier2",
)
}

#[test]
fn test_keccak256_serialization_deserialization() -> Result<()> {
test_circuit_serialization_deserialization(
"../mopro-core/examples/circom/keccak256",
"keccak256_256_test",
)
}

#[test]
fn test_rsa_serialization_deserialization() -> Result<()> {
test_circuit_serialization_deserialization("../mopro-core/examples/circom/rsa", "main")
}

// XXX: We do include_bytes for zkey data, so need to manually change this
#[test]
fn test_circuit_naive_read() -> Result<()> {
circuit_naive_read()
}

// #[test]
// fn test_read_arkzkey_from_bytes() -> Result<()> {
// const ARKZKEY_BYTES: &[u8] = include_bytes!(
// "../../mopro-core/examples/circom/keccak256/target/keccak256_256_test_final.arkzkey"
// );

// println!("Reading arkzkey from bytes (keccak)");
// let now = Instant::now();
// let (_deserialized_proving_key, _deserialized_constraint_matrices) =
// read_arkzkey_from_bytes(ARKZKEY_BYTES)?;
// println!("Time to read arkzkey: {:?}", now.elapsed());

// Ok(())
// }
}
4 changes: 2 additions & 2 deletions src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ const GRAPH_BYTES: [&[u8]; get_supported_depth_count()] =

static ZKEY: [Lazy<(ProvingKey<Bn254>, ConstraintMatrices<Fr>)>; get_supported_depth_count()] =
array_for_depths!(|depth| Lazy::new(|| {
let mut reader = Cursor::new(ZKEY_BYTES[get_depth_index(depth).unwrap()]);
ark_zkey::read_arkzkey(&mut reader).expect("zkey should be valid")
ark_zkey::read_arkzkey_from_bytes(ZKEY_BYTES[get_depth_index(depth).unwrap()])
.expect("zkey should be valid")
}));

#[must_use]
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ pub mod util;

pub mod lazy_merkle_tree;

use ark_bn254::Parameters;
use ark_bn254::Config;
use ark_ec::bn::Bn;

// Export types
Expand All @@ -26,7 +26,7 @@ pub use semaphore_depth_config::get_supported_depths;
#[cfg(feature = "dylib")]
pub use circuit::initialize;

pub type Groth16Proof = ark_groth16::Proof<Bn<Parameters>>;
pub type Groth16Proof = ark_groth16::Proof<Bn<Config>>;
pub type EthereumGroth16Proof = ark_circom::ethereum::Proof;

#[cfg(test)]
Expand Down
Loading

0 comments on commit 68f290a

Please sign in to comment.