Skip to content

Commit

Permalink
Allow halo2 constraint names to have non static names (privacy-scalin…
Browse files Browse the repository at this point in the history
…g-explorations#156)

* static ref to String type in Gates, Constraints, VirtualCell, Argument

* 'lookup'.to_string()

* return &str for gate name and constriant_name, also run fmt

* Update halo2_gadgets/Cargo.toml

Co-authored-by: Han <tinghan0110@gmail.com>

* upgrade rust-toochain

---------

Co-authored-by: Carlos Pérez <37264926+CPerezz@users.noreply.github.com>
Co-authored-by: Han <tinghan0110@gmail.com>
  • Loading branch information
3 people authored and Velaciela committed Oct 9, 2023
1 parent b26a0ae commit e577808
Show file tree
Hide file tree
Showing 7 changed files with 81 additions and 63 deletions.
10 changes: 5 additions & 5 deletions halo2_gadgets/src/utilities/lookup_range_check.rs
Original file line number Diff line number Diff line change
Expand Up @@ -586,13 +586,13 @@ 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 @@ -607,15 +607,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 @@ -645,7 +645,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 @@ -1413,7 +1413,7 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, 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 @@ -1787,7 +1787,7 @@ impl<'a, F: FromUniformBytes<64> + Ord> MockProver<'a, 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 @@ -2221,7 +2221,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 @@ -2355,7 +2355,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 @@ -159,7 +159,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 @@ -277,7 +277,7 @@ impl Debug for VerifyFailure {
};

let debug = ConstraintCaseDebug {
constraint: *constraint,
constraint: constraint.clone(),
location: location.clone(),
cell_values: cell_values
.iter()
Expand Down
8 changes: 4 additions & 4 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 Expand Up @@ -122,13 +122,13 @@ impl CircuitGates {
.gates
.iter()
.map(|gate| Gate {
name: gate.name(),
name: gate.name().to_string(),
constraints: gate
.polynomials()
.iter()
.enumerate()
.map(|(i, constraint)| Constraint {
name: gate.constraint_name(i),
name: gate.constraint_name(i).to_string(),
expression: constraint.evaluate(
&util::format_value,
&|selector| format!("S{}", selector.0),
Expand Down
51 changes: 29 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,33 @@ 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 +185,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 +197,21 @@ 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 +257,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
Loading

0 comments on commit e577808

Please sign in to comment.