Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: prevent duplicated assert message transformation #3038

Merged
merged 3 commits into from
Oct 9, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -3,15 +3,15 @@
native_types::{Expression, Witness},
FieldElement,
};
use indexmap::IndexMap;

Check warning on line 6 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (indexmap)

use crate::Language;

mod csat;

Check warning on line 10 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (csat)
mod fallback;
mod r1cs;

pub(crate) use csat::CSatTransformer;

Check warning on line 14 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (csat)
pub(crate) use fallback::FallbackTransformer;
pub(crate) use r1cs::R1CSTransformer;

Expand All @@ -27,7 +27,12 @@
// 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,27 +45,25 @@
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));
}
crate::Language::PLONKCSat { width } => {
let mut csat = CSatTransformer::new(*width);

Check warning on line 58 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (csat)
for value in acir.circuit_arguments() {
csat.mark_solvable(value);

Check warning on line 60 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (csat)
}
csat

Check warning on line 62 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (csat)
}
};

// TODO: the code below is only for CSAT transformer

Check warning on line 66 in acvm-repo/acvm/src/compiler/transformers/mod.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (CSAT)
// TODO it may be possible to refactor it in a way that we do not need to return early from the r1cs
// TODO or at the very least, we could put all of it inside of CSatOptimizer pass

Expand Down Expand Up @@ -200,18 +203,15 @@

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))
}