Skip to content

Commit 26a0d49

Browse files
authored
refactor!: introduce compute_note_hash_for_(consumption/insertion) (AztecProtocol#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
1 parent 8368659 commit 26a0d49

File tree

19 files changed

+67
-55
lines changed

19 files changed

+67
-55
lines changed

boxes/token/src/contracts/src/types/token_note.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ use dep::aztec::{
88
note::{
99
note_header::NoteHeader,
1010
note_interface::NoteInterface,
11-
utils::compute_note_hash_for_read_or_nullify,
11+
utils::compute_note_hash_for_consumption,
1212
},
1313
context::PrivateContext,
1414
state_vars::set::Set,
@@ -65,7 +65,7 @@ impl NoteInterface for TokenNote {
6565

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

7979
fn compute_nullifier_without_context(self) -> Field {
80-
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
80+
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
8181
let secret = get_nullifier_secret_key(self.owner);
8282
// TODO(#1205) Should use a non-zero generator index.
8383
pedersen_hash([

boxes/token/src/contracts/src/types/transparent_note.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use dep::aztec::{
33
note::{
44
note_header::NoteHeader,
55
note_interface::NoteInterface,
6-
utils::compute_note_hash_for_read_or_nullify,
6+
utils::compute_note_hash_for_consumption,
77
},
88
hash::{compute_secret_hash, pedersen_hash},
99
context::PrivateContext,
@@ -52,7 +52,7 @@ impl NoteInterface for TransparentNote {
5252
}
5353

5454
fn compute_nullifier_without_context(self) -> Field {
55-
let siloed_note_hash = compute_note_hash_for_read_or_nullify(self);
55+
let siloed_note_hash = compute_note_hash_for_consumption(self);
5656
// TODO(#1205) Should use a non-zero generator index.
5757
pedersen_hash([self.secret, siloed_note_hash],0)
5858
}

docs/docs/misc/migration_notes.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,15 @@ impl NoteInterface for CardNote {
2929
self.owner.to_field(),
3030
], 0)
3131
}
32-
``````
32+
```
33+
34+
### Introduce `compute_note_hash_for_consumption` and `compute_note_hash_for_insertion`
35+
36+
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).
37+
38+
`compute_note_hash_for_consumption` replaces `compute_note_hash_for_read_or_nullify`.
39+
`compute_note_hash_for_insertion` is new, and mainly used in `lifecycle.nr``
40+
3341

3442
## 0.22.0
3543

yarn-project/aztec-nr/address-note/src/address_note.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use dep::aztec::{
99
note::{
1010
note_header::NoteHeader,
1111
note_interface::NoteInterface,
12-
utils::compute_note_hash_for_read_or_nullify,
12+
utils::compute_note_hash_for_consumption,
1313
},
1414
oracle::{
1515
rand::rand,
@@ -55,7 +55,7 @@ impl NoteInterface for AddressNote {
5555
}
5656

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

6868
fn compute_nullifier_without_context(self) -> Field {
69-
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
69+
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
7070
let secret = get_nullifier_secret_key(self.owner);
7171
// TODO(#1205) Should use a non-zero generator index.
7272
pedersen_hash([

yarn-project/aztec-nr/aztec/src/history/note_inclusion.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use dep::std::merkle::compute_merkle_root;
33
use crate::{
44
context::PrivateContext,
55
note::{
6-
utils::compute_note_hash_for_read_or_nullify,
6+
utils::compute_note_hash_for_consumption,
77
note_header::NoteHeader,
88
note_interface::NoteInterface,
99
},
@@ -34,7 +34,7 @@ pub fn prove_note_inclusion<Note, N>(
3434
block_number: u32, // The block at which we'll prove that the note exists
3535
context: PrivateContext
3636
) where Note: NoteInterface {
37-
let note_commitment = compute_note_hash_for_read_or_nullify(note_with_header);
37+
let note_commitment = compute_note_hash_for_consumption(note_with_header);
3838

3939
prove_note_commitment_inclusion(note_commitment, block_number, context);
4040
}

yarn-project/aztec-nr/aztec/src/note/lifecycle.nr

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use crate::context::{
66
use crate::note::{
77
note_header::NoteHeader,
88
note_interface::NoteInterface,
9-
utils::compute_note_hash_for_read_or_nullify,
9+
utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption},
1010
};
1111
use crate::oracle::notes::{notify_created_note, notify_nullified_note};
1212
use dep::protocol_types::traits::{Serialize, Deserialize};
@@ -23,7 +23,7 @@ pub fn create_note<Note, N>(
2323
// TODO: change this to note.setHeader(header) once https://github.com/noir-lang/noir/issues/4095 is fixed
2424
Note::set_header(note, header);
2525
// As `is_transient` is true, this will compute the inner note hsah
26-
let inner_note_hash = compute_note_hash_for_read_or_nullify(*note);
26+
let inner_note_hash = compute_note_hash_for_insertion(*note);
2727

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

4747
context.push_new_note_hash(inner_note_hash);
4848
}
4949

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

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

68-
context.push_new_nullifier(nullifier, nullified_commitment)
68+
context.push_new_nullifier(nullifier, consumed_note_hash)
6969
}

yarn-project/aztec-nr/aztec/src/note/note_getter.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::note::{
1414
note_getter_options::{NoteGetterOptions, Select, Sort, SortOrder, Comparator, NoteStatus},
1515
note_interface::NoteInterface,
1616
note_viewer_options::NoteViewerOptions,
17-
utils::compute_note_hash_for_read_or_nullify,
17+
utils::compute_note_hash_for_consumption,
1818
};
1919
use crate::oracle;
2020

@@ -78,7 +78,7 @@ pub fn get_note<Note, N>(
7878

7979
check_note_header(*context, storage_slot, note);
8080

81-
let note_hash_for_read_request = compute_note_hash_for_read_or_nullify(note);
81+
let note_hash_for_read_request = compute_note_hash_for_consumption(note);
8282

8383
context.push_read_request(note_hash_for_read_request);
8484
note
@@ -104,7 +104,7 @@ pub fn get_notes<Note, N, FILTER_ARGS>(
104104
}
105105
prev_fields = fields;
106106

107-
let note_hash_for_read_request = compute_note_hash_for_read_or_nullify(note);
107+
let note_hash_for_read_request = compute_note_hash_for_consumption(note);
108108
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1410): test to ensure
109109
// failure if malicious oracle injects 0 nonce here for a "pre-existing" note.
110110
context.push_read_request(note_hash_for_read_request);

yarn-project/aztec-nr/aztec/src/note/utils.nr

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,17 @@ pub fn compute_siloed_nullifier<Note>(
6363
pedersen_hash(input, GENERATOR_INDEX__OUTER_NULLIFIER)
6464
}
6565

66-
pub fn compute_note_hash_for_read_or_nullify<Note>(note: Note) -> Field where Note: NoteInterface {
66+
pub fn compute_note_hash_for_insertion<Note>(note: Note) -> Field where Note: NoteInterface {
67+
compute_inner_note_hash(note)
68+
}
69+
70+
pub fn compute_note_hash_for_consumption<Note>(note: Note) -> Field where Note: NoteInterface {
6771
let header = note.get_header();
72+
// There are 3 cases for reading a note intended for consumption:
73+
// 1. The note was inserted in this transaction, and is transient.
74+
// 2. The note was inserted in a previous transaction, and was inserted in public
75+
// 3. The note was inserted in a previous transaction, and was inserted in private
6876

69-
// TODO(https://github.com/AztecProtocol/aztec-packages/issues/1386)
7077
if (header.is_transient) {
7178
// If a note is transient, we just read the inner_note_hash (kernel will silo by contract address).
7279
compute_inner_note_hash(note)

yarn-project/aztec-nr/aztec/src/state_vars/set.nr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use crate::note::{
1313
note_header::NoteHeader,
1414
note_interface::NoteInterface,
1515
note_viewer_options::NoteViewerOptions,
16-
utils::compute_note_hash_for_read_or_nullify,
16+
utils::compute_note_hash_for_consumption,
1717
};
1818

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

yarn-project/aztec-nr/value-note/src/value_note.nr

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use dep::aztec::{
66
note::{
77
note_header::NoteHeader,
88
note_interface::NoteInterface,
9-
utils::compute_note_hash_for_read_or_nullify,
9+
utils::compute_note_hash_for_consumption,
1010
},
1111
oracle::{
1212
rand::rand,
@@ -56,7 +56,7 @@ impl NoteInterface for ValueNote {
5656
// docs:start:nullifier
5757

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

7171
fn compute_nullifier_without_context(self) -> Field {
72-
let note_hash_for_nullify = compute_note_hash_for_read_or_nullify(self);
72+
let note_hash_for_nullify = compute_note_hash_for_consumption(self);
7373
let secret = get_nullifier_secret_key(self.owner);
7474
// TODO(#1205) Should use a non-zero generator index.
7575
pedersen_hash([

0 commit comments

Comments
 (0)