Skip to content

Commit

Permalink
feat: replace note emits
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jun 11, 2024
1 parent e692b32 commit b1ba695
Show file tree
Hide file tree
Showing 23 changed files with 96 additions and 205 deletions.
12 changes: 0 additions & 12 deletions noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -42,18 +42,6 @@ impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
])
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
// docs:start:encrypted
context.encrypt_and_emit_note(
slot,
ovpk_m,
ivpk_m,
self,
);
// docs:end:encrypted
}
}

impl AddressNote {
Expand Down
23 changes: 0 additions & 23 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -351,29 +351,6 @@ impl PrivateContext {
emit_encrypted_event_log(contract_address, randomness, encrypted_log, counter);
}

pub fn encrypt_and_emit_note<Note, N, NB, M>(
&mut self,
storage_slot: Field,
ovpk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
note: Note
) where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
let note_hash_counter = note.get_header().note_hash_counter;
let note_exists_index = find_index(
self.new_note_hashes.storage,
|n: NoteHash| n.counter == note_hash_counter
);
assert(
note_exists_index as u32 != MAX_NEW_NOTE_HASHES_PER_CALL, "Can only emit a note log for an existing note."
);

let contract_address = self.this_address();
let ovsk_app = self.request_ovsk_app(ovpk_m.hash());

let encrypted_log: [u8; M] = compute_encrypted_note_log(contract_address, storage_slot, ovsk_app, ovpk_m, ivpk_m, note);
self.emit_raw_note_log(note_hash_counter, encrypted_log);
}

