Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make Expression copiable using singleton arena #354

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion halo2_backend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = [
"Privacy Scaling Explorations team",
]
edition = "2021"
rust-version = "1.66.0"
rust-version = "1.70.0"
description = """
Halo2 backend implementation. This package implements the halo2 proof system which includes setup (key generation), proving and verifying.
"""
Expand Down
2 changes: 1 addition & 1 deletion halo2_debug/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ authors = [
"Privacy Scaling Explorations team",
]
edition = "2021"
rust-version = "1.66.0"
rust-version = "1.70.0"
description = """
Halo2 Debug. This package contains utilities for debugging and testing within
the halo2 ecosystem.
Expand Down
2 changes: 1 addition & 1 deletion halo2_frontend/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ authors = [
"Privacy Scaling Explorations team",
]
edition = "2021"
rust-version = "1.66.0"
rust-version = "1.70.0"
description = """
Halo2 frontend implementation. This package implements an API to write circuits, handles witness generation and contains the MockProver.
"""
Expand Down
56 changes: 30 additions & 26 deletions halo2_frontend/src/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ pub use floor_planner::single_pass::SimpleFloorPlanner;

pub mod layouter;

use crate::plonk::FieldFront;
pub use table_layouter::{SimpleTableLayouter, TableLayouter};

/// Compile a circuit. Runs configure and synthesize on the circuit in order to materialize the
Expand All @@ -37,15 +38,15 @@ pub use table_layouter::{SimpleTableLayouter, TableLayouter};
/// 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<F>>(
pub fn compile_circuit<FF: FieldFront<Field = F>, F: Field, ConcreteCircuit: Circuit<FF>>(
k: u32,
circuit: &ConcreteCircuit,
compress_selectors: bool,
) -> Result<
(
CompiledCircuit<F>,
ConcreteCircuit::Config,
ConstraintSystem<F>,
ConstraintSystem<FF>,
),
Error,
> {
Expand All @@ -64,7 +65,7 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(

let mut assembly = plonk::keygen::Assembly {
k,
fixed: vec![vec![F::ZERO.into(); n]; cs.num_fixed_columns],
fixed: vec![vec![FF::ZERO.into(); n]; cs.num_fixed_columns],
permutation: permutation::Assembly::new(n, &cs.permutation),
selectors: vec![vec![false; n]; cs.num_selectors],
usable_rows: 0..n - (cs.blinding_factors() + 1),
Expand Down Expand Up @@ -98,7 +99,10 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
permutation: halo2_middleware::permutation::AssemblyMid {
copies: assembly.permutation.copies,
},
fixed,
fixed: fixed
.into_iter()
.map(|f| f.into_iter().map(|f| f.into_field()).collect())
.collect(),
};

Ok((
Expand All @@ -111,7 +115,7 @@ pub fn compile_circuit<F: Field, ConcreteCircuit: Circuit<F>>(
))
}

struct WitnessCollection<'a, F: Field> {
struct WitnessCollection<'a, F: FieldFront> {
k: u32,
current_phase: sealed::Phase,
advice_column_phase: &'a Vec<sealed::Phase>,
Expand All @@ -121,7 +125,7 @@ struct WitnessCollection<'a, F: Field> {
usable_rows: RangeTo<usize>,
}

impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {
impl<'a, F: FieldFront> Assignment<F> for WitnessCollection<'a, F> {
fn enter_region<NR, N>(&mut self, _: N)
where
NR: Into<String>,
Expand Down Expand Up @@ -252,7 +256,7 @@ impl<'a, F: Field> Assignment<F> for WitnessCollection<'a, F> {

/// Witness calculator. Frontend function
#[derive(Debug)]
pub struct WitnessCalculator<'a, F: Field, ConcreteCircuit: Circuit<F>> {
pub struct WitnessCalculator<'a, F: FieldFront, ConcreteCircuit: Circuit<F>> {
k: u32,
n: usize,
unusable_rows_start: usize,
Expand All @@ -263,7 +267,7 @@ pub struct WitnessCalculator<'a, F: Field, ConcreteCircuit: Circuit<F>> {
next_phase: u8,
}

impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, ConcreteCircuit> {
impl<'a, F: FieldFront, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, ConcreteCircuit> {
/// Create a new WitnessCalculator
pub fn new(
k: u32,
Expand Down Expand Up @@ -360,7 +364,7 @@ impl<'a, F: Field, ConcreteCircuit: Circuit<F>> WitnessCalculator<'a, F, Concret

// Turn vectors of `Assigned<F>` into vectors of `F` by evaluation the divisions in `Assigned<F>`
// using batched inversions.
fn batch_invert_assigned<F: Field>(assigned: Vec<Vec<Assigned<F>>>) -> Vec<Vec<F>> {
fn batch_invert_assigned<F: FieldFront>(assigned: Vec<Vec<Assigned<F>>>) -> Vec<Vec<F>> {
let mut assigned_denominators: Vec<_> = assigned
.iter()
.map(|f| {
Expand Down Expand Up @@ -392,7 +396,7 @@ fn batch_invert_assigned<F: Field>(assigned: Vec<Vec<Assigned<F>>>) -> Vec<Vec<F
// Turn a slice of `Assigned<F>` 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<F>`.
fn poly_invert<F: Field>(
fn poly_invert<F: FieldFront>(
poly: &[Assigned<F>],
inv_denoms: impl ExactSizeIterator<Item = F>,
) -> Vec<F> {
Expand All @@ -411,7 +415,7 @@ fn poly_invert<F: Field>(
/// 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<F: Field>: Sized {
pub trait Chip<F: FieldFront>: 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`].
///
Expand Down Expand Up @@ -483,13 +487,13 @@ pub struct Cell {

/// An assigned cell.
#[derive(Clone, Debug)]
pub struct AssignedCell<V, F: Field> {
pub struct AssignedCell<V, F: FieldFront> {
value: Value<V>,
cell: Cell,
_marker: PhantomData<F>,
}

impl<V, F: Field> AssignedCell<V, F> {
impl<V, F: FieldFront> AssignedCell<V, F> {
/// Returns the value of the [`AssignedCell`].
pub fn value(&self) -> Value<&V> {
self.value.as_ref()
Expand All @@ -501,7 +505,7 @@ impl<V, F: Field> AssignedCell<V, F> {
}
}

impl<V, F: Field> AssignedCell<V, F>
impl<V, F: FieldFront> AssignedCell<V, F>
where
for<'v> Assigned<F>: From<&'v V>,
{
Expand All @@ -511,7 +515,7 @@ where
}
}

impl<F: Field> AssignedCell<Assigned<F>, F> {
impl<F: FieldFront> AssignedCell<Assigned<F>, F> {
/// Evaluates this assigned cell's value directly, performing an unbatched inversion
/// if necessary.
///
Expand All @@ -525,7 +529,7 @@ impl<F: Field> AssignedCell<Assigned<F>, F> {
}
}

impl<V: Clone, F: Field> AssignedCell<V, F>
impl<V: Clone, F: FieldFront> AssignedCell<V, F>
where
for<'v> Assigned<F>: From<&'v V>,
{
Expand Down Expand Up @@ -564,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: Field> {
pub struct Region<'r, F: FieldFront> {
region: &'r mut dyn layouter::RegionLayouter<F>,
}

impl<'r, F: Field> From<&'r mut dyn layouter::RegionLayouter<F>> for Region<'r, F> {
impl<'r, F: FieldFront> From<&'r mut dyn layouter::RegionLayouter<F>> for Region<'r, F> {
fn from(region: &'r mut dyn layouter::RegionLayouter<F>) -> Self {
Region { region }
}
}

impl<'r, F: Field> Region<'r, F> {
impl<'r, F: FieldFront> Region<'r, F> {
/// Enables a selector at the given offset.
pub fn enable_selector<A, AR>(
&mut self,
Expand Down Expand Up @@ -767,17 +771,17 @@ impl<'r, F: Field> Region<'r, F> {

/// A lookup table in the circuit.
#[derive(Debug)]
pub struct Table<'r, F: Field> {
pub struct Table<'r, F: FieldFront> {
table: &'r mut dyn TableLayouter<F>,
}

impl<'r, F: Field> From<&'r mut dyn TableLayouter<F>> for Table<'r, F> {
impl<'r, F: FieldFront> From<&'r mut dyn TableLayouter<F>> for Table<'r, F> {
fn from(table: &'r mut dyn TableLayouter<F>) -> Self {
Table { table }
}
}

impl<'r, F: Field> 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.
Expand Down Expand Up @@ -808,7 +812,7 @@ impl<'r, F: Field> Table<'r, F> {
///
/// This abstracts over the circuit assignments, handling row indices etc.
///
pub trait Layouter<F: Field> {
pub trait Layouter<F: FieldFront> {
/// Represents the type of the "root" of this layouter, so that nested namespaces
/// can minimize indirection.
type Root: Layouter<F>;
Expand Down Expand Up @@ -892,9 +896,9 @@ pub trait Layouter<F: Field> {
/// 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: Field, L: Layouter<F> + 'a>(&'a mut L, PhantomData<F>);
pub struct NamespacedLayouter<'a, F: FieldFront, L: Layouter<F> + 'a>(&'a mut L, PhantomData<F>);

impl<'a, F: Field, L: Layouter<F> + 'a> Layouter<F> for NamespacedLayouter<'a, F, L> {
impl<'a, F: FieldFront, L: Layouter<F> + 'a> Layouter<F> for NamespacedLayouter<'a, F, L> {
type Root = L::Root;

fn assign_region<A, AR, N, NR>(&mut self, name: N, assignment: A) -> Result<AR, Error>
Expand Down Expand Up @@ -945,7 +949,7 @@ impl<'a, F: Field, L: Layouter<F> + 'a> Layouter<F> for NamespacedLayouter<'a, F
}
}

impl<'a, F: Field, L: Layouter<F> + 'a> Drop for NamespacedLayouter<'a, F, L> {
impl<'a, F: FieldFront, L: Layouter<F> + 'a> Drop for NamespacedLayouter<'a, F, L> {
fn drop(&mut self) {
let gadget_name = {
#[cfg(feature = "gadget-traces")]
Expand Down
21 changes: 10 additions & 11 deletions halo2_frontend/src/circuit/floor_planner/single_pass.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ use std::fmt;
use std::marker::PhantomData;

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

use crate::plonk::Assigned;
use crate::plonk::{Assigned, FieldFront};
use crate::{
circuit::{
layouter::{RegionColumn, RegionLayouter, RegionShape, SyncDeps, TableLayouter},
Expand All @@ -28,7 +27,7 @@ use crate::{
pub struct SimpleFloorPlanner;

impl FloorPlanner for SimpleFloorPlanner {
fn synthesize<F: Field, CS: Assignment<F> + SyncDeps, C: Circuit<F>>(
fn synthesize<F: FieldFront, CS: Assignment<F> + SyncDeps, C: Circuit<F>>(
cs: &mut CS,
circuit: &C,
config: C::Config,
Expand All @@ -40,7 +39,7 @@ impl FloorPlanner for SimpleFloorPlanner {
}

/// A [`Layouter`] for a single-chip circuit.
pub struct SingleChipLayouter<'a, F: Field, CS: Assignment<F> + 'a> {
pub struct SingleChipLayouter<'a, F: FieldFront, CS: Assignment<F> + 'a> {
cs: &'a mut CS,
constants: Vec<Column<Fixed>>,
/// Stores the starting row for each region.
Expand All @@ -52,7 +51,7 @@ pub struct SingleChipLayouter<'a, F: Field, CS: Assignment<F> + 'a> {
_marker: PhantomData<F>,
}

impl<'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug for SingleChipLayouter<'a, F, CS> {
impl<'a, F: FieldFront, CS: Assignment<F> + '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)
Expand All @@ -61,7 +60,7 @@ impl<'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug for SingleChipLayouter<'a,
}
}

impl<'a, F: Field, CS: Assignment<F>> SingleChipLayouter<'a, F, CS> {
impl<'a, F: FieldFront, CS: Assignment<F>> SingleChipLayouter<'a, F, CS> {
/// Creates a new single-chip layouter.
pub fn new(cs: &'a mut CS, constants: Vec<Column<Fixed>>) -> Result<Self, Error> {
let ret = SingleChipLayouter {
Expand All @@ -76,7 +75,7 @@ impl<'a, F: Field, CS: Assignment<F>> SingleChipLayouter<'a, F, CS> {
}
}

impl<'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> Layouter<F>
impl<'a, F: FieldFront, CS: Assignment<F> + 'a + SyncDeps> Layouter<F>
for SingleChipLayouter<'a, F, CS>
{
type Root = Self;
Expand Down Expand Up @@ -223,14 +222,14 @@ impl<'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> Layouter<F>
}
}

struct SingleChipLayouterRegion<'r, 'a, F: Field, CS: Assignment<F> + 'a> {
struct SingleChipLayouterRegion<'r, 'a, F: FieldFront, CS: Assignment<F> + '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<F>, Cell)>,
}

impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug
impl<'r, 'a, F: FieldFront, CS: Assignment<F> + 'a> fmt::Debug
for SingleChipLayouterRegion<'r, 'a, F, CS>
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
Expand All @@ -241,7 +240,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> fmt::Debug
}
}

impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> SingleChipLayouterRegion<'r, 'a, F, CS> {
impl<'r, 'a, F: FieldFront, CS: Assignment<F> + 'a> SingleChipLayouterRegion<'r, 'a, F, CS> {
fn new(layouter: &'r mut SingleChipLayouter<'a, F, CS>, region_index: RegionIndex) -> Self {
SingleChipLayouterRegion {
layouter,
Expand All @@ -251,7 +250,7 @@ impl<'r, 'a, F: Field, CS: Assignment<F> + 'a> SingleChipLayouterRegion<'r, 'a,
}
}

impl<'r, 'a, F: Field, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
impl<'r, 'a, F: FieldFront, CS: Assignment<F> + 'a + SyncDeps> RegionLayouter<F>
for SingleChipLayouterRegion<'r, 'a, F, CS>
{
fn enable_selector<'v>(
Expand Down
Loading
Loading