Skip to main content

The Function Context

What is the context

The context is an object that is made available within every function in Aztec.nr. As mentioned in the kernel circuit documentation. At the beginning of a function's execution, the context contains all of the kernel information that application needs to execute. During the lifecycle of a transaction, the function will update the context with each of it's side effects (created notes, nullifiers etc.). At the end of a function's execution the mutated context is returned to the kernel to be checked for validity.

Behind the scenes, Aztec.nr will pass data the kernel needs to and from a circuit, this is abstracted away from the developer. In an developer's eyes; the context is a useful structure that allows access and mutate the state of the Aztec blockchain.

On this page, you'll learn

  • The details and functionalities of the private context in Aztec.nr
  • Difference between the private and public contexts and their unified APIs
  • Components of the private context, such as inputs, block header, and contract deployment data
  • Elements like return values, read requests, new commitments, and nullifiers in transaction processing
  • Differences between the private and public contexts, especially the unique features and variables in the public context

Two context's one API

The Aztec blockchain contains two environments public and private.

  • Private, for private transactions taking place on user's devices.
  • Public, for public transactions taking place on the network's sequencers.

As there are two distinct execution environments, they both require slightly differing execution contexts. Despite their differences; the API's for interacting with each are unified. Leading to minimal context switch when working between the two environments.

The following section will cover both contexts.

The Private Context

The code snippet below shows what is contained within the private context.

private-context
inputs: abi::PrivateContextInputs,

args_hash : Field,
return_values : BoundedVec<Field, RETURN_VALUES_LENGTH>,

read_requests: BoundedVec<Field, MAX_READ_REQUESTS_PER_CALL>,
pending_read_requests: BoundedVec<Field, MAX_PENDING_READ_REQUESTS_PER_CALL>,

new_commitments: BoundedVec<Field, MAX_NEW_COMMITMENTS_PER_CALL>,
new_nullifiers: BoundedVec<Field, MAX_NEW_NULLIFIERS_PER_CALL>,
nullified_commitments: BoundedVec<Field, MAX_NEW_NULLIFIERS_PER_CALL>,

private_call_stack : BoundedVec<Field, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL>,
public_call_stack : BoundedVec<Field, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL>,
new_l2_to_l1_msgs : BoundedVec<Field, MAX_NEW_L2_TO_L1_MSGS_PER_CALL>,
Source code: /yarn-project/aztec-nr/aztec/src/context.nr#L56-L72

Private Context Broken Down

Inputs

The context inputs includes all of the information that is passed from the kernel circuit into the application circuit. It contains the following values.

private-context-inputs
struct PrivateContextInputs {
call_context : CallContext,
block_header: BlockHeader,
contract_deployment_data: ContractDeploymentData,
private_global_variables: PrivateGlobalVariables,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L94-L101

As shown in the snippet, the application context is made up of 4 main structures. The call context, the block header, the contract deployment data and the private global variables.

First of all, the call context.

call-context
struct CallContext {
msg_sender : Field,
storage_contract_address : Field,
portal_contract_address : Field,
function_selector: Field,

is_delegate_call : bool,
is_static_call : bool,
is_contract_deployment: bool,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L113-L124

The call context contains information about the current call being made:

  1. Msg Sender
    • The message sender is the account (Aztec Contract) that sent the message to the current context. In the first call of the kernel circuit (often the account contract call), this value will be empty. For all subsequent calls the value will be the previous call.

The graphic below illustrates how the message sender changes throughout the kernel circuit iterations.

  1. Storage contract address

    • This value is the address of the current context's contract address. This value will be the value of the current contract that is being executed except for when the current call is a delegate call (Warning: This is yet to be implemented). In this case the value will be that of the sending contract.
  2. Portal Contract Address

    • This value stores the current contract's linked portal contract address. As a quick recap, this value is the value of the contracts related ethereum l1 contract address, and will be the recipient of any messages that are created by this contract.
  3. Flags

