Skip to content

Commit

Permalink
refactor(avm)!: remove PublicContextInputs
Browse files Browse the repository at this point in the history
  • Loading branch information
fcarreiro committed Sep 25, 2024
1 parent dae82d8 commit ee3da1d
Show file tree
Hide file tree
Showing 14 changed files with 54 additions and 151 deletions.
26 changes: 13 additions & 13 deletions noir-projects/aztec-nr/aztec/src/context/call_interfaces.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use dep::protocol_types::{

use crate::context::{
private_context::PrivateContext, public_context::PublicContext, gas::GasOpts,
inputs::{PrivateContextInputs, PublicContextInputs}
inputs::PrivateContextInputs
};

use crate::oracle::arguments::pack_arguments;
Expand Down Expand Up @@ -164,8 +164,8 @@ impl<let N: u32, Env> PrivateStaticVoidCallInterface<N, Env> {
}
}

impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, T, Env> for PublicCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> T {
impl<let N: u32, T, P, Env> CallInterface<N, T, Env> for PublicCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env]() -> T {
self.original
}
}
Expand All @@ -176,7 +176,7 @@ pub struct PublicCallInterface<let N: u32, T, Env> {
name: str<N>,
args: [Field],
gas_opts: GasOpts,
original: fn[Env](PublicContextInputs) -> T,
original: fn[Env]() -> T,
is_static: bool
}

Expand Down Expand Up @@ -238,8 +238,8 @@ impl<let N: u32, T, Env> PublicCallInterface<N, T, Env> {
}
}

impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, (), Env> for PublicVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> () {
impl<let N: u32, T, P, Env> CallInterface<N, (), Env> for PublicVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env]() -> () {
self.original
}
}
Expand All @@ -249,7 +249,7 @@ pub struct PublicVoidCallInterface<let N: u32, Env> {
selector: FunctionSelector,
name: str<N>,
args: [Field],
original: fn[Env](PublicContextInputs) -> (),
original: fn[Env]() -> (),
is_static: bool,
gas_opts: GasOpts
}
Expand Down Expand Up @@ -312,8 +312,8 @@ impl<let N: u32, Env> PublicVoidCallInterface<N, Env> {
}
}

impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, T, Env> for PublicStaticCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> T {
impl<let N: u32, T, P, Env> CallInterface<N, T, Env> for PublicStaticCallInterface<N, T, Env> {
fn get_original(self) -> fn[Env]() -> T {
self.original
}
}
Expand All @@ -323,7 +323,7 @@ pub struct PublicStaticCallInterface<let N: u32, T, Env> {
selector: FunctionSelector,
name: str<N>,
args: [Field],
original: fn[Env](PublicContextInputs) -> T,
original: fn[Env]() -> T,
is_static: bool,
gas_opts: GasOpts
}
Expand Down Expand Up @@ -353,8 +353,8 @@ impl<let N: u32, T, Env> PublicStaticCallInterface<N, T, Env> {
}
}

