AztecProtocol / aztec-packages

Apache License 2.0
155 stars 157 forks source link

Compute note type id in Noir instead of Rust #7165

Open benesjan opened 1 week ago

benesjan commented 1 week ago

Blocked by meta programming.

In this PR I've introduced a tiny-keccak dependency to macros and I compute the note type id directly in the rust macro code. This is unnecessary because instead we can just compute the value in Noir. This will not result in an unnecessary gate count increase because Noir evaluates constants during compile time.

I actually already tried implementing this but the changes turned out to be very complicated because in the current macro code we try to fetch the note type id directly from HIR when we are injecting note exports (inject_note_exports function). The issue is that at that point in compilation timeline the constants were not evaluated yet and the function throws for that reason.

I decided to give up on trying to make it work due to us completely rewriting the macros in a not-so-distant future due to meta programming. Once meta programming is in place, we should try tackling this (or ideally do it the correct way during the rewrite).

Here is the Noir functionality I've implemented but eventually reverted:

 use crate::utils::field::field_from_bytes;

global SELECTOR_SIZE = 4;

pub fn compute_note_selector<N>(signature: str<N>) -> Field {
    let bytes = signature.as_bytes();
    let hash = dep::std::hash::keccak256(bytes, bytes.len() as u32);

    let mut selector_be_bytes = [0; SELECTOR_SIZE];
    for i in 0..SELECTOR_SIZE {
        selector_be_bytes[i] = hash[i];
    }

    field_from_bytes(selector_be_bytes, true)
}