pub fn emit_raw_note_log<M>(&mut self, note_hash_counter: u32, encrypted_log: [u8; M]) {
let counter = self.next_counter();
let len = encrypted_log.len() as Field + 4;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::{
context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface},
encrypted_logs::payload::compute_encrypted_note_log
encrypted_logs::payload::compute_encrypted_note_log, oracle::logs_traits::LensForEncryptedLog
};
use dep::protocol_types::{
address::AztecAddress, grumpkin_point::GrumpkinPoint, abis::note_hash::NoteHash,
Expand All @@ -9,12 +9,13 @@ use dep::protocol_types::{

fn emit_with_keys<Note, N, NB, M>(
context: &mut PrivateContext,
storage_slot: Field,
note: Note,
ovpk: GrumpkinPoint,
ivpk: GrumpkinPoint
) where Note: NoteInterface<N, NB> {
let note_hash_counter = note.get_header().note_hash_counter;
) where Note: NoteInterface<N, NB>, [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;

let note_exists_index = find_index(
context.new_note_hashes.storage,
Expand All @@ -32,25 +33,25 @@ fn emit_with_keys<Note, N, NB, M>(
context.emit_raw_note_log(note_hash_counter, encrypted_log);
}

pub fn with_az_enc<Note, N, NB>(
pub fn with_az_enc<Note, N, NB, M>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB> {
) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
let header = context.get_header();
let ovpk = header.get_ovpk_m(context, ov);
let ivpk = header.get_ivpk_m(context, iv);
emit_with_keys(context, e.storage_slot, e.note, ovpk, ivpk);
emit_with_keys(context, e.note, ovpk, ivpk);
}
}

pub fn with_az_enc_with_keys<Note, N, NB>(
pub fn with_az_enc_with_keys<Note, N, NB, M>(
context: &mut PrivateContext,
ovpk: GrumpkinPoint,
ivpk: GrumpkinPoint
) -> fn[(&mut PrivateContext, GrumpkinPoint, GrumpkinPoint)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB> {
) -> fn[(&mut PrivateContext, GrumpkinPoint, GrumpkinPoint)](NoteEmission<Note>) -> () where Note: NoteInterface<N, NB>, [Field; N]: LensForEncryptedLog<N, M> {
| e: NoteEmission<Note> | {
emit_with_keys(context, e.storage_slot, e.note, ovpk, ivpk);
emit_with_keys(context, e.note, ovpk, ivpk);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -70,8 +70,6 @@ mod test {

fn compute_nullifier_without_context(self) -> Field {1}

fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {}

fn serialize_content(self) -> [Field; ADDRESS_NOTE_LEN] { [self.address.to_field(), self.owner.to_field(), self.randomness]}

fn deserialize_content(fields: [Field; ADDRESS_NOTE_LEN]) -> Self {
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/aztec/src/note/lifecycle.nr
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::context::{PrivateContext, PublicContext};
use crate::note::{
note_header::NoteHeader, note_interface::NoteInterface,
utils::{compute_note_hash_for_insertion, compute_note_hash_for_consumption},
note_emission::NoteEmission
note_emission::{NoteEmission, OuterNoteEmission}
};
use crate::oracle::notes::{notify_created_note, notify_nullified_note};

Expand Down Expand Up @@ -35,7 +35,7 @@ pub fn create_note<Note, N, M>(

context.push_new_note_hash(inner_note_hash);

NoteEmission::new(storage_slot, *note)
NoteEmission::new(*note)
}

pub fn create_note_hash_from_public<Note, N, M>(
Expand Down
36 changes: 32 additions & 4 deletions noir-projects/aztec-nr/aztec/src/note/note_emission.nr
Original file line number Diff line number Diff line change
@@ -1,15 +1,43 @@
/**
* A note emission struct containing the information required when to emit a note.
* The exact `emit` logic is passed in by the application code
*/
struct NoteEmission<Note> {
storage_slot: Field,
note: Note
}

impl<Note> NoteEmission<Note> {
pub fn new(storage_slot: Field, note: Note) -> Self {
Self { storage_slot, note }
pub fn new(note: Note) -> Self {
Self { note }
}

pub fn emit<Env>(self, _emit: fn[Env](Self) -> ()) {
_emit(self)
_emit(self);
}

pub fn discard(self) {}
}

/**
* A wrapper of the note emission
* This is the struct provided to application codes, which can be used to emit
* only when a note was actually inserted.
* It is fairly common to have cases where a function conditionally inserts,
* and this allows us to keep the same API for emission in both cases.
*/
struct OuterNoteEmission<Note> {
emission: Option<NoteEmission<Note>>,
}

impl<Note> OuterNoteEmission<Note> {
pub fn new(emission: Option<NoteEmission<Note>>) -> Self {
Self { emission }
}

pub fn emit<Env>(self, _emit: fn[Env](NoteEmission<Note>) -> ()) {
if self.emission.is_some() {
_emit(self.emission.unwrap());
}
}

pub fn discard(self) {}
Expand Down
4 changes: 1 addition & 3 deletions noir-projects/aztec-nr/aztec/src/note/note_interface.nr
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,7 @@ trait NoteInterface<N, M> {
fn compute_nullifier(self, context: &mut PrivateContext) -> Field;

fn compute_nullifier_without_context(self) -> Field;

fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) -> ();


// Autogenerated by the #[aztec(note)] macro unless it is overridden by a custom implementation
fn serialize_content(self) -> [Field; N];

Expand Down
6 changes: 0 additions & 6 deletions noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,6 @@ impl NoteInterface<MOCK_NOTE_LENGTH, MOCK_NOTE_BYTES_LENGTH> for MockNote {
0
}

fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
// MockNote does not support broadcasting. Since this function gets called in various places anyway we will verify
// that the dev really did not intend to broadcast by checking that zero keys were passed in.
assert(ovpk_m.is_zero() & ivpk_m.is_zero(), "MockNote does not support broadcast.");
}

fn to_be_bytes(self, storage_slot: Field) -> [u8; MOCK_NOTE_BYTES_LENGTH] {
let serialized_note = self.serialize_content();

Expand Down
23 changes: 4 additions & 19 deletions noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use dep::aztec::{
context::PrivateContext, protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint},
note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet
note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet,
encrypted_logs::encrypted_note_emission::with_az_enc
};
use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote};

Expand All @@ -24,29 +25,19 @@ impl<Context> EasyPrivateUint<&mut PrivateContext> {
pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let header = self.context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(self.context, owner);
let outgoing_viewer = header.get_ovpk_m(self.context, outgoing_viewer);
let owner_ivpk_m = header.get_ivpk_m(self.context, owner);
// Creates new note for the owner.
let mut addend_note = ValueNote::new(addend as Field, owner_npk_m_hash);

// Insert the new note to the owner's set of notes.
// docs:start:insert
self.set.insert(&mut addend_note);
addend_note.broadcast(
self.context,
self.set.storage_slot,
outgoing_viewer,
owner_ivpk_m
);
self.set.insert(&mut addend_note).emit(with_az_enc(self.context, outgoing_viewer, owner));
// docs:end:insert
}

// Very similar to `value_note::utils::decrement`.
pub fn sub(self, subtrahend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let header = self.context.get_header();
let owner_npk_m_hash = header.get_npk_m_hash(self.context, owner);
let outgoing_viewer_ovpk_m = header.get_ovpk_m(self.context, outgoing_viewer);
let owner_ivpk_m = header.get_ivpk_m(self.context, owner);

// docs:start:get_notes
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend as Field);
Expand All @@ -72,12 +63,6 @@ impl<Context> EasyPrivateUint<&mut PrivateContext> {
// Creates change note for the owner.
let result_value = minuend - subtrahend;
let mut result_note = ValueNote::new(result_value as Field, owner_npk_m_hash);
self.set.insert(&mut result_note);
result_note.broadcast(
self.context,
self.set.storage_slot,
outgoing_viewer_ovpk_m,
owner_ivpk_m
);
self.set.insert(&mut result_note).emit(with_az_enc(self.context, outgoing_viewer, owner));
}
}
10 changes: 0 additions & 10 deletions noir-projects/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -45,16 +45,6 @@ impl NoteInterface<VALUE_NOTE_LEN, VALUE_NOTE_BYTES_LEN> for ValueNote {
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
])
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
slot,
ovpk_m,
ivpk_m,
self,
);
}
}

impl ValueNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ impl NoteInterface<SUBSCRIPTION_NOTE_LEN, SUBSCRIPTION_NOTE_BYTES_LEN> for Subsc
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
])
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
slot,
ovpk_m,
ivpk_m,
self,
);
}
}

