switch gate (in progress)

This commit is contained in:
Nicholas Ward 2021-08-18 17:46:38 -07:00
parent d497c10858
commit 94a0ad7846
3 changed files with 364 additions and 7 deletions

View File

@ -14,6 +14,7 @@ pub(crate) mod noop;
pub(crate) mod public_input;
pub mod random_access;
pub mod reducing;
pub mod switch;
#[cfg(test)]
mod gate_testing;

View File

@ -27,7 +27,7 @@ impl<F: Extendable<D>, const D: usize> RandomAccessGate<F, D> {
}
}
pub fn wires_access_index(&self) -> usize {
pub fn wire_access_index(&self) -> usize {
0
}
@ -67,7 +67,7 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGate<F, D> {
}
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
let access_index = vars.local_wires[self.wires_access_index()];
let access_index = vars.local_wires[self.wire_access_index()];
let list_items = (0..self.vec_size)
.map(|i| vars.get_local_ext_algebra(self.wires_list_item(i)))
.collect::<Vec<_>>();
@ -93,7 +93,7 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGate<F, D> {
}
fn eval_unfiltered_base(&self, vars: EvaluationVarsBase<F>) -> Vec<F> {
let access_index = vars.local_wires[self.wires_access_index()];
let access_index = vars.local_wires[self.wire_access_index()];
let list_items = (0..self.vec_size)
.map(|i| vars.get_local_ext(self.wires_list_item(i)))
.collect::<Vec<_>>();
@ -124,7 +124,7 @@ impl<F: Extendable<D>, const D: usize> Gate<F, D> for RandomAccessGate<F, D> {
builder: &mut CircuitBuilder<F, D>,
vars: EvaluationTargets<D>,
) -> Vec<ExtensionTarget<D>> {
let access_index = vars.local_wires[self.wires_access_index()];
let access_index = vars.local_wires[self.wire_access_index()];
let list_items = (0..self.vec_size)
.map(|i| vars.get_local_ext_algebra(self.wires_list_item(i)))
.collect::<Vec<_>>();
@ -200,7 +200,7 @@ impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for RandomAccessGenera
let local_targets = |inputs: Range<usize>| inputs.map(local_target);
let mut deps = Vec::new();
deps.push(local_target(self.gate.wires_access_index()));
deps.push(local_target(self.gate.wire_access_index()));
deps.extend(local_targets(self.gate.wires_claimed_element()));
for i in 0..self.gate.vec_size {
deps.extend(local_targets(self.gate.wires_list_item(i)));
@ -218,7 +218,7 @@ impl<F: Extendable<D>, const D: usize> SimpleGenerator<F> for RandomAccessGenera
// Compute the new vector and the values for equality_dummy and index_matches
let vec_size = self.gate.vec_size;
let access_index_f = get_local_wire(self.gate.wires_access_index());
let access_index_f = get_local_wire(self.gate.wire_access_index());
let access_index = access_index_f.to_canonical_u64() as usize;
debug_assert!(
@ -268,7 +268,7 @@ mod tests {
_phantom: PhantomData,
};
assert_eq!(gate.wires_access_index(), 0);
assert_eq!(gate.wire_access_index(), 0);
assert_eq!(gate.wires_claimed_element(), 1..5);
assert_eq!(gate.wires_list_item(0), 5..9);
assert_eq!(gate.wires_list_item(2), 13..17);

356
src/gates/switch.rs Normal file
View File

@ -0,0 +1,356 @@
use std::marker::PhantomData;
use std::ops::Range;
use crate::field::extension_field::target::ExtensionTarget;
use crate::field::extension_field::{Extendable, FieldExtension};
use crate::field::field_types::Field;
use crate::gates::gate::Gate;
use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator};
use crate::iop::target::Target;
use crate::iop::wire::Wire;
use crate::iop::witness::PartialWitness;
use crate::plonk::circuit_builder::CircuitBuilder;
use crate::plonk::circuit_data::CircuitConfig;
use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase};
/// A gate for checking that a particular value in a list matches a given
#[derive(Clone, Debug)]
pub(crate) struct SwitchGate<F: Extendable<D>, const D: usize, const CHUNK_SIZE: usize> {
num_copies: usize,
_phantom: PhantomData<F>,
}
impl<F: Extendable<D>, const D: usize, const CHUNK_SIZE: usize> SwitchGate<F, D, CHUNK_SIZE> {
pub fn new(config: CircuitConfig) -> Self {
let num_copies = Self::max_num_chunks(config.num_wires, config.num_routed_wires);
Self {
num_copies,
_phantom: PhantomData,
}
}
pub fn wire_switch_bool(&self, copy: usize) -> usize {
debug_assert!(copy < self.num_copies);
copy * (4 * CHUNK_SIZE + 1)
}
pub fn wire_first_input(&self, copy: usize, element: usize) {
debug_assert!(copy < self.num_copies);
debug_assert!(element < CHUNK_SIZE);
copy * (4 * CHUNK_SIZE + 1) + 1 + element
}
pub fn wire_second_input(&self, copy: usize, element: usize) {
debug_assert!(copy < self.num_copies);
debug_assert!(element < CHUNK_SIZE);
copy * (4 * CHUNK_SIZE + 1) + 1 + CHUNK_SIZE + element
}
pub fn wire_first_output(&self, copy: usize, element: usize) {
debug_assert!(copy < self.num_copies);
debug_assert!(element < CHUNK_SIZE);
copy * (4 * CHUNK_SIZE + 1) + 1 + 2 * CHUNK_SIZE + element
}
pub fn wire_second_output(&self, copy: usize, element: usize) {
debug_assert!(copy < self.num_copies);
debug_assert!(element < CHUNK_SIZE);
copy * (4 * CHUNK_SIZE + 1) + 1 + 3 * CHUNK_SIZE + element
}
}
impl<F: Extendable<D>, const D: usize, const CHUNK_SIZE: usize> Gate<F, D> for SwitchGate<F, D, CHUNK_SIZE> {
fn id(&self) -> String {
format!("{:?}<D={}>", self, D)
}
fn eval_unfiltered(&self, vars: EvaluationVars<F, D>) -> Vec<F::Extension> {
let mut constraints = Vec::with_capacity(self.num_constraints());
for c in 0..self.num_copies {
let switch_bool = vars.local_wires[self.wire_switch_bool(c)];
for e in 0..CHUNK_SIZE {
let first_input = vars.local_wires[self.wire_first_input(c, e)];
let second_input = vars.local_wires[self.wire_second_input(c, e)];
let first_output = vars.local_wires[self.wire_first_output(c, e)];
let second_output = vars.local_wires[self.wire_second_output(c, e)];
constraints.push(switch_bool * (first_input - second_output));
constraints.push(switch_bool * (second_input - first_output));
constraints.push((F::Extension::ONE - switch_bool) * (first_input - first_output));
constraints.push((F::Extension::ONE - switch_bool) * (second_input - second_output));
}
}
constraints
}
fn eval_unfiltered_base(&self, vars: EvaluationVarsBase<F, D>) -> Vec<F> {
let mut constraints = Vec::with_capacity(self.num_constraints());
for c in 0..self.num_copies {
let switch_bool = vars.local_wires[self.wire_switch_bool(c)];
for e in 0..CHUNK_SIZE {
let first_input = vars.local_wires[self.wire_first_input(c, e)];
let second_input = vars.local_wires[self.wire_second_input(c, e)];
let first_output = vars.local_wires[self.wire_first_output(c, e)];
let second_output = vars.local_wires[self.wire_second_output(c, e)];
constraints.push(switch_bool * (first_input - second_output));
constraints.push(switch_bool * (second_input - first_output));
constraints.push((F::ONE - switch_bool) * (first_input - first_output));
constraints.push((F::ONE - switch_bool) * (second_input - second_output));
}
}
constraints
}
fn eval_unfiltered_recursively(
&self,
builder: &mut CircuitBuilder<F, D>,
vars: EvaluationTargets<D>,
) -> Vec<ExtensionTarget<D>> {
let mut constraints = Vec::with_capacity(self.num_constraints());
let one = builder.one_extension();
for c in 0..self.num_copies {
let switch_bool = vars.local_wires[self.wire_switch_bool(c)];
let not_switch = builder.sub_extension(one, switch_bool);
for e in 0..CHUNK_SIZE {
let first_input = vars.local_wires[self.wire_first_input(c, e)];
let second_input = vars.local_wires[self.wire_second_input(c, e)];
let first_output = vars.local_wires[self.wire_first_output(c, e)];
let second_output = vars.local_wires[self.wire_second_output(c, e)];
let first_switched = builder.sub_extension(first_input, second_output);
let first_switched_constraint = builder.mul_extension(switch_bool, first_switched);
constraints.push(first_switched_constraint);
let second_switched = builder.sub_extension(second_input, first_output);
let second_switched_constraint = builder.mul_extension(switch_bool, second_switched);
constraints.push(second_switched_constraint);
let first_not_switched = builder.sub_extension(first_input, first_output);
let first_not_switched_constraint = builder.mul_extension(not_switch, first_not_switched);
constraints.push(first_not_switched_constraint);
let second_not_switched = builder.sub_extension(second_input, second_output);
let second_not_switched_constraint = builder.mul_extension(not_switch, second_not_switched);
constraints.push(second_not_switched_constraint);
}
}
constraints
}
fn generators(
&self,
gate_index: usize,
_local_constants: &[F],
) -> Vec<Box<dyn WitnessGenerator<F>>> {
let gen = SwitchGenerator::<F, D> {
gate_index,
gate: self.clone(),
};
vec![Box::new(gen)]
}
fn num_wires(&self) -> usize {
self.wire_second_output(self.num_copies - 1, CHUNK_SIZE - 1) + 1
}
fn num_constants(&self) -> usize {
0
}
fn degree(&self) -> usize {
2
}
fn num_constraints(&self) -> usize {
4 * self.num_copies * CHUNK_SIZE
}
}
#[derive(Debug)]
struct SwitchGenerator<F: Extendable<D>, const D: usize, const CHUNK_SIZE: usize> {
gate_index: usize,
gate: SwitchGate<F, D, CHUNK_SIZE>,
}
impl<F: Extendable<D>, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator<F> for SwitchGenerator<F, D, CHUNK_SIZE> {
fn dependencies(&self) -> Vec<Target> {
let local_target = |input| Target::wire(self.gate_index, input);
let local_targets = |inputs: Range<usize>| inputs.map(local_target);
let mut deps = Vec::new();
for c in 0..self.gate.num_copies {
deps.push(local_target(self.gate.wire_switch_bool(c)));
for e in 0..CHUNK_SIZE {
deps.push(local_target(self.gate.wire_first_input(c, e)));
deps.push(local_target(self.gate.wire_second_input(c, e)));
}
}
deps
}
fn run_once(&self, witness: &PartialWitness<F>, out_buffer: &mut GeneratedValues<F>) {
let local_wire = |input| Wire {
gate: self.gate_index,
input,
};
let get_local_wire = |input| witness.get_wire(local_wire(input));
// Compute the new vector and the values for equality_dummy and index_matches
let vec_size = self.gate.vec_size;
let access_index_f = get_local_wire(self.gate.wires_access_index());
let access_index = access_index_f.to_canonical_u64() as usize;
debug_assert!(
access_index < vec_size,
"Access index {} is larger than the vector size {}",
access_index,
vec_size
);
for i in 0..vec_size {
let equality_dummy_wire = local_wire(self.gate.wire_equality_dummy_for_index(i));
let index_matches_wire = local_wire(self.gate.wire_index_matches_for_index(i));
if i == access_index {
out_buffer.set_wire(equality_dummy_wire, F::ONE);
out_buffer.set_wire(index_matches_wire, F::ONE);
} else {
out_buffer.set_wire(
equality_dummy_wire,
(F::from_canonical_usize(i) - F::from_canonical_usize(access_index)).inverse(),
);
out_buffer.set_wire(index_matches_wire, F::ZERO);
}
}
}
}
#[cfg(test)]
mod tests {
use std::marker::PhantomData;
use anyhow::Result;
use crate::field::crandall_field::CrandallField;
use crate::field::extension_field::quartic::QuarticCrandallField;
use crate::field::field_types::Field;
use crate::gates::gate::Gate;
use crate::gates::gate_testing::{test_eval_fns, test_low_degree};
use crate::gates::switch::SwitchGate;
use crate::hash::hash_types::HashOut;
use crate::plonk::vars::EvaluationVars;
#[test]
fn wire_indices() {
let gate = SwitchGate::<CrandallField, 4, 3> {
num_copies: 3,
_phantom: PhantomData,
};
assert_eq!(gate.wire_switch_bool(0), 0);
assert_eq!(gate.wire_first_input(0, 0), 1);
assert_eq!(gate.wire_first_input(0, 2), 3);
assert_eq!(gate.wire_second_input(0, 0), 4);
assert_eq!(gate.wire_second_input(0, 2), 6);
assert_eq!(gate.wire_first_output(0, 0), 7);
assert_eq!(gate.wire_second_output(0, 2), 12);
assert_eq!(gate.wire_first_input(1, 0), 13);
assert_eq!(gate.wire_second_output(1, 2), 24);
assert_eq!(gate.wire_first_input(2, 0), 25);
assert_eq!(gate.wire_second_output(2, 2), 36);
}
#[test]
fn low_degree() {
test_low_degree::<CrandallField, _, 4>(SwitchGate::new(CircuitConfig::large_config()));
}
#[test]
fn eval_fns() -> Result<()> {
test_eval_fns::<CrandallField, _, 4>(SwitchGate::new(CircuitConfig::large_config()))
}
#[test]
fn test_gate_constraint() {
type F = CrandallField;
type FF = QuarticCrandallField;
const D: usize = 4;
/// Returns the local wires for a random access gate given the vector, element to compare,
/// and index.
fn get_wires(list: Vec<FF>, access_index: usize, claimed_element: FF) -> Vec<FF> {
let vec_size = list.len();
let mut v = Vec::new();
v.push(F::from_canonical_usize(access_index));
v.extend(claimed_element.0);
for j in 0..vec_size {
v.extend(list[j].0);
}
let mut equality_dummy_vals = Vec::new();
let mut index_matches_vals = Vec::new();
for i in 0..vec_size {
if i == access_index {
equality_dummy_vals.push(F::ONE);
index_matches_vals.push(F::ONE);
} else {
equality_dummy_vals.push(
(F::from_canonical_usize(i) - F::from_canonical_usize(access_index))
.inverse(),
);
index_matches_vals.push(F::ZERO);
}
}
v.extend(equality_dummy_vals);
v.extend(index_matches_vals);
v.iter().map(|&x| x.into()).collect::<Vec<_>>()
}
let list = vec![FF::rand(); 3];
let access_index = 1;
let gate = SwitchGate::<F, D> {
vec_size: 3,
_phantom: PhantomData,
};
let good_claimed_element = list[access_index];
let good_vars = EvaluationVars {
local_constants: &[],
local_wires: &get_wires(list.clone(), access_index, good_claimed_element),
public_inputs_hash: &HashOut::rand(),
};
let bad_claimed_element = FF::rand();
let bad_vars = EvaluationVars {
local_constants: &[],
local_wires: &get_wires(list, access_index, bad_claimed_element),
public_inputs_hash: &HashOut::rand(),
};
assert!(
gate.eval_unfiltered(good_vars).iter().all(|x| x.is_zero()),
"Gate constraints are not satisfied."
);
assert!(
!gate.eval_unfiltered(bad_vars).iter().all(|x| x.is_zero()),
"Gate constraints are satisfied but shouold not be."
);
}
}