Skip to content

Commit

Permalink
feat!: aztec_macros are dead, long live aztec::macros (#8438)
Browse files Browse the repository at this point in the history
Requires noir sync and noir-lang/noir#6078 to
be merged so `compute_note_hash_and_optionally_a_nullifier` can be
autogenerated.

Also, fixed a lot of explicit numeric generics, used arithmetics on them
(yay!!) and introduced a macro for partial notes

---------

Co-authored-by: TomAFrench <tom@tomfren.ch>
Co-authored-by: Tom French <15848336+TomAFrench@users.noreply.github.com>
Co-authored-by: benesjan <janbenes1234@gmail.com>
Co-authored-by: Nicolás Venturo <nicolas.venturo@gmail.com>
Co-authored-by: Ary Borenszweig <asterite@gmail.com>
  • Loading branch information
6 people authored and AztecBot committed Sep 25, 2024
1 parent 366450b commit c0e1e98
Show file tree
Hide file tree
Showing 41 changed files with 1,836 additions and 405 deletions.
18 changes: 6 additions & 12 deletions address-note/src/address_note.nr
Original file line number Diff line number Diff line change
@@ -1,20 +1,14 @@
use dep::aztec::{
protocol_types::{
address::AztecAddress, traits::Empty, constants::GENERATOR_INDEX__NOTE_NULLIFIER,
hash::poseidon2_hash_with_separator
},
note::{note_header::NoteHeader, note_interface::NoteInterface, utils::compute_note_hash_for_nullify},
oracle::unsafe_rand::unsafe_rand, keys::getters::get_nsk_app, context::PrivateContext
protocol_types::{address::AztecAddress, constants::GENERATOR_INDEX__NOTE_NULLIFIER, hash::poseidon2_hash_with_separator},
note::{note_header::NoteHeader, note_interface::NullifiableNote, utils::compute_note_hash_for_nullify},
oracle::unsafe_rand::unsafe_rand, keys::getters::get_nsk_app, context::PrivateContext,
macros::notes::note
};

global ADDRESS_NOTE_LEN: u32 = 3;
// ADDRESS_NOTE_LEN * 32 + 32(storage_slot as bytes) + 32(note_type_id as bytes)
global ADDRESS_NOTE_BYTES_LEN: u32 = 3 * 32 + 64;

// docs:start:address_note_def
// docs:start:address_note_struct
// Stores an address
#[aztec(note)]
#[note]
struct AddressNote {
address: AztecAddress,
// The nullifying public key hash is used with the nsk_app to ensure that the note can be privately spent.
Expand All @@ -23,7 +17,7 @@ struct AddressNote {
}
// docs:end:address_note_struct

impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
impl NullifiableNote for AddressNote {

fn compute_nullifier(self, context: &mut PrivateContext, note_hash_for_nullify: Field) -> Field {
let secret = context.request_nsk_app(self.npk_m_hash);
Expand Down
2 changes: 1 addition & 1 deletion aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ use dep::protocol_types::{
};

// When finished, one can call .finish() to convert back to the abi
struct PrivateContext {
pub struct PrivateContext {
// docs:start:private-context
inputs: PrivateContextInputs,
side_effect_counter: u32,
Expand Down
44 changes: 22 additions & 22 deletions aztec/src/encrypted_logs/encrypted_event_emission.nr
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use crate::{
};
use dep::protocol_types::{address::AztecAddress, hash::sha256_to_field};

unconstrained fn compute_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
unconstrained fn compute_unconstrained<Event, let N: u32, let OB: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> ([u8; OB], Field) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
compute(
contract_address,
randomness,
Expand All @@ -26,15 +26,15 @@ unconstrained fn compute_unconstrained<Event, let NB: u32, let MB: u32, let OB:
)
}

fn compute<Event, let NB: u32, let MB: u32, let OB: u32>(
fn compute<Event, let N: u32, let OB: u32>(
contract_address: AztecAddress,
randomness: Field,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
event: Event
) -> ([u8; OB], Field) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> ([u8; OB], Field) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
let encrypted_log: [u8; OB] = compute_encrypted_event_log(
contract_address,
randomness,
Expand All @@ -48,26 +48,26 @@ fn compute<Event, let NB: u32, let MB: u32, let OB: u32>(
(encrypted_log, log_hash)
}

fn emit_with_keys<Event, let NB: u32, let MB: u32, let OB: u32>(
fn emit_with_keys<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
event: Event,
ovpk: OvpkM,
ivpk: IvpkM,
iv: AztecAddress,
inner_compute: fn(AztecAddress, Field, Field, OvpkM, IvpkM, AztecAddress, Event) -> ([u8; OB], Field)
) where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
let contract_address: AztecAddress = context.this_address();
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
let (encrypted_log, log_hash) = inner_compute(contract_address, randomness, ovsk_app, ovpk, ivpk, iv, event);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}

pub fn encode_and_encrypt_event<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -76,11 +76,11 @@ pub fn encode_and_encrypt_event<Event, let NB: u32, let MB: u32, let OB: u32>(
}
}

pub fn encode_and_encrypt_event_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -89,75 +89,75 @@ pub fn encode_and_encrypt_event_unconstrained<Event, let NB: u32, let MB: u32, l
}
}

pub fn encode_and_encrypt_event_with_randomness<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute);
}
}

pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext, Field)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
emit_with_keys(context, randomness, e, ovpk, ivpk, iv, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_unconstrained<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
let randomness = unsafe_rand();
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_with_randomness<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute);
}
}

pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, let NB: u32, let MB: u32, let OB: u32>(
pub fn encode_and_encrypt_event_with_keys_with_randomness_unconstrained<Event, let N: u32, let OB: u32>(
context: &mut PrivateContext,
randomness: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<NB, MB>, [u8; NB]: LensForEncryptedEvent<NB, OB> {
) -> fn[(&mut PrivateContext, Field, OvpkM, IvpkM, AztecAddress)](Event) -> () where Event: EventInterface<N>, [u8; N * 32 + 64]: LensForEncryptedEvent<N * 32 + 64, OB> {
| e: Event | {
emit_with_keys(context, randomness, e, ovpk, ivpk, recipient, compute_unconstrained);
}
Expand Down
24 changes: 12 additions & 12 deletions aztec/src/encrypted_logs/encrypted_note_emission.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@ use crate::{
};
use dep::protocol_types::{hash::sha256_to_field, address::AztecAddress, abis::note_hash::NoteHash};

fn compute_raw_note_log<Note, let N: u32, let NB: u32, let M: u32>(
fn compute_raw_note_log<Note, let N: u32, let M: u32>(
context: PrivateContext,
note: Note,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
num_public_values: u8 // Number of values to be appended to the log in public (used in partial note flow).
) -> (u32, [u8; M], Field) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> (u32, [u8; M], Field) where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
let note_header = note.get_header();
let note_hash_counter = note_header.note_hash_counter;
let storage_slot = note_header.storage_slot;
Expand All @@ -39,23 +39,23 @@ fn compute_raw_note_log<Note, let N: u32, let NB: u32, let M: u32>(
(note_hash_counter, encrypted_log, log_hash)
}

unconstrained fn compute_raw_note_log_unconstrained<Note, let N: u32, let NB: u32, let M: u32>(
unconstrained fn compute_raw_note_log_unconstrained<Note, let N: u32, let M: u32>(
context: PrivateContext,
note: Note,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
num_public_values: u8 // Number of values to be appended to the log in public (used in partial note flow).
) -> (u32, [u8; M], Field) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> (u32, [u8; M], Field) where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_raw_note_log(context, note, ovsk_app, ovpk, ivpk, recipient, num_public_values)
}

pub fn encode_and_encrypt_note<Note, let N: u32, let NB: u32, let M: u32>(
pub fn encode_and_encrypt_note<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
let ovpk = get_public_keys(ov).ovpk_m;
let ivpk = get_public_keys(iv).ivpk_m;
Expand All @@ -69,11 +69,11 @@ pub fn encode_and_encrypt_note<Note, let N: u32, let NB: u32, let M: u32>(
}
}

pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32, let NB: u32, let M: u32>(
pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
// Note: We could save a lot of gates by obtaining the following keys in an unconstrained context but this
// function is currently not used anywhere so we are not optimizing it.
Expand All @@ -93,12 +93,12 @@ pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32, let NB: u32, let
}
}

pub fn encode_and_encrypt_note_with_keys<Note, let N: u32, let NB: u32, let M: u32>(
pub fn encode_and_encrypt_note_with_keys<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());

Expand All @@ -111,12 +111,12 @@ pub fn encode_and_encrypt_note_with_keys<Note, let N: u32, let NB: u32, let M: u
}
}

pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, let N: u32, let NB: u32, let M: u32>(
pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, let N: u32, let M: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
) -> fn[(&mut PrivateContext, OvpkM, IvpkM, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
// Number of public values is always 0 here because `encode_and_encrypt_note_with_keys_unconstrained(...)` is only called
// in the non-partial note flow.
Expand Down
Loading

0 comments on commit c0e1e98

Please sign in to comment.