Skip to content

Commit

Permalink
refactor!: note interfaces (#12106)
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan authored and AztecBot committed Feb 26, 2025
1 parent 5722a03 commit 6999180
Show file tree
Hide file tree
Showing 28 changed files with 327 additions and 308 deletions.
53 changes: 3 additions & 50 deletions address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,64 +1,17 @@
use dep::aztec::{
context::PrivateContext,
keys::getters::{get_nsk_app, get_public_keys},
macros::notes::note,
note::{
note_interface::NullifiableNote, note_metadata::SettledNoteMetadata,
retrieved_note::RetrievedNote, utils::compute_note_hash_for_nullify,
},
oracle::random::random,
protocol_types::{
address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash_with_separator, traits::Serialize,
},
protocol_types::{address::AztecAddress, traits::Serialize},
};

// docs:start:address_note_def
// docs:start:address_note_struct
// Stores an address
#[note]
#[derive(Eq, Serialize)]
#[derive(Eq)]
pub struct AddressNote {
address: AztecAddress,
owner: AztecAddress,
randomness: Field,
}
// docs:end:address_note_struct

impl NullifiableNote for AddressNote {
fn compute_nullifier(
self,
context: &mut PrivateContext,
note_hash_for_nullify: Field,
) -> Field {
let owner_npk_m_hash = get_public_keys(self.owner).npk_m.hash();
let secret = context.request_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
)
}

unconstrained fn compute_nullifier_without_context(
self,
storage_slot: Field,
contract_address: AztecAddress,
note_nonce: Field,
) -> Field {
let retrieved_note = RetrievedNote {
note: self,
contract_address,
metadata: SettledNoteMetadata::new(note_nonce).into(),
};
let note_hash_for_nullify = compute_note_hash_for_nullify(retrieved_note, storage_slot);
let owner_npk_m_hash = get_public_keys(self.owner).npk_m.hash();
let secret = get_nsk_app(owner_npk_m_hash);
poseidon2_hash_with_separator(
[note_hash_for_nullify, secret],
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
)
}
}

