Skip to content

Commit

Permalink
WIP: cli: perf: load uncompressed params for perf
Browse files Browse the repository at this point in the history
  • Loading branch information
junyu0312 committed Nov 9, 2024
1 parent 944b5bb commit 0c945c8
Show file tree
Hide file tree
Showing 5 changed files with 114 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ console = "0.15.8"
cfg-if.workspace = true
circuits-batcher.workspace = true
poseidon.workspace = true
rayon.workspace = true

[features]
default = []
Expand Down
4 changes: 4 additions & 0 deletions crates/cli/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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 {
Expand Down Expand Up @@ -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!(
Expand All @@ -154,6 +157,7 @@ impl SetupArg {
let params = Params::<G1Affine>::unsafe_setup::<Bn256>(self.k);

params.write(&mut File::create(&params_path)?)?;
params.write_uncompressed(&mut File::create(&params_uncompressed_path)?)?;
params
}
};
Expand Down
15 changes: 14 additions & 1 deletion crates/cli/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -168,6 +170,17 @@ impl Config {
Ok(params)
}

fn read_params_uncompressed(&self, params_dir: &Path) -> anyhow::Result<Params<G1Affine>> {
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::<G1Affine>::read_uncompressed(&mut Cursor::new(&mut buf))?;

Ok(params)
}

fn read_circuit_data(
&self,
path: &PathBuf,
Expand Down Expand Up @@ -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);

Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/names.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
90 changes: 90 additions & 0 deletions crates/cli/src/utils.rs
Original file line number Diff line number Diff line change
@@ -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<G1Affine>,
Expand All @@ -20,3 +26,87 @@ pub fn get_named_advice_commitment(
get_advice_commitments_from_transcript::<Bn256, _, _>(vkey, &mut PoseidonRead::init(proof))
.unwrap()[img_col_idx as usize]
}

pub(crate) trait WriteUncomressed {
fn write_uncompressed<W: io::Write>(&self, writer: &mut W) -> io::Result<()>;
fn read_uncompressed<R: io::Read>(reader: R) -> io::Result<Self>
where
Self: Sized;
}

impl WriteUncomressed for Params<G1Affine> {
fn write_uncompressed<W: io::Write>(&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<R: io::Read>(mut reader: R) -> io::Result<Self> {
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<Vec<G1Affine>> {
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,
})
}
}

0 comments on commit 0c945c8

Please sign in to comment.