diff --git a/acvm-repo/acvm/src/compiler/mod.rs b/acvm-repo/acvm/src/compiler/mod.rs index 5b090e4603f..4abf94a2e78 100644 --- a/acvm-repo/acvm/src/compiler/mod.rs +++ b/acvm-repo/acvm/src/compiler/mod.rs @@ -11,6 +11,7 @@ mod optimizers; mod transformers; pub use optimizers::optimize; +use optimizers::optimize_internal; pub use transformers::transform; use transformers::transform_internal; @@ -73,7 +74,12 @@ pub fn compile( np_language: Language, is_opcode_supported: impl Fn(&Opcode) -> bool, ) -> Result<(Circuit, AcirTransformationMap), CompileError> { - let (acir, AcirTransformationMap { acir_opcode_positions }) = optimize(acir); + let (acir, AcirTransformationMap { acir_opcode_positions }) = optimize_internal(acir); - transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions) + let (mut acir, transformation_map) = + transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions)?; + + acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); + + Ok((acir, transformation_map)) } diff --git a/acvm-repo/acvm/src/compiler/optimizers/mod.rs b/acvm-repo/acvm/src/compiler/optimizers/mod.rs index d04d21039f9..c63cfdf9c82 100644 --- a/acvm-repo/acvm/src/compiler/optimizers/mod.rs +++ b/acvm-repo/acvm/src/compiler/optimizers/mod.rs @@ -13,6 +13,15 @@ use super::{transform_assert_messages, AcirTransformationMap}; /// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] independent optimizations to a [`Circuit`]. pub fn optimize(acir: Circuit) -> (Circuit, AcirTransformationMap) { + let (mut acir, transformation_map) = optimize_internal(acir); + + acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); + + (acir, transformation_map) +} + +/// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] independent optimizations to a [`Circuit`]. +pub(super) fn optimize_internal(acir: Circuit) -> (Circuit, AcirTransformationMap) { // General optimizer pass let mut opcodes: Vec = Vec::new(); for opcode in acir.opcodes { @@ -36,12 +45,10 @@ pub fn optimize(acir: Circuit) -> (Circuit, AcirTransformationMap) { // Range optimization pass let range_optimizer = RangeOptimizer::new(acir); - let (mut acir, acir_opcode_positions) = + let (acir, acir_opcode_positions) = range_optimizer.replace_redundant_ranges(acir_opcode_positions); let transformation_map = AcirTransformationMap { acir_opcode_positions }; - acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); - (acir, transformation_map) } diff --git a/acvm-repo/acvm/src/compiler/transformers/mod.rs b/acvm-repo/acvm/src/compiler/transformers/mod.rs index b909bc54662..3a3a013e8eb 100644 --- a/acvm-repo/acvm/src/compiler/transformers/mod.rs +++ b/acvm-repo/acvm/src/compiler/transformers/mod.rs @@ -27,7 +27,12 @@ pub fn transform( // by applying the modifications done to the circuit opcodes and also to the opcode_positions (delete and insert) let acir_opcode_positions = acir.opcodes.iter().enumerate().map(|(i, _)| i).collect(); - transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions) + let (mut acir, transformation_map) = + transform_internal(acir, np_language, is_opcode_supported, acir_opcode_positions)?; + + acir.assert_messages = transform_assert_messages(acir.assert_messages, &transformation_map); + + Ok((acir, transformation_map)) } /// Applies [`ProofSystemCompiler`][crate::ProofSystemCompiler] specific optimizations to a [`Circuit`]. @@ -40,14 +45,12 @@ pub(super) fn transform_internal( acir_opcode_positions: Vec, ) -> Result<(Circuit, AcirTransformationMap), CompileError> { // Fallback transformer pass - let (mut acir, acir_opcode_positions) = + let (acir, acir_opcode_positions) = FallbackTransformer::transform(acir, is_opcode_supported, acir_opcode_positions)?; let mut transformer = match &np_language { crate::Language::R1CS => { let transformation_map = AcirTransformationMap { acir_opcode_positions }; - acir.assert_messages = - transform_assert_messages(acir.assert_messages, &transformation_map); let transformer = R1CSTransformer::new(acir); return Ok((transformer.transform(), transformation_map)); } @@ -200,18 +203,15 @@ pub(super) fn transform_internal( let current_witness_index = next_witness_index - 1; - let transformation_map = - AcirTransformationMap { acir_opcode_positions: new_acir_opcode_positions }; - let acir = Circuit { current_witness_index, opcodes: transformed_opcodes, - // The optimizer does not add new public inputs - private_parameters: acir.private_parameters, - public_parameters: acir.public_parameters, - return_values: acir.return_values, - assert_messages: transform_assert_messages(acir.assert_messages, &transformation_map), + // The transformer does not add new public inputs + ..acir }; + let transformation_map = + AcirTransformationMap { acir_opcode_positions: new_acir_opcode_positions }; + Ok((acir, transformation_map)) }