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

Allow halo2 constraint names to have non static names #156

Merged
Show file tree
Hide file tree
Changes from 2 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
11 changes: 6 additions & 5 deletions halo2_gadgets/src/utilities/lookup_range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -582,13 +582,14 @@ mod tests {
assert_eq!(
prover.verify(),
Err(vec![VerifyFailure::Lookup {
name: "lookup",
name: "lookup".to_string(),
lookup_index: 0,
location: FailureLocation::InRegion {
region: (1, "Range check 6 bits").into(),
offset: 1,
},
}])
}]),

);
}

Expand All @@ -603,15 +604,15 @@ mod tests {
prover.verify(),
Err(vec![
VerifyFailure::Lookup {
name: "lookup",
name: "lookup".to_string(),
lookup_index: 0,
location: FailureLocation::InRegion {
region: (1, "Range check 6 bits").into(),
offset: 0,
},
},
VerifyFailure::Lookup {
name: "lookup",
name: "lookup".to_string(),
lookup_index: 0,
location: FailureLocation::InRegion {
region: (1, "Range check 6 bits").into(),
Expand Down Expand Up @@ -641,7 +642,7 @@ mod tests {
assert_eq!(
prover.verify(),
Err(vec![VerifyFailure::Lookup {
name: "lookup",
name: "lookup".to_string(),
lookup_index: 0,
location: FailureLocation::InRegion {
region: (1, "Range check 6 bits").into(),
Expand Down
8 changes: 4 additions & 4 deletions halo2_proofs/src/dev.rs
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ impl<F: FieldExt> MockProver<F> {
assert!(table.binary_search(input).is_err());

Some(VerifyFailure::Lookup {
name: lookup.name,
name: lookup.name.clone(),
lookup_index,
location: FailureLocation::find_expressions(
&self.cs,
Expand Down Expand Up @@ -1300,7 +1300,7 @@ impl<F: FieldExt> MockProver<F> {
.filter_map(move |(input, input_row)| {
if table.binary_search(input).is_err() {
Some(VerifyFailure::Lookup {
name: lookup.name,
name: lookup.name.clone(),
lookup_index,
location: FailureLocation::find_expressions(
&self.cs,
Expand Down Expand Up @@ -1723,7 +1723,7 @@ mod tests {
assert_eq!(
prover.verify(),
Err(vec![VerifyFailure::Lookup {
name: "lookup",
name: "lookup".to_string(),
lookup_index: 0,
location: FailureLocation::InRegion {
region: (1, "Faulty synthesis").into(),
Expand Down Expand Up @@ -1855,7 +1855,7 @@ mod tests {
assert_eq!(
prover.verify(),
Err(vec![VerifyFailure::Lookup {
name: "lookup",
name: "lookup".to_string(),
lookup_index: 0,
location: FailureLocation::InRegion {
region: (2, "Faulty synthesis").into(),
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/dev/failure.rs
Original file line number Diff line number Diff line change
Expand Up @@ -160,7 +160,7 @@ pub enum VerifyFailure {
/// A lookup input did not exist in its corresponding table.
Lookup {
/// The name of the lookup that is not satisfied.
name: &'static str,
name: String,
/// The index of the lookup that is not satisfied. These indices are assigned in
/// the order in which `ConstraintSystem::lookup` is called during
/// `Circuit::configure`.
Expand Down Expand Up @@ -278,7 +278,7 @@ impl Debug for VerifyFailure {
};

let debug = ConstraintCaseDebug {
constraint: *constraint,
constraint: constraint.clone(),
location: location.clone(),
cell_values: cell_values
.iter()
Expand Down
4 changes: 2 additions & 2 deletions halo2_proofs/src/dev/gates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,14 @@ use crate::{

#[derive(Debug)]
struct Constraint {
name: &'static str,
name: String,
expression: String,
queries: BTreeSet<String>,
}

#[derive(Debug)]
struct Gate {
name: &'static str,
name: String,
constraints: Vec<Constraint>,
}

Expand Down
44 changes: 22 additions & 22 deletions halo2_proofs/src/dev/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,25 +75,25 @@ impl fmt::Display for DebugColumn {
/// within a custom gate.
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord)]
pub struct VirtualCell {
name: &'static str,
name: String,
pub(super) column: Column,
pub(super) rotation: i32,
}

impl From<(Column, i32)> for VirtualCell {
fn from((column, rotation): (Column, i32)) -> Self {
VirtualCell {
name: "",
name: "".to_string(),
column,
rotation,
}
}
}

impl From<(&'static str, Column, i32)> for VirtualCell {
fn from((name, column, rotation): (&'static str, Column, i32)) -> Self {
impl<S: AsRef<str>> From<(S, Column, i32)> for VirtualCell {
fn from((name, column, rotation): (S, Column, i32)) -> Self {
VirtualCell {
name,
name: name.as_ref().to_string(),
column,
rotation,
}
Expand All @@ -103,7 +103,7 @@ impl From<(&'static str, Column, i32)> for VirtualCell {
impl From<plonk::VirtualCell> for VirtualCell {
fn from(c: plonk::VirtualCell) -> Self {
VirtualCell {
name: "",
name: "".to_string(),
column: c.column.into(),
rotation: c.rotation.0,
}
Expand All @@ -114,7 +114,7 @@ impl fmt::Display for VirtualCell {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}@{}", self.column, self.rotation)?;
if !self.name.is_empty() {
write!(f, "({})", self.name)?;
write!(f, "({})", self.name.as_str())?;
}
Ok(())
}
Expand All @@ -123,15 +123,15 @@ impl fmt::Display for VirtualCell {
/// Helper structure used to be able to inject Column annotations inside a `Display` or `Debug` call.
#[derive(Clone, Debug)]
pub(super) struct DebugVirtualCell {
name: &'static str,
name: String,
column: DebugColumn,
rotation: i32,
}

impl From<(&VirtualCell, Option<&HashMap<Column, String>>)> for DebugVirtualCell {
fn from(info: (&VirtualCell, Option<&HashMap<Column, String>>)) -> Self {
DebugVirtualCell {
name: info.0.name,
name: info.0.name.clone(),
column: DebugColumn::from((info.0.column, info.1)),
rotation: info.0.rotation,
}
Expand All @@ -149,30 +149,30 @@ impl fmt::Display for DebugVirtualCell {
}

/// Metadata about a configured gate within a circuit.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Gate {
/// The index of the active gate. These indices are assigned in the order in which
/// `ConstraintSystem::create_gate` is called during `Circuit::configure`.
pub(super) index: usize,
/// The name of the active gate. These are specified by the gate creator (such as
/// a chip implementation), and is not enforced to be unique.
pub(super) name: &'static str,
pub(super) name: String,
}

impl fmt::Display for Gate {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Gate {} ('{}')", self.index, self.name)
write!(f, "Gate {} ('{}')", self.index, self.name.as_str())
}
}

impl From<(usize, &'static str)> for Gate {
fn from((index, name): (usize, &'static str)) -> Self {
Gate { index, name }
impl<S: AsRef<str>> From<(usize, S)> for Gate {
fn from((index, name): (usize, S)) -> Self {
Gate { index, name: name.as_ref().to_string() }
}
}

/// Metadata about a configured constraint within a circuit.
#[derive(Debug, PartialEq, Eq, Clone, Copy)]
#[derive(Debug, PartialEq, Eq, Clone)]
pub struct Constraint {
/// The gate containing the constraint.
pub(super) gate: Gate,
Expand All @@ -182,7 +182,7 @@ pub struct Constraint {
pub(super) index: usize,
/// The name of the constraint. This is specified by the gate creator (such as a chip
/// implementation), and is not enforced to be unique.
pub(super) name: &'static str,
pub(super) name: String,
}

impl fmt::Display for Constraint {
Expand All @@ -194,17 +194,17 @@ impl fmt::Display for Constraint {
if self.name.is_empty() {
String::new()
} else {
format!(" ('{}')", self.name)
format!(" ('{}')", self.name.as_str())
},
self.gate.index,
self.gate.name,
)
}
}

impl From<(Gate, usize, &'static str)> for Constraint {
fn from((gate, index, name): (Gate, usize, &'static str)) -> Self {
Constraint { gate, index, name }
impl<S: AsRef<str>> From<(Gate, usize, S)> for Constraint {
fn from((gate, index, name): (Gate, usize, S)) -> Self {
Constraint { gate, index, name: name.as_ref().to_string() }
}
}

Expand Down Expand Up @@ -250,7 +250,7 @@ impl Debug for Region {

impl fmt::Display for Region {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Region {} ('{}')", self.index, self.name)
write!(f, "Region {} ('{}')", self.index, self.name.as_str())
}
}

Expand Down
42 changes: 21 additions & 21 deletions halo2_proofs/src/plonk/circuit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1213,25 +1213,25 @@ impl<Col: Into<Column<Any>>> From<(Col, Rotation)> for VirtualCell {
/// These are returned by the closures passed to `ConstraintSystem::create_gate`.
#[derive(Debug)]
pub struct Constraint<F: Field> {
name: &'static str,
name: String,
poly: Expression<F>,
}

impl<F: Field> From<Expression<F>> for Constraint<F> {
fn from(poly: Expression<F>) -> Self {
Constraint { name: "", poly }
Constraint { name: "".to_string(), poly }
}
}

impl<F: Field> From<(&'static str, Expression<F>)> for Constraint<F> {
fn from((name, poly): (&'static str, Expression<F>)) -> Self {
Constraint { name, poly }
impl<F: Field, S: AsRef<str>> From<(S, Expression<F>)> for Constraint<F> {
fn from((name, poly): (S, Expression<F>)) -> Self {
Constraint { name: name.as_ref().to_string(), poly }
}
}

impl<F: Field> From<Expression<F>> for Vec<Constraint<F>> {
fn from(poly: Expression<F>) -> Self {
vec![Constraint { name: "", poly }]
vec![Constraint { name: "".to_string(), poly }]
}
}

Expand Down Expand Up @@ -1321,8 +1321,8 @@ impl<F: Field, C: Into<Constraint<F>>, Iter: IntoIterator<Item = C>> IntoIterato
/// Gate
#[derive(Clone, Debug)]
pub struct Gate<F: Field> {
name: &'static str,
constraint_names: Vec<&'static str>,
name: String,
constraint_names: Vec<String>,
polys: Vec<Expression<F>>,
/// We track queried selectors separately from other cells, so that we can use them to
/// trigger debug checks on gates.
Expand All @@ -1331,12 +1331,12 @@ pub struct Gate<F: Field> {
}

impl<F: Field> Gate<F> {
pub(crate) fn name(&self) -> &'static str {
self.name
pub(crate) fn name(&self) -> String {
self.name.clone()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like the name will be cloned twice when calling (gate_index, gate.name()).into() to metadata::Gate, not sure if it'd be optimized out, but I think it'd be nice to have the code look no extra clone (and more consistent since other APIs returns also reference).

Suggested change
pub(crate) fn name(&self) -> String {
self.name.clone()
pub(crate) fn name(&self) -> &str {
self.name.as_str()

}

pub(crate) fn constraint_name(&self, constraint_index: usize) -> &'static str {
self.constraint_names[constraint_index]
pub(crate) fn constraint_name(&self, constraint_index: usize) -> String {
self.constraint_names[constraint_index].clone()
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub(crate) fn constraint_name(&self, constraint_index: usize) -> String {
self.constraint_names[constraint_index].clone()
pub(crate) fn constraint_name(&self, constraint_index: usize) -> &str {
self.constraint_names[constraint_index].as_str()

}

/// Returns constraints of this gate
Expand Down Expand Up @@ -1529,9 +1529,9 @@ impl<F: Field> ConstraintSystem<F> {
///
/// `table_map` returns a map between input expressions and the table columns
/// they need to match.
pub fn lookup(
pub fn lookup<S: AsRef<str>>(
&mut self,
name: &'static str,
name: S,
table_map: impl FnOnce(&mut VirtualCells<'_, F>) -> Vec<(Expression<F>, TableColumn)>,
) -> usize {
let mut cells = VirtualCells::new(self);
Expand All @@ -1550,7 +1550,7 @@ impl<F: Field> ConstraintSystem<F> {

let index = self.lookups.len();

self.lookups.push(lookup::Argument::new(name, table_map));
self.lookups.push(lookup::Argument::new(name.as_ref(), table_map));

index
}
Expand All @@ -1559,17 +1559,17 @@ impl<F: Field> ConstraintSystem<F> {
///
/// `table_map` returns a map between input expressions and the table expressions
/// they need to match.
pub fn lookup_any(
pub fn lookup_any<S: AsRef<str>>(
&mut self,
name: &'static str,
name: S,
table_map: impl FnOnce(&mut VirtualCells<'_, F>) -> Vec<(Expression<F>, Expression<F>)>,
) -> usize {
let mut cells = VirtualCells::new(self);
let table_map = table_map(&mut cells);

let index = self.lookups.len();

self.lookups.push(lookup::Argument::new(name, table_map));
self.lookups.push(lookup::Argument::new(name.as_ref(), table_map));

index
}
Expand Down Expand Up @@ -1689,9 +1689,9 @@ impl<F: Field> ConstraintSystem<F> {
///
/// A gate is required to contain polynomial constraints. This method will panic if
/// `constraints` returns an empty iterator.
pub fn create_gate<C: Into<Constraint<F>>, Iter: IntoIterator<Item = C>>(
pub fn create_gate<C: Into<Constraint<F>>, Iter: IntoIterator<Item = C>, S: AsRef<str>>(
&mut self,
name: &'static str,
name: S,
constraints: impl FnOnce(&mut VirtualCells<'_, F>) -> Iter,
) {
let mut cells = VirtualCells::new(self);
Expand All @@ -1711,7 +1711,7 @@ impl<F: Field> ConstraintSystem<F> {
);

self.gates.push(Gate {
name,
name: name.as_ref().to_string(),
constraint_names,
polys,
queried_selectors,
Expand Down
Loading