-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Refactor Logging to use Brillig foreign calls (#1917)
* initial stdlib methods to start refactoring logign * foreign call enum * working println and println_format w/ brillig oracles * fix up brillig_oracle test * uncomment regression test for slice return from foreign calls in brillig * cargo clippy * got structs serialized correctly without aos_to_soa * remove dbg * working println_format * cargo clippy * rename enable_slices to experimental_ssa * remove dbg and fix format_field_string * pr comments, and remove format work to move into separate PR * add comment about removing old println * remove old comment * have println return nothing * Update crates/noirc_frontend/src/hir/def_map/mod.rs Co-authored-by: jfecher <jake@aztecprotocol.com> * Update crates/noirc_frontend/src/hir/def_map/mod.rs Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com> * add comment to append_abi_arg call * include println oracle comment in stdlib --------- Co-authored-by: jfecher <jake@aztecprotocol.com> Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
- Loading branch information
1 parent
0449518
commit c15f9aa
Showing
23 changed files
with
443 additions
and
278 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
use acvm::{ | ||
acir::brillig::{ForeignCallResult, Value}, | ||
pwg::ForeignCallWaitInfo, | ||
}; | ||
use iter_extended::vecmap; | ||
use noirc_abi::{decode_string_value, decode_value, input_parser::json::JsonTypes, AbiType}; | ||
|
||
use crate::errors::ForeignCallError; | ||
|
||
/// This enumeration represents the Brillig foreign calls that are natively supported by nargo. | ||
/// After resolution of a foreign call, nargo will restart execution of the ACVM | ||
pub(crate) enum ForeignCall { | ||
Println, | ||
Sequence, | ||
ReverseSequence, | ||
} | ||
|
||
impl std::fmt::Display for ForeignCall { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
write!(f, "{}", self.name()) | ||
} | ||
} | ||
|
||
impl ForeignCall { | ||
pub(crate) fn name(&self) -> &'static str { | ||
match self { | ||
ForeignCall::Println => "println", | ||
ForeignCall::Sequence => "get_number_sequence", | ||
ForeignCall::ReverseSequence => "get_reverse_number_sequence", | ||
} | ||
} | ||
|
||
pub(crate) fn lookup(op_name: &str) -> Option<ForeignCall> { | ||
match op_name { | ||
"println" => Some(ForeignCall::Println), | ||
"get_number_sequence" => Some(ForeignCall::Sequence), | ||
"get_reverse_number_sequence" => Some(ForeignCall::ReverseSequence), | ||
_ => None, | ||
} | ||
} | ||
|
||
pub(crate) fn execute( | ||
foreign_call: &ForeignCallWaitInfo, | ||
) -> Result<ForeignCallResult, ForeignCallError> { | ||
let foreign_call_name = foreign_call.function.as_str(); | ||
match Self::lookup(foreign_call_name) { | ||
Some(ForeignCall::Println) => { | ||
Self::execute_println(&foreign_call.inputs)?; | ||
Ok(ForeignCallResult { values: vec![] }) | ||
} | ||
Some(ForeignCall::Sequence) => { | ||
let sequence_length: u128 = foreign_call.inputs[0][0].to_field().to_u128(); | ||
|
||
Ok(vecmap(0..sequence_length, Value::from).into()) | ||
} | ||
Some(ForeignCall::ReverseSequence) => { | ||
let sequence_length: u128 = foreign_call.inputs[0][0].to_field().to_u128(); | ||
|
||
Ok(vecmap((0..sequence_length).rev(), Value::from).into()) | ||
} | ||
None => panic!("unexpected foreign call {:?}", foreign_call_name), | ||
} | ||
} | ||
|
||
fn execute_println(foreign_call_inputs: &[Vec<Value>]) -> Result<(), ForeignCallError> { | ||
let (abi_type, input_values) = fetch_abi_type(foreign_call_inputs)?; | ||
|
||
// 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}"); | ||
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> { | ||
let (abi_type_as_values, input_values) = | ||
foreign_call_inputs.split_last().ok_or(ForeignCallError::MissingForeignCallInputs)?; | ||
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
5 changes: 5 additions & 0 deletions
5
crates/nargo_cli/tests/test_data_ssa_refactor/debug_logs/Nargo.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
[package] | ||
authors = [""] | ||
compiler_version = "0.8.0" | ||
|
||
[dependencies] |
2 changes: 2 additions & 0 deletions
2
crates/nargo_cli/tests/test_data_ssa_refactor/debug_logs/Prover.toml
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
x = "5" | ||
y = "10" |
22 changes: 22 additions & 0 deletions
22
crates/nargo_cli/tests/test_data_ssa_refactor/debug_logs/src/main.nr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
use dep::std; | ||
|
||
fn main(x : Field, y : pub Field) { | ||
|
||
std::println("*** println ***"); | ||
std::println(x); | ||
std::println([x, y]); | ||
|
||
let s = myStruct { y: x, x: y }; | ||
let foo = fooStruct { my_struct: s, foo: 15 }; | ||
std::println(foo); | ||
} | ||
|
||
struct myStruct { | ||
y: Field, | ||
x: Field, | ||
} | ||
|
||
struct fooStruct { | ||
my_struct: myStruct, | ||
foo: Field, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
mod json; | ||
pub mod json; | ||
mod toml; | ||
|
||
use std::collections::BTreeMap; | ||
|
Oops, something went wrong.