-
Notifications
You must be signed in to change notification settings - Fork 199
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(acir): Use types on dynamic arrays (#4364)
# Description ## Problem\* Resolves #4356 Supercedes #4360 ## Summary\* An ACIR dynamic array is a pointer to flat memory. We have been treating this flat memory as a list of fields, however, this breaks if we do in fact need accurate numeric type information such as when working black box function inputs. For example for hash inputs we set up the byte array based upon the bit size. This needs to be the correct bit size or else we will get a lot of extra garbage when calling `fetch_nearest_bytes` on a FieldElement. This PR attaches a list of `Vec<NumericType>` to the `AcirDynamicArray` structure. This gives us the expected output result for `sha` then. We probably could restrict the `AcirDynamicArray` to be created only through a constructor where we check that the `value_types` match the supplied len in size. I left it for a follow-up as this is a quick fix but I can do it as part of this PR. ## Additional Context ## Documentation\* Check one: - [ ] No documentation needed. - [ ] Documentation included in this PR. - [ ] **[Exceptional Case]** 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. --------- Co-authored-by: TomAFrench <tom@tomfren.ch>
- Loading branch information
1 parent
dcd7a1e
commit ba2c541
Showing
8 changed files
with
177 additions
and
15 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/array_dynamic_blackbox_input/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 = "array_dynamic_blackbox_input" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.24.0" | ||
|
||
[dependencies] |
4 changes: 4 additions & 0 deletions
4
test_programs/execution_success/array_dynamic_blackbox_input/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,4 @@ | ||
index = "1" | ||
leaf = ["51", "109", "224", "175", "60", "42", "79", "222", "117", "255", "174", "79", "126", "242", "74", "34", "100", "35", "20", "200", "109", "89", "191", "219", "41", "10", "118", "217", "165", "224", "215", "109"] | ||
path = ["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63"] | ||
root = [79, 230, 126, 184, 98, 125, 226, 58, 117, 45, 140, 15, 72, 118, 89, 173, 117, 161, 166, 0, 214, 125, 13, 16, 113, 81, 173, 156, 97, 15, 57, 216] |
27 changes: 27 additions & 0 deletions
27
test_programs/execution_success/array_dynamic_blackbox_input/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,27 @@ | ||
fn main(leaf: [u8; 32], path: [u8; 64], index: u32, root: [u8; 32]) { | ||
compute_root(leaf, path, index, root); | ||
} | ||
|
||
fn compute_root(leaf: [u8; 32], path: [u8; 64], _index: u32, root: [u8; 32]) { | ||
let mut current = leaf; | ||
let mut index = _index; | ||
|
||
for i in 0..2 { | ||
let mut hash_input = [0; 64]; | ||
let offset = i * 32; | ||
let is_right = (index & 1) != 0; | ||
let a = if is_right { 32 } else { 0 }; | ||
let b = if is_right { 0 } else { 32 }; | ||
|
||
for j in 0..32 { | ||
hash_input[j + a] = current[j]; | ||
hash_input[j + b] = path[offset + j]; | ||
} | ||
|
||
current = dep::std::hash::sha256(hash_input); | ||
index = index >> 1; | ||
} | ||
|
||
// Regression for issue #4258 | ||
assert(root == current); | ||
} |
7 changes: 7 additions & 0 deletions
7
test_programs/execution_success/array_dynamic_nested_blackbox_input/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 = "array_dynamic_nested_blackbox_input" | ||
type = "bin" | ||
authors = [""] | ||
compiler_version = ">=0.24.0" | ||
|
||
[dependencies] |
23 changes: 23 additions & 0 deletions
23
test_programs/execution_success/array_dynamic_nested_blackbox_input/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,23 @@ | ||
y = "3" | ||
hash_result = [50, 53, 90, 252, 105, 236, 223, 30, 135, 229, 193, 172, 51, 139, 8, 32, 188, 104, 151, 115, 129, 168, 27, 71, 203, 47, 40, 228, 89, 177, 129, 100] | ||
|
||
[[x]] | ||
a = "1" | ||
b = ["2", "3", "20"] | ||
|
||
[x.bar] | ||
inner = ["100", "101", "102"] | ||
|
||
[[x]] | ||
a = "4" # idx = 3, flattened start idx = 7 | ||
b = ["5", "6", "21"] # idx = 4, flattened start idx = 8 | ||
|
||
[x.bar] | ||
inner = ["103", "104", "105"] # idx = 5, flattened start idx = 11 | ||
|
||
[[x]] | ||
a = "7" | ||
b = ["8", "9", "22"] | ||
|
||
[x.bar] | ||
inner = ["106", "107", "108"] |
20 changes: 20 additions & 0 deletions
20
test_programs/execution_success/array_dynamic_nested_blackbox_input/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,20 @@ | ||
struct Bar { | ||
inner: [u8; 3], | ||
} | ||
|
||
struct Foo { | ||
a: Field, | ||
b: [Field; 3], | ||
bar: Bar, | ||
} | ||
|
||
fn main(mut x: [Foo; 3], y: pub Field, hash_result: pub [u8; 32]) { | ||
// Simple dynamic array set for entire inner most array | ||
x[y - 1].bar.inner = [106, 107, 10]; | ||
let mut hash_input = x[y - 1].bar.inner; | ||
// Make sure that we are passing a dynamic array to the black box function call | ||
// by setting the array using a dynamic index here | ||
hash_input[y - 1] = 0; | ||
let hash = dep::std::hash::sha256(hash_input); | ||
assert_eq(hash, hash_result); | ||
} |