mirror of https://github.com/zcash/halo2.git
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.
This commit is contained in:
parent
bf771a7446
commit
82da677add
|
@ -189,7 +189,7 @@ fn bench_with_k(name: &str, k: u32, c: &mut Criterion) {
|
||||||
let sb = meta.fixed_column();
|
let sb = meta.fixed_column();
|
||||||
let sc = 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 a = meta.query_advice(a, Rotation::cur());
|
||||||
let b = meta.query_advice(b, Rotation::cur());
|
let b = meta.query_advice(b, Rotation::cur());
|
||||||
let c = meta.query_advice(c, Rotation::cur());
|
let c = meta.query_advice(c, Rotation::cur());
|
||||||
|
|
|
@ -204,7 +204,7 @@ impl<F: FieldExt> Circuit<F> for MyCircuit<F> {
|
||||||
let sc = meta.fixed_column();
|
let sc = meta.fixed_column();
|
||||||
let sp = 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 a = meta.query_advice(a, Rotation::cur());
|
||||||
let b = meta.query_advice(b, Rotation::cur());
|
let b = meta.query_advice(b, Rotation::cur());
|
||||||
let c = meta.query_advice(c, Rotation::cur());
|
let c = meta.query_advice(c, Rotation::cur());
|
||||||
|
@ -217,7 +217,7 @@ impl<F: FieldExt> Circuit<F> for MyCircuit<F> {
|
||||||
a.clone() * sa + b.clone() * sb + a * b * sm + (c * sc * (-F::one()))
|
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 a = meta.query_advice(a, Rotation::cur());
|
||||||
let p = meta.query_aux(p, Rotation::cur());
|
let p = meta.query_aux(p, Rotation::cur());
|
||||||
let sp = meta.query_fixed(sp, Rotation::cur());
|
let sp = meta.query_fixed(sp, Rotation::cur());
|
||||||
|
|
|
@ -20,6 +20,9 @@ pub enum VerifyFailure {
|
||||||
/// order in which `ConstraintSystem::create_gate` is called during
|
/// order in which `ConstraintSystem::create_gate` is called during
|
||||||
/// `Circuit::configure`.
|
/// `Circuit::configure`.
|
||||||
gate_index: usize,
|
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.
|
/// The row on which this gate is not satisfied.
|
||||||
row: usize,
|
row: usize,
|
||||||
},
|
},
|
||||||
|
@ -86,7 +89,7 @@ pub enum VerifyFailure {
|
||||||
/// let b = meta.advice_column();
|
/// let b = meta.advice_column();
|
||||||
/// let c = 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 a = meta.query_advice(a, Rotation::cur());
|
||||||
/// let b = meta.query_advice(b, Rotation::cur());
|
/// let b = meta.query_advice(b, Rotation::cur());
|
||||||
/// let c = meta.query_advice(c, Rotation::cur());
|
/// let c = meta.query_advice(c, Rotation::cur());
|
||||||
|
@ -127,6 +130,7 @@ pub enum VerifyFailure {
|
||||||
/// prover.verify(),
|
/// prover.verify(),
|
||||||
/// Err(VerifyFailure::Gate {
|
/// Err(VerifyFailure::Gate {
|
||||||
/// gate_index: 0,
|
/// gate_index: 0,
|
||||||
|
/// gate_name: "R1CS constraint",
|
||||||
/// row: 0
|
/// row: 0
|
||||||
/// })
|
/// })
|
||||||
/// );
|
/// );
|
||||||
|
@ -268,7 +272,7 @@ impl<F: FieldExt> MockProver<F> {
|
||||||
let n = self.n as i32;
|
let n = self.n as i32;
|
||||||
|
|
||||||
// Check that all gates are satisfied for all rows.
|
// 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.
|
// We iterate from n..2n so we can just reduce to handle wrapping.
|
||||||
for row in n..(2 * n) {
|
for row in n..(2 * n) {
|
||||||
fn load<'a, F: FieldExt, T: ColumnType>(
|
fn load<'a, F: FieldExt, T: ColumnType>(
|
||||||
|
@ -295,6 +299,7 @@ impl<F: FieldExt> MockProver<F> {
|
||||||
{
|
{
|
||||||
return Err(VerifyFailure::Gate {
|
return Err(VerifyFailure::Gate {
|
||||||
gate_index,
|
gate_index,
|
||||||
|
gate_name,
|
||||||
row: (row - n) as usize,
|
row: (row - n) as usize,
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -426,7 +426,7 @@ fn test_proving() {
|
||||||
meta.lookup(&[a.into()], &[sl.into()]);
|
meta.lookup(&[a.into()], &[sl.into()]);
|
||||||
meta.lookup(&[a.into(), b.into()], &[sl.into(), sl2.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 d = meta.query_advice(d, Rotation::next());
|
||||||
let a = meta.query_advice(a, Rotation::cur());
|
let a = meta.query_advice(a, Rotation::cur());
|
||||||
let sf = meta.query_fixed(sf, 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)
|
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 a = meta.query_advice(a, Rotation::cur());
|
||||||
let p = meta.query_aux(p, Rotation::cur());
|
let p = meta.query_aux(p, Rotation::cur());
|
||||||
let sp = meta.query_fixed(sp, Rotation::cur());
|
let sp = meta.query_fixed(sp, Rotation::cur());
|
||||||
|
|
|
@ -349,7 +349,7 @@ pub struct ConstraintSystem<F> {
|
||||||
pub(crate) num_fixed_columns: usize,
|
pub(crate) num_fixed_columns: usize,
|
||||||
pub(crate) num_advice_columns: usize,
|
pub(crate) num_advice_columns: usize,
|
||||||
pub(crate) num_aux_columns: usize,
|
pub(crate) num_aux_columns: usize,
|
||||||
pub(crate) gates: Vec<Expression<F>>,
|
pub(crate) gates: Vec<(&'static str, Expression<F>)>,
|
||||||
pub(crate) advice_queries: Vec<(Column<Advice>, Rotation)>,
|
pub(crate) advice_queries: Vec<(Column<Advice>, Rotation)>,
|
||||||
pub(crate) aux_queries: Vec<(Column<Aux>, Rotation)>,
|
pub(crate) aux_queries: Vec<(Column<Aux>, Rotation)>,
|
||||||
pub(crate) fixed_queries: Vec<(Column<Fixed>, Rotation)>,
|
pub(crate) fixed_queries: Vec<(Column<Fixed>, Rotation)>,
|
||||||
|
@ -543,9 +543,9 @@ impl<F: Field> ConstraintSystem<F> {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Create a new gate
|
/// Create a new gate
|
||||||
pub fn create_gate(&mut self, f: impl FnOnce(&mut Self) -> Expression<F>) {
|
pub fn create_gate(&mut self, name: &'static str, f: impl FnOnce(&mut Self) -> Expression<F>) {
|
||||||
let poly = f(self);
|
let poly = f(self);
|
||||||
self.gates.push(poly);
|
self.gates.push((name, poly));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Allocate a new fixed column
|
/// Allocate a new fixed column
|
||||||
|
|
|
@ -46,7 +46,7 @@ where
|
||||||
|
|
||||||
// Account for each gate to ensure our quotient polynomial is the
|
// Account for each gate to ensure our quotient polynomial is the
|
||||||
// correct degree and that our extended domain is the right size.
|
// 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());
|
degree = std::cmp::max(degree, poly.degree());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -350,7 +350,7 @@ pub fn create_proof<C: CurveAffine, T: TranscriptWrite<C>, ConcreteCircuit: Circ
|
||||||
|(((advice, aux), permutation_expressions), lookup_expressions)| {
|
|(((advice, aux), permutation_expressions), lookup_expressions)| {
|
||||||
iter::empty()
|
iter::empty()
|
||||||
// Custom constraints
|
// Custom constraints
|
||||||
.chain(meta.gates.iter().map(move |poly| {
|
.chain(meta.gates.iter().map(move |(_, poly)| {
|
||||||
poly.evaluate(
|
poly.evaluate(
|
||||||
&|index| pk.fixed_cosets[index].clone(),
|
&|index| pk.fixed_cosets[index].clone(),
|
||||||
&|index| advice.advice_cosets[index].clone(),
|
&|index| advice.advice_cosets[index].clone(),
|
||||||
|
|
|
@ -159,7 +159,7 @@ pub fn verify_proof<'a, C: CurveAffine, T: TranscriptRead<C>>(
|
||||||
|
|
||||||
std::iter::empty()
|
std::iter::empty()
|
||||||
// Evaluate the circuit using the custom gates provided
|
// 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(
|
poly.evaluate(
|
||||||
&|index| fixed_evals[index],
|
&|index| fixed_evals[index],
|
||||||
&|index| advice_evals[index],
|
&|index| advice_evals[index],
|
||||||
|
|
Loading…
Reference in New Issue