Skip to content

Commit

Permalink
Update Cost model
Browse files Browse the repository at this point in the history
* Support only one polynomial commitments scheme.
* Rename ModelCircuit -> CircuitModel
  • Loading branch information
iquerejeta committed Jan 24, 2025
1 parent 3e0ed05 commit c913679
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 62 deletions.
9 changes: 2 additions & 7 deletions examples/proof-size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use halo2_proofs::{
};
use halo2curves::pasta::Fp;

use halo2_proofs::dev::cost_model::{from_circuit_to_model_circuit, CommitmentScheme};
use halo2_proofs::dev::cost_model::from_circuit_to_circuit_model;
use halo2_proofs::plonk::{Expression, Selector, TableColumn};
use halo2_proofs::poly::Rotation;

Expand Down Expand Up @@ -88,12 +88,7 @@ const K: u32 = 11;
fn main() {
let circuit = TestCircuit {};

let model = from_circuit_to_model_circuit::<_, _, 32, 32>(
Some(K),
&circuit,
vec![],
CommitmentScheme::KZGGWC,
);
let model = from_circuit_to_circuit_model::<_, _, 32, 32>(Some(K), &circuit, vec![]);
println!(
"Cost of circuit with 8 bit lookup table: \n{}",
serde_json::to_string_pretty(&model).unwrap()
Expand Down
78 changes: 23 additions & 55 deletions src/dev/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,18 +10,7 @@ use ff::{Field, FromUniformBytes};
use serde::Deserialize;
use serde_derive::Serialize;

use super::MockProver;

/// Supported commitment schemes
#[derive(Debug, Eq, PartialEq)]
pub enum CommitmentScheme {
/// Inner Product Argument commitment scheme
IPA,
/// KZG with GWC19 mutli-open strategy
KZGGWC,
/// KZG with BDFG20 mutli-open strategy
KZGSHPLONK,
}
use super::{CellValue, InstanceValue, Region};

/// Options to build a circuit specification to measure the cost model of.
#[derive(Debug)]
Expand Down Expand Up @@ -130,7 +119,7 @@ impl Permutation {

/// High-level specifications of an abstract circuit.
#[derive(Debug, Deserialize, Serialize)]
pub struct ModelCircuit {
pub struct CircuitModel {
/// Power-of-2 bound on the number of rows in the circuit.
pub k: usize,
/// Number of rows in the circuit (not including table rows).
Expand All @@ -154,12 +143,9 @@ pub struct ModelCircuit {
}

impl CostOptions {
/// Convert [CostOptions] to [ModelCircuit]. The proof sizè is computed depending on the base
/// Convert [CostOptions] to [CircuitModel]. The proof sizè is computed depending on the base
/// and scalar field size of the curve used, together with the [CommitmentScheme].
pub fn into_model_circuit<const COMM: usize, const SCALAR: usize>(
&self,
comm_scheme: CommitmentScheme,
) -> ModelCircuit {
pub fn into_circuit_model<const COMM: usize, const SCALAR: usize>(&self) -> CircuitModel {
let mut queries: Vec<_> = iter::empty()
.chain(self.advice.iter())
.chain(self.instance.iter())
Expand Down Expand Up @@ -198,41 +184,24 @@ impl CostOptions {
// - SCALAR bytes (evals) per set of points in multiopen argument
let multiopen = comp_bytes(1, point_sets);

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)
comp_bytes(1 + 2 * self.min_k, 2)
}
CommitmentScheme::KZGGWC => {
let mut nr_rotations = HashSet::new();
for poly in self.advice.iter() {
nr_rotations.extend(poly.rotations.clone());
}
for poly in self.fixed.iter() {
nr_rotations.extend(poly.rotations.clone());
}
for poly in self.instance.iter() {
nr_rotations.extend(poly.rotations.clone());
}
let mut nr_rotations = HashSet::new();
for poly in self.advice.iter() {
nr_rotations.extend(poly.rotations.clone());
}
for poly in self.fixed.iter() {
nr_rotations.extend(poly.rotations.clone());
}
for poly in self.instance.iter() {
nr_rotations.extend(poly.rotations.clone());
}

// Polycommit GWC:
// - number_rotations * COMM bytes
comp_bytes(nr_rotations.len(), 0)
}
CommitmentScheme::KZGSHPLONK => {
// Polycommit SHPLONK:
// - quotient polynomial commitment (COMM bytes)
comp_bytes(1, 0)
}
};
// Polycommit GWC:
// - number_rotations * COMM bytes
let polycomm = comp_bytes(nr_rotations.len(), 0);

let size = plonk + vanishing + multiopen + polycomm;

ModelCircuit {
CircuitModel {
k: self.min_k,
rows: self.rows_count,
table_rows: self.table_rows_count,
Expand All @@ -247,8 +216,8 @@ impl CostOptions {
}
}

/// Given a Plonk circuit, this function returns a [ModelCircuit]
pub fn from_circuit_to_model_circuit<
/// Given a Plonk circuit, this function returns a [CircuitModel]
pub fn from_circuit_to_circuit_model<
F: Ord + Field + FromUniformBytes<64>,
C: Circuit<F>,
const COMM: usize,
Expand All @@ -257,10 +226,9 @@ pub fn from_circuit_to_model_circuit<
k: Option<u32>,
circuit: &C,
instances: Vec<Vec<F>>,
comm_scheme: CommitmentScheme,
) -> ModelCircuit {
) -> CircuitModel {
let options = from_circuit_to_cost_model_options(k, circuit, instances);
options.into_model_circuit::<COMM, SCALAR>(comm_scheme)
options.into_circuit_model::<COMM, SCALAR>()
}

/// Given a circuit, this function returns [CostOptions]. If no upper bound for `k` is
Expand Down Expand Up @@ -368,7 +336,7 @@ pub fn from_circuit_to_cost_model_options<F: Ord + Field + FromUniformBytes<64>,
max_degree: cs.degree(),
lookup,
permutation,
min_k,
min_k: (min_k - 1).next_power_of_two().ilog2() as usize,
rows_count,
table_rows_count,
compressed_rows_count,
Expand Down

0 comments on commit c913679

Please sign in to comment.