Skip to content

Commit

Permalink
fix: Fix modulo operator for comptime values (#1361)
Browse files Browse the repository at this point in the history
use integer type for modulo simplification
  • Loading branch information
guipublic authored May 17, 2023
1 parent cfbc1f7 commit ba15d6d
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 1 deletion.
1 change: 1 addition & 0 deletions crates/nargo_cli/tests/test_data/2_div/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ fn main(mut x: u32, y: u32, z: u32) {
let a = x % y;
assert(x / y == z);
assert(a == x - z*y);
assert((50 as u64) % (9 as u64) == 5);
}
5 changes: 4 additions & 1 deletion crates/noirc_evaluator/src/ssa/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -922,7 +922,10 @@ impl Binary {
} else if l_is_zero {
return Ok(l_eval); //TODO what is the correct result?
} else if let (Some(lhs), Some(rhs)) = (lhs, rhs) {
return Ok(NodeEval::Const(lhs - rhs * (lhs / rhs), res_type));
let lhs = res_type.field_to_type(lhs).to_u128();
let rhs = res_type.field_to_type(rhs).to_u128();
let result = lhs - rhs * (lhs / rhs);
return Ok(NodeEval::Const(FieldElement::from(result), res_type));
}
}
BinaryOp::Srem(loc) => {
Expand Down

0 comments on commit ba15d6d

Please sign in to comment.