Skip to content

Commit

Permalink
feat: simplify multiplications by 0 or 1 in ACIR gen (#3924)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves <!-- Link to GitHub Issue -->

## Summary\*

This PR avoids churn on `AcirVar`s when we perform trivial
multiplications. This avoids some situations where we were codegenning
expressions with zero-coefficient terms.

## Additional Context



## Documentation\*

Check one:
- [x] No documentation needed.
- [ ] Documentation included in this PR.
- [ ] **[Exceptional Case]** Documentation to be submitted in a separate
PR.

# PR Checklist\*

- [x] I have tested the changes locally.
- [x] I have formatted the changes with [Prettier](https://prettier.io/)
and/or `cargo fmt` on default settings.
  • Loading branch information
TomAFrench authored Jan 2, 2024
1 parent 64dd78b commit e58844d
Showing 1 changed file with 12 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -533,8 +533,19 @@ impl AcirContext {
let lhs_data = self.vars[&lhs].clone();
let rhs_data = self.vars[&rhs].clone();
let result = match (lhs_data, rhs_data) {
// (x * 1) == (1 * x) == x
(AcirVarData::Const(constant), _) if constant.is_one() => rhs,
(_, AcirVarData::Const(constant)) if constant.is_one() => lhs,

// (x * 0) == (0 * x) == 0
(AcirVarData::Const(constant), _) | (_, AcirVarData::Const(constant))
if constant.is_zero() =>
{
self.add_constant(FieldElement::zero())
}

(AcirVarData::Const(lhs_constant), AcirVarData::Const(rhs_constant)) => {
self.add_data(AcirVarData::Const(lhs_constant * rhs_constant))
self.add_constant(lhs_constant * rhs_constant)
}
(AcirVarData::Witness(witness), AcirVarData::Const(constant))
| (AcirVarData::Const(constant), AcirVarData::Witness(witness)) => {
Expand Down

0 comments on commit e58844d

Please sign in to comment.