From 59c0475d3239828452eb8b770787e76127e7a0d1 Mon Sep 17 00:00:00 2001 From: superstar0402 Date: Tue, 15 Aug 2023 17:29:16 +0100 Subject: [PATCH] feat(aztec-noir): align public and private execution patterns (#1515) ## Metadata **fixes** - https://github.com/AztecProtocol/aztec-packages/issues/1484 **Relevant discussions** - https://github.com/AztecProtocol/aztec-packages/issues/1358#issuecomment-1662165062 ## Overview This PR aims to align the implementations of Public and Private state. - Currently public circuits do not have a context object, this will cause developers to have to learn more than one code path. **Other work I am thinking of completing within this PR** Right now, `PublicCircuitPublicInputs` (The object which is fed to the public kernel after circuit completion) is not created within the application circuit. It is created within the sequencer. If we zoom out, this is not a direct security concern as execution is occuring within the sequencer, however it does break away from the pattern seen within `PrivateExecution` where the result of each execution is the `PrivateCircuitPublicInputs` object. We could leave this as is, (alot of this code will be dumped when the vm arrives). However similar code paths will make building the library easier. For example, for some library methods we have two implementations, one for public and one for private. More and more standardisation would allow these methods to take in a generic context and utilise the same code paths. (A goal I think is worth pursuing). # Checklist: Remove the checklist to signal you've completed it. Enable auto-merge if the PR is ready to merge. - [ ] If the pull request requires a cryptography review (e.g. cryptographic algorithm implementations) I have added the 'crypto' tag. - [ ] I have reviewed my diff in github, line by line and removed unexpected formatting changes, testing logs, or commented-out code. - [ ] Every change is related to the PR description. - [ ] I have [linked](https://docs.github.com/en/issues/tracking-your-work-with-issues/linking-a-pull-request-to-an-issue) this pull request to relevant issues (if any exist). --- .../src/easy_private_state.nr | 10 +- yarn-project/noir-libs/noir-aztec/src/abi.nr | 38 ++-- .../noir-libs/noir-aztec/src/constants_gen.nr | 4 +- .../noir-libs/noir-aztec/src/context.nr | 203 ++++++++++++++++-- .../noir-libs/noir-aztec/src/entrypoint.nr | 4 +- yarn-project/noir-libs/noir-aztec/src/log.nr | 6 +- .../noir-aztec/src/note/lifecycle.nr | 15 +- .../noir-aztec/src/note/note_getter.nr | 16 +- .../noir-aztec/src/public_call_stack_item.nr | 27 --- .../src/state_vars/immutable_singleton.nr | 8 +- .../noir-aztec/src/state_vars/set.nr | 19 +- .../noir-aztec/src/state_vars/singleton.nr | 8 +- .../noir-libs/value-note/src/utils.nr | 10 +- 13 files changed, 261 insertions(+), 107 deletions(-) diff --git a/yarn-project/noir-libs/easy-private-state/src/easy_private_state.nr b/yarn-project/noir-libs/easy-private-state/src/easy_private_state.nr index 5bbb77e..5bbbf08 100644 --- a/yarn-project/noir-libs/easy-private-state/src/easy_private_state.nr +++ b/yarn-project/noir-libs/easy-private-state/src/easy_private_state.nr @@ -8,7 +8,7 @@ use dep::value_note::{ }; use dep::aztec::{ - context::Context, + context::PrivateContext, log::emit_encrypted_log, note::note_getter_options::NoteGetterOptions, oracle::get_public_key::get_public_key, @@ -39,7 +39,7 @@ impl EasyPrivateUint { // Very similar to `send_note`. fn add( self, - context: &mut Context, + context: &mut PrivateContext, addend: u120, owner: Field, ) { @@ -53,7 +53,7 @@ impl EasyPrivateUint { let owner_key = get_public_key(owner); emit_encrypted_log( context, - context.inputs.call_context.storage_contract_address, + (*context).this_address(), self.set.storage_slot, owner_key, addend_note.serialise(), @@ -63,7 +63,7 @@ impl EasyPrivateUint { // Very similar to `spend_note`. fn sub( self, - context: &mut Context, + context: &mut PrivateContext, subtrahend: u120, owner: Field, ) { @@ -106,7 +106,7 @@ impl EasyPrivateUint { emit_encrypted_log( context, - context.inputs.call_context.storage_contract_address, + (*context).this_address(), self.set.storage_slot, owner_key, encrypted_data, diff --git a/yarn-project/noir-libs/noir-aztec/src/abi.nr b/yarn-project/noir-libs/noir-aztec/src/abi.nr index a8a8613..4e0b93b 100644 --- a/yarn-project/noir-libs/noir-aztec/src/abi.nr +++ b/yarn-project/noir-libs/noir-aztec/src/abi.nr @@ -10,7 +10,7 @@ use crate::constants_gen::{ MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL, MAX_PUBLIC_DATA_READS_PER_CALL, GENERATOR_INDEX__FUNCTION_ARGS, - CONSTANT_HISTORIC_BLOCK_DATA_LENGTH, + HISTORIC_BLOCK_DATA_LENGTH, CONTRACT_DEPLOYMENT_DATA_LENGTH, CALL_CONTEXT_LENGTH, PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH, @@ -132,27 +132,27 @@ struct HistoricBlockData { contract_tree_root : Field, l1_to_l2_messages_tree_root : Field, blocks_tree_root: Field, - prev_global_variables_hash: Field, public_data_tree_root: Field, + global_variables_hash: Field, } impl HistoricBlockData { // NOTE: this order must match the order in `private_circuit_public_inputs.hpp` - fn serialize(self) -> [Field; CONSTANT_HISTORIC_BLOCK_DATA_LENGTH] { + fn serialize(self) -> [Field; HISTORIC_BLOCK_DATA_LENGTH] { [ self.private_data_tree_root, self.nullifier_tree_root, self.contract_tree_root, self.l1_to_l2_messages_tree_root, self.blocks_tree_root, - self.prev_global_variables_hash, - self.public_data_tree_root + self.public_data_tree_root, + self.global_variables_hash, ] } -} -fn empty_block_data() -> HistoricBlockData { - HistoricBlockData{ private_data_tree_root: 0, nullifier_tree_root: 0, contract_tree_root: 0, l1_to_l2_messages_tree_root: 0, blocks_tree_root: 0, prev_global_variables_hash: 0, public_data_tree_root: 0 } + fn empty() -> Self { + Self { private_data_tree_root: 0, nullifier_tree_root: 0, contract_tree_root: 0, l1_to_l2_messages_tree_root: 0, blocks_tree_root: 0, public_data_tree_root: 0, global_variables_hash: 0 } + } } struct FunctionData { @@ -249,10 +249,6 @@ struct ContractStorageRead { value: Field, } -fn empty_contract_storage_read() -> ContractStorageRead { - ContractStorageRead { storage_slot: 0, value: 0 } -} - impl ContractStorageRead { fn serialize(self) -> [Field; CONTRACT_STORAGE_READ_LENGTH] { [self.storage_slot, self.value] @@ -261,6 +257,10 @@ impl ContractStorageRead { fn hash(self) -> Field { dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_READ)[0] } + + fn empty() -> Self { + Self { storage_slot: 0, value: 0 } + } } struct ContractStorageUpdateRequest { @@ -277,10 +277,10 @@ impl ContractStorageUpdateRequest { fn hash(self) -> Field { dep::std::hash::pedersen_with_separator(self.serialize(), GENERATOR_INDEX__PUBLIC_DATA_UPDATE_REQUEST)[0] } -} -fn empty_contract_storage_update_request() -> ContractStorageUpdateRequest { - ContractStorageUpdateRequest { storage_slot: 0, old_value: 0, new_value: 0 } + fn empty() -> Self { + Self { storage_slot: 0, old_value: 0, new_value: 0 } + } } @@ -297,8 +297,10 @@ struct PublicCircuitPublicInputs { unencrypted_logs_hash: [Field; NUM_FIELDS_PER_SHA256], unencrypted_log_preimages_length: Field, block_data: HistoricBlockData, - historic_public_data_tree_root: Field, prover_address: Field, + + // TODO: include globals in here and check them elsewhere + // https://github.com/AztecProtocol/aztec-packages/issues/1567 } impl PublicCircuitPublicInputs { @@ -320,11 +322,10 @@ impl PublicCircuitPublicInputs { inputs.push_array(self.new_l2_to_l1_msgs); // We do not include block_data since it's not in the cpp hash - // inputs.push(self.block_data.hash()); see https://github.com/AztecProtocol/aztec-packages/issues/1473 inputs.push_array(self.unencrypted_logs_hash); inputs.push(self.unencrypted_log_preimages_length); - inputs.push(self.historic_public_data_tree_root); + inputs.push_array(self.block_data.serialize()); // see https://github.com/AztecProtocol/aztec-packages/issues/1473 inputs.push(self.prover_address); dep::std::hash::pedersen_with_separator(inputs.storage, GENERATOR_INDEX__PUBLIC_CIRCUIT_PUBLIC_INPUTS)[0] @@ -348,7 +349,6 @@ impl PublicCircuitPublicInputs { fields.push_array(self.unencrypted_logs_hash); fields.push(self.unencrypted_log_preimages_length); fields.push_array(self.block_data.serialize()); - fields.push(self.historic_public_data_tree_root); fields.push(self.prover_address); fields.storage } diff --git a/yarn-project/noir-libs/noir-aztec/src/constants_gen.nr b/yarn-project/noir-libs/noir-aztec/src/constants_gen.nr index 5eee612..aac66a0 100644 --- a/yarn-project/noir-libs/noir-aztec/src/constants_gen.nr +++ b/yarn-project/noir-libs/noir-aztec/src/constants_gen.nr @@ -50,7 +50,7 @@ global GET_NOTE_ORACLE_RETURN_LENGTH: comptime Field = 23; global MAX_NOTES_PER_PAGE: comptime Field = 10; global VIEW_NOTE_ORACLE_RETURN_LENGTH: comptime Field = 212; global CALL_CONTEXT_LENGTH: comptime Field = 6; -global CONSTANT_HISTORIC_BLOCK_DATA_LENGTH: comptime Field = 7; +global HISTORIC_BLOCK_DATA_LENGTH: comptime Field = 7; global FUNCTION_DATA_LENGTH: comptime Field = 4; global CONTRACT_DEPLOYMENT_DATA_LENGTH: comptime Field = 6; global PRIVATE_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 58; @@ -60,7 +60,7 @@ global PUBLIC_CIRCUIT_PUBLIC_INPUTS_LENGTH: comptime Field = 77; global GET_NOTES_ORACLE_RETURN_LENGTH: comptime Field = 86; global EMPTY_NULLIFIED_COMMITMENT: comptime Field = 1000000; global CALL_PRIVATE_FUNCTION_RETURN_SIZE: comptime Field = 64; -global PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: comptime Field = 41; +global PUBLIC_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: comptime Field = 47; global PRIVATE_CIRCUIT_PUBLIC_INPUTS_HASH_INPUT_LENGTH: comptime Field = 48; global COMMITMENTS_NUM_BYTES_PER_BASE_ROLLUP: comptime Field = 1024; global NULLIFIERS_NUM_BYTES_PER_BASE_ROLLUP: comptime Field = 1024; diff --git a/yarn-project/noir-libs/noir-aztec/src/context.nr b/yarn-project/noir-libs/noir-aztec/src/context.nr index 7923a70..b7da9ee 100644 --- a/yarn-project/noir-libs/noir-aztec/src/context.nr +++ b/yarn-project/noir-libs/noir-aztec/src/context.nr @@ -15,9 +15,6 @@ use crate::constants_gen::{ use crate::abi; use crate::abi::{ - empty_block_data, - empty_contract_storage_read, - empty_contract_storage_update_request, hash_args, CallContext, ContractDeploymentData, @@ -52,7 +49,7 @@ use crate::oracle::{ // When finished, one can call .finish() to convert back to the abi -struct Context { +struct PrivateContext { inputs: abi::PrivateContextInputs, args_hash : Field, @@ -68,14 +65,16 @@ struct Context { public_call_stack : BoundedVec, new_l2_to_l1_msgs : BoundedVec, + block_data: HistoricBlockData, + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) // encrypted_logs_preimages: Vec, // unencrypted_logs_preimages: Vec, } -impl Context { - fn new(inputs: abi::PrivateContextInputs, args_hash: Field) -> Context { - Context { +impl PrivateContext { + fn new(inputs: abi::PrivateContextInputs, args_hash: Field) -> PrivateContext { + PrivateContext { inputs: inputs, args_hash: args_hash, @@ -87,6 +86,8 @@ impl Context { new_nullifiers: BoundedVec::new(0), nullified_commitments: BoundedVec::new(0), + block_data: inputs.block_data, + private_call_stack: BoundedVec::new(0), public_call_stack: BoundedVec::new(0), new_l2_to_l1_msgs: BoundedVec::new(0), @@ -139,7 +140,7 @@ impl Context { unencrypted_logs_hash: unencrypted_logs_hash, encrypted_log_preimages_length: encrypted_log_preimages_length, unencrypted_log_preimages_length: unencrypted_log_preimages_length, - block_data: self.inputs.block_data, + block_data: self.block_data, contract_deployment_data: self.inputs.contract_deployment_data, chain_id: self.inputs.private_global_variables.chain_id, version: self.inputs.private_global_variables.version, @@ -254,8 +255,8 @@ impl Context { contract_tree_root : fields[50], l1_to_l2_messages_tree_root : fields[51], blocks_tree_root : fields[52], - prev_global_variables_hash: fields[53], - public_data_tree_root: fields[54], + public_data_tree_root: fields[53], + global_variables_hash: fields[54], }, contract_deployment_data: ContractDeploymentData { deployer_public_key: Point::new(fields[55], fields[56]), @@ -341,16 +342,15 @@ impl Context { }, args_hash: fields[11], return_values: [0; RETURN_VALUES_LENGTH], - contract_storage_update_requests: [empty_contract_storage_update_request(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL], - contract_storage_read: [empty_contract_storage_read(); MAX_PUBLIC_DATA_READS_PER_CALL], + contract_storage_update_requests: [ContractStorageUpdateRequest::empty(); MAX_PUBLIC_DATA_UPDATE_REQUESTS_PER_CALL], + contract_storage_read: [ContractStorageRead::empty(); MAX_PUBLIC_DATA_READS_PER_CALL], public_call_stack: [0; MAX_PUBLIC_CALL_STACK_LENGTH_PER_CALL], new_commitments: [0; MAX_NEW_COMMITMENTS_PER_CALL], new_nullifiers: [0; MAX_NEW_NULLIFIERS_PER_CALL], new_l2_to_l1_msgs:[0; MAX_NEW_L2_TO_L1_MSGS_PER_CALL], unencrypted_logs_hash:[0; NUM_FIELDS_PER_SHA256], unencrypted_log_preimages_length: 0, - block_data: empty_block_data(), - historic_public_data_tree_root: 0, + block_data: HistoricBlockData::empty(), prover_address: 0, }, is_execution_request: true, @@ -374,3 +374,178 @@ impl Context { self.public_call_stack.push(item.hash()); } } + +use crate::abi::{ + ContractStorageRead, + ContractStorageUpdateRequest +}; + +struct PublicContext { + inputs: abi::PublicContextInputs, + + args_hash : Field, + return_values : BoundedVec, + + contract_storage_update_requests: BoundedVec, + contract_storage_read: BoundedVec, + public_call_stack: BoundedVec, + + new_commitments: BoundedVec, + new_nullifiers: BoundedVec, + + new_l2_to_l1_msgs: BoundedVec, + + unencrypted_logs_hash: BoundedVec, + unencrypted_logs_preimages_length: Field, + + block_data: HistoricBlockData, + prover_address: Field, +} + +impl PublicContext { + fn new(inputs: abi::PublicContextInputs, args_hash: Field) -> PublicContext { + let empty_storage_read = ContractStorageRead::empty(); + let empty_storage_update = ContractStorageUpdateRequest::empty(); + PublicContext { + inputs: inputs, + + args_hash: args_hash, + return_values: BoundedVec::new(0), + + contract_storage_update_requests: BoundedVec::new(empty_storage_update), + contract_storage_read: BoundedVec::new(empty_storage_read), + public_call_stack: BoundedVec::new(0), + + new_commitments: BoundedVec::new(0), + new_nullifiers: BoundedVec::new(0), + + new_l2_to_l1_msgs: BoundedVec::new(0), + + + unencrypted_logs_hash: BoundedVec::new(0), + unencrypted_logs_preimages_length: 0, + + block_data: inputs.block_data, + prover_address: 0, + + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) + // encrypted_logs_preimages: Vec::new(), + // unencrypted_logs_preimages: Vec::new(), + } + } + + fn msg_sender(self) -> Field { + self.inputs.call_context.msg_sender + } + + fn this_address(self) -> Field { + self.inputs.call_context.storage_contract_address + } + + fn this_portal_address(self) -> Field { + self.inputs.call_context.portal_contract_address + } + + fn chain_id(self) -> Field { + self.inputs.public_global_variables.chain_id + } + + fn version(self) -> Field { + self.inputs.public_global_variables.version + } + + fn block_number(self) -> Field { + self.inputs.public_global_variables.block_number + } + + fn timestamp(self) -> Field { + self.inputs.public_global_variables.timestamp + } + + fn finish(self) -> abi::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 = abi::PublicCircuitPublicInputs { + call_context: self.inputs.call_context, // Done + args_hash: self.args_hash, // Done + contract_storage_update_requests: self.contract_storage_update_requests.storage, + contract_storage_read: self.contract_storage_read.storage, + return_values: self.return_values.storage, + new_commitments: self.new_commitments.storage, + new_nullifiers: self.new_nullifiers.storage, + public_call_stack: self.public_call_stack.storage, + new_l2_to_l1_msgs: self.new_l2_to_l1_msgs.storage, + unencrypted_logs_hash: unencrypted_logs_hash, + unencrypted_log_preimages_length: unencrypted_log_preimages_length, + block_data: self.inputs.block_data, + prover_address: self.prover_address, + }; + pub_circuit_pub_inputs + } + + fn push_new_note_hash(&mut self, note_hash: Field) { + self.new_commitments.push(note_hash); + } + + fn push_new_nullifier(&mut self, nullifier: Field, _nullified_commitment: Field) { + self.new_nullifiers.push(nullifier); + } + + fn message_portal(&mut self, msg: Field) { + self.new_l2_to_l1_msgs.push(msg); + } + + // PrivateContextInputs must be temporarily passed in to prevent too many unknowns + // Note this returns self to get around an issue where mutable structs do not maintain mutations unless reassigned + fn consume_l1_to_l2_message(&mut self, msg_key: Field, content: Field, secret: Field) { + let this = (*self).this_address(); + let nullifier = process_l1_to_l2_message(self.block_data.l1_to_l2_messages_tree_root, this, msg_key, content, secret); + + // Push nullifier (and the "commitment" corresponding to this can be "empty") + self.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT) + } + + fn accumulate_encrypted_logs(&mut self, log: [Field; N]) { + let _void1 = self; + let _void2 = log; + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) + } + + fn accumulate_unencrypted_logs(&mut self, log: T) { + let _void1 = self; + let _void2 = log; + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1165) + } + + fn call_public_function( + _self: Self, + contract_address: Field, + function_selector: Field, + args: [Field; ARGS_COUNT], + ) -> [Field; RETURN_VALUES_LENGTH] { + let args_hash = abi::hash_args(args); + assert(args_hash == arguments::pack_arguments(args)); + call_public_function_internal( + contract_address, + function_selector, + args_hash, + ) + } + + fn call_public_function_no_args( + _self: Self, + contract_address: Field, + function_selector: Field, + ) -> [Field; RETURN_VALUES_LENGTH] { + call_public_function_internal( + contract_address, + function_selector, + 0, + ) + } + +} \ No newline at end of file diff --git a/yarn-project/noir-libs/noir-aztec/src/entrypoint.nr b/yarn-project/noir-libs/noir-aztec/src/entrypoint.nr index c812612..bad450d 100644 --- a/yarn-project/noir-libs/noir-aztec/src/entrypoint.nr +++ b/yarn-project/noir-libs/noir-aztec/src/entrypoint.nr @@ -1,6 +1,6 @@ use crate::abi; use crate::types::vec::BoundedVec; -use crate::context::Context; +use crate::context::PrivateContext; use crate::private_call_stack_item::PrivateCallStackItem; use crate::public_call_stack_item::PublicCallStackItem; @@ -88,7 +88,7 @@ impl EntrypointPayload { } // Executes all private and public calls - fn execute_calls(self, context: &mut Context) { + fn execute_calls(self, context: &mut PrivateContext) { for i in 0..ACCOUNT_MAX_PRIVATE_CALLS { let target_address = self.flattened_targets[i]; if target_address != 0 { diff --git a/yarn-project/noir-libs/noir-aztec/src/log.nr b/yarn-project/noir-libs/noir-aztec/src/log.nr index 2077eb2..5ddbe4c 100644 --- a/yarn-project/noir-libs/noir-aztec/src/log.nr +++ b/yarn-project/noir-libs/noir-aztec/src/log.nr @@ -1,9 +1,9 @@ -use crate::context::Context; +use crate::context::PrivateContext; use crate::oracle; use crate::types::point::Point; fn emit_encrypted_log( - context: &mut Context, + context: &mut PrivateContext, contract_address: Field, storage_slot: Field, encryption_pub_key: Point, @@ -14,7 +14,7 @@ fn emit_encrypted_log( } fn emit_unencrypted_log( - context: &mut Context, + context: &mut PrivateContext, log: T, ) { let _ = oracle::logs::emit_unencrypted_log(log); diff --git a/yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr b/yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr index ff5a1b4..ddea040 100644 --- a/yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr +++ b/yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr @@ -1,5 +1,8 @@ use crate::abi::PublicContextInputs; -use crate::context::Context; +use crate::context::{ + PrivateContext, + PublicContext, +}; use crate::note::{ note_header::NoteHeader, note_interface::NoteInterface, @@ -11,12 +14,12 @@ use crate::constants_gen::EMPTY_NULLIFIED_COMMITMENT; use crate::types::option::Option; fn create_note( - context: &mut Context, + context: &mut PrivateContext, storage_slot: Field, note: &mut Note, note_interface: NoteInterface, ) { - let contract_address = context.inputs.call_context.storage_contract_address; + let contract_address = (*context).this_address(); let header = NoteHeader { contract_address, storage_slot, nonce: 0 }; let set_header = note_interface.set_header; @@ -31,12 +34,12 @@ fn create_note( } fn create_note_hash_from_public( - inputs: PublicContextInputs, + context: PublicContext, storage_slot: Field, note: &mut Note, note_interface: NoteInterface, ) { - let contract_address = inputs.call_context.storage_contract_address; + let contract_address = context.this_address(); let header = NoteHeader { contract_address, storage_slot, nonce: 0 }; let set_header = note_interface.set_header; @@ -47,7 +50,7 @@ fn create_note_hash_from_public( } fn destroy_note( - context: &mut Context, + context: &mut PrivateContext, storage_slot: Field, note: Note, note_interface: NoteInterface, diff --git a/yarn-project/noir-libs/noir-aztec/src/note/note_getter.nr b/yarn-project/noir-libs/noir-aztec/src/note/note_getter.nr index af17c68..b9d0f07 100644 --- a/yarn-project/noir-libs/noir-aztec/src/note/note_getter.nr +++ b/yarn-project/noir-libs/noir-aztec/src/note/note_getter.nr @@ -5,7 +5,7 @@ use crate::constants_gen::{ MAX_NOTES_PER_PAGE, VIEW_NOTE_ORACLE_RETURN_LENGTH, }; -use crate::context::Context; +use crate::context::PrivateContext; use crate::note::{ note_getter_options::NoteGetterOptions, note_interface::NoteInterface, @@ -20,20 +20,20 @@ use crate::oracle; use crate::types::option::Option; fn check_note_header( - context: Context, + context: PrivateContext, storage_slot: Field, note_interface: NoteInterface, note: Note, ) { let get_header = note_interface.get_header; let header = get_header(note); - let contract_address = context.inputs.call_context.storage_contract_address; + let contract_address = context.this_address(); assert(header.contract_address == contract_address); assert(header.storage_slot == storage_slot); } fn ensure_note_exists( - context: &mut Context, + context: &mut PrivateContext, storage_slot: Field, note_interface: NoteInterface, note: &mut Note, @@ -56,7 +56,7 @@ fn ensure_note_exists( // notes via the oracle. // Modifies the note by populating it with header info. fn ensure_note_hash_exists( - context: &mut Context, + context: &mut PrivateContext, storage_slot: Field, note_interface: NoteInterface, note: &mut Note, @@ -67,7 +67,7 @@ fn ensure_note_hash_exists( // - and the nonce (used in the unique siloed note hash) let set_header = note_interface.set_header; let note_header = NoteHeader { - contract_address: context.inputs.call_context.storage_contract_address, + contract_address: (*context).this_address(), // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be // real nonce (once public kernel applies nonces). nonce: 0, @@ -98,7 +98,7 @@ fn ensure_note_hash_exists( } fn get_note( - context: &mut Context, + context: &mut PrivateContext, storage_slot: Field, note_interface: NoteInterface, ) -> Note { @@ -113,7 +113,7 @@ fn get_note( } fn get_notes( - context: &mut Context, + context: &mut PrivateContext, storage_slot: Field, note_interface: NoteInterface, options: NoteGetterOptions, diff --git a/yarn-project/noir-libs/noir-aztec/src/public_call_stack_item.nr b/yarn-project/noir-libs/noir-aztec/src/public_call_stack_item.nr index 7942f16..25318b2 100644 --- a/yarn-project/noir-libs/noir-aztec/src/public_call_stack_item.nr +++ b/yarn-project/noir-libs/noir-aztec/src/public_call_stack_item.nr @@ -13,8 +13,6 @@ use crate::constants_gen::{ // oracles use crate::oracle::{ enqueue_public_function_call::enqueue_public_function_call_internal, - public_call::call_public_function_internal, - arguments, }; struct PublicCallStackItem { @@ -34,28 +32,3 @@ impl PublicCallStackItem { } } -// An open function doesn't have a context, so we call this pure function instead of going via Context. -fn call_public_function( - contract_address: Field, - function_selector: Field, - args: [Field; N], -) -> [Field; RETURN_VALUES_LENGTH] { - let args_hash = abi::hash_args(args); - assert(args_hash == arguments::pack_arguments(args)); - call_public_function_internal( - contract_address, - function_selector, - args_hash, - ) -} - -fn call_public_function_no_args( - contract_address: Field, - function_selector: Field, -) -> [Field; RETURN_VALUES_LENGTH] { - call_public_function_internal( - contract_address, - function_selector, - 0, - ) -} diff --git a/yarn-project/noir-libs/noir-aztec/src/state_vars/immutable_singleton.nr b/yarn-project/noir-libs/noir-aztec/src/state_vars/immutable_singleton.nr index a04c3b6..4614057 100644 --- a/yarn-project/noir-libs/noir-aztec/src/state_vars/immutable_singleton.nr +++ b/yarn-project/noir-libs/noir-aztec/src/state_vars/immutable_singleton.nr @@ -1,5 +1,5 @@ use dep::std::hash::pedersen_with_separator; -use crate::context::Context; +use crate::context::PrivateContext; use crate::note::lifecycle::create_note; use crate::note::note_getter::{ get_note, @@ -27,7 +27,7 @@ impl ImmutableSingleton { oracle::notes::is_nullifier_emitted(nullifier) } - fn initialise(self, context: &mut Context, note: &mut Note) { + fn initialise(self, context: &mut PrivateContext, note: &mut Note) { // Nullify the storage slot. let nullifier = self.compute_initialisation_nullifier(); context.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT); @@ -39,12 +39,12 @@ impl ImmutableSingleton { pedersen_with_separator([self.storage_slot], GENERATOR_INDEX__INITIALISATION_NULLIFIER)[0] } - fn get_note(self, context: &mut Context) -> Note { + fn get_note(self, context: &mut PrivateContext) -> Note { let storage_slot = self.storage_slot; get_note(context, storage_slot, self.note_interface) } - fn assert_contains(self, context: &mut Context, note: &mut Note) { + fn assert_contains(self, context: &mut PrivateContext, note: &mut Note) { ensure_note_exists(context, self.storage_slot, self.note_interface, note); } } \ No newline at end of file diff --git a/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr b/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr index 28e9f3c..1d38cbe 100644 --- a/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr +++ b/yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr @@ -1,5 +1,8 @@ use crate::abi::PublicContextInputs; -use crate::context::Context; +use crate::context::{ + PrivateContext, + PublicContext, +}; use crate::note::lifecycle::{create_note, create_note_hash_from_public, destroy_note}; use crate::note::{ note_getter::{get_notes, ensure_note_exists, ensure_note_hash_exists}, @@ -19,32 +22,32 @@ impl Set { Set { storage_slot, note_interface } } - fn insert(self, context: &mut Context, note: &mut Note) { + fn insert(self, context: &mut PrivateContext, note: &mut Note) { create_note(context, self.storage_slot, note, self.note_interface); } - fn insert_from_public(self, inputs: PublicContextInputs, note: &mut Note) { - create_note_hash_from_public(inputs, self.storage_slot, note, self.note_interface); + fn insert_from_public(self, context: PublicContext, note: &mut Note) { + create_note_hash_from_public(context, self.storage_slot, note, self.note_interface); } - fn assert_contains(self, context: &mut Context, note: &mut Note) { + fn assert_contains(self, context: &mut PrivateContext, note: &mut Note) { ensure_note_exists(context, self.storage_slot, self.note_interface, note); } // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): rename to // `assert_contains` and replace function above ^ once public kernel injects // nonces to note hashes. - fn assert_contains_note_hash(self, context: &mut Context, note: &mut Note) { + fn assert_contains_note_hash(self, context: &mut PrivateContext, note: &mut Note) { ensure_note_hash_exists(context, self.storage_slot, self.note_interface, note) } - fn remove(self, context: &mut Context, note: Note) { + fn remove(self, context: &mut PrivateContext, note: Note) { destroy_note(context, self.storage_slot, note, self.note_interface); } fn get_notes( self, - context: &mut Context, + context: &mut PrivateContext, options: NoteGetterOptions, ) -> [Option; S] { let storage_slot = self.storage_slot; diff --git a/yarn-project/noir-libs/noir-aztec/src/state_vars/singleton.nr b/yarn-project/noir-libs/noir-aztec/src/state_vars/singleton.nr index 510bad4..d6eb1c7 100644 --- a/yarn-project/noir-libs/noir-aztec/src/state_vars/singleton.nr +++ b/yarn-project/noir-libs/noir-aztec/src/state_vars/singleton.nr @@ -1,5 +1,5 @@ use dep::std::hash::pedersen_with_separator; -use crate::context::Context; +use crate::context::PrivateContext; use crate::oracle; use crate::note::{ lifecycle::{ @@ -29,7 +29,7 @@ impl Singleton { oracle::notes::is_nullifier_emitted(nullifier) } - fn initialise(self, context: &mut Context, note: &mut Note) { + fn initialise(self, context: &mut PrivateContext, note: &mut Note) { // Nullify the storage slot. let nullifier = self.compute_initialisation_nullifier(); context.push_new_nullifier(nullifier, EMPTY_NULLIFIED_COMMITMENT); @@ -41,7 +41,7 @@ impl Singleton { pedersen_with_separator([self.storage_slot], GENERATOR_INDEX__INITIALISATION_NULLIFIER)[0] } - fn replace(self, context: &mut Context, new_note: &mut Note) { + fn replace(self, context: &mut PrivateContext, new_note: &mut Note) { let prev_note = get_note(context, self.storage_slot, self.note_interface); // Nullify previous note. @@ -51,7 +51,7 @@ impl Singleton { create_note(context, self.storage_slot, new_note, self.note_interface); } - fn get_note(self, context: &mut Context) -> Note { + fn get_note(self, context: &mut PrivateContext) -> Note { let mut note = get_note(context, self.storage_slot, self.note_interface); // Nullify current note to make sure it's reading the latest note. diff --git a/yarn-project/noir-libs/value-note/src/utils.nr b/yarn-project/noir-libs/value-note/src/utils.nr index 7b951ef..974ae8d 100644 --- a/yarn-project/noir-libs/value-note/src/utils.nr +++ b/yarn-project/noir-libs/value-note/src/utils.nr @@ -1,4 +1,4 @@ -use dep::aztec::context::Context; +use dep::aztec::context::PrivateContext; use dep::aztec::log::emit_encrypted_log; use dep::aztec::note::note_getter_options::NoteGetterOptions; use dep::aztec::oracle::get_public_key::get_public_key; @@ -10,7 +10,7 @@ use crate::{ }; fn spend_notes( - context: &mut Context, + context: &mut PrivateContext, balance: Set, amount: Field, owner: Field, @@ -50,7 +50,7 @@ fn spend_notes( let encryption_pub_key = get_public_key(owner); emit_encrypted_log( context, - context.inputs.call_context.storage_contract_address, + (*context).this_address(), balance.storage_slot, encryption_pub_key, encrypted_data, @@ -58,7 +58,7 @@ fn spend_notes( } fn send_note( - context: &mut Context, + context: &mut PrivateContext, balance: Set, amount: Field, recipient: Field, @@ -73,7 +73,7 @@ fn send_note( let encryption_pub_key = get_public_key(recipient); emit_encrypted_log( context, - context.inputs.call_context.storage_contract_address, + (*context).this_address(), balance.storage_slot, encryption_pub_key, note.serialise(),