From f3843ee9e1d78d4e993fbd16f8639624b193e756 Mon Sep 17 00:00:00 2001 From: adria0 Date: Mon, 17 Jun 2024 11:42:26 +0200 Subject: [PATCH] some refactor --- halo2_frontend/src/circuit.rs | 53 +++++++------ .../src/circuit/floor_planner/single_pass.rs | 20 ++--- .../src/circuit/floor_planner/v1.rs | 34 +++++---- halo2_frontend/src/circuit/layouter.rs | 8 +- halo2_frontend/src/circuit/table_layouter.rs | 14 ++-- halo2_frontend/src/circuit/value.rs | 44 +++++------ halo2_frontend/src/dev.rs | 28 +++---- halo2_frontend/src/dev/cost.rs | 13 ++-- halo2_frontend/src/dev/cost_model.rs | 11 ++- halo2_frontend/src/dev/failure.rs | 18 ++--- halo2_frontend/src/dev/failure/emitter.rs | 4 +- halo2_frontend/src/dev/gates.rs | 6 +- halo2_frontend/src/dev/graph.rs | 9 +-- halo2_frontend/src/dev/graph/layout.rs | 5 +- halo2_frontend/src/dev/tfp.rs | 43 ++++++----- halo2_frontend/src/dev/util.rs | 12 +-- halo2_frontend/src/lib.rs | 2 +- halo2_frontend/src/plonk/assigned.rs | 74 +++++++++--------- halo2_frontend/src/plonk/circuit.rs | 16 ++-- .../src/plonk/circuit/compress_selectors.rs | 8 +- .../src/plonk/circuit/constraint_system.rs | 36 ++++----- .../src/plonk/circuit/expression.rs | 64 ++++++++-------- halo2_frontend/src/plonk/keygen.rs | 6 +- halo2_frontend/src/plonk/lookup.rs | 8 +- halo2_frontend/src/plonk/shuffle.rs | 8 +- halo2_proofs/CHANGELOG.md | 2 +- halo2_proofs/benches/dev_lookup.rs | 6 +- halo2_proofs/benches/plonk.rs | 24 ++++-- halo2_proofs/examples/circuit-layout.rs | 6 +- halo2_proofs/examples/simple-example.rs | 18 ++--- halo2_proofs/examples/two-chip.rs | 42 +++++----- halo2_proofs/examples/vector-mul.rs | 18 ++--- halo2_proofs/src/plonk.rs | 9 +-- halo2_proofs/src/plonk/keygen.rs | 10 +-- halo2_proofs/src/plonk/prover.rs | 76 ++++++++++++++----- halo2_proofs/tests/compress_selectors.rs | 27 ++++--- halo2_proofs/tests/plonk_api.rsx | 6 +- halo2_proofs/tests/shuffle.rsx | 14 ++-- halo2_proofs/tests/shuffle_api.rsx | 8 +- halo2_proofs/tests/vector-ops-unblinded.rsx | 32 ++++---- 40 files changed, 454 insertions(+), 388 deletions(-) diff --git a/halo2_frontend/src/circuit.rs b/halo2_frontend/src/circuit.rs index cff44823b1..399ee98cbc 100644 --- a/halo2_frontend/src/circuit.rs +++ b/halo2_frontend/src/circuit.rs @@ -29,8 +29,8 @@ pub use floor_planner::single_pass::SimpleFloorPlanner; pub mod layouter; +use crate::plonk::FieldFront; pub use table_layouter::{SimpleTableLayouter, TableLayouter}; -use crate::plonk::FieldFr; /// Compile a circuit. Runs configure and synthesize on the circuit in order to materialize the /// circuit into its columns and the column configuration; as well as doing the fixed column and @@ -38,7 +38,7 @@ use crate::plonk::FieldFr; /// generation, and proof generation. /// If `compress_selectors` is true, multiple selector columns may be multiplexed. #[allow(clippy::type_complexity)] -pub fn compile_circuit, F: Field, ConcreteCircuit: Circuit>( +pub fn compile_circuit, F: Field, ConcreteCircuit: Circuit>( k: u32, circuit: &ConcreteCircuit, compress_selectors: bool, @@ -99,7 +99,10 @@ pub fn compile_circuit, F: Field, ConcreteCircuit: Circuit< permutation: halo2_middleware::permutation::AssemblyMid { copies: assembly.permutation.copies, }, - fixed: fixed.into_iter().map(|f| f.into_iter().map(|f| f.into_field()).collect()).collect(), + fixed: fixed + .into_iter() + .map(|f| f.into_iter().map(|f| f.into_field()).collect()) + .collect(), }; Ok(( @@ -112,7 +115,7 @@ pub fn compile_circuit, F: Field, ConcreteCircuit: Circuit< )) } -struct WitnessCollection<'a, F: FieldFr> { +struct WitnessCollection<'a, F: FieldFront> { k: u32, current_phase: sealed::Phase, advice_column_phase: &'a Vec, @@ -122,7 +125,7 @@ struct WitnessCollection<'a, F: FieldFr> { usable_rows: RangeTo, } -impl<'a, F: FieldFr> Assignment for WitnessCollection<'a, F> { +impl<'a, F: FieldFront> Assignment for WitnessCollection<'a, F> { fn enter_region(&mut self, _: N) where NR: Into, @@ -253,7 +256,7 @@ impl<'a, F: FieldFr> Assignment for WitnessCollection<'a, F> { /// Witness calculator. Frontend function #[derive(Debug)] -pub struct WitnessCalculator<'a, F: FieldFr, ConcreteCircuit: Circuit> { +pub struct WitnessCalculator<'a, F: FieldFront, ConcreteCircuit: Circuit> { k: u32, n: usize, unusable_rows_start: usize, @@ -264,7 +267,7 @@ pub struct WitnessCalculator<'a, F: FieldFr, ConcreteCircuit: Circuit> { next_phase: u8, } -impl<'a, F: FieldFr, ConcreteCircuit: Circuit> WitnessCalculator<'a, F, ConcreteCircuit> { +impl<'a, F: FieldFront, ConcreteCircuit: Circuit> WitnessCalculator<'a, F, ConcreteCircuit> { /// Create a new WitnessCalculator pub fn new( k: u32, @@ -361,7 +364,7 @@ impl<'a, F: FieldFr, ConcreteCircuit: Circuit> WitnessCalculator<'a, F, Concr // Turn vectors of `Assigned` into vectors of `F` by evaluation the divisions in `Assigned` // using batched inversions. -fn batch_invert_assigned(assigned: Vec>>) -> Vec> { +fn batch_invert_assigned(assigned: Vec>>) -> Vec> { let mut assigned_denominators: Vec<_> = assigned .iter() .map(|f| { @@ -393,7 +396,7 @@ fn batch_invert_assigned(assigned: Vec>>) -> Vec` into a vector of F by multiplying each numerator with the elements // from `inv_denoms`, assuming that `inv_denoms` are the inverted denominators of the // `Assigned`. -fn poly_invert( +fn poly_invert( poly: &[Assigned], inv_denoms: impl ExactSizeIterator, ) -> Vec { @@ -412,7 +415,7 @@ fn poly_invert( /// The chip also loads any fixed configuration needed at synthesis time /// using its own implementation of `load`, and stores it in [`Chip::Loaded`]. /// This can be accessed via [`Chip::loaded`]. -pub trait Chip: Sized { +pub trait Chip: Sized { /// A type that holds the configuration for this chip, and any other state it may need /// during circuit synthesis, that can be derived during [`Circuit::configure`]. /// @@ -484,13 +487,13 @@ pub struct Cell { /// An assigned cell. #[derive(Clone, Debug)] -pub struct AssignedCell { +pub struct AssignedCell { value: Value, cell: Cell, _marker: PhantomData, } -impl AssignedCell { +impl AssignedCell { /// Returns the value of the [`AssignedCell`]. pub fn value(&self) -> Value<&V> { self.value.as_ref() @@ -502,7 +505,7 @@ impl AssignedCell { } } -impl AssignedCell +impl AssignedCell where for<'v> Assigned: From<&'v V>, { @@ -512,7 +515,7 @@ where } } -impl AssignedCell, F> { +impl AssignedCell, F> { /// Evaluates this assigned cell's value directly, performing an unbatched inversion /// if necessary. /// @@ -526,7 +529,7 @@ impl AssignedCell, F> { } } -impl AssignedCell +impl AssignedCell where for<'v> Assigned: From<&'v V>, { @@ -565,17 +568,17 @@ where /// "logical" columns that are guaranteed to correspond to the chip (and have come from /// `Chip::Config`). #[derive(Debug)] -pub struct Region<'r, F: FieldFr> { +pub struct Region<'r, F: FieldFront> { region: &'r mut dyn layouter::RegionLayouter, } -impl<'r, F: FieldFr> From<&'r mut dyn layouter::RegionLayouter> for Region<'r, F> { +impl<'r, F: FieldFront> From<&'r mut dyn layouter::RegionLayouter> for Region<'r, F> { fn from(region: &'r mut dyn layouter::RegionLayouter) -> Self { Region { region } } } -impl<'r, F: FieldFr> Region<'r, F> { +impl<'r, F: FieldFront> Region<'r, F> { /// Enables a selector at the given offset. pub fn enable_selector( &mut self, @@ -768,17 +771,17 @@ impl<'r, F: FieldFr> Region<'r, F> { /// A lookup table in the circuit. #[derive(Debug)] -pub struct Table<'r, F: FieldFr> { +pub struct Table<'r, F: FieldFront> { table: &'r mut dyn TableLayouter, } -impl<'r, F: FieldFr> From<&'r mut dyn TableLayouter> for Table<'r, F> { +impl<'r, F: FieldFront> From<&'r mut dyn TableLayouter> for Table<'r, F> { fn from(table: &'r mut dyn TableLayouter) -> Self { Table { table } } } -impl<'r, F: FieldFr> Table<'r, F> { +impl<'r, F: FieldFront> Table<'r, F> { /// Assigns a fixed value to a table cell. /// /// Returns an error if the table cell has already been assigned to. @@ -809,7 +812,7 @@ impl<'r, F: FieldFr> Table<'r, F> { /// /// This abstracts over the circuit assignments, handling row indices etc. /// -pub trait Layouter { +pub trait Layouter { /// Represents the type of the "root" of this layouter, so that nested namespaces /// can minimize indirection. type Root: Layouter; @@ -893,9 +896,9 @@ pub trait Layouter { /// This is a "namespaced" layouter which borrows a `Layouter` (pushing a namespace /// context) and, when dropped, pops out of the namespace context. #[derive(Debug)] -pub struct NamespacedLayouter<'a, F: FieldFr, L: Layouter + 'a>(&'a mut L, PhantomData); +pub struct NamespacedLayouter<'a, F: FieldFront, L: Layouter + 'a>(&'a mut L, PhantomData); -impl<'a, F: FieldFr, L: Layouter + 'a> Layouter for NamespacedLayouter<'a, F, L> { +impl<'a, F: FieldFront, L: Layouter + 'a> Layouter for NamespacedLayouter<'a, F, L> { type Root = L::Root; fn assign_region(&mut self, name: N, assignment: A) -> Result @@ -946,7 +949,7 @@ impl<'a, F: FieldFr, L: Layouter + 'a> Layouter for NamespacedLayouter<'a, } } -impl<'a, F: FieldFr, L: Layouter + 'a> Drop for NamespacedLayouter<'a, F, L> { +impl<'a, F: FieldFront, L: Layouter + 'a> Drop for NamespacedLayouter<'a, F, L> { fn drop(&mut self) { let gadget_name = { #[cfg(feature = "gadget-traces")] diff --git a/halo2_frontend/src/circuit/floor_planner/single_pass.rs b/halo2_frontend/src/circuit/floor_planner/single_pass.rs index 659b42a0d5..9d317f539e 100644 --- a/halo2_frontend/src/circuit/floor_planner/single_pass.rs +++ b/halo2_frontend/src/circuit/floor_planner/single_pass.rs @@ -5,7 +5,7 @@ use std::marker::PhantomData; use halo2_middleware::circuit::Any; -use crate::plonk::{Assigned, FieldFr}; +use crate::plonk::{Assigned, FieldFront}; use crate::{ circuit::{ layouter::{RegionColumn, RegionLayouter, RegionShape, SyncDeps, TableLayouter}, @@ -27,7 +27,7 @@ use crate::{ pub struct SimpleFloorPlanner; impl FloorPlanner for SimpleFloorPlanner { - fn synthesize + SyncDeps, C: Circuit>( + fn synthesize + SyncDeps, C: Circuit>( cs: &mut CS, circuit: &C, config: C::Config, @@ -39,7 +39,7 @@ impl FloorPlanner for SimpleFloorPlanner { } /// A [`Layouter`] for a single-chip circuit. -pub struct SingleChipLayouter<'a, F: FieldFr, CS: Assignment + 'a> { +pub struct SingleChipLayouter<'a, F: FieldFront, CS: Assignment + 'a> { cs: &'a mut CS, constants: Vec>, /// Stores the starting row for each region. @@ -51,7 +51,7 @@ pub struct SingleChipLayouter<'a, F: FieldFr, CS: Assignment + 'a> { _marker: PhantomData, } -impl<'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for SingleChipLayouter<'a, F, CS> { +impl<'a, F: FieldFront, CS: Assignment + 'a> fmt::Debug for SingleChipLayouter<'a, F, CS> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SingleChipLayouter") .field("regions", &self.regions) @@ -60,7 +60,7 @@ impl<'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for SingleChipLayouter<' } } -impl<'a, F: FieldFr, CS: Assignment> SingleChipLayouter<'a, F, CS> { +impl<'a, F: FieldFront, CS: Assignment> SingleChipLayouter<'a, F, CS> { /// Creates a new single-chip layouter. pub fn new(cs: &'a mut CS, constants: Vec>) -> Result { let ret = SingleChipLayouter { @@ -75,7 +75,7 @@ impl<'a, F: FieldFr, CS: Assignment> SingleChipLayouter<'a, F, CS> { } } -impl<'a, F: FieldFr, CS: Assignment + 'a + SyncDeps> Layouter +impl<'a, F: FieldFront, CS: Assignment + 'a + SyncDeps> Layouter for SingleChipLayouter<'a, F, CS> { type Root = Self; @@ -222,14 +222,14 @@ impl<'a, F: FieldFr, CS: Assignment + 'a + SyncDeps> Layouter } } -struct SingleChipLayouterRegion<'r, 'a, F: FieldFr, CS: Assignment + 'a> { +struct SingleChipLayouterRegion<'r, 'a, F: FieldFront, CS: Assignment + 'a> { layouter: &'r mut SingleChipLayouter<'a, F, CS>, region_index: RegionIndex, /// Stores the constants to be assigned, and the cells to which they are copied. constants: Vec<(Assigned, Cell)>, } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> fmt::Debug for SingleChipLayouterRegion<'r, 'a, F, CS> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { @@ -240,7 +240,7 @@ impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug } } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> SingleChipLayouterRegion<'r, 'a, F, CS> { +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> SingleChipLayouterRegion<'r, 'a, F, CS> { fn new(layouter: &'r mut SingleChipLayouter<'a, F, CS>, region_index: RegionIndex) -> Self { SingleChipLayouterRegion { layouter, @@ -250,7 +250,7 @@ impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> SingleChipLayouterRegion<'r, 'a } } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a + SyncDeps> RegionLayouter +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a + SyncDeps> RegionLayouter for SingleChipLayouterRegion<'r, 'a, F, CS> { fn enable_selector<'v>( diff --git a/halo2_frontend/src/circuit/floor_planner/v1.rs b/halo2_frontend/src/circuit/floor_planner/v1.rs index 94a0b78d2a..9445aa2141 100644 --- a/halo2_frontend/src/circuit/floor_planner/v1.rs +++ b/halo2_frontend/src/circuit/floor_planner/v1.rs @@ -2,7 +2,7 @@ use std::fmt; use halo2_middleware::circuit::Any; -use crate::plonk::{Assigned, FieldFr}; +use crate::plonk::{Assigned, FieldFront}; use crate::{ circuit::{ layouter::{RegionColumn, RegionLayouter, RegionShape, SyncDeps, TableLayouter}, @@ -28,7 +28,7 @@ pub mod strategy; #[derive(Debug)] pub struct V1; -struct V1Plan<'a, F: FieldFr, CS: Assignment + 'a> { +struct V1Plan<'a, F: FieldFront, CS: Assignment + 'a> { cs: &'a mut CS, /// Stores the starting row for each region. regions: Vec, @@ -38,13 +38,13 @@ struct V1Plan<'a, F: FieldFr, CS: Assignment + 'a> { table_columns: Vec, } -impl<'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for V1Plan<'a, F, CS> { +impl<'a, F: FieldFront, CS: Assignment + 'a> fmt::Debug for V1Plan<'a, F, CS> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("floor_planner::V1Plan").finish() } } -impl<'a, F: FieldFr, CS: Assignment + SyncDeps> V1Plan<'a, F, CS> { +impl<'a, F: FieldFront, CS: Assignment + SyncDeps> V1Plan<'a, F, CS> { /// Creates a new v1 layouter. pub fn new(cs: &'a mut CS) -> Result { let ret = V1Plan { @@ -58,7 +58,7 @@ impl<'a, F: FieldFr, CS: Assignment + SyncDeps> V1Plan<'a, F, CS> { } impl FloorPlanner for V1 { - fn synthesize + SyncDeps, C: Circuit>( + fn synthesize + SyncDeps, C: Circuit>( cs: &mut CS, circuit: &C, config: C::Config, @@ -142,16 +142,16 @@ impl FloorPlanner for V1 { } #[derive(Debug)] -enum Pass<'p, 'a, F: FieldFr, CS: Assignment + 'a> { +enum Pass<'p, 'a, F: FieldFront, CS: Assignment + 'a> { Measurement(&'p mut MeasurementPass), Assignment(&'p mut AssignmentPass<'p, 'a, F, CS>), } /// A single pass of the [`V1`] layouter. #[derive(Debug)] -pub struct V1Pass<'p, 'a, F: FieldFr, CS: Assignment + 'a>(Pass<'p, 'a, F, CS>); +pub struct V1Pass<'p, 'a, F: FieldFront, CS: Assignment + 'a>(Pass<'p, 'a, F, CS>); -impl<'p, 'a, F: FieldFr, CS: Assignment + 'a> V1Pass<'p, 'a, F, CS> { +impl<'p, 'a, F: FieldFront, CS: Assignment + 'a> V1Pass<'p, 'a, F, CS> { fn measure(pass: &'p mut MeasurementPass) -> Self { V1Pass(Pass::Measurement(pass)) } @@ -161,7 +161,7 @@ impl<'p, 'a, F: FieldFr, CS: Assignment + 'a> V1Pass<'p, 'a, F, CS> { } } -impl<'p, 'a, F: FieldFr, CS: Assignment + SyncDeps> Layouter for V1Pass<'p, 'a, F, CS> { +impl<'p, 'a, F: FieldFront, CS: Assignment + SyncDeps> Layouter for V1Pass<'p, 'a, F, CS> { type Root = Self; fn assign_region(&mut self, name: N, assignment: A) -> Result @@ -239,7 +239,7 @@ impl MeasurementPass { MeasurementPass { regions: vec![] } } - fn assign_region(&mut self, mut assignment: A) -> Result + fn assign_region(&mut self, mut assignment: A) -> Result where A: FnMut(Region<'_, F>) -> Result, { @@ -259,13 +259,13 @@ impl MeasurementPass { /// Assigns the circuit. #[derive(Debug)] -pub struct AssignmentPass<'p, 'a, F: FieldFr, CS: Assignment + 'a> { +pub struct AssignmentPass<'p, 'a, F: FieldFront, CS: Assignment + 'a> { plan: &'p mut V1Plan<'a, F, CS>, /// Counter tracking which region we need to assign next. region_index: usize, } -impl<'p, 'a, F: FieldFr, CS: Assignment + SyncDeps> AssignmentPass<'p, 'a, F, CS> { +impl<'p, 'a, F: FieldFront, CS: Assignment + SyncDeps> AssignmentPass<'p, 'a, F, CS> { fn new(plan: &'p mut V1Plan<'a, F, CS>) -> Self { AssignmentPass { plan, @@ -348,12 +348,12 @@ impl<'p, 'a, F: FieldFr, CS: Assignment + SyncDeps> AssignmentPass<'p, 'a, F, } } -struct V1Region<'r, 'a, F: FieldFr, CS: Assignment + 'a> { +struct V1Region<'r, 'a, F: FieldFront, CS: Assignment + 'a> { plan: &'r mut V1Plan<'a, F, CS>, region_index: RegionIndex, } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for V1Region<'r, 'a, F, CS> { +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> fmt::Debug for V1Region<'r, 'a, F, CS> { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("V1Region") .field("plan", &self.plan) @@ -362,13 +362,15 @@ impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for V1Region<'r, 'a, } } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> V1Region<'r, 'a, F, CS> { +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> V1Region<'r, 'a, F, CS> { fn new(plan: &'r mut V1Plan<'a, F, CS>, region_index: RegionIndex) -> Self { V1Region { plan, region_index } } } -impl<'r, 'a, F: FieldFr, CS: Assignment + SyncDeps> RegionLayouter for V1Region<'r, 'a, F, CS> { +impl<'r, 'a, F: FieldFront, CS: Assignment + SyncDeps> RegionLayouter + for V1Region<'r, 'a, F, CS> +{ fn enable_selector<'v>( &'v mut self, annotation: &'v (dyn Fn() -> String + 'v), diff --git a/halo2_frontend/src/circuit/layouter.rs b/halo2_frontend/src/circuit/layouter.rs index 796ff6b13f..ce8c3d74f9 100644 --- a/halo2_frontend/src/circuit/layouter.rs +++ b/halo2_frontend/src/circuit/layouter.rs @@ -4,8 +4,8 @@ use std::cmp; use std::collections::HashSet; use std::fmt; +use crate::plonk::FieldFront; use halo2_middleware::circuit::Any; -use crate::plonk::FieldFr; pub use super::table_layouter::TableLayouter; use super::{Cell, RegionIndex, Value}; @@ -31,7 +31,7 @@ impl SyncDeps for T {} /// This trait is used for implementing region assignments: /// /// ```ignore -/// impl<'a, F: FieldFr, C: Chip, CS: Assignment + 'a> Layouter for MyLayouter<'a, C, CS> { +/// impl<'a, F: FieldFront, C: Chip, CS: Assignment + 'a> Layouter for MyLayouter<'a, C, CS> { /// fn assign_region( /// &mut self, /// assignment: impl FnOnce(Region<'_, F, C>) -> Result<(), Error>, @@ -56,7 +56,7 @@ impl SyncDeps for T {} /// `Chip::Config`). /// /// [`Layouter`]: super::Layouter -pub trait RegionLayouter: fmt::Debug + SyncDeps { +pub trait RegionLayouter: fmt::Debug + SyncDeps { /// Enables a selector at the given offset. fn enable_selector<'v>( &'v mut self, @@ -210,7 +210,7 @@ impl RegionShape { } } -impl RegionLayouter for RegionShape { +impl RegionLayouter for RegionShape { fn enable_selector<'v>( &'v mut self, _: &'v (dyn Fn() -> String + 'v), diff --git a/halo2_frontend/src/circuit/table_layouter.rs b/halo2_frontend/src/circuit/table_layouter.rs index cab58b56a1..1c871b42ac 100644 --- a/halo2_frontend/src/circuit/table_layouter.rs +++ b/halo2_frontend/src/circuit/table_layouter.rs @@ -6,15 +6,15 @@ use std::{ }; use super::Value; +use crate::plonk::FieldFront; use crate::plonk::{Assigned, Assignment, Error, TableColumn, TableError}; -use crate::plonk::FieldFr; /// Helper trait for implementing a custom [`Layouter`]. /// /// This trait is used for implementing table assignments. /// /// [`Layouter`]: super::Layouter -pub trait TableLayouter: std::fmt::Debug { +pub trait TableLayouter: std::fmt::Debug { /// Assigns a fixed value to a table cell. /// /// Returns an error if the table cell has already been assigned to. @@ -37,14 +37,16 @@ pub trait TableLayouter: std::fmt::Debug { type DefaultTableValue = Option>>; /// A table layouter that can be used to assign values to a table. -pub struct SimpleTableLayouter<'r, 'a, F: FieldFr, CS: Assignment + 'a> { +pub struct SimpleTableLayouter<'r, 'a, F: FieldFront, CS: Assignment + 'a> { cs: &'a mut CS, used_columns: &'r [TableColumn], /// maps from a fixed column to a pair (default value, vector saying which rows are assigned) pub default_and_assigned: HashMap, Vec)>, } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for SimpleTableLayouter<'r, 'a, F, CS> { +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> fmt::Debug + for SimpleTableLayouter<'r, 'a, F, CS> +{ fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("SimpleTableLayouter") .field("used_columns", &self.used_columns) @@ -53,7 +55,7 @@ impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> fmt::Debug for SimpleTableLayou } } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> SimpleTableLayouter<'r, 'a, F, CS> { +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> SimpleTableLayouter<'r, 'a, F, CS> { /// Returns a new SimpleTableLayouter pub fn new(cs: &'a mut CS, used_columns: &'r [TableColumn]) -> Self { SimpleTableLayouter { @@ -64,7 +66,7 @@ impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> SimpleTableLayouter<'r, 'a, F, } } -impl<'r, 'a, F: FieldFr, CS: Assignment + 'a> TableLayouter +impl<'r, 'a, F: FieldFront, CS: Assignment + 'a> TableLayouter for SimpleTableLayouter<'r, 'a, F, CS> { fn assign_cell<'v>( diff --git a/halo2_frontend/src/circuit/value.rs b/halo2_frontend/src/circuit/value.rs index 3e8415ab10..e0adc97e54 100644 --- a/halo2_frontend/src/circuit/value.rs +++ b/halo2_frontend/src/circuit/value.rs @@ -1,7 +1,7 @@ use std::borrow::Borrow; use std::ops::{Add, Mul, Neg, Sub}; -use crate::plonk::FieldFr; +use crate::plonk::FieldFront; use crate::plonk::{Assigned, Error}; @@ -523,7 +523,7 @@ where // Assigned // -impl From> for Value> { +impl From> for Value> { #[must_use] fn from(value: Value) -> Self { Self { @@ -532,7 +532,7 @@ impl From> for Value> { } } -impl Add> for Value> { +impl Add> for Value> { type Output = Value>; #[must_use] @@ -543,7 +543,7 @@ impl Add> for Value> { } } -impl Add for Value> { +impl Add for Value> { type Output = Value>; #[must_use] @@ -552,7 +552,7 @@ impl Add for Value> { } } -impl Add> for Value<&Assigned> { +impl Add> for Value<&Assigned> { type Output = Value>; #[must_use] @@ -563,7 +563,7 @@ impl Add> for Value<&Assigned> { } } -impl Add for Value<&Assigned> { +impl Add for Value<&Assigned> { type Output = Value>; #[must_use] @@ -572,7 +572,7 @@ impl Add for Value<&Assigned> { } } -impl Sub> for Value> { +impl Sub> for Value> { type Output = Value>; #[must_use] @@ -583,7 +583,7 @@ impl Sub> for Value> { } } -impl Sub for Value> { +impl Sub for Value> { type Output = Value>; #[must_use] @@ -592,7 +592,7 @@ impl Sub for Value> { } } -impl Sub> for Value<&Assigned> { +impl Sub> for Value<&Assigned> { type Output = Value>; #[must_use] @@ -603,7 +603,7 @@ impl Sub> for Value<&Assigned> { } } -impl Sub for Value<&Assigned> { +impl Sub for Value<&Assigned> { type Output = Value>; #[must_use] @@ -612,7 +612,7 @@ impl Sub for Value<&Assigned> { } } -impl Mul> for Value> { +impl Mul> for Value> { type Output = Value>; #[must_use] @@ -623,7 +623,7 @@ impl Mul> for Value> { } } -impl Mul for Value> { +impl Mul for Value> { type Output = Value>; #[must_use] @@ -632,7 +632,7 @@ impl Mul for Value> { } } -impl Mul> for Value<&Assigned> { +impl Mul> for Value<&Assigned> { type Output = Value>; #[must_use] @@ -643,7 +643,7 @@ impl Mul> for Value<&Assigned> { } } -impl Mul for Value<&Assigned> { +impl Mul for Value<&Assigned> { type Output = Value>; #[must_use] @@ -655,7 +655,7 @@ impl Mul for Value<&Assigned> { impl Value { /// Returns the field element corresponding to this value. #[must_use] - pub fn to_field(&self) -> Value> + pub fn to_field(&self) -> Value> where for<'v> Assigned: From<&'v V>, { @@ -666,7 +666,7 @@ impl Value { /// Returns the field element corresponding to this value. #[must_use] - pub fn into_field(self) -> Value> + pub fn into_field(self) -> Value> where V: Into>, { @@ -679,7 +679,7 @@ impl Value { /// /// # Examples /// - /// If you have a `Value`, convert it to `Value>` first: + /// If you have a `Value`, convert it to `Value>` first: /// ``` /// # use halo2curves::pasta::pallas::Base as F; /// use halo2_frontend::circuit::Value; @@ -690,7 +690,7 @@ impl Value { /// let _ = v.double(); /// ``` #[must_use] - pub fn double(&self) -> Value> + pub fn double(&self) -> Value> where V: Borrow>, { @@ -701,7 +701,7 @@ impl Value { /// Squares this field element. #[must_use] - pub fn square(&self) -> Value> + pub fn square(&self) -> Value> where V: Borrow>, { @@ -712,7 +712,7 @@ impl Value { /// Cubes this field element. #[must_use] - pub fn cube(&self) -> Value> + pub fn cube(&self) -> Value> where V: Borrow>, { @@ -723,7 +723,7 @@ impl Value { /// Inverts this assigned value (taking the inverse of zero to be zero). #[must_use] - pub fn invert(&self) -> Value> + pub fn invert(&self) -> Value> where V: Borrow>, { @@ -733,7 +733,7 @@ impl Value { } } -impl Value> { +impl Value> { /// Evaluates this value directly, performing an unbatched inversion if necessary. /// /// If the denominator is zero, the returned value is zero. diff --git a/halo2_frontend/src/dev.rs b/halo2_frontend/src/dev.rs index 7e60c80625..2397147f46 100644 --- a/halo2_frontend/src/dev.rs +++ b/halo2_frontend/src/dev.rs @@ -41,7 +41,7 @@ pub use gates::CircuitGates; mod tfp; pub use tfp::TracingFloorPlanner; -use crate::plonk::FieldFr; +use crate::plonk::FieldFront; #[cfg(feature = "dev-graph")] mod graph; @@ -89,7 +89,7 @@ impl Region { /// The value of a particular cell within the circuit. #[derive(Clone, Copy, Debug, PartialEq, Eq)] -pub enum CellValue { +pub enum CellValue { /// An unassigned cell. Unassigned, /// A cell that has been assigned a value. @@ -100,12 +100,12 @@ pub enum CellValue { /// A value within an expression. #[derive(Clone, Copy, Debug, PartialEq, Eq, Ord, PartialOrd)] -enum Value { +enum Value { Real(F), Poison, } -impl From> for Value { +impl From> for Value { fn from(value: CellValue) -> Self { match value { // Cells that haven't been explicitly assigned to, default to zero. @@ -116,7 +116,7 @@ impl From> for Value { } } -impl Neg for Value { +impl Neg for Value { type Output = Self; fn neg(self) -> Self::Output { @@ -127,7 +127,7 @@ impl Neg for Value { } } -impl Add for Value { +impl Add for Value { type Output = Self; fn add(self, rhs: Self) -> Self::Output { @@ -138,7 +138,7 @@ impl Add for Value { } } -impl Mul for Value { +impl Mul for Value { type Output = Self; fn mul(self, rhs: Self) -> Self::Output { @@ -156,7 +156,7 @@ impl Mul for Value { } } -impl Mul for Value { +impl Mul for Value { type Output = Self; fn mul(self, rhs: F) -> Self::Output { @@ -289,7 +289,7 @@ impl Mul for Value { /// ); /// ``` #[derive(Debug)] -pub struct MockProver { +pub struct MockProver { k: u32, n: u32, cs: ConstraintSystem, @@ -321,14 +321,14 @@ pub struct MockProver { /// Instance Value #[derive(Debug, Clone, PartialEq, Eq)] -pub enum InstanceValue { +pub enum InstanceValue { /// Assigned instance value Assigned(F), /// Padding Padding, } -impl InstanceValue { +impl InstanceValue { /// Field value on the instance cell pub fn value(&self) -> F { match self { @@ -338,13 +338,13 @@ impl InstanceValue { } } -impl MockProver { +impl MockProver { fn in_phase(&self, phase: P) -> bool { self.current_phase == phase.to_sealed() } } -impl Assignment for MockProver { +impl Assignment for MockProver { fn enter_region(&mut self, name: N) where NR: Into, @@ -608,7 +608,7 @@ impl Assignment for MockProver { } } -impl + Ord> MockProver { +impl + Ord> MockProver { /// Runs a synthetic keygen-and-prove operation on the given circuit, collecting data /// about the constraints and their assignments. pub fn run>( diff --git a/halo2_frontend/src/dev/cost.rs b/halo2_frontend/src/dev/cost.rs index df0ca90c57..9e03afc806 100644 --- a/halo2_frontend/src/dev/cost.rs +++ b/halo2_frontend/src/dev/cost.rs @@ -8,10 +8,10 @@ use std::{ ops::{Add, Mul}, }; +use crate::plonk::FieldFront; use group::prime::PrimeGroup; use halo2_middleware::circuit::Any; -use crate::plonk::FieldFr; -use halo2_middleware::ff::{Field, PrimeField}; +use halo2_middleware::ff::PrimeField; use halo2_middleware::poly::Rotation; use crate::{ @@ -25,7 +25,8 @@ use crate::{ /// Measures a circuit to determine its costs, and explain what contributes to them. #[allow(dead_code)] #[derive(Debug)] -pub struct CircuitCost, ConcreteCircuit: Circuit> { +pub struct CircuitCost, ConcreteCircuit: Circuit> +{ /// Power-of-2 bound on the number of rows in the circuit. k: u32, /// Maximum degree of the circuit. @@ -148,7 +149,7 @@ impl Layout { } } -impl Assignment for Layout { +impl Assignment for Layout { fn enter_region(&mut self, name_fn: N) where NR: Into, @@ -267,7 +268,9 @@ impl Assignment for Layout { } } -impl, ConcreteCircuit: Circuit> CircuitCost { +impl, ConcreteCircuit: Circuit> + CircuitCost +{ /// Measures a circuit with parameter constant `k`. /// /// Panics if `k` is not large enough for the circuit. diff --git a/halo2_frontend/src/dev/cost_model.rs b/halo2_frontend/src/dev/cost_model.rs index 793c02f71f..ec415712ce 100644 --- a/halo2_frontend/src/dev/cost_model.rs +++ b/halo2_frontend/src/dev/cost_model.rs @@ -4,8 +4,8 @@ use std::collections::HashSet; use std::{iter, num::ParseIntError, str::FromStr}; -use crate::plonk::{Circuit, FieldFr}; -use halo2_middleware::ff::{Field, FromUniformBytes}; +use crate::plonk::{Circuit, FieldFront}; +use halo2_middleware::ff::FromUniformBytes; use serde::Deserialize; use serde_derive::Serialize; @@ -242,7 +242,7 @@ impl CostOptions { /// Given a Plonk circuit, this function returns a [ModelCircuit] pub fn from_circuit_to_model_circuit< - F: Ord + FieldFr + FromUniformBytes<64>, + F: Ord + FieldFront + FromUniformBytes<64>, C: Circuit, const COMM: usize, const SCALAR: usize, @@ -257,7 +257,10 @@ pub fn from_circuit_to_model_circuit< } /// Given a Plonk circuit, this function returns [CostOptions] -pub fn from_circuit_to_cost_model_options, C: Circuit>( +pub fn from_circuit_to_cost_model_options< + F: Ord + FieldFront + FromUniformBytes<64>, + C: Circuit, +>( k: u32, circuit: &C, instances: Vec>, diff --git a/halo2_frontend/src/dev/failure.rs b/halo2_frontend/src/dev/failure.rs index df04821b93..c1108e7211 100644 --- a/halo2_frontend/src/dev/failure.rs +++ b/halo2_frontend/src/dev/failure.rs @@ -1,7 +1,7 @@ use std::collections::{BTreeMap, HashSet}; use std::fmt::{self, Debug}; -use crate::plonk::FieldFr; +use crate::plonk::FieldFront; use super::metadata::{DebugColumn, DebugVirtualCell}; use super::MockProver; @@ -63,7 +63,7 @@ impl FailureLocation { } } - pub(super) fn find_expressions<'a, F: FieldFr>( + pub(super) fn find_expressions<'a, F: FieldFront>( cs: &ConstraintSystem, regions: &[Region], failure_row: usize, @@ -370,7 +370,7 @@ impl Debug for VerifyFailure { /// /// Gate 'Equality check' (applied at offset 1) queries these cells. /// ``` -fn render_cell_not_assigned( +fn render_cell_not_assigned( gates: &[Gate], gate: &metadata::Gate, region: &metadata::Region, @@ -439,7 +439,7 @@ fn render_cell_not_assigned( /// x0 = 0x5 /// x1 = 0x5 /// ``` -fn render_constraint_not_satisfied( +fn render_constraint_not_satisfied( gates: &[Gate], constraint: &metadata::Constraint, location: &FailureLocation, @@ -504,7 +504,7 @@ fn render_constraint_not_satisfied( /// | x0 = 0x5 /// | x1 = 1 /// ``` -fn render_lookup( +fn render_lookup( prover: &MockProver, name: &str, lookup_index: usize, @@ -570,7 +570,7 @@ fn render_lookup( ) }); - fn cell_value<'a, F: FieldFr, Q: Into + Copy>( + fn cell_value<'a, F: FieldFront, Q: Into + Copy>( load: impl Fn(Q) -> Value + 'a, ) -> impl Fn(Q) -> BTreeMap + 'a { move |query| { @@ -672,7 +672,7 @@ fn render_lookup( } } -fn render_shuffle( +fn render_shuffle( prover: &MockProver, name: &str, shuffle_index: usize, @@ -736,7 +736,7 @@ fn render_shuffle( ) }); - fn cell_value<'a, F: FieldFr, Q: Into + Copy>( + fn cell_value<'a, F: FieldFront, Q: Into + Copy>( load: impl Fn(Q) -> Value + 'a, ) -> impl Fn(Q) -> BTreeMap + 'a { move |query| { @@ -839,7 +839,7 @@ fn render_shuffle( impl VerifyFailure { /// Emits this failure in pretty-printed format to stderr. - pub fn emit(&self, prover: &MockProver) { + pub fn emit(&self, prover: &MockProver) { match self { Self::CellNotAssigned { gate, diff --git a/halo2_frontend/src/dev/failure/emitter.rs b/halo2_frontend/src/dev/failure/emitter.rs index 71fd5e0752..1a32910776 100644 --- a/halo2_frontend/src/dev/failure/emitter.rs +++ b/halo2_frontend/src/dev/failure/emitter.rs @@ -1,6 +1,6 @@ +use crate::plonk::FieldFront; use std::collections::BTreeMap; use std::iter; -use crate::plonk::FieldFr; use super::FailureLocation; use crate::dev::{metadata, util}; @@ -136,7 +136,7 @@ pub(super) fn render_cell_layout( } } -pub(super) fn expression_to_string( +pub(super) fn expression_to_string( expr: &Expression, layout: &BTreeMap>, ) -> String { diff --git a/halo2_frontend/src/dev/gates.rs b/halo2_frontend/src/dev/gates.rs index 524e42c3de..54fb8c4712 100644 --- a/halo2_frontend/src/dev/gates.rs +++ b/halo2_frontend/src/dev/gates.rs @@ -3,8 +3,8 @@ use std::{ fmt::{self, Write}, }; -use crate::{dev::util, plonk::FieldFr}; use crate::plonk::{sealed::SealedPhase, Circuit, ConstraintSystem, FirstPhase}; +use crate::{dev::util, plonk::FieldFront}; #[derive(Debug)] struct Constraint { @@ -39,7 +39,7 @@ struct Gate { /// #[derive(Clone, Default)] /// struct MyCircuit {} /// -/// impl Circuit for MyCircuit { +/// impl Circuit for MyCircuit { /// type Config = MyConfig; /// type FloorPlanner = SimpleFloorPlanner; /// #[cfg(feature = "circuit-params")] @@ -101,7 +101,7 @@ pub struct CircuitGates { impl CircuitGates { /// Collects the gates from within the circuit. - pub fn collect>( + pub fn collect>( #[cfg(feature = "circuit-params")] params: C::Params, ) -> Self { // Collect the graph details. diff --git a/halo2_frontend/src/dev/graph.rs b/halo2_frontend/src/dev/graph.rs index 045df059cd..44837c26a4 100644 --- a/halo2_frontend/src/dev/graph.rs +++ b/halo2_frontend/src/dev/graph.rs @@ -1,9 +1,8 @@ use crate::plonk::{ - Advice, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, Fixed, - FloorPlanner, Instance, Selector, FieldFr + Advice, Assigned, Assignment, Challenge, Circuit, Column, ConstraintSystem, Error, FieldFront, + Fixed, FloorPlanner, Instance, Selector, }; use halo2_middleware::circuit::Any; -use halo2_middleware::ff::Field; use tabbycat::{AttrList, Edge, GraphBuilder, GraphType, Identity, StmtList}; use crate::circuit::Value; @@ -16,7 +15,7 @@ pub mod layout; /// inside the gadgets and chips that it uses. /// /// [`Layouter::namespace`]: crate::circuit::Layouter#method.namespace -pub fn circuit_dot_graph>( +pub fn circuit_dot_graph>( circuit: &ConcreteCircuit, ) -> String { // Collect the graph details. @@ -79,7 +78,7 @@ struct Graph { current_namespace: Vec, } -impl Assignment for Graph { +impl Assignment for Graph { fn enter_region(&mut self, _: N) where NR: Into, diff --git a/halo2_frontend/src/dev/graph/layout.rs b/halo2_frontend/src/dev/graph/layout.rs index 2cb18b66f6..7e72117f12 100644 --- a/halo2_frontend/src/dev/graph/layout.rs +++ b/halo2_frontend/src/dev/graph/layout.rs @@ -1,4 +1,3 @@ -use halo2_middleware::ff::Field; use plotters::{ coord::Shift, prelude::{DrawingArea, DrawingAreaErrorKind, DrawingBackend}, @@ -6,7 +5,7 @@ use plotters::{ use std::collections::HashSet; use std::ops::Range; -use crate::plonk::{Circuit, Column, ConstraintSystem, FloorPlanner, FieldFr}; +use crate::plonk::{Circuit, Column, ConstraintSystem, FieldFront, FloorPlanner}; use crate::{circuit::layouter::RegionColumn, dev::cost::Layout}; use halo2_middleware::circuit::Any; @@ -80,7 +79,7 @@ impl CircuitLayout { } /// Renders the given circuit on the given drawing area. - pub fn render, DB: DrawingBackend>( + pub fn render, DB: DrawingBackend>( self, k: u32, circuit: &ConcreteCircuit, diff --git a/halo2_frontend/src/dev/tfp.rs b/halo2_frontend/src/dev/tfp.rs index 38c096d62d..eecb9cb9f9 100644 --- a/halo2_frontend/src/dev/tfp.rs +++ b/halo2_frontend/src/dev/tfp.rs @@ -3,15 +3,18 @@ use std::{fmt, marker::PhantomData}; use halo2_middleware::circuit::Any; use tracing::{debug, debug_span, span::EnteredSpan}; -use crate::{circuit::{ - layouter::{RegionLayouter, SyncDeps}, - AssignedCell, Cell, Layouter, Region, Table, Value, -}, plonk::FieldFr}; use crate::plonk::{ circuit::expression::{Challenge, Column}, Advice, Assigned, Assignment, Circuit, ConstraintSystem, Error, Fixed, FloorPlanner, Instance, Selector, }; +use crate::{ + circuit::{ + layouter::{RegionLayouter, SyncDeps}, + AssignedCell, Cell, Layouter, Region, Table, Value, + }, + plonk::FieldFront, +}; /// A helper type that augments a [`FloorPlanner`] with [`tracing`] spans and events. /// @@ -35,12 +38,12 @@ use crate::plonk::{ /// plonk::{Circuit, ConstraintSystem, Error}, /// }; /// -/// # struct MyCircuit { +/// # struct MyCircuit { /// # some_witness: Value, /// # }; /// # #[derive(Clone)] /// # struct MyConfig; -/// impl Circuit for MyCircuit { +/// impl Circuit for MyCircuit { /// // Wrap `TracingFloorPlanner` around your existing floor planner of choice. /// //type FloorPlanner = floor_planner::V1; /// type FloorPlanner = TracingFloorPlanner; @@ -85,7 +88,7 @@ pub struct TracingFloorPlanner { } impl FloorPlanner for TracingFloorPlanner

