From b87654e2b4761dfacc916dac70d43c1b572ec636 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Wed, 27 Mar 2024 17:46:29 +0000 Subject: [PATCH] fix(ssa): Accurate constant type for slice dummy data in flattening (#4661) # Description ## Problem\* Resolves ## Summary\* We were not accurately constructing the type for slice dummy data. We were always making slice dummy data a `Field` but this could lead to errors when eventually merging this value with a different numeric type. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [ ] I have tested the changes locally. - [ ] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --- .../src/ssa/opt/flatten_cfg/value_merger.rs | 4 ++-- .../regression_sha256_slice/Nargo.toml | 7 +++++++ .../regression_sha256_slice/Prover.toml | 1 + .../regression_sha256_slice/src/main.nr | 12 ++++++++++++ 4 files changed, 22 insertions(+), 2 deletions(-) create mode 100644 test_programs/execution_success/regression_sha256_slice/Nargo.toml create mode 100644 test_programs/execution_success/regression_sha256_slice/Prover.toml create mode 100644 test_programs/execution_success/regression_sha256_slice/src/main.nr diff --git a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs index 6b923a2e42d..0a351148fa3 100644 --- a/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs +++ b/compiler/noirc_evaluator/src/ssa/opt/flatten_cfg/value_merger.rs @@ -234,9 +234,9 @@ impl<'a> ValueMerger<'a> { /// such as with dynamic indexing of non-homogenous slices. fn make_slice_dummy_data(&mut self, typ: &Type) -> ValueId { match typ { - Type::Numeric(_) => { + Type::Numeric(numeric_type) => { let zero = FieldElement::zero(); - self.dfg.make_constant(zero, Type::field()) + self.dfg.make_constant(zero, Type::Numeric(*numeric_type)) } Type::Array(element_types, len) => { let mut array = im::Vector::new(); diff --git a/test_programs/execution_success/regression_sha256_slice/Nargo.toml b/test_programs/execution_success/regression_sha256_slice/Nargo.toml new file mode 100644 index 00000000000..759c3b20ba8 --- /dev/null +++ b/test_programs/execution_success/regression_sha256_slice/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "regression_sha256_slice" +type = "bin" +authors = [""] +compiler_version = ">=0.26.0" + +[dependencies] \ No newline at end of file diff --git a/test_programs/execution_success/regression_sha256_slice/Prover.toml b/test_programs/execution_success/regression_sha256_slice/Prover.toml new file mode 100644 index 00000000000..8a027e9eca9 --- /dev/null +++ b/test_programs/execution_success/regression_sha256_slice/Prover.toml @@ -0,0 +1 @@ +x = ["5", "10"] diff --git a/test_programs/execution_success/regression_sha256_slice/src/main.nr b/test_programs/execution_success/regression_sha256_slice/src/main.nr new file mode 100644 index 00000000000..60b0911cf09 --- /dev/null +++ b/test_programs/execution_success/regression_sha256_slice/src/main.nr @@ -0,0 +1,12 @@ +use dep::std; + +fn main(x: [u8; 2]) { + let mut y = x.as_slice(); + let digest1 = std::hash::sha256_slice(y); + let mut v = y; + if x[0] != 0 { + v = y.push_back(x[0]); + } + let digest2 = std::hash::sha256_slice(v); + assert(digest1 != digest2); +}