impl<let N: u32, T, P, Env> CallInterface<N, PublicContextInputs, (), Env> for PublicStaticVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env](PublicContextInputs) -> () {
impl<let N: u32, T, P, Env> CallInterface<N, (), Env> for PublicStaticVoidCallInterface<N, Env> {
fn get_original(self) -> fn[Env]() -> () {
self.original
}
}
Expand All @@ -364,7 +364,7 @@ pub struct PublicStaticVoidCallInterface<let N: u32, Env> {
selector: FunctionSelector,
name: str<N>,
args: [Field],
original: fn[Env](PublicContextInputs) -> (),
original: fn[Env]() -> (),
is_static: bool,
gas_opts: GasOpts
}
Expand Down
2 changes: 0 additions & 2 deletions noir-projects/aztec-nr/aztec/src/context/inputs/mod.nr
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
mod private_context_inputs;
mod public_context_inputs;

pub use crate::context::inputs::private_context_inputs::PrivateContextInputs;
pub use crate::context::inputs::public_context_inputs::PublicContextInputs;

This file was deleted.

8 changes: 3 additions & 5 deletions noir-projects/aztec-nr/aztec/src/context/public_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,17 @@ use dep::protocol_types::address::{AztecAddress, EthAddress};
use dep::protocol_types::constants::MAX_FIELD_VALUE;
use dep::protocol_types::traits::{Serialize, Deserialize, Empty};
use dep::protocol_types::abis::function_selector::FunctionSelector;
use crate::context::inputs::public_context_inputs::PublicContextInputs;
use crate::context::gas::GasOpts;
use crate::hash::ArgsHasher;

pub struct PublicContext {
inputs: PublicContextInputs,
args_hash: Option<Field>,
compute_args_hash: fn () -> Field,
}

impl PublicContext {
pub fn new(inputs: PublicContextInputs, compute_args_hash: fn() -> Field) -> Self {
PublicContext { inputs, args_hash: Option::none(), compute_args_hash }
pub fn new(compute_args_hash: fn() -> Field) -> Self {
PublicContext { args_hash: Option::none(), compute_args_hash }
}

pub fn emit_unencrypted_log<T, let N: u32>(_self: &mut Self, log: T) where T: Serialize<N> {
Expand Down Expand Up @@ -308,7 +306,7 @@ unconstrained fn storage_write(storage_slot: Field, value: Field) {

impl Empty for PublicContext {
fn empty() -> Self {
PublicContext::new(PublicContextInputs::empty(), || 0)
PublicContext::new(|| 0)
}
}

Expand Down
18 changes: 2 additions & 16 deletions noir-projects/aztec-nr/aztec/src/macros/functions/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -232,31 +232,17 @@ pub comptime fn public(f: FunctionDefinition) -> Quoted {
let module_has_initializer = module_has_initializer(f.module());
let module_has_storage = module_has_storage(f.module());

// Public functions undergo a lot of transformations from their Aztec.nr form into a circuit that can be fed to the
// Public Kernel Circuit.

// First we change the function signature so that it also receives `PublicContextInputs`, which contain information
// about the execution context (e.g. the caller).

// Public functions undergo a lot of transformations from their Aztec.nr form.
let original_params = f.parameters();
f.set_parameters(
&[
(
quote { inputs }, quote { crate::context::inputs::public_context_inputs::PublicContextInputs }.as_type()
)
].append(original_params)
);

let args_len = original_params.map(|(name, typ): (Quoted, Type)| flatten_to_fields(name, typ, &[]).0.len()).fold(0, |acc: u32, val: u32| acc + val);

// Unlike in the private case, in public the `context` does not need to receive the hash of the original params.
let context_creation = quote { let mut context = dep::aztec::context::public_context::PublicContext::new(inputs, || {
let serialized_args : [Field; $args_len] = dep::aztec::context::public_context::calldata_copy(2, $args_len);
let serialized_args : [Field; $args_len] = dep::aztec::context::public_context::calldata_copy(0, $args_len);
dep::aztec::hash::hash_args_array(serialized_args)
}); };

// Modifications introduced by the different marker attributes.

let internal_check = if is_fn_internal(f) {
create_internal_check(f)
} else {
Expand Down
9 changes: 1 addition & 8 deletions noir-projects/aztec-nr/aztec/src/test/helpers/cheatcodes.nr
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::protocol_types::{
abis::function_selector::FunctionSelector, address::AztecAddress,
constants::CONTRACT_INSTANCE_LENGTH, contract_instance::ContractInstance
};
use crate::context::inputs::{PublicContextInputs, PrivateContextInputs};
use crate::context::inputs::PrivateContextInputs;
use crate::test::helpers::utils::TestAccount;
use crate::keys::public_keys::PublicKeys;

Expand All @@ -22,10 +22,6 @@ unconstrained pub fn get_private_context_inputs(historical_block_number: u32) ->
oracle_get_private_context_inputs(historical_block_number)
}

unconstrained pub fn get_public_context_inputs() -> PublicContextInputs {
oracle_get_public_context_inputs()
}

unconstrained pub fn deploy<let N: u32, let M: u32, let P: u32>(
path: str<N>,
name: str<M>,
Expand Down Expand Up @@ -127,9 +123,6 @@ unconstrained fn oracle_advance_blocks_by(blocks: u32) {}
#[oracle(getPrivateContextInputs)]
unconstrained fn oracle_get_private_context_inputs(historical_block_number: u32) -> PrivateContextInputs {}

#[oracle(getPublicContextInputs)]
unconstrained fn oracle_get_public_context_inputs() -> PublicContextInputs {}

#[oracle(deploy)]
unconstrained fn oracle_deploy<let N: u32, let M: u32, let P: u32>(
path: str<N>,
Expand Down
17 changes: 4 additions & 13 deletions noir-projects/aztec-nr/aztec/src/test/helpers/test_environment.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::protocol_types::{
address::AztecAddress, traits::Deserialize
};

use crate::context::inputs::{PublicContextInputs, PrivateContextInputs};
use crate::context::inputs::PrivateContextInputs;
use crate::context::{packed_returns::PackedReturns, call_interfaces::CallInterface};

use crate::context::{PrivateContext, PublicContext, UnconstrainedContext};
Expand Down Expand Up @@ -138,10 +138,7 @@ impl TestEnvironment {
PackedReturns::new(public_inputs.returns_hash).assert_empty();
}

fn call_public<C, let M: u32, T, Env>(
_self: Self,
call_interface: C
) -> T where C: CallInterface<M, PublicContextInputs, T, Env> {
fn call_public<C, let M: u32, T, Env>(_self: Self, call_interface: C) -> T where C: CallInterface<M, T, Env> {
let original_fn = call_interface.get_original();
let original_msg_sender = cheatcodes::get_msg_sender();
let original_contract_address = get_contract_address();
Expand All @@ -154,12 +151,9 @@ impl TestEnvironment {
cheatcodes::set_contract_address(target_address);
cheatcodes::set_msg_sender(original_contract_address);
cheatcodes::set_is_static_call(call_interface.get_is_static());
let mut inputs = cheatcodes::get_public_context_inputs();
inputs.calldata_length = call_interface.get_args().len() as Field;
inputs.is_static_call = call_interface.get_is_static();
cheatcodes::set_calldata(calldata);

let result = original_fn(inputs);
let result = original_fn();

cheatcodes::set_fn_selector(original_fn_selector);
cheatcodes::set_contract_address(original_contract_address);
Expand All @@ -169,10 +163,7 @@ impl TestEnvironment {
result
}

fn assert_public_call_fails<C, let M: u32, T, Env>(
_self: Self,
call_interface: C
) where C: CallInterface<M, PublicContextInputs, T, Env> {
fn assert_public_call_fails<C, let M: u32, T, Env>(_self: Self, call_interface: C) where C: CallInterface<M, T, Env> {
cheatcodes::assert_public_call_fails(
call_interface.get_contract_address(),
call_interface.get_selector(),
Expand Down
9 changes: 3 additions & 6 deletions noir-projects/aztec-nr/aztec/src/test/helpers/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::protocol_types::{
contract_instance::ContractInstance
};

use crate::context::inputs::{PublicContextInputs, PrivateContextInputs};
use crate::context::inputs::PrivateContextInputs;
use crate::context::call_interfaces::CallInterface;
use crate::test::helpers::cheatcodes;
use crate::keys::public_keys::{PUBLIC_KEYS_LENGTH, PublicKeys};
Expand Down Expand Up @@ -68,7 +68,7 @@ impl<let N: u32, let M: u32> Deployer<N, M> {
pub fn with_public_initializer<C, let P: u32, T, Env>(
self,
call_interface: C
) -> ContractInstance where C: CallInterface<P, PublicContextInputs, T, Env> {
) -> ContractInstance where C: CallInterface<P, T, Env> {
let instance = cheatcodes::deploy(
self.path,
self.name,
Expand All @@ -87,12 +87,9 @@ impl<let N: u32, let M: u32> Deployer<N, M> {
cheatcodes::set_contract_address(instance.to_address());
cheatcodes::set_msg_sender(original_contract_address);
cheatcodes::set_is_static_call(call_interface.get_is_static());
let mut inputs = cheatcodes::get_public_context_inputs();
inputs.calldata_length = call_interface.get_args().len() as Field;
inputs.is_static_call = call_interface.get_is_static();
cheatcodes::set_calldata(calldata);

let _result: T = original_fn(inputs);
let _result: T = original_fn();

cheatcodes::set_fn_selector(original_fn_selector);
cheatcodes::set_contract_address(original_contract_address);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ contract AvmTest {

#[public]
fn assert_calldata_copy(args: [Field; 3]) {
let offset = 2; // Make this 0 when PublicContextInputs is removed.
let offset = 0;
let cd: [Field; 3] = dep::aztec::context::public_context::calldata_copy(offset, 3);
assert(cd == args, "Calldata copy failed");
}
Expand Down Expand Up @@ -497,28 +497,26 @@ contract AvmTest {
************************************************************************/
#[public]
fn bulk_testing(args_field: [Field; 10], args_u8: [u8; 10]) {
// Using "inputs" here is a bit of a hack. It's the PublicContextInputs injected
// by the public macros. It's needed by the non-nested call to an entry point function.
set_storage_single(inputs, 30);

dep::aztec::oracle::debug_log::debug_log("set_storage_single");
set_storage_single(30);
dep::aztec::oracle::debug_log::debug_log("set_storage_list");
set_storage_list(inputs, 40, 50);
set_storage_list(40, 50);
dep::aztec::oracle::debug_log::debug_log("read_storage_list");
let _ = set_storage_map(inputs, context.this_address(), 60);
let _ = set_storage_map(context.this_address(), 60);
dep::aztec::oracle::debug_log::debug_log("add_storage_map");
let _ = add_storage_map(inputs, context.this_address(), 10);
let _ = add_storage_map(context.this_address(), 10);
dep::aztec::oracle::debug_log::debug_log("read_storage_map");
let _ = read_storage_map(inputs, context.this_address());
let _ = read_storage_map(context.this_address());
dep::aztec::oracle::debug_log::debug_log("keccak_hash");
let _ = keccak_hash(inputs, args_u8);
let _ = keccak_hash(args_u8);
dep::aztec::oracle::debug_log::debug_log("sha256_hash");
let _ = sha256_hash(inputs, args_u8);
let _ = sha256_hash(args_u8);
dep::aztec::oracle::debug_log::debug_log("poseidon2_hash");
let _ = poseidon2_hash(inputs, args_field);
let _ = poseidon2_hash(args_field);
dep::aztec::oracle::debug_log::debug_log("pedersen_hash");
let _ = pedersen_hash(inputs, args_field);
let _ = pedersen_hash(args_field);
dep::aztec::oracle::debug_log::debug_log("pedersen_hash_with_index");
let _ = pedersen_hash_with_index(inputs, args_field);
let _ = pedersen_hash_with_index(args_field);
dep::aztec::oracle::debug_log::debug_log("test_get_contract_instance");
test_get_contract_instance(inputs);
dep::aztec::oracle::debug_log::debug_log("get_address");
Expand Down Expand Up @@ -550,20 +548,20 @@ contract AvmTest {
dep::aztec::oracle::debug_log::debug_log("emit_unencrypted_log");
let _ = emit_unencrypted_log(inputs);
dep::aztec::oracle::debug_log::debug_log("note_hash_exists");
let _ = note_hash_exists(inputs, 1, 2);
let _ = note_hash_exists(1, 2);
dep::aztec::oracle::debug_log::debug_log("new_note_hash");
let _ = new_note_hash(inputs, 1);
let _ = new_note_hash(1);
dep::aztec::oracle::debug_log::debug_log("new_nullifier");
let _ = new_nullifier(inputs, 1);
let _ = new_nullifier(1);
dep::aztec::oracle::debug_log::debug_log("nullifier_exists");
let _ = nullifier_exists(inputs, 1);
let _ = nullifier_exists(1);
dep::aztec::oracle::debug_log::debug_log("l1_to_l2_msg_exists");
let _ = l1_to_l2_msg_exists(inputs, 1, 2);
let _ = l1_to_l2_msg_exists(1, 2);
dep::aztec::oracle::debug_log::debug_log("send_l2_to_l1_msg");
let _ = send_l2_to_l1_msg(inputs, EthAddress::from_field(0x2020), 1);
let _ = send_l2_to_l1_msg(EthAddress::from_field(0x2020), 1);
dep::aztec::oracle::debug_log::debug_log("nested_call_to_add");
let _ = nested_call_to_add(inputs, 1, 2);
let _ = nested_call_to_add(1, 2);
dep::aztec::oracle::debug_log::debug_log("nested_static_call_to_add");
let _ = nested_static_call_to_add(inputs, 1, 2);
let _ = nested_static_call_to_add(1, 2);
}
}
Loading

0 comments on commit ee3da1d

Please sign in to comment.