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

feat: add noir-compiler checks to aztec_macros #4031

Merged
merged 3 commits into from
Jan 12, 2024
Merged
Changes from all commits
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
37 changes: 37 additions & 0 deletions aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,15 @@ impl MacroProcessor for AztecMacro {
}
}

const FUNCTION_TREE_HEIGHT: u32 = 5;
const MAX_CONTRACT_FUNCTIONS: usize = 2_usize.pow(FUNCTION_TREE_HEIGHT);
Comment on lines +34 to +35
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we have circuits.js/src/scripts/constants.in.ts spit out a .rs file in noir with this data?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes - very good point

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.


#[derive(Debug, Clone)]
pub enum AztecMacroError {
AztecNotFound,
AztecComputeNoteHashAndNullifierNotFound { span: Span },
AztecContractHasTooManyFunctions { span: Span },
AztecContractConstructorMissing { span: Span },
}

impl From<AztecMacroError> for MacroError {
Expand All @@ -50,6 +55,16 @@ impl From<AztecMacroError> for MacroError {
secondary_message: None,
span: Some(span),
},
AztecMacroError::AztecContractHasTooManyFunctions { span } => MacroError {
primary_message: format!("Contract can only have a maximum of {} functions", MAX_CONTRACT_FUNCTIONS),
secondary_message: None,
span: Some(span),
},
AztecMacroError::AztecContractConstructorMissing { span } => MacroError {
primary_message: "Contract must have a constructor function".to_owned(),
secondary_message: None,
span: Some(span),
},
}
}
}
Expand Down Expand Up @@ -340,6 +355,28 @@ fn transform_module(
has_transformed_module = true;
}
}

if has_transformed_module {
// We only want to run these checks if the macro processor has found the module to be an Aztec contract.

if module.functions.len() > MAX_CONTRACT_FUNCTIONS {
let crate_graph = &context.crate_graph[crate_id];
return Err((
AztecMacroError::AztecContractHasTooManyFunctions { span: Span::default() }.into(),
crate_graph.root_file_id,
));
}

let constructor_defined = module.functions.iter().any(|func| func.name() == "constructor");
if !constructor_defined {
let crate_graph = &context.crate_graph[crate_id];
return Err((
AztecMacroError::AztecContractConstructorMissing { span: Span::default() }.into(),
crate_graph.root_file_id,
));
}
}

Ok(has_transformed_module)
}

Expand Down
Loading