Skip to content

Commit

Permalink
feat: Reuse witnesses which have been assigned constant values during…
Browse files Browse the repository at this point in the history
… ACIR gen (noir-lang#3137)

Co-authored-by: Tom French <git@tomfren.ch>
  • Loading branch information
2 people authored and Sakapoi committed Oct 19, 2023
1 parent 32a05a6 commit 84653ae
Show file tree
Hide file tree
Showing 49 changed files with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ pub(crate) struct AcirContext {
/// 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 @@ impl AcirContext {
/// 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
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.

0 comments on commit 84653ae

Please sign in to comment.