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: Aztec prelude #4496

Closed
wants to merge 48 commits into from
Closed
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
48 commits
Select commit Hold shift + click to select a range
bebf5b5
add initial work for inserting an aztec prelude and remove imports fr…
vezenovm Feb 7, 2024
db50e56
merge conflicts w/ master
vezenovm Feb 7, 2024
e67eb78
delete more aztec deps in the contracts
vezenovm Feb 7, 2024
537c3d2
update methods in e2e tests
vezenovm Feb 8, 2024
4c3d679
fix macro imports and merge conflicts w/ master
vezenovm Feb 8, 2024
54d0be2
remove remaining imports from ecdsa_account_contract
vezenovm Feb 8, 2024
ceaba2c
fix e2e test and move prelude source to bottom of file
vezenovm Feb 8, 2024
188ffe7
update benchmarks e2e
vezenovm Feb 8, 2024
a117ec3
stateful test create_new_note
vezenovm Feb 8, 2024
4012615
format
vezenovm Feb 8, 2024
d91f69c
run tests on single thread
vezenovm Feb 8, 2024
1dbf3d5
missing maxWorkers
vezenovm Feb 8, 2024
8aa6ad8
fix stateful constructor and run tests on many threads
vezenovm Feb 8, 2024
fc01877
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 8, 2024
f03ca68
fix get public key test
vezenovm Feb 8, 2024
d584abc
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 8, 2024
f834222
update e2e_deploy_contract test after master merge
vezenovm Feb 8, 2024
65bb865
merge conflcits w/ master
vezenovm Feb 15, 2024
423e23c
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 15, 2024
87bdde6
all contracts working with new prelde
vezenovm Feb 15, 2024
159980d
fix e2e tests
vezenovm Feb 15, 2024
872d70e
cargo fmt
vezenovm Feb 15, 2024
8f96895
remove changes to slow_map.nr
vezenovm Feb 15, 2024
33a2d9c
cleanup has_aztec_dep func
vezenovm Feb 15, 2024
d795c27
use prelude.nr inside of aztec-nr
vezenovm Feb 15, 2024
9d4844f
cargo fmt
vezenovm Feb 15, 2024
f8467e7
rename process_crate_prelude to fetch_crate_prelude
vezenovm Feb 15, 2024
84b2c24
add note imports to prelude
vezenovm Feb 15, 2024
30b8ef7
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 15, 2024
187b17e
remove more imports due to prelude
vezenovm Feb 16, 2024
701519b
remove PrivateContext import
vezenovm Feb 16, 2024
cf6d1e5
resolve master merge conflicts
vezenovm Feb 16, 2024
9b919f6
dont edit test_contract autogen file
vezenovm Feb 16, 2024
ed10939
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 19, 2024
3ccd7dc
remove unused method
vezenovm Feb 19, 2024
82d889b
merge conflcits w/ master
vezenovm Feb 19, 2024
359f259
cargo fmt
vezenovm Feb 19, 2024
a160e92
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 19, 2024
2e3bd3b
Merge branch 'master' into mv/aztec-prelude
vezenovm Feb 19, 2024
b3cefdf
Merge branch 'master' into mv/aztec-prelude
benesjan Feb 29, 2024
266c2b9
fixes after merge
benesjan Feb 29, 2024
4b80987
import cleanup
benesjan Feb 29, 2024
a687f68
Merge branch 'master' into mv/aztec-prelude
benesjan Feb 29, 2024
7328b80
Merge branch 'master' into mv/aztec-prelude
TomAFrench Mar 4, 2024
5e30067
chore: undo unwanted formatting changes
TomAFrench Mar 4, 2024
1269ed7
chore: prevent formatter from running
TomAFrench Mar 4, 2024
35a1472
chore: revert more formatting changes
TomAFrench Mar 4, 2024
3a0796c
chore: one more
TomAFrench Mar 4, 2024
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
201 changes: 166 additions & 35 deletions noir/aztec_macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,21 @@ use std::vec;

