aztec-nr - noir_aztec::messages::discovery

Type alias ComputeNoteHashAndNullifier

pub type ComputeNoteHashAndNullifier<Env> = unconstrained fn[Env](BoundedVec<Field, 8>, AztecAddress, Field, Field, AztecAddress, Field, Field) -> Option<NoteHashAndNullifier>;

A contract's way of computing note hashes and nullifiers.

Each contract in the network is free to compute their note's hash and nullifiers as they see fit - the hash function itself and the nullifier derivation are not enshrined or standardized. Some aztec-nr functions however do need to know the details of this computation (e.g. when finding new notes), which is what this type represents.

This function takes a note's packed content, storage slot, note type ID, address of the emitting contract, randomness and note nonce, and attempts to compute its inner note hash (not siloed by address nor uniqued by nonce) and inner nullifier (not siloed by address).

Transient Notes

This function is meant to always be used on settled notes, i.e. those that have been inserted into the trees and for which the nonce is known. It is never invoked in the context of a transient note, as those are not involved in message processing.

Automatic Implementation

The [#aztec] macro automatically creates a correct implementation of this function for each contract by inspecting all note types in use and the storage layout. This injected function is a #[contract_library_function] called _compute_note_hash_and_nullifier, and it looks something like this:

|packed_note, owner, storage_slot, note_type_id, contract_address, randomness, note_nonce| {
    if note_type_id == MyNoteType::get_id() {
        assert(packed_note.len() == MY_NOTE_TYPE_SERIALIZATION_LENGTH);

        let note = MyNoteType::unpack(aztec::utils::array::subarray(packed_note.storage(), 0));

        let note_hash = note.compute_note_hash(owner, storage_slot, randomness);
        let note_hash_for_nullification = aztec::note::utils::compute_note_hash_for_nullification(
            HintedNote{ note, contract_address, metadata: SettledNoteMetadata::new(note_nonce).into() },
            storage_slot
        );

        let inner_nullifier = note.compute_nullifier_unconstrained(owner, note_hash_for_nullification);

        Option::some(
            aztec::messages::discovery::NoteHashAndNullifier {
                note_hash, inner_nullifier
            }
        )
    } else if note_type_id == MyOtherNoteType::get_id() {
          ... // Similar to above but calling MyOtherNoteType::unpack_content
    } else {
        Option::none() // Unknown note type ID
    };
}