Skip to content

Commit 55e7bc9

Browse files
author
AztecBot
committed
refactor(avm): unify noir macros flow (AztecProtocol/aztec-packages#5461)
Step towards integrating with initializers, etc. However in the future we might want to separate private and public-vm, many things might end up being different enough (see IFs in code).
2 parents a26453a + 0522909 commit 55e7bc9

File tree

5 files changed

+59
-70
lines changed

5 files changed

+59
-70
lines changed

.aztec-sync-commit

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
27bd8d318df486f6d30a01212f9d7894cafcec74
1+
54aee58952b2433ccad83f1b5fc3088957b10fbb

aztec_macros/src/lib.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ mod utils;
44
use transforms::{
55
compute_note_hash_and_nullifier::inject_compute_note_hash_and_nullifier,
66
events::{generate_selector_impl, transform_events},
7-
functions::{transform_function, transform_unconstrained, transform_vm_function},
7+
functions::{transform_function, transform_unconstrained},
88
note_interface::generate_note_interface_impl,
99
storage::{
1010
assign_storage_slots, check_for_storage_definition, check_for_storage_implementation,
@@ -138,19 +138,22 @@ fn transform_module(module: &mut SortedModule) -> Result<bool, AztecMacroError>
138138
}
139139

140140
// Apply transformations to the function based on collected attributes
141-
if is_private || is_public {
141+
if is_private || is_public || is_public_vm {
142142
transform_function(
143-
if is_private { "Private" } else { "Public" },
143+
if is_private {
144+
"Private"
145+
} else if is_public_vm {
146+
"Avm"
147+
} else {
148+
"Public"
149+
},
144150
func,
145151
storage_defined,
146152
is_initializer,
147153
insert_init_check,
148154
is_internal,
149155
)?;
150156
has_transformed_module = true;
151-
} else if is_public_vm {
152-
transform_vm_function(func, storage_defined)?;
153-
has_transformed_module = true;
154157
} else if storage_defined && func.def.is_unconstrained {
155158
transform_unconstrained(func);
156159
has_transformed_module = true;

aztec_macros/src/transforms/functions.rs

Lines changed: 42 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ pub fn transform_function(
3535
let context_name = format!("{}Context", ty);
3636
let inputs_name = format!("{}ContextInputs", ty);
3737
let return_type_name = format!("{}CircuitPublicInputs", ty);
38+
let is_avm = ty == "Avm";
3839

3940
// Add check that msg sender equals this address and flag function as internal
4041
if is_internal {
@@ -60,20 +61,26 @@ pub fn transform_function(
6061
}
6162

6263
// Insert the context creation as the first action
63-
let create_context = create_context(&context_name, &func.def.parameters)?;
64+
let create_context = if !is_avm {
65+
create_context(&context_name, &func.def.parameters)?
66+
} else {
67+
create_context_avm()?
68+
};
6469
func.def.body.statements.splice(0..0, (create_context).iter().cloned());
6570

6671
// Add the inputs to the params
6772
let input = create_inputs(&inputs_name);
6873
func.def.parameters.insert(0, input);
6974

7075
// Abstract return types such that they get added to the kernel's return_values
71-
if let Some(return_values) = abstract_return_values(func) {
72-
// In case we are pushing return values to the context, we remove the statement that originated it
73-
// This avoids running duplicate code, since blocks like if/else can be value returning statements
74-
func.def.body.statements.pop();
75-
// Add the new return statement
76-
func.def.body.statements.push(return_values);
76+
if !is_avm {
77+
if let Some(return_values) = abstract_return_values(func) {
78+
// In case we are pushing return values to the context, we remove the statement that originated it
79+
// This avoids running duplicate code, since blocks like if/else can be value returning statements
80+
func.def.body.statements.pop();
81+
// Add the new return statement
82+
func.def.body.statements.push(return_values);
83+
}
7784
}
7885

7986
// Before returning mark the contract as initialized
@@ -83,49 +90,29 @@ pub fn transform_function(
8390
}
8491

8592
// Push the finish method call to the end of the function
86-
let finish_def = create_context_finish();
87-
func.def.body.statements.push(finish_def);
93+
if !is_avm {
94+
let finish_def = create_context_finish();
95+
func.def.body.statements.push(finish_def);
96+
}
8897

89-
let return_type = create_return_type(&return_type_name);
90-
func.def.return_type = return_type;
91-
func.def.return_visibility = Visibility::Public;
98+
// The AVM doesn't need a return type yet.
99+
if !is_avm {
100+
let return_type = create_return_type(&return_type_name);
101+
func.def.return_type = return_type;
102+
func.def.return_visibility = Visibility::Public;
103+
}
92104

93105
// Distinct return types are only required for private functions
94106
// Public functions should have unconstrained auto-inferred
95107
match ty {
96108
"Private" => func.def.return_distinctness = Distinctness::Distinct,
97-
"Public" => func.def.is_unconstrained = true,
109+
"Public" | "Avm" => func.def.is_unconstrained = true,
98110
_ => (),
99111
}
100112

101113
Ok(())
102114
}
103115

104-
/// Transform a function to work with AVM bytecode
105-
pub fn transform_vm_function(
106-
func: &mut NoirFunction,
107-
storage_defined: bool,
108-
) -> Result<(), AztecMacroError> {
109-
// Create access to storage
110-
if storage_defined {
111-
let storage = abstract_storage("public_vm", true);
112-
func.def.body.statements.insert(0, storage);
113-
}
114-
115-
// Push Avm context creation to the beginning of the function
116-
let create_context = create_avm_context()?;
117-
func.def.body.statements.insert(0, create_context);
118-
119-
// Add the inputs to the params (first!)
120-
let input = create_inputs("AvmContextInputs");
121-
func.def.parameters.insert(0, input);
122-
123-
// We want the function to be seen as a public function
124-
func.def.is_unconstrained = true;
125-
126-
Ok(())
127-
}
128-
129116
/// Transform Unconstrained
130117
///
131118
/// Inserts the following code at the beginning of an unconstrained function
@@ -339,36 +326,36 @@ fn create_context(ty: &str, params: &[Param]) -> Result<Vec<Statement>, AztecMac
339326
Ok(injected_expressions)
340327
}
341328

342-
/// Creates an mutable avm context
329+
/// Creates the private context object to be accessed within the function, the parameters need to be extracted to be
330+
/// appended into the args hash object.
343331
///
332+
/// The replaced code:
344333
/// ```noir
345-
/// /// Before
346334
/// #[aztec(public-vm)]
347-
/// fn foo() -> Field {
348-
/// let mut context = aztec::context::AVMContext::new();
349-
/// let timestamp = context.timestamp();
350-
/// // ...
351-
/// }
352-
///
353-
/// /// After
354-
/// #[aztec(private)]
355-
/// fn foo() -> Field {
356-
/// let mut timestamp = context.timestamp();
357-
/// // ...
335+
/// fn foo(inputs: AvmContextInputs, ...) -> Field {
336+
/// let mut context = AvmContext::new(inputs);
358337
/// }
359-
fn create_avm_context() -> Result<Statement, AztecMacroError> {
338+
/// ```
339+
fn create_context_avm() -> Result<Vec<Statement>, AztecMacroError> {
340+
let mut injected_expressions: Vec<Statement> = vec![];
341+
360342
// Create the inputs to the context
343+
let ty = "AvmContext";
361344
let inputs_expression = variable("inputs");
345+
let path_snippet = ty.to_case(Case::Snake); // e.g. private_context
362346

347+
// let mut context = {ty}::new(inputs, hash);
363348
let let_context = mutable_assignment(
364349
"context", // Assigned to
365350
call(
366-
variable_path(chained_dep!("aztec", "context", "AVMContext", "new")), // Path
367-
vec![inputs_expression], // args
351+
variable_path(chained_dep!("aztec", "context", &path_snippet, ty, "new")), // Path
352+
vec![inputs_expression], // args
368353
),
369354
);
355+
injected_expressions.push(let_context);
370356

371-
Ok(let_context)
357+
// Return all expressions that will be injected by the hasher
358+
Ok(injected_expressions)
372359
}
373360

374361
/// Abstract Return Type

tooling/noir_js_backend_barretenberg/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"lint": "NODE_NO_WARNINGS=1 eslint . --ext .ts --ignore-path ./.eslintignore --max-warnings 0"
4343
},
4444
"dependencies": {
45-
"@aztec/bb.js": "0.31.0",
45+
"@aztec/bb.js": "portal:../../../../barretenberg/ts",
4646
"@noir-lang/types": "workspace:*",
4747
"fflate": "^0.8.0"
4848
},

yarn.lock

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -221,19 +221,18 @@ __metadata:
221221
languageName: node
222222
linkType: hard
223223

224-
"@aztec/bb.js@npm:0.31.0":
225-
version: 0.31.0
226-
resolution: "@aztec/bb.js@npm:0.31.0"
224+
"@aztec/bb.js@portal:../../../../barretenberg/ts::locator=%40noir-lang%2Fbackend_barretenberg%40workspace%3Atooling%2Fnoir_js_backend_barretenberg":
225+
version: 0.0.0-use.local
226+
resolution: "@aztec/bb.js@portal:../../../../barretenberg/ts::locator=%40noir-lang%2Fbackend_barretenberg%40workspace%3Atooling%2Fnoir_js_backend_barretenberg"
227227
dependencies:
228228
comlink: ^4.4.1
229229
commander: ^10.0.1
230230
debug: ^4.3.4
231231
tslib: ^2.4.0
232232
bin:
233-
bb.js: dest/node/main.js
234-
checksum: 3bcd8cef1fb0b5767332b757875719b4b4f2c34d5b4ed22984f305b920afbb74e17ccf626b3efe66cae904c3df8f74de1aa8c798b17195be13db60a65730bd98
233+
bb.js: ./dest/node/main.js
235234
languageName: node
236-
linkType: hard
235+
linkType: soft
237236

238237
"@babel/code-frame@npm:^7.0.0, @babel/code-frame@npm:^7.10.4, @babel/code-frame@npm:^7.12.11, @babel/code-frame@npm:^7.16.0, @babel/code-frame@npm:^7.22.13, @babel/code-frame@npm:^7.23.5, @babel/code-frame@npm:^7.8.3":
239238
version: 7.23.5
@@ -4396,7 +4395,7 @@ __metadata:
43964395
version: 0.0.0-use.local
43974396
resolution: "@noir-lang/backend_barretenberg@workspace:tooling/noir_js_backend_barretenberg"
43984397
dependencies:
4399-
"@aztec/bb.js": 0.31.0
4398+
"@aztec/bb.js": "portal:../../../../barretenberg/ts"
44004399
"@noir-lang/types": "workspace:*"
44014400
"@types/node": ^20.6.2
44024401
"@types/prettier": ^3

0 commit comments

Comments
 (0)