From 64680a1a2dcb81ee6fe9c44618fbff93bfe7fd76 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Tue, 16 Nov 2021 14:20:57 +0000 Subject: [PATCH] dev: Handle too-small `k` error in `CircuitLayout::render` In most places within `CircuitLayout`, we are flexible on `k`. However, for tracking selectors we need to know the number of rows. We could change this, but given that we also want the number of rows to be accurate for rendering unusable rows, we can instead use this as an opportunity to inform the user that they will need to bump `k`. --- CHANGELOG.md | 2 ++ src/dev/graph/layout.rs | 18 +++++++++++++----- 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index d3d5af77..65572cd9 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -18,6 +18,8 @@ and this project adheres to Rust's notion of - `Error::SynthesisError` is now `Error::Synthesis`. - `Error::TranscriptError` is now `Error::Transcript`, and stores the underlying `io::Error`. +- `halo2::dev::CircuitLayout::render` now takes `k` as a `u32`, matching the + regular parameter APIs. ### Removed - `halo2::arithmetic::BatchInvert` (use `ff::BatchInvert` instead). diff --git a/src/dev/graph/layout.rs b/src/dev/graph/layout.rs index c6993a08..7e6ca240 100644 --- a/src/dev/graph/layout.rs +++ b/src/dev/graph/layout.rs @@ -33,7 +33,8 @@ use crate::plonk::{ /// .unwrap(); /// /// let circuit = MyCircuit::default(); -/// CircuitLayout::default().render(&circuit, &drawing_area).unwrap(); +/// let k = 5; // Suitable size for MyCircuit +/// CircuitLayout::default().render(k, &circuit, &drawing_area).unwrap(); /// ``` #[derive(Debug, Default)] pub struct CircuitLayout { @@ -84,7 +85,7 @@ impl CircuitLayout { /// Renders the given circuit on the given drawing area. pub fn render, DB: DrawingBackend>( self, - k: usize, + k: u32, circuit: &ConcreteCircuit, drawing_area: &DrawingArea, ) -> Result<(), DrawingAreaErrorKind> { @@ -95,7 +96,7 @@ impl CircuitLayout { // Collect the layout details. let mut cs = ConstraintSystem::default(); let config = ConcreteCircuit::configure(&mut cs); - let mut layout = Layout::new(n, cs.num_selectors); + let mut layout = Layout::new(k, n, cs.num_selectors); ConcreteCircuit::FloorPlanner::synthesize( &mut layout, circuit, @@ -336,6 +337,7 @@ struct Region { #[derive(Default)] struct Layout { + k: u32, regions: Vec, current_region: Option, total_rows: usize, @@ -349,8 +351,9 @@ struct Layout { } impl Layout { - fn new(n: usize, num_selectors: usize) -> Self { + fn new(k: u32, n: usize, num_selectors: usize) -> Self { Layout { + k, regions: vec![], current_region: None, total_rows: 0, @@ -417,7 +420,12 @@ impl Assignment for Layout { A: FnOnce() -> AR, AR: Into, { - self.selectors[selector.0][row] = true; + if let Some(cell) = self.selectors[selector.0].get_mut(row) { + *cell = true; + } else { + return Err(Error::not_enough_rows_available(self.k, row + 1)); + } + self.update((*selector).into(), row); Ok(()) }