Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: 7 bit long note_type_id #10951

Draft
wants to merge 3 commits into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions cspell.json
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@
"noirc",
"noirup",
"nullifer",
"Nullifiable",
"offchain",
"onchain",
"opentelemetry",
Expand Down
31 changes: 16 additions & 15 deletions noir-projects/aztec-nr/aztec/src/macros/notes/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,19 @@ comptime global NOTE_HEADER_TYPE: Type = type_of(NoteHeader::empty());
pub comptime mut global NOTES: UHashMap<Type, (StructDefinition, u32, Field, [(Quoted, u32, bool)]), BuildHasherDefault<Poseidon2Hasher>> =
UHashMap::default();

/// Computes a note type id by hashing a note name (e.g. `TokenNote`), getting the first 4 bytes of the hash
/// and returning it as a `Field`.
comptime fn compute_note_type_id(name: Quoted) -> Field {
let (name_as_str_quote, _) = name.as_str_quote();
pub comptime mut global NOTE_TYPE_ID_COUNTER: u32 = 0;

/// The note type id is set by enumerating the note types.
comptime fn get_next_note_type_id() -> Field {
// We assert that the note type id fits within 7 bits
assert(
NOTE_TYPE_ID_COUNTER < 128 as u32,
"A contract can contain at most 128 different note types",
);

unquote!(
quote {
let bytes = $name_as_str_quote.as_bytes();
let hash = protocol_types::hash::poseidon2_hash_bytes(bytes);
let hash_bytes = hash.to_be_bytes::<4>();
protocol_types::utils::field::field_from_bytes(hash_bytes, true)
},
)
let note_type_id = NOTE_TYPE_ID_COUNTER as Field;
NOTE_TYPE_ID_COUNTER += 1;
note_type_id
}

/// Generates default `NoteInterface` implementation for a given note struct `s` and returns it as quote along with
Expand Down Expand Up @@ -123,6 +123,7 @@ comptime fn generate_note_interface(
let mut buffer: [u8; $content_len * 32 + 64] = [0; $content_len * 32 + 64];

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] = $name::get_note_type_id().to_be_bytes();

for i in 0..32 {
Expand Down Expand Up @@ -871,7 +872,7 @@ pub comptime fn partial_note(s: StructDefinition, nullable_fields: [Quoted]) ->
inject_note_header(s);

let note_properties = generate_note_properties(s);
let note_type_id = compute_note_type_id(s.name());
let note_type_id = get_next_note_type_id();
let (setup_payload_impl, setup_payload_name) =
generate_setup_payload(s, indexed_fixed_fields, indexed_nullable_fields);
let (finalization_payload_impl, finalization_payload_name) =
Expand Down Expand Up @@ -914,7 +915,7 @@ pub comptime fn note(s: StructDefinition) -> Quoted {
inject_note_header(s);

let note_properties = generate_note_properties(s);
let note_type_id = compute_note_type_id(s.name());
let note_type_id = get_next_note_type_id();
let (note_interface_impl, note_serialized_len) = generate_note_interface(
s,
note_type_id,
Expand Down Expand Up @@ -944,7 +945,7 @@ pub comptime fn note_custom_interface(s: StructDefinition) -> Quoted {
inject_note_header(s);

let note_properties = generate_note_properties(s);
let note_type_id = compute_note_type_id(s.name());
let note_type_id = get_next_note_type_id();
let serialized_len_type = fresh_type_variable();
let note_interface_impl = s.as_type().get_trait_impl(
quote { crate::note::note_interface::NoteInterface<$serialized_len_type> }
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/note/note_type_id.nr
nventuro marked this conversation as resolved.
Show resolved Hide resolved

This file was deleted.

2 changes: 1 addition & 1 deletion yarn-project/circuit-types/src/interfaces/pxe.ts
Original file line number Diff line number Diff line change
Expand Up @@ -388,7 +388,7 @@ export interface PXE {
isContractInitialized(address: AztecAddress): Promise<boolean>;

/**
* Returns the enctypred events given search parameters.
* Returns the encrypted events given search parameters.
* @param eventMetadata - Metadata of the event. This should be the class generated from the contract. e.g. Contract.events.Event
* @param from - The block number to search from.
* @param limit - The amount of blocks to search.
Expand Down
4 changes: 3 additions & 1 deletion yarn-project/foundation/src/abi/note_selector.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export interface NoteSelector {
_branding: 'NoteSelector';
}

/** A note selector is the first 4 bytes of the hash of a note signature. */
/** A note selector is the first 4 bytes of the hash of a note signature
benesjan marked this conversation as resolved.
Show resolved Hide resolved
* TODO(#10952): Encoding of note type id can be reduced to 7 bits.
*/
export class NoteSelector extends Selector {
/**
* Deserializes from a buffer or reader, corresponding to a write in cpp.
Expand Down
Loading