2020-12-21 15:00:39 -08:00
|
|
|
//! Tools for developing circuits.
|
|
|
|
|
2021-05-27 06:03:12 -07:00
|
|
|
use std::collections::HashMap;
|
|
|
|
use std::convert::TryInto;
|
|
|
|
use std::iter;
|
|
|
|
|
2020-12-21 15:00:39 -08:00
|
|
|
use ff::Field;
|
|
|
|
|
|
|
|
use crate::{
|
|
|
|
arithmetic::{FieldExt, Group},
|
2021-01-22 08:57:38 -08:00
|
|
|
plonk::{
|
2021-02-18 23:07:08 -08:00
|
|
|
permutation, Advice, Any, Assignment, Circuit, Column, ColumnType, ConstraintSystem, Error,
|
2021-05-27 04:43:32 -07:00
|
|
|
Expression, Fixed, Permutation, Selector,
|
2021-01-22 08:57:38 -08:00
|
|
|
},
|
2021-01-07 04:42:04 -08:00
|
|
|
poly::Rotation,
|
2020-12-21 15:00:39 -08:00
|
|
|
};
|
|
|
|
|
2021-01-22 13:13:11 -08:00
|
|
|
#[cfg(feature = "dev-graph")]
|
|
|
|
mod graph;
|
|
|
|
|
|
|
|
#[cfg(feature = "dev-graph")]
|
|
|
|
#[cfg_attr(docsrs, doc(cfg(feature = "dev-graph")))]
|
2021-01-23 09:54:31 -08:00
|
|
|
pub use graph::{circuit_dot_graph, layout::circuit_layout};
|
2021-01-22 13:13:11 -08:00
|
|
|
|
2021-05-27 06:03:12 -07:00
|
|
|
/// Cells that haven't been explicitly assigned to, default to zero.
|
|
|
|
fn cell_value<F: Field>(cell: Option<F>) -> F {
|
|
|
|
cell.unwrap_or_else(F::zero)
|
|
|
|
}
|
|
|
|
|
2020-12-21 15:00:39 -08:00
|
|
|
/// The reasons why a particular circuit is not satisfied.
|
|
|
|
#[derive(Debug, PartialEq)]
|
|
|
|
pub enum VerifyFailure {
|
2021-05-27 06:02:13 -07:00
|
|
|
/// A cell used in an active gate was not assigned to.
|
|
|
|
Cell {
|
|
|
|
/// The column in which this cell is located.
|
|
|
|
column: Column<Any>,
|
|
|
|
/// The row in which this cell is located.
|
|
|
|
row: usize,
|
|
|
|
/// The index of the active gate. These indices are assigned in the order in which
|
|
|
|
/// `ConstraintSystem::create_gate` is called during `Circuit::configure`.
|
|
|
|
gate_index: usize,
|
|
|
|
/// The name of the active gate. These are specified by the gate creator (such as
|
|
|
|
/// a chip implementation), and may not be unique.
|
|
|
|
gate_name: &'static str,
|
|
|
|
},
|
2020-12-21 15:00:39 -08:00
|
|
|
/// A gate was not satisfied for a particular row.
|
2020-12-23 11:32:39 -08:00
|
|
|
Gate {
|
|
|
|
/// The index of the gate that is not satisfied. These indices are assigned in the
|
|
|
|
/// order in which `ConstraintSystem::create_gate` is called during
|
|
|
|
/// `Circuit::configure`.
|
|
|
|
gate_index: usize,
|
2021-01-22 11:46:06 -08:00
|
|
|
/// 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,
|
2020-12-23 11:32:39 -08:00
|
|
|
/// The row on which this gate is not satisfied.
|
|
|
|
row: usize,
|
|
|
|
},
|
2020-12-21 15:00:39 -08:00
|
|
|
/// A lookup input did not exist in its corresponding table.
|
2020-12-23 11:32:39 -08:00
|
|
|
Lookup {
|
|
|
|
/// 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`.
|
|
|
|
lookup_index: usize,
|
|
|
|
/// The row on which this lookup is not satisfied.
|
|
|
|
row: usize,
|
|
|
|
},
|
2020-12-21 21:56:30 -08:00
|
|
|
/// A permutation did not preserve the original value of a cell.
|
|
|
|
Permutation {
|
2020-12-23 11:32:39 -08:00
|
|
|
/// The index of the permutation that is not satisfied. These indices are assigned
|
|
|
|
/// in the order in which `ConstraintSystem::lookup` is called during
|
|
|
|
/// `Circuit::configure`.
|
2020-12-21 21:56:30 -08:00
|
|
|
perm_index: usize,
|
2020-12-23 11:32:39 -08:00
|
|
|
/// The column in which this permutation is not satisfied.
|
2020-12-21 21:56:30 -08:00
|
|
|
column: usize,
|
2020-12-23 11:32:39 -08:00
|
|
|
/// The row on which this permutation is not satisfied.
|
2020-12-21 21:56:30 -08:00
|
|
|
row: usize,
|
|
|
|
},
|
2020-12-21 15:00:39 -08:00
|
|
|
}
|
|
|
|
|
2020-12-23 11:32:39 -08:00
|
|
|
/// A test prover for debugging circuits.
|
|
|
|
///
|
|
|
|
/// The normal proving process, when applied to a buggy circuit implementation, might
|
|
|
|
/// return proofs that do not validate when they should, but it can't indicate anything
|
|
|
|
/// other than "something is invalid". `MockProver` can be used to figure out _why_ these
|
|
|
|
/// are invalid: it stores all the private inputs along with the circuit internals, and
|
|
|
|
/// then checks every constraint manually.
|
2020-12-23 11:33:31 -08:00
|
|
|
///
|
|
|
|
/// # Examples
|
|
|
|
///
|
|
|
|
/// ```
|
|
|
|
/// use halo2::{
|
|
|
|
/// arithmetic::FieldExt,
|
|
|
|
/// dev::{MockProver, VerifyFailure},
|
|
|
|
/// pasta::Fp,
|
|
|
|
/// plonk::{Advice, Assignment, Circuit, Column, ConstraintSystem, Error},
|
2021-01-14 12:31:48 -08:00
|
|
|
/// poly::Rotation,
|
2020-12-23 11:33:31 -08:00
|
|
|
/// };
|
|
|
|
/// const K: u32 = 5;
|
|
|
|
///
|
2021-01-20 05:48:51 -08:00
|
|
|
/// #[derive(Copy, Clone)]
|
2020-12-23 11:33:31 -08:00
|
|
|
/// struct MyConfig {
|
|
|
|
/// a: Column<Advice>,
|
|
|
|
/// b: Column<Advice>,
|
|
|
|
/// c: Column<Advice>,
|
|
|
|
/// }
|
|
|
|
///
|
2021-01-20 05:48:51 -08:00
|
|
|
/// #[derive(Clone)]
|
2020-12-23 11:33:31 -08:00
|
|
|
/// struct MyCircuit {
|
|
|
|
/// a: Option<u64>,
|
|
|
|
/// b: Option<u64>,
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// impl<F: FieldExt> Circuit<F> for MyCircuit {
|
|
|
|
/// type Config = MyConfig;
|
|
|
|
///
|
|
|
|
/// fn configure(meta: &mut ConstraintSystem<F>) -> MyConfig {
|
|
|
|
/// let a = meta.advice_column();
|
|
|
|
/// let b = meta.advice_column();
|
|
|
|
/// let c = meta.advice_column();
|
|
|
|
///
|
2021-01-22 11:46:06 -08:00
|
|
|
/// meta.create_gate("R1CS constraint", |meta| {
|
2021-01-14 12:31:48 -08:00
|
|
|
/// let a = meta.query_advice(a, Rotation::cur());
|
|
|
|
/// let b = meta.query_advice(b, Rotation::cur());
|
|
|
|
/// let c = meta.query_advice(c, Rotation::cur());
|
2020-12-23 11:33:31 -08:00
|
|
|
///
|
|
|
|
/// // BUG: Should be a * b - c
|
2021-05-27 06:44:02 -07:00
|
|
|
/// vec![a * b + c]
|
2020-12-23 11:33:31 -08:00
|
|
|
/// });
|
|
|
|
///
|
|
|
|
/// MyConfig { a, b, c }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// fn synthesize(&self, cs: &mut impl Assignment<F>, config: MyConfig) -> Result<(), Error> {
|
2021-01-22 08:57:38 -08:00
|
|
|
/// cs.assign_advice(|| "a", config.a, 0, || {
|
2020-12-23 11:33:31 -08:00
|
|
|
/// self.a.map(|v| F::from_u64(v)).ok_or(Error::SynthesisError)
|
|
|
|
/// })?;
|
2021-01-22 08:57:38 -08:00
|
|
|
/// cs.assign_advice(|| "b", config.b, 0, || {
|
2020-12-23 11:33:31 -08:00
|
|
|
/// self.b.map(|v| F::from_u64(v)).ok_or(Error::SynthesisError)
|
|
|
|
/// })?;
|
2021-01-22 08:57:38 -08:00
|
|
|
/// cs.assign_advice(|| "c", config.c, 0, || {
|
2020-12-23 11:33:31 -08:00
|
|
|
/// self.a
|
|
|
|
/// .and_then(|a| self.b.map(|b| F::from_u64(a * b)))
|
|
|
|
/// .ok_or(Error::SynthesisError)
|
|
|
|
/// })
|
|
|
|
/// }
|
|
|
|
/// }
|
|
|
|
///
|
|
|
|
/// // Assemble the private inputs to the circuit.
|
|
|
|
/// let circuit = MyCircuit {
|
|
|
|
/// a: Some(2),
|
|
|
|
/// b: Some(4),
|
|
|
|
/// };
|
|
|
|
///
|
|
|
|
/// // This circuit has no public inputs.
|
2021-02-14 09:30:36 -08:00
|
|
|
/// let instance = vec![];
|
2020-12-23 11:33:31 -08:00
|
|
|
///
|
2021-02-14 09:30:36 -08:00
|
|
|
/// let prover = MockProver::<Fp>::run(K, &circuit, instance).unwrap();
|
2020-12-23 11:33:31 -08:00
|
|
|
/// assert_eq!(
|
|
|
|
/// prover.verify(),
|
2021-05-18 08:32:15 -07:00
|
|
|
/// Err(vec![VerifyFailure::Gate {
|
2020-12-23 11:33:31 -08:00
|
|
|
/// gate_index: 0,
|
2021-01-22 11:46:06 -08:00
|
|
|
/// gate_name: "R1CS constraint",
|
2020-12-23 11:33:31 -08:00
|
|
|
/// row: 0
|
2021-05-18 08:32:15 -07:00
|
|
|
/// }])
|
2020-12-23 11:33:31 -08:00
|
|
|
/// );
|
|
|
|
/// ```
|
2020-12-23 11:32:39 -08:00
|
|
|
#[derive(Debug)]
|
2021-02-10 03:36:25 -08:00
|
|
|
pub struct MockProver<F: Group + Field> {
|
2020-12-21 15:00:39 -08:00
|
|
|
n: u32,
|
|
|
|
cs: ConstraintSystem<F>,
|
|
|
|
|
2021-05-27 06:03:12 -07:00
|
|
|
/// The selectors that have been enabled in the circuit. All other selectors are by
|
|
|
|
/// construction not enabled.
|
|
|
|
enabled_selectors: HashMap<Selector, Vec<usize>>,
|
|
|
|
|
2020-12-21 15:00:39 -08:00
|
|
|
// The fixed cells in the circuit, arranged as [column][row].
|
2021-05-27 06:03:12 -07:00
|
|
|
fixed: Vec<Vec<Option<F>>>,
|
2020-12-21 15:00:39 -08:00
|
|
|
// The advice cells in the circuit, arranged as [column][row].
|
2021-05-27 06:03:12 -07:00
|
|
|
advice: Vec<Vec<Option<F>>>,
|
2021-02-14 09:30:36 -08:00
|
|
|
// The instance cells in the circuit, arranged as [column][row].
|
|
|
|
instance: Vec<Vec<F>>,
|
2020-12-21 15:00:39 -08:00
|
|
|
|
2020-12-21 21:56:30 -08:00
|
|
|
permutations: Vec<permutation::keygen::Assembly>,
|
2020-12-21 15:00:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: Field + Group> Assignment<F> for MockProver<F> {
|
2021-01-22 08:43:36 -08:00
|
|
|
fn enter_region<NR, N>(&mut self, _: N)
|
|
|
|
where
|
|
|
|
NR: Into<String>,
|
|
|
|
N: FnOnce() -> NR,
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
fn exit_region(&mut self) {}
|
|
|
|
|
2021-05-27 04:43:32 -07:00
|
|
|
fn enable_selector<A, AR>(
|
|
|
|
&mut self,
|
|
|
|
annotation: A,
|
|
|
|
selector: &Selector,
|
|
|
|
row: usize,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
A: FnOnce() -> AR,
|
|
|
|
AR: Into<String>,
|
|
|
|
{
|
2021-05-27 06:03:12 -07:00
|
|
|
// Track that this selector was enabled.
|
|
|
|
self.enabled_selectors
|
|
|
|
.entry(*selector)
|
|
|
|
.or_default()
|
|
|
|
.push(row);
|
|
|
|
|
2021-05-27 04:43:32 -07:00
|
|
|
// Selectors are just fixed columns.
|
|
|
|
self.assign_fixed(annotation, selector.0, row, || Ok(F::one()))
|
|
|
|
}
|
|
|
|
|
2021-01-22 08:57:38 -08:00
|
|
|
fn assign_advice<V, A, AR>(
|
2020-12-21 15:00:39 -08:00
|
|
|
&mut self,
|
2021-01-22 08:57:38 -08:00
|
|
|
_: A,
|
|
|
|
column: Column<Advice>,
|
2020-12-21 15:00:39 -08:00
|
|
|
row: usize,
|
2021-01-22 08:57:38 -08:00
|
|
|
to: V,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
V: FnOnce() -> Result<F, Error>,
|
|
|
|
A: FnOnce() -> AR,
|
|
|
|
AR: Into<String>,
|
|
|
|
{
|
2020-12-21 15:00:39 -08:00
|
|
|
*self
|
|
|
|
.advice
|
|
|
|
.get_mut(column.index())
|
|
|
|
.and_then(|v| v.get_mut(row))
|
2021-05-27 06:03:12 -07:00
|
|
|
.ok_or(Error::BoundsFailure)? = Some(to()?);
|
2020-12-21 15:00:39 -08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
2021-01-22 08:57:38 -08:00
|
|
|
fn assign_fixed<V, A, AR>(
|
2020-12-21 15:00:39 -08:00
|
|
|
&mut self,
|
2021-01-22 08:57:38 -08:00
|
|
|
_: A,
|
|
|
|
column: Column<Fixed>,
|
2020-12-21 15:00:39 -08:00
|
|
|
row: usize,
|
2021-01-22 08:57:38 -08:00
|
|
|
to: V,
|
|
|
|
) -> Result<(), Error>
|
|
|
|
where
|
|
|
|
V: FnOnce() -> Result<F, Error>,
|
|
|
|
A: FnOnce() -> AR,
|
|
|
|
AR: Into<String>,
|
|
|
|
{
|
2020-12-21 15:00:39 -08:00
|
|
|
*self
|
|
|
|
.fixed
|
|
|
|
.get_mut(column.index())
|
|
|
|
.and_then(|v| v.get_mut(row))
|
2021-05-27 06:03:12 -07:00
|
|
|
.ok_or(Error::BoundsFailure)? = Some(to()?);
|
2020-12-21 15:00:39 -08:00
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn copy(
|
|
|
|
&mut self,
|
2021-02-18 22:53:26 -08:00
|
|
|
permutation: &Permutation,
|
2021-02-18 23:07:08 -08:00
|
|
|
left_column: Column<Any>,
|
2020-12-21 15:00:39 -08:00
|
|
|
left_row: usize,
|
2021-02-18 23:07:08 -08:00
|
|
|
right_column: Column<Any>,
|
2020-12-21 15:00:39 -08:00
|
|
|
right_row: usize,
|
|
|
|
) -> Result<(), crate::plonk::Error> {
|
2020-12-21 21:56:30 -08:00
|
|
|
// Check bounds first
|
2021-02-18 22:53:26 -08:00
|
|
|
if permutation.index() >= self.permutations.len() {
|
2020-12-21 21:56:30 -08:00
|
|
|
return Err(Error::BoundsFailure);
|
|
|
|
}
|
|
|
|
|
2021-02-18 23:07:08 -08:00
|
|
|
let left_column_index = permutation
|
|
|
|
.mapping()
|
|
|
|
.iter()
|
|
|
|
.position(|c| c == &left_column)
|
|
|
|
.ok_or(Error::SynthesisError)?;
|
|
|
|
let right_column_index = permutation
|
|
|
|
.mapping()
|
|
|
|
.iter()
|
|
|
|
.position(|c| c == &right_column)
|
|
|
|
.ok_or(Error::SynthesisError)?;
|
|
|
|
|
|
|
|
self.permutations[permutation.index()].copy(
|
|
|
|
left_column_index,
|
|
|
|
left_row,
|
|
|
|
right_column_index,
|
|
|
|
right_row,
|
|
|
|
)
|
2020-12-21 15:00:39 -08:00
|
|
|
}
|
2021-01-22 10:36:42 -08:00
|
|
|
|
|
|
|
fn push_namespace<NR, N>(&mut self, _: N)
|
|
|
|
where
|
|
|
|
NR: Into<String>,
|
|
|
|
N: FnOnce() -> NR,
|
|
|
|
{
|
|
|
|
// TODO: Do something with namespaces :)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn pop_namespace(&mut self, _: Option<String>) {
|
|
|
|
// TODO: Do something with namespaces :)
|
|
|
|
}
|
2020-12-21 15:00:39 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<F: FieldExt> MockProver<F> {
|
2020-12-23 11:32:39 -08:00
|
|
|
/// Runs a synthetic keygen-and-prove operation on the given circuit, collecting data
|
|
|
|
/// about the constraints and their assignments.
|
2020-12-21 15:00:39 -08:00
|
|
|
pub fn run<ConcreteCircuit: Circuit<F>>(
|
|
|
|
k: u32,
|
|
|
|
circuit: &ConcreteCircuit,
|
2021-02-14 09:30:36 -08:00
|
|
|
instance: Vec<Vec<F>>,
|
2020-12-21 15:00:39 -08:00
|
|
|
) -> Result<Self, Error> {
|
2020-12-21 21:56:30 -08:00
|
|
|
let n = 1 << k;
|
|
|
|
|
2020-12-21 15:00:39 -08:00
|
|
|
let mut cs = ConstraintSystem::default();
|
|
|
|
let config = ConcreteCircuit::configure(&mut cs);
|
|
|
|
|
2021-05-27 06:03:12 -07:00
|
|
|
let fixed = vec![vec![None; n as usize]; cs.num_fixed_columns];
|
|
|
|
let advice = vec![vec![None; n as usize]; cs.num_advice_columns];
|
2020-12-21 21:56:30 -08:00
|
|
|
let permutations = cs
|
|
|
|
.permutations
|
|
|
|
.iter()
|
|
|
|
.map(|p| permutation::keygen::Assembly::new(n as usize, p))
|
|
|
|
.collect();
|
2020-12-21 15:00:39 -08:00
|
|
|
|
|
|
|
let mut prover = MockProver {
|
2020-12-21 21:56:30 -08:00
|
|
|
n,
|
2020-12-21 15:00:39 -08:00
|
|
|
cs,
|
2021-05-27 06:03:12 -07:00
|
|
|
enabled_selectors: HashMap::default(),
|
2020-12-21 15:00:39 -08:00
|
|
|
fixed,
|
|
|
|
advice,
|
2021-02-14 09:30:36 -08:00
|
|
|
instance,
|
2020-12-21 21:56:30 -08:00
|
|
|
permutations,
|
2020-12-21 15:00:39 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
circuit.synthesize(&mut prover, config)?;
|
|
|
|
|
|
|
|
Ok(prover)
|
|
|
|
}
|
|
|
|
|
2021-05-18 08:32:15 -07:00
|
|
|
/// Returns `Ok(())` if this `MockProver` is satisfied, or a list of errors indicating
|
|
|
|
/// the reasons that the circuit is not satisfied.
|
|
|
|
pub fn verify(&self) -> Result<(), Vec<VerifyFailure>> {
|
2020-12-21 15:00:39 -08:00
|
|
|
let n = self.n as i32;
|
|
|
|
|
2021-05-27 06:03:12 -07:00
|
|
|
// Check that all cells used in instantiated gates have been assigned to.
|
|
|
|
let selector_errors = self
|
|
|
|
.cs
|
|
|
|
.gates
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.flat_map(|(gate_index, gate)| {
|
|
|
|
gate.queried_selectors()
|
|
|
|
.iter()
|
|
|
|
.map(|vc| (Selector(vc.column.try_into().unwrap()), vc.rotation))
|
|
|
|
// Assume that if a queried selector is enabled, the user wants to use the
|
|
|
|
// corresponding gate in some way.
|
|
|
|
//
|
|
|
|
// TODO: This will trip up on the reverse case, where leaving a selector
|
|
|
|
// un-enabled keeps a gate enabled. We could alternatively require that
|
|
|
|
// every selector is explicitly enabled or disabled on every row? But that
|
|
|
|
// seems messy and confusing.
|
|
|
|
.filter_map(|(s, rotation)| {
|
|
|
|
self.enabled_selectors.get(&s).map(|at| (at, rotation))
|
|
|
|
})
|
|
|
|
.flat_map(move |(at, rotation)| {
|
|
|
|
at.iter().flat_map(move |selector_row| {
|
|
|
|
// Determine the gate instance's logical row from the selector's
|
|
|
|
// concrete row and its rotation in the gate.
|
|
|
|
let gate_row = (*selector_row as i32 + n - rotation.0) % n;
|
|
|
|
|
|
|
|
gate.queried_cells().iter().filter_map(move |cell| {
|
|
|
|
// Determine where this cell should have been assigned.
|
|
|
|
let cell_row = ((gate_row + n + cell.rotation.0) % n) as usize;
|
|
|
|
|
|
|
|
// Check that it was assigned!
|
|
|
|
if match cell.column.column_type() {
|
|
|
|
Any::Advice => {
|
|
|
|
self.advice[cell.column.index()][cell_row].is_some()
|
|
|
|
}
|
|
|
|
Any::Fixed => {
|
|
|
|
self.fixed[cell.column.index()][cell_row].is_some()
|
|
|
|
}
|
|
|
|
Any::Instance => unreachable!(),
|
|
|
|
} {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(VerifyFailure::Cell {
|
|
|
|
column: cell.column,
|
|
|
|
row: cell_row,
|
|
|
|
gate_index,
|
|
|
|
gate_name: gate.name(),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2020-12-21 15:00:39 -08:00
|
|
|
// Check that all gates are satisfied for all rows.
|
2021-05-26 17:15:03 -07:00
|
|
|
let gate_errors = self
|
|
|
|
.cs
|
|
|
|
.gates
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.flat_map(|(gate_index, gate)| {
|
|
|
|
// We iterate from n..2n so we can just reduce to handle wrapping.
|
2021-05-27 06:44:02 -07:00
|
|
|
(n..(2 * n)).flat_map(move |row| {
|
2021-05-27 06:03:12 -07:00
|
|
|
fn load_opt<'a, F: FieldExt, T: ColumnType>(
|
|
|
|
n: i32,
|
|
|
|
row: i32,
|
|
|
|
queries: &'a [(Column<T>, Rotation)],
|
|
|
|
cells: &'a [Vec<Option<F>>],
|
|
|
|
) -> impl Fn(usize) -> F + 'a {
|
|
|
|
move |index| {
|
|
|
|
let (column, at) = &queries[index];
|
|
|
|
let resolved_row = (row + at.0) % n;
|
|
|
|
cell_value(cells[column.index()][resolved_row as usize])
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-26 17:15:03 -07:00
|
|
|
fn load<'a, F: FieldExt, T: ColumnType>(
|
|
|
|
n: i32,
|
|
|
|
row: i32,
|
|
|
|
queries: &'a [(Column<T>, Rotation)],
|
|
|
|
cells: &'a [Vec<F>],
|
|
|
|
) -> impl Fn(usize) -> F + 'a {
|
|
|
|
move |index| {
|
|
|
|
let (column, at) = &queries[index];
|
|
|
|
let resolved_row = (row + at.0) % n;
|
|
|
|
cells[column.index()][resolved_row as usize]
|
2021-05-18 08:32:15 -07:00
|
|
|
}
|
2021-05-26 17:15:03 -07:00
|
|
|
}
|
2021-05-18 08:32:15 -07:00
|
|
|
|
2021-05-27 06:44:02 -07:00
|
|
|
gate.polynomials().iter().filter_map(move |poly| {
|
|
|
|
if poly.evaluate(
|
|
|
|
&|scalar| scalar,
|
2021-05-27 06:03:12 -07:00
|
|
|
&load_opt(n, row, &self.cs.fixed_queries, &self.fixed),
|
|
|
|
&load_opt(n, row, &self.cs.advice_queries, &self.advice),
|
2021-05-27 06:44:02 -07:00
|
|
|
&load(n, row, &self.cs.instance_queries, &self.instance),
|
|
|
|
&|a, b| a + &b,
|
|
|
|
&|a, b| a * &b,
|
|
|
|
&|a, scalar| a * scalar,
|
|
|
|
) == F::zero()
|
|
|
|
{
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(VerifyFailure::Gate {
|
|
|
|
gate_index,
|
|
|
|
gate_name: gate.name(),
|
|
|
|
row: (row - n) as usize,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
2021-05-26 17:15:03 -07:00
|
|
|
})
|
|
|
|
});
|
2020-12-21 15:00:39 -08:00
|
|
|
|
|
|
|
// Check that all lookups exist in their respective tables.
|
2021-05-18 08:32:15 -07:00
|
|
|
let lookup_errors =
|
|
|
|
self.cs
|
|
|
|
.lookups
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.flat_map(|(lookup_index, lookup)| {
|
|
|
|
(0..n).filter_map(move |input_row| {
|
|
|
|
let load = |expression: &Expression<F>, row| {
|
|
|
|
expression.evaluate(
|
|
|
|
&|scalar| scalar,
|
|
|
|
&|index| {
|
|
|
|
let query = self.cs.fixed_queries[index];
|
|
|
|
let column_index = query.0.index();
|
|
|
|
let rotation = query.1 .0;
|
2021-05-27 06:03:12 -07:00
|
|
|
cell_value(
|
|
|
|
self.fixed[column_index]
|
|
|
|
[(row as i32 + n + rotation) as usize % n as usize],
|
|
|
|
)
|
2021-05-18 08:32:15 -07:00
|
|
|
},
|
|
|
|
&|index| {
|
|
|
|
let query = self.cs.advice_queries[index];
|
|
|
|
let column_index = query.0.index();
|
|
|
|
let rotation = query.1 .0;
|
2021-05-27 06:03:12 -07:00
|
|
|
cell_value(
|
|
|
|
self.advice[column_index]
|
|
|
|
[(row as i32 + n + rotation) as usize % n as usize],
|
|
|
|
)
|
2021-05-18 08:32:15 -07:00
|
|
|
},
|
|
|
|
&|index| {
|
|
|
|
let query = self.cs.instance_queries[index];
|
|
|
|
let column_index = query.0.index();
|
|
|
|
let rotation = query.1 .0;
|
|
|
|
self.instance[column_index]
|
|
|
|
[(row as i32 + n + rotation) as usize % n as usize]
|
|
|
|
},
|
|
|
|
&|a, b| a + b,
|
|
|
|
&|a, b| a * b,
|
|
|
|
&|a, scalar| a * scalar,
|
|
|
|
)
|
|
|
|
};
|
|
|
|
|
|
|
|
let inputs: Vec<_> = lookup
|
|
|
|
.input_expressions
|
2021-02-11 18:24:55 -08:00
|
|
|
.iter()
|
2021-05-18 08:32:15 -07:00
|
|
|
.map(|c| load(c, input_row))
|
|
|
|
.collect();
|
|
|
|
let lookup_passes = (0..n)
|
|
|
|
.map(|table_row| {
|
|
|
|
lookup
|
|
|
|
.table_expressions
|
|
|
|
.iter()
|
|
|
|
.map(move |c| load(c, table_row))
|
|
|
|
})
|
|
|
|
.any(|table_row| table_row.eq(inputs.iter().cloned()));
|
|
|
|
if lookup_passes {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(VerifyFailure::Lookup {
|
|
|
|
lookup_index,
|
|
|
|
row: input_row as usize,
|
|
|
|
})
|
|
|
|
}
|
2021-02-11 18:24:55 -08:00
|
|
|
})
|
2021-05-18 08:32:15 -07:00
|
|
|
});
|
2020-12-21 15:00:39 -08:00
|
|
|
|
2020-12-21 21:56:30 -08:00
|
|
|
// Check that permutations preserve the original values of the cells.
|
2021-05-18 08:32:15 -07:00
|
|
|
let perm_errors =
|
|
|
|
self.permutations
|
2020-12-21 21:56:30 -08:00
|
|
|
.iter()
|
2021-05-18 08:32:15 -07:00
|
|
|
.enumerate()
|
|
|
|
.flat_map(|(perm_index, assembly)| {
|
|
|
|
// Original values of columns involved in the permutation
|
|
|
|
let original = |perm_index: usize, column, row| {
|
|
|
|
self.cs.permutations[perm_index]
|
|
|
|
.get_columns()
|
2021-05-27 06:03:12 -07:00
|
|
|
.get(column)
|
|
|
|
.map(|c: &Column<Any>| match c.column_type() {
|
|
|
|
Any::Advice => cell_value(self.advice[c.index()][row]),
|
|
|
|
Any::Fixed => cell_value(self.fixed[c.index()][row]),
|
|
|
|
Any::Instance => self.instance[c.index()][row],
|
2021-05-18 08:32:15 -07:00
|
|
|
})
|
2021-05-27 06:03:12 -07:00
|
|
|
.unwrap()
|
2021-05-18 08:32:15 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
// Iterate over each column of the permutation
|
|
|
|
assembly
|
|
|
|
.mapping
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.flat_map(move |(column, values)| {
|
|
|
|
// Iterate over each row of the column to check that the cell's
|
|
|
|
// value is preserved by the mapping.
|
|
|
|
values.iter().enumerate().filter_map(move |(row, cell)| {
|
|
|
|
let original_cell = original(perm_index, column, row);
|
|
|
|
let permuted_cell = original(perm_index, cell.0, cell.1);
|
|
|
|
if original_cell == permuted_cell {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(VerifyFailure::Permutation {
|
|
|
|
perm_index,
|
|
|
|
column,
|
|
|
|
row,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
})
|
|
|
|
})
|
|
|
|
});
|
|
|
|
|
2021-05-27 06:03:12 -07:00
|
|
|
let errors: Vec<_> = iter::empty()
|
|
|
|
.chain(selector_errors)
|
|
|
|
.chain(gate_errors)
|
2021-05-18 08:32:15 -07:00
|
|
|
.chain(lookup_errors)
|
|
|
|
.chain(perm_errors)
|
|
|
|
.collect();
|
|
|
|
if errors.is_empty() {
|
|
|
|
Ok(())
|
|
|
|
} else {
|
|
|
|
Err(errors)
|
2020-12-21 21:56:30 -08:00
|
|
|
}
|
2020-12-21 15:00:39 -08:00
|
|
|
}
|
|
|
|
}
|
2021-05-27 06:02:13 -07:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use pasta_curves::Fp;
|
|
|
|
|
|
|
|
use super::{MockProver, VerifyFailure};
|
|
|
|
use crate::{
|
|
|
|
circuit::{layouter::SingleChipLayouter, Layouter},
|
|
|
|
plonk::{Advice, Any, Assignment, Circuit, Column, ConstraintSystem, Error, Selector},
|
|
|
|
poly::Rotation,
|
|
|
|
};
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn unassigned_cell() {
|
|
|
|
const K: u32 = 4;
|
|
|
|
const FAULTY_ROW: usize = 2;
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
|
|
|
struct FaultyCircuitConfig {
|
|
|
|
a: Column<Advice>,
|
|
|
|
q: Selector,
|
|
|
|
}
|
|
|
|
|
|
|
|
struct FaultyCircuit {}
|
|
|
|
|
|
|
|
impl Circuit<Fp> for FaultyCircuit {
|
|
|
|
type Config = FaultyCircuitConfig;
|
|
|
|
|
|
|
|
fn configure(meta: &mut ConstraintSystem<Fp>) -> Self::Config {
|
|
|
|
let a = meta.advice_column();
|
|
|
|
let b = meta.advice_column();
|
|
|
|
let q = meta.selector();
|
|
|
|
|
|
|
|
meta.create_gate("Equality check", |cells| {
|
|
|
|
let a = cells.query_advice(a, Rotation::prev());
|
|
|
|
let b = cells.query_advice(b, Rotation::cur());
|
|
|
|
let q = cells.query_selector(q, Rotation::cur());
|
|
|
|
|
|
|
|
// If q is enabled, a and b must be assigned to.
|
|
|
|
vec![q * (a - b)]
|
|
|
|
});
|
|
|
|
|
|
|
|
FaultyCircuitConfig { a, q }
|
|
|
|
}
|
|
|
|
|
|
|
|
fn synthesize(
|
|
|
|
&self,
|
|
|
|
cs: &mut impl Assignment<Fp>,
|
|
|
|
config: Self::Config,
|
|
|
|
) -> Result<(), Error> {
|
|
|
|
let mut layouter = SingleChipLayouter::new(cs)?;
|
|
|
|
layouter.assign_region(
|
|
|
|
|| "Faulty synthesis",
|
|
|
|
|mut region| {
|
|
|
|
// Enable the equality gate.
|
|
|
|
config.q.enable(&mut region, FAULTY_ROW)?;
|
|
|
|
|
|
|
|
// Assign a = 0.
|
|
|
|
region.assign_advice(
|
|
|
|
|| "a",
|
|
|
|
config.a,
|
|
|
|
FAULTY_ROW - 1,
|
|
|
|
|| Ok(Fp::zero()),
|
|
|
|
)?;
|
|
|
|
|
|
|
|
// BUG: Forget to assign b = 0! This could go unnoticed during
|
|
|
|
// development, because cell values default to zero, which in this
|
|
|
|
// case is fine, but for other assignments would be broken.
|
|
|
|
Ok(())
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
let prover = MockProver::run(K, &FaultyCircuit {}, vec![]).unwrap();
|
|
|
|
assert_eq!(
|
|
|
|
prover.verify(),
|
|
|
|
Err(vec![VerifyFailure::Cell {
|
|
|
|
column: Column::new(1, Any::Advice),
|
|
|
|
row: FAULTY_ROW,
|
|
|
|
gate_index: 0,
|
|
|
|
gate_name: "Equality check"
|
|
|
|
}])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|