Skip to content

Commit

Permalink
Merge branch 'master' into pr/2003
Browse files Browse the repository at this point in the history
  • Loading branch information
jfecher committed Aug 1, 2023
2 parents ac22a06 + 3c82721 commit 163d9c3
Show file tree
Hide file tree
Showing 60 changed files with 1,307 additions and 581 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions crates/fm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ mod tests {

fn create_dummy_file(dir: &TempDir, file_name: &Path) {
let file_path = dir.path().join(file_name);
let _file = std::fs::File::create(file_path.clone()).unwrap();
let _file = std::fs::File::create(file_path).unwrap();
}

#[test]
Expand All @@ -175,7 +175,7 @@ mod tests {

let mut fm = FileManager::new(dir.path());

let file_id = fm.add_file(&file_name).unwrap();
let file_id = fm.add_file(file_name).unwrap();

assert!(fm.path(file_id).ends_with("foo"));
}
Expand All @@ -189,7 +189,7 @@ mod tests {
let file_name = Path::new("lib.nr");
create_dummy_file(&dir, file_name);

let file_id = fm.add_file(&file_name).unwrap();
let file_id = fm.add_file(file_name).unwrap();

// Create a sub directory
// we now have:
Expand Down Expand Up @@ -238,7 +238,7 @@ mod tests {
let second_file_name = PathBuf::from(sub_sub_dir.path()).join("./../../lib.nr");

// Add both files to the file manager
let file_id = fm.add_file(&file_name).unwrap();
let file_id = fm.add_file(file_name).unwrap();
let second_file_id = fm.add_file(&second_file_name).unwrap();

assert_eq!(file_id, second_file_id);
Expand Down
3 changes: 2 additions & 1 deletion crates/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,5 @@ serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
noirc_errors.workspace = true
base64.workspace = true
base64.workspace = true
regex = "1.9.1"
91 changes: 75 additions & 16 deletions crates/nargo/src/ops/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use acvm::{
pwg::ForeignCallWaitInfo,
};
use iter_extended::vecmap;
use noirc_abi::{decode_string_value, decode_value, input_parser::json::JsonTypes, AbiType};
use noirc_abi::{decode_string_value, input_parser::InputValueDisplay, AbiType};
use regex::{Captures, Regex};

use crate::errors::ForeignCallError;

Expand Down Expand Up @@ -63,31 +64,89 @@ impl ForeignCall {
}

fn execute_println(foreign_call_inputs: &[Vec<Value>]) -> Result<(), ForeignCallError> {
let (abi_type, input_values) = fetch_abi_type(foreign_call_inputs)?;
let (is_fmt_str, foreign_call_inputs) =
foreign_call_inputs.split_last().ok_or(ForeignCallError::MissingForeignCallInputs)?;

// We must use a flat map here as each value in a struct will be in a separate input value
let mut input_values_as_fields =
input_values.iter().flat_map(|values| values.iter().map(|value| value.to_field()));
let decoded_value = decode_value(&mut input_values_as_fields, &abi_type)?;

let json_value = JsonTypes::try_from_input_value(&decoded_value, &abi_type)?;

println!("{json_value}");
let output_string = if is_fmt_str[0].to_field().is_one() {
convert_fmt_string_inputs(foreign_call_inputs)?
} else {
convert_string_inputs(foreign_call_inputs)?
};
println!("{output_string}");
Ok(())
}
}

/// Fetch the abi type from the foreign call input
/// The remaining input values should hold the values to be printed
fn fetch_abi_type(
foreign_call_inputs: &[Vec<Value>],
) -> Result<(AbiType, &[Vec<Value>]), ForeignCallError> {
fn convert_string_inputs(foreign_call_inputs: &[Vec<Value>]) -> Result<String, ForeignCallError> {
// Fetch the abi type from the foreign call input
// The remaining input values should hold what is to be printed
let (abi_type_as_values, input_values) =
foreign_call_inputs.split_last().ok_or(ForeignCallError::MissingForeignCallInputs)?;
let abi_type = fetch_abi_type(abi_type_as_values)?;

// We must use a flat map here as each value in a struct will be in a separate input value
let mut input_values_as_fields =
input_values.iter().flat_map(|values| vecmap(values, |value| value.to_field()));

let input_value_display =
InputValueDisplay::try_from_fields(&mut input_values_as_fields, abi_type)?;

Ok(input_value_display.to_string())
}

fn convert_fmt_string_inputs(
foreign_call_inputs: &[Vec<Value>],
) -> Result<String, ForeignCallError> {
let (message_as_values, input_and_abi_values) =
foreign_call_inputs.split_first().ok_or(ForeignCallError::MissingForeignCallInputs)?;

let message_as_fields = vecmap(message_as_values, |value| value.to_field());
let message_as_string = decode_string_value(&message_as_fields);

let (num_values, input_and_abi_values) =
input_and_abi_values.split_first().ok_or(ForeignCallError::MissingForeignCallInputs)?;

let mut output_strings = Vec::new();
let num_values = num_values[0].to_field().to_u128() as usize;

let mut abi_types = Vec::new();
for abi_values in input_and_abi_values.iter().skip(input_and_abi_values.len() - num_values) {
let abi_type = fetch_abi_type(abi_values)?;
abi_types.push(abi_type);
}

for i in 0..num_values {
let abi_type = &abi_types[i];
let type_size = abi_type.field_count() as usize;

let mut input_values_as_fields = input_and_abi_values[i..(i + type_size)]
.iter()
.flat_map(|values| vecmap(values, |value| value.to_field()));

let input_value_display =
InputValueDisplay::try_from_fields(&mut input_values_as_fields, abi_type.clone())?;

output_strings.push(input_value_display.to_string());
}

let mut output_strings_iter = output_strings.into_iter();
let re = Regex::new(r"\{([a-zA-Z0-9_]+)\}")
.expect("ICE: an invalid regex pattern was used for checking format strings");

let formatted_str = re.replace_all(&message_as_string, |_: &Captures| {
output_strings_iter
.next()
.expect("ICE: there are more regex matches than fields supplied to the format string")
});

Ok(formatted_str.into_owned())
}

fn fetch_abi_type(abi_type_as_values: &[Value]) -> Result<AbiType, ForeignCallError> {
let abi_type_as_fields = vecmap(abi_type_as_values, |value| value.to_field());
let abi_type_as_string = decode_string_value(&abi_type_as_fields);
let abi_type: AbiType = serde_json::from_str(&abi_type_as_string)
.map_err(|err| ForeignCallError::InputParserError(err.into()))?;

Ok((abi_type, input_values))
Ok(abi_type)
}
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/1_mul/target/main.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"z","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2],"z":[3]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/9WYTW6DMBSEJwESGhrapNsuOIKNIZhdr1JUcv8jFFQ7UCu7zEPBEjKW4P3MvI8FHwA+8bc2w7V1ezE7b4Nz5M7+2Y17/8vt6rGlfR5w4yoE68G42t/EQ4xkpgO78JsgcWBE4s4QNCGMa9Slqvqm7LXR36psO1urqu4uVltd2/qntMb0trJN27WNanVlen2tW3N1gXeQMTTi1Fm6OnVM7Dkh+sLUTwrgxM0mmQMxgPdDjBQLADwmKjABnEIOYG8CG+AXrAPgPbHnlOgLUz8pgFM3m2QOxAA+DDEyLADwmKjABHAGOYC9CWyAX7EOgA/EnjOiL0z9pADO3GySORAD+DjEyLEAwGOiAhPAOeQA9iawAX7DOgA+EnvOib48uX63niNizzmxrneifhHuQA8+i8ya5/WeZvex27d3ZmIn0BOCPKGOoh9UKZNOAnHP4A2/VN9nvkf/Pk7PrGk0q9H/YAzXLy9W07upFAAA","proving_key":null,"verification_key":null}
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"z","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2],"z":[3]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/9WYTW6DMBSEJ+EvhAZVVdVuuuAIGEMwu16lqOT+RyiodiBWdpmHgiVkLMH7mXkfCz4BfOF/7cZrb/dicd5758Ce3bM7+/633cvHlnJ5wI1bwlsPxlXuJhxjRAsd2IVfBQk9IyJ7hqAJflxdnut6aKtBafVTVl1vmrJu+rNRRjWm+a2M1oOpTdv1XVt2qtaDujSdvtjAr5AxNODUWdk6VUjsOSL6wtRPCuDIziaZAzGA4zFGghUAnhIVmAFOIAewM4EN8Bu2AXBM7Dkh+sLUTwrgxM4mmQMxgA9jjBQrADwlKjADnEIOYGcCG+B3bAPgA7HnlOgLUz8pgFM7m2QOxAA+jjEyrADwlKjADHAGOYCdCWyAP7ANgI/EnjOiL0+u37XngNhzRqzrhahfgDvQg88is+ZlvafFfWj3/Z2ZiAV6gpfH1zGH4AdVyqSTQNwcvOGX6jvne3TzcXpmTYNFje4Ho7/+AAxbTEGpFAAA","proving_key":null,"verification_key":null}
Binary file modified crates/nargo_cli/tests/test_data/1_mul/target/witness.tr
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/2_div/target/main.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"z","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2],"z":[3]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/+1Y7U7CMBQ9bAMB5UNAYowm++ffdh+w/fNVJI73fwTX2Empxpjs3GUk3IRsTeD09NzbnlteALziKwb1J7DP2BkH3ji04+a7cH5v4s0+VbvQAx6WkuIYEDlGNcbQ05QuqCEceQkd2rEboaBQqdplWbVPKp3qd5WUhyJXWX7YFbrQeZF/JEWaVkVW7MtDuVelztJKH/MyPVqwIRFrRMRq9Bw5ekoUXPQLblvuj+BvNok6Yub+hshLSr+ArN+gn/qpq5lwzWRcY0zQgZmYiWKczGSCyzKTCRFrCr6ZTCFrJmPwzeQJuAgzYeb+lshLSr8+mwlRvz9rpi3PkKjfmMjrjqifWeMP4wD//GFydvnOnPcI57Xv1sRIYE3w5vF1nEPQlKWSNBPAnYNX/FLrnvNzdHY49VnTpiNnczSbIhDWlYCrm5dFjbFEB520mSjGqZNeQrbzC8Hv/O7RaxdPLE+9IK55ScwLU78A50Xb5+6PoOH3hoUXLNxVjbFGBwfBCqeTN7CTxt6c7KuQVDLbYj3jMg6UFXHNa2JeCfrp/+in2gX1f901EWtDrL+urlVMzi7fB+f9eq1qibmxgrJxt+j3tcqse8vPkei1iqlp6HBsunA/PgHqZfGSlh8AAA==","proving_key":null,"verification_key":null}
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"z","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2],"z":[3]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/+1Yy07DMBDcOn2ktE1bKoQQHHLhbufROjd+hYj0/z+BRNjEWEVC8myUiq5U2Zaa8e7seuLNCxG90pdN2p8wY+qshbeOzNr+l5znO3szowwzNcFhSS4fBdDHaYsx8ziFE9o5PPUSOjNr1yJGonJ5LIrmlDUqV+8yq2pdyqKsj1ppVeryI9N53uhCn6q6OslKFXmjzmWVnw3YDIg1B2JZPucOn37Bhe5h84fGfSD8YeOoowlTHckwU0j+bsKKFdZFixHTAMLabZRSL6wxXZewxkCsJeGFdUm8wrpgwH2k/yesMdCvofgLjTkCxrwA+nUH5C+iCyJKePFH+uz6u3LmUzOKCzUxZ4iJvH18HhNifEFxJWnFgLsmXPFzxb3G5+iHOI2ZU3s7RfvYHQrBzCsAV9nJhpgPrb31dBul1N8qE/r9FiTDTNnvO+hb0BON+i2eGT/VBhhzAswLkj+/vRQMdTsiDr8PLHmGwt22GDsaQAi21CuvMJum3p5jbgsSINYzXYegbIEx74B5BfCn/sKfDDOF/Na7A2LtgfU3VFuF9Nn1996Z39qqQMy9IRSNe6Bxt1Vd3Ad8jljbKiSnkeOjvYX79gnryeDFrh0AAA==","proving_key":null,"verification_key":null}
Binary file modified crates/nargo_cli/tests/test_data/2_div/target/witness.tr
Binary file not shown.
2 changes: 1 addition & 1 deletion crates/nargo_cli/tests/test_data/5_over/target/main.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/81X226EIBCdBbsX3fatj036CYyIwlt/pab4/59QbTHLkn3zjNlJDBDNmcs5wPhKRO/0b4f5UWn8zNYqrdd3h/T9VxrNNuMcE4hrqLCNuLxO9IxRFTHDC7IUWxdEVGlNgiSUuNb0XReHNrLlb9OG0TvTubH37Nl599N6a6Pv/BDGMJjAnY08uWCnBPxCMoRqTJxtipM1MOcKyMte9TPbjJVQ/bZiHYH10/Rg0xN+LyJjzuM9ZfMqjeqBJo4COVHhp6zjGwkeqFIknQRwz4QTv1TeZzxHd4fTM9dU0b1Q0bECLw90B7Sav8wYNe3QAS2OPujWAS1OdeETfZMhbp84/Vm4AIVX43IUFzEw1p6ERNzMGFfaQcQN3VprlZxKi7gGirgBivgqJAz0b0tD2M1GGf+l/QIoZZmihA8AAA==","proving_key":null,"verification_key":null}
{"backend":"acvm-backend-barretenberg","abi":{"parameters":[{"name":"x","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"},{"name":"y","type":{"kind":"integer","sign":"unsigned","width":32},"visibility":"private"}],"param_witnesses":{"x":[1],"y":[2]},"return_type":null,"return_witnesses":[]},"bytecode":"H4sIAAAAAAAA/81XUW6DMAx1E9YWqvVvn5N2hJgQSP52laGF+x9hsAY1jfrHM6ollESgZ/v5JTFXIvqgmx3mR6XxK1urtF7fHdL332k024xzTCCuocI24vI60TNGVcQMJ2QhWxeFqNKaBItQ4lrTd10c2siWf0wbRu9M58bes2fn3W/rrY2+80MYw2ACdzby5IKdEvA7yRRUY+JsU5ysgTlXwLrsxZ/ZZqyE+NuK9QbkT9OTTU/4vYiMOY/3mM2rNKonmjgK5ESFn5LHKwkeqFJFkiDqRDjxS+V9wtfo4XB6ZU4VPQoVHSvw8kB3QKv584xR0w4d0OLok+4d0OJUFz7RNxni9onTv4UzUHg1LkdxEQNj7UlIxM2McaEdRNzQvbVWyam0iGugiBugiC9CwkD/tjSE3WyU1b+0P+cFO+aEDwAA","proving_key":null,"verification_key":null}
Binary file modified crates/nargo_cli/tests/test_data/5_over/target/witness.tr
Binary file not shown.
Loading

0 comments on commit 163d9c3

Please sign in to comment.