diff --git a/backend/src/plonk/keygen.rs b/backend/src/plonk/keygen.rs index 0af7d704..4c61e459 100644 --- a/backend/src/plonk/keygen.rs +++ b/backend/src/plonk/keygen.rs @@ -18,7 +18,7 @@ use crate::{ EvaluationDomain, }, }; -use halo2_middleware::circuit::{Advice, Any, Challenge, CompiledCircuitV2, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, CompiledCircuitV2, Fixed, Instance}; use halo2_middleware::plonk::Assigned; pub(crate) fn create_domain( diff --git a/common/src/circuit.rs b/common/src/circuit.rs index 59826767..5b50a3d1 100644 --- a/common/src/circuit.rs +++ b/common/src/circuit.rs @@ -4,8 +4,11 @@ use std::{fmt, marker::PhantomData}; use halo2_middleware::ff::Field; -use crate::plonk::{circuit::Column, Error, Selector, TableColumn}; -use halo2_middleware::circuit::{Advice, Any, Challenge, Fixed, Instance}; +use crate::plonk::{ + circuit::{Challenge, Column}, + Error, Selector, TableColumn, +}; +use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::plonk::Assigned; mod value; diff --git a/common/src/circuit/floor_planner/single_pass.rs b/common/src/circuit/floor_planner/single_pass.rs index 8a1b9db4..18a04582 100644 --- a/common/src/circuit/floor_planner/single_pass.rs +++ b/common/src/circuit/floor_planner/single_pass.rs @@ -11,9 +11,9 @@ use crate::{ table_layouter::{compute_table_lengths, SimpleTableLayouter}, Cell, Column, Layouter, Region, RegionIndex, RegionStart, Table, Value, }, - plonk::{Assignment, Circuit, Error, FloorPlanner, Selector, TableColumn}, + plonk::{circuit::Challenge, Assignment, Circuit, Error, FloorPlanner, Selector, TableColumn}, }; -use halo2_middleware::circuit::{Advice, Any, Challenge, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::plonk::Assigned; /// A simple [`FloorPlanner`] that performs minimal optimizations. diff --git a/common/src/circuit/floor_planner/v1.rs b/common/src/circuit/floor_planner/v1.rs index 1557fe2d..820af78c 100644 --- a/common/src/circuit/floor_planner/v1.rs +++ b/common/src/circuit/floor_planner/v1.rs @@ -8,9 +8,9 @@ use crate::{ table_layouter::{compute_table_lengths, SimpleTableLayouter}, Cell, Column, Layouter, Region, RegionIndex, RegionStart, Table, Value, }, - plonk::{Assignment, Circuit, Error, FloorPlanner, Selector, TableColumn}, + plonk::{circuit::Challenge, Assignment, Circuit, Error, FloorPlanner, Selector, TableColumn}, }; -use halo2_middleware::circuit::{Advice, Any, Challenge, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::plonk::Assigned; pub mod strategy; diff --git a/common/src/plonk/circuit.rs b/common/src/plonk/circuit.rs index 6e11a99b..0150468d 100644 --- a/common/src/plonk/circuit.rs +++ b/common/src/plonk/circuit.rs @@ -4,7 +4,7 @@ use crate::circuit::{Layouter, Region, Value}; use core::cmp::max; use core::ops::{Add, Mul}; use halo2_middleware::circuit::{ - Advice, AdviceQueryMid, Any, Challenge, ColumnMid, ColumnType, ConstraintSystemV2Backend, + Advice, AdviceQueryMid, Any, ChallengeMid, ColumnMid, ColumnType, ConstraintSystemV2Backend, ExpressionMid, Fixed, FixedQueryMid, GateV2Backend, Instance, InstanceQueryMid, }; use halo2_middleware::ff::Field; @@ -56,7 +56,26 @@ impl Column { /// Return expression from column at a relative position pub fn query_cell(&self, at: Rotation) -> Expression { - self.column_type.query_cell(self.index, at) + let expr_mid = self.column_type.query_cell::(self.index, at); + match expr_mid { + ExpressionMid::Advice(q) => Expression::Advice(AdviceQuery { + index: None, + column_index: q.column_index, + rotation: q.rotation, + phase: sealed::Phase(q.phase), + }), + ExpressionMid::Fixed(q) => Expression::Fixed(FixedQuery { + index: None, + column_index: q.column_index, + rotation: q.rotation, + }), + ExpressionMid::Instance(q) => Expression::Instance(InstanceQuery { + index: None, + column_index: q.column_index, + rotation: q.rotation, + }), + _ => unreachable!(), + } } /// Return expression from column at the current row @@ -426,6 +445,48 @@ impl TableColumn { } } +/// A challenge squeezed from transcript after advice columns at the phase have been committed. +#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] +pub struct Challenge { + pub index: usize, + pub phase: u8, +} + +impl Challenge { + /// Index of this challenge. + pub fn index(&self) -> usize { + self.index + } + + /// Phase of this challenge. + pub fn phase(&self) -> u8 { + self.phase + } + + /// Return Expression + pub fn expr(&self) -> Expression { + Expression::Challenge(*self) + } +} + +impl Into for Challenge { + fn into(self) -> ChallengeMid { + ChallengeMid { + index: self.index, + phase: self.phase, + } + } +} + +impl From for Challenge { + fn from(c: ChallengeMid) -> Self { + Self { + index: c.index, + phase: c.phase, + } + } +} + /// This trait allows a [`Circuit`] to direct some backend to assign a witness /// for a constraint system. pub trait Assignment { @@ -669,7 +730,7 @@ impl Into> for Expression { column_index, rotation, }), - Expression::Challenge(c) => ExpressionMid::Challenge(c), + Expression::Challenge(c) => ExpressionMid::Challenge(c.into()), Expression::Negated(e) => ExpressionMid::Negated(Box::new((*e).into())), Expression::Sum(lhs, rhs) => { ExpressionMid::Sum(Box::new((*lhs).into()), Box::new((*rhs).into())) @@ -1477,7 +1538,7 @@ impl QueriesMap { rotation: query.rotation, }) } - ExpressionMid::Challenge(c) => Expression::Challenge(*c), + ExpressionMid::Challenge(c) => Expression::Challenge(c.clone().into()), ExpressionMid::Negated(e) => Expression::Negated(Box::new(self.as_expression(e))), ExpressionMid::Sum(lhs, rhs) => Expression::Sum( Box::new(self.as_expression(lhs)), diff --git a/common/src/plonk/keygen.rs b/common/src/plonk/keygen.rs index cd7a835b..77507589 100644 --- a/common/src/plonk/keygen.rs +++ b/common/src/plonk/keygen.rs @@ -3,11 +3,11 @@ use std::ops::Range; use halo2_middleware::ff::Field; use super::{ - circuit::{Assignment, Column, Selector}, + circuit::{Assignment, Challenge, Column, Selector}, permutation, Error, LagrangeCoeff, Polynomial, }; use crate::circuit::Value; -use halo2_middleware::circuit::{Advice, Any, Challenge, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::plonk::Assigned; /// Assembly to be used in circuit synthesis. diff --git a/frontend/src/circuit.rs b/frontend/src/circuit.rs index 9483b0ea..582ec394 100644 --- a/frontend/src/circuit.rs +++ b/frontend/src/circuit.rs @@ -1,16 +1,14 @@ //! Traits and structs for implementing circuit components. use halo2_common::plonk::{ - circuit::Column, + circuit::{Challenge, Column}, permutation, sealed::{self, SealedPhase}, Assignment, Circuit, ConstraintSystem, Error, FirstPhase, FloorPlanner, SecondPhase, Selector, ThirdPhase, }; use halo2_common::poly::{batch_invert_assigned, Polynomial}; -use halo2_middleware::circuit::{ - Advice, Any, Challenge, CompiledCircuitV2, Fixed, Instance, PreprocessingV2, -}; +use halo2_middleware::circuit::{Advice, Any, CompiledCircuitV2, Fixed, Instance, PreprocessingV2}; use halo2_middleware::ff::Field; use halo2_middleware::plonk::Assigned; use std::collections::BTreeSet; diff --git a/frontend/src/circuit/floor_planner/single_pass.rs b/frontend/src/circuit/floor_planner/single_pass.rs index ec2aa18e..39d0a6a0 100644 --- a/frontend/src/circuit/floor_planner/single_pass.rs +++ b/frontend/src/circuit/floor_planner/single_pass.rs @@ -5,8 +5,8 @@ mod tests { use super::SimpleFloorPlanner; use crate::dev::MockProver; - use halo2_common::plonk::{Circuit, ConstraintSystem, Error}; - use halo2_middleware::circuit::{Advice, Column}; + use halo2_common::plonk::{circuit::Column, Circuit, ConstraintSystem, Error}; + use halo2_middleware::circuit::Advice; #[test] fn not_enough_columns_for_constants() { diff --git a/frontend/src/circuit/floor_planner/v1.rs b/frontend/src/circuit/floor_planner/v1.rs index 6a78b58e..591cab81 100644 --- a/frontend/src/circuit/floor_planner/v1.rs +++ b/frontend/src/circuit/floor_planner/v1.rs @@ -7,8 +7,8 @@ mod tests { use halo2curves::pasta::vesta; use crate::dev::MockProver; - use halo2_common::plonk::{Circuit, ConstraintSystem, Error}; - use halo2_middleware::circuit::{Advice, Column}; + use halo2_common::plonk::{circuit::Column, Circuit, ConstraintSystem, Error}; + use halo2_middleware::circuit::Advice; #[test] fn not_enough_columns_for_constants() { diff --git a/frontend/src/circuit/floor_planner/v1/strategy.rs b/frontend/src/circuit/floor_planner/v1/strategy.rs index 7b05709d..29d8f431 100644 --- a/frontend/src/circuit/floor_planner/v1/strategy.rs +++ b/frontend/src/circuit/floor_planner/v1/strategy.rs @@ -2,7 +2,8 @@ fn test_slot_in() { use crate::circuit::layouter::RegionShape; use halo2_common::circuit::floor_planner::v1::strategy::slot_in; - use halo2_middleware::circuit::{Any, Column}; + use halo2_common::plonk::circuit::Column; + use halo2_middleware::circuit::Any; let regions = vec![ RegionShape { diff --git a/frontend/src/dev.rs b/frontend/src/dev.rs index 6707a99f..23d56c3f 100644 --- a/frontend/src/dev.rs +++ b/frontend/src/dev.rs @@ -12,14 +12,14 @@ use halo2_middleware::ff::FromUniformBytes; use halo2_common::{ circuit, plonk::{ - circuit::Column, + circuit::{Challenge, Column}, permutation, sealed::{self, SealedPhase}, Assignment, Circuit, ConstraintSystem, Error, Expression, FirstPhase, FloorPlanner, Phase, Selector, }, }; -use halo2_middleware::circuit::{Advice, Any, Challenge, ColumnMid, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, ColumnMid, Fixed, Instance}; use halo2_middleware::plonk::Assigned; use halo2_common::multicore::{ @@ -1252,9 +1252,9 @@ mod tests { use super::{FailureLocation, MockProver, VerifyFailure}; use crate::circuit::{Layouter, SimpleFloorPlanner, Value}; use halo2_common::plonk::{ - Circuit, ConstraintSystem, Error, Expression, Selector, TableColumn, + circuit::Column, Circuit, ConstraintSystem, Error, Expression, Selector, TableColumn, }; - use halo2_middleware::circuit::{Advice, Any, Column, Fixed, Instance}; + use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::poly::Rotation; #[test] diff --git a/frontend/src/dev/cost.rs b/frontend/src/dev/cost.rs index 36c0f686..9452975c 100644 --- a/frontend/src/dev/cost.rs +++ b/frontend/src/dev/cost.rs @@ -15,10 +15,11 @@ use halo2_middleware::poly::Rotation; use halo2_common::{ circuit::{layouter::RegionColumn, Value}, plonk::{ - circuit::Column, Assignment, Circuit, ConstraintSystem, Error, FloorPlanner, Selector, + circuit::{Challenge, Column}, + Assignment, Circuit, ConstraintSystem, Error, FloorPlanner, Selector, }, }; -use halo2_middleware::circuit::{Advice, Any, Challenge, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::plonk::Assigned; /// Measures a circuit to determine its costs, and explain what contributes to them. diff --git a/frontend/src/dev/tfp.rs b/frontend/src/dev/tfp.rs index 72c3ea27..8729d33a 100644 --- a/frontend/src/dev/tfp.rs +++ b/frontend/src/dev/tfp.rs @@ -8,9 +8,10 @@ use halo2_common::circuit::{ AssignedCell, Cell, Layouter, Region, Table, Value, }; use halo2_common::plonk::{ - circuit::Column, Assignment, Circuit, ConstraintSystem, Error, FloorPlanner, Selector, + circuit::{Challenge, Column}, + Assignment, Circuit, ConstraintSystem, Error, FloorPlanner, Selector, }; -use halo2_middleware::circuit::{Advice, Any, Challenge, Fixed, Instance}; +use halo2_middleware::circuit::{Advice, Any, Fixed, Instance}; use halo2_middleware::plonk::Assigned; /// A helper type that augments a [`FloorPlanner`] with [`tracing`] spans and events. diff --git a/halo2_proofs/src/plonk.rs b/halo2_proofs/src/plonk.rs index bc477308..6fbaf263 100644 --- a/halo2_proofs/src/plonk.rs +++ b/halo2_proofs/src/plonk.rs @@ -11,8 +11,9 @@ pub use verifier::verify_proof; pub use halo2_backend::plonk::{ProvingKey, VerifyingKey}; pub use halo2_common::plonk::{ - circuit::Column, Circuit, ConstraintSystem, Error, Expression, FirstPhase, SecondPhase, - Selector, TableColumn, ThirdPhase, + circuit::{Challenge, Column}, + Circuit, ConstraintSystem, Error, Expression, FirstPhase, SecondPhase, Selector, TableColumn, + ThirdPhase, }; -pub use halo2_middleware::circuit::{Advice, Challenge, Fixed, Instance}; +pub use halo2_middleware::circuit::{Advice, Fixed, Instance}; pub use halo2_middleware::plonk::Assigned; diff --git a/halo2_proofs/tests/frontend_backend_split.rs b/halo2_proofs/tests/frontend_backend_split.rs index fb97416e..c647fa2e 100644 --- a/halo2_proofs/tests/frontend_backend_split.rs +++ b/halo2_proofs/tests/frontend_backend_split.rs @@ -13,8 +13,8 @@ use halo2_backend::plonk::{ use halo2_common::{ circuit::{AssignedCell, Layouter, Region, SimpleFloorPlanner, Value}, plonk::{ - circuit::Column, Circuit, ConstraintSystem, Error, Expression, FirstPhase, SecondPhase, - Selector, + circuit::{Challenge, Column}, + Circuit, ConstraintSystem, Error, Expression, FirstPhase, SecondPhase, Selector, }, transcript::{ Blake2bRead, Blake2bWrite, Challenge255, TranscriptReadBuffer, TranscriptWriterBuffer, @@ -25,7 +25,7 @@ use halo2_frontend::{ dev::MockProver, }; use halo2_middleware::{ - circuit::{Advice, Challenge, Fixed, Instance}, + circuit::{Advice, Fixed, Instance}, ff::Field, poly::Rotation, }; diff --git a/middleware/src/circuit.rs b/middleware/src/circuit.rs index 918b307b..004e86d4 100644 --- a/middleware/src/circuit.rs +++ b/middleware/src/circuit.rs @@ -35,12 +35,12 @@ pub struct InstanceQueryMid { /// A challenge squeezed from transcript after advice columns at the phase have been committed. #[derive(Clone, Copy, Debug, Eq, PartialEq, Hash)] -pub struct Challenge { +pub struct ChallengeMid { pub index: usize, pub phase: u8, } -impl Challenge { +impl ChallengeMid { /// Index of this challenge. pub fn index(&self) -> usize { self.index @@ -51,10 +51,10 @@ impl Challenge { self.phase } - /// Return Expression - pub fn expr(&self) -> ExpressionMid { - ExpressionMid::Challenge(*self) - } + // /// Return Expression + // pub fn expr(&self) -> ExpressionMid { + // ExpressionMid::Challenge(*self) + // } } /// Low-degree expression representing an identity that must hold over the committed columns. @@ -69,7 +69,7 @@ pub enum ExpressionMid { /// This is an instance (external) column queried at a certain relative location Instance(InstanceQueryMid), /// This is a challenge - Challenge(Challenge), + Challenge(ChallengeMid), /// This is a negated polynomial Negated(Box>), /// This is the sum of two polynomials @@ -166,6 +166,12 @@ pub struct CompiledCircuitV2 { pub cs: ConstraintSystemV2Backend, } +// 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. /// A column type pub trait ColumnType: 'static + Sized + Copy + std::fmt::Debug + PartialEq + Eq + Into