From 82da677addbb7840982b21ab8783fe6ae729f077 Mon Sep 17 00:00:00 2001 From: Jack Grigg Date: Fri, 22 Jan 2021 19:46:06 +0000 Subject: [PATCH] Add name field to ConstraintSystem::create_gate The name has type `&'static str`, as gates apply to every row and thus do not require any runtime information to name. --- benches/plonk.rs | 2 +- examples/performance_model.rs | 4 ++-- src/dev.rs | 9 +++++++-- src/plonk.rs | 4 ++-- src/plonk/circuit.rs | 6 +++--- src/plonk/keygen.rs | 2 +- src/plonk/prover.rs | 2 +- src/plonk/verifier.rs | 2 +- 8 files changed, 18 insertions(+), 13 deletions(-) diff --git a/benches/plonk.rs b/benches/plonk.rs index 6a6a1d4..3df2e85 100644 --- a/benches/plonk.rs +++ b/benches/plonk.rs @@ -189,7 +189,7 @@ fn bench_with_k(name: &str, k: u32, c: &mut Criterion) { let sb = meta.fixed_column(); let sc = meta.fixed_column(); - meta.create_gate(|meta| { + meta.create_gate("Combined add-mult", |meta| { let a = meta.query_advice(a, Rotation::cur()); let b = meta.query_advice(b, Rotation::cur()); let c = meta.query_advice(c, Rotation::cur()); diff --git a/examples/performance_model.rs b/examples/performance_model.rs index ed068c8..706546e 100644 --- a/examples/performance_model.rs +++ b/examples/performance_model.rs @@ -204,7 +204,7 @@ impl Circuit for MyCircuit { let sc = meta.fixed_column(); let sp = meta.fixed_column(); - meta.create_gate(|meta| { + meta.create_gate("Combined add-mult", |meta| { let a = meta.query_advice(a, Rotation::cur()); let b = meta.query_advice(b, Rotation::cur()); let c = meta.query_advice(c, Rotation::cur()); @@ -217,7 +217,7 @@ impl Circuit for MyCircuit { a.clone() * sa + b.clone() * sb + a * b * sm + (c * sc * (-F::one())) }); - meta.create_gate(|meta| { + meta.create_gate("Public input", |meta| { let a = meta.query_advice(a, Rotation::cur()); let p = meta.query_aux(p, Rotation::cur()); let sp = meta.query_fixed(sp, Rotation::cur()); diff --git a/src/dev.rs b/src/dev.rs index b28b594..30105bc 100644 --- a/src/dev.rs +++ b/src/dev.rs @@ -20,6 +20,9 @@ pub enum VerifyFailure { /// order in which `ConstraintSystem::create_gate` is called during /// `Circuit::configure`. gate_index: usize, + /// The name of the gate that is not satisfied. These are specified by the gate + /// creator (such as a chip implementation), and may not be unique. + gate_name: &'static str, /// The row on which this gate is not satisfied. row: usize, }, @@ -86,7 +89,7 @@ pub enum VerifyFailure { /// let b = meta.advice_column(); /// let c = meta.advice_column(); /// -/// meta.create_gate(|meta| { +/// meta.create_gate("R1CS constraint", |meta| { /// let a = meta.query_advice(a, Rotation::cur()); /// let b = meta.query_advice(b, Rotation::cur()); /// let c = meta.query_advice(c, Rotation::cur()); @@ -127,6 +130,7 @@ pub enum VerifyFailure { /// prover.verify(), /// Err(VerifyFailure::Gate { /// gate_index: 0, +/// gate_name: "R1CS constraint", /// row: 0 /// }) /// ); @@ -268,7 +272,7 @@ impl MockProver { let n = self.n as i32; // Check that all gates are satisfied for all rows. - for (gate_index, gate) in self.cs.gates.iter().enumerate() { + for (gate_index, (gate_name, gate)) in self.cs.gates.iter().enumerate() { // We iterate from n..2n so we can just reduce to handle wrapping. for row in n..(2 * n) { fn load<'a, F: FieldExt, T: ColumnType>( @@ -295,6 +299,7 @@ impl MockProver { { return Err(VerifyFailure::Gate { gate_index, + gate_name, row: (row - n) as usize, }); } diff --git a/src/plonk.rs b/src/plonk.rs index dfedc64..ed73597 100644 --- a/src/plonk.rs +++ b/src/plonk.rs @@ -426,7 +426,7 @@ fn test_proving() { meta.lookup(&[a.into()], &[sl.into()]); meta.lookup(&[a.into(), b.into()], &[sl.into(), sl2.into()]); - meta.create_gate(|meta| { + meta.create_gate("Combined add-mult", |meta| { let d = meta.query_advice(d, Rotation::next()); let a = meta.query_advice(a, Rotation::cur()); let sf = meta.query_fixed(sf, Rotation::cur()); @@ -442,7 +442,7 @@ fn test_proving() { a.clone() * sa + b.clone() * sb + a * b * sm + (c * sc * (-F::one())) + sf * (d * e) }); - meta.create_gate(|meta| { + meta.create_gate("Public input", |meta| { let a = meta.query_advice(a, Rotation::cur()); let p = meta.query_aux(p, Rotation::cur()); let sp = meta.query_fixed(sp, Rotation::cur()); diff --git a/src/plonk/circuit.rs b/src/plonk/circuit.rs index 876cc00..e02b15b 100644 --- a/src/plonk/circuit.rs +++ b/src/plonk/circuit.rs @@ -349,7 +349,7 @@ pub struct ConstraintSystem { pub(crate) num_fixed_columns: usize, pub(crate) num_advice_columns: usize, pub(crate) num_aux_columns: usize, - pub(crate) gates: Vec>, + pub(crate) gates: Vec<(&'static str, Expression)>, pub(crate) advice_queries: Vec<(Column, Rotation)>, pub(crate) aux_queries: Vec<(Column, Rotation)>, pub(crate) fixed_queries: Vec<(Column, Rotation)>, @@ -543,9 +543,9 @@ impl ConstraintSystem { } /// Create a new gate - pub fn create_gate(&mut self, f: impl FnOnce(&mut Self) -> Expression) { + pub fn create_gate(&mut self, name: &'static str, f: impl FnOnce(&mut Self) -> Expression) { let poly = f(self); - self.gates.push(poly); + self.gates.push((name, poly)); } /// Allocate a new fixed column diff --git a/src/plonk/keygen.rs b/src/plonk/keygen.rs index a949a5e..a206913 100644 --- a/src/plonk/keygen.rs +++ b/src/plonk/keygen.rs @@ -46,7 +46,7 @@ where // Account for each gate to ensure our quotient polynomial is the // correct degree and that our extended domain is the right size. - for poly in cs.gates.iter() { + for (_, poly) in cs.gates.iter() { degree = std::cmp::max(degree, poly.degree()); } diff --git a/src/plonk/prover.rs b/src/plonk/prover.rs index db3ccfb..8615890 100644 --- a/src/plonk/prover.rs +++ b/src/plonk/prover.rs @@ -350,7 +350,7 @@ pub fn create_proof, ConcreteCircuit: Circ |(((advice, aux), permutation_expressions), lookup_expressions)| { iter::empty() // Custom constraints - .chain(meta.gates.iter().map(move |poly| { + .chain(meta.gates.iter().map(move |(_, poly)| { poly.evaluate( &|index| pk.fixed_cosets[index].clone(), &|index| advice.advice_cosets[index].clone(), diff --git a/src/plonk/verifier.rs b/src/plonk/verifier.rs index 7525439..9e9238f 100644 --- a/src/plonk/verifier.rs +++ b/src/plonk/verifier.rs @@ -159,7 +159,7 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead>( std::iter::empty() // Evaluate the circuit using the custom gates provided - .chain(vk.cs.gates.iter().map(move |poly| { + .chain(vk.cs.gates.iter().map(move |(_, poly)| { poly.evaluate( &|index| fixed_evals[index], &|index| advice_evals[index],