Skip to content

Commit

Permalink
feat: limit exposed functions on note utils (AztecProtocol#4207)
Browse files Browse the repository at this point in the history
- Removes the `note_hash` as it was only used in the `note_utils`. 
- Uses the `compute_note_hash_for_read_or_nullify` instead of its inner
functions when results will be the same
- This increases cost, but here I'm doing it for clarity making it
easier for us to have something useful first that is understandable and
then optimize later.
- Stop exposing the inner functions to enhance clarity (at performance
cost)
  • Loading branch information
LHerskind authored Jan 28, 2024
1 parent e1184ff commit 8338f39
Show file tree
Hide file tree
Showing 10 changed files with 47 additions and 53 deletions.
5 changes: 2 additions & 3 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_siloed_note_hash,
utils::compute_note_hash_for_read_or_nullify,
},
hash::{compute_secret_hash, pedersen_hash},
context::PrivateContext,
Expand Down Expand Up @@ -75,8 +75,7 @@ impl TransparentNote {
}

pub 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_siloed_note_hash(TransparentNoteMethods, self);
let siloed_note_hash = compute_note_hash_for_read_or_nullify(TransparentNoteMethods, self);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([self.secret, siloed_note_hash],0)
}
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_unique_siloed_note_hash,
utils::compute_note_hash_for_read_or_nullify,
note_header::NoteHeader,
note_interface::NoteInterface,
},
Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn prove_note_inclusion<Note, N>(
block_number: u32, // The block at which we'll prove that the note exists
context: PrivateContext
) {
let note_commitment = compute_unique_siloed_note_hash(note_interface, note_with_header);
let note_commitment = compute_note_hash_for_read_or_nullify(note_interface, note_with_header);

prove_note_commitment_inclusion(note_commitment, block_number, context);
}
1 change: 0 additions & 1 deletion yarn-project/aztec-nr/aztec/src/note.nr
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
mod lifecycle;
mod note_getter;
mod note_getter_options;
mod note_hash;
mod note_header;
mod note_interface;
mod note_viewer_options;
Expand Down
9 changes: 5 additions & 4 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_inner_note_hash,
utils::compute_note_hash_for_read_or_nullify,
};
use crate::oracle::notes::{notify_created_note, notify_nullified_note};

