Move Permutation struct from crate::circuit -> plonk::circuit

This commit is contained in:
therealyingtong 2021-02-19 14:36:19 +08:00
parent 20bd44f854
commit 340fb2b6df
4 changed files with 39 additions and 26 deletions

View file

@ -4,9 +4,11 @@ use std::marker::PhantomData;
use halo2::{ use halo2::{
arithmetic::FieldExt, arithmetic::FieldExt,
circuit::{layouter::SingleChip, Cell, Chip, Layouter, Permutation}, circuit::{layouter::SingleChip, Cell, Chip, Layouter},
dev::VerifyFailure, dev::VerifyFailure,
plonk::{Advice, Assignment, Circuit, Column, ConstraintSystem, Error, Fixed, Instance}, plonk::{
Advice, Assignment, Circuit, Column, ConstraintSystem, Error, Fixed, Instance, Permutation,
},
poly::Rotation, poly::Rotation,
}; };

View file

@ -4,7 +4,7 @@ use std::{fmt, marker::PhantomData};
use crate::{ use crate::{
arithmetic::FieldExt, arithmetic::FieldExt,
plonk::{Advice, Any, Column, ConstraintSystem, Error, Fixed}, plonk::{Advice, Any, Column, Error, Fixed, Permutation},
}; };
pub mod layouter; pub mod layouter;
@ -73,24 +73,6 @@ pub struct Cell {
column: Column<Any>, column: Column<Any>,
} }
/// A permutation configured by a chip.
#[derive(Clone, Debug)]
pub struct Permutation {
index: usize,
mapping: Vec<Column<Any>>,
}
impl Permutation {
/// Configures a new permutation for the given columns.
pub fn new<F: FieldExt>(meta: &mut ConstraintSystem<F>, columns: &[Column<Any>]) -> Self {
let index = meta.permutation(columns);
Permutation {
index,
mapping: columns.iter().copied().collect(),
}
}
}
/// A region of the circuit in which a [`Chip`] can assign cells. /// A region of the circuit in which a [`Chip`] can assign cells.
/// ///
/// Inside a region, the chip may freely use relative offsets; the [`Layouter`] will /// Inside a region, the chip may freely use relative offsets; the [`Layouter`] will

View file

@ -5,8 +5,8 @@ use std::collections::{HashMap, HashSet};
use std::fmt; use std::fmt;
use std::marker::PhantomData; use std::marker::PhantomData;
use super::{Cell, Chip, Layouter, Permutation, Region, RegionIndex, RegionStart}; use super::{Cell, Chip, Layouter, Region, RegionIndex, RegionStart};
use crate::plonk::{Advice, Any, Assignment, Column, Error, Fixed}; use crate::plonk::{Advice, Any, Assignment, Column, Error, Fixed, Permutation};
/// Helper trait for implementing a custom [`Layouter`]. /// Helper trait for implementing a custom [`Layouter`].
/// ///
@ -321,18 +321,18 @@ impl<'r, 'a, C: Chip, CS: Assignment<C::Field> + 'a> RegionLayouter<C>
right: Cell, right: Cell,
) -> Result<(), Error> { ) -> Result<(), Error> {
let left_column = permutation let left_column = permutation
.mapping .mapping()
.iter() .iter()
.position(|c| c == &left.column) .position(|c| c == &left.column)
.ok_or(Error::SynthesisError)?; .ok_or(Error::SynthesisError)?;
let right_column = permutation let right_column = permutation
.mapping .mapping()
.iter() .iter()
.position(|c| c == &right.column) .position(|c| c == &right.column)
.ok_or(Error::SynthesisError)?; .ok_or(Error::SynthesisError)?;
self.layouter.cs.copy( self.layouter.cs.copy(
permutation.index, permutation.index(),
left_column, left_column,
*self.layouter.regions[*left.region_index] + left.row_offset, *self.layouter.regions[*left.region_index] + left.row_offset,
right_column, right_column,

View file

@ -7,6 +7,7 @@ use std::{
}; };
use super::{lookup, permutation, Error}; use super::{lookup, permutation, Error};
use crate::arithmetic::FieldExt;
use crate::poly::Rotation; use crate::poly::Rotation;
/// A column type /// A column type
@ -126,6 +127,34 @@ impl TryFrom<Column<Any>> for Column<Instance> {
} }
} }
/// A permutation.
#[derive(Clone, Debug)]
pub struct Permutation {
index: usize,
mapping: Vec<Column<Any>>,
}
impl Permutation {
/// Configures a new permutation for the given columns.
pub fn new<F: FieldExt>(meta: &mut ConstraintSystem<F>, columns: &[Column<Any>]) -> Self {
let index = meta.permutation(columns);
Permutation {
index,
mapping: columns.iter().copied().collect(),
}
}
/// Returns index of permutation
pub fn index(&self) -> usize {
self.index
}
/// Returns mapping of permutation
pub fn mapping(&self) -> &[Column<Any>] {
&self.mapping
}
}
/// This trait allows a [`Circuit`] to direct some backend to assign a witness /// This trait allows a [`Circuit`] to direct some backend to assign a witness
/// for a constraint system. /// for a constraint system.
pub trait Assignment<F: Field> { pub trait Assignment<F: Field> {