From 944b5bba01ebb93b3159f239dcbf50895b5fb584 Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Fri, 8 Nov 2024 13:37:48 +0000 Subject: [PATCH 1/2] cli: perf: mask some checkings in release mode --- crates/cli/src/config.rs | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/crates/cli/src/config.rs b/crates/cli/src/config.rs index f3607751..de9974d4 100644 --- a/crates/cli/src/config.rs +++ b/crates/cli/src/config.rs @@ -79,6 +79,7 @@ pub(crate) struct Config { } impl Config { + #[cfg(debug_assertions)] fn image_consistent_check(&self, wasm_image: &[u8]) -> anyhow::Result<()> { if let Some(expected_wasm_image_md5) = &self.wasm_image_md5 { let wasm_image_md5 = format!("{:x}", md5::compute(wasm_image)); @@ -94,6 +95,7 @@ impl Config { Ok(()) } + #[cfg(debug_assertions)] fn params_consistent_check(&self, params: &[u8]) -> anyhow::Result<()> { let params_md5 = format!("{:x}", md5::compute(params)); @@ -146,6 +148,7 @@ impl Config { let mut buf = Vec::new(); File::open(wasm_image)?.read_to_end(&mut buf)?; + #[cfg(debug_assertions)] self.image_consistent_check(&buf)?; ZkWasmLoader::parse_module(&buf) @@ -157,6 +160,7 @@ impl Config { let mut buf = Vec::new(); File::open(path)?.read_to_end(&mut buf)?; + #[cfg(debug_assertions)] self.params_consistent_check(&buf)?; let params = Params::::read(&mut Cursor::new(&mut buf))?; @@ -167,18 +171,21 @@ impl Config { fn read_circuit_data( &self, path: &PathBuf, - expected_md5: &str, + _expected_md5: &str, ) -> anyhow::Result> { - let mut buf = Vec::new(); - File::open(path)?.read_to_end(&mut buf)?; + #[cfg(debug_assertions)] + { + let mut buf = Vec::new(); + File::open(path)?.read_to_end(&mut buf)?; - let circuit_data_md5 = format!("{:x}", md5::compute(&buf)); + let circuit_data_md5 = format!("{:x}", md5::compute(&buf)); - if circuit_data_md5 != expected_md5 { - anyhow::bail!( - "Circuit data is inconsistent with the one used to build the circuit. \ + if circuit_data_md5 != _expected_md5 { + anyhow::bail!( + "Circuit data is inconsistent with the one used to build the circuit. \ Maybe you have changed the circuit data after setup the circuit?", - ); + ); + } } let circuit_data = CircuitData::::read(&mut File::open(path)?)?; From 0c945c8d3ac216c2a84351780161cb25753fc03e Mon Sep 17 00:00:00 2001 From: Zhang Junyu Date: Sat, 9 Nov 2024 08:56:16 +0000 Subject: [PATCH 2/2] WIP: cli: perf: load uncompressed params for perf --- crates/cli/Cargo.toml | 1 + crates/cli/src/command.rs | 4 ++ crates/cli/src/config.rs | 15 ++++++- crates/cli/src/names.rs | 5 +++ crates/cli/src/utils.rs | 90 +++++++++++++++++++++++++++++++++++++++ 5 files changed, 114 insertions(+), 1 deletion(-) diff --git a/crates/cli/Cargo.toml b/crates/cli/Cargo.toml index 1c760732..4596504b 100644 --- a/crates/cli/Cargo.toml +++ b/crates/cli/Cargo.toml @@ -27,6 +27,7 @@ console = "0.15.8" cfg-if.workspace = true circuits-batcher.workspace = true poseidon.workspace = true +rayon.workspace = true [features] default = [] diff --git a/crates/cli/src/command.rs b/crates/cli/src/command.rs index 269dff24..e7c97bc6 100644 --- a/crates/cli/src/command.rs +++ b/crates/cli/src/command.rs @@ -8,6 +8,7 @@ use std::path::PathBuf; use crate::args::Scheme; use crate::config::CircuitDataConfig; use crate::config::CircuitDataMd5; +use crate::utils::WriteUncomressed; use crate::TRIVIAL_WASM; use clap::Args; use console::style; @@ -32,6 +33,7 @@ use crate::config::Config; use crate::names::name_of_circuit_data; use crate::names::name_of_config; use crate::names::name_of_params; +use crate::names::name_of_params_uncompressed; #[derive(Debug)] pub(crate) struct SetupArg { @@ -136,6 +138,7 @@ impl SetupArg { let wasm_image_md5 = md5::compute(&wasm_image); let params_path = params_dir.join(name_of_params(self.k)); + let params_uncompressed_path = params_dir.join(name_of_params_uncompressed(self.k)); let params = { if params_path.exists() { println!( @@ -154,6 +157,7 @@ impl SetupArg { let params = Params::::unsafe_setup::(self.k); params.write(&mut File::create(¶ms_path)?)?; + params.write_uncompressed(&mut File::create(¶ms_uncompressed_path)?)?; params } }; diff --git a/crates/cli/src/config.rs b/crates/cli/src/config.rs index de9974d4..5fec6988 100644 --- a/crates/cli/src/config.rs +++ b/crates/cli/src/config.rs @@ -38,8 +38,10 @@ use crate::names::name_of_frame_table_slice; use crate::names::name_of_instance; use crate::names::name_of_loadinfo; use crate::names::name_of_params; +use crate::names::name_of_params_uncompressed; use crate::names::name_of_transcript; use crate::names::name_of_witness; +use crate::utils::WriteUncomressed; #[derive(Serialize, Deserialize)] pub(crate) struct CircuitDataMd5 { @@ -168,6 +170,17 @@ impl Config { Ok(params) } + fn read_params_uncompressed(&self, params_dir: &Path) -> anyhow::Result> { + let path = params_dir.join(name_of_params_uncompressed(self.k)); + + let mut buf = Vec::new(); + File::open(path)?.read_to_end(&mut buf)?; + + let params = Params::::read_uncompressed(&mut Cursor::new(&mut buf))?; + + Ok(params) + } + fn read_circuit_data( &self, path: &PathBuf, @@ -265,7 +278,7 @@ impl Config { let module = self.read_wasm_image(wasm_image)?; println!("{} Load params...", style("[2/8]").bold().dim(),); - let params = self.read_params(params_dir)?; + let params = self.read_params_uncompressed(params_dir)?; let env = env_builder.create_env(arg); diff --git a/crates/cli/src/names.rs b/crates/cli/src/names.rs index f170f9b4..7a70c1f2 100644 --- a/crates/cli/src/names.rs +++ b/crates/cli/src/names.rs @@ -3,6 +3,11 @@ pub(crate) fn name_of_params(k: u32) -> String { format!("K{}.params", k) } +#[inline(always)] +pub(crate) fn name_of_params_uncompressed(k: u32) -> String { + format!("K{}.params_uncompressed", k) +} + #[inline(always)] pub(crate) fn name_of_config(name: &str) -> String { format!("{}.zkwasm.config", name) diff --git a/crates/cli/src/utils.rs b/crates/cli/src/utils.rs index 37268253..293b3e15 100644 --- a/crates/cli/src/utils.rs +++ b/crates/cli/src/utils.rs @@ -1,8 +1,14 @@ use halo2_proofs::pairing::bn256::Bn256; +use halo2_proofs::pairing::bn256::Fq; use halo2_proofs::pairing::bn256::G1Affine; use halo2_proofs::plonk::get_advice_commitments_from_transcript; use halo2_proofs::plonk::VerifyingKey; +use halo2_proofs::poly::commitment::Params; use halo2aggregator_s::transcript::poseidon::PoseidonRead; +use rayon::iter::IndexedParallelIterator; +use rayon::iter::IntoParallelRefMutIterator; +use rayon::iter::ParallelIterator; +use std::io; pub fn get_named_advice_commitment( vkey: &VerifyingKey, @@ -20,3 +26,87 @@ pub fn get_named_advice_commitment( get_advice_commitments_from_transcript::(vkey, &mut PoseidonRead::init(proof)) .unwrap()[img_col_idx as usize] } + +pub(crate) trait WriteUncomressed { + fn write_uncompressed(&self, writer: &mut W) -> io::Result<()>; + fn read_uncompressed(reader: R) -> io::Result + where + Self: Sized; +} + +impl WriteUncomressed for Params { + fn write_uncompressed(&self, writer: &mut W) -> io::Result<()> { + writer.write_all(&self.k.to_le_bytes())?; + for el in &self.g { + for i in 0..4 { + writer.write_all(&el.x.0[i].to_le_bytes())?; + } + for i in 0..4 { + writer.write_all(&el.y.0[i].to_le_bytes())?; + } + } + for el in &self.g_lagrange { + for i in 0..4 { + writer.write_all(&el.x.0[i].to_le_bytes())?; + } + for i in 0..4 { + writer.write_all(&el.y.0[i].to_le_bytes())?; + } + } + let additional_data_len = self.additional_data.len() as u32; + writer.write_all(&additional_data_len.to_le_bytes())?; + writer.write_all(&self.additional_data)?; + Ok(()) + } + + /// Reads params from a buffer. + fn read_uncompressed(mut reader: R) -> io::Result { + let mut k = [0u8; 4]; + reader.read_exact(&mut k[..])?; + let k = u32::from_le_bytes(k); + let n = 1 << k; + + let load_points_from_file_parallelly = |reader: &mut R| -> io::Result> { + let mut points_bytes: Vec<[u8; 8]> = vec![[0u8; 8]; n * 2 * 4]; + for points in points_bytes.iter_mut() { + reader.read_exact((*points).as_mut())?; + } + let mut points = vec![G1Affine::default(); n]; + points.par_iter_mut().enumerate().for_each(|(i, point)| { + *point = G1Affine { + x: Fq([ + u64::from_le_bytes(points_bytes[i * 8]), + u64::from_le_bytes(points_bytes[i * 8 + 1]), + u64::from_le_bytes(points_bytes[i * 8 + 2]), + u64::from_le_bytes(points_bytes[i * 8 + 3]), + ]), + y: Fq([ + u64::from_le_bytes(points_bytes[i * 8 + 4]), + u64::from_le_bytes(points_bytes[i * 8 + 5]), + u64::from_le_bytes(points_bytes[i * 8 + 6]), + u64::from_le_bytes(points_bytes[i * 8 + 7]), + ]), + } + }); + Ok(points) + }; + + let g = load_points_from_file_parallelly(&mut reader)?; + let g_lagrange = load_points_from_file_parallelly(&mut reader)?; + + let mut additional_data_len = [0u8; 4]; + reader.read_exact(&mut additional_data_len[..])?; + let additional_data_len = u32::from_le_bytes(additional_data_len); + let mut additional_data = vec![0u8; additional_data_len as usize]; + + reader.read_exact(&mut additional_data[..])?; + + Ok(Params { + k, + n: n as u64, + g, + g_lagrange, + additional_data, + }) + } +}