    • Furthermore there are a series of flags that are stored within the application context:
      • is_delegate_call: Denotes whether the current call is a delegate call. If true, then the storage contract address will be the address of the sender.
      • is_static_call: This will be set if and only if the current call is a static call. In a static call, state changing altering operations are not allowed.
      • is_contract_deployment: This will be set if and only if the current call is the contract's constructor.

Block Header

Another structure that is contained within the context is the Block Header object. This object is a special one as it contains all of the roots of Aztec's data trees.

block-header
struct BlockHeader {
note_hash_tree_root : Field,
nullifier_tree_root : Field,
contract_tree_root : Field,
l1_to_l2_messages_tree_root : Field,
blocks_tree_root: Field,
public_data_tree_root: Field,
global_variables_hash: Field,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L144-L154

Contract Deployment Data

Just like with the is_contract_deployment flag mentioned earlier. This data will only be set to true when the current transaction is one in which a contract is being deployed.

contract-deployment-data
struct ContractDeploymentData {
deployer_public_key: Point,
constructor_vk_hash : Field,
function_tree_root : Field,
contract_address_salt : Field,
portal_contract_address : Field,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L66-L74

Private Global Variables

In the private execution context, we only have access to a subset of the total global variables, we are restricted to those which can be reliably proven by the kernel circuits.

private-global-variables
struct PrivateGlobalVariables {
chain_id: Field,
version: Field,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L38-L43

Args Hash

To allow for flexibility in the number of arguments supported by Aztec functions, all function inputs are reduced to a singular value which can be proven from within the application.

The args_hash is the result of pedersen hashing all of a function's inputs.

Return Values

The return values are a set of values that are returned from an applications execution to be passed to other functions through the kernel. Developers do not need to worry about passing their function return values to the context directly as Aztec.nr takes care of it for you. See the documentation surrounding Aztec.nr macro expansion for more details.

return_values : BoundedVec<Field, RETURN_VALUES_LENGTH>,

Read Requests

New Commitments

New commitments contains an array of all of the commitments created in the current execution context.

New Nullifiers

New nullifiers contains an array of the new nullifiers emitted from the current execution context.

Nullified Commitments

Nullified commitments is an optimization for introduced to help reduce state growth. There are often cases where commitments are created and nullified within the same transaction.
In these cases there is no reason that these commitments should take up space on the node's commitment/nullifier trees. Keeping track of nullified commitments allows us to "cancel out" and prove these cases.

Private Call Stack

The private call stack contains all of the external private function calls that have been created within the current context. Any function call objects are hashed and then pushed to the execution stack. The kernel circuit will orchestrate dispatching the calls and returning the values to the current context.

Public Call Stack

The public call stack contains all of the external function calls that are created within the current context. Like the private call stack above, the calls are hashed and pushed to this stack. Unlike the private call stack, these calls are not executed client side. Whenever the function is sent to the network, it will have the public call stack attached to it. At this point the sequencer will take over and execute the transactions.

New L2 to L1 msgs

New L2 to L1 messages contains messages that are delivered to the l1 outbox on the execution of each rollup.

Public Context

The Public Context includes all of the information passed from the Public VM into the execution environment. It is very similar to the Private Context, however it has some minor differences (detailed below).

Public Context Inputs

In the current version of the system, the public context is almost a clone of the private execution context. It contains the same call context data, access to the same historical tree roots, however it does NOT have access to contract deployment data, this is due to traditional contract deployments only currently being possible from private transactions.

public-context-inputs
struct PublicContextInputs {
call_context: CallContext,
block_header: BlockHeader,

public_global_variables: PublicGlobalVariables,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L104-L111

Public Global Variables

The public global variables are provided by the rollup sequencer and consequently contain some more values than the private global variables.

public-global-variables
struct PublicGlobalVariables {
chain_id: Field,
version: Field,
block_number: Field,
timestamp: Field,
}
Source code: /yarn-project/aztec-nr/aztec/src/abi.nr#L51-L58