From 07655b9e7bfa3d69ee6072f6d91a072338c33ab0 Mon Sep 17 00:00:00 2001 From: therealyingtong Date: Wed, 22 Feb 2023 02:16:23 +0800 Subject: [PATCH] circuit::layouter::RegionLayouter: Introduce add_to_lookup() method Co-authored-by: Avi Dessauer --- halo2_proofs/src/circuit.rs | 8 ++++++++ .../src/circuit/floor_planner/single_pass.rs | 9 +++++++++ halo2_proofs/src/circuit/floor_planner/v1.rs | 7 +++++++ halo2_proofs/src/circuit/layouter.rs | 12 ++++++++++++ halo2_proofs/src/dev/tfp.rs | 6 ++++++ 5 files changed, 42 insertions(+) diff --git a/halo2_proofs/src/circuit.rs b/halo2_proofs/src/circuit.rs index 0822d8d8..9ad20b94 100644 --- a/halo2_proofs/src/circuit.rs +++ b/halo2_proofs/src/circuit.rs @@ -4,6 +4,8 @@ use std::{fmt, marker::PhantomData}; use ff::Field; +#[cfg(feature = "unstable-dynamic-lookups")] +use crate::plonk::TableTag; use crate::plonk::{Advice, Any, Assigned, Column, Error, Fixed, Instance, Selector, TableColumn}; mod value; @@ -204,6 +206,12 @@ impl<'r, F: Field> Region<'r, F> { .enable_selector(&|| annotation().into(), selector, offset) } + /// Enables a dynamic table lookup at the given offset. + #[cfg(feature = "unstable-dynamic-lookups")] + pub fn add_to_lookup(&mut self, table: TableTag, offset: usize) -> Result<(), Error> { + self.region.add_to_lookup(table, offset) + } + /// Assign an advice column value (witness). /// /// Even though `to` has `FnMut` bounds, it is guaranteed to be called at most once. diff --git a/halo2_proofs/src/circuit/floor_planner/single_pass.rs b/halo2_proofs/src/circuit/floor_planner/single_pass.rs index 968c3726..8e2c63d6 100644 --- a/halo2_proofs/src/circuit/floor_planner/single_pass.rs +++ b/halo2_proofs/src/circuit/floor_planner/single_pass.rs @@ -5,6 +5,8 @@ use std::marker::PhantomData; use ff::Field; +#[cfg(feature = "unstable-dynamic-lookups")] +use crate::plonk::TableTag; use crate::{ circuit::{ layouter::{RegionColumn, RegionLayouter, RegionShape}, @@ -259,6 +261,13 @@ impl<'r, 'a, F: Field, CS: Assignment + 'a> RegionLayouter ) } + #[cfg(feature = "unstable-dynamic-lookups")] + fn add_to_lookup(&mut self, table: TableTag, offset: usize) -> Result<(), Error> { + self.layouter + .cs + .add_to_lookup(table, *self.layouter.regions[*self.region_index] + offset) + } + fn assign_advice<'v>( &'v mut self, annotation: &'v (dyn Fn() -> String + 'v), diff --git a/halo2_proofs/src/circuit/floor_planner/v1.rs b/halo2_proofs/src/circuit/floor_planner/v1.rs index 42f197ca..2a54c3dc 100644 --- a/halo2_proofs/src/circuit/floor_planner/v1.rs +++ b/halo2_proofs/src/circuit/floor_planner/v1.rs @@ -374,6 +374,13 @@ impl<'r, 'a, F: Field, CS: Assignment + 'a> RegionLayouter for V1Region<'r ) } + #[cfg(feature = "unstable-dynamic-lookups")] + fn add_to_lookup(&mut self, table: crate::plonk::TableTag, offset: usize) -> Result<(), Error> { + self.plan + .cs + .add_to_lookup(table, *self.plan.regions[*self.region_index] + offset) + } + fn assign_advice<'v>( &'v mut self, annotation: &'v (dyn Fn() -> String + 'v), diff --git a/halo2_proofs/src/circuit/layouter.rs b/halo2_proofs/src/circuit/layouter.rs index 3e3e87b0..e36839f8 100644 --- a/halo2_proofs/src/circuit/layouter.rs +++ b/halo2_proofs/src/circuit/layouter.rs @@ -51,6 +51,10 @@ pub trait RegionLayouter: fmt::Debug { offset: usize, ) -> Result<(), Error>; + /// Enables a dynamic table lookup at the given offset. + #[cfg(feature = "unstable-dynamic-lookups")] + fn add_to_lookup(&mut self, table: TableTag, offset: usize) -> Result<(), Error>; + /// Assign an advice column value (witness) fn assign_advice<'v>( &'v mut self, @@ -215,6 +219,14 @@ impl RegionLayouter for RegionShape { Ok(()) } + #[cfg(feature = "unstable-dynamic-lookups")] + fn add_to_lookup(&mut self, table: TableTag, offset: usize) -> Result<(), Error> { + // Track the tag's fixed column as part of the region's shape. + self.columns.insert(table.into()); + self.row_count = cmp::max(self.row_count, offset + 1); + Ok(()) + } + fn assign_advice<'v>( &'v mut self, _: &'v (dyn Fn() -> String + 'v), diff --git a/halo2_proofs/src/dev/tfp.rs b/halo2_proofs/src/dev/tfp.rs index 77d4657d..31cdeb7c 100644 --- a/halo2_proofs/src/dev/tfp.rs +++ b/halo2_proofs/src/dev/tfp.rs @@ -236,6 +236,12 @@ impl<'r, F: Field> RegionLayouter for TracingRegion<'r, F> { self.0.enable_selector(annotation, selector, offset) } + #[cfg(feature = "unstable-dynamic-lookups")] + fn add_to_lookup(&mut self, table: TableTag, offset: usize) -> Result<(), Error> { + debug!(target: "add_to_lookup", table = ?table, offset = ?offset); + self.0.add_to_lookup(table, offset) + } + fn assign_advice<'v>( &'v mut self, annotation: &'v (dyn Fn() -> String + 'v),