Expand All @@ -22,7 +22,8 @@ pub fn create_note<Note, N>(
let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
let set_header = note_interface.set_header;
set_header(note, header);
let inner_note_hash = compute_inner_note_hash(note_interface, *note);
// As `is_transient` is true, this will compute the inner note hsah
let inner_note_hash = compute_note_hash_for_read_or_nullify(note_interface, *note);

let serialize = note_interface.serialize;
let serialized_note = serialize(*note);
Expand All @@ -47,7 +48,7 @@ pub fn create_note_hash_from_public<Note, N>(
let header = NoteHeader { contract_address, storage_slot, nonce: 0, is_transient: true };
let set_header = note_interface.set_header;
set_header(note, header);
let inner_note_hash = compute_inner_note_hash(note_interface, *note);
let inner_note_hash = compute_note_hash_for_read_or_nullify(note_interface, *note);

context.push_new_note_hash(inner_note_hash);
}
Expand All @@ -72,7 +73,7 @@ pub fn destroy_note<Note, N>(
// 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_inner_note_hash(note_interface, note);
nullified_commitment = compute_note_hash_for_read_or_nullify(note_interface, note);
}
assert(notify_nullified_note(nullifier, nullified_commitment) == 0);

Expand Down
23 changes: 0 additions & 23 deletions yarn-project/aztec-nr/aztec/src/note/note_hash.nr

This file was deleted.

36 changes: 28 additions & 8 deletions yarn-project/aztec-nr/aztec/src/note/utils.nr
Original file line number Diff line number Diff line change
@@ -1,18 +1,38 @@
use dep::protocol_types::{
constants::GENERATOR_INDEX__OUTER_NULLIFIER,
hash::pedersen_hash,
};
use crate::{
context::PrivateContext,
note::{
note_hash::{compute_inner_hash, compute_siloed_hash, compute_unique_hash},
note_header::NoteHeader,
note_interface::NoteInterface,
},
utils::arr_copy_slice,
};

pub fn compute_inner_note_hash<Note, N>(note_interface: NoteInterface<Note, N>, note: Note) -> Field {
use dep::protocol_types::{
address::AztecAddress,
constants::{
GENERATOR_INDEX__OUTER_NULLIFIER,
GENERATOR_INDEX__UNIQUE_COMMITMENT,
GENERATOR_INDEX__SILOED_COMMITMENT,
},
hash::pedersen_hash,
};

fn compute_inner_hash(storage_slot: Field, note_hash: Field) -> Field {
// TODO(#1205) Do we need a generator index here?
pedersen_hash([storage_slot, note_hash], 0)
}

fn compute_siloed_hash(contract_address: AztecAddress, inner_note_hash: Field) -> Field {
let inputs = [contract_address.to_field(), inner_note_hash];
pedersen_hash(inputs, GENERATOR_INDEX__SILOED_COMMITMENT)
}

fn compute_unique_hash(nonce: Field, siloed_note_hash: Field) -> Field {
let inputs = [nonce, siloed_note_hash];
pedersen_hash(inputs, GENERATOR_INDEX__UNIQUE_COMMITMENT)
}

fn compute_inner_note_hash<Note, N>(note_interface: NoteInterface<Note, N>, note: Note) -> Field {
let get_header = note_interface.get_header;
let header = get_header(note);

Expand All @@ -22,7 +42,7 @@ pub fn compute_inner_note_hash<Note, N>(note_interface: NoteInterface<Note, N>,
compute_inner_hash(header.storage_slot, note_hash)
}

pub fn compute_siloed_note_hash<Note, N>(note_interface: NoteInterface<Note, N>, note_with_header: Note) -> Field {
fn compute_siloed_note_hash<Note, N>(note_interface: NoteInterface<Note, N>, note_with_header: Note) -> Field {
let get_header = note_interface.get_header;
let header = get_header(note_with_header);

Expand All @@ -31,7 +51,7 @@ pub fn compute_siloed_note_hash<Note, N>(note_interface: NoteInterface<Note, N>,
compute_siloed_hash(header.contract_address, inner_note_hash)
}

pub fn compute_unique_siloed_note_hash<Note, N>(note_interface: NoteInterface<Note, N>, note_with_header: Note) -> Field {
fn compute_unique_siloed_note_hash<Note, N>(note_interface: NoteInterface<Note, N>, note_with_header: Note) -> Field {
let get_header = note_interface.get_header;
let header = get_header(note_with_header);

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_unique_siloed_note_hash,
utils::compute_note_hash_for_read_or_nullify,
},
oracle::{
nullifier_key::get_nullifier_secret_key,
Expand Down Expand Up @@ -61,7 +61,7 @@ impl EcdsaPublicKeyNote {
}

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

pub fn compute_nullifier_without_context(self) -> Field {
let unique_siloed_note_hash = compute_unique_siloed_note_hash(EcdsaPublicKeyNoteInterface, self);
let unique_siloed_note_hash = compute_note_hash_for_read_or_nullify(EcdsaPublicKeyNoteInterface, 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_unique_siloed_note_hash,
utils::compute_note_hash_for_read_or_nullify,
},
hash::pedersen_hash,
oracle::{
Expand Down Expand Up @@ -41,7 +41,7 @@ impl PublicKeyNote {
}

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

pub fn compute_nullifier_without_context(self) -> Field {
let unique_siloed_note_hash = compute_unique_siloed_note_hash(PublicKeyNoteMethods, self);
let unique_siloed_note_hash = compute_note_hash_for_read_or_nullify(PublicKeyNoteMethods, 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_siloed_note_hash,
utils::compute_note_hash_for_read_or_nullify,
},
hash::{compute_secret_hash, pedersen_hash},
context::PrivateContext,
Expand Down Expand Up @@ -75,8 +75,7 @@ impl TransparentNote {
}

pub 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_siloed_note_hash(TransparentNoteMethods, self);
let siloed_note_hash = compute_note_hash_for_read_or_nullify(TransparentNoteMethods, 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 @@ -3,7 +3,7 @@ use dep::aztec::{
note::{
note_header::NoteHeader,
note_interface::NoteInterface,
utils::compute_siloed_note_hash,
utils::compute_note_hash_for_read_or_nullify,
},
hash::{compute_secret_hash, pedersen_hash},
context::PrivateContext,
Expand Down Expand Up @@ -75,8 +75,7 @@ impl TransparentNote {
}

pub 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_siloed_note_hash(TransparentNoteMethods, self);
let siloed_note_hash = compute_note_hash_for_read_or_nullify(TransparentNoteMethods, self);
// TODO(#1205) Should use a non-zero generator index.
pedersen_hash([self.secret, siloed_note_hash],0)
}
Expand Down

0 comments on commit 8338f39

Please sign in to comment.