halo2/src/circuit/layouter.rs

63 lines
1.9 KiB
Rust
Raw Normal View History

//! Implementations of common circuit layouters.
use std::fmt;
use super::{Cell, Chip, Permutation};
use crate::plonk::{Advice, Column, Error, Fixed};
/// Helper trait for implementing a custom [`Layouter`].
///
/// This trait is used for implementing region assignments:
///
/// ```ignore
/// impl<'a, C: Chip, CS: Assignment<C::Field> + 'a> Layouter<C> for MyLayouter<'a, C, CS> {
/// fn assign_region(
/// &mut self,
/// assignment: impl FnOnce(Region<'_, C>) -> Result<(), Error>,
/// ) -> Result<(), Error> {
/// let region_index = self.regions.len();
/// self.regions.push(self.current_gate);
///
/// let mut region = MyRegion::new(self, region_index);
/// {
/// let region: &mut dyn RegionLayouter<C> = &mut region;
/// assignment(region.into())?;
/// }
/// self.current_gate += region.row_count;
///
/// Ok(())
/// }
/// }
/// ```
///
/// TODO: It would be great if we could constrain the columns in these types to be
/// "logical" columns that are guaranteed to correspond to the chip (and have come from
/// `Chip::Config`).
pub trait RegionLayouter<C: Chip>: fmt::Debug {
/// Assign an advice column value (witness)
fn assign_advice<'v>(
&'v mut self,
column: Column<Advice>,
offset: usize,
to: &'v mut (dyn FnMut() -> Result<C::Field, Error> + 'v),
) -> Result<Cell, Error>;
/// Assign a fixed value
fn assign_fixed<'v>(
&'v mut self,
column: Column<Fixed>,
offset: usize,
to: &'v mut (dyn FnMut() -> Result<C::Field, Error> + 'v),
) -> Result<Cell, Error>;
/// Constraint two cells to have the same value.
///
/// Returns an error if either of the cells is not within the given permutation.
fn constrain_equal(
&mut self,
permutation: &Permutation,
left: Cell,
right: Cell,
) -> Result<(), Error>;
}