Skip to content

Commit

Permalink
chore(noir-libs): TransparentNote rework (#1412)
Browse files Browse the repository at this point in the history
Closes AztecProtocol/aztec-packages#1194
Closes AztecProtocol/aztec-packages#1363

Will likely rename getCommitment alongside all instances of "commitment"
as a part of this other issue:
AztecProtocol/aztec-packages#1408

# Checklist:
Remove the checklist to signal you've completed it. Enable auto-merge if
the PR is ready to merge.
- [x] If the pull request requires a cryptography review (e.g.
cryptographic algorithm implementations) I have added the 'crypto' tag.
- [x] I have reviewed my diff in github, line by line and removed
unexpected formatting changes, testing logs, or commented-out code.
- [x] Every change is related to the PR description.
- [x] 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).

---------

Co-authored-by: Michael Connor <[email protected]>
  • Loading branch information
superstar0402 and iAmMichaelConnor committed Aug 7, 2023
1 parent 16f2a7f commit d0b1b1d
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 9 deletions.
21 changes: 19 additions & 2 deletions yarn-project/noir-libs/noir-aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -14,13 +16,12 @@ fn create_note<Note, N>(
note: &mut Note,
note_interface: NoteInterface<Note, N>,
) {
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);
Expand All @@ -29,6 +30,22 @@ fn create_note<Note, N>(
context.push_new_note_hash(inner_note_hash);
}

fn create_note_hash_from_public<Note, N>(
inputs: PublicContextInputs,
storage_slot: Field,
note: &mut Note,
note_interface: NoteInterface<Note, N>,
) {
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<Note, N>(
context: &mut Context,
storage_slot: Field,
Expand Down
49 changes: 49 additions & 0 deletions yarn-project/noir-libs/noir-aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -48,6 +52,51 @@ fn ensure_note_exists<Note, N>(
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<Note, N>(
context: &mut Context,
storage_slot: Field,
note_interface: NoteInterface<Note, N>,
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<Note, N>(
context: &mut Context,
storage_slot: Field,
Expand Down
20 changes: 13 additions & 7 deletions yarn-project/noir-libs/noir-aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
@@ -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<Note, N> {
Expand All @@ -22,16 +23,21 @@ impl<Note, N> Set<Note, N> {
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);
}
Expand Down

0 comments on commit d0b1b1d

Please sign in to comment.