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 87aa804..ee725e4 100644 --- a/yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr +++ b/yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr @@ -1,9 +1,11 @@ +use crate::abi::PublicContextInputs; use crate::context::Context; use crate::note::{ note_header::NoteHeader, note_interface::NoteInterface, utils::compute_inner_note_hash, }; +use crate::oracle::create_commitment::create_commitment; use crate::oracle::notes::{notify_created_note, notify_nullified_note}; use crate::constants_gen::EMPTY_NULLIFIED_COMMITMENT; use crate::types::option::Option; @@ -14,13 +16,12 @@ fn create_note( note: &mut Note, note_interface: NoteInterface, ) { - let mut inner_note_hash = 0; let contract_address = context.inputs.call_context.storage_contract_address; let header = NoteHeader { contract_address, storage_slot, nonce: 0 }; let set_header = note_interface.set_header; set_header(note, header); - inner_note_hash = compute_inner_note_hash(note_interface, *note); + let inner_note_hash = compute_inner_note_hash(note_interface, *note); let serialise = note_interface.serialise; let preimage = serialise(*note); @@ -29,6 +30,22 @@ fn create_note( context.push_new_note_hash(inner_note_hash); } +fn create_note_hash_from_public( + inputs: PublicContextInputs, + storage_slot: Field, + note: &mut Note, + note_interface: NoteInterface, +) { + let contract_address = inputs.call_context.storage_contract_address; + + let header = NoteHeader { contract_address, storage_slot, nonce: 0 }; + let set_header = note_interface.set_header; + set_header(note, header); + let inner_note_hash = compute_inner_note_hash(note_interface, *note); + + create_commitment(inner_note_hash); +} + fn destroy_note( context: &mut Context, storage_slot: Field, 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 3a6c243..af17c68 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 @@ -9,9 +9,13 @@ use crate::context::Context; use crate::note::{ note_getter_options::NoteGetterOptions, note_interface::NoteInterface, + note_header::NoteHeader, utils::compute_note_hash_for_read_or_nullify, utils::compute_unique_siloed_note_hash, + utils::compute_inner_note_hash, + utils::compute_siloed_note_hash, }; +use crate::messaging::get_commitment_getter_data::make_commitment_getter_data; use crate::oracle; use crate::types::option::Option; @@ -48,6 +52,51 @@ fn ensure_note_exists( context.push_read_request(note_hash_for_read_request); } +// Ensure a note's hash exists in the tree without retrieving the entire +// notes via the oracle. +// Modifies the note by populating it with header info. +fn ensure_note_hash_exists( + context: &mut Context, + storage_slot: Field, + note_interface: NoteInterface, + note: &mut Note, +) { + // Initialize header of note. Must be done before computing note hashes as it initializes the: + // - storage slot (used in inner note hash) + // - the contract address (used in siloed note hash) + // - 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, + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be + // real nonce (once public kernel applies nonces). + nonce: 0, + storage_slot + }; + set_header(note, note_header); + + // Get a note from oracle and early out if it doesn't exist. + let inner_note_hash = compute_inner_note_hash(note_interface, *note); + + let raw_oracle_ret = oracle::get_commitment::get_commitment(inner_note_hash); + let deserialized_oracle_ret = make_commitment_getter_data(raw_oracle_ret, 0); + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be + // unique_siloed_note_hash once public kernel applies nonces + let saved_siloed_note_hash = deserialized_oracle_ret.message; + + assert(saved_siloed_note_hash != 0); // TODO(dbanks12): necessary? + + check_note_header(*context, storage_slot, note_interface, *note); + + // Ensure that the note hash retrieved from oracle matches the one computed from note. + let computed_siloed_note_hash = compute_siloed_note_hash(note_interface, *note); + // TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386): should be + // compute_note_hash_for_read_or_nullify once public kernel applies nonces + assert(computed_siloed_note_hash == saved_siloed_note_hash); + + context.push_read_request(computed_siloed_note_hash); +} + fn get_note( context: &mut Context, storage_slot: Field, 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 2f5066c..28e9f3c 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,11 +1,12 @@ +use crate::abi::PublicContextInputs; use crate::context::Context; -use crate::note::lifecycle::{create_note, destroy_note}; +use crate::note::lifecycle::{create_note, create_note_hash_from_public, destroy_note}; use crate::note::{ - note_getter::{get_notes, ensure_note_exists}, + note_getter::{get_notes, ensure_note_exists, ensure_note_hash_exists}, note_getter_options::NoteGetterOptions, note_interface::NoteInterface, + utils::compute_inner_note_hash, }; -use crate::oracle::create_commitment::create_commitment; use crate::types::option::Option; struct Set { @@ -22,16 +23,21 @@ impl Set { create_note(context, self.storage_slot, note, self.note_interface); } - fn insert_from_public(self, note: Note) { - let compute_note_hash = self.note_interface.compute_note_hash; - let note_hash = compute_note_hash(note); - create_commitment(note_hash); + fn insert_from_public(self, inputs: PublicContextInputs, note: &mut Note) { + create_note_hash_from_public(inputs, self.storage_slot, note, self.note_interface); } fn assert_contains(self, context: &mut Context, 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) { + ensure_note_hash_exists(context, self.storage_slot, self.note_interface, note) + } + fn remove(self, context: &mut Context, note: Note) { destroy_note(context, self.storage_slot, note, self.note_interface); }