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: evaluate constant division #909

Merged
merged 1 commit into from
Feb 24, 2023
Merged
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
29 changes: 18 additions & 11 deletions crates/noirc_evaluator/src/ssa/acir_gen/operations/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,17 +145,24 @@ pub(crate) fn evaluate(
let l_c = var_cache.get_or_compute_internal_var_unwrap(binary.lhs, evaluator, ctx);
let mut r_c = var_cache.get_or_compute_internal_var_unwrap(binary.rhs, evaluator, ctx);
let predicate = get_predicate(var_cache,binary, evaluator, ctx).expression().clone();
//TODO avoid creating witnesses here.
let x_witness = r_c.get_or_compute_witness(evaluator, false).expect("unexpected constant expression");

let inverse = Expression::from(&constraints::evaluate_inverse(
x_witness, &predicate, evaluator,
));
InternalVar::from(constraints::mul_with_witness(
evaluator,
l_c.expression(),
&inverse,
))
if let Some(r_value) = r_c.to_const() {
if r_value.is_zero() {
panic!("Panic - division by zero");
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
} else {
constraints::add(&Expression::zero(), r_value.inverse(), l_c.expression()).into()
kevaundray marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
//TODO avoid creating witnesses here.
let x_witness = r_c.get_or_compute_witness(evaluator, false).expect("unexpected constant expression");
let inverse = Expression::from(&constraints::evaluate_inverse(
x_witness, &predicate, evaluator,
));
InternalVar::from(constraints::mul_with_witness(
evaluator,
l_c.expression(),
&inverse,
))
}
}
BinaryOp::Eq => {
let l_c = var_cache.get_or_compute_internal_var(binary.lhs, evaluator, ctx);
Expand Down