diff --git a/src/gadgets/permutation.rs b/src/gadgets/permutation.rs index 64e18f93..e94315b0 100644 --- a/src/gadgets/permutation.rs +++ b/src/gadgets/permutation.rs @@ -58,19 +58,19 @@ impl, const D: usize> CircuitBuilder { &mut self, a: [Target; CHUNK_SIZE], b: [Target; CHUNK_SIZE], - ) -> (usize, [Target; CHUNK_SIZE], [Target; CHUNK_SIZE]) { + ) -> (Target, [Target; CHUNK_SIZE], [Target; CHUNK_SIZE]) { if self.current_switch_gates.len() < CHUNK_SIZE { self.current_switch_gates .extend(vec![None; CHUNK_SIZE - self.current_switch_gates.len()]); } - let (gate_index, mut next_copy) = match self.current_switch_gates[CHUNK_SIZE - 1] { + let (gate, gate_index, mut next_copy) = match self.current_switch_gates[CHUNK_SIZE - 1] { None => { let gate = SwitchGate::::new_from_config(self.config.clone()); let gate_index = self.add_gate(gate.clone(), vec![]); - (gate_index, 0) + (gate, gate_index, 0) } - Some((idx, next_copy)) => (idx, next_copy), + Some((idx, next_copy)) => (self.gate_instances[idx], idx, next_copy), }; let num_copies = @@ -83,26 +83,31 @@ impl, const D: usize> CircuitBuilder { a[e], Target::wire( gate_index, - SwitchGate::::wire_first_input(0, e), + SwitchGate::::wire_first_input(next_copy, e), ), ); self.route( b[e], Target::wire( gate_index, - SwitchGate::::wire_second_input(0, e), + SwitchGate::::wire_second_input(next_copy, e), ), ); c.push(Target::wire( gate_index, - SwitchGate::::wire_first_output(0, e), + SwitchGate::::wire_first_output(next_copy, e), )); d.push(Target::wire( gate_index, - SwitchGate::::wire_second_output(0, e), + SwitchGate::::wire_second_output(next_copy, e), )); } + let switch = Target::wire( + gate_index, + SwitchGate::::wire_switch_bool(gate.num_copies, next_copy), + ); + let c_arr: [Target; CHUNK_SIZE] = c.try_into().unwrap(); let d_arr: [Target; CHUNK_SIZE] = d.try_into().unwrap(); @@ -115,7 +120,7 @@ impl, const D: usize> CircuitBuilder { self.current_switch_gates[CHUNK_SIZE - 1] = Some((gate_index, next_copy)); } - (gate_index, c_arr, d_arr) + (switch, c_arr, d_arr) } fn assert_permutation_recursive( @@ -140,12 +145,12 @@ impl, const D: usize> CircuitBuilder { }; for i in 0..a_num_switches { - let (_, out_1, out_2) = self.create_switch(a[i * 2], a[i * 2 + 1]); + let (a_switch, out_1, out_2) = self.create_switch(a[i * 2], a[i * 2 + 1]); child_1_a.push(out_1); child_2_a.push(out_2); } for i in 0..b_num_switches { - let (_, out_1, out_2) = self.create_switch(b[i * 2], b[i * 2 + 1]); + let (b_switch, out_1, out_2) = self.create_switch(b[i * 2], b[i * 2 + 1]); child_1_b.push(out_1); child_2_b.push(out_2); } @@ -161,6 +166,8 @@ impl, const D: usize> CircuitBuilder { self.assert_permutation(child_1_a, child_1_b); self.assert_permutation(child_2_a, child_2_b); + + self.add_generator(PermutationGenerator {}); } } @@ -198,7 +205,7 @@ fn route( // if it still needs to be routed. If so, we add it to partial_routes. let enqueue_other_side = |partial_routes: &mut [BTreeMap], witness: &PartialWitness, - out_buffer: &mut GeneratedValues, + _out_buffer: &mut GeneratedValues, side: usize, this_i: usize, subnet: bool| { @@ -306,8 +313,6 @@ fn route( } struct PermutationGenerator { - a_values: Vec<[F; CHUNK_SIZE]>, - b_values: Vec<[F; CHUNK_SIZE]>, a_wires: Vec<[Target; CHUNK_SIZE]>, b_wires: Vec<[Target; CHUNK_SIZE]>, } @@ -322,9 +327,19 @@ impl SimpleGenerator for PermutationGenera } fn run_once(&self, witness: &PartialWitness, out_buffer: &mut GeneratedValues) { + let wire_chunk_to_vals = |wire| { + let mut vals = [F::ZERO; CHUNK_SIZE]; + for e in 0..CHUNK_SIZE { + vals[e] = witness.get_target(wire[e]); + } + vals + }; + + let a_values = self.a_wires.iter().map(wire_chunk_to_vals).collect(); + let b_values = self.b_wires.iter().map(wire_chunk_to_vals).collect(); route( - self.a_values.clone(), - self.b_values.clone(), + a_values.clone(), + b_values.clone(), self.a_wires.clone(), self.b_wires.clone(), witness, diff --git a/src/gates/switch.rs b/src/gates/switch.rs index c421775e..85a26753 100644 --- a/src/gates/switch.rs +++ b/src/gates/switch.rs @@ -161,11 +161,12 @@ 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(), }; - vec![Box::new(gen)] + vec![Box::new(gen)]*/ + vec![] } fn num_wires(&self) -> usize {