Skip to content

Commit

Permalink
feat: allow returning nested arrays from brillig (#2047)
Browse files Browse the repository at this point in the history
* feat: allow returning nested arrays from brillig

* style: remove extraneous println

* style: restore whitespace

* chore: update json and witness

* style: whitespacing

* docs: added doc comment
  • Loading branch information
sirasistant authored Jul 26, 2023
1 parent 5907e96 commit 4378bb8
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 11 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.6.0"

[dependencies]
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
values = [1,2,3,4,5,6]
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
struct Header {
params: [Field; 3],
}

struct MyNote {
plain: Field,
array: [Field; 2],
header: Header,
}

fn access_nested(notes: [MyNote; 2]) -> Field {
notes[0].array[1] + notes[1].array[0] + notes[0].plain + notes[1].header.params[0]
}

unconstrained fn create_inside_brillig(values: [Field; 6]) -> [MyNote; 2] {
let header = Header { params: [values[0], values[1], values[2]]};
let note0 = MyNote { array: [values[0], values[1]], plain : values[2], header };
let note1 = MyNote { array: [values[3], values[4]], plain : values[5], header };
[note0, note1]
}

fn main(values: [Field; 6]) {
let notes = create_inside_brillig(values);
assert(access_nested(notes) == (2 + 4 + 3 + 1));
}

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"values","type":{"kind":"array","length":6,"type":{"kind":"field"}},"visibility":"private"}],"param_witnesses":{"values":[1,2,3,4,5,6]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/92a21LiQBCGh1MiBCOCGE6+Q44Q7niVdRcv9pn3YTYzpsufqSGidrtkp2oqCRO6vz5mjKyUUkP1Om7qo1cfO9XsVrNXzX41B/WaV6/RPXS/gs/0ONTH+Gsj6fDJiqUYuy1g7LWAsd8CxkELGD1GRqz1cX3uq9d+oXvHqJpBvXZbzbCad9WcVPO+mlP1Nv7UR/29rjod2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8HHFeOPSnFw/4h3xsiMvNgk6aV315ETnoBNytJj+zFUwo1XIkgPAnLnii/5peye88fopLlfu09x/AWj8PM1SjMAAA==","proving_key":null,"verification_key":null}
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -855,18 +855,9 @@ impl AcirContext {
AcirValue::Var(var, output.clone())
}
AcirType::Array(element_types, size) => {
let mut witnesses = Vec::new();
let mut array_values = im::Vector::new();
for _ in 0..size {
for element_type in &element_types {
let witness_index = self.acir_ir.next_witness_index();
witnesses.push(witness_index);
let var = self.add_data(AcirVarData::Witness(witness_index));
array_values.push_back(AcirValue::Var(var, element_type.clone()));
}
}
let (acir_value, witnesses) = self.brillig_array_output(&element_types, size);
b_outputs.push(BrilligOutputs::Array(witnesses));
AcirValue::Array(array_values)
acir_value
}
});
let predicate = self.vars[&predicate].to_expression().into_owned();
Expand Down Expand Up @@ -906,6 +897,36 @@ impl AcirContext {
}
}

/// Recursively create acir values for returned arrays. This is necessary because a brillig returned array can have nested arrays as elements.
/// A singular array of witnesses is collected for a top level array, by deflattening the assigned witnesses at each level.
fn brillig_array_output(
&mut self,
element_types: &[AcirType],
size: usize,
) -> (AcirValue, Vec<Witness>) {
let mut witnesses = Vec::new();
let mut array_values = im::Vector::new();
for _ in 0..size {
for element_type in element_types {
match element_type {
AcirType::Array(nested_element_types, nested_size) => {
let (nested_acir_value, mut nested_witnesses) =
self.brillig_array_output(nested_element_types, *nested_size);
witnesses.append(&mut nested_witnesses);
array_values.push_back(nested_acir_value);
}
AcirType::NumericType(_) => {
let witness_index = self.acir_ir.next_witness_index();
witnesses.push(witness_index);
let var = self.add_data(AcirVarData::Witness(witness_index));
array_values.push_back(AcirValue::Var(var, element_type.clone()));
}
}
}
}
(AcirValue::Array(array_values), witnesses)
}

/// Generate output variables that are constrained to be the sorted inputs
/// The outputs are the sorted inputs iff
/// outputs are sorted and
Expand Down

0 comments on commit 4378bb8

Please sign in to comment.