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

feat: Reuse witnesses which have been assigned constant values during ACIR gen #3137

Merged
merged 3 commits into from
Oct 13, 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
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@
/// The vars object is an instance of the `TwoWayMap`, which provides a bidirectional mapping between `AcirVar` and `AcirVarData`.
vars: HashMap<AcirVar, AcirVarData>,

constant_witnesses: HashMap<FieldElement, Witness>,

/// An in-memory representation of ACIR.
///
/// This struct will progressively be populated
Expand Down Expand Up @@ -227,7 +229,16 @@
/// Converts an [`AcirVar`] to a [`Witness`]
fn var_to_witness(&mut self, var: AcirVar) -> Result<Witness, InternalError> {
let expression = self.var_to_expression(var)?;
Ok(self.acir_ir.get_or_create_witness(&expression))
let witness = if let Some(constant) = expression.to_const() {
// Check if a witness has been assigned this value already, if so reuse it.
*self
.constant_witnesses
.entry(constant)
.or_insert_with(|| self.acir_ir.get_or_create_witness(&expression))
} else {
self.acir_ir.get_or_create_witness(&expression)
};
Ok(witness)
}

/// Converts an [`AcirVar`] to an [`Expression`]
Expand Down Expand Up @@ -281,7 +292,7 @@
let inverted_var = self.add_data(AcirVarData::Const(constant.inverse()));

// Check that the inverted var is valid.
// This check prevents invalid divisons by zero.

Check warning on line 295 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (divisons)
let should_be_one = self.mul_var(inverted_var, var)?;
self.maybe_eq_predicate(should_be_one, predicate)?;

Expand All @@ -300,7 +311,7 @@
let inverted_var = Self::expect_one_var(results);

// Check that the inverted var is valid.
// This check prevents invalid divisons by zero.

Check warning on line 314 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (divisons)
let should_be_one = self.mul_var(inverted_var, var)?;
self.maybe_eq_predicate(should_be_one, predicate)?;

Expand Down Expand Up @@ -560,7 +571,7 @@
/// Returns the quotient and remainder such that lhs = rhs * quotient + remainder
/// and |remainder| < |rhs|
/// and remainder has the same sign than lhs
/// Note that this is not the euclidian division, where we have instead remainder < |rhs|

Check warning on line 574 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (euclidian)
fn signed_division_var(
&mut self,
lhs: AcirVar,
Expand Down Expand Up @@ -616,7 +627,7 @@
}

/// Returns an `AcirVar` which will be constrained to be lhs mod 2^{rhs}
/// In order to do this, we 'simply' perform euclidian division of lhs by 2^{rhs}

Check warning on line 630 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (euclidian)
/// The remainder of the division is then lhs mod 2^{rhs}
pub(crate) fn truncate_var(
&mut self,
Expand Down Expand Up @@ -934,7 +945,7 @@
}

/// Recursively create acir values for returned arrays. This is necessary because a brillig returned array can have nested arrays as elements.
/// A singular array of witnesses is collected for a top level array, by deflattening the assigned witnesses at each level.

Check warning on line 948 in compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/acir_variable.rs

View workflow job for this annotation

GitHub Actions / Spellcheck / Spellcheck

Unknown word (deflattening)
fn brillig_array_output(
&mut self,
element_types: &[AcirType],
Expand Down
Binary file modified tooling/nargo_cli/tests/acir_artifacts/7_function/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/eddsa/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/regression/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/schnorr/target/acir.gz
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/schnorr/target/witness.gz
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/sha2_blocks/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/sha2_byte/target/acir.gz
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/sha2_byte/target/witness.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/slices/target/acir.gz
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/slices/target/witness.gz
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/to_be_bytes/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file modified tooling/nargo_cli/tests/acir_artifacts/to_le_bytes/target/acir.gz
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.