Skip to content

Commit

Permalink
feat!: introduce UnconstrainedContext (#6752)
Browse files Browse the repository at this point in the history
Continuing the work from
#6442, this PR
further formalizes the notion of a top-level unconstrained execution
context by introducing a struct that represents it (instead of relying
on the unit type). Not only is this less cryptic, it also provides
access to data previously unavailable such as the current block number
and contract address, which we'll need for some unconstrained getters
like `SharedMutable`'s.

The macro functions could potentially be refactored somewhat now that
private, public and unconstrained are more similar, but I'm not sure we
want to invest much effort there so I made the change as small as
possible.
  • Loading branch information
nventuro authored and Maddiaa0 committed Jun 5, 2024
1 parent 6e86abd commit 0583cd7
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions noir/noir-repo/aztec_macros/src/transforms/functions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,12 +216,30 @@ pub fn export_fn_abi(
///
/// Inserts the following code at the beginning of an unconstrained function
/// ```noir
/// let storage = Storage::init(Context::none());
/// let context = UnconstrainedContext::new();
/// let storage = Storage::init(context);
/// ```
///
/// This will allow developers to access their contract' storage struct in unconstrained functions
pub fn transform_unconstrained(func: &mut NoirFunction, storage_struct_name: String) {
// let context = UnconstrainedContext::new();
let let_context = assignment(
"context", // Assigned to
call(
variable_path(chained_dep!(
"aztec",
"context",
"unconstrained_context",
"UnconstrainedContext",
"new"
)),
vec![],
),
);

// We inject the statements at the beginning, in reverse order.
func.def.body.statements.insert(0, abstract_storage(storage_struct_name, true));
func.def.body.statements.insert(0, let_context);
}

/// Helper function that returns what the private context would look like in the ast
Expand Down Expand Up @@ -597,30 +615,26 @@ fn abstract_return_values(func: &NoirFunction) -> Result<Option<Vec<Statement>>,
/// ```noir
/// #[aztec(private)]
/// fn lol() {
/// let storage = Storage::init(context);
/// let storage = Storage::init(&mut context);
/// }
/// ```
///
/// For public functions:
/// ```noir
/// #[aztec(public)]
/// fn lol() {
/// let storage = Storage::init(context);
/// let storage = Storage::init(&mut context);
/// }
/// ```
///
/// For unconstrained functions:
/// ```noir
/// unconstrained fn lol() {
/// let storage = Storage::init(());
/// let storage = Storage::init(context);
/// }
fn abstract_storage(storage_struct_name: String, unconstrained: bool) -> Statement {
let context_expr = if unconstrained {
// Note that the literal unit type (i.e. '()') is not the same as a tuple with zero elements
expression(ExpressionKind::Literal(Literal::Unit))
} else {
mutable_reference("context")
};
let context_expr =
if unconstrained { variable("context") } else { mutable_reference("context") };

assignment(
"storage", // Assigned to
Expand Down

0 comments on commit 0583cd7

Please sign in to comment.