impl SubscriptionNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,16 +49,6 @@ impl NoteInterface<CARD_NOTE_LEN, CARD_NOTE_BYTES_LEN> for CardNote {
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
])
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
slot,
ovpk_m,
ivpk_m,
self,
);
}
}

impl Serialize<3> for CardNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,6 @@ impl NoteInterface<ECDSA_PUBLIC_KEY_NOTE_LEN, ECDSA_PUBLIC_KEY_NOTE_BYTES_LEN> f
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
])
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
slot,
ovpk_m,
ivpk_m,
self,
);
}
}

impl EcdsaPublicKeyNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ contract PendingNoteHashes {
use dep::aztec::protocol_types::grumpkin_point::GrumpkinPoint;
use dep::aztec::protocol_types::constants::{MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NEW_NOTE_HASHES_PER_CALL};
use dep::aztec::encrypted_logs::encrypted_note_emission::{with_az_enc, with_az_enc_with_keys};
use dep::aztec::note::note_emission::NoteEmission;

#[aztec(storage)]
struct Storage {
Expand Down Expand Up @@ -355,12 +356,7 @@ contract PendingNoteHashes {
let existing_note_header = good_note.get_header();
bad_note.set_header(existing_note_header);

context.encrypt_and_emit_note(
existing_note_header.storage_slot,
outgoing_viewer_ovpk_m,
owner_ivpk_m,
bad_note
);
NoteEmission::new(bad_note).emit(with_az_enc_with_keys(&mut context, outgoing_viewer_ovpk_m, owner_ivpk_m));
}

#[contract_library_method]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,6 @@ impl NoteInterface<PUBLIC_KEY_NOTE_LEN, PUBLIC_KEY_NOTE_BYTES_LEN> for PublicKey
GENERATOR_INDEX__NOTE_NULLIFIER as Field,
])
}

// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
slot,
ovpk_m,
ivpk_m,
self,
);
}
}

impl PublicKeyNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,6 @@ impl NoteInterface<TEST_NOTE_LEN, TEST_NOTE_BYTES_LENGTH> for TestNote {
// This note is expected to be shared between users and for this reason can't be nullified using a secret.
0
}

fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
// TestNote does not support broadcasting. Since this function gets called in various places anyway we will verify
// that the dev really did not intend to broadcast by checking that zero keys were passed in.
assert(ovpk_m.is_zero() & ivpk_m.is_zero(), "TestNote does not support broadcast.");
}
}

impl TestNote {
Expand Down
Loading

0 comments on commit b1ba695

Please sign in to comment.