Skip to content

Commit

Permalink
Nits
Browse files Browse the repository at this point in the history
  • Loading branch information
iquerejeta committed Dec 14, 2023
1 parent 67440c9 commit b37001e
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 49 deletions.
4 changes: 3 additions & 1 deletion halo2_proofs/examples/cost-model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ impl CliCostOptions {

fn main() {
let opts = CliCostOptions::parse_args_default_or_exit();
let c = opts.to_cost_options().into_model_circuit::<32, 32>(CommitmentScheme::IPA);
let c = opts
.to_cost_options()
.into_model_circuit::<32, 32>(CommitmentScheme::IPA);
println!("{:#?}", c);
}
7 changes: 6 additions & 1 deletion halo2_proofs/examples/proof-size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,12 @@ fn main() {
_spec: PhantomData,
};

let model = from_circuit_to_model_circuit::<_, _,56, 56>(K, &circuit, vec![vec![output]], CommitmentScheme::KZGGWC);
let model = from_circuit_to_model_circuit::<_, _, 56, 56>(
K,
&circuit,
vec![vec![output]],
CommitmentScheme::KZGGWC,
);
println!(
"Cost of Poseidon with WIDTH = 12 and RATE = 11: \n{}",
serde_json::to_string_pretty(&model).unwrap()
Expand Down
71 changes: 24 additions & 47 deletions halo2_proofs/src/dev/cost_model.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
//! The cost estimator takes high-level parameters for a circuit design, and estimates the
//! verification cost, as well as resulting proof size.
use std::{iter, num::ParseIntError, str::FromStr};
use std::collections::HashSet;
use std::{iter, num::ParseIntError, str::FromStr};

use crate::plonk::Circuit;
use ff::{Field, FromUniformBytes};
Expand Down Expand Up @@ -122,34 +122,13 @@ impl Permutation {

/// Structure holding the [Shuffle] related data for circuit benchmarks.
#[derive(Debug, Clone)]
pub struct Shuffle {
_columns: usize,
input_deg: usize,
shuffle_deg: usize,
}

impl FromStr for Shuffle {
type Err = ParseIntError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut parts = s.split(',');
let _columns = parts.next().unwrap().parse()?;
let input_deg = parts.next().unwrap().parse()?;
let shuffle_deg = parts.next().unwrap().parse()?;
Ok(Shuffle {
_columns,
input_deg,
shuffle_deg,
})
}
}
pub struct Shuffle;

impl Shuffle {
fn queries(&self) -> impl Iterator<Item = Poly> {
let shuffle = "0, 1".parse().unwrap();

iter::empty()
.chain(Some(shuffle))
iter::empty().chain(Some(shuffle))
}
}

Expand Down Expand Up @@ -179,7 +158,10 @@ pub struct ModelCircuit {
impl CostOptions {
/// Convert [CostOptions] to [ModelCircuit]. 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_model_circuit<const COMM: usize, const SCALAR: usize>(
&self,
comm_scheme: CommitmentScheme,
) -> ModelCircuit {
let mut queries: Vec<_> = iter::empty()
.chain(self.advice.iter())
.chain(self.instance.iter())
Expand All @@ -205,16 +187,18 @@ impl CostOptions {
// - COMM bytes (eval) per column per permutation argument
let plonk = comp_bytes(1, 0) * self.advice.len()
+ comp_bytes(3, 5) * self.lookup.len()
+ self.permutation
.iter()
.map(|p| comp_bytes(1, 2 + p.columns))
.sum::<usize>();
+ self
.permutation
.iter()
.map(|p| comp_bytes(1, 2 + p.columns))
.sum::<usize>();

// Vanishing argument:
// - (max_deg - 1) * COMM bytes (commitments) + (max_deg - 1) * SCALAR bytes (h_evals)
// for quotient polynomial
// - SCALAR bytes (eval) per column query
let vanishing = comp_bytes(self.max_degree - 1, self.max_degree - 1) + comp_bytes(0, column_queries);
let vanishing =
comp_bytes(self.max_degree - 1, self.max_degree - 1) + comp_bytes(0, column_queries);

// Multiopening argument:
// - f_commitment (COMM bytes)
Expand Down Expand Up @@ -270,7 +254,12 @@ impl CostOptions {
}

/// Given a Plonk circuit, this function returns a [ModelCircuit]
pub fn from_circuit_to_model_circuit<F: Ord + Field + FromUniformBytes<64>, C: Circuit<F>, const COMM: usize, const SCALAR: usize>(
pub fn from_circuit_to_model_circuit<
F: Ord + Field + FromUniformBytes<64>,
C: Circuit<F>,
const COMM: usize,
const SCALAR: usize,
>(
k: u32,
circuit: &C,
instances: Vec<Vec<F>>,
Expand Down Expand Up @@ -316,28 +305,16 @@ pub fn from_circuit_to_cost_model_options<F: Ord + Field + FromUniformBytes<64>,
instance
};

let lookup = {
cs.lookups().iter().map(|_| Lookup).collect::<Vec<_>>()
};
let lookup = { cs.lookups().iter().map(|_| Lookup).collect::<Vec<_>>() };

let permutation = vec![Permutation {
columns: cs.permutation().get_columns().len(),
}];

let shuffle = {
let mut shuffle = vec![];
for l in cs.shuffles().iter() {
shuffle.push(Shuffle {
// this isn't actually used for estimation right now, so ignore it.
_columns: 1,
input_deg: l.input_expressions().iter().map(Expression::degree).max().unwrap(),
shuffle_deg: l.shuffle_expressions().iter().map(Expression::degree).max().unwrap(),
});
}
shuffle
};
let shuffle = { cs.shuffles.iter().map(|_| Shuffle).collect::<Vec<_>>() };

let gate_degree = cs.gates
let gate_degree = cs
.gates
.iter()
.flat_map(|gate| gate.polynomials().iter().map(|poly| poly.degree()))
.max()
Expand Down

0 comments on commit b37001e

Please sign in to comment.