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::{
arithmetic::FieldExt,
circuit::{layouter::SingleChip, Cell, Chip, Layouter, Permutation},
circuit::{layouter::SingleChip, Cell, Chip, Layouter},
dev::VerifyFailure,
plonk::{Advice, Assignment, Circuit, Column, ConstraintSystem, Error, Fixed, Instance},
plonk::{
Advice, Assignment, Circuit, Column, ConstraintSystem, Error, Fixed, Instance, Permutation,
},
poly::Rotation,
};

View File

@ -4,7 +4,7 @@ use std::{fmt, marker::PhantomData};
use crate::{
arithmetic::FieldExt,
plonk::{Advice, Any, Column, ConstraintSystem, Error, Fixed},
plonk::{Advice, Any, Column, Error, Fixed, Permutation},
};
pub mod layouter;
@ -73,24 +73,6 @@ pub struct Cell {
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.
///
/// 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::marker::PhantomData;
use super::{Cell, Chip, Layouter, Permutation, Region, RegionIndex, RegionStart};
use crate::plonk::{Advice, Any, Assignment, Column, Error, Fixed};
use super::{Cell, Chip, Layouter, Region, RegionIndex, RegionStart};
use crate::plonk::{Advice, Any, Assignment, Column, Error, Fixed, Permutation};
/// 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,
) -> Result<(), Error> {
let left_column = permutation
.mapping
.mapping()
.iter()
.position(|c| c == &left.column)
.ok_or(Error::SynthesisError)?;
let right_column = permutation
.mapping
.mapping()
.iter()
.position(|c| c == &right.column)
.ok_or(Error::SynthesisError)?;
self.layouter.cs.copy(
permutation.index,
permutation.index(),
left_column,
*self.layouter.regions[*left.region_index] + left.row_offset,
right_column,

View File

@ -7,6 +7,7 @@ use std::{
};
use super::{lookup, permutation, Error};
use crate::arithmetic::FieldExt;
use crate::poly::Rotation;
/// 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
/// for a constraint system.
pub trait Assignment<F: Field> {