Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Allow arrays of arbitrary types in the program ABI #1651

Merged
merged 12 commits into from
Jul 11, 2023
Merged
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[package]
authors = [""]
compiler_version = "0.6.0"

[dependencies]
11 changes: 11 additions & 0 deletions crates/nargo_cli/tests/test_data/struct_array_inputs/Prover.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
[[foos]]
bar = 0
baz = 0

[[foos]]
bar = 0
baz = 0

[[foos]]
bar = 1
baz = 2
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
struct Foo {
bar: Field,
baz: Field,
}

fn main(foos: [Foo; 3]) -> pub Field {
foos[2].bar + foos[2].baz
}
54 changes: 28 additions & 26 deletions crates/noirc_abi/src/input_parser/json.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{parse_str_to_field, InputValue};
use crate::{errors::InputParserError, Abi, AbiType, MAIN_RETURN_NAME};
use acvm::FieldElement;
use iter_extended::{btree_map, try_btree_map, try_vecmap, vecmap};
use iter_extended::{btree_map, try_btree_map, try_vecmap};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -61,12 +61,8 @@ enum JsonTypes {
Integer(u64),
// Simple boolean flag
Bool(bool),
// Array of regular integers
ArrayNum(Vec<u64>),
// Array of hexadecimal integers
ArrayString(Vec<String>),
// Array of booleans
ArrayBool(Vec<bool>),
// Array of JsonTypes
Array(Vec<JsonTypes>),
// Struct of JsonTypes
Table(BTreeMap<String, JsonTypes>),
}
Expand All @@ -79,8 +75,18 @@ impl From<InputValue> for JsonTypes {
JsonTypes::String(f_str)
}
InputValue::Vec(v) => {
let array = v.iter().map(|i| format!("0x{}", i.to_hex())).collect();
JsonTypes::ArrayString(array)
let array = v
.iter()
.map(|i| match i {
InputValue::Field(field) => {
JsonTypes::String(format!("0x{}", field.to_hex()))
}
_ => unreachable!(
"Only arrays of simple field elements are allowable currently"
),
})
.collect();
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
JsonTypes::Array(array)
}
InputValue::String(s) => JsonTypes::String(s),
InputValue::Struct(map) => {
Expand Down Expand Up @@ -115,23 +121,19 @@ impl InputValue {
InputValue::Field(new_value)
}
JsonTypes::Bool(boolean) => InputValue::Field(boolean.into()),
JsonTypes::ArrayNum(arr_num) => {
let array_elements =
vecmap(arr_num, |elem_num| FieldElement::from(i128::from(elem_num)));

InputValue::Vec(array_elements)
}
JsonTypes::ArrayString(arr_str) => {
let array_elements = try_vecmap(arr_str, |elem_str| parse_str_to_field(&elem_str))?;

InputValue::Vec(array_elements)
}
JsonTypes::ArrayBool(arr_bool) => {
let array_elements = vecmap(arr_bool, FieldElement::from);

InputValue::Vec(array_elements)
}

JsonTypes::Array(array) => match param_type {
AbiType::Array { typ, .. }
if matches!(
typ.as_ref(),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean
) =>
{
let array_elements =
try_vecmap(array, |value| InputValue::try_from_json(value, typ, arg_name))?;
InputValue::Vec(array_elements)
}
_ => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
},
JsonTypes::Table(table) => match param_type {
AbiType::Struct { fields } => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
Expand Down
18 changes: 11 additions & 7 deletions crates/noirc_abi/src/input_parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ use crate::{Abi, AbiType};
#[derive(Debug, Clone, Serialize, PartialEq)]
pub enum InputValue {
Field(FieldElement),
Vec(Vec<FieldElement>),
String(String),
Vec(Vec<InputValue>),
Struct(BTreeMap<String, InputValue>),
}

Expand All @@ -32,14 +32,12 @@ impl InputValue {
field_element.is_one() || field_element.is_zero()
}

(InputValue::Vec(field_elements), AbiType::Array { length, typ, .. }) => {
if field_elements.len() != *length as usize {
(InputValue::Vec(array_elements), AbiType::Array { length, typ, .. }) => {
if array_elements.len() != *length as usize {
return false;
}
// Check that all of the array's elements' values match the ABI as well.
field_elements
.iter()
.all(|field_element| Self::Field(*field_element).matches_abi(typ))
array_elements.iter().all(|input_value| input_value.matches_abi(typ))
}

(InputValue::String(string), AbiType::String { length }) => {
Expand Down Expand Up @@ -163,7 +161,13 @@ mod serialization_tests {
"bar".into(),
InputValue::Struct(BTreeMap::from([
("field1".into(), InputValue::Field(255u128.into())),
("field2".into(), InputValue::Vec(vec![true.into(), false.into()])),
(
"field2".into(),
InputValue::Vec(vec![
InputValue::Field(true.into()),
InputValue::Field(false.into()),
]),
),
])),
),
(MAIN_RETURN_NAME.into(), InputValue::String("hello".to_owned())),
Expand Down
54 changes: 28 additions & 26 deletions crates/noirc_abi/src/input_parser/toml.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use super::{parse_str_to_field, InputValue};
use crate::{errors::InputParserError, Abi, AbiType, MAIN_RETURN_NAME};
use acvm::FieldElement;
use iter_extended::{btree_map, try_btree_map, try_vecmap, vecmap};
use iter_extended::{btree_map, try_btree_map, try_vecmap};
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;

Expand Down Expand Up @@ -58,12 +58,8 @@ enum TomlTypes {
Integer(u64),
// Simple boolean flag
Bool(bool),
// Array of regular integers
ArrayNum(Vec<u64>),
// Array of hexadecimal integers
ArrayString(Vec<String>),
// Array of booleans
ArrayBool(Vec<bool>),
// Array of TomlTypes
Array(Vec<TomlTypes>),
// Struct of TomlTypes
Table(BTreeMap<String, TomlTypes>),
}
Expand All @@ -76,8 +72,18 @@ impl From<InputValue> for TomlTypes {
TomlTypes::String(f_str)
}
InputValue::Vec(v) => {
let array = v.iter().map(|i| format!("0x{}", i.to_hex())).collect();
TomlTypes::ArrayString(array)
let array = v
.iter()
.map(|i| match i {
InputValue::Field(field) => {
TomlTypes::String(format!("0x{}", field.to_hex()))
}
_ => unreachable!(
"Only arrays of simple field elements are allowable currently"
),
})
.collect();
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
TomlTypes::Array(array)
}
InputValue::String(s) => TomlTypes::String(s),
InputValue::Struct(map) => {
Expand Down Expand Up @@ -112,23 +118,19 @@ impl InputValue {
InputValue::Field(new_value)
}
TomlTypes::Bool(boolean) => InputValue::Field(boolean.into()),
TomlTypes::ArrayNum(arr_num) => {
let array_elements =
vecmap(arr_num, |elem_num| FieldElement::from(i128::from(elem_num)));

InputValue::Vec(array_elements)
}
TomlTypes::ArrayString(arr_str) => {
let array_elements = try_vecmap(arr_str, |elem_str| parse_str_to_field(&elem_str))?;

InputValue::Vec(array_elements)
}
TomlTypes::ArrayBool(arr_bool) => {
let array_elements = vecmap(arr_bool, FieldElement::from);

InputValue::Vec(array_elements)
}

TomlTypes::Array(array) => match param_type {
AbiType::Array { typ, .. }
if matches!(
typ.as_ref(),
AbiType::Field | AbiType::Integer { .. } | AbiType::Boolean
) =>
TomAFrench marked this conversation as resolved.
Show resolved Hide resolved
{
let array_elements =
try_vecmap(array, |value| InputValue::try_from_toml(value, typ, arg_name))?;
InputValue::Vec(array_elements)
}
_ => return Err(InputParserError::AbiTypeMismatch(param_type.clone())),
},
TomlTypes::Table(table) => match param_type {
AbiType::Struct { fields } => {
let native_table = try_btree_map(fields, |(field_name, abi_type)| {
Expand Down
28 changes: 22 additions & 6 deletions crates/noirc_abi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,14 @@ impl Abi {
let mut encoded_value = Vec::new();
match value {
InputValue::Field(elem) => encoded_value.push(elem),
InputValue::Vec(vec_elem) => encoded_value.extend(vec_elem),
InputValue::Vec(vec_elements) => match abi_type {
AbiType::Array { typ, .. } => {
for elem in vec_elements {
encoded_value.extend(Self::encode_value(elem, typ)?);
}
}
_ => unreachable!("value should have already been checked to match abi type"),
},
InputValue::String(string) => {
let str_as_fields =
string.bytes().map(|byte| FieldElement::from_be_bytes_reduce(&[byte]));
Expand Down Expand Up @@ -383,11 +390,14 @@ impl Abi {

InputValue::Field(field_element)
}
AbiType::Array { length, .. } => {
let field_elements: Vec<FieldElement> =
field_iterator.take(*length as usize).collect();
AbiType::Array { length, typ } => {
let length = *length as usize;
let mut array_elements = Vec::with_capacity(length);
for _ in 0..length {
array_elements.push(Self::decode_value(field_iterator, typ)?);
}

InputValue::Vec(field_elements)
InputValue::Vec(array_elements)
}
AbiType::String { length } => {
let field_elements: Vec<FieldElement> =
Expand Down Expand Up @@ -458,7 +468,13 @@ mod test {

// Note we omit return value from inputs
let inputs: InputMap = BTreeMap::from([
("thing1".to_string(), InputValue::Vec(vec![FieldElement::one(), FieldElement::one()])),
(
"thing1".to_string(),
InputValue::Vec(vec![
InputValue::Field(FieldElement::one()),
InputValue::Field(FieldElement::one()),
]),
),
("thing2".to_string(), InputValue::Field(FieldElement::zero())),
]);

Expand Down