From 855ef52cb2aba94774773ea342b21d221ecef0c3 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Wed, 15 Nov 2023 08:02:21 +0000 Subject: [PATCH 1/2] fix: apply predicate to over/underflow checks --- compiler/noirc_evaluator/src/ssa/ir/printer.rs | 2 +- .../noirc_evaluator/src/ssa/opt/flatten_cfg.rs | 16 ++++++++++++++++ .../conditional_regression_underflow/Nargo.toml | 6 ++++++ .../conditional_regression_underflow/Prover.toml | 1 + .../conditional_regression_underflow/src/main.nr | 6 ++++++ 5 files changed, 30 insertions(+), 1 deletion(-) create mode 100644 tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Nargo.toml create mode 100644 tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Prover.toml create mode 100644 tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr diff --git a/compiler/noirc_evaluator/src/ssa/ir/printer.rs b/compiler/noirc_evaluator/src/ssa/ir/printer.rs index 51e436643ab..c6b1f3c7528 100644 --- a/compiler/noirc_evaluator/src/ssa/ir/printer.rs +++ b/compiler/noirc_evaluator/src/ssa/ir/printer.rs @@ -173,7 +173,7 @@ pub(crate) fn display_instruction( ) } Instruction::RangeCheck { value, max_bit_size, .. } => { - write!(f, "range_check {} to {} bits", show(*value), *max_bit_size,) + writeln!(f, "range_check {} to {} bits", show(*value), *max_bit_size,) } } } diff --git a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs index f35e8023584..d2ed21c60d7 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg.rs @@ -662,6 +662,22 @@ impl<'f> Context<'f> { self.remember_store(address, value); Instruction::Store { address, value } } + Instruction::RangeCheck { value, max_bit_size, assert_message } => { + // Replace value with `value * predicate` to zero out value when predicate is inactive. + + // Condition needs to be cast to argument type in order to multiply them together. + let argument_type = self.inserter.function.dfg.type_of_value(value); + let casted_condition = self.insert_instruction( + Instruction::Cast(condition, argument_type), + call_stack.clone(), + ); + + let value = self.insert_instruction( + Instruction::binary(BinaryOp::Mul, value, casted_condition), + call_stack.clone(), + ); + Instruction::RangeCheck { value, max_bit_size, assert_message } + } other => other, } } else { diff --git a/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Nargo.toml b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Nargo.toml new file mode 100644 index 00000000000..54a082081f7 --- /dev/null +++ b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Nargo.toml @@ -0,0 +1,6 @@ +[package] +name = "conditional_underflow" +type = "bin" +authors = [""] + +[dependencies] diff --git a/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Prover.toml b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Prover.toml new file mode 100644 index 00000000000..ca65e8b9428 --- /dev/null +++ b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/Prover.toml @@ -0,0 +1 @@ +x = "4" \ No newline at end of file diff --git a/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr new file mode 100644 index 00000000000..5b35d854b9e --- /dev/null +++ b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr @@ -0,0 +1,6 @@ +// Regression test for https://github.com/noir-lang/noir/issues/3493 +fn main(x: u4) { + if x == 10 { + x + 15; + } +} \ No newline at end of file From 8540b55c9ed2fc9f183ec6b070dc3511ef0bd0a2 Mon Sep 17 00:00:00 2001 From: TomAFrench Date: Wed, 15 Nov 2023 08:14:47 +0000 Subject: [PATCH 2/2] chore: cover all affected instructions in regression test --- .../conditional_regression_underflow/src/main.nr | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr index 5b35d854b9e..be70b67be5e 100644 --- a/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr +++ b/tooling/nargo_cli/tests/execution_success/conditional_regression_underflow/src/main.nr @@ -3,4 +3,13 @@ fn main(x: u4) { if x == 10 { x + 15; } + if x == 9 { + x << 3; + } + if x == 8 { + x * 3; + } + if x == 7 { + x - 8; + } } \ No newline at end of file