Skip to content

Commit

Permalink
feat: add return values to aztec fns (#5389)
Browse files Browse the repository at this point in the history
Leverages the `#[abi(tag)]` feature from
#5386 to output the
aztec functions abi *before* macro transformation, so that values can be
decoded in #5551

---------

Co-authored-by: esau <152162806+sklppy88@users.noreply.github.com>
  • Loading branch information
Thunkar and sklppy88 authored Apr 9, 2024
1 parent 5912212 commit 7b88bac
Show file tree
Hide file tree
Showing 7 changed files with 102 additions and 9 deletions.
2 changes: 1 addition & 1 deletion l1-contracts/src/core/libraries/ConstantsGen.sol
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ library Constants {
uint256 internal constant DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
uint256 internal constant DEPLOYER_CONTRACT_ADDRESS =
0x12bcd3e09e8d8559c75d59552db47471f63c72fbbc9decb59c517b4e58634a72;
0x1b628eeb6349f2a4c000b703942eb8a625bfe5e6ee34ccc210748cf9ae05af98;
uint256 internal constant L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 17;
uint256 internal constant MAX_NOTE_FIELDS_LENGTH = 20;
uint256 internal constant GET_NOTE_ORACLE_RETURN_LENGTH = 23;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ global REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE = 0xe7af8166354
// CONTRACT INSTANCE CONSTANTS
// sha224sum 'struct ContractInstanceDeployed'
global DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE = 0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631;
global DEPLOYER_CONTRACT_ADDRESS = 0x12bcd3e09e8d8559c75d59552db47471f63c72fbbc9decb59c517b4e58634a72;
global DEPLOYER_CONTRACT_ADDRESS = 0x1b628eeb6349f2a4c000b703942eb8a625bfe5e6ee34ccc210748cf9ae05af98;

// NOIR CONSTANTS - constants used only in yarn-packages/noir-contracts
// Some are defined here because Noir doesn't yet support globals referencing other globals yet.
Expand Down
3 changes: 2 additions & 1 deletion noir/noir-repo/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod utils;
use transforms::{
compute_note_hash_and_nullifier::inject_compute_note_hash_and_nullifier,
events::{generate_selector_impl, transform_events},
functions::{transform_function, transform_unconstrained},
functions::{export_fn_abi, transform_function, transform_unconstrained},
note_interface::{generate_note_interface_impl, inject_note_exports},
storage::{
assign_storage_slots, check_for_storage_definition, check_for_storage_implementation,
Expand Down Expand Up @@ -129,6 +129,7 @@ fn transform_module(module: &mut SortedModule) -> Result<bool, AztecMacroError>

// Apply transformations to the function based on collected attributes
if is_private || is_public || is_public_vm {
export_fn_abi(&mut module.types, func)?;
transform_function(
if is_private {
"Private"
Expand Down
94 changes: 90 additions & 4 deletions noir/noir-repo/aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use convert_case::{Case, Casing};
use noirc_errors::Span;
use noirc_frontend::{
macros_api::FieldElement, BlockExpression, ConstrainKind, ConstrainStatement, Distinctness,
Expression, ExpressionKind, ForLoopStatement, ForRange, FunctionReturnType, Ident, Literal,
NoirFunction, Param, PathKind, Pattern, Signedness, Statement, StatementKind, UnresolvedType,
UnresolvedTypeData, Visibility,
macros_api::FieldElement, parse_program, BlockExpression, ConstrainKind, ConstrainStatement,
Distinctness, Expression, ExpressionKind, ForLoopStatement, ForRange, FunctionReturnType,
Ident, Literal, NoirFunction, NoirStruct, Param, PathKind, Pattern, Signedness, Statement,
StatementKind, UnresolvedType, UnresolvedTypeData, Visibility,
};

use crate::{
Expand Down Expand Up @@ -113,6 +113,92 @@ pub fn transform_function(
Ok(())
}

// Generates a global struct containing the original (before transform_function gets executed) function abi that gets exported
// in the contract artifact after compilation. The abi will be later used to decode the function return values in the simulator.
pub fn export_fn_abi(
types: &mut Vec<NoirStruct>,
func: &NoirFunction,
) -> Result<(), AztecMacroError> {
let mut parameters_struct_source: Option<&str> = None;

let struct_source = format!(
"
struct {}_parameters {{
{}
}}
",
func.name(),
func.parameters()
.iter()
.map(|param| {
let param_name = match param.pattern.clone() {
Pattern::Identifier(ident) => Ok(ident.0.contents),
_ => Err(AztecMacroError::CouldNotExportFunctionAbi {
span: Some(param.span),
secondary_message: Some(
"Only identifier patterns are supported".to_owned(),
),
}),
};

format!(
"{}: {}",
param_name.unwrap(),
param.typ.typ.to_string().replace("plain::", "")
)
})
.collect::<Vec<String>>()
.join(",\n"),
);

if !func.parameters().is_empty() {
parameters_struct_source = Some(&struct_source);
}

let mut program = String::new();

let parameters = if let Some(parameters_struct_source) = parameters_struct_source {
program.push_str(parameters_struct_source);
format!("parameters: {}_parameters,\n", func.name())
} else {
"".to_string()
};

let return_type_str = func.return_type().typ.to_string().replace("plain::", "");
let return_type = if return_type_str != "()" {
format!("return_type: {},\n", return_type_str)
} else {
"".to_string()
};

let export_struct_source = format!(
"
#[abi(functions)]
struct {}_abi {{
{}{}
}}",
func.name(),
parameters,
return_type
);

program.push_str(&export_struct_source);

let (ast, errors) = parse_program(&program);
if !errors.is_empty() {
return Err(AztecMacroError::CouldNotExportFunctionAbi {
span: None,
secondary_message: Some(
format!("Failed to parse Noir macro code (struct {}_abi). This is either a bug in the compiler or the Noir macro code", func.name())
)
});
}

let sorted_ast = ast.into_sorted();
types.extend(sorted_ast.types);
Ok(())
}

/// Transform Unconstrained
///
/// Inserts the following code at the beginning of an unconstrained function
Expand Down
6 changes: 6 additions & 0 deletions noir/noir-repo/aztec_macros/src/utils/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ pub enum AztecMacroError {
CouldNotImplementNoteInterface { span: Option<Span>, secondary_message: Option<String> },
MultipleStorageDefinitions { span: Option<Span> },
CouldNotExportStorageLayout { span: Option<Span>, secondary_message: Option<String> },
CouldNotExportFunctionAbi { span: Option<Span>, secondary_message: Option<String> },
EventError { span: Span, message: String },
UnsupportedAttributes { span: Span, secondary_message: Option<String> },
}
Expand Down Expand Up @@ -60,6 +61,11 @@ impl From<AztecMacroError> for MacroError {
secondary_message,
span,
},
AztecMacroError::CouldNotExportFunctionAbi { secondary_message, span } => MacroError {
primary_message: "Could not generate and export function abi".to_string(),
secondary_message,
span,
},
AztecMacroError::EventError { span, message } => MacroError {
primary_message: message,
secondary_message: None,
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/constants.gen.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export const REGISTERER_UNCONSTRAINED_FUNCTION_BROADCASTED_MAGIC_VALUE =
0xe7af816635466f128568edb04c9fa024f6c87fb9010fdbffa68b3d99n;
export const DEPLOYER_CONTRACT_INSTANCE_DEPLOYED_MAGIC_VALUE =
0x85864497636cf755ae7bde03f267ce01a520981c21c3682aaf82a631n;
export const DEPLOYER_CONTRACT_ADDRESS = 0x12bcd3e09e8d8559c75d59552db47471f63c72fbbc9decb59c517b4e58634a72n;
export const DEPLOYER_CONTRACT_ADDRESS = 0x1b628eeb6349f2a4c000b703942eb8a625bfe5e6ee34ccc210748cf9ae05af98n;
export const L1_TO_L2_MESSAGE_ORACLE_CALL_LENGTH = 17;
export const MAX_NOTE_FIELDS_LENGTH = 20;
export const GET_NOTE_ORACLE_RETURN_LENGTH = 23;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ describe('ArtifactHash', () => {
it('calculates the artifact hash', () => {
const artifact = getBenchmarkContractArtifact();
expect(computeArtifactHash(artifact).toString()).toMatchInlineSnapshot(
`"0x011603d7f02ebec628e8f1b2458edff811648ea3af5399cec32302aab6217b26"`,
`"0x2000991c126ffc45142660f7c3ad26f65aeb853a3b9c7e5906ba8205a2556128"`,
);
});
});

0 comments on commit 7b88bac

Please sign in to comment.