From 7658c172a2a9725ff90e12b9a525e3b488c039f6 Mon Sep 17 00:00:00 2001 From: Aztec Bot <49558828+AztecBot@users.noreply.github.com> Date: Thu, 21 Nov 2024 08:36:46 -0500 Subject: [PATCH] feat: Sync from aztec-packages (#6576) Co-authored-by: Tom French --- .aztec-sync-commit | 2 +- Cargo.lock | 4 +- .../compiler/optimizers/merge_expressions.rs | 47 ++++++++++--------- tooling/noir_js_types/src/types.ts | 2 +- 4 files changed, 28 insertions(+), 27 deletions(-) diff --git a/.aztec-sync-commit b/.aztec-sync-commit index dc4d9c58bda..063664dceac 100644 --- a/.aztec-sync-commit +++ b/.aztec-sync-commit @@ -1 +1 @@ -58761fcf181d0a26c4e387105b1bc8a3a75b0368 +b8bace9a00c3a8eb93f42682e8cbfa351fc5238c diff --git a/Cargo.lock b/Cargo.lock index 6b24c0f8c67..0f4866f8c7e 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -805,9 +805,9 @@ dependencies = [ [[package]] name = "clap_complete" -version = "4.5.37" +version = "4.5.38" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "11611dca53440593f38e6b25ec629de50b14cdfa63adc0fb856115a2c6d97595" +checksum = "d9647a559c112175f17cf724dc72d3645680a883c58481332779192b0d8e7a01" dependencies = [ "clap", ] diff --git a/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs b/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs index 9d0d5b3af60..0a55e4ca17c 100644 --- a/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs +++ b/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs @@ -50,31 +50,36 @@ impl MergeExpressionsOptimizer { let mut new_circuit = Vec::new(); let mut new_acir_opcode_positions = Vec::new(); // For each opcode, try to get a target opcode to merge with - for (i, opcode) in circuit.opcodes.iter().enumerate() { + for (i, (opcode, opcode_position)) in + circuit.opcodes.iter().zip(acir_opcode_positions).enumerate() + { if !matches!(opcode, Opcode::AssertZero(_)) { new_circuit.push(opcode.clone()); - new_acir_opcode_positions.push(acir_opcode_positions[i]); + new_acir_opcode_positions.push(opcode_position); continue; } let opcode = modified_gates.get(&i).unwrap_or(opcode).clone(); let mut to_keep = true; let input_witnesses = self.witness_inputs(&opcode); - for w in input_witnesses.clone() { - let empty_gates = BTreeSet::new(); - let gates_using_w = used_witness.get(&w).unwrap_or(&empty_gates); + for w in input_witnesses { + let Some(gates_using_w) = used_witness.get(&w) else { + continue; + }; // We only consider witness which are used in exactly two arithmetic gates if gates_using_w.len() == 2 { - let gates_using_w: Vec<_> = gates_using_w.iter().collect(); - let mut b = *gates_using_w[1]; - if b == i { - b = *gates_using_w[0]; + let first = *gates_using_w.first().expect("gates_using_w.len == 2"); + let second = *gates_using_w.last().expect("gates_using_w.len == 2"); + let b = if second == i { + first } else { // sanity check - assert!(i == *gates_using_w[0]); - } - let second_gate = modified_gates.get(&b).unwrap_or(&circuit.opcodes[b]).clone(); + assert!(i == first); + second + }; + + let second_gate = modified_gates.get(&b).unwrap_or(&circuit.opcodes[b]); if let (Opcode::AssertZero(expr_define), Opcode::AssertZero(expr_use)) = - (opcode.clone(), second_gate) + (&opcode, second_gate) { // We cannot merge an expression into an earlier opcode, because this // would break the 'execution ordering' of the opcodes @@ -85,16 +90,15 @@ impl MergeExpressionsOptimizer { // - doing this pass again until there is no change, or // - merging 'b' into 'i' instead if i < b { - if let Some(expr) = Self::merge(&expr_use, &expr_define, w) { + if let Some(expr) = Self::merge(expr_use, expr_define, w) { modified_gates.insert(b, Opcode::AssertZero(expr)); to_keep = false; // Update the 'used_witness' map to account for the merge. - for w2 in CircuitSimulator::expr_wit(&expr_define) { + for w2 in CircuitSimulator::expr_wit(expr_define) { if !circuit_inputs.contains(&w2) { - let mut v = used_witness[&w2].clone(); + let v = used_witness.entry(w2).or_default(); v.insert(b); v.remove(&i); - used_witness.insert(w2, v); } } // We need to stop here and continue with the next opcode @@ -107,12 +111,9 @@ impl MergeExpressionsOptimizer { } if to_keep { - if modified_gates.contains_key(&i) { - new_circuit.push(modified_gates[&i].clone()); - } else { - new_circuit.push(opcode.clone()); - } - new_acir_opcode_positions.push(acir_opcode_positions[i]); + let opcode = modified_gates.get(&i).cloned().unwrap_or(opcode); + new_circuit.push(opcode); + new_acir_opcode_positions.push(opcode_position); } } (new_circuit, new_acir_opcode_positions) diff --git a/tooling/noir_js_types/src/types.ts b/tooling/noir_js_types/src/types.ts index 03e2a93f851..0cfe5d05a17 100644 --- a/tooling/noir_js_types/src/types.ts +++ b/tooling/noir_js_types/src/types.ts @@ -1,5 +1,5 @@ export type Field = string | number | boolean; -export type InputValue = Field | InputMap | (Field | InputMap)[]; +export type InputValue = Field | InputMap | InputValue[]; export type InputMap = { [key: string]: InputValue }; export type Visibility = 'public' | 'private' | 'databus';