From 8e50c666e6cdd3eb4208c3e5cac735884cda6d39 Mon Sep 17 00:00:00 2001 From: iquerejeta Date: Tue, 12 Dec 2023 14:38:00 +0100 Subject: [PATCH] Make report generic over field size and commitment scheme --- halo2_proofs/examples/cost-model.rs | 2 +- halo2_proofs/examples/proof-size.rs | 7 ++- halo2_proofs/src/dev/cost_model.rs | 74 +++++++++++++++++++++-------- 3 files changed, 60 insertions(+), 23 deletions(-) diff --git a/halo2_proofs/examples/cost-model.rs b/halo2_proofs/examples/cost-model.rs index 4c66a49c44..8eaece690e 100644 --- a/halo2_proofs/examples/cost-model.rs +++ b/halo2_proofs/examples/cost-model.rs @@ -59,5 +59,5 @@ fn main() { let opts = CliCostOptions::parse_args_default_or_exit(); let c = ModelCircuit::from(opts.to_cost_options()); println!("{:#?}", c); - println!("Proof size: {} bytes", c.proof_size()); + println!("Proof size: {} bytes", c.proof_size::<32, 32>(CommitmentScheme::IPA)); } diff --git a/halo2_proofs/examples/proof-size.rs b/halo2_proofs/examples/proof-size.rs index 80bb9929ac..4b8c862cca 100644 --- a/halo2_proofs/examples/proof-size.rs +++ b/halo2_proofs/examples/proof-size.rs @@ -12,7 +12,7 @@ use halo2_gadgets::poseidon::{ use std::convert::TryInto; use std::marker::PhantomData; -use halo2_proofs::dev::cost_model::from_circuit_to_model_circuit; +use halo2_proofs::dev::cost_model::{from_circuit_to_model_circuit, CommitmentScheme}; use rand_core::OsRng; #[derive(Clone, Copy)] @@ -150,5 +150,8 @@ fn main() { }; let model = from_circuit_to_model_circuit(K, &circuit, vec![vec![output]]); - println!("Cost of Poseidon with WIDTH = 12 and RATE = 11: {model:?}"); + println!( + "Cost of Poseidon with WIDTH = 12 and RATE = 11: {:?}", + model.report::<56, 56>(CommitmentScheme::KZG) + ); } diff --git a/halo2_proofs/src/dev/cost_model.rs b/halo2_proofs/src/dev/cost_model.rs index acd93815a0..b90e72e05a 100644 --- a/halo2_proofs/src/dev/cost_model.rs +++ b/halo2_proofs/src/dev/cost_model.rs @@ -8,6 +8,15 @@ use ff::{Field, FromUniformBytes}; use super::MockProver; +/// Supported commitment schemes +#[derive(Debug)] +pub enum CommitmentScheme { + /// Inner Product Argument commitment scheme + IPA, + /// KZG commitment scheme + KZG, +} + /// Options to build a circuit specifiction to measure the cost model of. #[derive(Debug)] pub struct CostOptions { @@ -186,14 +195,17 @@ impl From for ModelCircuit { impl ModelCircuit { /// Size of the proof in bytes - pub fn proof_size(&self) -> usize { - let size = |points: usize, scalars: usize| points * 32 + scalars * 32; + pub fn proof_size( + &self, + comm_scheme: CommitmentScheme, + ) -> usize { + let size = |points: usize, scalars: usize| points * COMM + scalars * SCALAR; // PLONK: - // - 32 bytes (commitment) per advice column - // - 3 * 32 bytes (commitments) + 5 * 32 bytes (evals) per lookup argument - // - 32 bytes (commitment) + 2 * 32 bytes (evals) per permutation argument - // - 32 bytes (eval) per column per permutation argument + // - COMM bytes (commitment) per advice column + // - 3 * COMM bytes (commitments) + 5 * SCALAR bytes (evals) per lookup argument + // - COMM bytes (commitment) + 2 * SCALAR bytes (evals) per permutation argument + // - COMM bytes (eval) per column per permutation argument let plonk = size(1, 0) * self.advice_columns + size(3, 5) * self.lookups + self @@ -203,36 +215,55 @@ impl ModelCircuit { .sum::(); // Vanishing argument: - // - (max_deg - 1) * 32 bytes (commitments) + (max_deg - 1) * 32 bytes (h_evals) + // - (max_deg - 1) * COMM bytes (commitments) + (max_deg - 1) * SCALAR bytes (h_evals) // for quotient polynomial - // - 32 bytes (eval) per column query + // - SCALAR bytes (eval) per column query let vanishing = size(self.max_deg - 1, self.max_deg - 1) + size(0, self.column_queries); // Multiopening argument: - // - f_commitment (32 bytes) - // - 32 bytes (evals) per set of points in multiopen argument + // - f_commitment (COMM bytes) + // - SCALAR bytes (evals) per set of points in multiopen argument let multiopen = size(1, self.point_sets); - // Polycommit: - // - s_poly commitment (32 bytes) - // - inner product argument (k rounds * 2 * 32 bytes) - // - a (32 bytes) - // - xi (32 bytes) - let polycomm = size(1 + 2 * self.k, 2); + let polycomm = match comm_scheme { + CommitmentScheme::IPA => { + // Polycommit IPA: + // - s_poly commitment (COMM bytes) + // - inner product argument (k rounds * 2 * COMM bytes) + // - a (SCALAR bytes) + // - xi (SCALAR bytes) + size(1 + 2 * self.k, 2) + } + CommitmentScheme::KZG => { + // Polycommit KZG: + // - quotient polynomial commitment (COMM bytes) + size(1, 0) + } + }; plonk + vanishing + multiopen + polycomm } /// Generate a report. - pub fn report(&self) -> String { + pub fn report( + &self, + comm_scheme: CommitmentScheme, + ) -> String { let mut str = String::new(); str.push_str(&format!("{:#?}", self)); - str.push_str(&format!("Proof size: {} bytes", self.proof_size())); + str.push_str(&format!( + "Proof size: {} bytes", + self.proof_size::(comm_scheme) + )); str } /// Write a CSV report - pub fn report_csv(&self, w: &mut W) -> std::io::Result<()> { + pub fn report_csv( + &self, + w: &mut W, + comm_scheme: CommitmentScheme, + ) -> std::io::Result<()> { let mut w = csv::Writer::from_writer(w); w.write_record(["max_deg", &self.max_deg.to_string()])?; w.write_record(["advice_columns", &self.advice_columns.to_string()])?; @@ -246,7 +277,10 @@ impl ModelCircuit { }])?; w.write_record(["column_queries", &self.column_queries.to_string()])?; w.write_record(["point_sets", &self.point_sets.to_string()])?; - w.write_record(["proof_size", &self.proof_size().to_string()])?; + w.write_record([ + "proof_size", + &self.proof_size::(comm_scheme).to_string(), + ])?; Ok(()) } }