use convert_case::{Case, Casing};
use iter_extended::vecmap;
use noirc_frontend::macros_api::parse_program;
use noirc_frontend::macros_api::HirImportDirective;
use noirc_frontend::macros_api::FieldElement;
use noirc_frontend::macros_api::{
BlockExpression, CallExpression, CastExpression, Distinctness, Expression, ExpressionKind,
ForLoopStatement, ForRange, FunctionDefinition, FunctionReturnType, FunctionVisibility,
HirContext, HirExpression, HirLiteral, HirStatement, Ident, ImportStatement, IndexExpression,
HirContext, HirExpression, HirLiteral, HirStatement, Ident, IndexExpression,
LetStatement, Literal, MemberAccessExpression, MethodCallExpression, NoirFunction, NoirStruct,
Param, Path, PathKind, Pattern, PrefixExpression, SecondaryAttribute, Signedness, Span,
Param, Path, Pattern, PrefixExpression, SecondaryAttribute, Signedness, Span,
Statement, StatementKind, StructType, Type, TypeImpl, UnaryOp, UnresolvedType,
UnresolvedTypeData, Visibility,
};
use noirc_frontend::macros_api::{CrateId, FileId};
use noirc_frontend::macros_api::{MacroError, MacroProcessor};
use noirc_frontend::macros_api::{ModuleDefId, NodeInterner, SortedModule, StructId};
use noirc_frontend::macros_api::{LocalModuleId, ModuleDefId, NodeInterner, SortedModule, StructId};
use noirc_frontend::node_interner::{TraitId, TraitImplKind};
use noirc_frontend::Lambda;

Expand All @@ -31,6 +33,31 @@ impl MacroProcessor for AztecMacro {
transform(ast, crate_id, context)
}

fn process_crate_prelude(
&self,
crate_id: &CrateId,
context: &HirContext,
collected_imports: &mut Vec<HirImportDirective>,
submodules: &[LocalModuleId],
) -> Result<(), (MacroError, FileId)> {
if !has_aztec_dependency(crate_id, context) {
return Ok(());
}
let def_map = context.def_map(crate_id).expect("Should have a def map");

let crate_root = def_map.root();
inject_aztec_prelude(*crate_id, crate_root, collected_imports);
for submodule in submodules.iter() {
inject_aztec_prelude(
*crate_id,
*submodule,
collected_imports,
);
}

Ok(())
}

fn process_typed_ast(
&self,
crate_id: &CrateId,
Expand Down Expand Up @@ -216,19 +243,6 @@ macro_rules! chained_path {
}
}

macro_rules! chained_dep {
( $base:expr $(, $tail:expr)* ) => {
{
let mut base_path = ident_path($base);
base_path.kind = PathKind::Dep;
$(
base_path.segments.push(ident($tail));
)*
base_path
}
}
}

fn cast(lhs: Expression, ty: UnresolvedTypeData) -> Expression {
expression(ExpressionKind::Cast(Box::new(CastExpression { lhs, r#type: make_type(ty) })))
}
Expand All @@ -251,10 +265,6 @@ fn index_array_variable(array: Expression, index: &str) -> Expression {
})))
}

fn import(path: Path) -> ImportStatement {
ImportStatement { path, alias: None }
}

//
// Create AST Nodes for Aztec
//
Expand All @@ -274,7 +284,6 @@ fn transform(
.map_err(|(err, file_id)| (err.into(), file_id))?
{
check_for_aztec_dependency(crate_id, context)?;
include_relevant_imports(&mut submodule.contents);
}
}
Ok(ast)
Expand All @@ -293,18 +302,31 @@ fn transform_hir(
assign_storage_slots(crate_id, context)
}