{ - fn synthesize + SyncDeps, C: Circuit>( + fn synthesize + SyncDeps, C: Circuit>( cs: &mut CS, circuit: &C, config: C::Config, @@ -101,12 +104,12 @@ impl FloorPlanner for TracingFloorPlanner

{ } /// A helper type that augments a [`Circuit`] with [`tracing`] spans and events. -enum TracingCircuit<'c, F: FieldFr, C: Circuit> { +enum TracingCircuit<'c, F: FieldFront, C: Circuit> { Borrowed(&'c C, PhantomData), Owned(C, PhantomData), } -impl<'c, F: FieldFr, C: Circuit> TracingCircuit<'c, F, C> { +impl<'c, F: FieldFront, C: Circuit> TracingCircuit<'c, F, C> { fn borrowed(circuit: &'c C) -> Self { Self::Borrowed(circuit, PhantomData) } @@ -123,7 +126,7 @@ impl<'c, F: FieldFr, C: Circuit> TracingCircuit<'c, F, C> { } } -impl<'c, F: FieldFr, C: Circuit> Circuit for TracingCircuit<'c, F, C> { +impl<'c, F: FieldFront, C: Circuit> Circuit for TracingCircuit<'c, F, C> { type Config = C::Config; type FloorPlanner = C::FloorPlanner; #[cfg(feature = "circuit-params")] @@ -146,13 +149,13 @@ impl<'c, F: FieldFr, C: Circuit> Circuit for TracingCircuit<'c, F, C> { } /// A helper type that augments a [`Layouter`] with [`tracing`] spans and events. -struct TracingLayouter> { +struct TracingLayouter> { layouter: L, namespace_spans: Vec, _phantom: PhantomData, } -impl> TracingLayouter { +impl> TracingLayouter { fn new(layouter: L) -> Self { Self { layouter, @@ -162,7 +165,7 @@ impl> TracingLayouter { } } -impl> Layouter for TracingLayouter { +impl> Layouter for TracingLayouter { type Root = Self; fn assign_region(&mut self, name: N, mut assignment: A) -> Result @@ -222,23 +225,23 @@ impl> Layouter for TracingLayouter { } } -fn debug_value(value: &AssignedCell) { +fn debug_value(value: &AssignedCell) { value.value().assert_if_known(|v| { debug!(target: "assigned", value = ?v); true }); } -fn debug_value_and_return_cell(value: AssignedCell) -> Cell { +fn debug_value_and_return_cell(value: AssignedCell) -> Cell { debug_value(&value); value.cell() } /// A helper type that augments a [`Region`] with [`tracing`] spans and events. #[derive(Debug)] -struct TracingRegion<'r, F: FieldFr>(Region<'r, F>); +struct TracingRegion<'r, F: FieldFront>(Region<'r, F>); -impl<'r, F: FieldFr> RegionLayouter for TracingRegion<'r, F> { +impl<'r, F: FieldFront> RegionLayouter for TracingRegion<'r, F> { fn enable_selector<'v>( &'v mut self, annotation: &'v (dyn Fn() -> String + 'v), @@ -354,13 +357,13 @@ impl<'r, F: FieldFr> RegionLayouter for TracingRegion<'r, F> { } /// A helper type that augments an [`Assignment`] with [`tracing`] spans and events. -struct TracingAssignment<'cs, F: FieldFr, CS: Assignment> { +struct TracingAssignment<'cs, F: FieldFront, CS: Assignment> { cs: &'cs mut CS, in_region: bool, _phantom: PhantomData, } -impl<'cs, F: FieldFr, CS: Assignment> TracingAssignment<'cs, F, CS> { +impl<'cs, F: FieldFront, CS: Assignment> TracingAssignment<'cs, F, CS> { fn new(cs: &'cs mut CS) -> Self { Self { cs, @@ -370,7 +373,7 @@ impl<'cs, F: FieldFr, CS: Assignment> TracingAssignment<'cs, F, CS> { } } -impl<'cs, F: FieldFr, CS: Assignment> Assignment for TracingAssignment<'cs, F, CS> { +impl<'cs, F: FieldFront, CS: Assignment> Assignment for TracingAssignment<'cs, F, CS> { fn enter_region(&mut self, name_fn: N) where NR: Into, diff --git a/halo2_frontend/src/dev/util.rs b/halo2_frontend/src/dev/util.rs index 0a1dfe4bac..e1e036c6c1 100644 --- a/halo2_frontend/src/dev/util.rs +++ b/halo2_frontend/src/dev/util.rs @@ -1,12 +1,12 @@ use std::collections::BTreeMap; use super::{metadata, CellValue, InstanceValue, Value}; +use crate::plonk::FieldFront; use crate::plonk::{ AdviceQuery, Column, ColumnType, Expression, FixedQuery, Gate, InstanceQuery, VirtualCell, }; use halo2_middleware::circuit::Any; use halo2_middleware::poly::Rotation; -use crate::plonk::FieldFr; pub(crate) struct AnyQuery { /// Query index @@ -52,7 +52,7 @@ impl From for AnyQuery { } } -pub(super) fn format_value(v: F) -> String { +pub(super) fn format_value(v: F) -> String { if v.is_zero_vartime() { "0".into() } else if v == F::ONE { @@ -69,7 +69,7 @@ pub(super) fn format_value(v: F) -> String { } } -pub(super) fn load<'a, F: FieldFr, T: ColumnType, Q: Into + Copy>( +pub(super) fn load<'a, F: FieldFront, T: ColumnType, Q: Into + Copy>( n: i32, row: i32, queries: &'a [(Column, Rotation)], @@ -82,7 +82,7 @@ pub(super) fn load<'a, F: FieldFr, T: ColumnType, Q: Into + Copy>( } } -pub(super) fn load_instance<'a, F: FieldFr, T: ColumnType, Q: Into + Copy>( +pub(super) fn load_instance<'a, F: FieldFront, T: ColumnType, Q: Into + Copy>( n: i32, row: i32, queries: &'a [(Column, Rotation)], @@ -96,7 +96,7 @@ pub(super) fn load_instance<'a, F: FieldFr, T: ColumnType, Q: Into + C } } -fn cell_value<'a, F: FieldFr, Q: Into + Copy>( +fn cell_value<'a, F: FieldFront, Q: Into + Copy>( virtual_cells: &'a [VirtualCell], load: impl Fn(Q) -> Value + 'a, ) -> impl Fn(Q) -> BTreeMap + 'a { @@ -129,7 +129,7 @@ fn cell_value<'a, F: FieldFr, Q: Into + Copy>( } } -pub(super) fn cell_values<'a, F: FieldFr>( +pub(super) fn cell_values<'a, F: FieldFront>( gate: &Gate, poly: &Expression, load_fixed: impl Fn(FixedQuery) -> Value + 'a, diff --git a/halo2_frontend/src/lib.rs b/halo2_frontend/src/lib.rs index 60f14c042b..fb45137388 100644 --- a/halo2_frontend/src/lib.rs +++ b/halo2_frontend/src/lib.rs @@ -2,4 +2,4 @@ pub mod circuit; pub mod dev; -pub mod plonk; \ No newline at end of file +pub mod plonk; diff --git a/halo2_frontend/src/plonk/assigned.rs b/halo2_frontend/src/plonk/assigned.rs index 755b5ddde4..b072e6bcb7 100644 --- a/halo2_frontend/src/plonk/assigned.rs +++ b/halo2_frontend/src/plonk/assigned.rs @@ -1,5 +1,5 @@ +use crate::plonk::FieldFront; use std::ops::{Add, AddAssign, Mul, MulAssign, Neg, Sub, SubAssign}; -use crate::plonk::FieldFr; /// A value assigned to a cell within a circuit. /// @@ -16,31 +16,31 @@ pub enum Assigned { Rational(F, F), } -impl From<&Assigned> for Assigned { +impl From<&Assigned> for Assigned { fn from(val: &Assigned) -> Self { *val } } -impl From<&F> for Assigned { +impl From<&F> for Assigned { fn from(numerator: &F) -> Self { Assigned::Trivial(*numerator) } } -impl From for Assigned { +impl From for Assigned { fn from(numerator: F) -> Self { Assigned::Trivial(numerator) } } -impl From<(F, F)> for Assigned { +impl From<(F, F)> for Assigned { fn from((numerator, denominator): (F, F)) -> Self { Assigned::Rational(numerator, denominator) } } -impl PartialEq for Assigned { +impl PartialEq for Assigned { fn eq(&self, other: &Self) -> bool { match (self, other) { // At least one side is directly zero. @@ -68,9 +68,9 @@ impl PartialEq for Assigned { } } -impl Eq for Assigned {} +impl Eq for Assigned {} -impl Neg for Assigned { +impl Neg for Assigned { type Output = Assigned; fn neg(self) -> Self::Output { match self { @@ -81,14 +81,14 @@ impl Neg for Assigned { } } -impl Neg for &Assigned { +impl Neg for &Assigned { type Output = Assigned; fn neg(self) -> Self::Output { -*self } } -impl Add for Assigned { +impl Add for Assigned { type Output = Assigned; fn add(self, rhs: Assigned) -> Assigned { match (self, rhs) { @@ -120,108 +120,108 @@ impl Add for Assigned { } } -impl Add for Assigned { +impl Add for Assigned { type Output = Assigned; fn add(self, rhs: F) -> Assigned { self + Self::Trivial(rhs) } } -impl Add for &Assigned { +impl Add for &Assigned { type Output = Assigned; fn add(self, rhs: F) -> Assigned { *self + rhs } } -impl Add<&Assigned> for Assigned { +impl Add<&Assigned> for Assigned { type Output = Assigned; fn add(self, rhs: &Self) -> Assigned { self + *rhs } } -impl Add> for &Assigned { +impl Add> for &Assigned { type Output = Assigned; fn add(self, rhs: Assigned) -> Assigned { *self + rhs } } -impl Add<&Assigned> for &Assigned { +impl Add<&Assigned> for &Assigned { type Output = Assigned; fn add(self, rhs: &Assigned) -> Assigned { *self + *rhs } } -impl AddAssign for Assigned { +impl AddAssign for Assigned { fn add_assign(&mut self, rhs: Self) { *self = *self + rhs; } } -impl AddAssign<&Assigned> for Assigned { +impl AddAssign<&Assigned> for Assigned { fn add_assign(&mut self, rhs: &Self) { *self = *self + rhs; } } -impl Sub for Assigned { +impl Sub for Assigned { type Output = Assigned; fn sub(self, rhs: Assigned) -> Assigned { self + (-rhs) } } -impl Sub for Assigned { +impl Sub for Assigned { type Output = Assigned; fn sub(self, rhs: F) -> Assigned { self + (-rhs) } } -impl Sub for &Assigned { +impl Sub for &Assigned { type Output = Assigned; fn sub(self, rhs: F) -> Assigned { *self - rhs } } -impl Sub<&Assigned> for Assigned { +impl Sub<&Assigned> for Assigned { type Output = Assigned; fn sub(self, rhs: &Self) -> Assigned { self - *rhs } } -impl Sub> for &Assigned { +impl Sub> for &Assigned { type Output = Assigned; fn sub(self, rhs: Assigned) -> Assigned { *self - rhs } } -impl Sub<&Assigned> for &Assigned { +impl Sub<&Assigned> for &Assigned { type Output = Assigned; fn sub(self, rhs: &Assigned) -> Assigned { *self - *rhs } } -impl SubAssign for Assigned { +impl SubAssign for Assigned { fn sub_assign(&mut self, rhs: Self) { *self = *self - rhs; } } -impl SubAssign<&Assigned> for Assigned { +impl SubAssign<&Assigned> for Assigned { fn sub_assign(&mut self, rhs: &Self) { *self = *self - rhs; } } -impl Mul for Assigned { +impl Mul for Assigned { type Output = Assigned; fn mul(self, rhs: Assigned) -> Assigned { match (self, rhs) { @@ -242,40 +242,40 @@ impl Mul for Assigned { } } -impl Mul for Assigned { +impl Mul for Assigned { type Output = Assigned; fn mul(self, rhs: F) -> Assigned { self * Self::Trivial(rhs) } } -impl Mul for &Assigned { +impl Mul for &Assigned { type Output = Assigned; fn mul(self, rhs: F) -> Assigned { *self * rhs } } -impl Mul<&Assigned> for Assigned { +impl Mul<&Assigned> for Assigned { type Output = Assigned; fn mul(self, rhs: &Assigned) -> Assigned { self * *rhs } } -impl MulAssign for Assigned { +impl MulAssign for Assigned { fn mul_assign(&mut self, rhs: Self) { *self = *self * rhs; } } -impl MulAssign<&Assigned> for Assigned { +impl MulAssign<&Assigned> for Assigned { fn mul_assign(&mut self, rhs: &Self) { *self = *self * rhs; } } -impl Assigned { +impl Assigned { /// Returns the numerator. pub fn numerator(&self) -> F { match self { @@ -371,9 +371,9 @@ mod proptests { ops::{Add, Mul, Neg, Sub}, }; + use crate::plonk::FieldFront; use halo2curves::pasta::Fp; use proptest::{collection::vec, prelude::*, sample::select}; - use crate::plonk::FieldFr; use super::Assigned; @@ -384,7 +384,7 @@ mod proptests { fn inv0(&self) -> Self; } - impl UnaryOperand for F { + impl UnaryOperand for F { fn double(&self) -> Self { self.double() } @@ -402,7 +402,7 @@ mod proptests { } } - impl UnaryOperand for Assigned { + impl UnaryOperand for Assigned { fn double(&self) -> Self { self.double() } @@ -450,8 +450,8 @@ mod proptests { } trait BinaryOperand: Sized + Add + Sub + Mul {} - impl BinaryOperand for F {} - impl BinaryOperand for Assigned {} + impl BinaryOperand for F {} + impl BinaryOperand for Assigned {} #[derive(Clone, Debug)] enum BinaryOperator { diff --git a/halo2_frontend/src/plonk/circuit.rs b/halo2_frontend/src/plonk/circuit.rs index d784d5727b..552e415fdd 100644 --- a/halo2_frontend/src/plonk/circuit.rs +++ b/halo2_frontend/src/plonk/circuit.rs @@ -15,7 +15,7 @@ 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; + fn query_cell(&self, index: usize, at: Rotation) -> Expression; } /// An advice column @@ -31,7 +31,7 @@ pub struct Fixed; pub struct Instance; impl ColumnType for Advice { - fn query_cell(&self, index: usize, at: Rotation) -> Expression { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { Expression::Advice(AdviceQuery { index: None, column_index: index, @@ -40,7 +40,7 @@ impl ColumnType for Advice { } } impl ColumnType for Fixed { - fn query_cell(&self, index: usize, at: Rotation) -> Expression { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { Expression::Fixed(FixedQuery { index: None, column_index: index, @@ -49,7 +49,7 @@ impl ColumnType for Fixed { } } impl ColumnType for Instance { - fn query_cell(&self, index: usize, at: Rotation) -> Expression { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { Expression::Instance(InstanceQuery { index: None, column_index: index, @@ -58,7 +58,7 @@ impl ColumnType for Instance { } } impl ColumnType for Any { - fn query_cell(&self, index: usize, at: Rotation) -> Expression { + fn query_cell(&self, index: usize, at: Rotation) -> Expression { match self { Any::Advice => Expression::Advice(AdviceQuery { index: None, @@ -99,7 +99,7 @@ impl From for Any { /// This trait allows a [`Circuit`] to direct some backend to assign a witness /// for a constraint system. -pub trait Assignment { +pub trait Assignment { /// Creates a new region and enters into it. /// /// Panics if we are currently in a region (if `exit_region` was not called). @@ -228,7 +228,7 @@ pub trait FloorPlanner { /// - Perform any necessary setup or measurement tasks, which may involve one or more /// calls to `Circuit::default().synthesize(config, &mut layouter)`. /// - Call `circuit.synthesize(config, &mut layouter)` exactly once. - fn synthesize + SyncDeps, C: Circuit>( + fn synthesize + SyncDeps, C: Circuit>( cs: &mut CS, circuit: &C, config: C::Config, @@ -239,7 +239,7 @@ pub trait FloorPlanner { /// This is a trait that circuits provide implementations for so that the /// backend prover can ask the circuit to synthesize using some given /// [`ConstraintSystem`] implementation. -pub trait Circuit { +pub trait Circuit { /// This is a configuration object that stores things like columns. type Config: Clone; /// The floor planner used for this circuit. This is an associated type of the diff --git a/halo2_frontend/src/plonk/circuit/compress_selectors.rs b/halo2_frontend/src/plonk/circuit/compress_selectors.rs index 507cd77bff..70c2520673 100644 --- a/halo2_frontend/src/plonk/circuit/compress_selectors.rs +++ b/halo2_frontend/src/plonk/circuit/compress_selectors.rs @@ -1,4 +1,4 @@ -use super::{Expression, FieldFr}; +use super::{Expression, FieldFront}; /// This describes a selector and where it is activated. #[derive(Debug, Clone)] @@ -19,7 +19,7 @@ pub(crate) struct SelectorDescription { /// This describes the assigned combination of a particular selector as well as /// the expression it should be substituted with. #[derive(Debug, Clone)] -pub(crate) struct SelectorAssignment { +pub(crate) struct SelectorAssignment { /// The selector that this structure references, by index. pub selector: usize, @@ -47,7 +47,7 @@ pub(crate) struct SelectorAssignment { /// substitutions to the constraint system. /// /// This function is completely deterministic. -pub(crate) fn process( +pub(crate) fn process( mut selectors: Vec, max_degree: usize, mut allocate_fixed_column: E, @@ -229,11 +229,11 @@ where mod tests { use super::*; use crate::plonk::FixedQuery; + use halo2_middleware::ff::Field; use halo2_middleware::poly::Rotation; use halo2curves::pasta::Fp; use proptest::collection::{vec, SizeRange}; use proptest::prelude::*; - use halo2_middleware::ff::Field; prop_compose! { fn arb_selector(assignment_size: usize, max_degree: usize) diff --git a/halo2_frontend/src/plonk/circuit/constraint_system.rs b/halo2_frontend/src/plonk/circuit/constraint_system.rs index b75a8903d2..f7edbb6b3f 100644 --- a/halo2_frontend/src/plonk/circuit/constraint_system.rs +++ b/halo2_frontend/src/plonk/circuit/constraint_system.rs @@ -1,5 +1,6 @@ use super::compress_selectors; use super::expression::sealed; +use crate::plonk::FieldFront; use crate::plonk::{ lookup, permutation, shuffle, Advice, AdviceQuery, Challenge, Column, Expression, FirstPhase, Fixed, FixedQuery, Instance, InstanceQuery, Phase, Selector, TableColumn, @@ -11,7 +12,6 @@ use halo2_middleware::poly::Rotation; use std::collections::HashMap; use std::convert::TryFrom; use std::fmt::Debug; -use crate::plonk::FieldFr; /// Represents an index into a vector where each entry corresponds to a distinct /// point that polynomials are queried at. @@ -40,12 +40,12 @@ impl>> From<(Col, Rotation)> for VirtualCell { /// /// These are returned by the closures passed to `ConstraintSystem::create_gate`. #[derive(Debug)] -pub struct Constraint { +pub struct Constraint { name: String, poly: Expression, } -impl From> for Constraint { +impl From> for Constraint { fn from(poly: Expression) -> Self { Constraint { name: "".to_string(), @@ -54,7 +54,7 @@ impl From> for Constraint { } } -impl> From<(S, Expression)> for Constraint { +impl> From<(S, Expression)> for Constraint { fn from((name, poly): (S, Expression)) -> Self { Constraint { name: name.as_ref().to_string(), @@ -63,7 +63,7 @@ impl> From<(S, Expression)> for Constraint { } } -impl From> for Vec> { +impl From> for Vec> { fn from(poly: Expression) -> Self { vec![Constraint { name: "".to_string(), @@ -108,12 +108,12 @@ impl From> for Vec> { /// support Rust 1.51 or 1.52. If your minimum supported Rust version is 1.53 or greater, /// you can pass an array directly. #[derive(Debug)] -pub struct Constraints>, Iter: IntoIterator> { +pub struct Constraints>, Iter: IntoIterator> { selector: Expression, constraints: Iter, } -impl>, Iter: IntoIterator> Constraints { +impl>, Iter: IntoIterator> Constraints { /// Constructs a set of constraints that are controlled by the given selector. /// /// Each constraint `c` in `iterator` will be converted into the constraint @@ -126,7 +126,7 @@ impl>, Iter: IntoIterator> Constrain } } -fn apply_selector_to_constraint>>( +fn apply_selector_to_constraint>>( (selector, c): (Expression, C), ) -> Constraint { let constraint: Constraint = c.into(); @@ -142,7 +142,7 @@ type ConstraintsIterator = std::iter::Map< ApplySelectorToConstraint, >; -impl>, Iter: IntoIterator> IntoIterator +impl>, Iter: IntoIterator> IntoIterator for Constraints { type Item = Constraint; @@ -157,7 +157,7 @@ impl>, Iter: IntoIterator> IntoItera /// Gate #[derive(Clone, Debug)] -pub struct Gate { +pub struct Gate { pub(crate) name: String, pub(crate) constraint_names: Vec, pub(crate) polys: Vec>, @@ -167,7 +167,7 @@ pub struct Gate { pub(crate) queried_cells: Vec, } -impl Gate { +impl Gate { /// Returns the gate name. pub fn name(&self) -> &str { self.name.as_str() @@ -192,7 +192,7 @@ impl Gate { } } -impl, IF: Field> From> for ConstraintSystemMid { +impl, IF: Field> From> for ConstraintSystemMid { fn from(cs: ConstraintSystem) -> Self { ConstraintSystemMid { num_fixed_columns: cs.num_fixed_columns, @@ -259,7 +259,7 @@ impl, IF: Field> From> for ConstraintSy /// This is a description of the circuit environment, such as the gate, column and /// permutation arrangements. #[derive(Debug, Clone)] -pub struct ConstraintSystem { +pub struct ConstraintSystem { pub(crate) num_fixed_columns: usize, pub(crate) num_advice_columns: usize, pub(crate) num_instance_columns: usize, @@ -309,7 +309,7 @@ pub struct ConstraintSystem { pub(crate) minimum_degree: Option, } -impl Default for ConstraintSystem { +impl Default for ConstraintSystem { fn default() -> ConstraintSystem { ConstraintSystem { num_fixed_columns: 0, @@ -336,7 +336,7 @@ impl Default for ConstraintSystem { } } -impl ConstraintSystem { +impl ConstraintSystem { /// Enables this fixed column to be used for global constant assignments. /// /// # Side-effects @@ -694,7 +694,7 @@ impl ConstraintSystem { } fn replace_selectors_with_fixed(&mut self, selector_replacements: &[Expression]) { - fn replace_selectors( + fn replace_selectors( expr: &mut Expression, selector_replacements: &[Expression], must_be_nonsimple: bool, @@ -1110,13 +1110,13 @@ impl ConstraintSystem { /// Exposes the "virtual cells" that can be queried while creating a custom gate or lookup /// table. #[derive(Debug)] -pub struct VirtualCells<'a, F: FieldFr> { +pub struct VirtualCells<'a, F: FieldFront> { pub(super) meta: &'a mut ConstraintSystem, pub(super) queried_selectors: Vec, pub(super) queried_cells: Vec, } -impl<'a, F: FieldFr> VirtualCells<'a, F> { +impl<'a, F: FieldFront> VirtualCells<'a, F> { fn new(meta: &'a mut ConstraintSystem) -> Self { VirtualCells { meta, diff --git a/halo2_frontend/src/plonk/circuit/expression.rs b/halo2_frontend/src/plonk/circuit/expression.rs index 9ce4298e4a..4279b7a18f 100644 --- a/halo2_frontend/src/plonk/circuit/expression.rs +++ b/halo2_frontend/src/plonk/circuit/expression.rs @@ -46,27 +46,27 @@ impl Column { } /// Return expression from column at a relative position - pub fn query_cell(&self, at: Rotation) -> Expression { + pub fn query_cell(&self, at: Rotation) -> Expression { self.column_type.query_cell(self.index, at) } /// Return expression from column at the current row - pub fn cur(&self) -> Expression { + pub fn cur(&self) -> Expression { self.query_cell(Rotation::cur()) } /// Return expression from column at the next row - pub fn next(&self) -> Expression { + pub fn next(&self) -> Expression { self.query_cell(Rotation::next()) } /// Return expression from column at the previous row - pub fn prev(&self) -> Expression { + pub fn prev(&self) -> Expression { self.query_cell(Rotation::prev()) } /// Return expression from column at the specified rotation - pub fn rot(&self, rotation: i32) -> Expression { + pub fn rot(&self, rotation: i32) -> Expression { self.query_cell(Rotation(rotation)) } } @@ -262,7 +262,7 @@ impl SealedPhase for ThirdPhase { /// s: Selector, /// } /// -/// fn circuit_logic>(chip: C, mut layouter: impl Layouter) -> Result<(), Error> { +/// fn circuit_logic>(chip: C, mut layouter: impl Layouter) -> Result<(), Error> { /// let config = chip.config(); /// # let config: Config = todo!(); /// layouter.assign_region(|| "bar", |mut region| { @@ -278,7 +278,11 @@ pub struct Selector(pub usize, pub(crate) bool); impl Selector { /// Enable this selector at the given offset within the given region. - pub fn enable(&self, region: &mut Region, offset: usize) -> Result<(), Error> { + pub fn enable( + &self, + region: &mut Region, + offset: usize, + ) -> Result<(), Error> { region.enable_selector(|| "", self, offset) } @@ -294,7 +298,7 @@ impl Selector { } /// Return expression from selector - pub fn expr(&self) -> Expression { + pub fn expr(&self) -> Expression { Expression::Selector(*self) } } @@ -416,7 +420,7 @@ impl Challenge { } /// Return Expression - pub fn expr(&self) -> Expression { + pub fn expr(&self) -> Expression { Expression::Challenge(*self) } } @@ -441,7 +445,7 @@ impl From for Challenge { /// Low-degree expression representing an identity that must hold over the committed columns. #[derive(Copy, Clone, PartialEq, Eq)] -pub enum Expression { +pub enum Expression { /// This is a constant polynomial Constant(F), /// This is a virtual selector @@ -467,7 +471,7 @@ pub enum Expression { } // Arena context -pub trait FieldFr: Field { +pub trait FieldFront: Field { type Field: Field; fn alloc(expr: Expression) -> ExprRef; fn get(ref_: &ExprRef) -> Expression; @@ -477,19 +481,19 @@ pub trait FieldFr: Field { #[derive(Clone, Copy, Eq, PartialEq)] pub struct ExprRef(usize, std::marker::PhantomData); -impl Into> for Expression { +impl Into> for Expression { fn into(self) -> ExprRef { F::alloc(self) } } #[allow(unused)] -struct ArenaPool { +struct ArenaPool { expressions: Vec>, } #[allow(unused)] -impl ArenaPool { +impl ArenaPool { fn new() -> Self { Self { expressions: Vec::new(), @@ -505,7 +509,7 @@ impl ArenaPool { } } -impl, IF: Field> From> for ExpressionMid { +impl, IF: Field> From> for ExpressionMid { fn from(val: Expression) -> Self { match val { Expression::Constant(c) => ExpressionMid::Constant(c.into_field()), @@ -554,7 +558,7 @@ impl, IF: Field> From> for ExpressionMid Expression { +impl Expression { /// Make side effects pub fn query_cells(&mut self, cells: &mut VirtualCells<'_, F>) { match self { @@ -1021,7 +1025,7 @@ impl Expression { } } -impl std::fmt::Debug for Expression { +impl std::fmt::Debug for Expression { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self { Expression::Constant(scalar) => f.debug_tuple("Constant").field(scalar).finish(), @@ -1084,7 +1088,7 @@ impl std::fmt::Debug for Expression { } } -impl Neg for Expression { +impl Neg for Expression { type Output = Expression; fn neg(self) -> Self::Output { let ref_ = match self { @@ -1096,7 +1100,7 @@ impl Neg for Expression { } } -impl Add for Expression { +impl Add for Expression { type Output = Expression; fn add(self, rhs: Expression) -> Expression { if self.contains_simple_selector() || rhs.contains_simple_selector() { @@ -1115,7 +1119,7 @@ impl Add for Expression { } } -impl Sub for Expression { +impl Sub for Expression { type Output = Expression; fn sub(self, rhs: Expression) -> Expression { if self.contains_simple_selector() || rhs.contains_simple_selector() { @@ -1135,7 +1139,7 @@ impl Sub for Expression { } } -impl Mul for Expression { +impl Mul for Expression { type Output = Expression; fn mul(self, rhs: Expression) -> Expression { if self.contains_simple_selector() && rhs.contains_simple_selector() { @@ -1153,7 +1157,7 @@ impl Mul for Expression { } } -impl Mul for Expression { +impl Mul for Expression { type Output = Expression; fn mul(self, rhs: F) -> Expression { let self_ = match self { @@ -1164,14 +1168,14 @@ impl Mul for Expression { } } -impl Sum for Expression { +impl Sum for Expression { fn sum>(iter: I) -> Self { iter.reduce(|acc, x| acc + x) .unwrap_or(Expression::Constant(F::ZERO)) } } -impl Product for Expression { +impl Product for Expression { fn product>(iter: I) -> Self { iter.reduce(|acc, x| acc * x) .unwrap_or(Expression::Constant(F::ONE)) @@ -1179,9 +1183,9 @@ impl Product for Expression { } #[allow(non_camel_case_types)] -pub struct ExprArena(Vec>); +pub struct ExprArena(Vec>); -impl ExprArena { +impl ExprArena { pub fn new() -> Self { Self(Vec::new()) } @@ -1199,11 +1203,12 @@ impl ExprArena { macro_rules! expression_arena { ($arena:ident, $field:ty) => { fn $arena() -> &'static std::sync::RwLock> { - static LINES: std::sync::OnceLock>> = std::sync::OnceLock::new(); + static LINES: std::sync::OnceLock>> = + std::sync::OnceLock::new(); LINES.get_or_init(|| std::sync::RwLock::new(ExprArena::new())) } - impl crate::plonk::FieldFr for $field { + impl crate::plonk::FieldFront for $field { type Field = $field; fn into_field(self) -> Self::Field { self @@ -1221,7 +1226,7 @@ macro_rules! expression_arena { impl Into> for $field { fn into(self) -> crate::plonk::ExprRef<$field> { - crate::plonk::FieldFr::alloc(crate::plonk::Expression::Constant(self)) + crate::plonk::FieldFront::alloc(crate::plonk::Expression::Constant(self)) } } }; @@ -1234,7 +1239,6 @@ expression_arena!(arena_pasta_fp, halo2curves::pasta::Fp); #[cfg(test)] mod tests { - use super::Expression; use super::*; use halo2curves::bn256::Fr; diff --git a/halo2_frontend/src/plonk/keygen.rs b/halo2_frontend/src/plonk/keygen.rs index 88650e8255..5bdd117078 100644 --- a/halo2_frontend/src/plonk/keygen.rs +++ b/halo2_frontend/src/plonk/keygen.rs @@ -1,7 +1,7 @@ use std::ops::Range; +use crate::plonk::FieldFront; use halo2_middleware::circuit::Any; -use crate::plonk::FieldFr; use crate::circuit::Value; use crate::plonk::{ @@ -10,7 +10,7 @@ use crate::plonk::{ /// Assembly to be used in circuit synthesis. #[derive(Debug)] -pub(crate) struct Assembly { +pub(crate) struct Assembly { pub(crate) k: u32, pub(crate) fixed: Vec>>, pub(crate) permutation: permutation::Assembly, @@ -20,7 +20,7 @@ pub(crate) struct Assembly { pub(crate) _marker: std::marker::PhantomData, } -impl Assignment for Assembly { +impl Assignment for Assembly { fn enter_region(&mut self, _: N) where NR: Into, diff --git a/halo2_frontend/src/plonk/lookup.rs b/halo2_frontend/src/plonk/lookup.rs index 5e9f9e2b88..d45225ff3c 100644 --- a/halo2_frontend/src/plonk/lookup.rs +++ b/halo2_frontend/src/plonk/lookup.rs @@ -1,16 +1,16 @@ use crate::plonk::Expression; +use crate::plonk::FieldFront; use std::fmt::{self, Debug}; -use crate::plonk::FieldFr; /// Expressions involved in a lookup argument, with a name as metadata. #[derive(Clone)] -pub struct Argument { +pub struct Argument { pub(crate) name: String, pub(crate) input_expressions: Vec>, pub(crate) table_expressions: Vec>, } -impl Debug for Argument { +impl Debug for Argument { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Argument") .field("input_expressions", &self.input_expressions) @@ -19,7 +19,7 @@ impl Debug for Argument { } } -impl Argument { +impl Argument { /// Constructs a new lookup argument. /// /// `table_map` is a sequence of `(input, table)` tuples. diff --git a/halo2_frontend/src/plonk/shuffle.rs b/halo2_frontend/src/plonk/shuffle.rs index 6f90f04ebf..2d485182c5 100644 --- a/halo2_frontend/src/plonk/shuffle.rs +++ b/halo2_frontend/src/plonk/shuffle.rs @@ -1,16 +1,16 @@ use crate::plonk::Expression; +use crate::plonk::FieldFront; use std::fmt::{self, Debug}; -use crate::plonk::FieldFr; /// Expressions involved in a shuffle argument, with a name as metadata. #[derive(Clone)] -pub struct Argument { +pub struct Argument { pub(crate) name: String, pub(crate) input_expressions: Vec>, pub(crate) shuffle_expressions: Vec>, } -impl Debug for Argument { +impl Debug for Argument { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.debug_struct("Argument") .field("input_expressions", &self.input_expressions) @@ -19,7 +19,7 @@ impl Debug for Argument { } } -impl Argument { +impl Argument { /// Constructs a new shuffle argument. /// /// `shuffle` is a sequence of `(input, shuffle)` tuples. diff --git a/halo2_proofs/CHANGELOG.md b/halo2_proofs/CHANGELOG.md index e087727a3a..b5aaf86a1e 100644 --- a/halo2_proofs/CHANGELOG.md +++ b/halo2_proofs/CHANGELOG.md @@ -11,7 +11,7 @@ and this project adheres to Rust's notion of ### Added - `halo2_proofs::circuit::Value`, a more usable and type-safe replacement for `Option` in circuit synthesis. -- `impl Mul for &Assigned` +- `impl Mul for &Assigned` ### Changed All APIs that represented witnessed values as `Option` now represent them as diff --git a/halo2_proofs/benches/dev_lookup.rs b/halo2_proofs/benches/dev_lookup.rs index e3ca036a40..ad03956d95 100644 --- a/halo2_proofs/benches/dev_lookup.rs +++ b/halo2_proofs/benches/dev_lookup.rs @@ -2,7 +2,7 @@ extern crate criterion; use ff::{Field, PrimeField}; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::circuit::{Layouter, SimpleFloorPlanner, Value}; use halo2_proofs::dev::MockProver; use halo2_proofs::plonk::*; @@ -15,7 +15,7 @@ use criterion::{BenchmarkId, Criterion}; fn criterion_benchmark(c: &mut Criterion) { #[derive(Clone, Default)] - struct MyCircuit { + struct MyCircuit { _marker: PhantomData, } @@ -26,7 +26,7 @@ fn criterion_benchmark(c: &mut Criterion) { advice: Column, } - impl> Circuit for MyCircuit { + impl> Circuit for MyCircuit { type Config = MyConfig; type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] diff --git a/halo2_proofs/benches/plonk.rs b/halo2_proofs/benches/plonk.rs index 9802969a7b..100c76b5f9 100644 --- a/halo2_proofs/benches/plonk.rs +++ b/halo2_proofs/benches/plonk.rs @@ -2,7 +2,7 @@ extern crate criterion; use group::ff::Field; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::circuit::{Cell, Layouter, SimpleFloorPlanner, Value}; use halo2_proofs::plonk::*; use halo2_proofs::poly::{commitment::ParamsProver, Rotation}; @@ -44,7 +44,7 @@ fn criterion_benchmark(c: &mut Criterion) { sm: Column, } - trait StandardCs { + trait StandardCs { fn raw_multiply( &self, layouter: &mut impl Layouter, @@ -68,17 +68,17 @@ fn criterion_benchmark(c: &mut Criterion) { } #[derive(Clone)] - struct MyCircuit { + struct MyCircuit { a: Value, k: u32, } - struct StandardPlonk { + struct StandardPlonk { config: PlonkConfig, _marker: PhantomData, } - impl StandardPlonk { + impl StandardPlonk { fn new(config: PlonkConfig) -> Self { StandardPlonk { config, @@ -87,7 +87,7 @@ fn criterion_benchmark(c: &mut Criterion) { } } - impl StandardCs for StandardPlonk { + impl StandardCs for StandardPlonk { fn raw_multiply( &self, layouter: &mut impl Layouter, @@ -187,7 +187,7 @@ fn criterion_benchmark(c: &mut Criterion) { } } - impl Circuit for MyCircuit { + impl Circuit for MyCircuit { type Config = PlonkConfig; type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] @@ -288,7 +288,15 @@ fn criterion_benchmark(c: &mut Criterion) { }; let mut transcript = Blake2bWrite::<_, _, Challenge255>::init(vec![]); - create_proof::, ProverIPA, _, _, _, _, halo2curves::pasta::Fp>( + create_proof::< + IPACommitmentScheme, + ProverIPA, + _, + _, + _, + _, + halo2curves::pasta::Fp, + >( params, pk, &[circuit], diff --git a/halo2_proofs/examples/circuit-layout.rs b/halo2_proofs/examples/circuit-layout.rs index 6547498df7..c0e206fee9 100644 --- a/halo2_proofs/examples/circuit-layout.rs +++ b/halo2_proofs/examples/circuit-layout.rs @@ -47,12 +47,12 @@ trait StandardCs { ) -> Result<(), ErrorFront>; } -struct MyCircuit { +struct MyCircuit { a: Value, lookup_table: Vec, } -struct StandardPlonk { +struct StandardPlonk { config: PlonkConfig, _marker: PhantomData, } @@ -175,7 +175,7 @@ impl StandardCs for StandardPlonk { } } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { type Config = PlonkConfig; type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] diff --git a/halo2_proofs/examples/simple-example.rs b/halo2_proofs/examples/simple-example.rs index f526e2232f..2578c36d29 100644 --- a/halo2_proofs/examples/simple-example.rs +++ b/halo2_proofs/examples/simple-example.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::{ arithmetic::Field, circuit::{AssignedCell, Chip, Layouter, Region, SimpleFloorPlanner, Value}, @@ -9,7 +9,7 @@ use halo2_proofs::{ }; // ANCHOR: instructions -trait NumericInstructions: Chip { +trait NumericInstructions: Chip { /// Variable representing a number. type Num; @@ -48,7 +48,7 @@ trait NumericInstructions: Chip { // ANCHOR: chip /// The chip that will implement our instructions! Chips store their own /// config, as well as type markers if necessary. -struct FieldChip { +struct FieldChip { config: FieldConfig, _marker: PhantomData, } @@ -74,7 +74,7 @@ struct FieldConfig { s_mul: Selector, } -impl FieldChip { +impl FieldChip { fn construct(config: >::Config) -> Self { Self { config, @@ -135,7 +135,7 @@ impl FieldChip { // ANCHOR_END: chip-config // ANCHOR: chip-impl -impl Chip for FieldChip { +impl Chip for FieldChip { type Config = FieldConfig; type Loaded = (); @@ -152,9 +152,9 @@ impl Chip for FieldChip { // ANCHOR: instructions-impl /// A variable representing a number. #[derive(Clone)] -struct Number(AssignedCell); +struct Number(AssignedCell); -impl NumericInstructions for FieldChip { +impl NumericInstructions for FieldChip { type Num = Number; fn load_private( @@ -247,13 +247,13 @@ impl NumericInstructions for FieldChip { /// they won't have any value during key generation. During proving, if any of these /// were `None` we would get an error. #[derive(Default)] -struct MyCircuit { +struct MyCircuit { constant: F, a: Value, b: Value, } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { // Since we are using a single chip for everything, we can just reuse its config. type Config = FieldConfig; type FloorPlanner = SimpleFloorPlanner; diff --git a/halo2_proofs/examples/two-chip.rs b/halo2_proofs/examples/two-chip.rs index a6badb16ee..98f583886c 100644 --- a/halo2_proofs/examples/two-chip.rs +++ b/halo2_proofs/examples/two-chip.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::{ arithmetic::Field, circuit::{AssignedCell, Chip, Layouter, Region, SimpleFloorPlanner, Value}, @@ -11,9 +11,9 @@ use halo2_proofs::{ // ANCHOR: field-instructions /// A variable representing a number. #[derive(Clone)] -struct Number(AssignedCell); +struct Number(AssignedCell); -trait FieldInstructions: AddInstructions + MulInstructions { +trait FieldInstructions: AddInstructions + MulInstructions { /// Variable representing a number. type Num; @@ -44,7 +44,7 @@ trait FieldInstructions: AddInstructions + MulInstructions { // ANCHOR_END: field-instructions // ANCHOR: add-instructions -trait AddInstructions: Chip { +trait AddInstructions: Chip { /// Variable representing a number. type Num; @@ -59,7 +59,7 @@ trait AddInstructions: Chip { // ANCHOR_END: add-instructions // ANCHOR: mul-instructions -trait MulInstructions: Chip { +trait MulInstructions: Chip { /// Variable representing a number. type Num; @@ -109,28 +109,28 @@ struct MulConfig { // ANCHOR: field-chip /// The top-level chip that will implement the `FieldInstructions`. -struct FieldChip { +struct FieldChip { config: FieldConfig, _marker: PhantomData, } // ANCHOR_END: field-chip // ANCHOR: add-chip -struct AddChip { +struct AddChip { config: AddConfig, _marker: PhantomData, } // ANCHOR END: add-chip // ANCHOR: mul-chip -struct MulChip { +struct MulChip { config: MulConfig, _marker: PhantomData, } // ANCHOR_END: mul-chip // ANCHOR: add-chip-trait-impl -impl Chip for AddChip { +impl Chip for AddChip { type Config = AddConfig; type Loaded = (); @@ -145,7 +145,7 @@ impl Chip for AddChip { // ANCHOR END: add-chip-trait-impl // ANCHOR: add-chip-impl -impl AddChip { +impl AddChip { fn construct(config: >::Config, _loaded: >::Loaded) -> Self { Self { config, @@ -175,7 +175,7 @@ impl AddChip { // ANCHOR END: add-chip-impl // ANCHOR: add-instructions-impl -impl AddInstructions for FieldChip { +impl AddInstructions for FieldChip { type Num = Number; fn add( &self, @@ -190,7 +190,7 @@ impl AddInstructions for FieldChip { } } -impl AddInstructions for AddChip { +impl AddInstructions for AddChip { type Num = Number; fn add( @@ -232,7 +232,7 @@ impl AddInstructions for AddChip { // ANCHOR END: add-instructions-impl // ANCHOR: mul-chip-trait-impl -impl Chip for MulChip { +impl Chip for MulChip { type Config = MulConfig; type Loaded = (); @@ -247,7 +247,7 @@ impl Chip for MulChip { // ANCHOR END: mul-chip-trait-impl // ANCHOR: mul-chip-impl -impl MulChip { +impl MulChip { fn construct(config: >::Config, _loaded: >::Loaded) -> Self { Self { config, @@ -297,7 +297,7 @@ impl MulChip { // ANCHOR_END: mul-chip-impl // ANCHOR: mul-instructions-impl -impl MulInstructions for FieldChip { +impl MulInstructions for FieldChip { type Num = Number; fn mul( &self, @@ -311,7 +311,7 @@ impl MulInstructions for FieldChip { } } -impl MulInstructions for MulChip { +impl MulInstructions for MulChip { type Num = Number; fn mul( @@ -353,7 +353,7 @@ impl MulInstructions for MulChip { // ANCHOR END: mul-instructions-impl // ANCHOR: field-chip-trait-impl -impl Chip for FieldChip { +impl Chip for FieldChip { type Config = FieldConfig; type Loaded = (); @@ -368,7 +368,7 @@ impl Chip for FieldChip { // ANCHOR_END: field-chip-trait-impl // ANCHOR: field-chip-impl -impl FieldChip { +impl FieldChip { fn construct(config: >::Config, _loaded: >::Loaded) -> Self { Self { config, @@ -397,7 +397,7 @@ impl FieldChip { // ANCHOR_END: field-chip-impl // ANCHOR: field-instructions-impl -impl FieldInstructions for FieldChip { +impl FieldInstructions for FieldChip { type Num = Number; fn load_private( @@ -449,13 +449,13 @@ impl FieldInstructions for FieldChip { /// they won't have any value during key generation. During proving, if any of these /// were `Value::unknown()` we would get an error. #[derive(Default)] -struct MyCircuit { +struct MyCircuit { a: Value, b: Value, c: Value, } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { // Since we are using a single chip for everything, we can just reuse its config. type Config = FieldConfig; type FloorPlanner = SimpleFloorPlanner; diff --git a/halo2_proofs/examples/vector-mul.rs b/halo2_proofs/examples/vector-mul.rs index ea80bd0d98..28ec830a60 100644 --- a/halo2_proofs/examples/vector-mul.rs +++ b/halo2_proofs/examples/vector-mul.rs @@ -1,6 +1,6 @@ use std::marker::PhantomData; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::{ arithmetic::Field, circuit::{AssignedCell, Chip, Layouter, Region, SimpleFloorPlanner, Value}, @@ -9,7 +9,7 @@ use halo2_proofs::{ }; // ANCHOR: instructions -trait NumericInstructions: Chip { +trait NumericInstructions: Chip { /// Variable representing a number. type Num; @@ -41,7 +41,7 @@ trait NumericInstructions: Chip { // ANCHOR: chip /// The chip that will implement our instructions! Chips store their own /// config, as well as type markers if necessary. -struct FieldChip { +struct FieldChip { config: FieldConfig, _marker: PhantomData, } @@ -67,7 +67,7 @@ struct FieldConfig { s_mul: Selector, } -impl FieldChip { +impl FieldChip { fn construct(config: >::Config) -> Self { Self { config, @@ -125,7 +125,7 @@ impl FieldChip { // ANCHOR_END: chip-config // ANCHOR: chip-impl -impl Chip for FieldChip { +impl Chip for FieldChip { type Config = FieldConfig; type Loaded = (); @@ -142,9 +142,9 @@ impl Chip for FieldChip { // ANCHOR: instructions-impl /// A variable representing a number. #[derive(Clone, Debug)] -struct Number(AssignedCell); +struct Number(AssignedCell); -impl NumericInstructions for FieldChip { +impl NumericInstructions for FieldChip { type Num = Number; fn load_private( @@ -224,12 +224,12 @@ impl NumericInstructions for FieldChip { /// they won't have any value during key generation. During proving, if any of these /// were `None` we would get an error. #[derive(Default)] -struct MyCircuit { +struct MyCircuit { a: Vec>, b: Vec>, } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { // Since we are using a single chip for everything, we can just reuse its config. type Config = FieldConfig; type FloorPlanner = SimpleFloorPlanner; diff --git a/halo2_proofs/src/plonk.rs b/halo2_proofs/src/plonk.rs index 0c13e1085f..b3d3232f95 100644 --- a/halo2_proofs/src/plonk.rs +++ b/halo2_proofs/src/plonk.rs @@ -12,7 +12,7 @@ mod verifier { pub use halo2_backend::plonk::verifier::verify_proof; } -use halo2_frontend::{circuit::compile_circuit, plonk::FieldFr}; +use halo2_frontend::{circuit::compile_circuit, plonk::FieldFront}; pub use keygen::{keygen_pk, keygen_pk_custom, keygen_vk, keygen_vk_custom}; pub use prover::{create_proof, create_proof_with_engine}; @@ -50,7 +50,7 @@ pub fn vk_read> ) -> io::Result> where C::Scalar: SerdePrimeField + FromUniformBytes<64>, - F: FieldFr, + F: FieldFront, { let (_, _, cs) = compile_circuit(k, circuit, compress_selectors) .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; @@ -79,9 +79,8 @@ pub fn pk_read> ) -> io::Result> where C::Scalar: SerdePrimeField + FromUniformBytes<64>, - F: FieldFr, - - { + F: FieldFront, +{ let (_, _, cs) = compile_circuit(k, circuit, compress_selectors) .map_err(|e| io::Error::new(io::ErrorKind::Other, e.to_string()))?; let cs_mid: ConstraintSystemMid<_> = cs.into(); diff --git a/halo2_proofs/src/plonk/keygen.rs b/halo2_proofs/src/plonk/keygen.rs index 079c8fd5a2..91a348d6ce 100644 --- a/halo2_proofs/src/plonk/keygen.rs +++ b/halo2_proofs/src/plonk/keygen.rs @@ -4,8 +4,8 @@ use halo2_backend::plonk::{ ProvingKey, VerifyingKey, }; use halo2_backend::{arithmetic::CurveAffine, poly::commitment::Params}; -use halo2_frontend::{circuit::compile_circuit, plonk::FieldFr}; use halo2_frontend::plonk::Circuit; +use halo2_frontend::{circuit::compile_circuit, plonk::FieldFront}; use halo2_middleware::ff::FromUniformBytes; /// Generate a `VerifyingKey` from an instance of `Circuit`. @@ -21,7 +21,7 @@ pub fn keygen_vk( where C: CurveAffine, P: Params, - F: FieldFr, + F: FieldFront, ConcreteCircuit: Circuit, C::Scalar: FromUniformBytes<64>, { @@ -44,7 +44,7 @@ pub fn keygen_vk_custom( where C: CurveAffine, P: Params, - F: FieldFr, + F: FieldFront, ConcreteCircuit: Circuit, C::Scalar: FromUniformBytes<64>, { @@ -66,7 +66,7 @@ pub fn keygen_pk( where C: CurveAffine, P: Params, - F: FieldFr, + F: FieldFront, ConcreteCircuit: Circuit, { keygen_pk_custom(params, vk, circuit, true) @@ -89,7 +89,7 @@ pub fn keygen_pk_custom( where C: CurveAffine, P: Params, - F: FieldFr, + F: FieldFront, ConcreteCircuit: Circuit, { let (compiled_circuit, _, _) = compile_circuit(params.k(), circuit, compress_selectors)?; diff --git a/halo2_proofs/src/plonk/prover.rs b/halo2_proofs/src/plonk/prover.rs index 7641ba9efe..e4fefb1a73 100644 --- a/halo2_proofs/src/plonk/prover.rs +++ b/halo2_proofs/src/plonk/prover.rs @@ -3,7 +3,7 @@ use crate::poly::commitment::{self, CommitmentScheme, Params}; use crate::transcript::{EncodedChallenge, TranscriptWrite}; use halo2_backend::plonk::{prover::Prover, ProvingKey}; use halo2_frontend::circuit::WitnessCalculator; -use halo2_frontend::plonk::{Circuit, ConstraintSystem, FieldFr}; +use halo2_frontend::plonk::{Circuit, ConstraintSystem, FieldFront}; use halo2_middleware::ff::{FromUniformBytes, WithSmallOrderMulGroup}; use halo2_middleware::zal::{ impls::{PlonkEngine, PlonkEngineConfig}, @@ -25,7 +25,7 @@ pub fn create_proof_with_engine< T: TranscriptWrite, ConcreteCircuit: Circuit, M: MsmAccel, - F: FieldFr, + F: FieldFront, >( engine: PlonkEngine, params: &'params Scheme::ParamsProver, @@ -81,12 +81,20 @@ where .iter() .map(|v| { v.into_iter() - .map(|v| v.as_ref().map(|v| v.into_iter().map(|f| f.into_field()).collect())) + .map(|v| { + v.as_ref() + .map(|v| v.into_iter().map(|f| f.into_field()).collect()) + }) .collect() }) .collect(); - challenges = prover.commit_phase(*phase, witnesses).unwrap().into_iter().map(|(k, v)| (k, F::into_field_fr(v))).collect(); + challenges = prover + .commit_phase(*phase, witnesses) + .unwrap() + .into_iter() + .map(|(k, v)| (k, F::into_field_fr(v))) + .collect(); } Ok(prover.create_proof()?) } @@ -103,7 +111,7 @@ pub fn create_proof< R: RngCore, T: TranscriptWrite, ConcreteCircuit: Circuit, - F: FieldFr, + F: FieldFront, >( params: &'params Scheme::ParamsProver, pk: &ProvingKey, @@ -124,9 +132,9 @@ where #[cfg(test)] mod test { - use crate::plonk::Error; - use halo2_frontend::plonk::{Circuit, FieldFr}; use super::*; + use crate::plonk::Error; + use halo2_frontend::plonk::{Circuit, FieldFront}; #[test] fn test_create_proof() { @@ -139,14 +147,13 @@ mod test { }, transcript::{Blake2bWrite, Challenge255, TranscriptWriterBuffer}, }; - use halo2_middleware::ff::Field; use halo2curves::bn256::Bn256; use rand_core::OsRng; #[derive(Clone, Copy)] struct MyCircuit; - impl Circuit for MyCircuit { + impl Circuit for MyCircuit { type Config = (); type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] @@ -168,12 +175,22 @@ mod test { } let params: ParamsKZG = ParamsKZG::setup(3, OsRng); - let vk = keygen_vk::<_, _, _,halo2curves::bn256::Fr>(¶ms, &MyCircuit).expect("keygen_vk should not fail"); - let pk = keygen_pk::<_,_,_,halo2curves::bn256::Fr>(¶ms, vk, &MyCircuit).expect("keygen_pk should not fail"); + let vk = keygen_vk::<_, _, _, halo2curves::bn256::Fr>(¶ms, &MyCircuit) + .expect("keygen_vk should not fail"); + let pk = keygen_pk::<_, _, _, halo2curves::bn256::Fr>(¶ms, vk, &MyCircuit) + .expect("keygen_pk should not fail"); let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]); // Create proof with wrong number of instances - let proof = create_proof::, ProverSHPLONK<_>,_, _, _, _, halo2curves::bn256::Fr>( + let proof = create_proof::< + KZGCommitmentScheme<_>, + ProverSHPLONK<_>, + _, + _, + _, + _, + halo2curves::bn256::Fr, + >( ¶ms, &pk, &[MyCircuit, MyCircuit], @@ -210,15 +227,14 @@ mod test { }, transcript::{Blake2bWrite, Challenge255, TranscriptWriterBuffer}, }; - use halo2_frontend::plonk::FieldFr; - use halo2_middleware::ff::Field; + use halo2_frontend::plonk::FieldFront; use halo2curves::bn256::Bn256; use rand_core::OsRng; #[derive(Clone, Copy)] struct MyCircuit; - impl Circuit for MyCircuit { + impl Circuit for MyCircuit { type Config = (); type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] @@ -241,14 +257,32 @@ mod test { let params: ParamsKZG = ParamsKZG::setup(3, OsRng); let compress_selectors = true; - let vk = keygen_vk_custom::<_,_,_,halo2curves::bn256::Fr>(¶ms, &MyCircuit, compress_selectors) - .expect("keygen_vk_custom should not fail"); - let pk = keygen_pk_custom::<_,_,_,halo2curves::bn256::Fr>(¶ms, vk, &MyCircuit, compress_selectors) - .expect("keygen_pk_custom should not fail"); + let vk = keygen_vk_custom::<_, _, _, halo2curves::bn256::Fr>( + ¶ms, + &MyCircuit, + compress_selectors, + ) + .expect("keygen_vk_custom should not fail"); + let pk = keygen_pk_custom::<_, _, _, halo2curves::bn256::Fr>( + ¶ms, + vk, + &MyCircuit, + compress_selectors, + ) + .expect("keygen_pk_custom should not fail"); let mut transcript = Blake2bWrite::<_, _, Challenge255<_>>::init(vec![]); let engine = PlonkEngineConfig::build_default(); - create_proof_with_engine::, ProverSHPLONK<_>,_,_, _, _, _, halo2curves::bn256::Fr>( + create_proof_with_engine::< + KZGCommitmentScheme<_>, + ProverSHPLONK<_>, + _, + _, + _, + _, + _, + halo2curves::bn256::Fr, + >( engine, ¶ms, &pk, @@ -259,4 +293,4 @@ mod test { ) .expect("proof generation should not fail"); } -} \ No newline at end of file +} diff --git a/halo2_proofs/tests/compress_selectors.rs b/halo2_proofs/tests/compress_selectors.rs index 32abfe4195..b14ae5324f 100644 --- a/halo2_proofs/tests/compress_selectors.rs +++ b/halo2_proofs/tests/compress_selectors.rs @@ -5,7 +5,7 @@ use std::marker::PhantomData; use ff::PrimeField; use halo2_debug::display::expr_disp_names; use halo2_frontend::circuit::compile_circuit; -use halo2_frontend::plonk::{Error, FieldFr}; +use halo2_frontend::plonk::{Error, FieldFront}; use halo2_proofs::circuit::{Cell, Layouter, SimpleFloorPlanner, Value}; use halo2_proofs::poly::Rotation; @@ -24,8 +24,6 @@ use halo2_proofs::poly::kzg::strategy::SingleStrategy; use halo2curves::bn256::{Bn256, Fr, G1Affine}; use rand_core::block::BlockRng; use rand_core::block::BlockRngCore; -use halo2_frontend::expression_arena; -use halo2_frontend::plonk; // One number generator, that can be used as a deterministic Rng, outputing fixed values. pub struct OneNg {} @@ -56,12 +54,12 @@ struct MyCircuitConfig { } #[derive(Debug)] -struct MyCircuitChip { +struct MyCircuitChip { config: MyCircuitConfig, marker: PhantomData, } -trait MyCircuitComposer { +trait MyCircuitComposer { fn raw_multiply( &self, layouter: &mut impl Layouter, @@ -93,7 +91,7 @@ trait MyCircuitComposer { FM: FnMut() -> Value<(Assigned, Assigned)>; } -impl MyCircuitChip { +impl MyCircuitChip { fn construct(config: MyCircuitConfig) -> Self { Self { config, @@ -164,7 +162,7 @@ impl MyCircuitChip { } } -impl MyCircuitComposer for MyCircuitChip { +impl MyCircuitComposer for MyCircuitChip { fn raw_multiply( &self, layouter: &mut impl Layouter, @@ -293,13 +291,13 @@ impl MyCircuitComposer for MyCircuitChip { } #[derive(Debug, Clone, Default)] -struct MyCircuit { +struct MyCircuit { x: Value, y: Value, constant: F, } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { type Config = MyCircuitConfig; type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] @@ -376,7 +374,16 @@ fn test_mycircuit( let instances = vec![vec![vec![Fr::one(), Fr::from_u128(3)]]]; let mut transcript = Blake2bWrite::<_, G1Affine, Challenge255<_>>::init(vec![]); - create_proof_with_engine::, ProverSHPLONK<'_, Bn256>, _, _, _, _, _, halo2curves::bn256::Fr>( + create_proof_with_engine::< + KZGCommitmentScheme, + ProverSHPLONK<'_, Bn256>, + _, + _, + _, + _, + _, + halo2curves::bn256::Fr, + >( engine, ¶ms, &pk, diff --git a/halo2_proofs/tests/plonk_api.rsx b/halo2_proofs/tests/plonk_api.rsx index c5606c945b..5ca0fe3584 100644 --- a/halo2_proofs/tests/plonk_api.rsx +++ b/halo2_proofs/tests/plonk_api.rsx @@ -87,12 +87,12 @@ fn plonk_api() { } #[derive(Clone)] - struct MyCircuit { + struct MyCircuit { a: Value, lookup_table: Vec, } - struct StandardPlonk { + struct StandardPlonk { config: PlonkConfig, _marker: PhantomData, } @@ -280,7 +280,7 @@ fn plonk_api() { } } - impl Circuit for MyCircuit { + impl Circuit for MyCircuit { type Config = PlonkConfig; type FloorPlanner = SimpleFloorPlanner; #[cfg(feature = "circuit-params")] diff --git a/halo2_proofs/tests/shuffle.rsx b/halo2_proofs/tests/shuffle.rsx index 23867d0dba..7c35061d36 100644 --- a/halo2_proofs/tests/shuffle.rsx +++ b/halo2_proofs/tests/shuffle.rsx @@ -1,5 +1,5 @@ use ff::{BatchInvert, FromUniformBytes}; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::{ arithmetic::{CurveAffine, Field}, circuit::{floor_planner::V1, Layouter, Value}, @@ -22,11 +22,11 @@ use halo2_proofs::{ use rand_core::{OsRng, RngCore}; use std::iter; -fn rand_2d_array(rng: &mut R) -> [[F; H]; W] { +fn rand_2d_array(rng: &mut R) -> [[F; H]; W] { [(); W].map(|_| [(); H].map(|_| F::random(&mut *rng))) } -fn shuffled( +fn shuffled( original: [[F; H]; W], rng: &mut R, ) -> [[F; H]; W] { @@ -55,7 +55,7 @@ struct MyConfig { } impl MyConfig { - fn configure(meta: &mut ConstraintSystem) -> Self { + fn configure(meta: &mut ConstraintSystem) -> Self { let [q_shuffle, q_first, q_last] = [(); 3].map(|_| meta.selector()); // First phase let original = [(); W].map(|_| meta.advice_column_in(FirstPhase)); @@ -111,12 +111,12 @@ impl MyConfig { } #[derive(Clone, Default)] -struct MyCircuit { +struct MyCircuit { original: Value<[[F; H]; W]>, shuffled: Value<[[F; H]; W]>, } -impl MyCircuit { +impl MyCircuit { fn rand(rng: &mut R) -> Self { let original = rand_2d_array::(rng); let shuffled = shuffled(original, rng); @@ -128,7 +128,7 @@ impl MyCircuit { } } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { type Config = MyConfig; type FloorPlanner = V1; #[cfg(feature = "circuit-params")] diff --git a/halo2_proofs/tests/shuffle_api.rsx b/halo2_proofs/tests/shuffle_api.rsx index c024684839..e3368fa5b2 100644 --- a/halo2_proofs/tests/shuffle_api.rsx +++ b/halo2_proofs/tests/shuffle_api.rsx @@ -25,7 +25,7 @@ use halo2_proofs::{ use halo2curves::{pasta::EqAffine, CurveAffine}; use rand_core::OsRng; -struct ShuffleChip { +struct ShuffleChip { config: ShuffleConfig, _marker: PhantomData, } @@ -40,7 +40,7 @@ struct ShuffleConfig { s_shuffle: Selector, } -impl ShuffleChip { +impl ShuffleChip { fn construct(config: ShuffleConfig) -> Self { Self { config, @@ -81,14 +81,14 @@ impl ShuffleChip { } #[derive(Default)] -struct MyCircuit { +struct MyCircuit { input_0: Vec>, input_1: Vec, shuffle_0: Vec>, shuffle_1: Vec>, } -impl Circuit for MyCircuit { +impl Circuit for MyCircuit { // Since we are using a single chip for everything, we can just reuse its config. type Config = ShuffleConfig; type FloorPlanner = SimpleFloorPlanner; diff --git a/halo2_proofs/tests/vector-ops-unblinded.rsx b/halo2_proofs/tests/vector-ops-unblinded.rsx index 9f1187de40..b33689b07b 100644 --- a/halo2_proofs/tests/vector-ops-unblinded.rsx +++ b/halo2_proofs/tests/vector-ops-unblinded.rsx @@ -4,7 +4,7 @@ use std::marker::PhantomData; use ff::FromUniformBytes; -use halo2_frontend::plonk::FieldFr; +use halo2_frontend::plonk::FieldFront; use halo2_proofs::{ arithmetic::{CurveAffine, Field}, circuit::{AssignedCell, Chip, Layouter, Region, SimpleFloorPlanner, Value}, @@ -25,7 +25,7 @@ use halo2_proofs::{ use rand_core::OsRng; // ANCHOR: instructions -trait NumericInstructions: Chip { +trait NumericInstructions: Chip { /// Variable representing a number. type Num; @@ -65,7 +65,7 @@ trait NumericInstructions: Chip { // ANCHOR: chip /// The chip that will implement our instructions! Chips store their own /// config, as well as type markers if necessary. -struct MultChip { +struct MultChip { config: FieldConfig, _marker: PhantomData, } @@ -73,7 +73,7 @@ struct MultChip { // ANCHOR: chip /// The chip that will implement our instructions! Chips store their own /// config, as well as type markers if necessary. -struct AddChip { +struct AddChip { config: FieldConfig, _marker: PhantomData, } @@ -89,7 +89,7 @@ struct FieldConfig { s: Selector, } -impl MultChip { +impl MultChip { fn construct(config: >::Config) -> Self { Self { config, @@ -126,7 +126,7 @@ impl MultChip { } } -impl AddChip { +impl AddChip { fn construct(config: >::Config) -> Self { Self { config, @@ -165,7 +165,7 @@ impl AddChip { // ANCHOR_END: chip-config // ANCHOR: chip-impl -impl Chip for MultChip { +impl Chip for MultChip { type Config = FieldConfig; type Loaded = (); @@ -180,7 +180,7 @@ impl Chip for MultChip { // ANCHOR_END: chip-impl // ANCHOR: chip-impl -impl Chip for AddChip { +impl Chip for AddChip { type Config = FieldConfig; type Loaded = (); @@ -196,9 +196,9 @@ impl Chip for AddChip { // ANCHOR: instructions-impl /// A variable representing a number. #[derive(Clone, Debug)] -struct Number(AssignedCell); +struct Number(AssignedCell); -impl NumericInstructions for MultChip { +impl NumericInstructions for MultChip { type Num = Number; fn load_unblinded( @@ -280,7 +280,7 @@ impl NumericInstructions for MultChip { } // ANCHOR_END: instructions-impl -impl NumericInstructions for AddChip { +impl NumericInstructions for AddChip { type Num = Number; fn load_unblinded( @@ -362,12 +362,12 @@ impl NumericInstructions for AddChip { } #[derive(Default)] -struct MulCircuit { +struct MulCircuit { a: Vec>, b: Vec>, } -impl Circuit for MulCircuit { +impl Circuit for MulCircuit { // Since we are using a single chip for everything, we can just reuse its config. type Config = FieldConfig; type FloorPlanner = SimpleFloorPlanner; @@ -415,12 +415,12 @@ impl Circuit for MulCircuit { // ANCHOR_END: circuit #[derive(Default)] -struct AddCircuit { +struct AddCircuit { a: Vec>, b: Vec>, } -impl Circuit for AddCircuit { +impl Circuit for AddCircuit { // Since we are using a single chip for everything, we can just reuse its config. type Config = FieldConfig; type FloorPlanner = SimpleFloorPlanner; @@ -467,7 +467,7 @@ impl Circuit for AddCircuit { } // ANCHOR_END: circuit -fn test_prover>( +fn test_prover>( k: u32, circuit: impl Circuit, expected: bool,