Skip to content

Commit

Permalink
refactor!: introduce compute_note_hash_for_(consumption/insertion) (A…
Browse files Browse the repository at this point in the history
…ztecProtocol#4344)

Part of AztecProtocol#4199 

Replaces the `compute_note_hash_for_read_or_nullify` with two functions:
- `compute_note_hash_for_insertion`
- `compute_note_hash_for_consumption`

The practical difference is small, but should make it a bit easier to follow the flow
  • Loading branch information
LHerskind committed Jan 31, 2024
1 parent 8368659 commit 26a0d49
Show file tree
Hide file tree
Showing 19 changed files with 67 additions and 55 deletions.
6 changes: 3 additions & 3 deletions boxes/token/src/contracts/src/types/token_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
context::PrivateContext,
state_vars::set::Set,
Expand Down Expand Up @@ -65,7 +65,7 @@ impl NoteInterface for TokenNote {

// docs:start:nullifier
fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand All @@ -77,7 +77,7 @@ impl NoteInterface for TokenNote {
// docs:end:nullifier

fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand Down
4 changes: 2 additions & 2 deletions boxes/token/src/contracts/src/types/transparent_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
hash::{compute_secret_hash, pedersen_hash},
context::PrivateContext,
Expand Down Expand Up @@ -52,7 +52,7 @@ impl NoteInterface for TransparentNote {
}

fn compute_nullifier_without_context(self) -> Field {
let siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
let siloed_note_hash = compute_note_hash_for_consumption(self);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([self.secret, siloed_note_hash],0)
}
Expand Down
10 changes: 9 additions & 1 deletion docs/docs/misc/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,15 @@ impl NoteInterface for CardNote {
self.owner.to_field(),
], 0)
}
``````
```

### Introduce `compute_note_hash_for_consumption` and `compute_note_hash_for_insertion`

Makes a split in logic for note hash computation for consumption and insertion. This is to avoid confusion between the two, and to make it clear that the note hash for consumption is different from the note hash for insertion (sometimes).

`compute_note_hash_for_consumption` replaces `compute_note_hash_for_read_or_nullify`.
`compute_note_hash_for_insertion` is new, and mainly used in `lifecycle.nr``


## 0.22.0

Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
oracle::{
rand::rand,
Expand Down Expand Up @@ -55,7 +55,7 @@ impl NoteInterface for AddressNote {
}

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand All @@ -66,7 +66,7 @@ impl NoteInterface for AddressNote {
}

fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::std::merkle::compute_merkle_root;
use crate::{
context::PrivateContext,
note::{
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
note_header::NoteHeader,
note_interface::NoteInterface,
},
Expand Down Expand Up @@ -34,7 +34,7 @@ pub fn prove_note_inclusion<Note, N>(
block_number: u32, // The block at which we'll prove that the note exists
context: PrivateContext
) where Note: NoteInterface {
let note_commitment = compute_note_hash_for_read_or_nullify(note_with_header);
let note_commitment = compute_note_hash_for_consumption(note_with_header);

prove_note_commitment_inclusion(note_commitment, block_number, context);
}
22 changes: 11 additions & 11 deletions yarn-project/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::context::{
use crate::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption},
};
use crate::oracle::notes::{notify_created_note, notify_nullified_note};
use dep::protocol_types::traits::{Serialize, Deserialize};
Expand All @@ -23,7 +23,7 @@ pub fn create_note<Note, N>(
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
Note::set_header(note, header);
// As `is_transient` is true, this will compute the inner note hsah
let inner_note_hash = compute_note_hash_for_read_or_nullify(*note);
let inner_note_hash = compute_note_hash_for_insertion(*note);

// TODO: Strong typing required because of https://github.com/noir-lang/noir/issues/4088
let serialized_note: [Field; N] = Note::serialize(*note);
Expand All @@ -42,28 +42,28 @@ pub fn create_note_hash_from_public<Note>(context: &mut PublicContext, storage_s
let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
Note::set_header(note, header);
let inner_note_hash = compute_note_hash_for_read_or_nullify(*note);
let inner_note_hash = compute_note_hash_for_insertion(*note);

context.push_new_note_hash(inner_note_hash);
}

pub fn destroy_note<Note>(context: &mut PrivateContext, note: Note) where Note: NoteInterface {
let mut nullifier = 0;
let mut nullified_commitment: Field = 0;
let mut consumed_note_hash: Field = 0;
nullifier = note.compute_nullifier(context);

// We also need the note commitment corresponding to the "nullifier"
// We also need the note hash corresponding to the "nullifier"
let header = note.get_header();
// `nullified_commitment` is used to inform the kernel which pending commitment
// `consumed_note_hash` is used to inform the kernel which pending note hash
// the nullifier corresponds to so they can be matched and both squashed/deleted.
// nonzero nonce implies "persistable" nullifier (nullifies a persistent/in-tree
// commitment) in which case `nullified_commitment` is not used since the kernel
// note hash) in which case `consumed_note_hash` is not used since the kernel
// just siloes and forwards the nullifier to its output.
if (header.is_transient) {
// TODO(1718): Can we reuse the note commitment computed in `compute_nullifier`?
nullified_commitment = compute_note_hash_for_read_or_nullify(note);
// TODO(1718): Can we reuse the note hash computed in `compute_nullifier`?
consumed_note_hash = compute_note_hash_for_consumption(note);
}
assert(notify_nullified_note(nullifier, nullified_commitment) == 0);
assert(notify_nullified_note(nullifier, consumed_note_hash) == 0);

context.push_new_nullifier(nullifier, nullified_commitment)
context.push_new_nullifier(nullifier, consumed_note_hash)
}
6 changes: 3 additions & 3 deletions yarn-project/aztec-nr/aztec/src/note/note_getter.nr
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use crate::note::{
note_getter_options::{NoteGetterOptions, Select, Sort, SortOrder, Comparator, NoteStatus},
note_interface::NoteInterface,
note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
};
use crate::oracle;

Expand Down Expand Up @@ -78,7 +78,7 @@ pub fn get_note<Note, N>(

check_note_header(*context, storage_slot, note);

let note_hash_for_read_request = compute_note_hash_for_read_or_nullify(note);
let note_hash_for_read_request = compute_note_hash_for_consumption(note);

context.push_read_request(note_hash_for_read_request);
note
Expand All @@ -104,7 +104,7 @@ pub fn get_notes<Note, N, FILTER_ARGS>(
}
prev_fields = fields;

let note_hash_for_read_request = compute_note_hash_for_read_or_nullify(note);
let note_hash_for_read_request = compute_note_hash_for_consumption(note);
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1410): test to ensure
// failure if malicious oracle injects 0 nonce here for a "pre-existing" note.
context.push_read_request(note_hash_for_read_request);
Expand Down
11 changes: 9 additions & 2 deletions yarn-project/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,17 @@ pub fn compute_siloed_nullifier<Note>(
pedersen_hash(input, GENERATOR_INDEX__OUTER_NULLIFIER)
}

pub fn compute_note_hash_for_read_or_nullify<Note>(note: Note) -> Field where Note: NoteInterface {
pub fn compute_note_hash_for_insertion<Note>(note: Note) -> Field where Note: NoteInterface {
compute_inner_note_hash(note)
}

pub fn compute_note_hash_for_consumption<Note>(note: Note) -> Field where Note: NoteInterface {
let header = note.get_header();
// There are 3 cases for reading a note intended for consumption:
// 1. The note was inserted in this transaction, and is transient.
// 2. The note was inserted in a previous transaction, and was inserted in public
// 3. The note was inserted in a previous transaction, and was inserted in private

// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386)
if (header.is_transient) {
// If a note is transient, we just read the inner_note_hash (kernel will silo by contract address).
compute_inner_note_hash(note)
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-nr/aztec/src/state_vars/set.nr
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
note_viewer_options::NoteViewerOptions,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
};

// docs:start:struct
Expand Down Expand Up @@ -74,7 +74,7 @@ impl<Note> Set<Note> {
// docs:start:remove
pub fn remove(self, note: Note) where Note: NoteInterface {
let context = self.context.private.unwrap();
let note_hash = compute_note_hash_for_read_or_nullify(note);
let note_hash = compute_note_hash_for_consumption(note);
let has_been_read = context.read_requests.any(|r: SideEffect| r.value == note_hash);
assert(has_been_read, "Can only remove a note that has been read from the set.");

Expand Down
6 changes: 3 additions & 3 deletions yarn-project/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
oracle::{
rand::rand,
Expand Down Expand Up @@ -56,7 +56,7 @@ impl NoteInterface for ValueNote {
// docs:start:nullifier

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand All @@ -69,7 +69,7 @@ impl NoteInterface for ValueNote {
// docs:end:nullifier

fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
oracle::{
nullifier_key::get_nullifier_secret_key,
Expand Down Expand Up @@ -64,7 +64,7 @@ impl NoteInterface for CardNote {
}

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
pedersen_hash([
note_hash_for_nullify,
Expand All @@ -74,7 +74,7 @@ impl NoteInterface for CardNote {
}

fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
pedersen_hash([
note_hash_for_nullify,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
oracle::{
nullifier_key::get_nullifier_secret_key,
Expand Down Expand Up @@ -81,7 +81,7 @@ impl NoteInterface for EcdsaPublicKeyNote {
}

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let unique_siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
let unique_siloed_note_hash = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand All @@ -92,7 +92,7 @@ impl NoteInterface for EcdsaPublicKeyNote {
}

fn compute_nullifier_without_context(self) -> Field {
let unique_siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
let unique_siloed_note_hash = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
hash::pedersen_hash,
oracle::{
Expand Down Expand Up @@ -48,7 +48,7 @@ impl Deserialize<PUBLIC_KEY_NOTE_LEN> for PublicKeyNote {
impl NoteInterface for PublicKeyNote {

fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let unique_siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
let unique_siloed_note_hash = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand All @@ -59,7 +59,7 @@ impl NoteInterface for PublicKeyNote {
}

fn compute_nullifier_without_context(self) -> Field {
let unique_siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
let unique_siloed_note_hash = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ use dep::aztec::note::{
use dep::aztec::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
};
use crate::types::token_note::TokenNote;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
context::PrivateContext,
state_vars::set::Set,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl NoteInterface for TokenNote {

// docs:start:nullifier
fn compute_nullifier(self, context: &mut PrivateContext) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = context.request_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand All @@ -73,7 +73,7 @@ impl NoteInterface for TokenNote {
// docs:end:nullifier

fn compute_nullifier_without_context(self) -> Field {
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
let secret = get_nullifier_secret_key(self.owner);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
utils::compute_note_hash_for_consumption,
},
hash::{compute_secret_hash, pedersen_hash},
context::PrivateContext,
Expand Down Expand Up @@ -52,8 +52,7 @@ impl NoteInterface for TransparentNote {
}

fn compute_nullifier_without_context(self) -> Field {
// TODO(#1386): should use `compute_note_hash_for_read_or_nullify` once public functions inject nonce!
let siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
let siloed_note_hash = compute_note_hash_for_consumption(self);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([self.secret, siloed_note_hash],0)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use dep::aztec::note::{
};
use dep::aztec::note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_note_hash_for_read_or_nullify,
note_interface::NoteInterface
};
use crate::types::token_note::TokenNote;

Expand Down
Loading

0 comments on commit 26a0d49

Please sign in to comment.