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: compute_note_hash_and_nullifier check #3216

Merged
merged 8 commits into from
Oct 26, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
45 changes: 31 additions & 14 deletions compiler/noirc_frontend/src/hir/aztec_library.rs
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,9 @@ pub(crate) fn transform(

// Covers all functions in the ast
for submodule in ast.submodules.iter_mut().filter(|submodule| submodule.is_contract) {
let storage_defined = check_for_storage_definition(&submodule.contents);

if transform_module(&mut submodule.contents, storage_defined) {
match check_for_aztec_dependency(crate_id, context) {
Ok(()) => include_relevant_imports(&mut submodule.contents),
Err(file_id) => {
return Err((DefCollectorErrorKind::AztecNotFound {}, file_id));
}
if transform_module(&mut submodule.contents)? {
if check_for_aztec_dependency(crate_id, context)? {
include_relevant_imports(&mut submodule.contents);
benesjan marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down Expand Up @@ -209,19 +204,30 @@ fn include_relevant_imports(ast: &mut SortedModule) {
}

/// Creates an error alerting the user that they have not downloaded the Aztec-noir library
fn check_for_aztec_dependency(crate_id: &CrateId, context: &Context) -> Result<(), FileId> {
fn check_for_aztec_dependency(
crate_id: &CrateId,
context: &Context,
) -> Result<bool, (DefCollectorErrorKind, FileId)> {
let crate_graph = &context.crate_graph[crate_id];
let has_aztec_dependency = crate_graph.dependencies.iter().any(|dep| dep.as_name() == "aztec");
if has_aztec_dependency {
Ok(())
Ok(true)
benesjan marked this conversation as resolved.
Show resolved Hide resolved
} else {
Err(crate_graph.root_file_id)
Err((DefCollectorErrorKind::AztecNotFound {}, crate_graph.root_file_id))
}
}

// Check to see if the user has defined a storage struct
fn check_for_storage_definition(module: &SortedModule) -> bool {
module.types.iter().any(|function| function.name.0.contents == "Storage")
module.types.iter().any(|r#struct| r#struct.name.0.contents == "Storage")
}

// Check to see if the user has defined a compute_note_hash_and_nullifier function exists
fn check_for_compute_note_hash_and_nullifier_definition(module: &SortedModule) -> bool {
module
.functions
.iter()
.any(|func| func.def.name.0.contents == "compute_note_hash_and_nullifier")
Maddiaa0 marked this conversation as resolved.
Show resolved Hide resolved
}

/// Checks if an attribute is a custom attribute with a specific name
Expand All @@ -236,9 +242,20 @@ fn is_custom_attribute(attr: &SecondaryAttribute, attribute_name: &str) -> bool
/// Determines if ast nodes are annotated with aztec attributes.
/// For annotated functions it calls the `transform` function which will perform the required transformations.
/// Returns true if an annotated node is found, false otherwise
fn transform_module(module: &mut SortedModule, storage_defined: bool) -> bool {
fn transform_module(module: &mut SortedModule) -> Result<bool, (DefCollectorErrorKind, FileId)> {
let mut has_transformed_module = false;

// Check for a user defined storage struct
let storage_defined = check_for_storage_definition(&module);

if storage_defined && check_for_compute_note_hash_and_nullifier_definition(&module) {
// TODO: do a refactor so that file_id is real --> you want to get crate_graph.root_file_id
return Err((
benesjan marked this conversation as resolved.
Show resolved Hide resolved
DefCollectorErrorKind::AztecComputeNoteHashAndNullifierNotFound {},
FileId::default(),
));
}

for structure in module.types.iter() {
if structure.attributes.iter().any(|attr| matches!(attr, SecondaryAttribute::Event)) {
module.impls.push(generate_selector_impl(structure));
Expand All @@ -262,7 +279,7 @@ fn transform_module(module: &mut SortedModule, storage_defined: bool) -> bool {
has_transformed_module = true;
}
}
has_transformed_module
Ok(has_transformed_module)
}

/// If it does, it will insert the following things:
Expand Down
11 changes: 8 additions & 3 deletions compiler/noirc_frontend/src/hir/def_collector/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,18 @@ pub enum DefCollectorErrorKind {
ModuleAlreadyPartOfCrate { mod_name: Ident, span: Span },
#[error("Module was originally declared here")]
ModuleOriginallyDefined { mod_name: Ident, span: Span },
#[cfg(feature = "aztec")]
#[error("Aztec dependency not found. Please add aztec as a dependency in your Cargo.toml")]
AztecNotFound {},
#[error(
"Either the type or the trait must be from the same crate as the trait implementation"
)]
TraitImplOrphaned { span: Span },

// Aztec feature flag errors
#[cfg(feature = "aztec")]
#[error("Aztec dependency not found. Please add aztec as a dependency in your Cargo.toml")]
AztecNotFound {},
#[cfg(feature = "aztec")]
#[error("compute_note_hash_and_nullifier function not found. Define it in your contract.")]
AztecComputeNoteHashAndNullifierNotFound {},
}

impl DefCollectorErrorKind {
Expand Down