From abf4984e49ff4e766fb9a4fab31b41850eb48a47 Mon Sep 17 00:00:00 2001 From: sklppy88 Date: Sun, 23 Jun 2024 11:59:17 +0000 Subject: [PATCH] init --- boxes/boxes/react/src/contracts/src/main.nr | 6 +- boxes/boxes/vanilla/src/contracts/src/main.nr | 6 +- .../aztec/src/context/private_context.nr | 40 +------------ .../aztec-nr/aztec/src/encrypted_logs.nr | 1 + .../encrypted_event_emission.nr | 45 ++++++++++++++ .../encrypted_logs/encrypted_note_emission.nr | 4 +- .../aztec/src/encrypted_logs/incoming_body.nr | 4 ++ .../aztec/src/encrypted_logs/payload.nr | 58 +++++++++++++++++++ .../aztec/src/event/event_interface.nr | 1 + .../aztec-nr/aztec/src/oracle/logs_traits.nr | 25 +++++++- .../src/easy_private_uint.nr | 6 +- .../aztec-nr/value-note/src/utils.nr | 4 +- .../app_subscription_contract/src/main.nr | 6 +- .../contracts/card_game_contract/src/cards.nr | 4 +- .../contracts/child_contract/src/main.nr | 4 +- .../crowdfunding_contract/src/main.nr | 4 +- .../delegated_on_contract/src/main.nr | 4 +- .../docs_example_contract/src/main.nr | 14 ++--- .../ecdsa_account_contract/src/main.nr | 4 +- .../contracts/escrow_contract/src/main.nr | 4 +- .../inclusion_proofs_contract/src/main.nr | 4 +- .../pending_note_hashes_contract/src/main.nr | 20 +++---- .../schnorr_account_contract/src/main.nr | 4 +- .../static_child_contract/src/main.nr | 6 +- .../contracts/test_contract/src/main.nr | 36 ++++++------ .../contracts/test_log_contract/src/main.nr | 46 +++++---------- .../token_blacklist_contract/src/main.nr | 12 ++-- .../contracts/token_contract/src/main.nr | 18 +++--- .../aztec_macros/src/transforms/events.rs | 27 +++++++++ .../end-to-end/src/e2e_event_logs.test.ts | 24 -------- 30 files changed, 264 insertions(+), 177 deletions(-) create mode 100644 noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr diff --git a/boxes/boxes/react/src/contracts/src/main.nr b/boxes/boxes/react/src/contracts/src/main.nr index 944c66a75e9..6cbe8afbba8 100644 --- a/boxes/boxes/react/src/contracts/src/main.nr +++ b/boxes/boxes/react/src/contracts/src/main.nr @@ -1,7 +1,7 @@ contract BoxReact { use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader}; use dep::aztec::protocol_types::grumpkin_point::GrumpkinPoint; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_with_keys; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys; use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN}; #[aztec(storage)] @@ -20,7 +20,7 @@ contract BoxReact { ) { let numbers = storage.numbers; let mut new_number = ValueNote::new(number, owner_npk_m_hash); - numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); + numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); } #[aztec(private)] @@ -33,7 +33,7 @@ contract BoxReact { ) { let numbers = storage.numbers; let mut new_number = ValueNote::new(number, owner_npk_m_hash); - numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); + numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); } unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote { diff --git a/boxes/boxes/vanilla/src/contracts/src/main.nr b/boxes/boxes/vanilla/src/contracts/src/main.nr index 424471e8bfd..c8090a00f88 100644 --- a/boxes/boxes/vanilla/src/contracts/src/main.nr +++ b/boxes/boxes/vanilla/src/contracts/src/main.nr @@ -1,7 +1,7 @@ contract Vanilla { use dep::aztec::prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader}; use dep::aztec::protocol_types::grumpkin_point::GrumpkinPoint; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_with_keys; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys; use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN}; #[aztec(storage)] @@ -20,7 +20,7 @@ contract Vanilla { ) { let numbers = storage.numbers; let mut new_number = ValueNote::new(number, owner_npk_m_hash); - numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); + numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); } #[aztec(private)] @@ -33,7 +33,7 @@ contract Vanilla { ) { let numbers = storage.numbers; let mut new_number = ValueNote::new(number, owner_npk_m_hash); - numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); + numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m)); } unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote { diff --git a/noir-projects/aztec-nr/aztec/src/context/private_context.nr b/noir-projects/aztec-nr/aztec/src/context/private_context.nr index 71d69f484a6..25bb33ba663 100644 --- a/noir-projects/aztec-nr/aztec/src/context/private_context.nr +++ b/noir-projects/aztec-nr/aztec/src/context/private_context.nr @@ -1,5 +1,3 @@ -use crate::encrypted_logs::{payload::compute_encrypted_note_log}; - use crate::{ context::{inputs::PrivateContextInputs, packed_returns::PackedReturns}, messaging::process_l1_to_l2_message, @@ -10,7 +8,7 @@ use crate::{ key_validation_request::get_key_validation_request, arguments, returns::pack_returns, call_private_function::call_private_function_internal, header::get_header_at, logs::{ - emit_encrypted_note_log, emit_encrypted_event_log, compute_encrypted_event_log, + emit_encrypted_note_log, emit_encrypted_event_log, emit_contract_class_unencrypted_log_private_internal, emit_unencrypted_log_private_internal }, logs_traits::{LensForEncryptedLog, ToBytesForUnencryptedLog}, @@ -276,10 +274,7 @@ impl PrivateContext { // --> might be a better approach to force devs to make a public function call that emits the log if needed then // it would be less easy to accidentally leak information. // If we decide to keep this function around would make sense to wait for traits and then merge it with emit_unencrypted_log. - pub fn emit_unencrypted_log( - &mut self, - log: T - ) where T: ToBytesForUnencryptedLog { + pub fn emit_unencrypted_log(&mut self, log: T) where T: ToBytesForUnencryptedLog { let event_selector = 5; // TODO: compute actual event selector. let contract_address = self.this_address(); let counter = self.next_counter(); @@ -313,36 +308,7 @@ impl PrivateContext { // NB: A randomness value of 0 signals that the kernels should not mask the contract address // used in siloing later on e.g. 'handshaking' contract w/ known address. - pub fn encrypt_and_emit_event( - &mut self, - randomness: Field, // Secret random value used later for masked_contract_address - event_type_id: Field, - ovpk_m: GrumpkinPoint, - ivpk_m: GrumpkinPoint, - preimage: [Field; N] - ) where [Field; N]: LensForEncryptedLog { - let ovsk_app = self.request_ovsk_app(ovpk_m.hash()); - let contract_address = self.this_address(); - - // We are currently just encrypting it unconstrained, but otherwise the same way as if it was a note. - let encrypted_log: [u8; M] = compute_encrypted_event_log( - contract_address, - randomness, - event_type_id, - ovsk_app, - ovpk_m, - ivpk_m, - preimage - ); - - self.emit_raw_event_log_with_masked_address(randomness, encrypted_log); - } - - pub fn emit_raw_event_log_with_masked_address( - &mut self, - randomness: Field, - encrypted_log: [u8; M] - ) { + pub fn emit_raw_event_log_with_masked_address(&mut self, randomness: Field, encrypted_log: [u8; M]) { let counter = self.next_counter(); let contract_address = self.this_address(); let len = encrypted_log.len() as Field + 4; diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr index 2f1b93d9aad..dfc49fc0a86 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs.nr @@ -3,3 +3,4 @@ mod incoming_body; mod outgoing_body; mod payload; mod encrypted_note_emission; +mod encrypted_event_emission; diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr new file mode 100644 index 00000000000..a027c168515 --- /dev/null +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_event_emission.nr @@ -0,0 +1,45 @@ +use crate::{ + context::PrivateContext, event::event_interface::EventInterface, + encrypted_logs::payload::compute_encrypted_event_log, oracle::logs_traits::LensForEncryptedEvent +}; +use dep::protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint}; + +fn emit_with_keys( + context: &mut PrivateContext, + randomness: Field, + event: Event, + ovpk: GrumpkinPoint, + ivpk: GrumpkinPoint +) where Event: EventInterface, [u8; NB]: LensForEncryptedEvent { + let contract_address: AztecAddress = context.this_address(); + let ovsk_app: Field = context.request_ovsk_app(ovpk.hash()); + + let encrypted_log: [u8; OB] = compute_encrypted_event_log(contract_address, randomness, ovsk_app, ovpk, ivpk, event); + + context.emit_raw_event_log_with_masked_address(randomness, encrypted_log); +} + +pub fn encode_and_encrypt_event( + context: &mut PrivateContext, + randomness: Field, + ov: AztecAddress, + iv: AztecAddress +) -> fn[(&mut PrivateContext, AztecAddress, AztecAddress, Field)](Event) -> () where Event: EventInterface, [u8; NB]: LensForEncryptedEvent { + | e: Event | { + 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, randomness, e, ovpk, ivpk); + } +} + +pub fn encode_and_encrypt_event_with_keys( + context: &mut PrivateContext, + randomness: Field, + ovpk: GrumpkinPoint, + ivpk: GrumpkinPoint +) -> fn[(&mut PrivateContext, Field, GrumpkinPoint, GrumpkinPoint)](Event) -> () where Event: EventInterface, [u8; NB]: LensForEncryptedEvent { + | e: Event | { + emit_with_keys(context, randomness, e, ovpk, ivpk); + } +} diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr index 12994a4b4fa..5b66e2e8027 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/encrypted_note_emission.nr @@ -33,7 +33,7 @@ fn emit_with_keys( context.emit_raw_note_log(note_hash_counter, encrypted_log); } -pub fn encode_and_encrypt( +pub fn encode_and_encrypt_note( context: &mut PrivateContext, ov: AztecAddress, iv: AztecAddress @@ -46,7 +46,7 @@ pub fn encode_and_encrypt( } } -pub fn encode_and_encrypt_with_keys( +pub fn encode_and_encrypt_note_with_keys( context: &mut PrivateContext, ovpk: GrumpkinPoint, ivpk: GrumpkinPoint diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr index ccb1abdb764..e95771baa30 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/incoming_body.nr @@ -205,6 +205,10 @@ mod test { buffer } + + fn emit(self, _emit: fn[Env](Self) -> ()) { + _emit(self); + } } #[test] diff --git a/noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr b/noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr index 273142bca9d..ae19787a2d7 100644 --- a/noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr +++ b/noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr @@ -7,6 +7,7 @@ use dep::std::{embedded_curve_ops::{embedded_curve_add, EmbeddedCurvePoint}, fie use crate::oracle::unsafe_rand::unsafe_rand; +use crate::event::event_interface::EventInterface; use crate::note::note_interface::NoteInterface; use crate::encrypted_logs::{ @@ -14,6 +15,63 @@ use crate::encrypted_logs::{ outgoing_body::EncryptedLogOutgoingBody }; +pub fn compute_encrypted_event_log( + contract_address: AztecAddress, + randomness: Field, + ovsk_app: Field, + ovpk: GrumpkinPoint, + ivpk: GrumpkinPoint, + event: Event +) -> [u8; OB] where Event: EventInterface { + // @todo Need to draw randomness from the full domain of Fq not only Fr + let eph_sk: GrumpkinPrivateKey = fr_to_private_key(unsafe_rand()); + let eph_pk = eph_sk.derive_public_key(); + + // @todo This value needs to be populated! + let recipient = AztecAddress::from_field(0); + + let ivpk_app = compute_ivpk_app(ivpk, contract_address); + + let header = EncryptedLogHeader::new(contract_address); + + let incoming_header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ivpk); + let outgoing_Header_ciphertext: [u8; 48] = header.compute_ciphertext(eph_sk, ovpk); + let incoming_body_ciphertext = EncryptedLogIncomingBody::from_event(event, randomness).compute_ciphertext(eph_sk, ivpk_app); + let outgoing_body_ciphertext: [u8; 176] = EncryptedLogOutgoingBody::new(eph_sk, recipient, ivpk_app).compute_ciphertext(fr_to_private_key(ovsk_app), eph_pk); + + let mut encrypted_bytes: [u8; OB] = [0; OB]; + // @todo We ignore the tags for now + + let eph_pk_bytes = eph_pk.to_be_bytes(); + for i in 0..64 { + encrypted_bytes[64 + i] = eph_pk_bytes[i]; + } + for i in 0..48 { + encrypted_bytes[128 + i] = incoming_header_ciphertext[i]; + encrypted_bytes[176 + i] = outgoing_Header_ciphertext[i]; + } + for i in 0..176 { + encrypted_bytes[224 + i] = outgoing_body_ciphertext[i]; + } + // Then we fill in the rest as the incoming body ciphertext + let size = OB - 400; + assert_eq(size, incoming_body_ciphertext.len(), "ciphertext length mismatch"); + for i in 0..size { + encrypted_bytes[400 + i] = incoming_body_ciphertext[i]; + } + + // Current unoptimized size of the encrypted log + // incoming_tag (32 bytes) + // outgoing_tag (32 bytes) + // eph_pk (64 bytes) + // incoming_header (48 bytes) + // outgoing_header (48 bytes) + // outgoing_body (176 bytes) + // incoming_body_fixed (64 bytes) + // incoming_body_variable (N * 32 bytes + 16 bytes padding) + encrypted_bytes +} + pub fn compute_encrypted_note_log( contract_address: AztecAddress, storage_slot: Field, diff --git a/noir-projects/aztec-nr/aztec/src/event/event_interface.nr b/noir-projects/aztec-nr/aztec/src/event/event_interface.nr index 971aa460fd6..4505dedd1ab 100644 --- a/noir-projects/aztec-nr/aztec/src/event/event_interface.nr +++ b/noir-projects/aztec-nr/aztec/src/event/event_interface.nr @@ -6,4 +6,5 @@ trait EventInterface { fn private_to_be_bytes(self, randomness: Field) -> [u8; NB]; fn to_be_bytes(self) -> [u8; MB]; fn get_event_type_id() -> EventSelector; + fn emit(self, _emit: fn[Env](Self) -> ()); } diff --git a/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr b/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr index c6632f5a4d3..899f7f0d0d1 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/logs_traits.nr @@ -31,7 +31,6 @@ impl LensForEncryptedLog<3, 576> for [Field; 3] { impl LensForEncryptedLog<4, 608> for [Field; 4] { fn output_fields(self) -> [Field; 4] {[self[0]; 4]} fn output_bytes(self) -> [u8; 608] {[self[0] as u8; 608]} - } impl LensForEncryptedLog<5, 640> for [Field; 5] { fn output_fields(self) -> [Field; 5] {[self[0]; 5]} @@ -40,7 +39,31 @@ impl LensForEncryptedLog<5, 640> for [Field; 5] { impl LensForEncryptedLog<6, 672> for [Field; 6] { fn output_fields(self) -> [Field; 6] {[self[0]; 6]} fn output_bytes(self) -> [u8; 672] {[self[0] as u8; 672]} +} +trait LensForEncryptedEvent { + // N = event preimage input in bytes + // M = encryption output len in bytes (= 480 + M) + fn output(self: [u8; N]) -> [u8; M]; +} + +impl LensForEncryptedEvent<96, 512> for [u8; 96] { + fn output(self) -> [u8; 512] {[self[0] as u8; 512]} +} +impl LensForEncryptedEvent<128, 544> for [u8; 128] { + fn output(self) -> [u8; 544] {[self[0] as u8; 544]} +} +impl LensForEncryptedEvent<160, 576> for [u8; 160] { + fn output(self) -> [u8; 576] {[self[0] as u8; 576]} +} +impl LensForEncryptedEvent<192, 608> for [u8; 192] { + fn output(self) -> [u8; 608] {[self[0] as u8; 608]} +} +impl LensForEncryptedEvent<224, 640> for [u8; 224] { + fn output(self) -> [u8; 640] {[self[0] as u8; 640]} +} +impl LensForEncryptedEvent<256, 672> for [u8; 256] { + fn output(self) -> [u8; 672] {[self[0] as u8; 672]} } // This trait defines the length of the inputs in bytes to diff --git a/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr b/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr index ff23dd0e159..1a84b86acba 100644 --- a/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr +++ b/noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr @@ -1,7 +1,7 @@ use dep::aztec::{ context::PrivateContext, protocol_types::{address::AztecAddress, grumpkin_point::GrumpkinPoint}, note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet, - encrypted_logs::encrypted_note_emission::encode_and_encrypt + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note }; use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote}; @@ -30,7 +30,7 @@ impl EasyPrivateUint<&mut PrivateContext> { // Insert the new note to the owner's set of notes. // docs:start:insert - self.set.insert(&mut addend_note).emit(encode_and_encrypt(self.context, outgoing_viewer, owner)); + self.set.insert(&mut addend_note).emit(encode_and_encrypt_note(self.context, outgoing_viewer, owner)); // docs:end:insert } @@ -63,6 +63,6 @@ impl 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).emit(encode_and_encrypt(self.context, outgoing_viewer, owner)); + self.set.insert(&mut result_note).emit(encode_and_encrypt_note(self.context, outgoing_viewer, owner)); } } diff --git a/noir-projects/aztec-nr/value-note/src/utils.nr b/noir-projects/aztec-nr/value-note/src/utils.nr index 927f8325281..44094999c76 100644 --- a/noir-projects/aztec-nr/value-note/src/utils.nr +++ b/noir-projects/aztec-nr/value-note/src/utils.nr @@ -1,7 +1,7 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, PrivateSet, NoteGetterOptions}; use dep::aztec::note::note_getter_options::SortOrder; use dep::aztec::protocol_types::grumpkin_point::GrumpkinPoint; -use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; +use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use crate::{filter::filter_notes_min_sum, value_note::{ValueNote, VALUE_NOTE_LEN, VALUE_NOTE_BYTES_LEN}}; // Sort the note values (0th field) in descending order. @@ -23,7 +23,7 @@ pub fn increment( let mut note = ValueNote::new(amount, recipient_npk_m_hash); // Insert the new note to the owner's set of notes and emit the log if value is non-zero. - balance.insert(&mut note).emit(encode_and_encrypt(balance.context, outgoing_viewer, recipient)); + balance.insert(&mut note).emit(encode_and_encrypt_note(balance.context, outgoing_viewer, recipient)); } // Find some of the `owner`'s notes whose values add up to the `amount`. diff --git a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr index df46548453e..d9c5e6e6b1d 100644 --- a/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr @@ -9,7 +9,7 @@ contract AppSubscription { AztecAddress, FunctionSelector, PrivateContext, NoteHeader, Map, PrivateMutable, PublicMutable, SharedImmutable }, - encrypted_logs::encrypted_note_emission::encode_and_encrypt, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, protocol_types::{traits::is_empty, grumpkin_point::GrumpkinPoint} }, authwit::{auth_witness::get_auth_witness, auth::assert_current_call_valid_authwit}, @@ -45,7 +45,7 @@ contract AppSubscription { // We are emitting both the outgoing and the incoming logs to the subscriber here because passing a separate // outgoing_viewer arg to entrypoint function is impractical and the outgoing are not so valuable here. - storage.subscriptions.at(user_address).replace(&mut note).emit(encode_and_encrypt(&mut context, user_address, user_address)); + storage.subscriptions.at(user_address).replace(&mut note).emit(encode_and_encrypt_note(&mut context, user_address, user_address)); context.set_as_fee_payer(); @@ -116,7 +116,7 @@ contract AppSubscription { let subscriber_npk_m_hash = header.get_npk_m_hash(&mut context, subscriber_address); let mut subscription_note = SubscriptionNote::new(subscriber_npk_m_hash, expiry_block_number, tx_count); - storage.subscriptions.at(subscriber_address).initialize_or_replace(&mut subscription_note).emit(encode_and_encrypt(&mut context, context.msg_sender(), subscriber_address)); + storage.subscriptions.at(subscriber_address).initialize_or_replace(&mut subscription_note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), subscriber_address)); } unconstrained fn is_initialized(subscriber_address: AztecAddress) -> pub bool { diff --git a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr index e8ae03dd953..7065267bc2e 100644 --- a/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr +++ b/noir-projects/noir-contracts/contracts/card_game_contract/src/cards.nr @@ -6,7 +6,7 @@ use dep::aztec::{ traits::{ToField, Serialize, FromField}, grumpkin_point::GrumpkinPoint, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL }, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, note::note_getter::view_notes, state_vars::PrivateSet, note::constants::MAX_NOTES_PER_PAGE }; use dep::std; @@ -114,7 +114,7 @@ impl Deck<&mut PrivateContext> { let mut inserted_cards = &[]; for card in cards { let mut card_note = CardNote::from_card(card, owner_npk_m_hash); - self.set.insert(&mut card_note.note).emit(encode_and_encrypt_with_keys(self.set.context, msg_sender_ovpk_m, owner_ivpk_m)); + self.set.insert(&mut card_note.note).emit(encode_and_encrypt_note_with_keys(self.set.context, msg_sender_ovpk_m, owner_ivpk_m)); inserted_cards = inserted_cards.push_back(card_note); } diff --git a/noir-projects/noir-contracts/contracts/child_contract/src/main.nr b/noir-projects/noir-contracts/contracts/child_contract/src/main.nr index ab9483cce2a..bd7220461a1 100644 --- a/noir-projects/noir-contracts/contracts/child_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/child_contract/src/main.nr @@ -6,7 +6,7 @@ contract Child { context::gas::GasOpts, protocol_types::{abis::call_context::CallContext, grumpkin_point::GrumpkinPoint}, note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note }; use dep::value_note::value_note::ValueNote; @@ -56,7 +56,7 @@ contract Child { let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt(&mut context, owner, owner)); + storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, owner, owner)); new_value } diff --git a/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr b/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr index 7ced056ff21..9e43661a329 100644 --- a/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr @@ -8,7 +8,7 @@ contract Crowdfunding { abis::function_selector::FunctionSelector, address::AztecAddress, traits::Serialize, grumpkin_point::GrumpkinPoint }, - encrypted_logs::encrypted_note_emission::encode_and_encrypt, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, state_vars::{PrivateSet, PublicImmutable, SharedImmutable} }; use dep::value_note::value_note::ValueNote; @@ -81,7 +81,7 @@ contract Crowdfunding { // contract by proving that the hash of this note exists in the note hash tree. let donor_npk_m_hash = header.get_npk_m_hash(&mut context, donor); let mut note = ValueNote::new(amount as Field, donor_npk_m_hash); - storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt(&mut context, donor, donor)); + storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(&mut context, donor, donor)); } // docs:end:donate diff --git a/noir-projects/noir-contracts/contracts/delegated_on_contract/src/main.nr b/noir-projects/noir-contracts/contracts/delegated_on_contract/src/main.nr index da7a24ce09b..7a848446055 100644 --- a/noir-projects/noir-contracts/contracts/delegated_on_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/delegated_on_contract/src/main.nr @@ -4,7 +4,7 @@ contract DelegatedOn { AztecAddress, FunctionSelector, NoteHeader, NoteGetterOptions, NoteViewerOptions, PublicMutable, PrivateSet, PrivateContext, Map }; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::aztec::{protocol_types::grumpkin_point::GrumpkinPoint}; use dep::value_note::value_note::ValueNote; @@ -20,7 +20,7 @@ contract DelegatedOn { let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), owner)); + storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); new_value } diff --git a/noir-projects/noir-contracts/contracts/docs_example_contract/src/main.nr b/noir-projects/noir-contracts/contracts/docs_example_contract/src/main.nr index 493bead7cab..22242e9404a 100644 --- a/noir-projects/noir-contracts/contracts/docs_example_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/docs_example_contract/src/main.nr @@ -18,7 +18,7 @@ contract DocsExample { PrivateContext, Map, PublicMutable, PublicImmutable, PrivateMutable, PrivateImmutable, PrivateSet, SharedImmutable, Deserialize }; - use dep::aztec::encrypted_logs::encrypted_note_emission::{encode_and_encrypt, encode_and_encrypt_with_keys}; + use dep::aztec::encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys}; use dep::aztec::note::note_getter_options::Comparator; use dep::aztec::protocol_types::grumpkin_point::GrumpkinPoint; // how to import methods from other files/folders within your workspace @@ -173,7 +173,7 @@ contract DocsExample { let msg_sender_npk_m_hash = header.get_npk_m_hash(&mut context, context.msg_sender()); let mut new_card = CardNote::new(points, randomness, msg_sender_npk_m_hash); - storage.private_immutable.initialize(&mut new_card).emit(encode_and_encrypt(&mut context, context.msg_sender(), context.msg_sender())); + storage.private_immutable.initialize(&mut new_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); } // docs:end:initialize-private-mutable @@ -185,7 +185,7 @@ contract DocsExample { let mut legendary_card = CardNote::new(points, randomness, msg_sender_npk_m_hash); // create and broadcast note - storage.legendary_card.initialize(&mut legendary_card).emit(encode_and_encrypt(&mut context, context.msg_sender(), context.msg_sender())); + storage.legendary_card.initialize(&mut legendary_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); } #[aztec(private)] @@ -197,7 +197,7 @@ contract DocsExample { for i in 0..amounts.len() { let mut note = CardNote::new(amounts[i], 1, msg_sender_npk_m_hash); - storage.set.insert(&mut note).emit(encode_and_encrypt_with_keys(&mut context, msg_sender_ovpk_m, msg_sender_ivpk_m)); + storage.set.insert(&mut note).emit(encode_and_encrypt_note_with_keys(&mut context, msg_sender_ovpk_m, msg_sender_ivpk_m)); } } @@ -207,7 +207,7 @@ contract DocsExample { let msg_sender_npk_m_hash = header.get_npk_m_hash(&mut context, context.msg_sender()); let mut note = CardNote::new(amount, randomness, msg_sender_npk_m_hash); - storage.set.insert(&mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), context.msg_sender())); + storage.set.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); } // docs:start:state_vars-NoteGetterOptionsComparatorExampleNoir @@ -229,7 +229,7 @@ contract DocsExample { let msg_sender_npk_m_hash = header.get_npk_m_hash(&mut context, context.msg_sender()); let mut new_card = CardNote::new(points, randomness, msg_sender_npk_m_hash); - storage.legendary_card.replace(&mut new_card).emit(encode_and_encrypt(&mut context, context.msg_sender(), context.msg_sender())); + storage.legendary_card.replace(&mut new_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); DocsExample::at(context.this_address()).update_leader(context.msg_sender(), points).enqueue(&mut context); } @@ -250,7 +250,7 @@ contract DocsExample { let mut new_card = CardNote::new(points, card.randomness, msg_sender_npk_m_hash); // docs:start:state_vars-PrivateMutableReplace - storage.legendary_card.replace(&mut new_card).emit(encode_and_encrypt(&mut context, context.msg_sender(), context.msg_sender())); + storage.legendary_card.replace(&mut new_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); // docs:end:state_vars-PrivateMutableReplace DocsExample::at(context.this_address()).update_leader(context.msg_sender(), points).enqueue(&mut context); diff --git a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/main.nr b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/main.nr index 160776cc861..f4495e48217 100644 --- a/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/main.nr @@ -4,7 +4,7 @@ mod ecdsa_public_key_note; // The signing key is stored in an immutable private note and should be different from the signing key. contract EcdsaAccount { use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, NoteGetterOptions, PrivateContext, PrivateImmutable}; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::aztec::protocol_types::abis::call_context::CallContext; use dep::std; @@ -33,7 +33,7 @@ contract EcdsaAccount { // important. let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_npk_m_hash); - storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt(&mut context, this, this)); + storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this, this)); } // Note: If you globally change the entrypoint signature don't forget to update default_entrypoint.ts diff --git a/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr b/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr index 661283b1f85..27159437c1b 100644 --- a/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr @@ -1,7 +1,7 @@ // Sample escrow contract that stores a balance of a private token on behalf of an owner. contract Escrow { use dep::aztec::prelude::{AztecAddress, EthAddress, FunctionSelector, NoteHeader, PrivateContext, PrivateImmutable}; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::address_note::address_note::AddressNote; @@ -20,7 +20,7 @@ contract Escrow { let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = AddressNote::new(owner, owner_npk_m_hash); - storage.owner.initialize(&mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), owner)); + storage.owner.initialize(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); } // Withdraws balance. Requires that msg.sender is the owner. diff --git a/noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr b/noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr index 372b11fbd5b..71cc8d803f7 100644 --- a/noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/inclusion_proofs_contract/src/main.nr @@ -4,7 +4,7 @@ contract InclusionProofs { AztecAddress, EthAddress, FunctionSelector, NoteHeader, NoteGetterOptions, PrivateContext, Map, PrivateSet, PublicMutable }; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::aztec::protocol_types::{grumpkin_point::GrumpkinPoint, contract_class_id::ContractClassId, header::Header}; use dep::aztec::{note::note_getter_options::NoteStatus}; @@ -36,7 +36,7 @@ contract InclusionProofs { let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(value, owner_npk_m_hash); - owner_private_values.insert(&mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), owner)); + owner_private_values.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); } // docs:end:create_note diff --git a/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr b/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr index 6f5076d4ddf..c35c36d6eb5 100644 --- a/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr @@ -8,7 +8,7 @@ contract PendingNoteHashes { use dep::value_note::{balance_utils, filter::filter_notes_min_sum, value_note::{VALUE_NOTE_LEN, ValueNote}}; 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::{encode_and_encrypt, encode_and_encrypt_with_keys}; + use dep::aztec::encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys}; use dep::aztec::note::note_emission::NoteEmission; #[aztec(storage)] @@ -36,7 +36,7 @@ contract PendingNoteHashes { let mut note = ValueNote::new(amount, owner_npk_m_hash); // Insert note - owner_balance.insert(&mut note).emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount); // get note inserted above @@ -71,7 +71,7 @@ contract PendingNoteHashes { // Insert note let mut note = ValueNote::new(amount, owner_npk_m_hash); - owner_balance.insert(&mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), owner)); + owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); 0 } @@ -91,7 +91,7 @@ contract PendingNoteHashes { let mut note = ValueNote::new(amount, owner_npk_m_hash); // Insert note - owner_balance.insert(&mut note).emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); } // Nested/inner function to create and insert a note @@ -112,7 +112,7 @@ contract PendingNoteHashes { note.randomness = 2; // Insert note - owner_balance.insert(&mut note).emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); } // Nested/inner function to create and insert a note @@ -129,10 +129,10 @@ contract PendingNoteHashes { // Insert note let emission = owner_balance.insert(&mut note); - emission.emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + emission.emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); // Emit note again - emission.emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + emission.emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); } // Nested/inner function to get a note and confirm it matches the expected value @@ -351,7 +351,7 @@ contract PendingNoteHashes { let owner_ivpk_m = header.get_ivpk_m(&mut context, owner); let mut good_note = ValueNote::new(10, owner_npk_m_hash); // Insert good note with real log - owner_balance.insert(&mut good_note).emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut good_note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); // We will emit a note log with an incorrect preimage to ensure the pxe throws // This note has not been inserted... @@ -360,7 +360,7 @@ contract PendingNoteHashes { let existing_note_header = good_note.get_header(); bad_note.set_header(existing_note_header); - NoteEmission::new(bad_note).emit(encode_and_encrypt_with_keys(&mut context, outgoing_viewer_ovpk_m, owner_ivpk_m)); + NoteEmission::new(bad_note).emit(encode_and_encrypt_note_with_keys(&mut context, outgoing_viewer_ovpk_m, owner_ivpk_m)); } #[contract_library_method] @@ -378,7 +378,7 @@ contract PendingNoteHashes { for i in 0..max_notes_per_call() { let mut note = ValueNote::new(i as Field, owner_npk_m_hash); - owner_balance.insert(&mut note).emit(encode_and_encrypt_with_keys(context, outgoing_viewer_ovpk_m, owner_ivpk_m)); + owner_balance.insert(&mut note).emit(encode_and_encrypt_note_with_keys(context, outgoing_viewer_ovpk_m, owner_ivpk_m)); } } diff --git a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/main.nr b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/main.nr index f5661c26853..8632a64bc62 100644 --- a/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/schnorr_account_contract/src/main.nr @@ -6,7 +6,7 @@ contract SchnorrAccount { use dep::std; use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, PrivateContext, PrivateImmutable}; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::authwit::{ entrypoint::{app::AppPayload, fee::FeePayload}, account::AccountActions, auth_witness::get_auth_witness, auth::{compute_authwit_nullifier, compute_outer_authwit_hash} @@ -36,7 +36,7 @@ contract SchnorrAccount { // docs:start:initialize let mut pub_key_note = PublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_npk_m_hash); - storage.signing_public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt(&mut context, this, this)); + storage.signing_public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this, this)); // docs:end:initialize } diff --git a/noir-projects/noir-contracts/contracts/static_child_contract/src/main.nr b/noir-projects/noir-contracts/contracts/static_child_contract/src/main.nr index 9ed71ce290e..e0d113569fe 100644 --- a/noir-projects/noir-contracts/contracts/static_child_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/static_child_contract/src/main.nr @@ -5,7 +5,7 @@ contract StaticChild { use dep::aztec::{ context::{PublicContext, gas::GasOpts}, protocol_types::{abis::{call_context::CallContext}}, note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note }; use dep::value_note::value_note::ValueNote; @@ -43,7 +43,7 @@ contract StaticChild { let header = context.get_header(); let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_private_value.insert(&mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), owner)); + storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); new_value } @@ -57,7 +57,7 @@ contract StaticChild { let header = context.get_header(); let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_private_value.insert(&mut note).emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); new_value } diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr index 0ec38297670..df0428f9918 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -7,7 +7,8 @@ contract Test { AztecAddress, EthAddress, FunctionSelector, NoteHeader, NoteGetterOptions, NoteViewerOptions, PrivateContext, PrivateImmutable, PrivateSet, SharedImmutable }; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; + use dep::aztec::encrypted_logs::encrypted_event_emission::encode_and_encrypt_event_with_keys; use dep::aztec::protocol_types::{ abis::private_circuit_public_inputs::PrivateCircuitPublicInputs, @@ -41,7 +42,11 @@ contract Test { #[aztec(event)] struct ExampleEvent { - value: Field, + value0: Field, + value1: Field, + value2: Field, + value3: Field, + value4: Field, } #[aztec(storage)] @@ -97,7 +102,7 @@ contract Test { let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(value, owner_npk_m_hash); - create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt(&mut context, outgoing_viewer, owner)); + create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); } #[aztec(private)] @@ -262,23 +267,20 @@ contract Test { let header = context.get_header(); let outgoing_viewer_ovpk_m = header.get_ovpk_m(&mut context, outgoing_viewer); let owner_ivpk_m = header.get_ivpk_m(&mut context, owner); - context.encrypt_and_emit_event( - 5, // testing only - this should be a secret random value to salt the addr - 1, - outgoing_viewer_ovpk_m, - owner_ivpk_m, - fields - ); + + let event = ExampleEvent { value0: fields[0], value1: fields[1], value2: fields[2], value3: fields[3], value4: fields[4] }; + + event.emit(encode_and_encrypt_event_with_keys(&mut context, 5, outgoing_viewer_ovpk_m, owner_ivpk_m)); + // this contract has reached max number of functions, so using this one fn // to test nested and non nested encrypted logs if nest { Test::at(context.this_address()).emit_array_as_encrypted_log([0, 0, 0, 0, 0], owner, outgoing_viewer, false).call(&mut context); - context.encrypt_and_emit_event( - 0, // testing only - this signals to the kerels to not mask the address - 1, - outgoing_viewer_ovpk_m, - owner_ivpk_m, - [1, 2, 3, 4, 5] + + let otherEvent = ExampleEvent { value0: 1, value1: 2, value2: 3, value3: 4, value4: 5 }; + + otherEvent.emit( + encode_and_encrypt_event_with_keys(&mut context, 0, outgoing_viewer_ovpk_m, owner_ivpk_m) ); } } @@ -312,7 +314,7 @@ contract Test { let owner_npk_m_hash = header.get_npk_m_hash(&mut context, owner); let mut note = ValueNote::new(value + 1, owner_npk_m_hash); - create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt(&mut context, context.msg_sender(), owner)); + create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); storage_slot += 1; Test::at(context.this_address()).call_create_note(value + 2, owner, outgoing_viewer, storage_slot).call(&mut context); } diff --git a/noir-projects/noir-contracts/contracts/test_log_contract/src/main.nr b/noir-projects/noir-contracts/contracts/test_log_contract/src/main.nr index 9eb045f0f2f..f42cb2ffd7a 100644 --- a/noir-projects/noir-contracts/contracts/test_log_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_log_contract/src/main.nr @@ -4,6 +4,7 @@ contract TestLog { use dep::value_note::value_note::ValueNote; use dep::aztec::encrypted_logs::incoming_body::EncryptedLogIncomingBody; use dep::aztec::event::event_interface::EventInterface; + use dep::aztec::encrypted_logs::encrypted_event_emission::{encode_and_encrypt_event, encode_and_encrypt_event_with_keys}; #[aztec(event)] struct ExampleEvent0 { @@ -39,45 +40,28 @@ contract TestLog { ).compute_ciphertext(secret, point).as_array() } - #[aztec(private)] - fn emit_encrypted_log(randomness: Field, event_type_id: Field, preimage: [Field; 6]) { - let header = context.get_header(); - let msg_sender_ivpk_m = header.get_ivpk_m(&mut context, context.msg_sender()); - let msg_sender_ovpk_m = header.get_ovpk_m(&mut context, context.msg_sender()); - - context.encrypt_and_emit_event( - randomness, - event_type_id, - msg_sender_ovpk_m, - msg_sender_ivpk_m, - preimage - ); - } - #[aztec(private)] fn emit_encrypted_events(randomness: [Field; 2], preimages: [Field; 4]) { - let header = context.get_header(); - let msg_sender_ivpk_m = header.get_ivpk_m(&mut context, context.msg_sender()); - let msg_sender_ovpk_m = header.get_ovpk_m(&mut context, context.msg_sender()); - let event0 = ExampleEvent0 { value0: preimages[0], value1: preimages[1] }; - context.encrypt_and_emit_event( - randomness[0], - ExampleEvent0::get_event_type_id().to_field(), - msg_sender_ovpk_m, - msg_sender_ivpk_m, - event0.serialize() + event0.emit( + encode_and_encrypt_event( + &mut context, + randomness[0], + context.msg_sender(), + context.msg_sender() + ) ); let event1 = ExampleEvent1 { value2: preimages[2], value3: preimages[3] }; - context.encrypt_and_emit_event( - randomness[1], - ExampleEvent1::get_event_type_id().to_field(), - msg_sender_ovpk_m, - msg_sender_ivpk_m, - event1.serialize() + event1.emit( + encode_and_encrypt_event( + &mut context, + randomness[1], + context.msg_sender(), + context.msg_sender() + ) ); } } diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index defab65772e..114fa764515 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr @@ -13,7 +13,7 @@ contract TokenBlacklist { use dep::aztec::{ hash::compute_secret_hash, prelude::{AztecAddress, FunctionSelector, Map, NoteGetterOptions, PrivateSet, PublicMutable, SharedMutable}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note }; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; @@ -179,7 +179,7 @@ contract TokenBlacklist { // Add the token note to user's balances set let caller = context.msg_sender(); - storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, caller, to)); + storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, caller, to)); } #[aztec(private)] @@ -195,7 +195,7 @@ contract TokenBlacklist { assert(nonce == 0, "invalid nonce"); } - storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, from, from)); + storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, from)); TokenBlacklist::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); } @@ -215,8 +215,8 @@ contract TokenBlacklist { } let amount = U128::from_integer(amount); - storage.balances.sub(from, amount).emit(encode_and_encrypt(&mut context, from, from)); - storage.balances.add(to, amount).emit(encode_and_encrypt(&mut context, from, to)); + storage.balances.sub(from, amount).emit(encode_and_encrypt_note(&mut context, from, from)); + storage.balances.add(to, amount).emit(encode_and_encrypt_note(&mut context, from, to)); } #[aztec(private)] @@ -230,7 +230,7 @@ contract TokenBlacklist { assert(nonce == 0, "invalid nonce"); } - storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, from, from)); + storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, from)); TokenBlacklist::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context); } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr index 36390dda0be..32cb0359cdc 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -17,7 +17,7 @@ contract Token { use dep::aztec::{ hash::compute_secret_hash, prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}, - encrypted_logs::encrypted_note_emission::{encode_and_encrypt, encode_and_encrypt_with_keys} + encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys} }; // docs:start:import_authwit @@ -195,7 +195,7 @@ contract Token { #[aztec(private)] fn privately_mint_private_note(amount: Field) { let caller = context.msg_sender(); - storage.balances.add(caller, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, caller, caller)); + storage.balances.add(caller, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, caller, caller)); Token::at(context.this_address()).assert_minter_and_mint(context.msg_sender(), amount).enqueue(&mut context); } @@ -289,7 +289,7 @@ contract Token { // Note: Using context.msg_sender() as a sender below makes this incompatible with escrows because we send // outgoing logs to that address and to send outgoing logs you need to get a hold of ovsk_m. let from = context.msg_sender(); - storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, from, to)); + storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, to)); } // docs:end:redeem_shield @@ -302,7 +302,7 @@ contract Token { assert(nonce == 0, "invalid nonce"); } - storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, from, from)); + storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, from)); Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); } @@ -321,8 +321,8 @@ contract Token { let to_ivpk = header.get_ivpk_m(&mut context, to); let amount = U128::from_integer(amount); - storage.balances.sub(from, amount).emit(encode_and_encrypt_with_keys(&mut context, from_ovpk, from_ivpk)); - storage.balances.add(to, amount).emit(encode_and_encrypt_with_keys(&mut context, from_ovpk, to_ivpk)); + storage.balances.sub(from, amount).emit(encode_and_encrypt_note_with_keys(&mut context, from_ovpk, from_ivpk)); + storage.balances.add(to, amount).emit(encode_and_encrypt_note_with_keys(&mut context, from_ovpk, to_ivpk)); } // docs:end:transfer @@ -357,10 +357,10 @@ contract Token { let amount = U128::from_integer(amount); // docs:start:increase_private_balance // docs:start:encrypted - storage.balances.sub(from, amount).emit(encode_and_encrypt_with_keys(&mut context, from_ovpk, from_ivpk)); + storage.balances.sub(from, amount).emit(encode_and_encrypt_note_with_keys(&mut context, from_ovpk, from_ivpk)); // docs:end:encrypted // docs:end:increase_private_balance - storage.balances.add(to, amount).emit(encode_and_encrypt_with_keys(&mut context, from_ovpk, to_ivpk)); + storage.balances.add(to, amount).emit(encode_and_encrypt_note_with_keys(&mut context, from_ovpk, to_ivpk)); } // docs:start:burn @@ -372,7 +372,7 @@ contract Token { assert(nonce == 0, "invalid nonce"); } - storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt(&mut context, from, from)); + storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, from)); Token::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context); } diff --git a/noir/noir-repo/aztec_macros/src/transforms/events.rs b/noir/noir-repo/aztec_macros/src/transforms/events.rs index 748032f6300..05861b96eb4 100644 --- a/noir/noir-repo/aztec_macros/src/transforms/events.rs +++ b/noir/noir-repo/aztec_macros/src/transforms/events.rs @@ -67,6 +67,9 @@ pub fn generate_event_impls(module: &mut SortedModule) -> Result<(), AztecMacroE event_interface_trait_impl.items.push(TraitImplItem::Function( generate_fn_to_be_bytes(event_type.as_str(), event_byte_len)?, )); + event_interface_trait_impl + .items + .push(TraitImplItem::Function(generate_fn_emit(event_type.as_str())?)); submodule.contents.trait_impls.push(event_interface_trait_impl); let serialize_trait_impl = @@ -299,6 +302,30 @@ fn generate_fn_to_be_bytes( Ok(noir_fn) } +fn generate_fn_emit(event_type: &str) -> Result { + let function_source = format!( + " + fn emit(self: {event_type}, _emit: fn[Env](Self) -> ()) {{ + _emit(self); + }} + " + ) + .to_string(); + + let (function_ast, errors) = parse_program(&function_source); + if !errors.is_empty() { + dbg!(errors); + return Err(AztecMacroError::CouldNotImplementEventInterface { + secondary_message: Some(format!("Failed to parse Noir macro code (fn emit, implemented for EventInterface of {event_type}). This is either a bug in the compiler or the Noir macro code")), + }); + } + + let mut function_ast = function_ast.into_sorted(); + let mut noir_fn = function_ast.functions.remove(0); + noir_fn.def.visibility = ItemVisibility::Public; + Ok(noir_fn) +} + // We do this pass in the HIR to work around the "#[abi(tag)] attributes can only be used in contracts" error pub fn transform_event_abi( crate_id: &CrateId, diff --git a/yarn-project/end-to-end/src/e2e_event_logs.test.ts b/yarn-project/end-to-end/src/e2e_event_logs.test.ts index 3455b0166f6..b2fbe9d1dac 100644 --- a/yarn-project/end-to-end/src/e2e_event_logs.test.ts +++ b/yarn-project/end-to-end/src/e2e_event_logs.test.ts @@ -30,30 +30,6 @@ describe('Logs', () => { afterAll(() => teardown()); describe('functionality around emitting an encrypted log', () => { - it('emits a generic encrypted log and checks for correctness', async () => { - const randomness = Fr.random(); - const eventTypeId = Fr.random(); - const preimage = makeTuple(6, Fr.random); - - const tx = await testLogContract.methods.emit_encrypted_log(randomness, eventTypeId, preimage).send().wait(); - - const txEffect = await node.getTxEffect(tx.txHash); - - const encryptedLogs = txEffect!.encryptedLogs.unrollLogs(); - expect(encryptedLogs.length).toBe(1); - - const decryptedLog = TaggedLog.decryptAsIncoming( - encryptedLogs[0], - deriveMasterIncomingViewingSecretKey(wallets[0].getSecretKey()), - L1EventPayload, - ); - - expect(decryptedLog?.payload.contractAddress).toStrictEqual(testLogContract.address); - expect(decryptedLog?.payload.randomness).toStrictEqual(randomness); - expect(decryptedLog?.payload.eventTypeId).toStrictEqual(eventTypeId); - expect(decryptedLog?.payload.event.items).toStrictEqual(preimage); - }); - it('emits multiple events as encrypted logs and decodes them', async () => { const randomness = makeTuple(2, Fr.random); const preimage = makeTuple(4, Fr.random);