From d8bd78f60c447bb8488a844d779e8aaf4150afe7 Mon Sep 17 00:00:00 2001 From: Maxim Vezenov Date: Sat, 30 Sep 2023 00:29:40 +0100 Subject: [PATCH] feat(abi): Tuples as inputs/outputs to main (#2899) Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> Co-authored-by: Tom French --- .../execution_success/tuple_inputs/Nargo.toml | 7 ++++ .../tuple_inputs/Prover.toml | 12 ++++++ .../tuple_inputs/src/main.nr | 37 +++++++++++++++++++ tooling/noirc_abi/src/input_parser/json.rs | 14 +++++++ tooling/noirc_abi/src/input_parser/mod.rs | 11 ++++++ tooling/noirc_abi/src/input_parser/toml.rs | 14 +++++++ tooling/noirc_abi/src/lib.rs | 24 +++++++++++- tooling/noirc_abi_wasm/src/temp.rs | 14 +++++++ 8 files changed, 132 insertions(+), 1 deletion(-) create mode 100644 tooling/nargo_cli/tests/execution_success/tuple_inputs/Nargo.toml create mode 100644 tooling/nargo_cli/tests/execution_success/tuple_inputs/Prover.toml create mode 100644 tooling/nargo_cli/tests/execution_success/tuple_inputs/src/main.nr diff --git a/tooling/nargo_cli/tests/execution_success/tuple_inputs/Nargo.toml b/tooling/nargo_cli/tests/execution_success/tuple_inputs/Nargo.toml new file mode 100644 index 00000000000..7cc7bc85393 --- /dev/null +++ b/tooling/nargo_cli/tests/execution_success/tuple_inputs/Nargo.toml @@ -0,0 +1,7 @@ +[package] +name = "tuple_inputs" +type = "bin" +authors = [""] +compiler_version = "0.14.1" + +[dependencies] \ No newline at end of file diff --git a/tooling/nargo_cli/tests/execution_success/tuple_inputs/Prover.toml b/tooling/nargo_cli/tests/execution_success/tuple_inputs/Prover.toml new file mode 100644 index 00000000000..43d62a7a15b --- /dev/null +++ b/tooling/nargo_cli/tests/execution_success/tuple_inputs/Prover.toml @@ -0,0 +1,12 @@ +pair = [1, 0] +x = [[0, 1, 2], [3, 4, 5]] + +[[struct_pair]] +a = "1" +b = ["2", "3", "20"] + +[struct_pair.bar] +inner = ["100", "101", "102"] + +[[struct_pair]] +inner = ["103", "104", "105"] diff --git a/tooling/nargo_cli/tests/execution_success/tuple_inputs/src/main.nr b/tooling/nargo_cli/tests/execution_success/tuple_inputs/src/main.nr new file mode 100644 index 00000000000..37160d5a0e6 --- /dev/null +++ b/tooling/nargo_cli/tests/execution_success/tuple_inputs/src/main.nr @@ -0,0 +1,37 @@ +struct Bar { + inner: [Field; 3], +} + +struct Foo { + a: Field, + b: [Field; 3], + bar: Bar, +} + +fn main(pair : (Field, Field), x: [(u8, u8, u8); 2], struct_pair: (Foo, Bar)) -> pub (Field, u8) { + assert(pair.0 == 1); + assert(pair.1 == 0); + + let mut start_val = 0; + for i in 0..2 { + assert(x[i].0 == start_val); + assert(x[i].1 == start_val + 1); + assert(x[i].2 == start_val + 2); + start_val += 3; + } + + assert(struct_pair.0.a == 1); + assert(struct_pair.0.b == [2, 3, 20]); + assert(struct_pair.0.bar.inner == [100, 101, 102]); + assert(struct_pair.1.inner == [103, 104, 105]); + + let (u, v) = if pair.0 as u32 < 1 { + (pair.0, pair.0 + 1) + } else { + (pair.0 + 1, pair.0) + }; + assert(u == pair.0 + 1); + assert(v == pair.0); + + (u, v as u8) +} diff --git a/tooling/noirc_abi/src/input_parser/json.rs b/tooling/noirc_abi/src/input_parser/json.rs index c6f1e304728..518685b7cf3 100644 --- a/tooling/noirc_abi/src/input_parser/json.rs +++ b/tooling/noirc_abi/src/input_parser/json.rs @@ -104,6 +104,13 @@ impl JsonTypes { JsonTypes::Table(map_with_json_types) } + (InputValue::Vec(vector), AbiType::Tuple { fields }) => { + let fields = try_vecmap(vector.iter().zip(fields), |(value, typ)| { + JsonTypes::try_from_input_value(value, typ) + })?; + JsonTypes::Array(fields) + } + _ => return Err(InputParserError::AbiTypeMismatch(abi_type.clone())), }; Ok(json_value) @@ -169,6 +176,13 @@ impl InputValue { InputValue::Struct(native_table) } + (JsonTypes::Array(array), AbiType::Tuple { fields }) => { + let tuple_fields = try_vecmap(array.into_iter().zip(fields), |(value, typ)| { + InputValue::try_from_json(value, typ, arg_name) + })?; + InputValue::Vec(tuple_fields) + } + (_, _) => return Err(InputParserError::AbiTypeMismatch(param_type.clone())), }; diff --git a/tooling/noirc_abi/src/input_parser/mod.rs b/tooling/noirc_abi/src/input_parser/mod.rs index fc6cd4b3b30..e26cfecc389 100644 --- a/tooling/noirc_abi/src/input_parser/mod.rs +++ b/tooling/noirc_abi/src/input_parser/mod.rs @@ -64,6 +64,17 @@ impl InputValue { }) } + (InputValue::Vec(vec_elements), AbiType::Tuple { fields }) => { + if vec_elements.len() != fields.len() { + return false; + } + + vec_elements + .iter() + .zip(fields) + .all(|(input_value, abi_param)| input_value.matches_abi(abi_param)) + } + // All other InputValue-AbiType combinations are fundamentally incompatible. _ => false, } diff --git a/tooling/noirc_abi/src/input_parser/toml.rs b/tooling/noirc_abi/src/input_parser/toml.rs index dc47cbcda37..645b59b00cd 100644 --- a/tooling/noirc_abi/src/input_parser/toml.rs +++ b/tooling/noirc_abi/src/input_parser/toml.rs @@ -102,6 +102,13 @@ impl TomlTypes { TomlTypes::Table(map_with_toml_types) } + (InputValue::Vec(vector), AbiType::Tuple { fields }) => { + let fields = try_vecmap(vector.iter().zip(fields), |(value, typ)| { + TomlTypes::try_from_input_value(value, typ) + })?; + TomlTypes::Array(fields) + } + _ => return Err(InputParserError::AbiTypeMismatch(abi_type.clone())), }; Ok(toml_value) @@ -156,6 +163,13 @@ impl InputValue { InputValue::Struct(native_table) } + (TomlTypes::Array(array), AbiType::Tuple { fields }) => { + let tuple_fields = try_vecmap(array.into_iter().zip(fields), |(value, typ)| { + InputValue::try_from_toml(value, typ, arg_name) + })?; + InputValue::Vec(tuple_fields) + } + (_, _) => return Err(InputParserError::AbiTypeMismatch(param_type.clone())), }; diff --git a/tooling/noirc_abi/src/lib.rs b/tooling/noirc_abi/src/lib.rs index eb654e25f8c..d5c2314b3a6 100644 --- a/tooling/noirc_abi/src/lib.rs +++ b/tooling/noirc_abi/src/lib.rs @@ -62,6 +62,9 @@ pub enum AbiType { )] fields: Vec<(String, AbiType)>, }, + Tuple { + fields: Vec, + }, String { length: u64, }, @@ -164,7 +167,10 @@ impl AbiType { context.fully_qualified_struct_path(context.root_crate_id(), struct_type.id); Self::Struct { fields, path } } - Type::Tuple(_) => todo!("AbiType::from_type not yet implemented for tuple types"), + Type::Tuple(fields) => { + let fields = vecmap(fields, |typ| Self::from_type(context, typ)); + Self::Tuple { fields } + } Type::TypeVariable(_, _) => unreachable!(), Type::NamedGeneric(..) => unreachable!(), Type::Forall(..) => unreachable!(), @@ -182,6 +188,9 @@ impl AbiType { AbiType::Struct { fields, .. } => { fields.iter().fold(0, |acc, (_, field_type)| acc + field_type.field_count()) } + AbiType::Tuple { fields } => { + fields.iter().fold(0, |acc, field_typ| acc + field_typ.field_count()) + } AbiType::String { length } => *length as u32, } } @@ -370,6 +379,11 @@ impl Abi { encoded_value.extend(Self::encode_value(object[field].clone(), typ)?); } } + (InputValue::Vec(vec_elements), AbiType::Tuple { fields }) => { + for (value, typ) in vec_elements.into_iter().zip(fields) { + encoded_value.extend(Self::encode_value(value, typ)?); + } + } _ => unreachable!("value should have already been checked to match abi type"), } Ok(encoded_value) @@ -462,6 +476,14 @@ fn decode_value( InputValue::Struct(struct_map) } + AbiType::Tuple { fields } => { + let mut tuple_elements = Vec::with_capacity(fields.len()); + for field_typ in fields { + tuple_elements.push(decode_value(field_iterator, field_typ)?); + } + + InputValue::Vec(tuple_elements) + } }; Ok(value) diff --git a/tooling/noirc_abi_wasm/src/temp.rs b/tooling/noirc_abi_wasm/src/temp.rs index e6c4e384950..f833ecb756e 100644 --- a/tooling/noirc_abi_wasm/src/temp.rs +++ b/tooling/noirc_abi_wasm/src/temp.rs @@ -55,6 +55,13 @@ impl JsonTypes { JsonTypes::Table(map_with_json_types) } + (InputValue::Vec(vector), AbiType::Tuple { fields }) => { + let fields = try_vecmap(vector.iter().zip(fields), |(value, typ)| { + JsonTypes::try_from_input_value(value, typ) + })?; + JsonTypes::Array(fields) + } + _ => return Err(InputParserError::AbiTypeMismatch(abi_type.clone())), }; Ok(json_value) @@ -104,6 +111,13 @@ pub(super) fn input_value_from_json_type( InputValue::Struct(native_table) } + (JsonTypes::Array(array), AbiType::Tuple { fields }) => { + let tuple_fields = try_vecmap(array.into_iter().zip(fields), |(value, typ)| { + input_value_from_json_type(value, typ, arg_name) + })?; + InputValue::Vec(tuple_fields) + } + (_, _) => return Err(InputParserError::AbiTypeMismatch(param_type.clone())), };