diff --git a/halo2_backend/src/plonk/circuit.rs b/halo2_backend/src/plonk/circuit.rs index 30de9d3ff5..2e5cc9ebd1 100644 --- a/halo2_backend/src/plonk/circuit.rs +++ b/halo2_backend/src/plonk/circuit.rs @@ -181,7 +181,7 @@ impl ConstraintSystemBack { pub fn get_any_query_index(&self, column: ColumnMid, at: Rotation) -> usize { let queries = match column.column_type { - Any::Advice(_) => &self.advice_queries, + Any::Advice => &self.advice_queries, Any::Fixed => &self.fixed_queries, Any::Instance => &self.instance_queries, }; diff --git a/halo2_backend/src/plonk/evaluation.rs b/halo2_backend/src/plonk/evaluation.rs index d3c911f906..e1f85163af 100644 --- a/halo2_backend/src/plonk/evaluation.rs +++ b/halo2_backend/src/plonk/evaluation.rs @@ -456,7 +456,7 @@ impl Evaluator { for (values, permutation) in columns .iter() .map(|&column| match column.column_type { - Any::Advice(_) => &advice[column.index], + Any::Advice => &advice[column.index], Any::Fixed => &fixed[column.index], Any::Instance => &instance[column.index], }) @@ -467,7 +467,7 @@ impl Evaluator { let mut right = set.permutation_product_coset[idx]; for values in columns.iter().map(|&column| match column.column_type { - Any::Advice(_) => &advice[column.index], + Any::Advice => &advice[column.index], Any::Fixed => &fixed[column.index], Any::Instance => &instance[column.index], }) { @@ -698,9 +698,10 @@ impl GraphEvaluator { query.column_index, rot_idx, ))), - Any::Advice(_) => self.add_calculation(Calculation::Store( - ValueSource::Advice(query.column_index, rot_idx), - )), + Any::Advice => self.add_calculation(Calculation::Store(ValueSource::Advice( + query.column_index, + rot_idx, + ))), Any::Instance => self.add_calculation(Calculation::Store( ValueSource::Instance(query.column_index, rot_idx), )), @@ -867,7 +868,7 @@ pub fn evaluate( let rot_idx = get_rotation_idx(idx, query.rotation.0, rot_scale, isize); match query.column_type { Any::Fixed => fixed[query.column_index][rot_idx], - Any::Advice(_) => advice[query.column_index][rot_idx], + Any::Advice => advice[query.column_index][rot_idx], Any::Instance => instance[query.column_index][rot_idx], } } @@ -886,7 +887,7 @@ pub fn evaluate( mod test { use crate::plonk::circuit::{ExpressionBack, QueryBack, VarBack}; use crate::poly::LagrangeCoeff; - use halo2_middleware::circuit::{Advice, Any, ChallengeMid}; + use halo2_middleware::circuit::{Any, ChallengeMid}; use halo2_middleware::poly::Rotation; use halo2curves::pasta::pallas::{Affine, Scalar}; @@ -969,7 +970,7 @@ mod test { ExpressionBack::Var(Query(QueryBack { index: 0, column_index: col, - column_type: Any::Advice(Advice { phase: 0 }), + column_type: Any::Advice, rotation: Rotation(rot), })), expected, diff --git a/halo2_backend/src/plonk/keygen.rs b/halo2_backend/src/plonk/keygen.rs index 8bace825d0..27bc653dc2 100644 --- a/halo2_backend/src/plonk/keygen.rs +++ b/halo2_backend/src/plonk/keygen.rs @@ -198,7 +198,7 @@ impl QueriesMap { .map .entry((col, rot)) .or_insert_with(|| match col.column_type { - Any::Advice(_) => { + Any::Advice => { self.advice.push((col, rot)); self.advice.len() - 1 } @@ -219,7 +219,7 @@ impl QueriesMap { match expr { ExpressionMid::Constant(c) => ExpressionBack::Constant(*c), ExpressionMid::Var(VarMid::Query(query)) => { - let column = ColumnMid::new(query.column_index, query.column_type); + let column = ColumnMid::new(query.column_type, query.column_index); let index = self.add(column, query.rotation); ExpressionBack::Var(VarBack::Query(QueryBack { index, @@ -336,16 +336,7 @@ fn collect_queries( // Each column used in a copy constraint involves a query at rotation current. for column in &cs_mid.permutation.columns { - match column.column_type { - Any::Instance => { - queries.add(ColumnMid::new(column.index, Any::Instance), Rotation::cur()) - } - Any::Fixed => queries.add(ColumnMid::new(column.index, Any::Fixed), Rotation::cur()), - Any::Advice(advice) => queries.add( - ColumnMid::new(column.index, Any::Advice(advice)), - Rotation::cur(), - ), - }; + queries.add(*column, Rotation::cur()); } let mut num_advice_queries = vec![0; cs_mid.num_advice_columns]; diff --git a/halo2_backend/src/plonk/lookup/verifier.rs b/halo2_backend/src/plonk/lookup/verifier.rs index 2d8759011a..f0342f80f8 100644 --- a/halo2_backend/src/plonk/lookup/verifier.rs +++ b/halo2_backend/src/plonk/lookup/verifier.rs @@ -123,7 +123,7 @@ impl Evaluated { index, column_type, .. }) => match column_type { Any::Fixed => fixed_evals[index], - Any::Advice(_) => advice_evals[index], + Any::Advice => advice_evals[index], Any::Instance => instance_evals[index], }, }, diff --git a/halo2_backend/src/plonk/permutation/prover.rs b/halo2_backend/src/plonk/permutation/prover.rs index 01565efbbc..3b9b139812 100644 --- a/halo2_backend/src/plonk/permutation/prover.rs +++ b/halo2_backend/src/plonk/permutation/prover.rs @@ -102,7 +102,7 @@ pub(in crate::plonk) fn permutation_commit< // Iterate over each column of the permutation for (&column, permuted_column_values) in columns.iter().zip(permutations.iter()) { let values = match column.column_type { - Any::Advice(_) => advice, + Any::Advice => advice, Any::Fixed => fixed, Any::Instance => instance, }; @@ -125,7 +125,7 @@ pub(in crate::plonk) fn permutation_commit< for &column in columns.iter() { let omega = domain.get_omega(); let values = match column.column_type { - Any::Advice(_) => advice, + Any::Advice => advice, Any::Fixed => fixed, Any::Instance => instance, }; diff --git a/halo2_backend/src/plonk/permutation/verifier.rs b/halo2_backend/src/plonk/permutation/verifier.rs index d7ad3b87ae..6fead2ffc7 100644 --- a/halo2_backend/src/plonk/permutation/verifier.rs +++ b/halo2_backend/src/plonk/permutation/verifier.rs @@ -160,7 +160,7 @@ impl Evaluated { for (eval, permutation_eval) in columns .iter() .map(|&column| match column.column_type { - Any::Advice(_) => { + Any::Advice => { advice_evals[vk.cs.get_any_query_index(column, Rotation::cur())] } Any::Fixed => { @@ -181,7 +181,7 @@ impl Evaluated { * (::DELTA .pow_vartime([(chunk_index * chunk_len) as u64])); for eval in columns.iter().map(|&column| match column.column_type { - Any::Advice(_) => { + Any::Advice => { advice_evals[vk.cs.get_any_query_index(column, Rotation::cur())] } Any::Fixed => { diff --git a/halo2_backend/src/plonk/shuffle/verifier.rs b/halo2_backend/src/plonk/shuffle/verifier.rs index 44dd32f386..9c7623d99d 100644 --- a/halo2_backend/src/plonk/shuffle/verifier.rs +++ b/halo2_backend/src/plonk/shuffle/verifier.rs @@ -82,7 +82,7 @@ impl Evaluated { index, column_type, .. }) => match column_type { Any::Fixed => fixed_evals[index], - Any::Advice(_) => advice_evals[index], + Any::Advice => advice_evals[index], Any::Instance => instance_evals[index], }, }, diff --git a/halo2_backend/src/plonk/verifier.rs b/halo2_backend/src/plonk/verifier.rs index 490d517598..9bca96089a 100644 --- a/halo2_backend/src/plonk/verifier.rs +++ b/halo2_backend/src/plonk/verifier.rs @@ -363,7 +363,7 @@ where &|var| match var { VarBack::Query(query) => match query.column_type { Any::Fixed => fixed_evals[query.index], - Any::Advice(_) => advice_evals[query.index], + Any::Advice => advice_evals[query.index], Any::Instance => instance_evals[query.index], }, VarBack::Challenge(challenge) => challenges[challenge.index], diff --git a/halo2_frontend/src/circuit.rs b/halo2_frontend/src/circuit.rs index fdea279e92..f485a434cb 100644 --- a/halo2_frontend/src/circuit.rs +++ b/halo2_frontend/src/circuit.rs @@ -4,10 +4,10 @@ use crate::plonk; use crate::plonk::{ permutation, sealed::{self, SealedPhase}, - Assignment, Circuit, ConstraintSystem, FirstPhase, FloorPlanner, SecondPhase, SelectorsToFixed, - ThirdPhase, + Advice, Assignment, Circuit, ConstraintSystem, FirstPhase, Fixed, FloorPlanner, Instance, + SecondPhase, SelectorsToFixed, ThirdPhase, }; -use halo2_middleware::circuit::{Advice, Any, CompiledCircuitV2, Fixed, Instance, PreprocessingV2}; +use halo2_middleware::circuit::{Any, CompiledCircuitV2, PreprocessingV2}; use halo2_middleware::ff::{BatchInvert, Field}; use std::collections::BTreeSet; use std::collections::HashMap; @@ -130,14 +130,13 @@ pub fn compile_circuit>( } pub struct WitnessCollection<'a, F: Field> { - pub k: u32, - pub current_phase: sealed::Phase, - pub advice: Vec>>, - // pub unblinded_advice: HashSet, - pub challenges: &'a HashMap, - pub instances: &'a [&'a [F]], - pub usable_rows: RangeTo, - pub _marker: std::marker::PhantomData, + k: u32, + current_phase: sealed::Phase, + advice_column_phase: &'a Vec, + advice: Vec>>, + challenges: &'a HashMap, + instances: &'a [&'a [F]], + usable_rows: RangeTo, } impl<'a, F: Field> Assignment for WitnessCollection<'a, F> { @@ -197,7 +196,8 @@ impl<'a, F: Field> Assignment for WitnessCollection<'a, F> { AR: Into, { // Ignore assignment of advice column in different phase than current one. - if self.current_phase.0 != column.column_type().phase { + let phase = self.advice_column_phase[column.index]; + if self.current_phase != phase { return Ok(()); } @@ -326,6 +326,7 @@ impl<'a, F: Field, ConcreteCircuit: Circuit> WitnessCalculator<'a, F, Concret let mut witness = WitnessCollection { k: self.k, current_phase, + advice_column_phase: &self.cs.advice_column_phase, advice: vec![vec![Assigned::Zero; self.n]; self.cs.num_advice_columns], instances: self.instances, challenges, @@ -334,7 +335,6 @@ impl<'a, F: Field, ConcreteCircuit: Circuit> WitnessCalculator<'a, F, Concret // number of blinding factors and an extra row for use in the // permutation argument. usable_rows: ..self.unusable_rows_start, - _marker: std::marker::PhantomData, }; // Synthesize the circuit to obtain the witness and other information. diff --git a/halo2_frontend/src/circuit/floor_planner/single_pass.rs b/halo2_frontend/src/circuit/floor_planner/single_pass.rs index 5845c392b6..9a37c56e1c 100644 --- a/halo2_frontend/src/circuit/floor_planner/single_pass.rs +++ b/halo2_frontend/src/circuit/floor_planner/single_pass.rs @@ -3,6 +3,7 @@ use std::collections::HashMap; use std::fmt; use std::marker::PhantomData; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; use crate::plonk::Assigned; @@ -12,9 +13,11 @@ use crate::{ table_layouter::{compute_table_lengths, SimpleTableLayouter}, Cell, Column, Layouter, Region, RegionIndex, RegionStart, Table, Value, }, - plonk::{Assignment, Challenge, Circuit, Error, FloorPlanner, Selector, TableColumn}, + plonk::{ + Advice, Assignment, Challenge, Circuit, Error, Fixed, FloorPlanner, Instance, Selector, + TableColumn, + }, }; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; /// A simple [`FloorPlanner`] that performs minimal optimizations. /// @@ -381,8 +384,7 @@ mod tests { use super::SimpleFloorPlanner; use crate::dev::MockProver; - use crate::plonk::{Circuit, Column, ConstraintSystem, Error}; - use halo2_middleware::circuit::Advice; + use crate::plonk::{Advice, Circuit, Column, ConstraintSystem, Error}; #[test] fn not_enough_columns_for_constants() { diff --git a/halo2_frontend/src/circuit/floor_planner/v1.rs b/halo2_frontend/src/circuit/floor_planner/v1.rs index 9cd9403524..7b62e78ca0 100644 --- a/halo2_frontend/src/circuit/floor_planner/v1.rs +++ b/halo2_frontend/src/circuit/floor_planner/v1.rs @@ -1,5 +1,6 @@ use std::fmt; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; use crate::plonk::Assigned; @@ -9,9 +10,11 @@ use crate::{ table_layouter::{compute_table_lengths, SimpleTableLayouter}, Cell, Column, Layouter, Region, RegionIndex, RegionStart, Table, Value, }, - plonk::{Assignment, Challenge, Circuit, Error, FloorPlanner, Selector, TableColumn}, + plonk::{ + Advice, Assignment, Challenge, Circuit, Error, Fixed, FloorPlanner, Instance, Selector, + TableColumn, + }, }; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; pub mod strategy; @@ -496,8 +499,7 @@ mod tests { use halo2curves::pasta::vesta; use crate::dev::MockProver; - use crate::plonk::{Circuit, Column, ConstraintSystem, Error}; - use halo2_middleware::circuit::Advice; + use crate::plonk::{Advice, Circuit, Column, ConstraintSystem, Error}; #[test] fn not_enough_columns_for_constants() { diff --git a/halo2_frontend/src/circuit/floor_planner/v1/strategy.rs b/halo2_frontend/src/circuit/floor_planner/v1/strategy.rs index 2a4ce92a22..b78721f501 100644 --- a/halo2_frontend/src/circuit/floor_planner/v1/strategy.rs +++ b/halo2_frontend/src/circuit/floor_planner/v1/strategy.rs @@ -206,7 +206,7 @@ pub fn slot_in_biggest_advice_first( .columns() .iter() .filter(|c| match c { - RegionColumn::Column(c) => matches!(c.column_type(), Any::Advice(_)), + RegionColumn::Column(c) => matches!(c.column_type(), Any::Advice), _ => false, }) .count(); @@ -252,7 +252,7 @@ fn test_slot_in() { let regions = vec![ RegionShape { region_index: 0.into(), - columns: vec![Column::new(0, Any::advice()), Column::new(1, Any::advice())] + columns: vec![Column::new(0, Any::Advice), Column::new(1, Any::Advice)] .into_iter() .map(|a| a.into()) .collect(), @@ -260,7 +260,7 @@ fn test_slot_in() { }, RegionShape { region_index: 1.into(), - columns: vec![Column::new(2, Any::advice())] + columns: vec![Column::new(2, Any::Advice)] .into_iter() .map(|a| a.into()) .collect(), @@ -268,7 +268,7 @@ fn test_slot_in() { }, RegionShape { region_index: 2.into(), - columns: vec![Column::new(2, Any::advice()), Column::new(0, Any::advice())] + columns: vec![Column::new(2, Any::Advice), Column::new(0, Any::Advice)] .into_iter() .map(|a| a.into()) .collect(), diff --git a/halo2_frontend/src/circuit/layouter.rs b/halo2_frontend/src/circuit/layouter.rs index 1ceda1ae30..80625676cd 100644 --- a/halo2_frontend/src/circuit/layouter.rs +++ b/halo2_frontend/src/circuit/layouter.rs @@ -4,13 +4,13 @@ use std::cmp; use std::collections::HashSet; use std::fmt; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; pub use super::table_layouter::TableLayouter; use super::{Cell, RegionIndex, Value}; use crate::plonk::Assigned; -use crate::plonk::{Column, Error, Selector}; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; +use crate::plonk::{Advice, Column, Error, Fixed, Instance, Selector}; /// Intermediate trait requirements for [`RegionLayouter`] when thread-safe regions are enabled. #[cfg(feature = "thread-safe-region")] diff --git a/halo2_frontend/src/dev.rs b/halo2_frontend/src/dev.rs index c83a3b9c91..932542986f 100644 --- a/halo2_frontend/src/dev.rs +++ b/halo2_frontend/src/dev.rs @@ -12,14 +12,14 @@ use crate::{ plonk::{ permutation, sealed::{self, SealedPhase}, - Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, Expression, - FirstPhase, FloorPlanner, Phase, Selector, + Advice, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, + Expression, FirstPhase, Fixed, FloorPlanner, Instance, Phase, Selector, }, }; use halo2_common::multicore::{ IntoParallelIterator, IntoParallelRefIterator, ParallelIterator, ParallelSliceMut, }; -use halo2_middleware::circuit::{Advice, Any, ColumnMid, Fixed, Instance}; +use halo2_middleware::circuit::{Any, ColumnMid}; use halo2_middleware::ff::{Field, FromUniformBytes}; pub mod metadata; @@ -212,9 +212,9 @@ impl Mul for Value { /// use halo2_frontend::{ /// circuit::{Layouter, SimpleFloorPlanner, Value}, /// dev::{FailureLocation, MockProver, VerifyFailure}, -/// plonk::{circuit::Column, Circuit, ConstraintSystem, Error, Selector}, +/// plonk::{circuit::Column, Circuit, ConstraintSystem, Error, Advice, Selector}, /// }; -/// use halo2_middleware::circuit::{Advice, Any}; +/// use halo2_middleware::circuit::{Any, ColumnMid}; /// use halo2_middleware::poly::Rotation; /// use halo2_middleware::ff::PrimeField; /// use halo2curves::pasta::Fp; @@ -299,9 +299,9 @@ impl Mul for Value { /// offset: 0, /// }, /// cell_values: vec![ -/// (((Any::advice(), 0).into(), 0).into(), "0x2".to_string()), -/// (((Any::advice(), 1).into(), 0).into(), "0x4".to_string()), -/// (((Any::advice(), 2).into(), 0).into(), "0x8".to_string()), +/// ((ColumnMid::new(Any::Advice, 0), 0).into(), "0x2".to_string()), +/// ((ColumnMid::new(Any::Advice, 1), 0).into(), "0x4".to_string()), +/// ((ColumnMid::new(Any::Advice, 2), 0).into(), "0x8".to_string()), /// ], /// }]) /// ); @@ -513,7 +513,8 @@ impl Assignment for MockProver { } Err(err) => { // Propagate `assign` error if the column is in current phase. - if self.in_phase(sealed::Phase(column.column_type().phase)) { + let phase = self.cs.advice_column_phase[column.index]; + if self.in_phase(phase) { return Err(err); } } @@ -1144,9 +1145,9 @@ impl + Ord> MockProver { // Check that permutations preserve the original values of the cells. // Original values of columns involved in the permutation. let original = |column: ColumnMid, row: usize| match column.column_type { - Any::Advice(_) => self.advice[column.index][row], - Any::Fixed => self.fixed[column.index][row], - Any::Instance => { + halo2_middleware::circuit::Any::Advice => self.advice[column.index][row], + halo2_middleware::circuit::Any::Fixed => self.fixed[column.index][row], + halo2_middleware::circuit::Any::Instance => { let cell: &InstanceValue = &self.instance[column.index][row]; CellValue::Assigned(cell.value()) } @@ -1161,7 +1162,7 @@ impl + Ord> MockProver { None } else { Some(VerifyFailure::Permutation { - column: cell_a.column.into(), + column: cell_a.column, location: FailureLocation::find( &self.regions, cell_a.row, @@ -1287,9 +1288,10 @@ mod tests { use super::{FailureLocation, MockProver, VerifyFailure}; use crate::circuit::{Layouter, SimpleFloorPlanner, Value}; use crate::plonk::{ - Circuit, Column, ConstraintSystem, Error, Expression, Selector, TableColumn, + Advice, Circuit, Column, ConstraintSystem, Error, Expression, Fixed, Instance, Selector, + TableColumn, }; - use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; + use halo2_middleware::circuit::{Any, ColumnMid}; use halo2_middleware::poly::Rotation; #[test] @@ -1368,7 +1370,7 @@ mod tests { gate: (0, "Equality check").into(), region: (0, "Faulty synthesis".to_owned()).into(), gate_offset: 1, - column: Column::new(1, Any::Advice(Advice { phase: 0 })), + column: Column::new(1, Any::Advice), offset: 1, }]) ); @@ -1815,19 +1817,13 @@ mod tests { offset: 0, }, cell_values: vec![ + ((ColumnMid::new(Any::Advice, 0), 0).into(), "1".to_string()), + ((ColumnMid::new(Any::Advice, 1), 0).into(), "0".to_string()), ( - ((Any::Advice(Advice { phase: 0 }), 0).into(), 0).into(), - "1".to_string() - ), - ( - ((Any::Advice(Advice { phase: 0 }), 1).into(), 0).into(), - "0".to_string() - ), - ( - ((Any::Advice(Advice { phase: 0 }), 2).into(), 0).into(), + (ColumnMid::new(Any::Advice, 2), 0).into(), "0x5".to_string() ), - (((Any::Fixed, 0).into(), 0).into(), "0x7".to_string()), + ((ColumnMid::new(Any::Fixed, 0), 0).into(), "0x7".to_string()), ], },]) ) diff --git a/halo2_frontend/src/dev/cost.rs b/halo2_frontend/src/dev/cost.rs index 992bdf7e21..7d1b70ac1c 100644 --- a/halo2_frontend/src/dev/cost.rs +++ b/halo2_frontend/src/dev/cost.rs @@ -9,17 +9,17 @@ use std::{ }; use group::prime::PrimeGroup; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::{Field, PrimeField}; use halo2_middleware::poly::Rotation; use crate::{ circuit::{layouter::RegionColumn, Value}, plonk::{ - Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, FloorPlanner, - Selector, + Advice, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, Fixed, + FloorPlanner, Instance, Selector, }, }; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; /// Measures a circuit to determine its costs, and explain what contributes to them. #[allow(dead_code)] @@ -118,9 +118,7 @@ impl Layout { if let RegionColumn::Column(col) = column { match col.column_type() { - Any::Advice(_) => { - self.total_advice_rows = cmp::max(self.total_advice_rows, row + 1) - } + Any::Advice => self.total_advice_rows = cmp::max(self.total_advice_rows, row + 1), Any::Fixed => self.total_fixed_rows = cmp::max(self.total_fixed_rows, row + 1), _ => {} } diff --git a/halo2_frontend/src/dev/failure.rs b/halo2_frontend/src/dev/failure.rs index 381792827b..f7c663381c 100644 --- a/halo2_frontend/src/dev/failure.rs +++ b/halo2_frontend/src/dev/failure.rs @@ -16,7 +16,7 @@ use crate::plonk::{ circuit::expression::{Column, Expression}, ConstraintSystem, Gate, }; -use halo2_middleware::circuit::Any; +use halo2_middleware::circuit::{Any, ColumnMid}; mod emitter; @@ -53,7 +53,7 @@ impl FailureLocation { /// Returns a `DebugColumn` from Column metadata and `&self`. pub(super) fn get_debug_column( &self, - metadata: halo2_middleware::metadata::Column, + metadata: halo2_middleware::circuit::ColumnMid, ) -> DebugColumn { match self { Self::InRegion { region, .. } => { @@ -535,7 +535,7 @@ fn render_lookup( prover .cs .general_column_annotations - .get(&metadata::Column::from((Any::Fixed, query.column_index))) + .get(&ColumnMid::new(Any::Fixed, query.column_index)) .cloned() .unwrap_or_else(|| format!("F{}", query.column_index())) ) @@ -546,7 +546,7 @@ fn render_lookup( prover .cs .general_column_annotations - .get(&metadata::Column::from((Any::advice(), query.column_index))) + .get(&ColumnMid::new(Any::Advice, query.column_index)) .cloned() .unwrap_or_else(|| format!("A{}", query.column_index())) ) @@ -557,7 +557,7 @@ fn render_lookup( prover .cs .general_column_annotations - .get(&metadata::Column::from((Any::Instance, query.column_index))) + .get(&ColumnMid::new(Any::Instance, query.column_index)) .cloned() .unwrap_or_else(|| format!("I{}", query.column_index())) ) @@ -581,7 +581,7 @@ fn render_lookup( .. } = query.into(); Some(( - ((column_type, column_index).into(), rotation.0).into(), + (ColumnMid::new(column_type, column_index), rotation.0).into(), match load(query) { Value::Real(v) => util::format_value(v), Value::Poison => unreachable!(), @@ -701,7 +701,7 @@ fn render_shuffle( prover .cs .general_column_annotations - .get(&metadata::Column::from((Any::Fixed, query.column_index))) + .get(&ColumnMid::new(Any::Fixed, query.column_index)) .cloned() .unwrap_or_else(|| format!("F{}", query.column_index())) ) @@ -712,7 +712,7 @@ fn render_shuffle( prover .cs .general_column_annotations - .get(&metadata::Column::from((Any::advice(), query.column_index))) + .get(&ColumnMid::new(Any::Advice, query.column_index)) .cloned() .unwrap_or_else(|| format!("A{}", query.column_index())) ) @@ -723,7 +723,7 @@ fn render_shuffle( prover .cs .general_column_annotations - .get(&metadata::Column::from((Any::Instance, query.column_index))) + .get(&ColumnMid::new(Any::Instance, query.column_index)) .cloned() .unwrap_or_else(|| format!("I{}", query.column_index())) ) @@ -747,7 +747,7 @@ fn render_shuffle( .. } = query.into(); Some(( - ((column_type, column_index).into(), rotation.0).into(), + (ColumnMid::new(column_type, column_index), rotation.0).into(), match load(query) { Value::Real(v) => util::format_value(v), Value::Poison => unreachable!(), diff --git a/halo2_frontend/src/dev/failure/emitter.rs b/halo2_frontend/src/dev/failure/emitter.rs index 6768925f10..fe63c475d1 100644 --- a/halo2_frontend/src/dev/failure/emitter.rs +++ b/halo2_frontend/src/dev/failure/emitter.rs @@ -6,7 +6,7 @@ use group::ff::Field; use super::FailureLocation; use crate::dev::{metadata, util}; use crate::plonk::circuit::expression::Expression; -use halo2_middleware::circuit::{Advice, Any}; +use halo2_middleware::circuit::{Any, ColumnMid}; fn padded(p: char, width: usize, text: &str) -> String { let pad = width - text.len(); @@ -23,7 +23,7 @@ fn column_type_and_idx(column: &metadata::Column) -> String { format!( "{}{}", match column.column_type { - Any::Advice(_) => "A", + Any::Advice => "A", Any::Fixed => "F", Any::Instance => "I", }, @@ -147,7 +147,7 @@ pub(super) fn expression_to_string( &|query| { if let Some(label) = layout .get(&query.rotation.0) - .and_then(|row| row.get(&(Any::Fixed, query.column_index).into())) + .and_then(|row| row.get(&ColumnMid::new(Any::Fixed, query.column_index))) { label.clone() } else if query.rotation.0 == 0 { @@ -161,17 +161,7 @@ pub(super) fn expression_to_string( &|query| { layout .get(&query.rotation.0) - .and_then(|map| { - map.get( - &( - Any::Advice(Advice { - phase: query.phase.0, - }), - query.column_index, - ) - .into(), - ) - }) + .and_then(|map| map.get(&ColumnMid::new(Any::Advice, query.column_index))) .cloned() .unwrap_or_default() }, @@ -179,7 +169,7 @@ pub(super) fn expression_to_string( layout .get(&query.rotation.0) .unwrap() - .get(&(Any::Instance, query.column_index).into()) + .get(&ColumnMid::new(Any::Instance, query.column_index)) .unwrap() .clone() }, diff --git a/halo2_frontend/src/dev/gates.rs b/halo2_frontend/src/dev/gates.rs index 9c911b7ed1..5c48bbed4c 100644 --- a/halo2_frontend/src/dev/gates.rs +++ b/halo2_frontend/src/dev/gates.rs @@ -129,14 +129,13 @@ impl CircuitGates { &|selector| format!("S{}", selector.0), &|query| format!("F{}@{}", query.column_index, query.rotation.0), &|query| { - if query.phase == FirstPhase.to_sealed() { + let phase = cs.advice_column_phase[query.column_index]; + if phase == FirstPhase.to_sealed() { format!("A{}@{}", query.column_index, query.rotation.0) } else { format!( "A{}({})@{}", - query.column_index, - query.phase(), - query.rotation.0 + query.column_index, phase.0, query.rotation.0 ) } }, @@ -179,14 +178,13 @@ impl CircuitGates { .collect() }, &|query| { - let query = if query.phase == FirstPhase.to_sealed() { + let phase = cs.advice_column_phase[query.column_index]; + let query = if phase == FirstPhase.to_sealed() { format!("A{}@{}", query.column_index, query.rotation.0) } else { format!( "A{}({})@{}", - query.column_index, - query.phase(), - query.rotation.0 + query.column_index, phase.0, query.rotation.0 ) }; vec![query].into_iter().collect() diff --git a/halo2_frontend/src/dev/graph.rs b/halo2_frontend/src/dev/graph.rs index 6ab2e779c7..13baca9bbd 100644 --- a/halo2_frontend/src/dev/graph.rs +++ b/halo2_frontend/src/dev/graph.rs @@ -1,8 +1,8 @@ use crate::plonk::{ - Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, FloorPlanner, - Selector, + Advice, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, Fixed, + FloorPlanner, Instance, Selector, }; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; use tabbycat::{AttrList, Edge, GraphBuilder, GraphType, Identity, StmtList}; diff --git a/halo2_frontend/src/dev/graph/layout.rs b/halo2_frontend/src/dev/graph/layout.rs index 887bd3bd87..3520a2cc4a 100644 --- a/halo2_frontend/src/dev/graph/layout.rs +++ b/halo2_frontend/src/dev/graph/layout.rs @@ -119,7 +119,7 @@ impl CircuitLayout { column.index() + match column.column_type() { Any::Instance => 0, - Any::Advice(_) => cs.num_instance_columns, + Any::Advice => cs.num_instance_columns, Any::Fixed => cs.num_instance_columns + cs.num_advice_columns, } }; diff --git a/halo2_frontend/src/dev/metadata.rs b/halo2_frontend/src/dev/metadata.rs index cafbfa76f5..291032ce3b 100644 --- a/halo2_frontend/src/dev/metadata.rs +++ b/halo2_frontend/src/dev/metadata.rs @@ -3,7 +3,7 @@ use super::metadata::Column as ColumnMetadata; use crate::plonk; use halo2_middleware::circuit::Any; -pub use halo2_middleware::metadata::Column; +pub use halo2_middleware::circuit::ColumnMid as Column; use std::{ collections::HashMap, fmt::{self, Debug}, diff --git a/halo2_frontend/src/dev/tfp.rs b/halo2_frontend/src/dev/tfp.rs index 2951cd068e..0d02d1d2bd 100644 --- a/halo2_frontend/src/dev/tfp.rs +++ b/halo2_frontend/src/dev/tfp.rs @@ -1,5 +1,6 @@ use std::{fmt, marker::PhantomData}; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; use tracing::{debug, debug_span, span::EnteredSpan}; @@ -9,9 +10,9 @@ use crate::circuit::{ }; use crate::plonk::{ circuit::expression::{Challenge, Column}, - Assigned, Assignment, Circuit, ConstraintSystem, Error, FloorPlanner, Selector, + Advice, Assigned, Assignment, Circuit, ConstraintSystem, Error, Fixed, FloorPlanner, Instance, + Selector, }; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; /// A helper type that augments a [`FloorPlanner`] with [`tracing`] spans and events. /// diff --git a/halo2_frontend/src/dev/util.rs b/halo2_frontend/src/dev/util.rs index 8ed1b80be5..c83a9b8497 100644 --- a/halo2_frontend/src/dev/util.rs +++ b/halo2_frontend/src/dev/util.rs @@ -2,8 +2,10 @@ use group::ff::Field; use std::collections::BTreeMap; use super::{metadata, CellValue, InstanceValue, Value}; -use crate::plonk::{AdviceQuery, Column, Expression, FixedQuery, Gate, InstanceQuery, VirtualCell}; -use halo2_middleware::circuit::{Advice, Any, ColumnType}; +use crate::plonk::{ + AdviceQuery, Column, ColumnType, Expression, FixedQuery, Gate, InstanceQuery, VirtualCell, +}; +use halo2_middleware::circuit::Any; use halo2_middleware::poly::Rotation; pub(crate) struct AnyQuery { @@ -32,9 +34,7 @@ impl From for AnyQuery { fn from(query: AdviceQuery) -> Self { Self { index: query.index, - column_type: Any::Advice(Advice { - phase: query.phase.0, - }), + column_type: Any::Advice, column_index: query.column_index, rotation: query.rotation, } diff --git a/halo2_frontend/src/plonk/circuit.rs b/halo2_frontend/src/plonk/circuit.rs index 259a1f511a..e81f455bc5 100644 --- a/halo2_frontend/src/plonk/circuit.rs +++ b/halo2_frontend/src/plonk/circuit.rs @@ -1,7 +1,8 @@ use crate::circuit::{layouter::SyncDeps, Layouter, Value}; use crate::plonk::{Assigned, Error}; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; +use halo2_middleware::poly::Rotation; pub mod compress_selectors; pub mod constraint_system; @@ -10,9 +11,92 @@ pub mod expression; pub use constraint_system::*; pub use expression::*; -// TODO: Bring ColumnType, Advice, Fixed, Instance and Any here, where Advice has a sealed phase -// Keep a slim copy of those types in middleware. -// https://github.com/privacy-scaling-explorations/halo2/issues/295 +/// A column type +pub trait ColumnType: + 'static + Sized + Copy + std::fmt::Debug + PartialEq + Eq + Into +{ + /// Return expression from cell + fn query_cell(&self, index: usize, at: Rotation) -> Expression; +} + +/// An advice column +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub struct Advice; + +/// A fixed column +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub struct Fixed; + +/// An instance column +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub struct Instance; + +impl ColumnType for Advice { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { + Expression::Advice(AdviceQuery { + index: None, + column_index: index, + rotation: at, + }) + } +} +impl ColumnType for Fixed { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { + Expression::Fixed(FixedQuery { + index: None, + column_index: index, + rotation: at, + }) + } +} +impl ColumnType for Instance { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { + Expression::Instance(InstanceQuery { + index: None, + column_index: index, + rotation: at, + }) + } +} +impl ColumnType for Any { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { + match self { + Any::Advice => Expression::Advice(AdviceQuery { + index: None, + column_index: index, + rotation: at, + }), + Any::Fixed => Expression::Fixed(FixedQuery { + index: None, + column_index: index, + rotation: at, + }), + Any::Instance => Expression::Instance(InstanceQuery { + index: None, + column_index: index, + rotation: at, + }), + } + } +} + +impl From for Any { + fn from(_: Advice) -> Any { + Any::Advice + } +} + +impl From for Any { + fn from(_: Fixed) -> Any { + Any::Fixed + } +} + +impl From for Any { + fn from(_: Instance) -> Any { + Any::Instance + } +} /// This trait allows a [`Circuit`] to direct some backend to assign a witness /// for a constraint system. diff --git a/halo2_frontend/src/plonk/circuit/constraint_system.rs b/halo2_frontend/src/plonk/circuit/constraint_system.rs index 8b19b9a3a8..d6685c1f09 100644 --- a/halo2_frontend/src/plonk/circuit/constraint_system.rs +++ b/halo2_frontend/src/plonk/circuit/constraint_system.rs @@ -1,13 +1,12 @@ use super::compress_selectors; use super::expression::sealed; use crate::plonk::{ - lookup, permutation, shuffle, AdviceQuery, Challenge, Column, Expression, FirstPhase, - FixedQuery, InstanceQuery, Phase, Selector, TableColumn, + lookup, permutation, shuffle, Advice, AdviceQuery, Challenge, Column, Expression, FirstPhase, + Fixed, FixedQuery, Instance, InstanceQuery, Phase, Selector, TableColumn, }; use core::cmp::max; -use halo2_middleware::circuit::{Advice, Any, ConstraintSystemMid, Fixed, GateMid, Instance}; +use halo2_middleware::circuit::{Any, ColumnMid, ConstraintSystemMid, GateMid}; use halo2_middleware::ff::Field; -use halo2_middleware::metadata; use halo2_middleware::poly::Rotation; use std::collections::HashMap; use std::convert::TryFrom; @@ -302,7 +301,7 @@ pub struct ConstraintSystem { pub(crate) shuffles: Vec>, // List of indexes of Fixed columns which are associated to a circuit-general Column tied to their annotation. - pub(crate) general_column_annotations: HashMap, + pub(crate) general_column_annotations: HashMap, // Vector of fixed columns, which can be used to store constant values // that are copied into advice columns. @@ -541,9 +540,7 @@ impl ConstraintSystem { fn query_any_index(&mut self, column: Column, at: Rotation) -> usize { match column.column_type() { - Any::Advice(_) => { - self.query_advice_index(Column::::try_from(column).unwrap(), at) - } + Any::Advice => self.query_advice_index(Column::::try_from(column).unwrap(), at), Any::Fixed => self.query_fixed_index(Column::::try_from(column).unwrap(), at), Any::Instance => { self.query_instance_index(Column::::try_from(column).unwrap(), at) @@ -583,7 +580,7 @@ impl ConstraintSystem { pub fn get_any_query_index(&self, column: Column, at: Rotation) -> usize { match column.column_type() { - Any::Advice(_) => { + Any::Advice => { self.get_advice_query_index(Column::::try_from(column).unwrap(), at) } Any::Fixed => { @@ -860,7 +857,10 @@ impl ConstraintSystem { { // We don't care if the table has already an annotation. If it's the case we keep the new one. self.general_column_annotations.insert( - metadata::Column::from((Any::Fixed, column.inner().index)), + ColumnMid { + index: column.inner().index, + column_type: halo2_middleware::circuit::Any::Fixed, + }, annotation().into(), ); } @@ -875,7 +875,10 @@ impl ConstraintSystem { let col_any = column.into(); // We don't care if the table has already an annotation. If it's the case we keep the new one. self.general_column_annotations.insert( - metadata::Column::from((col_any.column_type, col_any.index)), + ColumnMid { + column_type: col_any.column_type, + index: col_any.index, + }, annotation().into(), ); } @@ -913,7 +916,7 @@ impl ConstraintSystem { let tmp = Column { index: self.num_advice_columns, - column_type: Advice { phase: phase.0 }, + column_type: Advice, }; self.unblinded_advice_columns.push(tmp.index); self.num_advice_columns += 1; @@ -938,7 +941,7 @@ impl ConstraintSystem { let tmp = Column { index: self.num_advice_columns, - column_type: Advice { phase: phase.0 }, + column_type: Advice, }; self.num_advice_columns += 1; self.num_advice_queries.push(0); @@ -1130,7 +1133,7 @@ impl ConstraintSystem { } /// Returns general column annotations - pub fn general_column_annotations(&self) -> &HashMap { + pub fn general_column_annotations(&self) -> &HashMap { &self.general_column_annotations } @@ -1211,7 +1214,6 @@ impl<'a, F: Field> VirtualCells<'a, F> { index: Some(self.meta.query_advice_index(column, at)), column_index: column.index, rotation: at, - phase: sealed::Phase(column.column_type().phase), }) } @@ -1229,7 +1231,7 @@ impl<'a, F: Field> VirtualCells<'a, F> { pub fn query_any>>(&mut self, column: C, at: Rotation) -> Expression { let column = column.into(); match column.column_type() { - Any::Advice(_) => self.query_advice(Column::::try_from(column).unwrap(), at), + Any::Advice => self.query_advice(Column::::try_from(column).unwrap(), at), Any::Fixed => self.query_fixed(Column::::try_from(column).unwrap(), at), Any::Instance => self.query_instance(Column::::try_from(column).unwrap(), at), } diff --git a/halo2_frontend/src/plonk/circuit/expression.rs b/halo2_frontend/src/plonk/circuit/expression.rs index b8f61f3a1c..9636b76190 100644 --- a/halo2_frontend/src/plonk/circuit/expression.rs +++ b/halo2_frontend/src/plonk/circuit/expression.rs @@ -1,14 +1,10 @@ use crate::circuit::Region; -use crate::plonk::circuit::VirtualCells; +use crate::plonk::circuit::{Advice, ColumnType, Fixed, Instance, VirtualCells}; use crate::plonk::Error; use core::cmp::max; use core::ops::{Add, Mul}; -use halo2_middleware::circuit::{ - Advice, Any, ChallengeMid, ColumnMid, ColumnType, ExpressionMid, Fixed, Instance, QueryMid, - VarMid, -}; +use halo2_middleware::circuit::{Any, ChallengeMid, ColumnMid, ExpressionMid, QueryMid, VarMid}; use halo2_middleware::ff::Field; -use halo2_middleware::metadata; use halo2_middleware::poly::Rotation; use sealed::SealedPhase; use std::fmt::Debug; @@ -25,11 +21,11 @@ pub struct Column { pub column_type: C, } -impl From> for metadata::Column { +impl From> for ColumnMid { fn from(val: Column) -> Self { - metadata::Column { + ColumnMid { index: val.index(), - column_type: *val.column_type(), + column_type: (*val.column_type()), } } } @@ -51,28 +47,7 @@ impl Column { /// Return expression from column at a relative position pub fn query_cell(&self, at: Rotation) -> Expression { - let expr_mid = self.column_type.query_cell::(self.index, at); - match expr_mid { - ExpressionMid::Var(VarMid::Query(q)) => match q.column_type { - Any::Advice(Advice { phase }) => Expression::Advice(AdviceQuery { - index: None, - column_index: q.column_index, - rotation: q.rotation, - phase: sealed::Phase(phase), - }), - Any::Fixed => Expression::Fixed(FixedQuery { - index: None, - column_index: q.column_index, - rotation: q.rotation, - }), - Any::Instance => Expression::Instance(InstanceQuery { - index: None, - column_index: q.column_index, - rotation: q.rotation, - }), - }, - _ => unreachable!(), - } + self.column_type.query_cell(self.index, at) } /// Return expression from column at the current row @@ -123,20 +98,11 @@ impl From for Column { } } -impl From> for ColumnMid { - fn from(val: Column) -> Self { - ColumnMid { - index: val.index(), - column_type: *val.column_type(), - } - } -} - impl From> for Column { fn from(advice: Column) -> Column { Column { index: advice.index(), - column_type: Any::Advice(advice.column_type), + column_type: Any::Advice, } } } @@ -164,9 +130,9 @@ impl TryFrom> for Column { fn try_from(any: Column) -> Result { match any.column_type() { - Any::Advice(advice) => Ok(Column { + Any::Advice => Ok(Column { index: any.index(), - column_type: *advice, + column_type: Advice, }), _ => Err("Cannot convert into Column"), } @@ -286,11 +252,9 @@ impl SealedPhase for ThirdPhase { /// Selectors are disabled on all rows by default, and must be explicitly enabled on each /// row when required: /// ``` -/// use halo2_middleware::circuit::Advice; /// use halo2_frontend::circuit::{Chip, Layouter, Value}; -/// use halo2_frontend::plonk::{Error, Column, Selector}; +/// use halo2_frontend::plonk::{Advice, Fixed, Error, Column, Selector}; /// use halo2_middleware::ff::Field; -/// # use halo2_middleware::circuit::Fixed; /// /// struct Config { /// a: Column, @@ -367,8 +331,6 @@ pub struct AdviceQuery { pub column_index: usize, /// Rotation of this query pub rotation: Rotation, - /// Phase of this advice column - pub phase: sealed::Phase, } impl AdviceQuery { @@ -381,11 +343,6 @@ impl AdviceQuery { pub fn rotation(&self) -> Rotation { self.rotation } - - /// Phase of this advice column - pub fn phase(&self) -> u8 { - self.phase.0 - } } /// Query of instance column at a certain relative location @@ -524,11 +481,10 @@ impl From> for ExpressionMid { Expression::Advice(AdviceQuery { column_index, rotation, - phase, .. }) => ExpressionMid::Var(VarMid::Query(QueryMid { column_index, - column_type: Any::Advice(Advice { phase: phase.0 }), + column_type: Any::Advice, rotation, })), Expression::Instance(InstanceQuery { @@ -577,9 +533,7 @@ impl Expression { if query.index.is_none() { let col = Column { index: query.column_index, - column_type: Advice { - phase: query.phase.0, - }, + column_type: Advice, }; cells.queried_cells.push((col, query.rotation).into()); query.index = Some(cells.meta.query_advice_index(col, query.rotation)); @@ -1018,10 +972,6 @@ impl std::fmt::Debug for Expression { debug_struct .field("column_index", &query.column_index) .field("rotation", &query.rotation); - // Only show advice's phase if it's not in first phase. - if query.phase != FirstPhase.to_sealed() { - debug_struct.field("phase", &query.phase); - } debug_struct.finish() } Expression::Instance(query) => { diff --git a/halo2_frontend/src/plonk/keygen.rs b/halo2_frontend/src/plonk/keygen.rs index 7d20e88e45..94c44c1f43 100644 --- a/halo2_frontend/src/plonk/keygen.rs +++ b/halo2_frontend/src/plonk/keygen.rs @@ -1,10 +1,12 @@ use std::ops::Range; +use halo2_middleware::circuit::Any; use halo2_middleware::ff::Field; use crate::circuit::Value; -use crate::plonk::{permutation, Assigned, Assignment, Challenge, Column, Error, Selector}; -use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; +use crate::plonk::{ + permutation, Advice, Assigned, Assignment, Challenge, Column, Error, Fixed, Instance, Selector, +}; /// Assembly to be used in circuit synthesis. #[derive(Debug)] diff --git a/halo2_frontend/src/plonk/permutation.rs b/halo2_frontend/src/plonk/permutation.rs index eb12ec7401..20444df083 100644 --- a/halo2_frontend/src/plonk/permutation.rs +++ b/halo2_frontend/src/plonk/permutation.rs @@ -2,7 +2,6 @@ use crate::plonk::{Column, Error}; use halo2_middleware::circuit::{Any, Cell}; -use halo2_middleware::permutation::ArgumentMid; /// A permutation argument. #[derive(Default, Debug, Clone)] @@ -11,14 +10,6 @@ pub struct Argument { pub(crate) columns: Vec>, } -impl From for Argument { - fn from(arg: ArgumentMid) -> Self { - Self { - columns: arg.columns.into_iter().map(|c| c.into()).collect(), - } - } -} - impl Argument { /// Returns the minimum circuit degree required by the permutation argument. /// The argument may use larger degree gates depending on the actual diff --git a/halo2_middleware/src/circuit.rs b/halo2_middleware/src/circuit.rs index 4a28bd5558..3200659419 100644 --- a/halo2_middleware/src/circuit.rs +++ b/halo2_middleware/src/circuit.rs @@ -1,8 +1,9 @@ use crate::expression::{Expression, Variable}; use crate::poly::Rotation; -use crate::{lookup, metadata, permutation, shuffle}; +use crate::{lookup, permutation, shuffle}; use ff::Field; use std::collections::HashMap; +use std::fmt; /// A challenge squeezed from transcript after advice columns at the phase have been committed. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] @@ -61,7 +62,7 @@ impl Variable for VarMid { VarMid::Query(query) => { match query.column_type { Any::Fixed => write!(writer, "fixed")?, - Any::Advice(_) => write!(writer, "advice")?, + Any::Advice => write!(writer, "advice")?, Any::Instance => write!(writer, "instance")?, }; write!(writer, "[{}][{}]", query.column_index, query.rotation.0) @@ -127,7 +128,7 @@ pub struct ConstraintSystemMid { pub shuffles: Vec>, // List of indexes of Fixed columns which are associated to a circuit-general Column tied to their annotation. - pub general_column_annotations: HashMap, + pub general_column_annotations: HashMap, // The minimum degree required by the circuit, which can be set to a // larger amount than actually needed. This can be used, for example, to @@ -150,114 +151,21 @@ pub struct CompiledCircuitV2 { pub cs: ConstraintSystemMid, } -// TODO: The query_cell method is only used in the frontend, which uses Expression. By having this -// trait implemented here we can only return ExpressionMid, which requires conversion to Expression -// when used. On the other hand, it's difficult to move ColumnType to the frontend because this -// trait is implemented for Any which is used in the backend. It would be great to find a way to -// move all the `query_cell` implementations to the frontend and have them return `Expression`, -// while keeping `Any` in the middleware. -// https://github.com/privacy-scaling-explorations/halo2/issues/270 -/// A column type -pub trait ColumnType: - 'static + Sized + Copy + std::fmt::Debug + PartialEq + Eq + Into -{ - /// Return expression from cell - fn query_cell(&self, index: usize, at: Rotation) -> ExpressionMid; -} - -/// A column with an index and type -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] -pub struct ColumnMid { - /// The index of the column. - pub index: usize, - /// The type of the column. - pub column_type: Any, -} - -impl ColumnMid { - pub fn new(index: usize, column_type: Any) -> Self { - ColumnMid { index, column_type } - } -} - -/// A cell identifies a position in the plonkish matrix identified by a column and a row offset. -#[derive(Clone, Debug)] -pub struct Cell { - pub column: ColumnMid, - pub row: usize, -} - -/// An advice column -#[derive(Default, Clone, Copy, Eq, PartialEq, Hash)] -pub struct Advice { - pub phase: u8, -} - -impl Advice { - /// Returns `Advice` in given `Phase` - pub fn new(phase: u8) -> Advice { - Advice { phase } - } - - /// Phase of this column - pub fn phase(&self) -> u8 { - self.phase - } -} - -impl std::fmt::Debug for Advice { - fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { - let mut debug_struct = f.debug_struct("Advice"); - // Only show advice's phase if it's not in first phase. - if self.phase != 0 { - debug_struct.field("phase", &self.phase); - } - debug_struct.finish() - } -} - -/// A fixed column -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] -pub struct Fixed; - -/// An instance column -#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] -pub struct Instance; - /// An enum over the Advice, Fixed, Instance structs #[derive(Clone, Copy, Eq, PartialEq, Hash)] pub enum Any { /// An Advice variant - Advice(Advice), + Advice, /// A Fixed variant Fixed, /// An Instance variant Instance, } -impl Any { - /// Returns Advice variant in `FirstPhase` - pub fn advice() -> Any { - Any::Advice(Advice::default()) - } - - /// Returns Advice variant in given `Phase` - pub fn advice_in(phase: u8) -> Any { - Any::Advice(Advice::new(phase)) - } -} - impl std::fmt::Debug for Any { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { - Any::Advice(advice) => { - let mut debug_struct = f.debug_struct("Advice"); - // Only show advice's phase if it's not in first phase. - if advice.phase != 0 { - debug_struct.field("phase", &advice.phase); - } - debug_struct.finish() - } + Any::Advice => f.debug_struct("Advice").finish(), Any::Fixed => f.debug_struct("Fixed").finish(), Any::Instance => f.debug_struct("Instance").finish(), } @@ -269,15 +177,16 @@ impl Ord for Any { // This ordering is consensus-critical! The layouters rely on deterministic column // orderings. match (self, other) { - (Any::Instance, Any::Instance) | (Any::Fixed, Any::Fixed) => std::cmp::Ordering::Equal, - (Any::Advice(lhs), Any::Advice(rhs)) => lhs.phase.cmp(&rhs.phase), + (Any::Instance, Any::Instance) + | (Any::Fixed, Any::Fixed) + | (Any::Advice, Any::Advice) => std::cmp::Ordering::Equal, // Across column types, sort Instance < Advice < Fixed. - (Any::Instance, Any::Advice(_)) - | (Any::Advice(_), Any::Fixed) + (Any::Instance, Any::Advice) + | (Any::Advice, Any::Fixed) | (Any::Instance, Any::Fixed) => std::cmp::Ordering::Less, (Any::Fixed, Any::Instance) - | (Any::Fixed, Any::Advice(_)) - | (Any::Advice(_), Any::Instance) => std::cmp::Ordering::Greater, + | (Any::Fixed, Any::Advice) + | (Any::Advice, Any::Instance) => std::cmp::Ordering::Greater, } } } @@ -288,69 +197,35 @@ impl PartialOrd for Any { } } -impl ColumnType for Advice { - fn query_cell(&self, index: usize, at: Rotation) -> ExpressionMid { - ExpressionMid::Var(VarMid::Query(QueryMid { - column_index: index, - column_type: Any::Advice(Advice { phase: self.phase }), - rotation: at, - })) - } -} -impl ColumnType for Fixed { - fn query_cell(&self, index: usize, at: Rotation) -> ExpressionMid { - ExpressionMid::Var(VarMid::Query(QueryMid { - column_index: index, - column_type: Any::Fixed, - rotation: at, - })) - } -} -impl ColumnType for Instance { - fn query_cell(&self, index: usize, at: Rotation) -> ExpressionMid { - ExpressionMid::Var(VarMid::Query(QueryMid { - column_index: index, - column_type: Any::Instance, - rotation: at, - })) - } -} -impl ColumnType for Any { - fn query_cell(&self, index: usize, at: Rotation) -> ExpressionMid { - match self { - Any::Advice(Advice { phase }) => ExpressionMid::Var(VarMid::Query(QueryMid { - column_index: index, - column_type: Any::Advice(Advice { phase: *phase }), - rotation: at, - })), - Any::Fixed => ExpressionMid::Var(VarMid::Query(QueryMid { - column_index: index, - column_type: Any::Fixed, - rotation: at, - })), - Any::Instance => ExpressionMid::Var(VarMid::Query(QueryMid { - column_index: index, - column_type: Any::Instance, - rotation: at, - })), - } - } +/// A column with an index and type +#[derive(Clone, Copy, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)] +pub struct ColumnMid { + /// The type of the column. + pub column_type: Any, + /// The index of the column. + pub index: usize, } -impl From for Any { - fn from(advice: Advice) -> Any { - Any::Advice(advice) +impl ColumnMid { + pub fn new(column_type: Any, index: usize) -> Self { + ColumnMid { column_type, index } } } -impl From for Any { - fn from(_: Fixed) -> Any { - Any::Fixed +impl fmt::Display for ColumnMid { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + let column_type = match self.column_type { + Any::Advice => "a", + Any::Fixed => "f", + Any::Instance => "i", + }; + write!(f, "{}{}", column_type, self.index) } } -impl From for Any { - fn from(_: Instance) -> Any { - Any::Instance - } +/// A cell identifies a position in the plonkish matrix identified by a column and a row offset. +#[derive(Clone, Debug)] +pub struct Cell { + pub column: ColumnMid, + pub row: usize, } diff --git a/halo2_middleware/src/lib.rs b/halo2_middleware/src/lib.rs index 9dc5f89a86..0b42d50ad6 100644 --- a/halo2_middleware/src/lib.rs +++ b/halo2_middleware/src/lib.rs @@ -1,7 +1,6 @@ pub mod circuit; pub mod expression; pub mod lookup; -pub mod metadata; pub mod permutation; pub mod poly; pub mod shuffle; diff --git a/halo2_middleware/src/metadata.rs b/halo2_middleware/src/metadata.rs deleted file mode 100644 index ebc105985d..0000000000 --- a/halo2_middleware/src/metadata.rs +++ /dev/null @@ -1,44 +0,0 @@ -use crate::circuit::{self, Any}; -use std::fmt::{self, Debug}; - -// TODO: Could we replace this by circuit::Column? at least for the middleware? -/// Metadata about a column within a circuit. -#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)] -pub struct Column { - /// The type of the column. - pub column_type: Any, - /// The index of the column. - pub index: usize, -} - -impl Column { - /// Return the column type. - pub fn column_type(&self) -> Any { - self.column_type - } - /// Return the column index. - pub fn index(&self) -> usize { - self.index - } -} - -impl fmt::Display for Column { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "Column('{:?}', {})", self.column_type, self.index) - } -} - -impl From<(Any, usize)> for Column { - fn from((column_type, index): (Any, usize)) -> Self { - Column { column_type, index } - } -} - -impl From for Column { - fn from(column: circuit::ColumnMid) -> Self { - Column { - column_type: column.column_type, - index: column.index, - } - } -} diff --git a/halo2_proofs/src/plonk.rs b/halo2_proofs/src/plonk.rs index f5f16b5cc7..236379f16f 100644 --- a/halo2_proofs/src/plonk.rs +++ b/halo2_proofs/src/plonk.rs @@ -20,10 +20,10 @@ pub use verifier::verify_proof; pub use error::Error; pub use halo2_backend::plonk::{Error as ErrorBack, ProvingKey, VerifyingKey}; pub use halo2_frontend::plonk::{ - Assigned, Challenge, Circuit, Column, ConstraintSystem, Error as ErrorFront, Expression, - FirstPhase, SecondPhase, Selector, TableColumn, ThirdPhase, + Advice, Assigned, Challenge, Circuit, Column, ConstraintSystem, Error as ErrorFront, + Expression, FirstPhase, Fixed, Instance, SecondPhase, Selector, TableColumn, ThirdPhase, }; -pub use halo2_middleware::circuit::{Advice, ConstraintSystemMid, Fixed, Instance}; +pub use halo2_middleware::circuit::{Any, ConstraintSystemMid}; use group::ff::FromUniformBytes; use halo2_common::helpers::{SerdeCurveAffine, SerdePrimeField}; diff --git a/halo2_proofs/tests/frontend_backend_split.rs b/halo2_proofs/tests/frontend_backend_split.rs index e67f0acade..76249f48e8 100644 --- a/halo2_proofs/tests/frontend_backend_split.rs +++ b/halo2_proofs/tests/frontend_backend_split.rs @@ -23,15 +23,11 @@ use halo2_frontend::{ dev::MockProver, plonk::{ circuit::{Challenge, Column}, - Circuit, ConstraintSystem, Error as ErrorFront, Expression, FirstPhase, SecondPhase, - Selector, + Advice, Circuit, ConstraintSystem, Error as ErrorFront, Expression, FirstPhase, Fixed, + Instance, SecondPhase, Selector, }, }; -use halo2_middleware::{ - circuit::{Advice, Fixed, Instance}, - ff::Field, - poly::Rotation, -}; +use halo2_middleware::{ff::Field, poly::Rotation}; use halo2_proofs::poly::commitment::ParamsProver; use std::collections::HashMap; diff --git a/halo2_proofs/tests/plonk_api.rs b/halo2_proofs/tests/plonk_api.rs index 01643e3352..cd25fd9ba8 100644 --- a/halo2_proofs/tests/plonk_api.rs +++ b/halo2_proofs/tests/plonk_api.rs @@ -862,8 +862,8 @@ fn plonk_api() { advice_queries: [ ( ColumnMid { - index: 1, column_type: Advice, + index: 1, }, Rotation( 0, @@ -871,8 +871,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 2, column_type: Advice, + index: 2, }, Rotation( 0, @@ -880,8 +880,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 3, column_type: Advice, + index: 3, }, Rotation( 0, @@ -889,8 +889,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 4, column_type: Advice, + index: 4, }, Rotation( 1, @@ -898,8 +898,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 0, column_type: Advice, + index: 0, }, Rotation( -1, @@ -907,8 +907,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 0, column_type: Advice, + index: 0, }, Rotation( 0, @@ -916,8 +916,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 4, column_type: Advice, + index: 4, }, Rotation( 0, @@ -927,8 +927,8 @@ fn plonk_api() { instance_queries: [ ( ColumnMid { - index: 0, column_type: Instance, + index: 0, }, Rotation( 0, @@ -938,8 +938,8 @@ fn plonk_api() { fixed_queries: [ ( ColumnMid { - index: 2, column_type: Fixed, + index: 2, }, Rotation( 0, @@ -947,8 +947,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 3, column_type: Fixed, + index: 3, }, Rotation( 0, @@ -956,8 +956,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 1, column_type: Fixed, + index: 1, }, Rotation( 0, @@ -965,8 +965,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 4, column_type: Fixed, + index: 4, }, Rotation( 0, @@ -974,8 +974,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 0, column_type: Fixed, + index: 0, }, Rotation( 0, @@ -983,8 +983,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 5, column_type: Fixed, + index: 5, }, Rotation( 0, @@ -992,8 +992,8 @@ fn plonk_api() { ), ( ColumnMid { - index: 6, column_type: Fixed, + index: 6, }, Rotation( 0, @@ -1003,52 +1003,52 @@ fn plonk_api() { permutation: ArgumentMid { columns: [ ColumnMid { - index: 1, column_type: Advice, + index: 1, }, ColumnMid { - index: 2, column_type: Advice, + index: 2, }, ColumnMid { - index: 3, column_type: Advice, + index: 3, }, ColumnMid { - index: 0, column_type: Fixed, + index: 0, }, ColumnMid { - index: 0, column_type: Advice, + index: 0, }, ColumnMid { - index: 4, column_type: Advice, + index: 4, }, ColumnMid { - index: 0, column_type: Instance, + index: 0, }, ColumnMid { - index: 1, column_type: Fixed, + index: 1, }, ColumnMid { - index: 2, column_type: Fixed, + index: 2, }, ColumnMid { - index: 3, column_type: Fixed, + index: 3, }, ColumnMid { - index: 4, column_type: Fixed, + index: 4, }, ColumnMid { - index: 5, column_type: Fixed, + index: 5, }, ], },