From 1d6cc1089c95e23d71a276e2abb10a9edfff476e Mon Sep 17 00:00:00 2001 From: guipublic Date: Fri, 14 Jul 2023 08:47:22 +0000 Subject: [PATCH] adds inputs to the circuit and update sort for stepwise acir --- crates/noirc_abi/src/lib.rs | 4 ++++ crates/noirc_evaluator/src/lib.rs | 1 + crates/noirc_evaluator/src/ssa_refactor.rs | 5 +++-- .../acir_gen/acir_ir/acir_variable.rs | 5 ++--- .../acir_gen/acir_ir/generated_acir.rs | 17 +++++++++++------ 5 files changed, 21 insertions(+), 11 deletions(-) diff --git a/crates/noirc_abi/src/lib.rs b/crates/noirc_abi/src/lib.rs index 856889b386..11cce3a514 100644 --- a/crates/noirc_abi/src/lib.rs +++ b/crates/noirc_abi/src/lib.rs @@ -170,6 +170,10 @@ impl Abi { self.parameters.iter().map(|x| &x.name).collect() } + pub fn parameter_witness(&self) -> Vec { + self.param_witnesses.values().flatten().copied().collect() + } + pub fn num_parameters(&self) -> usize { self.parameters.len() } diff --git a/crates/noirc_evaluator/src/lib.rs b/crates/noirc_evaluator/src/lib.rs index a77ded3e2d..7420c98a21 100644 --- a/crates/noirc_evaluator/src/lib.rs +++ b/crates/noirc_evaluator/src/lib.rs @@ -86,6 +86,7 @@ pub fn create_circuit( opcodes, public_parameters: PublicInputs(public_parameters), return_values: PublicInputs(return_values.iter().copied().collect()), + inputs: param_witnesses.values().flatten().copied().collect(), }; let (parameters, return_type) = program.main_function_signature; diff --git a/crates/noirc_evaluator/src/ssa_refactor.rs b/crates/noirc_evaluator/src/ssa_refactor.rs index a61eae4ca9..33456d7c15 100644 --- a/crates/noirc_evaluator/src/ssa_refactor.rs +++ b/crates/noirc_evaluator/src/ssa_refactor.rs @@ -76,8 +76,9 @@ pub fn experimental_create_circuit( let public_parameters = PublicInputs(public_abi.param_witnesses.values().flatten().copied().collect()); let return_values = PublicInputs(return_witnesses.into_iter().collect()); - - let circuit = Circuit { current_witness_index, opcodes, public_parameters, return_values }; + let inputs = abi.parameter_witness(); + let circuit = + Circuit { current_witness_index, opcodes, public_parameters, return_values, inputs }; Ok((circuit, abi)) } diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs index c378403399..2844151ee8 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/acir_variable.rs @@ -856,13 +856,12 @@ impl AcirContext { let outputs_var = vecmap(&outputs_witness, |witness_index| { self.add_data(AcirVarData::Witness(*witness_index)) }); + // Enforce the outputs to be a permutation of the inputs + self.acir_ir.permutation(&inputs_expr, &output_expr); // Enforce the outputs to be sorted for i in 0..(outputs_var.len() - 1) { self.less_than_constrain(outputs_var[i], outputs_var[i + 1], bit_size, None)?; } - // Enforce the outputs to be a permutation of the inputs - self.acir_ir.permutation(&inputs_expr, &output_expr); - Ok(outputs_var) } diff --git a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs index ed354f77e1..011e14d8e1 100644 --- a/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs +++ b/crates/noirc_evaluator/src/ssa_refactor/acir_gen/acir_ir/generated_acir.rs @@ -781,19 +781,24 @@ impl GeneratedAcir { /// n.b. A sorting network is a predetermined set of switches, /// the control bits indicate the configuration of each switch: false for pass-through and true for cross-over pub(crate) fn permutation(&mut self, in_expr: &[Expression], out_expr: &[Expression]) { - let bits = Vec::new(); - let (w, b) = self.permutation_layer(in_expr, &bits, true); - // Constrain the network output to out_expr - for (b, o) in b.iter().zip(out_expr) { - self.push_opcode(AcirOpcode::Arithmetic(b - o)); + let mut bits_len = 0; + for i in 0..in_expr.len() { + bits_len += ((i + 1) as f32).log2().ceil() as u32; } + let bits = vecmap(0..bits_len, |_| self.next_witness_index()); let inputs = in_expr.iter().map(|a| vec![a.clone()]).collect(); self.push_opcode(AcirOpcode::Directive(Directive::PermutationSort { inputs, tuple: 1, - bits: w, + bits: bits.clone(), sort_by: vec![0], })); + let (_, b) = self.permutation_layer(in_expr, &bits, false); + + // Constrain the network output to out_expr + for (b, o) in b.iter().zip(out_expr) { + self.push_opcode(AcirOpcode::Arithmetic(b - o)); + } } }