From ba15d6d654739cc710e147dc08d94dcfe9dedb2a Mon Sep 17 00:00:00 2001 From: guipublic <47281315+guipublic@users.noreply.github.com> Date: Thu, 18 May 2023 01:33:54 +0200 Subject: [PATCH] fix: Fix modulo operator for comptime values (#1361) use integer type for modulo simplification --- crates/nargo_cli/tests/test_data/2_div/src/main.nr | 1 + crates/noirc_evaluator/src/ssa/node.rs | 5 ++++- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/crates/nargo_cli/tests/test_data/2_div/src/main.nr b/crates/nargo_cli/tests/test_data/2_div/src/main.nr index 00608cb697d..ff0dee755cc 100644 --- a/crates/nargo_cli/tests/test_data/2_div/src/main.nr +++ b/crates/nargo_cli/tests/test_data/2_div/src/main.nr @@ -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); } diff --git a/crates/noirc_evaluator/src/ssa/node.rs b/crates/noirc_evaluator/src/ssa/node.rs index 4566d974813..b964743b370 100644 --- a/crates/noirc_evaluator/src/ssa/node.rs +++ b/crates/noirc_evaluator/src/ssa/node.rs @@ -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) => {