Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cost options #13

Merged
merged 3 commits into from
Nov 13, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
52 changes: 41 additions & 11 deletions halo2_frontend/src/dev/cost_model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//! verification cost, as well as resulting proof size.

use std::collections::HashSet;
use std::{iter, num::ParseIntError, str::FromStr};
use std::panic::AssertUnwindSafe;
use std::{iter, num::ParseIntError, panic, str::FromStr};

use crate::plonk::Circuit;
use halo2_middleware::ff::{Field, FromUniformBytes};
Expand Down Expand Up @@ -49,8 +50,8 @@ pub struct CostOptions {
/// A shuffle over N columns with max input degree I and max shuffle degree T. May be repeated.
pub shuffle: Vec<Shuffle>,

/// 2^K bound on the number of rows.
pub k: usize,
/// 2^K bound on the number of rows, accounting for ZK, PIs and Lookup tables.
pub min_k: usize,

/// Rows count, not including table rows and not accounting for compression
/// (where multiple regions can use the same rows).
Expand Down Expand Up @@ -220,7 +221,7 @@ impl CostOptions {
// - inner product argument (k rounds * 2 * COMM bytes)
// - a (SCALAR bytes)
// - xi (SCALAR bytes)
comp_bytes(1 + 2 * self.k, 2)
comp_bytes(1 + 2 * self.min_k, 2)
}
CommitmentScheme::KZGGWC => {
let mut nr_rotations = HashSet::new();
Expand Down Expand Up @@ -248,7 +249,7 @@ impl CostOptions {
let size = plonk + vanishing + multiopen + polycomm;

ModelCircuit {
k: self.k,
k: self.min_k,
rows: self.rows_count,
table_rows: self.table_rows_count,
max_deg: self.max_degree,
Expand All @@ -270,7 +271,7 @@ pub fn from_circuit_to_model_circuit<
const COMM: usize,
const SCALAR: usize,
>(
k: u32,
k: Option<u32>,
circuit: &C,
instances: Vec<Vec<F>>,
comm_scheme: CommitmentScheme,
Expand All @@ -279,13 +280,35 @@ pub fn from_circuit_to_model_circuit<
options.into_model_circuit::<COMM, SCALAR>(comm_scheme)
}

/// Given a Plonk circuit, this function returns [CostOptions]
fn run_mock_prover_with_fallback<F: Ord + Field + FromUniformBytes<64>, C: Circuit<F>>(
circuit: &C,
instances: Vec<Vec<F>>,
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved
) -> MockProver<F> {
(5..25)
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved
.find_map(|k| {
panic::catch_unwind(AssertUnwindSafe(|| {
MockProver::run(k, circuit, instances.clone()).unwrap()
}))
.ok()
})
.expect("No valid prover found within the range")
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved
}

/// Given a Plonk circuit, this function returns [CostOptions]. If no upper bound for `k` is
/// provided, the function iterates until a valid `k` is found. This might delay computation.
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved
pub fn from_circuit_to_cost_model_options<F: Ord + Field + FromUniformBytes<64>, C: Circuit<F>>(
k: u32,
k_upper_bound: Option<u32>,
circuit: &C,
instances: Vec<Vec<F>>,
) -> CostOptions {
let prover = MockProver::run(k, circuit, instances).unwrap();
let instance_len = instances.first().map_or(0, Vec::len);
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved

let prover = if let Some(k) = k_upper_bound {
MockProver::run(k, circuit, instances).unwrap()
} else {
run_mock_prover_with_fallback(circuit, instances.clone())
};

let cs = prover.cs;

let fixed = {
Expand Down Expand Up @@ -362,7 +385,14 @@ pub fn from_circuit_to_cost_model_options<F: Ord + Field + FromUniformBytes<64>,
(rows_count, table_rows_count, compressed_rows_count)
};

let k = prover.k.try_into().unwrap();
let min_k = [
rows_count + cs.blinding_factors(),
table_rows_count + cs.blinding_factors(),
instance_len,
iquerejeta marked this conversation as resolved.
Show resolved Hide resolved
]
.into_iter()
.max()
.unwrap();

CostOptions {
advice,
Expand All @@ -373,7 +403,7 @@ pub fn from_circuit_to_cost_model_options<F: Ord + Field + FromUniformBytes<64>,
lookup,
permutation,
shuffle,
k,
min_k,
rows_count,
table_rows_count,
compressed_rows_count,
Expand Down
2 changes: 1 addition & 1 deletion halo2_proofs/examples/proof-size.rs
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ fn main() {
let circuit = TestCircuit {};

let model = from_circuit_to_model_circuit::<_, _, 56, 56>(
K,
Some(K),
&circuit,
vec![],
CommitmentScheme::KZGGWC,
Expand Down
Loading