Skip to content

Commit 2c175c0

Browse files
authored
feat: Optimize euclidean division acir-gen (#3121)
Co-authored-by: Tom French <[email protected]>
1 parent 8e3fb4a commit 2c175c0

File tree

206 files changed

+24
-3
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

206 files changed

+24
-3
lines changed

compiler/noirc_evaluator/src/ssa/acir_gen/acir_ir/generated_acir.rs

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -440,9 +440,30 @@ impl GeneratedAcir {
440440
//
441441
// When the predicate is 0, the equation always passes.
442442
// When the predicate is 1, the rhs must not be 0.
443-
let rhs_is_zero = self.is_zero(rhs);
444-
let rhs_is_not_zero = self.mul_with_witness(&rhs_is_zero.into(), predicate);
445-
self.assert_is_zero(rhs_is_not_zero);
443+
let rhs_is_nonzero_const = rhs.is_const() && !rhs.is_zero();
444+
if !rhs_is_nonzero_const {
445+
match predicate.to_const() {
446+
Some(predicate) if predicate.is_zero() => {
447+
// If predicate is known to be inactive, we don't need to lay down constraints.
448+
}
449+
450+
Some(predicate) if predicate.is_one() => {
451+
// If the predicate is known to be active, we simply assert that an inverse must exist.
452+
// This implies that `rhs != 0`.
453+
let unsafe_inverse = self.brillig_inverse(rhs.clone());
454+
let rhs_has_inverse =
455+
self.mul_with_witness(rhs, &unsafe_inverse.into()) - FieldElement::one();
456+
self.assert_is_zero(rhs_has_inverse);
457+
}
458+
459+
_ => {
460+
// Otherwise we must handle both potential cases.
461+
let rhs_is_zero = self.is_zero(rhs);
462+
let rhs_is_not_zero = self.mul_with_witness(&rhs_is_zero.into(), predicate);
463+
self.assert_is_zero(rhs_is_not_zero);
464+
}
465+
}
466+
}
446467

447468
// maximum bit size for q and for [r and rhs]
448469
let mut max_q_bits = max_bit_size;
Binary file not shown.
-185 Bytes
Binary file not shown.
-116 Bytes
Binary file not shown.
-123 Bytes
Binary file not shown.
-64 Bytes
Binary file not shown.
-146 Bytes
Binary file not shown.
-106 Bytes
Binary file not shown.
-53 Bytes
Binary file not shown.
-48 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)