From 84f732acb44b51fb8c850a8327fdeb59cd7382a2 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Sat, 20 Feb 2021 21:19:06 +0800 Subject: [PATCH] Add circuit::RegionIndex and circuit::RegionStart newtypes --- src/circuit.rs | 38 +++++++++++++++++++++++++++++++++++++- src/circuit/layouter.rs | 29 +++++++++++++++-------------- 2 files changed, 52 insertions(+), 15 deletions(-) diff --git a/src/circuit.rs b/src/circuit.rs index 8658ad0..b414211 100644 --- a/src/circuit.rs +++ b/src/circuit.rs @@ -28,11 +28,47 @@ pub trait Chip: Sized { fn load(layouter: &mut impl Layouter) -> Result<(), Error>; } +/// Index of a region in a layouter +#[derive(Clone, Copy, Debug)] +pub struct RegionIndex(usize); + +impl From for RegionIndex { + fn from(idx: usize) -> RegionIndex { + RegionIndex(idx) + } +} + +impl std::ops::Deref for RegionIndex { + type Target = usize; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + +/// Starting row of a region in a layouter +#[derive(Clone, Copy, Debug)] +pub struct RegionStart(usize); + +impl From for RegionStart { + fn from(idx: usize) -> RegionStart { + RegionStart(idx) + } +} + +impl std::ops::Deref for RegionStart { + type Target = usize; + + fn deref(&self) -> &Self::Target { + &self.0 + } +} + /// A pointer to a cell within a circuit. #[derive(Clone, Copy, Debug)] pub struct Cell { /// Identifies the region in which this cell resides. - region_index: usize, + region_index: RegionIndex, row_offset: usize, column: Column, } diff --git a/src/circuit/layouter.rs b/src/circuit/layouter.rs index 88ad9cc..e9c20d3 100644 --- a/src/circuit/layouter.rs +++ b/src/circuit/layouter.rs @@ -5,7 +5,7 @@ use std::collections::{HashMap, HashSet}; use std::fmt; use std::marker::PhantomData; -use super::{Cell, Chip, Layouter, Permutation, Region}; +use super::{Cell, Chip, Layouter, Permutation, Region, RegionIndex, RegionStart}; use crate::plonk::{Advice, Any, Assignment, Column, Error, Fixed}; /// Helper trait for implementing a custom [`Layouter`]. @@ -70,7 +70,8 @@ pub trait RegionLayouter: fmt::Debug { pub struct SingleChip<'a, C: Chip, CS: Assignment + 'a> { cs: &'a mut CS, config: C::Config, - regions: Vec, + /// Stores the starting row for each region. + regions: Vec, /// Stores the first empty row for each column. columns: HashMap, usize>, _marker: PhantomData, @@ -115,7 +116,7 @@ impl<'a, C: Chip, CS: Assignment + 'a> Layouter for SingleChip<'a, let region_index = self.regions.len(); // Get shape of the region. - let mut shape = RegionShape::new(region_index); + let mut shape = RegionShape::new(region_index.into()); { let region: &mut dyn RegionLayouter = &mut shape; assignment(region.into())?; @@ -127,7 +128,7 @@ impl<'a, C: Chip, CS: Assignment + 'a> Layouter for SingleChip<'a, for column in &shape.columns { region_start = cmp::max(region_start, self.columns.get(column).cloned().unwrap_or(0)); } - self.regions.push(region_start); + self.regions.push(region_start.into()); // Update column usage information. for column in shape.columns { @@ -135,7 +136,7 @@ impl<'a, C: Chip, CS: Assignment + 'a> Layouter for SingleChip<'a, } self.cs.enter_region(name); - let mut region = SingleChipRegion::new(self, region_index); + let mut region = SingleChipRegion::new(self, region_index.into()); let result = { let region: &mut dyn RegionLayouter = &mut region; assignment(region.into()) @@ -166,14 +167,14 @@ impl<'a, C: Chip, CS: Assignment + 'a> Layouter for SingleChip<'a, /// the set of columns it uses as well as the number of rows it uses. #[derive(Debug)] pub struct RegionShape { - region_index: usize, + region_index: RegionIndex, columns: HashSet>, row_count: usize, } impl RegionShape { /// Create a new `RegionShape` for a region at `region_index`. - pub fn new(region_index: usize) -> Self { + pub fn new(region_index: RegionIndex) -> Self { RegionShape { region_index, columns: HashSet::default(), @@ -182,7 +183,7 @@ impl RegionShape { } /// Get the `region_index` of a `RegionShape`. - pub fn region_index(&self) -> usize { + pub fn region_index(&self) -> RegionIndex { self.region_index } @@ -245,7 +246,7 @@ impl RegionLayouter for RegionShape { struct SingleChipRegion<'r, 'a, C: Chip, CS: Assignment + 'a> { layouter: &'r mut SingleChip<'a, C, CS>, - region_index: usize, + region_index: RegionIndex, } impl<'r, 'a, C: Chip, CS: Assignment + 'a> fmt::Debug @@ -260,7 +261,7 @@ impl<'r, 'a, C: Chip, CS: Assignment + 'a> fmt::Debug } impl<'r, 'a, C: Chip, CS: Assignment + 'a> SingleChipRegion<'r, 'a, C, CS> { - fn new(layouter: &'r mut SingleChip<'a, C, CS>, region_index: usize) -> Self { + fn new(layouter: &'r mut SingleChip<'a, C, CS>, region_index: RegionIndex) -> Self { SingleChipRegion { layouter, region_index, @@ -281,7 +282,7 @@ impl<'r, 'a, C: Chip, CS: Assignment + 'a> RegionLayouter self.layouter.cs.assign_advice( annotation, column, - self.layouter.regions[self.region_index] + offset, + *self.layouter.regions[*self.region_index] + offset, to, )?; @@ -302,7 +303,7 @@ impl<'r, 'a, C: Chip, CS: Assignment + 'a> RegionLayouter self.layouter.cs.assign_fixed( annotation, column, - self.layouter.regions[self.region_index] + offset, + *self.layouter.regions[*self.region_index] + offset, to, )?; @@ -333,9 +334,9 @@ impl<'r, 'a, C: Chip, CS: Assignment + 'a> RegionLayouter self.layouter.cs.copy( permutation.index, left_column, - self.layouter.regions[left.region_index] + left.row_offset, + *self.layouter.regions[*left.region_index] + left.row_offset, right_column, - self.layouter.regions[right.region_index] + right.row_offset, + *self.layouter.regions[*right.region_index] + right.row_offset, )?; Ok(())