2021-01-23 09:54:31 -08:00
|
|
|
use ff::Field;
|
|
|
|
use plotters::{
|
|
|
|
coord::Shift,
|
|
|
|
prelude::{DrawingArea, DrawingAreaErrorKind, DrawingBackend},
|
|
|
|
};
|
|
|
|
use std::cmp;
|
|
|
|
use std::collections::HashSet;
|
2021-06-08 04:11:21 -07:00
|
|
|
use std::ops::Range;
|
2021-01-23 09:54:31 -08:00
|
|
|
|
2021-07-21 14:45:36 -07:00
|
|
|
use crate::circuit::layouter::RegionColumn;
|
2021-02-18 23:24:21 -08:00
|
|
|
use crate::plonk::{
|
2021-06-11 08:39:34 -07:00
|
|
|
Advice, Any, Assigned, Assignment, Circuit, Column, ConstraintSystem, Error, Fixed,
|
2021-07-08 13:44:01 -07:00
|
|
|
FloorPlanner, Instance, Selector,
|
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)],
|
2021-06-08 04:06:48 -07:00
|
|
|
&BLACK,
|
|
|
|
))?;
|
|
|
|
|
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(),
|
|
|
|
))?;
|
|
|
|
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 {
|
|
|
|
for (column, row) in region.cells {
|
|
|
|
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.
|
|
|
|
for (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-16 07:01:55 -07:00
|
|
|
for (l_col, l_row, r_col, r_row) in &layout.equality {
|
2021-07-21 14:45:36 -07:00
|
|
|
let l_col = column_index(&cs, (*l_col).into());
|
|
|
|
let r_col = column_index(&cs, (*r_col).into());
|
2021-07-16 15:00:49 -07:00
|
|
|
|
|
|
|
// Deduplicate cells.
|
|
|
|
cells.insert((l_col, *l_row));
|
|
|
|
cells.insert((r_col, *r_row));
|
|
|
|
}
|
|
|
|
|
|
|
|
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 {
|
|
|
|
for (l_col, l_row, r_col, r_row) in &layout.equality {
|
2021-07-21 14:45:36 -07:00
|
|
|
let l_col = column_index(&cs, (*l_col).into());
|
|
|
|
let r_col = column_index(&cs, (*r_col).into());
|
2021-07-16 07:01:55 -07:00
|
|
|
root.draw(&PathElement::new(
|
|
|
|
[(l_col, *l_row), (r_col, *r_row)],
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
struct Region {
|
|
|
|
/// The name of the region. Not required to be unique.
|
|
|
|
name: String,
|
|
|
|
/// The columns used by this region.
|
2021-07-21 14:45:36 -07:00
|
|
|
columns: HashSet<RegionColumn>,
|
2021-01-23 09:54:31 -08:00
|
|
|
/// The row that this region starts on, if known.
|
|
|
|
offset: Option<usize>,
|
|
|
|
/// The number of rows that this region takes up.
|
|
|
|
rows: usize,
|
2021-01-29 06:10:25 -08:00
|
|
|
/// The cells assigned in this region. We store this as a `Vec` so that if any cells
|
|
|
|
/// are double-assigned, they will be visibly darker.
|
2021-07-21 14:45:36 -07:00
|
|
|
cells: Vec<(RegionColumn, usize)>,
|
2021-01-23 09:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Default)]
|
|
|
|
struct Layout {
|
2021-11-16 06:20:57 -08:00
|
|
|
k: u32,
|
2021-01-23 09:54:31 -08:00
|
|
|
regions: Vec<Region>,
|
|
|
|
current_region: Option<usize>,
|
|
|
|
total_rows: usize,
|
2021-01-29 06:10:25 -08:00
|
|
|
/// Any cells assigned outside of a region. We store this as a `Vec` so that if any
|
|
|
|
/// cells are double-assigned, they will be visibly darker.
|
2021-07-21 14:45:36 -07:00
|
|
|
loose_cells: Vec<(RegionColumn, usize)>,
|
2021-07-16 07:01:55 -07:00
|
|
|
/// Pairs of cells between which we have equality constraints.
|
|
|
|
equality: Vec<(Column<Any>, usize, Column<Any>, usize)>,
|
2021-07-21 14:45:36 -07:00
|
|
|
/// Selector assignments used for optimization pass
|
|
|
|
selectors: Vec<Vec<bool>>,
|
2021-01-23 09:54:31 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Layout {
|
2021-11-16 06:20:57 -08:00
|
|
|
fn new(k: u32, n: usize, num_selectors: usize) -> Self {
|
2021-07-21 14:45:36 -07:00
|
|
|
Layout {
|
2021-11-16 06:20:57 -08:00
|
|
|
k,
|
2021-07-21 14:45:36 -07:00
|
|
|
regions: vec![],
|
|
|
|
current_region: None,
|
|
|
|
total_rows: 0,
|
|
|
|
/// Any cells assigned outside of a region. We store this as a `Vec` so that if any
|
|
|
|
/// cells are double-assigned, they will be visibly darker.
|
|
|
|
loose_cells: vec![],
|
|
|
|
/// Pairs of cells between which we have equality constraints.
|
|
|
|
equality: vec![],
|
|
|
|
/// Selector assignments used for optimization pass
|
|
|
|
selectors: vec![vec![false; n]; num_selectors],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn update(&mut self, column: RegionColumn, row: usize) {
|
2021-01-23 09:54:31 -08:00
|
|
|
self.total_rows = cmp::max(self.total_rows, row + 1);
|
|
|
|
|
|
|
|
if let Some(region) = self.current_region {
|
|
|
|
let region = &mut self.regions[region];
|
|
|
|
region.columns.insert(column);
|
2021-04-08 19:30:53 -07:00
|
|
|
|
|
|
|
// The region offset is the earliest row assigned to.
|
|
|
|
let mut offset = region.offset.unwrap_or(row);
|
|
|
|
if row < offset {
|
|
|
|
// The first row assigned was not at offset 0 within the region.
|
|
|
|
region.rows += offset - row;
|
|
|
|
offset = row;
|
|
|
|
}
|
|
|
|
// The number of rows in this region is the gap between the earliest and
|
|
|
|
// latest rows assigned.
|
2021-01-23 09:54:31 -08:00
|
|
|
region.rows = cmp::max(region.rows, row - offset + 1);
|
|
|
|
region.offset = Some(offset);
|
2021-04-08 19:30:53 -07:00
|
|
|
|
2021-01-29 06:10:25 -08:00
|
|
|
region.cells.push((column, row));
|
|
|
|
} else {
|
|
|
|
self.loose_cells.push((column, row));
|
2021-01-23 09:54:31 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Field> Assignment<F> for Layout {
|
|
|
|
fn enter_region<NR, N>(&mut self, name_fn: N)
|
|
|
|
where
|
|
|
|
NR: Into<String>,
|
|
|
|
N: FnOnce() -> NR,
|
|
|
|
{
|
|
|
|
assert!(self.current_region.is_none());
|
|
|
|
self.current_region = Some(self.regions.len());
|
|
|
|
self.regions.push(Region {
|
|
|
|
name: name_fn().into(),
|
|
|
|
columns: HashSet::default(),
|
|
|
|
offset: None,
|
|
|
|
rows: 0,
|
2021-01-29 06:10:25 -08:00
|
|
|
cells: vec![],
|
2021-01-23 09:54:31 -08:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_region(&mut self) {
|
|
|
|
assert!(self.current_region.is_some());
|
|
|
|
self.current_region = None;
|
|
|
|
}
|
|
|
|
|
2021-07-21 14:45:36 -07:00
|
|
|
fn enable_selector<A, AR>(&mut self, _: A, selector: &Selector, row: usize) -> Result<(), Error>
|
2021-05-27 04:43:32 -07:00
|
|
|
where
|
|
|
|
A: FnOnce() -> AR,
|
|
|
|
AR: Into<String>,
|
|
|
|
{
|
2021-11-16 06:20:57 -08:00
|
|
|
if let Some(cell) = self.selectors[selector.0].get_mut(row) {
|
|
|
|
*cell = true;
|
|
|
|
} else {
|
2021-11-16 23:54:26 -08:00
|
|
|
return Err(Error::not_enough_rows_available(self.k));
|
2021-11-16 06:20:57 -08:00
|
|
|
}
|
|
|
|
|
2021-07-21 14:45:36 -07:00
|
|
|
self.update((*selector).into(), row);
|
|
|
|
Ok(())
|
2021-05-27 04:43:32 -07:00
|
|
|
}
|
|
|
|
|
2021-07-08 13:44:01 -07:00
|
|
|
fn query_instance(&self, _: Column<Instance>, _: usize) -> Result<Option<F>, Error> {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
|
2021-06-11 08:39:34 -07:00
|
|
|
fn assign_advice<V, VR, A, AR>(
|
2021-01-23 09:54:31 -08:00
|
|
|
&mut self,
|
|
|
|
_: A,
|
|
|
|
column: Column<Advice>,
|
|
|
|
row: usize,
|
|
|
|
_: V,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
2021-06-11 08:39:34 -07:00
|
|
|
V: FnOnce() -> Result<VR, Error>,
|
|
|
|
VR: Into<Assigned<F>>,
|
2021-01-23 09:54:31 -08:00
|
|
|
A: FnOnce() -> AR,
|
|
|
|
AR: Into<String>,
|
|
|
|
{
|
2021-07-21 14:45:36 -07:00
|
|
|
self.update(Column::<Any>::from(column).into(), row);
|
2021-01-23 09:54:31 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-06-11 08:39:34 -07:00
|
|
|
fn assign_fixed<V, VR, A, AR>(
|
2021-01-23 09:54:31 -08:00
|
|
|
&mut self,
|
|
|
|
_: A,
|
|
|
|
column: Column<Fixed>,
|
|
|
|
row: usize,
|
|
|
|
_: V,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
2021-06-11 08:39:34 -07:00
|
|
|
V: FnOnce() -> Result<VR, Error>,
|
|
|
|
VR: Into<Assigned<F>>,
|
2021-01-23 09:54:31 -08:00
|
|
|
A: FnOnce() -> AR,
|
|
|
|
AR: Into<String>,
|
|
|
|
{
|
2021-07-21 14:45:36 -07:00
|
|
|
self.update(Column::<Any>::from(column).into(), row);
|
2021-01-23 09:54:31 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy(
|
|
|
|
&mut self,
|
2021-07-16 07:01:55 -07:00
|
|
|
l_col: Column<Any>,
|
|
|
|
l_row: usize,
|
|
|
|
r_col: Column<Any>,
|
|
|
|
r_row: usize,
|
2021-01-23 09:54:31 -08:00
|
|
|
) -> Result<(), crate::plonk::Error> {
|
2021-07-16 07:01:55 -07:00
|
|
|
self.equality.push((l_col, l_row, r_col, r_row));
|
2021-01-23 09:54:31 -08:00
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-07-26 17:34:51 -07:00
|
|
|
fn fill_from_row(
|
|
|
|
&mut self,
|
|
|
|
_: Column<Fixed>,
|
|
|
|
_: usize,
|
|
|
|
_: Option<Assigned<F>>,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-23 09:54:31 -08:00
|
|
|
fn push_namespace<NR, N>(&mut self, _: N)
|
|
|
|
where
|
|
|
|
NR: Into<String>,
|
|
|
|
N: FnOnce() -> NR,
|
|
|
|
{
|
|
|
|
// Do nothing; we don't care about namespaces in this context.
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pop_namespace(&mut self, _: Option<String>) {
|
|
|
|
// Do nothing; we don't care about namespaces in this context.
|
|
|
|
}
|
|
|
|
}
|