2021-01-23 09:54:31 -08:00
|
|
|
use ff::Field;
|
|
|
|
use plotters::{
|
|
|
|
coord::Shift,
|
|
|
|
prelude::{DrawingArea, DrawingAreaErrorKind, DrawingBackend},
|
|
|
|
};
|
2021-07-20 02:44:04 -07:00
|
|
|
|
2021-01-23 09:54:31 -08:00
|
|
|
use std::collections::HashSet;
|
2021-06-08 04:11:21 -07:00
|
|
|
use std::ops::Range;
|
2021-01-23 09:54:31 -08:00
|
|
|
|
2022-06-07 16:15:35 -07:00
|
|
|
use crate::{
|
2022-10-08 03:28:44 -07:00
|
|
|
circuit::layouter::RegionColumn,
|
2021-07-20 02:44:04 -07:00
|
|
|
dev::cost::{Cell, Layout, LayoutRegion},
|
2022-10-08 03:28:44 -07:00
|
|
|
plonk::{Any, Circuit, Column, ConstraintSystem, FloorPlanner},
|
2021-02-18 23:24:21 -08:00
|
|
|
};
|
2021-01-23 09:54:31 -08:00
|
|
|
|
2021-06-08 04:06:48 -07:00
|
|
|
/// Graphical renderer for circuit layouts.
|
2021-01-23 09:54:31 -08:00
|
|
|
///
|
2021-01-29 06:10:25 -08:00
|
|
|
/// Cells that have been assigned to by the circuit will be shaded. If any cells are
|
|
|
|
/// assigned to more than once (which is usually a mistake), they will be shaded darker
|
|
|
|
/// than the surrounding cells.
|
|
|
|
///
|
2021-01-23 09:54:31 -08:00
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```ignore
|
2022-01-27 15:28:02 -08:00
|
|
|
/// use halo2_proofs::dev::CircuitLayout;
|
2021-01-23 09:54:31 -08:00
|
|
|
/// use plotters::prelude::*;
|
|
|
|
///
|
|
|
|
/// let drawing_area = BitMapBackend::new("example-circuit-layout.png", (1024, 768))
|
|
|
|
/// .into_drawing_area();
|
|
|
|
/// drawing_area.fill(&WHITE).unwrap();
|
|
|
|
/// let drawing_area = drawing_area
|
|
|
|
/// .titled("Example Circuit Layout", ("sans-serif", 60))
|
|
|
|
/// .unwrap();
|
|
|
|
///
|
|
|
|
/// let circuit = MyCircuit::default();
|
2021-11-16 06:20:57 -08:00
|
|
|
/// let k = 5; // Suitable size for MyCircuit
|
|
|
|
/// CircuitLayout::default().render(k, &circuit, &drawing_area).unwrap();
|
2021-01-23 09:54:31 -08:00
|
|
|
/// ```
|
2021-06-08 04:06:48 -07:00
|
|
|
#[derive(Debug, Default)]
|
2021-06-08 04:11:21 -07:00
|
|
|
pub struct CircuitLayout {
|
2021-06-08 04:16:02 -07:00
|
|
|
hide_labels: bool,
|
2021-07-16 07:01:55 -07:00
|
|
|
mark_equality_cells: bool,
|
|
|
|
show_equality_constraints: bool,
|
2021-06-08 04:11:21 -07:00
|
|
|
view_width: Option<Range<usize>>,
|
|
|
|
view_height: Option<Range<usize>>,
|
|
|
|
}
|
2021-06-08 04:06:48 -07:00
|
|
|
|
|
|
|
impl CircuitLayout {
|
2021-06-08 04:16:02 -07:00
|
|
|
/// Sets the visibility of region labels.
|
|
|
|
///
|
|
|
|
/// The default is to show labels.
|
|
|
|
pub fn show_labels(mut self, show: bool) -> Self {
|
|
|
|
self.hide_labels = !show;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-16 07:01:55 -07:00
|
|
|
/// Marks cells involved in equality constraints, in red.
|
|
|
|
///
|
|
|
|
/// The default is to not mark these cells.
|
|
|
|
pub fn mark_equality_cells(mut self, show: bool) -> Self {
|
|
|
|
self.mark_equality_cells = show;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-07-16 15:02:18 -07:00
|
|
|
/// Draws red lines between equality-constrained cells.
|
2021-07-16 07:01:55 -07:00
|
|
|
///
|
|
|
|
/// The default is to not show these, as they can get _very_ messy.
|
|
|
|
pub fn show_equality_constraints(mut self, show: bool) -> Self {
|
|
|
|
self.show_equality_constraints = show;
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-06-08 04:11:21 -07:00
|
|
|
/// Sets the view width for this layout, as a number of columns.
|
|
|
|
pub fn view_width(mut self, width: Range<usize>) -> Self {
|
|
|
|
self.view_width = Some(width);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Sets the view height for this layout, as a number of rows.
|
|
|
|
pub fn view_height(mut self, height: Range<usize>) -> Self {
|
|
|
|
self.view_height = Some(height);
|
|
|
|
self
|
|
|
|
}
|
|
|
|
|
2021-06-08 04:06:48 -07:00
|
|
|
/// Renders the given circuit on the given drawing area.
|
|
|
|
pub fn render<F: Field, ConcreteCircuit: Circuit<F>, DB: DrawingBackend>(
|
|
|
|
self,
|
2021-11-16 06:20:57 -08:00
|
|
|
k: u32,
|
2021-06-08 04:06:48 -07:00
|
|
|
circuit: &ConcreteCircuit,
|
|
|
|
drawing_area: &DrawingArea<DB, Shift>,
|
|
|
|
) -> Result<(), DrawingAreaErrorKind<DB::ErrorType>> {
|
|
|
|
use plotters::coord::types::RangedCoordusize;
|
|
|
|
use plotters::prelude::*;
|
|
|
|
|
2021-07-21 14:45:36 -07:00
|
|
|
let n = 1 << k;
|
2021-06-08 04:06:48 -07:00
|
|
|
// Collect the layout details.
|
|
|
|
let mut cs = ConstraintSystem::default();
|
|
|
|
let config = ConcreteCircuit::configure(&mut cs);
|
2021-11-16 06:20:57 -08:00
|
|
|
let mut layout = Layout::new(k, n, cs.num_selectors);
|
2021-07-20 08:01:38 -07:00
|
|
|
ConcreteCircuit::FloorPlanner::synthesize(
|
|
|
|
&mut layout,
|
|
|
|
circuit,
|
|
|
|
config,
|
|
|
|
cs.constants.clone(),
|
|
|
|
)
|
|
|
|
.unwrap();
|
2021-07-21 14:45:36 -07:00
|
|
|
let (cs, selector_polys) = cs.compress_selectors(layout.selectors);
|
|
|
|
let non_selector_fixed_columns = cs.num_fixed_columns - selector_polys.len();
|
2021-06-08 04:06:48 -07:00
|
|
|
|
|
|
|
// Figure out what order to render the columns in.
|
|
|
|
// TODO: For now, just render them in the order they were configured.
|
2021-07-20 11:54:37 -07:00
|
|
|
let total_columns = cs.num_instance_columns + cs.num_advice_columns + cs.num_fixed_columns;
|
2021-07-21 14:45:36 -07:00
|
|
|
let column_index = |cs: &ConstraintSystem<F>, column: RegionColumn| {
|
|
|
|
let column: Column<Any> = match column {
|
|
|
|
RegionColumn::Column(col) => col,
|
|
|
|
RegionColumn::Selector(selector) => cs.selector_map[selector.0].into(),
|
|
|
|
};
|
2021-06-08 04:06:48 -07:00
|
|
|
column.index()
|
|
|
|
+ match column.column_type() {
|
2021-07-20 11:54:37 -07:00
|
|
|
Any::Instance => 0,
|
|
|
|
Any::Advice => cs.num_instance_columns,
|
|
|
|
Any::Fixed => cs.num_instance_columns + cs.num_advice_columns,
|
2021-06-08 04:06:48 -07:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2021-06-08 04:11:21 -07:00
|
|
|
let view_width = self.view_width.unwrap_or(0..total_columns);
|
2021-07-26 18:25:46 -07:00
|
|
|
let view_height = self.view_height.unwrap_or(0..n);
|
2021-06-08 04:11:21 -07:00
|
|
|
let view_bottom = view_height.end;
|
|
|
|
|
2021-06-08 04:06:48 -07:00
|
|
|
// Prepare the grid layout. We render a red background for advice columns, white for
|
2021-06-09 16:06:20 -07:00
|
|
|
// instance columns, and blue for fixed columns (with a darker blue for selectors).
|
2021-06-08 04:06:48 -07:00
|
|
|
let root =
|
|
|
|
drawing_area.apply_coord_spec(Cartesian2d::<RangedCoordusize, RangedCoordusize>::new(
|
2021-06-08 04:11:21 -07:00
|
|
|
view_width,
|
|
|
|
view_height,
|
2021-06-08 04:06:48 -07:00
|
|
|
drawing_area.get_pixel_range(),
|
|
|
|
));
|
|
|
|
root.draw(&Rectangle::new(
|
2021-06-08 04:11:21 -07:00
|
|
|
[(0, 0), (total_columns, view_bottom)],
|
2021-06-08 04:06:48 -07:00
|
|
|
ShapeStyle::from(&WHITE).filled(),
|
|
|
|
))?;
|
2021-01-23 09:54:31 -08:00
|
|
|
root.draw(&Rectangle::new(
|
2021-07-20 11:54:37 -07:00
|
|
|
[
|
|
|
|
(cs.num_instance_columns, 0),
|
|
|
|
(cs.num_instance_columns + cs.num_advice_columns, view_bottom),
|
|
|
|
],
|
2021-06-08 04:06:48 -07:00
|
|
|
ShapeStyle::from(&RED.mix(0.2)).filled(),
|
2021-01-23 09:54:31 -08:00
|
|
|
))?;
|
2021-01-29 06:10:25 -08:00
|
|
|
root.draw(&Rectangle::new(
|
2021-06-08 04:06:48 -07:00
|
|
|
[
|
2021-07-20 11:54:37 -07:00
|
|
|
(cs.num_instance_columns + cs.num_advice_columns, 0),
|
2021-06-08 04:11:21 -07:00
|
|
|
(total_columns, view_bottom),
|
2021-06-08 04:06:48 -07:00
|
|
|
],
|
|
|
|
ShapeStyle::from(&BLUE.mix(0.2)).filled(),
|
|
|
|
))?;
|
2021-07-21 14:45:36 -07:00
|
|
|
{
|
2021-06-09 16:06:20 -07:00
|
|
|
root.draw(&Rectangle::new(
|
|
|
|
[
|
|
|
|
(
|
2021-07-21 14:45:36 -07:00
|
|
|
cs.num_instance_columns
|
|
|
|
+ cs.num_advice_columns
|
|
|
|
+ non_selector_fixed_columns,
|
|
|
|
0,
|
2021-06-09 16:06:20 -07:00
|
|
|
),
|
2021-07-21 14:45:36 -07:00
|
|
|
(total_columns, view_bottom),
|
2021-06-09 16:06:20 -07:00
|
|
|
],
|
|
|
|
ShapeStyle::from(&BLUE.mix(0.1)).filled(),
|
|
|
|
))?;
|
|
|
|
}
|
2021-07-26 18:25:07 -07:00
|
|
|
|
|
|
|
// Mark the unusable rows of the circuit.
|
|
|
|
let usable_rows = n - (cs.blinding_factors() + 1);
|
|
|
|
if view_bottom > usable_rows {
|
|
|
|
root.draw(&Rectangle::new(
|
|
|
|
[(0, usable_rows), (total_columns, view_bottom)],
|
|
|
|
ShapeStyle::from(&RED.mix(0.4)).filled(),
|
|
|
|
))?;
|
|
|
|
}
|
|
|
|
|
2021-06-08 04:06:48 -07:00
|
|
|
root.draw(&Rectangle::new(
|
2021-06-08 04:11:21 -07:00
|
|
|
[(0, 0), (total_columns, view_bottom)],
|
2022-11-29 11:34:47 -08:00
|
|
|
BLACK,
|
2021-06-08 04:06:48 -07:00
|
|
|
))?;
|
|
|
|
|
2021-06-09 16:05:49 -07:00
|
|
|
let draw_region = |root: &DrawingArea<_, _>, top_left, bottom_right| {
|
2021-06-09 16:06:20 -07:00
|
|
|
root.draw(&Rectangle::new(
|
|
|
|
[top_left, bottom_right],
|
|
|
|
ShapeStyle::from(&WHITE).filled(),
|
|
|
|
))?;
|
|
|
|
root.draw(&Rectangle::new(
|
|
|
|
[top_left, bottom_right],
|
|
|
|
ShapeStyle::from(&RED.mix(0.2)).filled(),
|
|
|
|
))?;
|
2021-06-08 04:06:48 -07:00
|
|
|
root.draw(&Rectangle::new(
|
|
|
|
[top_left, bottom_right],
|
|
|
|
ShapeStyle::from(&GREEN.mix(0.2)).filled(),
|
|
|
|
))?;
|
2022-11-29 11:34:47 -08:00
|
|
|
root.draw(&Rectangle::new([top_left, bottom_right], BLACK))?;
|
2021-06-08 04:16:02 -07:00
|
|
|
Ok(())
|
2021-06-08 04:06:48 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
let draw_cell = |root: &DrawingArea<_, _>, column, row| {
|
|
|
|
root.draw(&Rectangle::new(
|
|
|
|
[(column, row), (column + 1, row + 1)],
|
|
|
|
ShapeStyle::from(&BLACK.mix(0.1)).filled(),
|
|
|
|
))
|
|
|
|
};
|
|
|
|
|
|
|
|
// Render the regions!
|
2021-06-09 16:05:49 -07:00
|
|
|
let mut labels = if self.hide_labels { None } else { Some(vec![]) };
|
2021-07-26 18:25:46 -07:00
|
|
|
for region in &layout.regions {
|
2021-06-08 04:06:48 -07:00
|
|
|
if let Some(offset) = region.offset {
|
|
|
|
// Sort the region's columns according to the defined ordering.
|
2021-07-26 18:25:46 -07:00
|
|
|
let mut columns: Vec<_> = region.columns.iter().cloned().collect();
|
2021-07-21 14:45:36 -07:00
|
|
|
columns.sort_unstable_by_key(|a| column_index(&cs, *a));
|
2021-06-08 04:06:48 -07:00
|
|
|
|
|
|
|
// Render contiguous parts of the same region as a single box.
|
|
|
|
let mut width = None;
|
|
|
|
for column in columns {
|
2021-07-21 14:45:36 -07:00
|
|
|
let column = column_index(&cs, column);
|
2021-06-08 04:06:48 -07:00
|
|
|
match width {
|
|
|
|
Some((start, end)) if end == column => width = Some((start, end + 1)),
|
|
|
|
Some((start, end)) => {
|
2021-06-09 16:05:49 -07:00
|
|
|
draw_region(&root, (start, offset), (end, offset + region.rows))?;
|
|
|
|
if let Some(labels) = &mut labels {
|
|
|
|
labels.push((region.name.clone(), (start, offset)));
|
|
|
|
}
|
2021-06-08 04:06:48 -07:00
|
|
|
width = Some((column, column + 1));
|
|
|
|
}
|
|
|
|
None => width = Some((column, column + 1)),
|
2021-01-23 09:54:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-08 04:06:48 -07:00
|
|
|
// Render the last part of the region.
|
|
|
|
if let Some((start, end)) = width {
|
2021-06-09 16:05:49 -07:00
|
|
|
draw_region(&root, (start, offset), (end, offset + region.rows))?;
|
|
|
|
if let Some(labels) = &mut labels {
|
|
|
|
labels.push((region.name.clone(), (start, offset)));
|
|
|
|
}
|
2021-06-08 04:06:48 -07:00
|
|
|
}
|
2021-07-26 18:25:46 -07:00
|
|
|
}
|
|
|
|
}
|
2021-01-29 06:10:25 -08:00
|
|
|
|
2021-07-26 18:25:46 -07:00
|
|
|
// Darken the cells of the region that have been assigned to.
|
|
|
|
for region in layout.regions {
|
2021-07-20 02:43:12 -07:00
|
|
|
for Cell { column, row } in region.cells {
|
2021-07-26 18:25:46 -07:00
|
|
|
draw_cell(&root, column_index(&cs, column), row)?;
|
2021-01-29 06:10:25 -08:00
|
|
|
}
|
2021-01-23 09:54:31 -08:00
|
|
|
}
|
|
|
|
|
2021-06-08 04:06:48 -07:00
|
|
|
// Darken any loose cells that have been assigned to.
|
2021-07-20 02:43:12 -07:00
|
|
|
for Cell { column, row } in layout.loose_cells {
|
2021-07-21 14:45:36 -07:00
|
|
|
draw_cell(&root, column_index(&cs, column), row)?;
|
2021-06-08 04:06:48 -07:00
|
|
|
}
|
2021-01-29 06:10:25 -08:00
|
|
|
|
2021-07-16 07:01:55 -07:00
|
|
|
// Mark equality-constrained cells.
|
|
|
|
if self.mark_equality_cells {
|
2021-07-16 15:00:49 -07:00
|
|
|
let mut cells = HashSet::new();
|
2021-07-20 02:43:12 -07:00
|
|
|
for (l, r) in &layout.equality {
|
|
|
|
let l_col = column_index(&cs, l.column);
|
|
|
|
let r_col = column_index(&cs, r.column);
|
2021-07-16 15:00:49 -07:00
|
|
|
|
|
|
|
// Deduplicate cells.
|
2021-07-20 02:43:12 -07:00
|
|
|
cells.insert((l_col, l.row));
|
|
|
|
cells.insert((r_col, r.row));
|
2021-07-16 15:00:49 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
for (col, row) in cells {
|
2021-07-16 07:01:55 -07:00
|
|
|
root.draw(&Rectangle::new(
|
2021-07-16 15:00:49 -07:00
|
|
|
[(col, row), (col + 1, row + 1)],
|
|
|
|
ShapeStyle::from(&RED.mix(0.5)).filled(),
|
2021-07-16 07:01:55 -07:00
|
|
|
))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Draw lines between equality-constrained cells.
|
|
|
|
if self.show_equality_constraints {
|
2021-07-20 02:43:12 -07:00
|
|
|
for (l, r) in &layout.equality {
|
|
|
|
let l_col = column_index(&cs, l.column);
|
|
|
|
let r_col = column_index(&cs, r.column);
|
2021-07-16 07:01:55 -07:00
|
|
|
root.draw(&PathElement::new(
|
2021-07-20 02:43:12 -07:00
|
|
|
[(l_col, l.row), (r_col, r.row)],
|
2021-07-16 07:01:55 -07:00
|
|
|
ShapeStyle::from(&RED),
|
|
|
|
))?;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-26 18:25:07 -07:00
|
|
|
// Add a line showing the total used rows.
|
|
|
|
root.draw(&PathElement::new(
|
|
|
|
[(0, layout.total_rows), (total_columns, layout.total_rows)],
|
|
|
|
ShapeStyle::from(&BLACK),
|
|
|
|
))?;
|
|
|
|
|
2021-06-09 16:05:49 -07:00
|
|
|
// Render labels last, on top of everything else.
|
|
|
|
if let Some(labels) = labels {
|
|
|
|
for (label, top_left) in labels {
|
|
|
|
root.draw(
|
|
|
|
&(EmptyElement::at(top_left)
|
|
|
|
+ Text::new(label, (10, 10), ("sans-serif", 15.0).into_font())),
|
|
|
|
)?;
|
|
|
|
}
|
2021-07-26 18:25:07 -07:00
|
|
|
root.draw(
|
|
|
|
&(EmptyElement::at((0, layout.total_rows))
|
|
|
|
+ Text::new(
|
|
|
|
format!("{} used rows", layout.total_rows),
|
|
|
|
(10, 10),
|
|
|
|
("sans-serif", 15.0).into_font(),
|
|
|
|
)),
|
|
|
|
)?;
|
|
|
|
root.draw(
|
|
|
|
&(EmptyElement::at((0, usable_rows))
|
|
|
|
+ Text::new(
|
|
|
|
format!("{} usable rows", usable_rows),
|
|
|
|
(10, 10),
|
|
|
|
("sans-serif", 15.0).into_font(),
|
|
|
|
)),
|
|
|
|
)?;
|
2021-06-09 16:05:49 -07:00
|
|
|
}
|
2021-06-08 04:06:48 -07:00
|
|
|
Ok(())
|
|
|
|
}
|
2021-01-23 09:54:31 -08:00
|
|
|
}
|
2021-07-20 02:44:04 -07:00
|
|
|
|
|
|
|
/// Renders the given circuit layout to a JSON string.
|
|
|
|
pub fn render_to_json<F: Field, ConcreteCircuit: Circuit<F>>(
|
|
|
|
circuit: &ConcreteCircuit,
|
|
|
|
) -> Result<String, serde_json::Error> {
|
|
|
|
// Collect the layout details.
|
|
|
|
let mut cs = ConstraintSystem::default();
|
|
|
|
let config = ConcreteCircuit::configure(&mut cs);
|
|
|
|
let mut layout = Layout::default();
|
|
|
|
ConcreteCircuit::FloorPlanner::synthesize(&mut layout, circuit, config, cs.constants).unwrap();
|
|
|
|
|
|
|
|
// Render.
|
|
|
|
#[derive(serde::Serialize)]
|
|
|
|
struct Circuit {
|
|
|
|
num_instance_columns: usize,
|
|
|
|
num_advice_columns: usize,
|
|
|
|
num_fixed_columns: usize,
|
|
|
|
total_rows: usize,
|
|
|
|
regions: Vec<LayoutRegion>,
|
|
|
|
loose_cells: Vec<Cell>,
|
|
|
|
selectors: Vec<Vec<bool>>,
|
|
|
|
}
|
|
|
|
serde_json::to_string(&Circuit {
|
|
|
|
num_instance_columns: cs.num_instance_columns,
|
|
|
|
num_advice_columns: cs.num_advice_columns,
|
|
|
|
num_fixed_columns: cs.num_fixed_columns,
|
|
|
|
total_rows: layout.total_rows,
|
|
|
|
regions: layout.regions,
|
|
|
|
loose_cells: layout.loose_cells,
|
|
|
|
selectors: layout.selectors,
|
|
|
|
})
|
|
|
|
}
|