From 3909d315d9ce5d76e6914c229f79e82c300e399d Mon Sep 17 00:00:00 2001 From: Facundo Date: Wed, 20 Mar 2024 17:00:30 +0000 Subject: [PATCH] refactor(aztec-nr): unify contexts behind interfaces (#5294) * Unified common parts of `{Public,Private,AVM}Context` behind `ContextInterface` * Created (temporary) `PublicContextInterface` to unify `Public/AVM` --- aztec/src/context.nr | 6 +- aztec/src/context/avm.nr | 172 ---------------- aztec/src/context/avm_context.nr | 282 +++++++++++++++++++++++++++ aztec/src/context/interface.nr | 39 +++- aztec/src/context/private_context.nr | 5 +- aztec/src/context/public_context.nr | 229 +++++++++++----------- 6 files changed, 441 insertions(+), 292 deletions(-) delete mode 100644 aztec/src/context/avm.nr create mode 100644 aztec/src/context/avm_context.nr diff --git a/aztec/src/context.nr b/aztec/src/context.nr index 9f5ae7ef..4c87c333 100644 --- a/aztec/src/context.nr +++ b/aztec/src/context.nr @@ -3,13 +3,13 @@ mod inputs; mod private_context; mod public_context; +mod avm_context; mod interface; -mod avm; -use private_context::PrivateContext; use interface::ContextInterface; +use private_context::PrivateContext; use public_context::PublicContext; -use avm::AVMContext; +use avm_context::AVMContext; struct Context { private: Option<&mut PrivateContext>, diff --git a/aztec/src/context/avm.nr b/aztec/src/context/avm.nr deleted file mode 100644 index 96902336..00000000 --- a/aztec/src/context/avm.nr +++ /dev/null @@ -1,172 +0,0 @@ -use dep::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH}; -use dep::protocol_types::traits::{Serialize}; -use dep::protocol_types::abis::function_selector::FunctionSelector; - -// Getters that will be converted by the transpiler into their -// own opcodes -struct AVMContext {} - -impl AVMContext { - // Empty new function enables retaining context. syntax - pub fn new() -> Self { - Self {} - } - - // OPCODES - #[oracle(avmOpcodeAddress)] - pub fn address(self) -> AztecAddress {} - - #[oracle(avmOpcodeStorageAddress)] - pub fn storage_address(self) -> AztecAddress {} - - #[oracle(avmOpcodeOrigin)] - pub fn origin(self) -> AztecAddress {} - - #[oracle(avmOpcodeSender)] - pub fn sender(self) -> AztecAddress {} - - #[oracle(avmOpcodePortal)] - pub fn portal(self) -> EthAddress {} - - #[oracle(avmOpcodeFeePerL1Gas)] - pub fn fee_per_l1_gas(self) -> Field {} - - #[oracle(avmOpcodeFeePerL2Gas)] - pub fn fee_per_l2_gas(self) -> Field {} - - #[oracle(avmOpcodeFeePerDaGas)] - pub fn fee_per_da_gas(self) -> Field {} - - #[oracle(avmOpcodeChainId)] - pub fn chain_id(self) -> Field {} - - #[oracle(avmOpcodeVersion)] - pub fn version(self) -> Field {} - - #[oracle(avmOpcodeBlockNumber)] - pub fn block_number(self) -> Field {} - - #[oracle(avmOpcodeTimestamp)] - pub fn timestamp(self) -> Field {} - - // #[oracle(avmOpcodeContractCallDepth)] - // pub fn contract_call_depth(self) -> Field {} - - #[oracle(avmOpcodeNoteHashExists)] - pub fn note_hash_exists(self, note_hash: Field, leaf_index: Field) -> u8 {} - - #[oracle(avmOpcodeEmitNoteHash)] - pub fn emit_note_hash(self, note_hash: Field) {} - - #[oracle(avmOpcodeNullifierExists)] - pub fn nullifier_exists(self, nullifier: Field) -> u8 {} - - #[oracle(avmOpcodeEmitNullifier)] - pub fn emit_nullifier(self, nullifier: Field) {} - - /** - * Emit a log with the given event selector and message. - * - * @param event_selector The event selector for the log. - * @param message The message to emit in the log. - * Should be automatically convertible to [Field; N]. For example str works with - * one char per field. Otherwise you can use CompressedString. - */ - #[oracle(amvOpcodeEmitUnencryptedLog)] - pub fn emit_unencrypted_log(self, event_selector: Field, message: T) {} - - #[oracle(avmOpcodeL1ToL2MsgExists)] - pub fn l1_to_l2_msg_exists(self, msg_hash: Field, msg_leaf_index: Field) -> u8 {} - - #[oracle(avmOpcodeSendL2ToL1Msg)] - pub fn send_l2_to_l1_msg(self, recipient: EthAddress, content: Field) {} - - #[oracle(avmOpcodeCall)] - pub fn call( - self, - gas: [Field; 3], // gas allocation: [l1Gas, l2Gas, daGas] - address: AztecAddress, - args: [Field; ARGS_COUNT], - temporary_function_selector: Field - ) -> ([Field; RET_SIZE], u8) {} - // ^ return data ^ success - - #[oracle(avmOpcodeStaticCall)] - pub fn call_static( - self, - gas: [Field; 3], // gas allocation: [l1Gas, l2Gas, daGas] - address: AztecAddress, - args: [Field; ARGS_COUNT], - // TODO(5110): consider passing in calldata directly - temporary_function_selector: Field - ) -> ([Field; RET_SIZE], u8) {} - // ^ return data ^ success - - //////////////////////////////////////////////////////////////////////////////// - // The functions below allow interface-equivalence with current public/private - //////////////////////////////////////////////////////////////////////////////// - pub fn this_address(self) -> AztecAddress { - self.address() - } - - #[oracle(avmOpcodeSendL2ToL1Msg)] - pub fn message_portal(&mut self, recipient: EthAddress, content: Field) {} - - pub fn consume_l1_to_l2_message( - &mut self, - _msg_key: Field, - _content: Field, - _secret: Field, - _sender: EthAddress - ) { - assert(false, "Not implemented!"); - } - - #[oracle(avmOpcodeEmitNoteHash)] - pub fn push_new_note_hash(self: &mut Self, note_hash: Field) {} - - pub fn push_new_nullifier(self: &mut Self, nullifier: Field, _nullified_commitment: Field) { - // Cannot nullify pending commitments in AVM, so `nullified_commitment` is not used - self.emit_nullifier(nullifier); - } - - pub fn call_public_function( - self: Self, - contract_address: AztecAddress, - temporary_function_selector: FunctionSelector, - args: [Field; ARGS_COUNT] - ) -> [Field; RET_SIZE] { - let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420]; - - let results = self.call( - gas, - contract_address, - args, - temporary_function_selector.to_field() - ); - let returnData: [Field; RET_SIZE] = results.0; - let success: u8 = results.1; - assert(success == 1, "Nested call failed!"); - - returnData - } - - pub fn static_call_public_function( - self: Self, - contract_address: AztecAddress, - temporary_function_selector: FunctionSelector, - args: [Field; ARGS_COUNT] - ) -> [Field; RET_SIZE] { - let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420]; - - let (returnData, success): ([Field; RET_SIZE], u8) = self.call_static( - gas, - contract_address, - args, - temporary_function_selector.to_field() - ); - - assert(success == 1, "Nested static call failed!"); - returnData - } -} diff --git a/aztec/src/context/avm_context.nr b/aztec/src/context/avm_context.nr new file mode 100644 index 00000000..803d1e92 --- /dev/null +++ b/aztec/src/context/avm_context.nr @@ -0,0 +1,282 @@ +use dep::protocol_types::{address::{AztecAddress, EthAddress}, constants::L1_TO_L2_MESSAGE_LENGTH, header::Header}; +use dep::protocol_types::traits::Serialize; +use dep::protocol_types::abis::function_selector::FunctionSelector; +use dep::protocol_types::abis::public_circuit_public_inputs::PublicCircuitPublicInputs; +use dep::protocol_types::constants::RETURN_VALUES_LENGTH; +use crate::context::inputs::PublicContextInputs; +use crate::context::interface::ContextInterface; +use crate::context::interface::PublicContextInterface; + +struct AVMContext {} + +impl AVMContext { + pub fn new() -> Self { + AVMContext {} + } + + pub fn origin(self) -> AztecAddress { + origin() + } + pub fn storage_address(self) -> AztecAddress { + storage_address() + } + pub fn fee_per_l1_gas(self) -> Field { + fee_per_l1_gas() + } + pub fn fee_per_l2_gas(self) -> Field { + fee_per_l2_gas() + } + pub fn fee_per_da_gas(self) -> Field { + fee_per_da_gas() + } + /** + * Emit a log with the given event selector and message. + * + * @param event_selector The event selector for the log. + * @param message The message to emit in the log. + * Should be automatically convertible to [Field; N]. For example str works with + * one char per field. Otherwise you can use CompressedString. + */ + pub fn accumulate_unencrypted_logs(&mut self, event_selector: Field, log: T) { + emit_unencrypted_log(event_selector, log); + } + pub fn note_hash_exists(self, note_hash: Field, leaf_index: Field) -> bool { + note_hash_exists(note_hash, leaf_index) == 1 + } + pub fn l1_to_l2_msg_exists(self, msg_hash: Field, msg_leaf_index: Field) -> bool { + l1_to_l2_msg_exists(msg_hash, msg_leaf_index) == 1 + } + pub fn nullifier_exists(self, nullifier: Field) -> bool { + nullifier_exists(nullifier) == 1 + } + + fn call_public_function_raw( + self: &mut Self, + gas: [Field; 3], + contract_address: AztecAddress, + temporary_function_selector: Field, + args: [Field; ARGS_COUNT] + ) -> ([Field; RET_COUNT], u8) { + call(gas, contract_address, args, temporary_function_selector) + } + + fn static_call_public_function_raw( + self: &mut Self, + gas: [Field; 3], + contract_address: AztecAddress, + temporary_function_selector: Field, + args: [Field; ARGS_COUNT] + ) -> ([Field; RET_COUNT], u8) { + call_static(gas, contract_address, args, temporary_function_selector) + } +} + +impl PublicContextInterface for AVMContext { + fn block_number(self) -> Field { + block_number() + } + + fn timestamp(self) -> Field { + timestamp() + } + + fn coinbase(self) -> EthAddress { + assert(false, "'coinbase' not implemented!"); + EthAddress::zero() + } + + fn fee_recipient(self) -> AztecAddress { + assert(false, "'fee_recipient' not implemented!"); + AztecAddress::zero() + } + + fn push_nullifier_read_request(&mut self, nullifier: Field) { + assert(false, "'push_nullifier_read_request' not implemented!"); + } + + fn push_nullifier_non_existent_read_request(&mut self, nullifier: Field) { + assert(false, "'push_nullifier_non_existent_read_request' not implemented!"); + } + + fn accumulate_encrypted_logs(&mut self, log: [Field; N]) { + assert(false, "'accumulate_encrypted_logs' not implemented!"); + } + + fn accumulate_unencrypted_logs(&mut self, log: T) { + let event_selector = 0; + self.accumulate_unencrypted_logs(event_selector, log); + } + + fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress) { + assert(false, "'consume_l1_to_l2_message' not implemented!"); + } + + fn message_portal(&mut self, recipient: EthAddress, content: Field) { + send_l2_to_l1_msg(recipient, content); + } + + fn call_public_function( + self: &mut Self, + contract_address: AztecAddress, + temporary_function_selector: FunctionSelector, + args: [Field; ARGS_COUNT] + ) -> [Field; RETURN_VALUES_LENGTH] { + let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420]; + + let results = call( + gas, + contract_address, + args, + temporary_function_selector.to_field() + ); + let returnData: [Field; RETURN_VALUES_LENGTH] = results.0; + let success: u8 = results.1; + assert(success == 1, "Nested call failed!"); + + returnData + } + + fn static_call_public_function( + self: &mut Self, + contract_address: AztecAddress, + temporary_function_selector: FunctionSelector, + args: [Field; ARGS_COUNT] + ) -> [Field; RETURN_VALUES_LENGTH] { + let gas = [/*l1Gas*/42, /*l2Gas*/24, /*daGas*/420]; + + let (returnData, success): ([Field; RETURN_VALUES_LENGTH], u8) = call_static( + gas, + contract_address, + args, + temporary_function_selector.to_field() + ); + + assert(success == 1, "Nested static call failed!"); + returnData + } + + fn delegate_call_public_function( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector, + args: [Field; ARGS_COUNT] + ) -> [Field; RETURN_VALUES_LENGTH] { + assert(false, "'delegate_call_public_function' not implemented!"); + [0; RETURN_VALUES_LENGTH] + } +} + +impl ContextInterface for AVMContext { + fn push_new_note_hash(&mut self, note_hash: Field) { + emit_note_hash(note_hash); + } + fn push_new_nullifier(&mut self, nullifier: Field, _nullified_commitment: Field) { + // Cannot nullify pending commitments in AVM, so `nullified_commitment` is not used + emit_nullifier(nullifier); + } + fn msg_sender(self) -> AztecAddress { + sender() + } + fn this_address(self) -> AztecAddress { + address() + } + fn this_portal_address(self) -> EthAddress { + portal() + } + fn chain_id(self) -> Field { + chain_id() + } + fn version(self) -> Field { + version() + } + fn selector(self) -> FunctionSelector { + assert(false, "'selector' not implemented!"); + FunctionSelector::zero() + } + fn get_header(self) -> Header { + assert(false, "'get_header' not implemented!"); + Header::empty() + } + fn get_args_hash(self) -> Field { + assert(false, "'get_args_hash' not implemented!"); + 0 + } +} + +// AVM oracles (opcodes) follow, do not use directly. +#[oracle(avmOpcodeAddress)] +fn address() -> AztecAddress {} + +#[oracle(avmOpcodeStorageAddress)] +fn storage_address() -> AztecAddress {} + +#[oracle(avmOpcodeOrigin)] +fn origin() -> AztecAddress {} + +#[oracle(avmOpcodeSender)] +fn sender() -> AztecAddress {} + +#[oracle(avmOpcodePortal)] +fn portal() -> EthAddress {} + +#[oracle(avmOpcodeFeePerL1Gas)] +fn fee_per_l1_gas() -> Field {} + +#[oracle(avmOpcodeFeePerL2Gas)] +fn fee_per_l2_gas() -> Field {} + +#[oracle(avmOpcodeFeePerDaGas)] +fn fee_per_da_gas() -> Field {} + +#[oracle(avmOpcodeChainId)] +fn chain_id() -> Field {} + +#[oracle(avmOpcodeVersion)] +fn version() -> Field {} + +#[oracle(avmOpcodeBlockNumber)] +fn block_number() -> Field {} + +#[oracle(avmOpcodeTimestamp)] +fn timestamp() -> Field {} + +#[oracle(avmOpcodeNoteHashExists)] +fn note_hash_exists(note_hash: Field, leaf_index: Field) -> u8 {} + +#[oracle(avmOpcodeEmitNoteHash)] +fn emit_note_hash(note_hash: Field) {} + +#[oracle(avmOpcodeNullifierExists)] +fn nullifier_exists(nullifier: Field) -> u8 {} + +#[oracle(avmOpcodeEmitNullifier)] +fn emit_nullifier(nullifier: Field) {} + +#[oracle(amvOpcodeEmitUnencryptedLog)] +fn emit_unencrypted_log(event_selector: Field, message: T) {} + +#[oracle(avmOpcodeL1ToL2MsgExists)] +fn l1_to_l2_msg_exists(msg_hash: Field, msg_leaf_index: Field) -> u8 {} + +#[oracle(avmOpcodeSendL2ToL1Msg)] +fn send_l2_to_l1_msg(recipient: EthAddress, content: Field) {} + +#[oracle(avmOpcodeCall)] +fn call( + gas: [Field; 3], // gas allocation: [l1Gas, l2Gas, daGas] + address: AztecAddress, + args: [Field; ARGS_COUNT], + // TODO(5110): consider passing in calldata directly + temporary_function_selector: Field +) -> ([Field; RET_SIZE], u8) {} +// ^ return data ^ success + +#[oracle(avmOpcodeStaticCall)] +fn call_static( + gas: [Field; 3], // gas allocation: [l1Gas, l2Gas, daGas] + address: AztecAddress, + args: [Field; ARGS_COUNT], + // TODO(5110): consider passing in calldata directly + temporary_function_selector: Field +) -> ([Field; RET_SIZE], u8) {} +// ^ return data ^ success \ No newline at end of file diff --git a/aztec/src/context/interface.nr b/aztec/src/context/interface.nr index 19a34c90..cc18ffba 100644 --- a/aztec/src/context/interface.nr +++ b/aztec/src/context/interface.nr @@ -1,4 +1,7 @@ -use dep::protocol_types::{abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress}, header::Header}; +use dep::protocol_types::{ + abis::function_selector::FunctionSelector, address::{AztecAddress, EthAddress}, header::Header, + constants::RETURN_VALUES_LENGTH +}; trait ContextInterface { fn push_new_note_hash(&mut self, note_hash: Field); @@ -12,3 +15,37 @@ trait ContextInterface { fn get_args_hash(self) -> Field; fn get_header(self) -> Header; } + +// TEMPORARY: This trait is to promote sharing of the current public context +// and the upcoming AVMContext. This will be removed once the AVMContext is the default. +// If you are adding something here, then you should also implement it in the AVMContext. +trait PublicContextInterface { + fn block_number(self) -> Field; + fn timestamp(self) -> Field; + fn coinbase(self) -> EthAddress; + fn fee_recipient(self) -> AztecAddress; + fn push_nullifier_read_request(&mut self, nullifier: Field); + fn push_nullifier_non_existent_read_request(&mut self, nullifier: Field); + fn message_portal(&mut self, recipient: EthAddress, content: Field); + fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress); + fn accumulate_encrypted_logs(&mut self, log: [Field; N]); + fn accumulate_unencrypted_logs(&mut self, log: T); + fn call_public_function( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector, + args: [Field; ARGS_COUNT] + ) -> [Field; RETURN_VALUES_LENGTH]; + fn static_call_public_function( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector, + args: [Field; ARGS_COUNT] + ) -> [Field; RETURN_VALUES_LENGTH]; + fn delegate_call_public_function( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector, + args: [Field; ARGS_COUNT] + ) -> [Field; RETURN_VALUES_LENGTH]; +} diff --git a/aztec/src/context/private_context.nr b/aztec/src/context/private_context.nr index 99f5580d..23b3c1c3 100644 --- a/aztec/src/context/private_context.nr +++ b/aztec/src/context/private_context.nr @@ -23,8 +23,9 @@ use dep::protocol_types::{ MAX_NEW_NOTE_HASHES_PER_CALL, MAX_NEW_L2_TO_L1_MSGS_PER_CALL, MAX_NEW_NULLIFIERS_PER_CALL, MAX_PRIVATE_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL, MAX_PUBLIC_DATA_READS_PER_CALL, MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL, - MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL, - MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, NUM_FIELDS_PER_SHA256, RETURN_VALUES_LENGTH + MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_READ_REQUESTS_PER_CALL, + MAX_NULLIFIER_NON_EXISTENT_READ_REQUESTS_PER_CALL, MAX_NULLIFIER_KEY_VALIDATION_REQUESTS_PER_CALL, + NUM_FIELDS_PER_SHA256, RETURN_VALUES_LENGTH }, contrakt::{storage_read::StorageRead, storage_update_request::StorageUpdateRequest}, grumpkin_private_key::GrumpkinPrivateKey, hash::hash_args, header::Header, diff --git a/aztec/src/context/public_context.nr b/aztec/src/context/public_context.nr index cc5dc60d..c33aa633 100644 --- a/aztec/src/context/public_context.nr +++ b/aztec/src/context/public_context.nr @@ -1,5 +1,5 @@ use crate::{ - context::{inputs::PublicContextInputs, interface::ContextInterface}, + context::{inputs::PublicContextInputs, interface::ContextInterface, interface::PublicContextInterface}, messaging::process_l1_to_l2_message, oracle::{arguments, public_call::call_public_function_internal} }; use dep::protocol_types::{ @@ -49,6 +49,106 @@ struct PublicContext { prover_address: AztecAddress, } +impl PublicContext { + pub fn new(inputs: PublicContextInputs, args_hash: Field) -> PublicContext { + PublicContext { + inputs, + side_effect_counter: inputs.start_side_effect_counter, + args_hash, + return_values: BoundedVec::new(), + nullifier_read_requests: BoundedVec::new(), + nullifier_non_existent_read_requests: BoundedVec::new(), + contract_storage_update_requests: BoundedVec::new(), + contract_storage_reads: BoundedVec::new(), + public_call_stack_hashes: BoundedVec::new(), + new_note_hashes: BoundedVec::new(), + new_nullifiers: BoundedVec::new(), + new_l2_to_l1_msgs: BoundedVec::new(), + unencrypted_logs_hash: BoundedVec::new(), + unencrypted_logs_preimages_length: 0, + historical_header: inputs.historical_header, + prover_address: AztecAddress::zero() // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) + // encrypted_logs_preimages: Vec::new(), + // unencrypted_logs_preimages: Vec::new(), + } + } + + pub fn call_public_function_no_args( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector + ) -> [Field; RETURN_VALUES_LENGTH] { + self.call_public_function_with_packed_args(contract_address, function_selector, 0, false, false) + } + + pub fn static_call_public_function_no_args( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector + ) -> [Field; RETURN_VALUES_LENGTH] { + self.call_public_function_with_packed_args(contract_address, function_selector, 0, true, false) + } + + pub fn delegate_call_public_function_no_args( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector + ) -> [Field; RETURN_VALUES_LENGTH] { + self.call_public_function_with_packed_args(contract_address, function_selector, 0, false, true) + } + + pub fn call_public_function_with_packed_args( + self: &mut Self, + contract_address: AztecAddress, + function_selector: FunctionSelector, + args_hash: Field, + is_static_call: bool, + is_delegate_call: bool + ) -> [Field; RETURN_VALUES_LENGTH] { + let side_effect_counter = self.side_effect_counter; + // TODO get next value from output of `call_public_function_internal` + self.side_effect_counter += 1; + + call_public_function_internal( + contract_address, + function_selector, + args_hash, + side_effect_counter, + is_static_call, + is_delegate_call + ) + } + + pub fn finish(self) -> PublicCircuitPublicInputs { + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) + let unencrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256]; + let unencrypted_log_preimages_length = 0; + + // Compute the public call stack hashes + let pub_circuit_pub_inputs = PublicCircuitPublicInputs { + call_context: self.inputs.call_context, // Done + args_hash: self.args_hash, // Done + nullifier_read_requests: self.nullifier_read_requests.storage, + nullifier_non_existent_read_requests: self.nullifier_non_existent_read_requests.storage, + contract_storage_update_requests: self.contract_storage_update_requests.storage, + contract_storage_reads: self.contract_storage_reads.storage, + return_values: self.return_values.storage, + new_note_hashes: self.new_note_hashes.storage, + new_nullifiers: self.new_nullifiers.storage, + public_call_stack_hashes: self.public_call_stack_hashes.storage, + new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage, + start_side_effect_counter: self.inputs.start_side_effect_counter, + end_side_effect_counter: self.side_effect_counter, + unencrypted_logs_hash, + unencrypted_log_preimages_length, + historical_header: self.inputs.historical_header, + prover_address: self.prover_address, + reverted: false + }; + pub_circuit_pub_inputs + } +} + impl ContextInterface for PublicContext { fn msg_sender(self) -> AztecAddress { self.inputs.call_context.msg_sender @@ -97,96 +197,43 @@ impl ContextInterface for PublicContext { self.new_nullifiers.push(side_effect); self.side_effect_counter = self.side_effect_counter + 1; } - } -impl PublicContext { - pub fn new(inputs: PublicContextInputs, args_hash: Field) -> PublicContext { - PublicContext { - inputs, - side_effect_counter: inputs.start_side_effect_counter, - args_hash, - return_values: BoundedVec::new(), - nullifier_read_requests: BoundedVec::new(), - nullifier_non_existent_read_requests: BoundedVec::new(), - contract_storage_update_requests: BoundedVec::new(), - contract_storage_reads: BoundedVec::new(), - public_call_stack_hashes: BoundedVec::new(), - new_note_hashes: BoundedVec::new(), - new_nullifiers: BoundedVec::new(), - new_l2_to_l1_msgs: BoundedVec::new(), - unencrypted_logs_hash: BoundedVec::new(), - unencrypted_logs_preimages_length: 0, - historical_header: inputs.historical_header, - prover_address: AztecAddress::zero() // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) - // encrypted_logs_preimages: Vec::new(), - // unencrypted_logs_preimages: Vec::new(), - } - } - - pub fn block_number(self) -> Field { +impl PublicContextInterface for PublicContext { + fn block_number(self) -> Field { self.inputs.public_global_variables.block_number } - pub fn timestamp(self) -> Field { + fn timestamp(self) -> Field { self.inputs.public_global_variables.timestamp } - pub fn coinbase(self) -> EthAddress { + fn coinbase(self) -> EthAddress { self.inputs.public_global_variables.coinbase } - pub fn fee_recipient(self) -> AztecAddress { + fn fee_recipient(self) -> AztecAddress { self.inputs.public_global_variables.fee_recipient } - pub fn finish(self) -> PublicCircuitPublicInputs { - // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) - let unencrypted_logs_hash = [0; NUM_FIELDS_PER_SHA256]; - let unencrypted_log_preimages_length = 0; - - // Compute the public call stack hashes - let pub_circuit_pub_inputs = PublicCircuitPublicInputs { - call_context: self.inputs.call_context, // Done - args_hash: self.args_hash, // Done - nullifier_read_requests: self.nullifier_read_requests.storage, - nullifier_non_existent_read_requests: self.nullifier_non_existent_read_requests.storage, - contract_storage_update_requests: self.contract_storage_update_requests.storage, - contract_storage_reads: self.contract_storage_reads.storage, - return_values: self.return_values.storage, - new_note_hashes: self.new_note_hashes.storage, - new_nullifiers: self.new_nullifiers.storage, - public_call_stack_hashes: self.public_call_stack_hashes.storage, - new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage, - start_side_effect_counter: self.inputs.start_side_effect_counter, - end_side_effect_counter: self.side_effect_counter, - unencrypted_logs_hash, - unencrypted_log_preimages_length, - historical_header: self.inputs.historical_header, - prover_address: self.prover_address, - reverted: false - }; - pub_circuit_pub_inputs - } - - pub fn push_nullifier_read_request(&mut self, nullifier: Field) { + fn push_nullifier_read_request(&mut self, nullifier: Field) { let request = ReadRequest { value: nullifier, counter: self.side_effect_counter }; self.nullifier_read_requests.push(request); self.side_effect_counter = self.side_effect_counter + 1; } - pub fn push_nullifier_non_existent_read_request(&mut self, nullifier: Field) { + fn push_nullifier_non_existent_read_request(&mut self, nullifier: Field) { let request = ReadRequest { value: nullifier, counter: self.side_effect_counter }; self.nullifier_non_existent_read_requests.push(request); self.side_effect_counter = self.side_effect_counter + 1; } - pub fn message_portal(&mut self, recipient: EthAddress, content: Field) { + fn message_portal(&mut self, recipient: EthAddress, content: Field) { let message = L2ToL1Message { recipient, content }; self.new_l2_to_l1_msgs.push(message); } - pub fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress) { + fn consume_l1_to_l2_message(&mut self, content: Field, secret: Field, sender: EthAddress) { let this = (*self).this_address(); let nullifier = process_l1_to_l2_message( self.historical_header.state.l1_to_l2_message_tree.root, @@ -202,19 +249,19 @@ impl PublicContext { self.push_new_nullifier(nullifier, 0) } - pub fn accumulate_encrypted_logs(&mut self, log: [Field; N]) { + fn accumulate_encrypted_logs(&mut self, log: [Field; N]) { let _void1 = self; let _void2 = log; // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) } - pub fn accumulate_unencrypted_logs(&mut self, log: T) { + fn accumulate_unencrypted_logs(&mut self, log: T) { let _void1 = self; let _void2 = log; // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) } - pub fn call_public_function( + fn call_public_function( self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector, @@ -225,7 +272,7 @@ impl PublicContext { self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, false, false) } - pub fn static_call_public_function( + fn static_call_public_function( self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector, @@ -236,7 +283,7 @@ impl PublicContext { self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, true, false) } - pub fn delegate_call_public_function( + fn delegate_call_public_function( self: &mut Self, contract_address: AztecAddress, function_selector: FunctionSelector, @@ -246,50 +293,4 @@ impl PublicContext { assert(args_hash == arguments::pack_arguments(args)); self.call_public_function_with_packed_args(contract_address, function_selector, args_hash, false, true) } - - pub fn call_public_function_no_args( - self: &mut Self, - contract_address: AztecAddress, - function_selector: FunctionSelector - ) -> [Field; RETURN_VALUES_LENGTH] { - self.call_public_function_with_packed_args(contract_address, function_selector, 0, false, false) - } - - pub fn static_call_public_function_no_args( - self: &mut Self, - contract_address: AztecAddress, - function_selector: FunctionSelector - ) -> [Field; RETURN_VALUES_LENGTH] { - self.call_public_function_with_packed_args(contract_address, function_selector, 0, true, false) - } - - pub fn delegate_call_public_function_no_args( - self: &mut Self, - contract_address: AztecAddress, - function_selector: FunctionSelector - ) -> [Field; RETURN_VALUES_LENGTH] { - self.call_public_function_with_packed_args(contract_address, function_selector, 0, false, true) - } - - pub fn call_public_function_with_packed_args( - self: &mut Self, - contract_address: AztecAddress, - function_selector: FunctionSelector, - args_hash: Field, - is_static_call: bool, - is_delegate_call: bool - ) -> [Field; RETURN_VALUES_LENGTH] { - let side_effect_counter = self.side_effect_counter; - // TODO get next value from output of `call_public_function_internal` - self.side_effect_counter += 1; - - call_public_function_internal( - contract_address, - function_selector, - args_hash, - side_effect_counter, - is_static_call, - is_delegate_call - ) - } }