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

chore: move implementation of print foreign call into nargo #6865

Merged
merged 4 commits into from
Jan 2, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions Cargo.lock

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

5 changes: 1 addition & 4 deletions compiler/noirc_printable_type/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ workspace = true

[dependencies]
acvm.workspace = true
iter-extended.workspace = true
serde.workspace = true
serde_json.workspace = true
thiserror.workspace = true
jsonrpc.workspace = true
regex = "1.9.1"

[dev-dependencies]
286 changes: 40 additions & 246 deletions compiler/noirc_printable_type/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
#![forbid(unsafe_code)]
#![warn(unused_crate_dependencies, unused_extern_crates)]
#![warn(unreachable_pub)]
#![warn(clippy::semicolon_if_nothing_returned)]

use std::{collections::BTreeMap, str};

use acvm::{acir::AcirField, brillig_vm::brillig::ForeignCallParam};
use iter_extended::vecmap;
use acvm::AcirField;

use regex::{Captures, Regex};
use serde::{Deserialize, Serialize};
use thiserror::Error;

#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(tag = "kind", rename_all = "lowercase")]
Expand Down Expand Up @@ -67,95 +71,28 @@
FmtString(String, Vec<(PrintableValue<F>, PrintableType)>),
}

#[derive(Debug, Error)]
pub enum ForeignCallError {
#[error("No handler could be found for foreign call `{0}`")]
NoHandler(String),

#[error("Foreign call inputs needed for execution are missing")]
MissingForeignCallInputs,

#[error("Could not parse PrintableType argument. {0}")]
ParsingError(#[from] serde_json::Error),

#[error("Failed calling external resolver. {0}")]
ExternalResolverError(#[from] jsonrpc::Error),

#[error("Assert message resolved after an unsatisified constrain. {0}")]
ResolvedAssertMessage(String),
}

impl<F: AcirField> TryFrom<&[ForeignCallParam<F>]> for PrintableValueDisplay<F> {
type Error = ForeignCallError;
impl<F: AcirField> std::fmt::Display for PrintableValueDisplay<F> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Plain(value, typ) => {
let output_string = to_string(value, typ).ok_or(std::fmt::Error)?;
write!(fmt, "{output_string}")
}
Self::FmtString(template, values) => {
let mut display_iter = values.iter();
let re = Regex::new(r"\{([a-zA-Z0-9_]+)\}").map_err(|_| std::fmt::Error)?;

fn try_from(foreign_call_inputs: &[ForeignCallParam<F>]) -> Result<Self, Self::Error> {
let (is_fmt_str, foreign_call_inputs) =
foreign_call_inputs.split_last().ok_or(ForeignCallError::MissingForeignCallInputs)?;
let formatted_str = replace_all(&re, template, |_: &Captures| {
let (value, typ) = display_iter.next().ok_or(std::fmt::Error)?;
to_string(value, typ).ok_or(std::fmt::Error)
})?;

if is_fmt_str.unwrap_field().is_one() {
convert_fmt_string_inputs(foreign_call_inputs)
} else {
convert_string_inputs(foreign_call_inputs)
write!(fmt, "{formatted_str}")
}
}
}
}

fn convert_string_inputs<F: AcirField>(
foreign_call_inputs: &[ForeignCallParam<F>],
) -> Result<PrintableValueDisplay<F>, ForeignCallError> {
// Fetch the PrintableType from the foreign call input
// The remaining input values should hold what is to be printed
let (printable_type_as_values, input_values) =
foreign_call_inputs.split_last().ok_or(ForeignCallError::MissingForeignCallInputs)?;
let printable_type = fetch_printable_type(printable_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(|param| param.fields());

let value = decode_value(&mut input_values_as_fields, &printable_type);

Ok(PrintableValueDisplay::Plain(value, printable_type))
}

fn convert_fmt_string_inputs<F: AcirField>(
foreign_call_inputs: &[ForeignCallParam<F>],
) -> Result<PrintableValueDisplay<F>, ForeignCallError> {
let (message, input_and_printable_types) =
foreign_call_inputs.split_first().ok_or(ForeignCallError::MissingForeignCallInputs)?;

let message_as_fields = message.fields();
let message_as_string = decode_string_value(&message_as_fields);

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

let mut output = Vec::new();
let num_values = num_values.unwrap_field().to_u128() as usize;

let types_start_at = input_and_printable_types.len() - num_values;
let mut input_iter =
input_and_printable_types[0..types_start_at].iter().flat_map(|param| param.fields());
for printable_type in input_and_printable_types.iter().skip(types_start_at) {
let printable_type = fetch_printable_type(printable_type)?;
let value = decode_value(&mut input_iter, &printable_type);

output.push((value, printable_type));
}

Ok(PrintableValueDisplay::FmtString(message_as_string, output))
}

fn fetch_printable_type<F: AcirField>(
printable_type: &ForeignCallParam<F>,
) -> Result<PrintableType, ForeignCallError> {
let printable_type_as_fields = printable_type.fields();
let printable_type_as_string = decode_string_value(&printable_type_as_fields);
let printable_type: PrintableType = serde_json::from_str(&printable_type_as_string)?;

Ok(printable_type)
}

fn to_string<F: AcirField>(value: &PrintableValue<F>, typ: &PrintableType) -> Option<String> {
let mut output = String::new();
match (value, typ) {
Expand Down Expand Up @@ -193,7 +130,7 @@
(PrintableValue::Vec { array_elements, is_slice }, PrintableType::Array { typ, .. })
| (PrintableValue::Vec { array_elements, is_slice }, PrintableType::Slice { typ }) => {
if *is_slice {
output.push('&')
output.push('&');
}
output.push('[');
let mut values = array_elements.iter().peekable();
Expand Down Expand Up @@ -233,12 +170,12 @@

(PrintableValue::Vec { array_elements, .. }, PrintableType::Tuple { types }) => {
output.push('(');
let mut elems = array_elements.iter().zip(types).peekable();

Check warning on line 173 in compiler/noirc_printable_type/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
while let Some((value, typ)) = elems.next() {

Check warning on line 174 in compiler/noirc_printable_type/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
output.push_str(
&PrintableValueDisplay::Plain(value.clone(), typ.clone()).to_string(),
);
if elems.peek().is_some() {

Check warning on line 178 in compiler/noirc_printable_type/src/lib.rs

View workflow job for this annotation

GitHub Actions / Code

Unknown word (elems)
output.push_str(", ");
}
}
Expand All @@ -253,82 +190,22 @@
Some(output)
}

impl<F: AcirField> std::fmt::Display for PrintableValueDisplay<F> {
fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Plain(value, typ) => {
let output_string = to_string(value, typ).ok_or(std::fmt::Error)?;
write!(fmt, "{output_string}")
}
Self::FmtString(template, values) => {
let mut values_iter = values.iter();
write_template_replacing_interpolations(template, fmt, || {
values_iter.next().and_then(|(value, typ)| to_string(value, typ))
})
}
}
// Taken from Regex docs directly
fn replace_all<E>(
re: &Regex,
haystack: &str,
mut replacement: impl FnMut(&Captures) -> Result<String, E>,
) -> Result<String, E> {
let mut new = String::with_capacity(haystack.len());
let mut last_match = 0;
for caps in re.captures_iter(haystack) {
let m = caps.get(0).unwrap();
new.push_str(&haystack[last_match..m.start()]);
new.push_str(&replacement(&caps)?);
last_match = m.end();
}
}

fn write_template_replacing_interpolations(
template: &str,
fmt: &mut std::fmt::Formatter<'_>,
mut replacement: impl FnMut() -> Option<String>,
) -> std::fmt::Result {
let mut last_index = 0; // How far we've written from the template
let mut char_indices = template.char_indices().peekable();
while let Some((char_index, char)) = char_indices.next() {
// If we see a '}' it must be "}}" because the ones for interpolation are handled
// when we see '{'
if char == '}' {
// Write what we've seen so far in the template, including this '}'
write!(fmt, "{}", &template[last_index..=char_index])?;

// Skip the second '}'
let (_, closing_curly) = char_indices.next().unwrap();
assert_eq!(closing_curly, '}');

last_index = char_indices.peek().map(|(index, _)| *index).unwrap_or(template.len());
continue;
}

// Keep going forward until we find a '{'
if char != '{' {
continue;
}

// We'll either have to write an interpolation or '{{' if it's an escape,
// so let's write what we've seen so far in the template.
write!(fmt, "{}", &template[last_index..char_index])?;

// If it's '{{', write '{' and keep going
if char_indices.peek().map(|(_, char)| char) == Some(&'{') {
write!(fmt, "{{")?;

// Skip the second '{'
char_indices.next().unwrap();

last_index = char_indices.peek().map(|(index, _)| *index).unwrap_or(template.len());
continue;
}

// Write the interpolation
if let Some(string) = replacement() {
write!(fmt, "{}", string)?;
} else {
return Err(std::fmt::Error);
}

// Whatever was inside '{...}' doesn't matter, so skip until we find '}'
while let Some((_, char)) = char_indices.next() {
if char == '}' {
last_index = char_indices.peek().map(|(index, _)| *index).unwrap_or(template.len());
break;
}
}
}

write!(fmt, "{}", &template[last_index..])
new.push_str(&haystack[last_match..]);
Ok(new)
}

/// This trims any leading zeroes.
Expand All @@ -346,94 +223,11 @@
"0x".to_owned() + &trimmed_field
}

/// Assumes that `field_iterator` contains enough field elements in order to decode the [PrintableType]
pub fn decode_value<F: AcirField>(
field_iterator: &mut impl Iterator<Item = F>,
typ: &PrintableType,
) -> PrintableValue<F> {
match typ {
PrintableType::Field
| PrintableType::SignedInteger { .. }
| PrintableType::UnsignedInteger { .. }
| PrintableType::Boolean => {
let field_element = field_iterator.next().unwrap();

PrintableValue::Field(field_element)
}
PrintableType::Array { length, typ } => {
let length = *length as usize;
let mut array_elements = Vec::with_capacity(length);
for _ in 0..length {
array_elements.push(decode_value(field_iterator, typ));
}

PrintableValue::Vec { array_elements, is_slice: false }
}
PrintableType::Slice { typ } => {
let length = field_iterator
.next()
.expect("not enough data to decode variable array length")
.to_u128() as usize;
let mut array_elements = Vec::with_capacity(length);
for _ in 0..length {
array_elements.push(decode_value(field_iterator, typ));
}

PrintableValue::Vec { array_elements, is_slice: true }
}
PrintableType::Tuple { types } => PrintableValue::Vec {
array_elements: vecmap(types, |typ| decode_value(field_iterator, typ)),
is_slice: false,
},
PrintableType::String { length } => {
let field_elements: Vec<F> = field_iterator.take(*length as usize).collect();

PrintableValue::String(decode_string_value(&field_elements))
}
PrintableType::Struct { fields, .. } => {
let mut struct_map = BTreeMap::new();

for (field_key, param_type) in fields {
let field_value = decode_value(field_iterator, param_type);

struct_map.insert(field_key.to_owned(), field_value);
}

PrintableValue::Struct(struct_map)
}
PrintableType::Function { env, .. } => {
let field_element = field_iterator.next().unwrap();
let func_ref = PrintableValue::Field(field_element);
// we want to consume the fields from the environment, but for now they are not actually printed
decode_value(field_iterator, env);
func_ref
}
PrintableType::MutableReference { typ } => {
// we decode the reference, but it's not really used for printing
decode_value(field_iterator, typ)
}
PrintableType::Unit => PrintableValue::Field(F::zero()),
}
}

pub fn decode_string_value<F: AcirField>(field_elements: &[F]) -> String {
// TODO: Replace with `into` when Char is supported
let string_as_slice = vecmap(field_elements, |e| {
let mut field_as_bytes = e.to_be_bytes();
let char_byte = field_as_bytes.pop().unwrap(); // A character in a string is represented by a u8, thus we just want the last byte of the element
assert!(field_as_bytes.into_iter().all(|b| b == 0)); // Assert that the rest of the field element's bytes are empty
char_byte
});

let final_string = str::from_utf8(&string_as_slice).unwrap();
final_string.to_owned()
}

#[cfg(test)]
mod tests {
use acvm::FieldElement;

use crate::{PrintableType, PrintableValue, PrintableValueDisplay};
use super::{PrintableType, PrintableValue, PrintableValueDisplay};

#[test]
fn printable_value_display_to_string_without_interpolations() {
Expand Down
3 changes: 1 addition & 2 deletions tooling/debugger/src/foreign_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,11 @@ use acvm::{
AcirField, FieldElement,
};
use nargo::{
foreign_calls::{DefaultForeignCallExecutor, ForeignCallExecutor},
foreign_calls::{DefaultForeignCallExecutor, ForeignCallError, ForeignCallExecutor},
PrintOutput,
};
use noirc_artifacts::debug::{DebugArtifact, DebugVars, StackFrame};
use noirc_errors::debug_info::{DebugFnId, DebugVarId};
use noirc_printable_type::ForeignCallError;

pub(crate) enum DebugForeignCall {
VarAssign,
Expand Down
1 change: 1 addition & 0 deletions tooling/nargo/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ rayon.workspace = true
jsonrpc.workspace = true
rand.workspace = true
serde.workspace = true
serde_json.workspace = true
walkdir = "2.5.0"

[target.'cfg(not(target_arch = "wasm32"))'.dependencies]
Expand Down
3 changes: 2 additions & 1 deletion tooling/nargo/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,10 @@ use noirc_errors::{
pub use noirc_errors::Location;

use noirc_driver::CrateName;
use noirc_printable_type::ForeignCallError;
use thiserror::Error;

use crate::foreign_calls::ForeignCallError;

/// Errors covering situations where a package cannot be compiled.
#[derive(Debug, Error)]
pub enum CompileError {
Expand Down
Loading
Loading