impl AddressNote {
pub fn new(address: AztecAddress, owner: AztecAddress) -> Self {
Expand All @@ -69,5 +22,5 @@ impl AddressNote {
let randomness = unsafe { random() };
AddressNote { address, owner, randomness }
}
// docs:end:address_note_def
}
// docs:end:address_note_def
6 changes: 3 additions & 3 deletions aztec/src/discovery/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ global NOTE_PRIVATE_LOG_RESERVED_FIELDS: u32 = 2;
pub global MAX_NOTE_PACKED_LEN: u32 = PRIVATE_LOG_SIZE_IN_FIELDS - NOTE_PRIVATE_LOG_RESERVED_FIELDS;

pub struct NoteHashAndNullifier {
/// The result of NoteInterface::compute_note_hash
/// The result of NoteHash::compute_note_hash
pub note_hash: Field,
/// The result of NullifiableNote::compute_nullifier_without_context
pub inner_nullifier: Field,
Expand All @@ -33,7 +33,7 @@ pub struct NoteHashAndNullifier {
///
/// ```
/// |packed_note_content, contract_address, nonce, storage_slot, note_type_id| {
/// if note_type_id == MyNoteType::get_note_type_id() {
/// if note_type_id == MyNoteType::get_id() {
/// assert(packed_note_content.len() == MY_NOTE_TYPE_SERIALIZATION_LENGTH);
///
/// let note = MyNoteType::unpack(aztec::utils::array::subarray(packed_note.storage(), 0));
Expand All @@ -46,7 +46,7 @@ pub struct NoteHashAndNullifier {
/// note_hash, inner_nullifier
/// }
/// )
/// } else if note_type_id == MyOtherNoteType::get_note_type_id() {
/// } else if note_type_id == MyOtherNoteType::get_id() {
/// ... // Similar to above but calling MyOtherNoteType::unpack_content
/// } else {
/// Option::none() // Unknown note type ID
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use crate::{
ecdh_shared_secret::derive_ecdh_shared_secret_using_aztec_address,
ephemeral::generate_ephemeral_key_pair,
},
note::{note_emission::NoteEmission, note_interface::NoteInterface},
note::{note_emission::NoteEmission, note_interface::NoteType},
oracle::{
notes::{get_app_tag_as_sender, increment_app_tagging_secret_index_as_sender},
random::random,
Expand Down Expand Up @@ -218,14 +218,14 @@ fn compute_note_plaintext_for_this_strategy<Note, let N: u32>(
storage_slot: Field,
) -> [u8; N * 32 + 64]
where
Note: NoteInterface + Packable<N>,
Note: NoteType + Packable<N>,
{
let packed_note = note.pack();

let storage_slot_bytes: [u8; 32] = storage_slot.to_be_bytes();

// TODO(#10952): The following can be reduced to 7 bits
let note_type_id_bytes: [u8; 32] = Note::get_note_type_id().to_be_bytes();
let note_type_id_bytes: [u8; 32] = Note::get_id().to_be_bytes();

// We combine all the bytes into plaintext_bytes:
let mut plaintext_bytes: [u8; N * 32 + 64] = [0; N * 32 + 64];
Expand All @@ -252,7 +252,7 @@ fn compute_log<Note, let N: u32>(
sender: AztecAddress,
) -> [Field; PRIVATE_LOG_SIZE_IN_FIELDS]
where
Note: NoteInterface + Packable<N>,
Note: NoteType + Packable<N>,
{
// *****************************************************************************
// Compute the shared secret
Expand Down Expand Up @@ -416,7 +416,7 @@ unconstrained fn compute_log_unconstrained<Note, let N: u32>(
sender: AztecAddress,
) -> [Field; PRIVATE_LOG_SIZE_IN_FIELDS]
where
Note: NoteInterface + Packable<N>,
Note: NoteType + Packable<N>,
{
compute_log(context, note, storage_slot, recipient, sender)
}
Expand All @@ -431,7 +431,7 @@ pub fn encode_and_encrypt_note<Note, let N: u32>(
sender: AztecAddress,
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> ()
where
Note: NoteInterface + Packable<N>,
Note: NoteType + Packable<N>,
{
|e: NoteEmission<Note>| {
let note = e.note;
Expand All @@ -454,7 +454,7 @@ pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32>(
sender: AztecAddress,
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> ()
where
Note: NoteInterface + Packable<N>,
Note: NoteType + Packable<N>,
{
|e: NoteEmission<Note>| {
let note = e.note;
Expand Down
7 changes: 3 additions & 4 deletions aztec/src/history/note_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@ use dep::protocol_types::merkle_tree::root::root_from_sibling_path;

use crate::{
note::{
note_interface::{NoteInterface, NullifiableNote},
retrieved_note::RetrievedNote,
note_interface::NoteHash, retrieved_note::RetrievedNote,
utils::compute_note_hash_for_nullify,
},
oracle::get_membership_witness::get_note_hash_membership_witness,
Expand All @@ -17,13 +16,13 @@ trait ProveNoteInclusion {
storage_slot: Field,
)
where
Note: NoteInterface + NullifiableNote;
Note: NoteHash;
}

impl ProveNoteInclusion for BlockHeader {
fn prove_note_inclusion<Note>(self, retrieved_note: RetrievedNote<Note>, storage_slot: Field)
where
Note: NoteInterface + NullifiableNote,
Note: NoteHash,
{
let note_hash = compute_note_hash_for_nullify(retrieved_note, storage_slot);

Expand Down
6 changes: 3 additions & 3 deletions aztec/src/history/note_validity.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
context::PrivateContext,
note::{note_interface::{NoteInterface, NullifiableNote}, retrieved_note::RetrievedNote},
note::{note_interface::NoteHash, retrieved_note::RetrievedNote},
};

use dep::protocol_types::block_header::BlockHeader;
Expand All @@ -13,7 +13,7 @@ trait ProveNoteValidity {
context: &mut PrivateContext,
)
where
Note: NoteInterface + NullifiableNote;
Note: NoteHash;
}

impl ProveNoteValidity for BlockHeader {
Expand All @@ -24,7 +24,7 @@ impl ProveNoteValidity for BlockHeader {
context: &mut PrivateContext,
)
where
Note: NoteInterface + NullifiableNote,
Note: NoteHash,
{
self.prove_note_inclusion(retrieved_note, storage_slot);
self.prove_note_not_nullified(retrieved_note, storage_slot, context);
Expand Down
7 changes: 3 additions & 4 deletions aztec/src/history/nullifier_inclusion.nr
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ use dep::protocol_types::merkle_tree::root::root_from_sibling_path;
use crate::{
context::PrivateContext,
note::{
note_interface::{NoteInterface, NullifiableNote},
retrieved_note::RetrievedNote,
note_interface::NoteHash, retrieved_note::RetrievedNote,
utils::compute_siloed_note_nullifier,
},
oracle::get_nullifier_membership_witness::get_nullifier_membership_witness,
Expand Down Expand Up @@ -50,7 +49,7 @@ trait ProveNoteIsNullified {
context: &mut PrivateContext,
)
where
Note: NoteInterface + NullifiableNote;
Note: NoteHash;
}

impl ProveNoteIsNullified for BlockHeader {
Expand All @@ -62,7 +61,7 @@ impl ProveNoteIsNullified for BlockHeader {
context: &mut PrivateContext,
)
where
Note: NoteInterface + NullifiableNote,
Note: NoteHash,
{
let nullifier = compute_siloed_note_nullifier(retrieved_note, storage_slot, context);

Expand Down
7 changes: 3 additions & 4 deletions aztec/src/history/nullifier_non_inclusion.nr
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
use crate::{
context::PrivateContext,
note::{
note_interface::{NoteInterface, NullifiableNote},
retrieved_note::RetrievedNote,
note_interface::NoteHash, retrieved_note::RetrievedNote,
utils::compute_siloed_note_nullifier,
},
oracle::get_nullifier_membership_witness::get_low_nullifier_membership_witness,
Expand Down Expand Up @@ -63,7 +62,7 @@ trait ProveNoteNotNullified {
context: &mut PrivateContext,
)
where
Note: NoteInterface + NullifiableNote;
Note: NoteHash;
}

impl ProveNoteNotNullified for BlockHeader {
Expand All @@ -75,7 +74,7 @@ impl ProveNoteNotNullified for BlockHeader {
context: &mut PrivateContext,
)
where
Note: NoteInterface + NullifiableNote,
Note: NoteHash,
{
let nullifier = compute_siloed_note_nullifier(retrieved_note, storage_slot, context);

Expand Down
3 changes: 1 addition & 2 deletions aztec/src/macros/functions/call_interface_stubs.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use crate::macros::utils::{
add_to_field_slice, AsStrQuote, compute_fn_selector, get_trait_impl_method, is_fn_private,
is_fn_view,
add_to_field_slice, AsStrQuote, compute_fn_selector, is_fn_private, is_fn_view,
};
use std::meta::{type_of, unquote};

Expand Down
4 changes: 2 additions & 2 deletions aztec/src/macros/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -147,8 +147,8 @@ comptime fn generate_contract_library_method_compute_note_hash_and_nullifier() -

let get_note_type_id = get_trait_impl_method(
typ,
quote { crate::note::note_interface::NoteInterface },
quote { get_note_type_id },
quote { crate::note::note_interface::NoteType },
quote { get_id },
);
let unpack = get_trait_impl_method(
typ,
Expand Down
Loading

0 comments on commit 6999180

Please sign in to comment.