/// Includes an import to the aztec library if it has not been included yet
fn include_relevant_imports(ast: &mut SortedModule) {
// Create the aztec import path using the assumed chained_dep! macro
let aztec_import_path = import(chained_dep!("aztec"));

// Check if the aztec import already exists
let is_aztec_imported =
ast.imports.iter().any(|existing_import| existing_import.path == aztec_import_path.path);

// If aztec is not imported, add the import at the beginning
if !is_aztec_imported {
ast.imports.insert(0, aztec_import_path);
fn inject_aztec_prelude(
crate_id: CrateId,
crate_root: LocalModuleId,
collected_imports: &mut Vec<HirImportDirective>,
) {
let (imports_ast, errors) = parse_program(aztec_prelude_source());
if !errors.is_empty() {
dbg!(errors.clone());
}
assert_eq!(errors.len(), 0, "Failed to parse Noir macro code. This is either a bug in the compiler or the Noir macro code");

let imports_ast = imports_ast.into_sorted();

if !crate_id.is_stdlib() {
for import in imports_ast.imports {
collected_imports.insert(
0,
HirImportDirective {
module_id: crate_root,
path: import.path,
alias: import.alias,
is_prelude: true,
},
);
}
}
}

Expand All @@ -313,12 +335,24 @@ fn check_for_aztec_dependency(
crate_id: &CrateId,
context: &HirContext,
) -> Result<(), (MacroError, FileId)> {
if has_aztec_dependency(crate_id, context) {
Ok(())
} else {
let crate_graph = &context.crate_graph[crate_id];
Err((AztecMacroError::AztecDepNotFound.into(), crate_graph.root_file_id))
}
}

fn has_aztec_dependency(
crate_id: &CrateId,
context: &HirContext,
) -> bool {
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 {
benesjan marked this conversation as resolved.
Show resolved Hide resolved
Ok(())
true
} else {
Err((AztecMacroError::AztecDepNotFound.into(), crate_graph.root_file_id))
false
}
benesjan marked this conversation as resolved.
Show resolved Hide resolved
}

Expand Down Expand Up @@ -1597,3 +1631,100 @@ fn event_signature(event: &StructType) -> String {
let fields = vecmap(event.get_fields(&[]), |(_, typ)| signature_of_type(&typ));
format!("{}({})", event.name.0.contents, fields.join(","))
}

fn aztec_prelude_source() -> &'static str {
let aztec_imports = "
use dep::aztec;

use dep::aztec::{
benesjan marked this conversation as resolved.
Show resolved Hide resolved
protocol_types::{
abis::{
function_selector::FunctionSelector,
call_context::CallContext,
private_circuit_public_inputs::PrivateCircuitPublicInputs,
},
address::{
AztecAddress,
EthAddress,
PartialAddress,
PublicKeysHash,
},
contract_class::ContractClassId,
constants::{
MAX_NOTES_PER_PAGE,
MAX_READ_REQUESTS_PER_CALL,
ARTIFACT_FUNCTION_TREE_MAX_HEIGHT,
FUNCTION_TREE_HEIGHT,
MAX_PACKED_PUBLIC_BYTECODE_SIZE_IN_FIELDS,
REGISTERER_CONTRACT_CLASS_REGISTERED_MAGIC_VALUE,
RETURN_VALUES_LENGTH,
},
grumpkin_point::GrumpkinPoint,
hash::{sha256_to_field, hash_args},
traits::{Serialize, Deserialize},
},
context::{PrivateContext, PublicContext, Context, inputs::private_context_inputs::PrivateContextInputs},
note::{
utils as note_utils,
lifecycle::{create_note, destroy_note},
utils::compute_note_hash_for_consumption,
note_getter_options::NoteGetterOptions,
note_getter_options::NoteStatus,
note_viewer_options::NoteViewerOptions,
note_getter::{get_notes, view_notes},
note_header::NoteHeader,
note_interface::NoteInterface,
},
oracle::{
nullifier_key::get_nullifier_secret_key,
get_public_key::get_public_key,
context::get_portal_address,
rand::rand,
},
log::{
emit_unencrypted_log,
emit_unencrypted_log_from_private,
emit_encrypted_log,
},
hash::{
pedersen_hash,
compute_secret_hash,
},
state_vars::{
map::Map,
public_state::PublicState,
singleton::Singleton,
immutable_singleton::ImmutableSingleton,
set::Set,
stable_public_state::StablePublicState,
},
history::{
contract_inclusion::{
prove_contract_inclusion,
prove_contract_inclusion_at,
},
note_inclusion::{
prove_note_inclusion,
prove_note_inclusion_at,
},
note_validity::{
prove_note_validity,
prove_note_validity_at,
},
nullifier_inclusion::{
prove_nullifier_inclusion,
prove_nullifier_inclusion_at,
},
nullifier_non_inclusion::{
prove_note_not_nullified,
prove_note_not_nullified_at,
},
public_value_inclusion::{
prove_public_value_inclusion,
prove_public_value_inclusion_at,
},
},
};
";
aztec_imports
}
16 changes: 12 additions & 4 deletions noir/compiler/noirc_frontend/src/hir/def_collector/dc_crate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -252,20 +252,28 @@ impl DefCollector {
context,
));

let submodules = vecmap(def_collector.def_map.modules().iter(), |(index, _)| index);
let submodules = vecmap(def_collector.def_map.modules().iter(), |(index, _)| LocalModuleId(index));
// Add the current crate to the collection of DefMaps
context.def_maps.insert(crate_id, def_collector.def_map);

