Skip to content

Commit

Permalink
Move "frontend" column types from middleware to the frontend (privacy…
Browse files Browse the repository at this point in the history
…-scaling-explorations#300)

Summary:
- move `Fixed`, `Advice`, `Instance`, `ColumnType`, `Column`, `sealed` from
  middleware to frontend
  - These types are only used in the frontend.  In particular, the `Fixed, Advice, Instance` types are used in the generic `Column<C>` type which is useful for the fronted because it brings type safety for column types; but in the backend it's unneeded because we use the simplified type `ColumnMid`.
- remove metadata::Column in favor of ColumnMid
- remove phase from `Advice`
  - I noticed that the phase in the `Advice` was redundant because a column always exists in the context of a `ConstraintSystem` which has the `advice_column_phase` with that same information.  This removal simplifies the code.
- Remove `From<(Any, usize)> for Column` in favor of using constructors in order to improve code clarity.  

* chore: simplify Column types

- remove phase from Advice
- remove metadata::Column in favour of ColumnMid
- move Fixed, Advice, Instance, ColumnType, Column, sealed from
  middleware to frontend

* chore: remove unneeded pubs

* chore: make Display for ColumnMid succint

* fix: set correct ColumnMid field order for sorting

* fix: update plonk_api test hardcoded vk

* fix: doc tests imports

* fix: address comments from @duguorong009 and @CPerezz
  • Loading branch information
ed255 authored Mar 21, 2024
1 parent 20ad57d commit 6eb4371
Show file tree
Hide file tree
Showing 34 changed files with 302 additions and 468 deletions.
2 changes: 1 addition & 1 deletion halo2_backend/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl<F: Field> ConstraintSystemBack<F> {

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,
};
Expand Down
17 changes: 9 additions & 8 deletions halo2_backend/src/plonk/evaluation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ impl<C: CurveAffine> Evaluator<C> {
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],
})
Expand All @@ -467,7 +467,7 @@ impl<C: CurveAffine> Evaluator<C> {

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],
}) {
Expand Down Expand Up @@ -698,9 +698,10 @@ impl<C: CurveAffine> GraphEvaluator<C> {
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),
)),
Expand Down Expand Up @@ -867,7 +868,7 @@ pub fn evaluate<F: Field, B: LagrangeBasis>(
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],
}
}
Expand All @@ -886,7 +887,7 @@ pub fn evaluate<F: Field, B: LagrangeBasis>(
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};

Expand Down Expand Up @@ -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,
Expand Down
15 changes: 3 additions & 12 deletions halo2_backend/src/plonk/keygen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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,
Expand Down Expand Up @@ -336,16 +336,7 @@ fn collect_queries<F: Field>(

// 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];
Expand Down
2 changes: 1 addition & 1 deletion halo2_backend/src/plonk/lookup/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ impl<C: CurveAffine> Evaluated<C> {
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],
},
},
Expand Down
4 changes: 2 additions & 2 deletions halo2_backend/src/plonk/permutation/prover.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
};
Expand All @@ -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,
};
Expand Down
4 changes: 2 additions & 2 deletions halo2_backend/src/plonk/permutation/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ impl<C: CurveAffine> Evaluated<C> {
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 => {
Expand All @@ -181,7 +181,7 @@ impl<C: CurveAffine> Evaluated<C> {
* (<C::Scalar as PrimeField>::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 => {
Expand Down
2 changes: 1 addition & 1 deletion halo2_backend/src/plonk/shuffle/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ impl<C: CurveAffine> Evaluated<C> {
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],
},
},
Expand Down
2 changes: 1 addition & 1 deletion halo2_backend/src/plonk/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand Down
26 changes: 13 additions & 13 deletions halo2_frontend/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -130,14 +130,13 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
}

pub struct WitnessCollection<'a, F: Field> {
pub k: u32,
pub current_phase: sealed::Phase,
pub advice: Vec<Vec<Assigned<F>>>,
// pub unblinded_advice: HashSet<usize>,
pub challenges: &'a HashMap<usize, F>,
pub instances: &'a [&'a [F]],
pub usable_rows: RangeTo<usize>,
pub _marker: std::marker::PhantomData<F>,
k: u32,
current_phase: sealed::Phase,
advice_column_phase: &'a Vec<sealed::Phase>,
advice: Vec<Vec<Assigned<F>>>,
challenges: &'a HashMap<usize, F>,
instances: &'a [&'a [F]],
usable_rows: RangeTo<usize>,
}

impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {
Expand Down Expand Up @@ -197,7 +196,8 @@ impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {
AR: Into<String>,
{
// 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(());
}

Expand Down Expand Up @@ -326,6 +326,7 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> 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,
Expand All @@ -334,7 +335,6 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> 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.
Expand Down
10 changes: 6 additions & 4 deletions halo2_frontend/src/circuit/floor_planner/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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.
///
Expand Down Expand Up @@ -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() {
Expand Down
10 changes: 6 additions & 4 deletions halo2_frontend/src/circuit/floor_planner/v1.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt;

use halo2_middleware::circuit::Any;
use halo2_middleware::ff::Field;

use crate::plonk::Assigned;
Expand All @@ -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;

Expand Down Expand Up @@ -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() {
Expand Down
8 changes: 4 additions & 4 deletions halo2_frontend/src/circuit/floor_planner/v1/strategy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -252,23 +252,23 @@ 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(),
row_count: 15,
},
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(),
row_count: 10,
},
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(),
Expand Down
4 changes: 2 additions & 2 deletions halo2_frontend/src/circuit/layouter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
Loading

0 comments on commit 6eb4371

Please sign in to comment.