Skip to content

Commit

Permalink
fix: prevent duplicated assert message transformation (noir-lang#3038)
Browse files Browse the repository at this point in the history
Co-authored-by: kevaundray <kevtheappdev@gmail.com>
  • Loading branch information
2 people authored and Sakapoi committed Oct 19, 2023
1 parent b58c8d6 commit d12b64e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 17 deletions.
10 changes: 8 additions & 2 deletions acvm-repo/acvm/src/compiler/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ mod optimizers;
mod transformers;

pub use optimizers::optimize;
use optimizers::optimize_internal;
pub use transformers::transform;
use transformers::transform_internal;

Expand Down Expand Up @@ -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))
}
13 changes: 10 additions & 3 deletions acvm-repo/acvm/src/compiler/optimizers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Opcode> = Vec::new();
for opcode in acir.opcodes {
Expand All @@ -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)
}
24 changes: 12 additions & 12 deletions acvm-repo/acvm/src/compiler/transformers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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`].
Expand All @@ -40,14 +45,12 @@ pub(super) fn transform_internal(
acir_opcode_positions: Vec<usize>,
) -> 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));
}
Expand Down Expand Up @@ -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))
}

0 comments on commit d12b64e

Please sign in to comment.