diff --git a/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs b/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs index fbc294b35a..43e32101cc 100644 --- a/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs +++ b/acvm-repo/acvm/src/compiler/optimizers/merge_expressions.rs @@ -328,6 +328,50 @@ mod tests { check_circuit(circuit); } + #[test] + fn does_not_eliminate_witnesses_returned_from_circuit() { + let opcodes = vec![ + Opcode::AssertZero(Expression { + mul_terms: vec![(FieldElement::from(-1i128), Witness(0), Witness(0))], + linear_combinations: vec![(FieldElement::from(1i128), Witness(1))], + q_c: FieldElement::zero(), + }), + Opcode::AssertZero(Expression { + mul_terms: Vec::new(), + linear_combinations: vec![ + (FieldElement::from(-1i128), Witness(1)), + (FieldElement::from(1i128), Witness(2)), + ], + q_c: FieldElement::zero(), + }), + ]; + // Witness(1) could be eliminated because it's only used by 2 opcodes. + + let mut private_parameters = BTreeSet::new(); + private_parameters.insert(Witness(0)); + + let mut return_values = BTreeSet::new(); + return_values.insert(Witness(1)); + return_values.insert(Witness(2)); + + let circuit = Circuit { + current_witness_index: 2, + expression_width: ExpressionWidth::Bounded { width: 4 }, + opcodes, + private_parameters, + public_parameters: PublicInputs::default(), + return_values: PublicInputs(return_values), + assert_messages: Default::default(), + }; + + let mut merge_optimizer = MergeExpressionsOptimizer::new(); + let acir_opcode_positions = vec![0; 20]; + let (opcodes, _) = + merge_optimizer.eliminate_intermediate_variable(&circuit, acir_opcode_positions); + + assert_eq!(opcodes.len(), 2); + } + #[test] fn does_not_attempt_to_merge_into_previous_opcodes() { let opcodes = vec![