From 94a0ad7846a6ab929b620fb1b85f6dddeb0ee92a Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Wed, 18 Aug 2021 17:46:38 -0700 Subject: [PATCH 1/8] switch gate (in progress) --- src/gates/mod.rs | 1 + src/gates/random_access.rs | 14 +- src/gates/switch.rs | 356 +++++++++++++++++++++++++++++++++++++ 3 files changed, 364 insertions(+), 7 deletions(-) create mode 100644 src/gates/switch.rs diff --git a/src/gates/mod.rs b/src/gates/mod.rs index dcb96b96..5c4eeafe 100644 --- a/src/gates/mod.rs +++ b/src/gates/mod.rs @@ -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; diff --git a/src/gates/random_access.rs b/src/gates/random_access.rs index 0dd008f0..bdec75f1 100644 --- a/src/gates/random_access.rs +++ b/src/gates/random_access.rs @@ -27,7 +27,7 @@ impl, const D: usize> RandomAccessGate { } } - pub fn wires_access_index(&self) -> usize { + pub fn wire_access_index(&self) -> usize { 0 } @@ -67,7 +67,7 @@ impl, const D: usize> Gate for RandomAccessGate { } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { - 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::>(); @@ -93,7 +93,7 @@ impl, const D: usize> Gate for RandomAccessGate { } fn eval_unfiltered_base(&self, vars: EvaluationVarsBase) -> Vec { - 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::>(); @@ -124,7 +124,7 @@ impl, const D: usize> Gate for RandomAccessGate { builder: &mut CircuitBuilder, vars: EvaluationTargets, ) -> Vec> { - 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::>(); @@ -200,7 +200,7 @@ impl, const D: usize> SimpleGenerator for RandomAccessGenera let local_targets = |inputs: Range| 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, const D: usize> SimpleGenerator 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); diff --git a/src/gates/switch.rs b/src/gates/switch.rs new file mode 100644 index 00000000..3ee5e563 --- /dev/null +++ b/src/gates/switch.rs @@ -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, const D: usize, const CHUNK_SIZE: usize> { + num_copies: usize, + _phantom: PhantomData, +} + +impl, const D: usize, const CHUNK_SIZE: usize> SwitchGate { + 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, const D: usize, const CHUNK_SIZE: usize> Gate for SwitchGate { + fn id(&self) -> String { + format!("{:?}", self, D) + } + + fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { + 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) -> Vec { + 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, + vars: EvaluationTargets, + ) -> Vec> { + 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>> { + let gen = SwitchGenerator:: { + 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, const D: usize, const CHUNK_SIZE: usize> { + gate_index: usize, + gate: SwitchGate, +} + +impl, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator for SwitchGenerator { + fn dependencies(&self) -> Vec { + let local_target = |input| Target::wire(self.gate_index, input); + + let local_targets = |inputs: Range| 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, out_buffer: &mut GeneratedValues) { + 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:: { + 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::(SwitchGate::new(CircuitConfig::large_config())); + } + + #[test] + fn eval_fns() -> Result<()> { + test_eval_fns::(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, access_index: usize, claimed_element: FF) -> Vec { + 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::>() + } + + let list = vec![FF::rand(); 3]; + let access_index = 1; + let gate = SwitchGate:: { + 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." + ); + } +} From 1ccff4d004b2a862b7c9dd62bcb3504671fe38cd Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 19 Aug 2021 10:38:46 -0700 Subject: [PATCH 2/8] progress --- src/gates/switch.rs | 115 +++++++++++++++++++++----------------------- 1 file changed, 55 insertions(+), 60 deletions(-) diff --git a/src/gates/switch.rs b/src/gates/switch.rs index 3ee5e563..e9e34cd7 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -34,39 +34,41 @@ impl, const D: usize, const CHUNK_SIZE: usize> SwitchGate 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) { + pub fn wire_second_input(&self, copy: usize, element: usize) -> 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) { + pub fn wire_first_output(&self, copy: usize, element: usize) -> 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) { + pub fn wire_second_output(&self, copy: usize, element: usize) -> usize { debug_assert!(copy < self.num_copies); debug_assert!(element < CHUNK_SIZE); copy * (4 * CHUNK_SIZE + 1) + 1 + 3 * CHUNK_SIZE + element } } -impl, const D: usize, const CHUNK_SIZE: usize> Gate for SwitchGate { +impl, const D: usize, const CHUNK_SIZE: usize> Gate + for SwitchGate +{ fn id(&self) -> String { format!("{:?}", self, D) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { 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)]; @@ -75,20 +77,21 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for S 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 + .push((F::Extension::ONE - switch_bool) * (second_input - second_output)); } } constraints } - fn eval_unfiltered_base(&self, vars: EvaluationVarsBase) -> Vec { + fn eval_unfiltered_base(&self, vars: EvaluationVarsBase) -> Vec { 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)]; @@ -97,7 +100,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for S 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)); @@ -114,7 +117,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for S vars: EvaluationTargets, ) -> Vec> { 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)]; @@ -125,22 +128,24 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for S 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); + 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); + 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); + let second_not_switched_constraint = + builder.mul_extension(not_switch, second_not_switched); constraints.push(second_not_switched_constraint); } } @@ -183,7 +188,9 @@ struct SwitchGenerator, const D: usize, const CHUNK_SIZE: usize gate: SwitchGate, } -impl, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator for SwitchGenerator { +impl, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator + for SwitchGenerator +{ fn dependencies(&self) -> Vec { let local_target = |input| Target::wire(self.gate_index, input); @@ -252,6 +259,7 @@ mod tests { 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::circuit_data::CircuitConfig; use crate::plonk::vars::EvaluationVars; #[test] @@ -261,7 +269,6 @@ mod tests { _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); @@ -290,67 +297,55 @@ mod tests { type F = CrandallField; type FF = QuarticCrandallField; const D: usize = 4; + const CHUNK_SIZE: usize = 4; + let num_copies = 3; /// Returns the local wires for a random access gate given the vector, element to compare, /// and index. - fn get_wires(list: Vec, access_index: usize, claimed_element: FF) -> Vec { - let vec_size = list.len(); + fn get_wires( + first_inputs: Vec>, + second_inputs: Vec>, + switch_bools: Vec, + ) -> Vec { + let num_copies = first_inputs.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); + for c in 0..num_copies { + let switch = switch_bools[c]; + v.push(F::from_bool(switch)); + for e in 0..CHUNK_SIZE { + let first_input = first_inputs[c][e]; + let second_input = second_inputs[c][e]; + let first_output = if switch { second_input } else { first_input }; + let second_output = if switch { first_input } else { second_input }; + v.push(first_input); + v.push(second_input); + v.push(first_output); + v.push(second_output); } } - v.extend(equality_dummy_vals); - v.extend(index_matches_vals); - v.iter().map(|&x| x.into()).collect::>() } - let list = vec![FF::rand(); 3]; - let access_index = 1; - let gate = SwitchGate:: { - vec_size: 3, + let first_inputs = vec![vec![F::rand(); CHUNK_SIZE]; num_copies]; + let second_inputs = vec![vec![F::rand(); CHUNK_SIZE]; num_copies]; + let switch_bools = vec![true, false, true]; + + let gate = SwitchGate:: { + num_copies, _phantom: PhantomData, }; - let good_claimed_element = list[access_index]; - let good_vars = EvaluationVars { + let 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), + local_wires: &get_wires(first_inputs, second_inputs, switch_bools), public_inputs_hash: &HashOut::rand(), }; assert!( - gate.eval_unfiltered(good_vars).iter().all(|x| x.is_zero()), + gate.eval_unfiltered(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." - ); } } From c2d7044fb60c1c8a7810d68fddd51e04fa90017b Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 19 Aug 2021 16:47:15 -0700 Subject: [PATCH 3/8] progress --- src/gates/switch.rs | 39 +++++++++++++++------------------------ 1 file changed, 15 insertions(+), 24 deletions(-) diff --git a/src/gates/switch.rs b/src/gates/switch.rs index e9e34cd7..09318d6b 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -216,32 +216,23 @@ impl, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator< 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()); + for c in 0..self.gate.num_copies { + let switch_bool = get_local_wire(self.gate.wire_switch_bool(c)); + for e in 0..CHUNK_SIZE { + let first_input = get_local_wire(self.gate.wire_first_input(c, e)); + let second_input = get_local_wire(self.gate.wire_second_input(c, e)); + let first_output_wire = local_wire(self.gate.wire_first_output(c, e)); + let second_output_wire = local_wire(self.gate.wire_second_output(c, e)); - 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); + if switch_bool == F::ONE { + out_buffer.set_wire(first_output_wire, second_input); + out_buffer.set_wire(second_output_wire, first_input); + } else { + out_buffer.set_wire(first_output_wire, first_input); + out_buffer.set_wire(second_output_wire, second_input); + } } + } } } From f5c5ed9cfd8db901d40bfec8b1397df7448b8c62 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 19 Aug 2021 19:41:18 -0700 Subject: [PATCH 4/8] finished switch gate --- src/gadgets/random_access.rs | 2 +- src/gates/random_access.rs | 2 +- src/gates/switch.rs | 36 ++++++++++++++++++++++-------------- 3 files changed, 24 insertions(+), 16 deletions(-) diff --git a/src/gadgets/random_access.rs b/src/gadgets/random_access.rs index 6c34c0be..99c2ae5a 100644 --- a/src/gadgets/random_access.rs +++ b/src/gadgets/random_access.rs @@ -24,7 +24,7 @@ impl, const D: usize> CircuitBuilder { }); self.route( access_index, - Target::wire(gate_index, gate.wires_access_index()), + Target::wire(gate_index, gate.wire_access_index()), ); self.route_extension( claimed_element, diff --git a/src/gates/random_access.rs b/src/gates/random_access.rs index bdec75f1..32d1a8fa 100644 --- a/src/gates/random_access.rs +++ b/src/gates/random_access.rs @@ -12,7 +12,7 @@ use crate::iop::witness::PartialWitness; use crate::plonk::circuit_builder::CircuitBuilder; use crate::plonk::vars::{EvaluationTargets, EvaluationVars, EvaluationVarsBase}; -/// A gate for checking that a particular value in a list matches a given +/// A gate for checking that a particular element of a list matches a given value. #[derive(Clone, Debug)] pub(crate) struct RandomAccessGate, const D: usize> { pub vec_size: usize, diff --git a/src/gates/switch.rs b/src/gates/switch.rs index 09318d6b..721ce032 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -2,7 +2,7 @@ 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::extension_field::Extendable; use crate::field::field_types::Field; use crate::gates::gate::Gate; use crate::iop::generator::{GeneratedValues, SimpleGenerator, WitnessGenerator}; @@ -13,7 +13,7 @@ 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 +/// A gate for conditionally swapping input values based on a boolean. #[derive(Clone, Debug)] pub(crate) struct SwitchGate, const D: usize, const CHUNK_SIZE: usize> { num_copies: usize, @@ -22,13 +22,17 @@ pub(crate) struct SwitchGate, const D: usize, const CHUNK_SIZE: impl, const D: usize, const CHUNK_SIZE: usize> SwitchGate { pub fn new(config: CircuitConfig) -> Self { - let num_copies = Self::max_num_chunks(config.num_wires, config.num_routed_wires); + let num_copies = Self::max_num_copies(config.num_routed_wires); Self { num_copies, _phantom: PhantomData, } } + fn max_num_copies(num_routed_wires: usize) -> usize { + num_routed_wires / (4 * CHUNK_SIZE + 1) + } + pub fn wire_switch_bool(&self, copy: usize) -> usize { debug_assert!(copy < self.num_copies); copy * (4 * CHUNK_SIZE + 1) @@ -158,7 +162,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate gate_index: usize, _local_constants: &[F], ) -> Vec>> { - let gen = SwitchGenerator:: { + let gen = SwitchGenerator:: { gate_index, gate: self.clone(), }; @@ -232,7 +236,6 @@ impl, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator< out_buffer.set_wire(second_output_wire, second_input); } } - } } } @@ -267,20 +270,26 @@ mod tests { 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); + assert_eq!(gate.wire_switch_bool(1), 13); + assert_eq!(gate.wire_first_input(1, 0), 14); + assert_eq!(gate.wire_second_output(1, 2), 25); + assert_eq!(gate.wire_switch_bool(2), 26); + assert_eq!(gate.wire_first_input(2, 0), 27); + assert_eq!(gate.wire_second_output(2, 2), 38); } #[test] fn low_degree() { - test_low_degree::(SwitchGate::new(CircuitConfig::large_config())); + test_low_degree::(SwitchGate::<_, 4, 3>::new( + CircuitConfig::large_config(), + )); } #[test] fn eval_fns() -> Result<()> { - test_eval_fns::(SwitchGate::new(CircuitConfig::large_config())) + test_eval_fns::(SwitchGate::<_, 4, 3>::new( + CircuitConfig::large_config(), + )) } #[test] @@ -291,13 +300,12 @@ mod tests { const CHUNK_SIZE: usize = 4; let num_copies = 3; - /// Returns the local wires for a random access gate given the vector, element to compare, - /// and index. + /// Returns the local wires for a switch gate given the inputs and the switch booleans. fn get_wires( first_inputs: Vec>, second_inputs: Vec>, switch_bools: Vec, - ) -> Vec { + ) -> Vec { let num_copies = first_inputs.len(); let mut v = Vec::new(); From 7dea2451d0d23222889029633398dbaf26c3bdd9 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 19 Aug 2021 22:30:11 -0700 Subject: [PATCH 5/8] addressed comments --- src/gates/switch.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/gates/switch.rs b/src/gates/switch.rs index 721ce032..f06ae7a9 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -67,7 +67,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for SwitchGate { fn id(&self) -> String { - format!("{:?}", self, D) + format!("{:?}", self, D, CHUNK_SIZE) } fn eval_unfiltered(&self, vars: EvaluationVars) -> Vec { @@ -75,6 +75,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for c in 0..self.num_copies { let switch_bool = vars.local_wires[self.wire_switch_bool(c)]; + let not_switch = F::Extension::ONE - switch_bool; for e in 0..CHUNK_SIZE { let first_input = vars.local_wires[self.wire_first_input(c, e)]; @@ -84,9 +85,9 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate 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(not_switch * (first_input - first_output)); constraints - .push((F::Extension::ONE - switch_bool) * (second_input - second_output)); + .push(not_switch * (second_input - second_output)); } } @@ -98,6 +99,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate for c in 0..self.num_copies { let switch_bool = vars.local_wires[self.wire_switch_bool(c)]; + let not_switch = F::ONE - switch_bool; for e in 0..CHUNK_SIZE { let first_input = vars.local_wires[self.wire_first_input(c, e)]; @@ -107,8 +109,8 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate 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.push(not_switch * (first_input - first_output)); + constraints.push(not_switch * (second_input - second_output)); } } From 0155c422ab890677aafa80639ef533e923cd3317 Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 19 Aug 2021 22:34:09 -0700 Subject: [PATCH 6/8] fmt --- src/gates/switch.rs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/gates/switch.rs b/src/gates/switch.rs index f06ae7a9..13bb2883 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -86,8 +86,7 @@ impl, const D: usize, const CHUNK_SIZE: usize> Gate constraints.push(switch_bool * (first_input - second_output)); constraints.push(switch_bool * (second_input - first_output)); constraints.push(not_switch * (first_input - first_output)); - constraints - .push(not_switch * (second_input - second_output)); + constraints.push(not_switch * (second_input - second_output)); } } From 40c760c80cf3ef082c21d20abb0293d4838bad2f Mon Sep 17 00:00:00 2001 From: Nicholas Ward Date: Thu, 19 Aug 2021 22:56:51 -0700 Subject: [PATCH 7/8] actually randomizes --- src/gates/switch.rs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/gates/switch.rs b/src/gates/switch.rs index 13bb2883..966c1a52 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -328,8 +328,8 @@ mod tests { v.iter().map(|&x| x.into()).collect::>() } - let first_inputs = vec![vec![F::rand(); CHUNK_SIZE]; num_copies]; - let second_inputs = vec![vec![F::rand(); CHUNK_SIZE]; num_copies]; + let first_inputs: Vec> = (0..num_copies).map(|_| F::rand_vec(CHUNK_SIZE)).collect(); + let second_inputs: Vec> = (0..num_copies).map(|_| F::rand_vec(CHUNK_SIZE)).collect(); let switch_bools = vec![true, false, true]; let gate = SwitchGate:: { From 717efbb894b51ec99d110af8cadf832ab54eccd2 Mon Sep 17 00:00:00 2001 From: wborgeaud Date: Fri, 20 Aug 2021 18:21:14 +0200 Subject: [PATCH 8/8] Fix test --- src/gates/switch.rs | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/src/gates/switch.rs b/src/gates/switch.rs index 966c1a52..7d99387f 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -1,5 +1,4 @@ use std::marker::PhantomData; -use std::ops::Range; use crate::field::extension_field::target::ExtensionTarget; use crate::field::extension_field::Extendable; @@ -199,8 +198,6 @@ impl, const D: usize, const CHUNK_SIZE: usize> SimpleGenerator< fn dependencies(&self) -> Vec { let local_target = |input| Target::wire(self.gate_index, input); - let local_targets = |inputs: Range| 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))); @@ -313,16 +310,24 @@ mod tests { for c in 0..num_copies { let switch = switch_bools[c]; v.push(F::from_bool(switch)); + let mut first_input_chunk = Vec::with_capacity(CHUNK_SIZE); + let mut second_input_chunk = Vec::with_capacity(CHUNK_SIZE); + let mut first_output_chunk = Vec::with_capacity(CHUNK_SIZE); + let mut second_output_chunk = Vec::with_capacity(CHUNK_SIZE); for e in 0..CHUNK_SIZE { let first_input = first_inputs[c][e]; let second_input = second_inputs[c][e]; let first_output = if switch { second_input } else { first_input }; let second_output = if switch { first_input } else { second_input }; - v.push(first_input); - v.push(second_input); - v.push(first_output); - v.push(second_output); + first_input_chunk.push(first_input); + second_input_chunk.push(second_input); + first_output_chunk.push(first_output); + second_output_chunk.push(second_output); } + v.append(&mut first_input_chunk); + v.append(&mut second_input_chunk); + v.append(&mut first_output_chunk); + v.append(&mut second_output_chunk); } v.iter().map(|&x| x.into()).collect::>()