Skip to content

Commit

Permalink
fix: handle struct with nested arrays in oracle return values (#5244)
Browse files Browse the repository at this point in the history
# Description

## Problem\*

Resolves #5238

## Summary\*
Use nested values when the sizes do not match


## Additional Context



## 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
guipublic and TomAFrench authored Jun 20, 2024
1 parent 508e677 commit a30814f
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
9 changes: 7 additions & 2 deletions acvm-repo/brillig_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -500,9 +500,14 @@ impl<'a, F: AcirField, B: BlackBoxFunctionSolver<F>> VM<'a, F, B> {
match output {
ForeignCallParam::Array(values) => {
if values.len() != *size {
return Err("Foreign call result array doesn't match expected size".to_string());
// foreign call returning flattened values into a nested type, so the sizes do not match
let destination = self.memory.read_ref(*pointer_index);
let return_type = value_type;
let mut flatten_values_idx = 0; //index of values read from flatten_values
self.write_slice_of_values_to_memory(destination, &output.fields(), &mut flatten_values_idx, return_type)?;
} else {
self.write_values_to_memory_slice(*pointer_index, values, value_types)?;
}
self.write_values_to_memory_slice(*pointer_index, values, value_types)?;
}
_ => {
return Err("Function result size does not match brillig bytecode size".to_string());
Expand Down
34 changes: 34 additions & 0 deletions test_programs/noir_test_success/regression_4561/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,37 @@ fn two_nested_return() {
OracleMock::mock("two_nested_return").returns((0, [1, 2, 3, 4, 5, 6], 7, [1, 2, 3, 4, 5, 6]));
assert_eq(two_nested_return_unconstrained(), (0, [[1, 2, 3], [4, 5, 6]], 7, [[1, 2, 3], [4, 5, 6]]));
}

#[oracle(foo_return)]
unconstrained fn foo_return() -> (Field, TReturn, TestTypeFoo) {}
unconstrained fn foo_return_unconstrained() -> (Field, TReturn, TestTypeFoo) {
foo_return()
}

struct TestTypeFoo {
a: Field,
b: [[[Field; 3]; 4]; 2],
c: [TReturnElem; 2],
d: TReturnElem,
}

#[test]
fn complexe_struct_return() {
OracleMock::mock("foo_return").returns(
(
0, [1, 2, 3, 4, 5, 6], 7, [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24], [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12], [1, 2, 3, 4, 5, 6]
)
);
let foo_x = foo_return_unconstrained();
assert_eq((foo_x.0, foo_x.1), (0, [[1, 2, 3], [4, 5, 6]]));
assert_eq(foo_x.2.a, 7);
assert_eq(
foo_x.2.b, [
[[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]], [[13, 14, 15], [16, 17, 18], [19, 20, 21], [22, 23, 24]]
]
);
let a: TReturnElem = [1, 2, 3];
let b: TReturnElem = [4, 5, 6];
assert_eq(foo_x.2.c, [a, b]);
assert_eq(foo_x.2.d, a);
}

0 comments on commit a30814f

Please sign in to comment.