inject_prelude(crate_id, context, crate_root, &mut def_collector.collected_imports);
for submodule in submodules {
for submodule in submodules.iter() {
inject_prelude(
crate_id,
context,
LocalModuleId(submodule),
*submodule,
&mut def_collector.collected_imports,
);
}

for macro_processor in macro_processors.clone() {
benesjan marked this conversation as resolved.
Show resolved Hide resolved
macro_processor.process_crate_prelude(&crate_id, context, &mut def_collector.collected_imports, &submodules).unwrap_or_else(
|(macro_err, file_id)| {
errors.push((macro_err.into(), file_id));
},
);
}

// Resolve unresolved imports collected from the crate, one by one.
for collected_import in def_collector.collected_imports {
match resolve_import(crate_id, collected_import, &context.def_maps) {
Expand Down Expand Up @@ -405,7 +413,7 @@ fn inject_prelude(
let prelude = context.module(module_id).scope().names();

for path in prelude {
let mut segments = segments.clone();
let mut segments: Vec<Ident> = segments.clone();
segments.push(Ident::new(path.to_string(), Span::default()));

collected_imports.insert(
Expand Down
2 changes: 1 addition & 1 deletion noir/compiler/noirc_frontend/src/hir/def_map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ impl CrateDefMap {
if context.def_map(&crate_id).is_some() {
return errors;
}

benesjan marked this conversation as resolved.
Show resolved Hide resolved
// First parse the root file.
let root_file_id = context.crate_graph[crate_id].root_file_id;
let (ast, parsing_errors) = context.parsed_file_results(root_file_id);
Expand Down
4 changes: 4 additions & 0 deletions noir/compiler/noirc_frontend/src/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,10 @@ impl Context<'_, '_> {
self.def_maps.get(crate_id)
}

pub fn def_maps(&self) -> &BTreeMap<CrateId, CrateDefMap> {
&self.def_maps
}

vezenovm marked this conversation as resolved.
Show resolved Hide resolved
/// Return the CrateId for each crate that has been compiled
/// successfully
pub fn crates(&self) -> impl Iterator<Item = CrateId> + '_ {
Expand Down
16 changes: 14 additions & 2 deletions noir/compiler/noirc_frontend/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,13 @@ pub mod macros_api {
pub use crate::hir_def::expr::{HirExpression, HirLiteral};
pub use crate::hir_def::stmt::HirStatement;
pub use crate::node_interner::{NodeInterner, StructId};
pub use crate::parser::SortedModule;
pub use crate::parser::{parse_program, SortedModule};
pub use crate::token::SecondaryAttribute;

pub use crate::hir::def_map::ModuleDefId;
pub use crate::hir::def_collector::dc_crate::DefCollector as HirDefCollector;
pub use crate::hir::resolution::import::ImportDirective as HirImportDirective;
pub use crate::hir::resolution::path_resolver;
pub use crate::hir::def_map::{LocalModuleId, ModuleId, ModuleDefId};
pub use crate::{
hir::Context as HirContext, BlockExpression, CallExpression, CastExpression, Distinctness,
Expression, ExpressionKind, FunctionReturnType, Ident, IndexExpression, LetStatement,
Expand All @@ -73,6 +76,15 @@ pub mod macros_api {
crate_id: &CrateId,
context: &HirContext,
) -> Result<SortedModule, (MacroError, FileId)>;
/// Function to manipulate the crate definition before type checking has been completed.
/// This is needed to process module declarations as the original AST will not yet have them.
fn process_crate_prelude(
&self,
crate_id: &CrateId,
context: &HirContext,
def_collector: &mut Vec<HirImportDirective>,
submodules: &[LocalModuleId],
) -> Result<(), (MacroError, FileId)>;
/// Function to manipulate the AST after type checking has been completed.
/// The AST after type checking has been done is called the HIR.
fn process_typed_ast(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Tests a very simple program.
//
// The features being tested is assertion
fn main(x: Field, y: pub Field) {
fn main(x: Field, y: pub Field) {
benesjan marked this conversation as resolved.
Show resolved Hide resolved
assert(x == y, "x and y are not equal");
assert_eq(x, y, "x and y are not equal");
}
5 changes: 2 additions & 3 deletions yarn-project/aztec-nr/slow-updates-tree/src/slow_map.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use dep::aztec::context::{PrivateContext, PublicContext, Context};
use dep::aztec::oracle::storage::{storage_read, storage_write};
use dep::aztec::protocol_types::traits::{Serialize, Deserialize};
use dep::std::hash::pedersen_hash;
use dep::std::merkle::compute_merkle_root;
use dep::std::option::Option;

Expand Down Expand Up @@ -141,7 +140,7 @@ impl<N,M> SlowMap<N,M> {

// docs:start:read_leaf_at
pub fn read_leaf_at(self: Self, key: Field) -> Leaf {
let derived_storage_slot = pedersen_hash([self.storage_slot, key]);
let derived_storage_slot = dep::std::hash::pedersen_hash([self.storage_slot, key]);
let fields = storage_read(derived_storage_slot);
Leaf::deserialize(fields)
}
Expand Down Expand Up @@ -247,7 +246,7 @@ impl<N,M> SlowMap<N,M> {

// Updates the value in the in storage with no checks.
fn update_unsafe(self: Self, index: Field, leaf: Leaf, root: Leaf) {
let derived_storage_slot = pedersen_hash([self.storage_slot, index]);
let derived_storage_slot = dep::std::hash::pedersen_hash([self.storage_slot, index]);
let fields = leaf.serialize();
storage_write(derived_storage_slot, fields);

Expand Down
Loading
Loading