-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: set index and value to 0 for array_get with predicate (#4971)
# Description ## Problem\* Resolves issue on noir-protocol-circuit and should also resolve issues described in #4716. The issue it resolves is that array_get under a predicate are done at index 0 if the predicate is false, but since arrays are not homogenous, it may contain some value that overflow the type of the array_get. ## Summary\* When getting value from an array_get, I multiply it with the predicate to avoid any possible overflow if element at index 0 has not the same type as the expected one. ## Additional Context If the array is simple (not nested), I get the offset to a compatible type and use it when computing the predicate_index. If the first element of the array has compatible size, then the predicate_index will be correct If not, we fallback to the multiplication of the value with the predicate. ## Documentation\* Check one: - [X] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[For Experimental Features]** Documentation to be submitted in a separate PR. # PR Checklist\* - [X] I have tested the changes locally. - [X] I have formatted the changes with [Prettier](https://prettier.io/) and/or `cargo fmt` on default settings. --------- Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
f3f1150
commit c49d3a9
Showing
5 changed files
with
132 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
test_programs/execution_success/regression_struct_array_conditional/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
[package] | ||
name = "regression_struct_array_conditional" | ||
version = "0.1.0" | ||
type = "bin" | ||
authors = [""] | ||
|
||
[dependencies] |
18 changes: 18 additions & 0 deletions
18
test_programs/execution_success/regression_struct_array_conditional/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
y = 1 | ||
z = 1 | ||
|
||
[[x]] | ||
value = "0x23de33be058ce5504e1ade738db8bdacfe268fa9dbde777092bf1d38519bdf59" | ||
counter = "10" | ||
dummy = "0" | ||
|
||
[[x]] | ||
value = "3" | ||
counter = "2" | ||
dummy = "0" | ||
|
||
[[x]] | ||
value = "2" | ||
counter = "0" | ||
dummy = "0" | ||
|
38 changes: 38 additions & 0 deletions
38
test_programs/execution_success/regression_struct_array_conditional/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
struct foo { | ||
value: Field, | ||
counter: u8, | ||
dummy: u8, | ||
} | ||
struct bar { | ||
dummy: [u8;3], | ||
value: Field, | ||
counter: u8, | ||
} | ||
struct bar_field { | ||
dummy: [Field;3], | ||
value: Field, | ||
counter: u8, | ||
} | ||
fn main(x: [foo; 3], y: u32, z: u32) -> pub u8 { | ||
let a = [y, z, x[y].counter as u32]; | ||
let mut b = [bar { value: 0, counter: 0, dummy: [0; 3] }; 3]; | ||
let mut c = [bar_field { value: 0, counter: 0, dummy: [0; 3] }; 3]; | ||
for i in 0..3 { | ||
b[i].value = x[i].value; | ||
b[i].counter = x[i].counter; | ||
b[i].dummy[0] = x[i].dummy; | ||
c[i].value = x[i].value; | ||
c[i].counter = x[i].counter; | ||
c[i].dummy[0] = x[i].dummy as Field; | ||
} | ||
if z == 0 { | ||
// offset | ||
assert(y as u8 < x[y].counter); | ||
assert(y <= a[y]); | ||
// first element is compatible | ||
assert(y as u8 < b[y].counter); | ||
// fallback | ||
assert(y as u8 < c[y].counter); | ||
} | ||
x[0].counter | ||
} |