From 449406f44f73cf49a85c93adabf126a066b330a4 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 26 Sep 2024 09:42:27 +0000 Subject: [PATCH 1/3] refactor: nuking encode_and_encrypt_note(...) --- boxes/boxes/react/src/contracts/src/main.nr | 6 +- boxes/boxes/vanilla/src/contracts/src/main.nr | 6 +- docs/docs/migration_notes.md | 13 +++ .../encrypted_logs/encrypted_note_emission.nr | 39 +------- .../src/easy_private_uint.nr | 7 +- .../aztec-nr/value-note/src/utils.nr | 4 +- .../app_subscription_contract/src/main.nr | 7 +- .../contracts/card_game_contract/src/cards.nr | 8 +- .../contracts/child_contract/src/main.nr | 4 +- .../crowdfunding_contract/src/main.nr | 4 +- .../delegated_on_contract/src/main.nr | 7 +- .../docs_example_contract/src/main.nr | 78 +++++++++++----- .../ecdsa_k_account_contract/src/main.nr | 4 +- .../ecdsa_r_account_contract/src/main.nr | 4 +- .../contracts/escrow_contract/src/main.nr | 6 +- .../inclusion_proofs_contract/src/main.nr | 8 +- .../contracts/nft_contract/src/main.nr | 6 +- .../pending_note_hashes_contract/src/main.nr | 90 +++++++++++++++---- .../schnorr_account_contract/src/main.nr | 4 +- .../contracts/spam_contract/src/main.nr | 4 +- .../static_child_contract/src/main.nr | 24 +++-- .../contracts/test_contract/src/main.nr | 21 +++-- .../token_blacklist_contract/src/main.nr | 23 +++-- .../contracts/token_contract/src/main.nr | 24 +++-- 24 files changed, 244 insertions(+), 157 deletions(-) diff --git a/boxes/boxes/react/src/contracts/src/main.nr b/boxes/boxes/react/src/contracts/src/main.nr index 6d5d4ecc830..4c8e38cc476 100644 --- a/boxes/boxes/react/src/contracts/src/main.nr +++ b/boxes/boxes/react/src/contracts/src/main.nr @@ -5,7 +5,7 @@ contract BoxReact { use dep::aztec::{ keys::public_keys::{IvpkM, OvpkM}, prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, macros::{storage::storage, functions::{private, public, initializer}} }; use dep::value_note::value_note::ValueNote; @@ -26,7 +26,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_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); + numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); } #[private] @@ -39,7 +39,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_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); + numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); } 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 f336c4ad841..41c0ff7d3a3 100644 --- a/boxes/boxes/vanilla/src/contracts/src/main.nr +++ b/boxes/boxes/vanilla/src/contracts/src/main.nr @@ -5,7 +5,7 @@ contract Vanilla { use dep::aztec::{ keys::public_keys::{IvpkM, OvpkM}, prelude::{AztecAddress, PrivateMutable, Map, NoteInterface, NoteHeader, Point}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, macros::{storage::storage, functions::{private, public, initializer}} }; use dep::value_note::value_note::{ValueNote, VALUE_NOTE_LEN}; @@ -26,7 +26,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_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); + numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); } #[private] @@ -39,7 +39,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_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); + numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); } unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote { diff --git a/docs/docs/migration_notes.md b/docs/docs/migration_notes.md index 40b0cea19e5..9bc471aa1d5 100644 --- a/docs/docs/migration_notes.md +++ b/docs/docs/migration_notes.md @@ -33,6 +33,19 @@ All of `TestEnvironment`'s functions are now `unconstrained`, preventing acciden let env = TestEnvironment::new(); ``` +### [Aztec.nr] removed `encode_and_encrypt_note` and renamed `encode_and_encrypt_note_with_keys` to `encode_and_encrypt_note` + +````diff +contract XYZ { +- use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys; ++ use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; +.... + +- numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note_with_keys(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); ++ numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner_ivpk_m, owner)); + +} + ## 0.56.0 ### [Aztec.nr] Changes to contract definition 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 9b782e57ede..fd4f9e7f4ae 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 @@ -1,6 +1,6 @@ use crate::{ context::PrivateContext, note::{note_emission::NoteEmission, note_interface::NoteInterface}, - keys::{getters::{get_public_keys, get_ovsk_app}, public_keys::{OvpkM, IvpkM}}, + keys::{getters::get_ovsk_app, public_keys::{OvpkM, IvpkM}}, encrypted_logs::payload::compute_encrypted_log }; use dep::protocol_types::{hash::sha256_to_field, address::AztecAddress, abis::note_hash::NoteHash}; @@ -42,41 +42,6 @@ unconstrained fn compute_raw_note_log_unconstrained( } pub fn encode_and_encrypt_note( - context: &mut PrivateContext, - ov: AztecAddress, - iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission) -> () where Note: NoteInterface { - | e: NoteEmission | { - let ovpk = get_public_keys(ov).ovpk_m; - let ivpk = get_public_keys(iv).ivpk_m; - let ovsk_app: Field = context.request_ovsk_app(ovpk.hash()); - - let (note_hash_counter, encrypted_log, log_hash) = compute_raw_note_log(*context, e.note, ovsk_app, ovpk, ivpk, iv); - context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash); - } -} - -pub fn encode_and_encrypt_note_unconstrained( - context: &mut PrivateContext, - ov: AztecAddress, - iv: AztecAddress -) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission) -> () where Note: NoteInterface { - | e: NoteEmission | { - // 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. - let ovpk = get_public_keys(ov).ovpk_m; - let ivpk = get_public_keys(iv).ivpk_m; - - // See the comment in `encode_and_encrypt_note_with_keys_unconstrained` for why having note hash counter - // and log hash unconstrained here is fine. - let (note_hash_counter, encrypted_log, log_hash) = unsafe { - compute_raw_note_log_unconstrained(*context, e.note, ovpk, ivpk, iv) - }; - context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash); - } -} - -pub fn encode_and_encrypt_note_with_keys( context: &mut PrivateContext, ovpk: OvpkM, ivpk: IvpkM, @@ -90,7 +55,7 @@ pub fn encode_and_encrypt_note_with_keys( } } -pub fn encode_and_encrypt_note_with_keys_unconstrained( +pub fn encode_and_encrypt_note_unconstrained( context: &mut PrivateContext, ovpk: OvpkM, ivpk: IvpkM, 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 8863b8e9699..2bf133a7f25 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,8 +1,7 @@ use dep::aztec::{ context::PrivateContext, protocol_types::{address::AztecAddress}, note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, - keys::getters::get_public_keys + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys }; use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote}; @@ -32,7 +31,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_note_with_keys( + encode_and_encrypt_note( self.context, outgoing_viewer_keys.ovpk_m, owner_keys.ivpk_m, @@ -67,7 +66,7 @@ impl EasyPrivateUint<&mut PrivateContext> { let result_value = minuend - subtrahend; let mut result_note = ValueNote::new(result_value as Field, owner_keys.npk_m.hash()); self.set.insert(&mut result_note).emit( - encode_and_encrypt_note_with_keys( + encode_and_encrypt_note( self.context, outgoing_viewer_keys.ovpk_m, owner_keys.ivpk_m, diff --git a/noir-projects/aztec-nr/value-note/src/utils.nr b/noir-projects/aztec-nr/value-note/src/utils.nr index d6a65d16af8..3efc118588b 100644 --- a/noir-projects/aztec-nr/value-note/src/utils.nr +++ b/noir-projects/aztec-nr/value-note/src/utils.nr @@ -1,6 +1,6 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, PrivateSet, NoteGetterOptions}; use dep::aztec::note::note_getter_options::SortOrder; -use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys; +use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::aztec::keys::getters::get_public_keys; use crate::{filter::filter_notes_min_sum, value_note::{ValueNote, VALUE_NOTE_LEN}}; @@ -25,7 +25,7 @@ pub fn increment( let mut note = ValueNote::new(amount, recipient_keys.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_note_with_keys( + encode_and_encrypt_note( balance.context, outgoing_viewer_ovpk_m, recipient_keys.ivpk_m, 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 dc5db7ace32..dfd2f5480ab 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 @@ -10,7 +10,7 @@ contract AppSubscription { use aztec::{ prelude::{AztecAddress, Map, PrivateMutable, SharedImmutable}, keys::getters::get_public_keys, protocol_types::constants::MAX_FIELD_VALUE, utils::comparison::Comparator, - encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys}, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, macros::{storage::storage, functions::{public, initializer, private}} }; use authwit::auth::assert_current_call_valid_authwit; @@ -46,7 +46,8 @@ 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_note(&mut context, user_address, user_address)); + let keys = get_public_keys(user_address); + storage.subscriptions.at(user_address).replace(&mut note).emit(encode_and_encrypt_note(&mut context, keys.ovpk_m, keys.ivpk_m, user_address)); context.set_as_fee_payer(); @@ -102,7 +103,7 @@ contract AppSubscription { let mut subscription_note = SubscriptionNote::new(subscriber_keys.npk_m.hash(), expiry_block_number, tx_count); storage.subscriptions.at(subscriber).initialize_or_replace(&mut subscription_note).emit( - encode_and_encrypt_note_with_keys( + encode_and_encrypt_note( &mut context, msg_sender_ovpk_m, subscriber_keys.ivpk_m, 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 4d575eae01e..93fecf7c288 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 @@ -3,8 +3,8 @@ use dep::aztec::prelude::{AztecAddress, PrivateContext, NoteGetterOptions, NoteV use dep::aztec::{ context::UnconstrainedContext, protocol_types::{traits::{ToField, Serialize, FromField}, constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, - keys::getters::get_public_keys, state_vars::PrivateSet, note::constants::MAX_NOTES_PER_PAGE + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, + state_vars::PrivateSet, note::constants::MAX_NOTES_PER_PAGE }; use dep::value_note::value_note::ValueNote; @@ -112,9 +112,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_note_with_keys(self.set.context, msg_sender_ovpk_m, owner_ivpk_m, owner) - ); + self.set.insert(&mut card_note.note).emit(encode_and_encrypt_note(self.set.context, msg_sender_ovpk_m, owner_ivpk_m, owner)); 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 a6fb7ffbf11..04a90c9a08d 100644 --- a/noir-projects/noir-contracts/contracts/child_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/child_contract/src/main.nr @@ -7,7 +7,7 @@ contract Child { use dep::aztec::{ note::{note_getter_options::NoteGetterOptions}, - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, utils::comparison::Comparator, macros::{storage::storage, functions::{private, public, internal}} }; @@ -58,7 +58,7 @@ contract Child { let owner_keys = get_public_keys(owner); let mut note = ValueNote::new(new_value, owner_keys.npk_m.hash()); - storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note_with_keys(&mut context, owner_keys.ovpk_m, owner_keys.ivpk_m, owner)); + storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, owner_keys.ovpk_m, owner_keys.ivpk_m, 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 bd34f60917d..a6c6b183041 100644 --- a/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr @@ -7,7 +7,7 @@ contract Crowdfunding { // docs:start:all-deps use dep::aztec::{ - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, prelude::{AztecAddress, PrivateSet, SharedImmutable}, utils::comparison::Comparator, unencrypted_logs::unencrypted_event_emission::encode_event, macros::{storage::storage, events::event, functions::{public, initializer, private, internal}}, @@ -80,7 +80,7 @@ contract Crowdfunding { // docs:start:valuenote_new let mut note = ValueNote::new(amount as Field, donor_keys.npk_m.hash()); // docs:end:valuenote_new - storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note_with_keys(&mut context, donor_keys.ovpk_m, donor_keys.ivpk_m, donor)); + storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(&mut context, donor_keys.ovpk_m, donor_keys.ivpk_m, 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 2548052f7ec..30a4ab02771 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 @@ -19,10 +19,11 @@ contract DelegatedOn { #[private] fn private_set_value(new_value: Field, owner: AztecAddress) -> Field { - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); + let owner_keys = get_public_keys(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_note(&mut context, context.msg_sender(), owner)); + let mut note = ValueNote::new(new_value, owner_keys.npk_m.hash()); + storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, 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 b5a6465d2c3..c62ddf6663d 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 @@ -21,7 +21,7 @@ contract DocsExample { PrivateMutable, PrivateImmutable, PrivateSet, SharedImmutable }; use dep::aztec::{ - encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys}, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, macros::{storage::storage, functions::{public, private, internal, view}} }; @@ -174,35 +174,48 @@ contract DocsExample { // docs:start:initialize-private-mutable #[private] fn initialize_private_immutable(randomness: Field, points: u8) { - let msg_sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash(); + let msg_sender_keys = get_public_keys(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_note(&mut context, context.msg_sender(), context.msg_sender())); + let mut new_card = CardNote::new(points, randomness, msg_sender_keys.npk_m.hash()); + storage.private_immutable.initialize(&mut new_card).emit( + encode_and_encrypt_note( + &mut context, + msg_sender_keys.ovpk_m, + msg_sender_keys.ivpk_m, + context.msg_sender() + ) + ); } // docs:end:initialize-private-mutable #[private] // msg_sender() is 0 at deploy time. So created another function fn initialize_private(randomness: Field, points: u8) { - let msg_sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); - let mut legendary_card = CardNote::new(points, randomness, msg_sender_npk_m_hash); + let mut legendary_card = CardNote::new(points, randomness, msg_sender_keys.npk_m.hash()); // create and broadcast note - storage.legendary_card.initialize(&mut legendary_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); + storage.legendary_card.initialize(&mut legendary_card).emit( + encode_and_encrypt_note( + &mut context, + msg_sender_keys.ovpk_m, + msg_sender_keys.ivpk_m, + context.msg_sender() + ) + ); } #[private] fn insert_notes(amounts: [u8; 3]) { - let sender_keys = get_public_keys(context.msg_sender()); - let sender_npk_m_hash = sender_keys.npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); for i in 0..amounts.len() { - let mut note = CardNote::new(amounts[i], 1, sender_npk_m_hash); + let mut note = CardNote::new(amounts[i], 1, msg_sender_keys.npk_m.hash()); storage.set.insert(&mut note).emit( - encode_and_encrypt_note_with_keys( + encode_and_encrypt_note( &mut context, - sender_keys.ovpk_m, - sender_keys.ivpk_m, + msg_sender_keys.ovpk_m, + msg_sender_keys.ivpk_m, context.msg_sender() ) ); @@ -210,10 +223,17 @@ contract DocsExample { } #[private] fn insert_note(amount: u8, randomness: Field) { - let sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); - let mut note = CardNote::new(amount, randomness, sender_npk_m_hash); - storage.set.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); + let mut note = CardNote::new(amount, randomness, msg_sender_keys.npk_m.hash()); + storage.set.insert(&mut note).emit( + encode_and_encrypt_note( + &mut context, + msg_sender_keys.ovpk_m, + msg_sender_keys.ivpk_m, + context.msg_sender() + ) + ); } // docs:start:state_vars-NoteGetterOptionsComparatorExampleNoir unconstrained fn read_note(comparator: u8, amount: Field) -> BoundedVec { @@ -223,10 +243,17 @@ contract DocsExample { // docs:end:state_vars-NoteGetterOptionsComparatorExampleNoir #[private] fn update_legendary_card(randomness: Field, points: u8) { - let sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); - let mut new_card = CardNote::new(points, randomness, sender_npk_m_hash); - storage.legendary_card.replace(&mut new_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); + let mut new_card = CardNote::new(points, randomness, msg_sender_keys.npk_m.hash()); + storage.legendary_card.replace(&mut new_card).emit( + encode_and_encrypt_note( + &mut context, + msg_sender_keys.ovpk_m, + msg_sender_keys.ivpk_m, + context.msg_sender() + ) + ); DocsExample::at(context.this_address()).update_leader(context.msg_sender(), points).enqueue(&mut context); } #[private] @@ -234,15 +261,22 @@ contract DocsExample { // Ensure `points` > current value // Also serves as a e2e test that you can `get_note()` and then `replace()` - let sender_npk_m_hash = get_public_keys(context.msg_sender()).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); // docs:start:state_vars-PrivateMutableGet let card = storage.legendary_card.get_note().note; // docs:end:state_vars-PrivateMutableGet let points = card.points + 1; - let mut new_card = CardNote::new(points, card.randomness, sender_npk_m_hash); + let mut new_card = CardNote::new(points, card.randomness, msg_sender_keys.npk_m.hash()); // docs:start:state_vars-PrivateMutableReplace - storage.legendary_card.replace(&mut new_card).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), context.msg_sender())); + storage.legendary_card.replace(&mut new_card).emit( + encode_and_encrypt_note( + &mut context, + msg_sender_keys.ovpk_m, + msg_sender_keys.ivpk_m, + 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_k_account_contract/src/main.nr b/noir-projects/noir-contracts/contracts/ecdsa_k_account_contract/src/main.nr index 5bdce740498..3e3b9c0405d 100644 --- a/noir-projects/noir-contracts/contracts/ecdsa_k_account_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/ecdsa_k_account_contract/src/main.nr @@ -6,7 +6,7 @@ use dep::aztec::macros::aztec; contract EcdsaKAccount { use dep::aztec::prelude::{PrivateContext, PrivateImmutable}; use dep::aztec::{ - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, macros::{storage::storage, functions::{private, initializer, view, noinitcheck}} }; @@ -34,7 +34,7 @@ contract EcdsaKAccount { // important. let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_keys.npk_m.hash()); - storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note_with_keys(&mut context, this_keys.ovpk_m, this_keys.ivpk_m, this)); + storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this_keys.ovpk_m, this_keys.ivpk_m, this)); } // Note: If you globally change the entrypoint signature don't forget to update account_entrypoint.ts diff --git a/noir-projects/noir-contracts/contracts/ecdsa_r_account_contract/src/main.nr b/noir-projects/noir-contracts/contracts/ecdsa_r_account_contract/src/main.nr index 06335e30a91..be0c4d14ccb 100644 --- a/noir-projects/noir-contracts/contracts/ecdsa_r_account_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/ecdsa_r_account_contract/src/main.nr @@ -5,7 +5,7 @@ use dep::aztec::macros::aztec; contract EcdsaRAccount { use dep::aztec::prelude::{PrivateContext, PrivateImmutable}; use dep::aztec::{ - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, macros::{storage::storage, functions::{private, initializer, view, noinitcheck}} }; @@ -33,7 +33,7 @@ contract EcdsaRAccount { // important. let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_keys.npk_m.hash()); - storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note_with_keys(&mut context, this_keys.ovpk_m, this_keys.ivpk_m, this)); + storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this_keys.ovpk_m, this_keys.ivpk_m, this)); } // Note: If you globally change the entrypoint signature don't forget to update account_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 f121333cf2e..785f6dbcfc1 100644 --- a/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr @@ -5,7 +5,7 @@ use dep::aztec::macros::aztec; contract Escrow { use dep::aztec::prelude::{AztecAddress, PrivateImmutable}; use dep::aztec::{ - encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys, + encrypted_logs::encrypted_note_emission::encode_and_encrypt_note, keys::getters::get_public_keys, macros::{storage::storage, functions::{private, initializer}} }; @@ -28,9 +28,7 @@ contract Escrow { // docs:start:addressnote_new let mut note = AddressNote::new(owner, owner_keys.npk_m.hash()); // docs:end:addressnote_new - storage.owner.initialize(&mut note).emit( - encode_and_encrypt_note_with_keys(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, owner) - ); + storage.owner.initialize(&mut note).emit(encode_and_encrypt_note(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, 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 b67fdd30cfd..971162f2d7e 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 @@ -30,11 +30,11 @@ contract InclusionProofs { // Creates a value note owned by `owner`. #[private] fn create_note(owner: AztecAddress, value: Field) { - let owner_private_values = storage.private_values.at(owner); - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); + let owner_keys = get_public_keys(owner); - let mut note = ValueNote::new(value, owner_npk_m_hash); - owner_private_values.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); + let mut note = ValueNote::new(value, owner_keys.npk_m.hash()); + storage.private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, owner)); } // docs:end:create_note diff --git a/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr b/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr index 03cb0d0a8d3..cb72da92eec 100644 --- a/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/nft_contract/src/main.nr @@ -10,8 +10,8 @@ contract NFT { use dep::compressed_string::FieldCompressedString; use dep::aztec::{ prelude::{NoteGetterOptions, NoteViewerOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress}, - encrypted_logs::{encrypted_note_emission::encode_and_encrypt_note_with_keys}, - hash::pedersen_hash, keys::getters::get_public_keys, note::constants::MAX_NOTES_PER_PAGE, + encrypted_logs::{encrypted_note_emission::encode_and_encrypt_note}, hash::pedersen_hash, + keys::getters::get_public_keys, note::constants::MAX_NOTES_PER_PAGE, protocol_types::traits::is_empty, utils::comparison::Comparator, protocol_types::{point::Point, traits::Serialize}, macros::{storage::storage, events::event, functions::{private, public, view, internal, initializer}} @@ -246,7 +246,7 @@ contract NFT { let to_keys = get_public_keys(to); let mut new_note = NFTNote::new(token_id, to_keys.npk_m.hash()); - nfts.at(to).insert(&mut new_note).emit(encode_and_encrypt_note_with_keys(&mut context, from_ovpk_m, to_keys.ivpk_m, to)); + nfts.at(to).insert(&mut new_note).emit(encode_and_encrypt_note(&mut context, from_ovpk_m, to_keys.ivpk_m, to)); } #[private] 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 9663eebb80f..dc4ba79a8b7 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 @@ -10,7 +10,7 @@ contract PendingNoteHashes { use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteGetterOptions, PrivateContext, Map, PrivateSet}; use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote}; use dep::aztec::protocol_types::constants::{MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, MAX_NOTE_HASHES_PER_CALL}; - use dep::aztec::encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys}; + use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note; use dep::aztec::note::note_emission::NoteEmission; use dep::aztec::keys::getters::get_public_keys; use dep::aztec::macros::{storage::storage, functions::private}; @@ -34,12 +34,20 @@ contract PendingNoteHashes { ) -> Field { let owner_balance = storage.balances.at(owner); - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let owner_keys = get_public_keys(owner); + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); - let mut note = ValueNote::new(amount, owner_npk_m_hash); + let mut note = ValueNote::new(amount, owner_keys.npk_m.hash()); // Insert note - owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount); // get note inserted above @@ -81,12 +89,20 @@ contract PendingNoteHashes { fn insert_note(amount: Field, owner: AztecAddress, outgoing_viewer: AztecAddress) { let owner_balance = storage.balances.at(owner); - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let owner_keys = get_public_keys(owner); + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); - let mut note = ValueNote::new(amount, owner_npk_m_hash); + let mut note = ValueNote::new(amount, owner_keys.npk_m.hash()); // Insert note - owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); } // Nested/inner function to create and insert a note @@ -100,13 +116,21 @@ contract PendingNoteHashes { ) { let mut owner_balance = storage.balances.at(owner); - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let owner_keys = get_public_keys(owner); + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); - let mut note = ValueNote::new(amount, owner_npk_m_hash); + let mut note = ValueNote::new(amount, owner_keys.npk_m.hash()); note.randomness = 2; // Insert note - owner_balance.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); } // Nested/inner function to create and insert a note @@ -115,17 +139,32 @@ contract PendingNoteHashes { fn insert_note_extra_emit(amount: Field, owner: AztecAddress, outgoing_viewer: AztecAddress) { let mut owner_balance = storage.balances.at(owner); - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let owner_keys = get_public_keys(owner); + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); - let mut note = ValueNote::new(amount, owner_npk_m_hash); + let mut note = ValueNote::new(amount, owner_keys.npk_m.hash()); // Insert note let emission = owner_balance.insert(&mut note); - emission.emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + emission.emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); // Emit note again - emission.emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + emission.emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); } // Nested/inner function to get a note and confirm it matches the expected value @@ -335,12 +374,18 @@ contract PendingNoteHashes { let owner_keys = get_public_keys(owner); let owner_npk_m_hash = owner_keys.npk_m.hash(); - let owner_ivpk_m = owner_keys.ivpk_m; - let outgoing_viewer_ovpk_m = get_public_keys(outgoing_viewer).ovpk_m; + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); 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_note(&mut context, outgoing_viewer, owner)); + owner_balance.insert(&mut good_note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); // We will emit a note log with an incorrect preimage to ensure the pxe throws // This note has not been inserted... @@ -349,7 +394,14 @@ 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_note_with_keys(&mut context, outgoing_viewer_ovpk_m, owner_ivpk_m, owner)); + NoteEmission::new(bad_note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); } #[contract_library_method] @@ -368,7 +420,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_note_with_keys(context, outgoing_viewer_ovpk_m, owner_ivpk_m, owner)); + owner_balance.insert(&mut note).emit(encode_and_encrypt_note(context, outgoing_viewer_ovpk_m, owner_ivpk_m, owner)); } } 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 da74e7c3104..7a2aba19720 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 @@ -9,7 +9,7 @@ contract SchnorrAccount { use dep::std; use dep::aztec::prelude::{AztecAddress, PrivateContext, PrivateImmutable}; - use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys; + 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_authwit_message_hash} @@ -36,7 +36,7 @@ contract SchnorrAccount { // important. let mut pub_key_note = PublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_keys.npk_m.hash()); - storage.signing_public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note_with_keys(&mut context, this_keys.ovpk_m, this_keys.ivpk_m, this)); + storage.signing_public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this_keys.ovpk_m, this_keys.ivpk_m, this)); } // Note: If you globally change the entrypoint signature don't forget to update account_entrypoint.ts file diff --git a/noir-projects/noir-contracts/contracts/spam_contract/src/main.nr b/noir-projects/noir-contracts/contracts/spam_contract/src/main.nr index fae6a1bbad0..e6206501fee 100644 --- a/noir-projects/noir-contracts/contracts/spam_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/spam_contract/src/main.nr @@ -8,7 +8,7 @@ contract Spam { use dep::aztec::{ prelude::{Map, AztecAddress, PublicMutable}, - encrypted_logs::{encrypted_note_emission::encode_and_encrypt_note_with_keys_unconstrained}, + encrypted_logs::{encrypted_note_emission::encode_and_encrypt_note_unconstrained}, keys::getters::get_public_keys, protocol_types::{ hash::poseidon2_hash_with_separator, @@ -36,7 +36,7 @@ contract Spam { for _ in 0..MAX_NOTE_HASHES_PER_CALL { storage.balances.at(caller).add(caller_keys.npk_m, U128::from_integer(amount)).emit( - encode_and_encrypt_note_with_keys_unconstrained(&mut context, caller_keys.ovpk_m, caller_keys.ivpk_m, caller) + encode_and_encrypt_note_unconstrained(&mut context, caller_keys.ovpk_m, caller_keys.ivpk_m, caller) ); } 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 fbdcf745c57..0ec1b4bce79 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 @@ -44,9 +44,12 @@ contract StaticChild { #[private] #[view] fn private_illegal_set_value(new_value: Field, owner: AztecAddress) -> Field { - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); - let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); + let msg_sender_keys = get_public_keys(context.msg_sender()); + let owner_keys = get_public_keys(owner); + + let mut note = ValueNote::new(new_value, owner_keys.npk_m.hash()); + + storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, owner)); new_value } @@ -57,9 +60,18 @@ contract StaticChild { owner: AztecAddress, outgoing_viewer: AztecAddress ) -> Field { - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); - let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_private_value.insert(&mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + let owner_keys = get_public_keys(owner); + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); + + let mut note = ValueNote::new(new_value, owner_keys.npk_m.hash()); + storage.a_private_value.insert(&mut note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + 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 5636fc84dec..6ea2ad6f0a0 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -102,10 +102,18 @@ contract Test { storage_slot != storage.example_constant.get_storage_slot(), "this storage slot is reserved for example_constant" ); - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let outgoing_viewer_keys = get_public_keys(outgoing_viewer); + let owner_keys = get_public_keys(owner); - let mut note = ValueNote::new(value, owner_npk_m_hash); - create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt_note(&mut context, outgoing_viewer, owner)); + let mut note = ValueNote::new(value, owner_keys.npk_m.hash()); + create_note(&mut context, storage_slot, &mut note).emit( + encode_and_encrypt_note( + &mut context, + outgoing_viewer_keys.ovpk_m, + owner_keys.ivpk_m, + owner + ) + ); } #[private] @@ -312,10 +320,11 @@ contract Test { Test::at(context.this_address()).call_create_note(value, owner, outgoing_viewer, storage_slot).call(&mut context); storage_slot += 1; - let owner_npk_m_hash = get_public_keys(owner).npk_m.hash(); + let msg_sender_keys = get_public_keys(context.msg_sender()); + let owner_keys = get_public_keys(owner); - let mut note = ValueNote::new(value + 1, owner_npk_m_hash); - create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner)); + let mut note = ValueNote::new(value + 1, owner_keys.npk_m.hash()); + create_note(&mut context, storage_slot, &mut note).emit(encode_and_encrypt_note(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, 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/token_blacklist_contract/src/main.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/main.nr index 45cc7e76994..42ae2f7fd74 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 @@ -18,7 +18,8 @@ contract TokenBlacklist { prelude::{AztecAddress, Map, NoteGetterOptions, PrivateSet, PublicMutable, SharedMutable}, encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note_unconstrained, encode_and_encrypt_note}, utils::comparison::Comparator, - macros::{storage::storage, functions::{private, public, initializer, view, internal}} + macros::{storage::storage, functions::{private, public, initializer, view, internal}}, + keys::getters::get_public_keys }; use dep::authwit::{auth::{assert_current_call_valid_authwit, assert_current_call_valid_authwit_public}}; @@ -182,8 +183,9 @@ contract TokenBlacklist { assert(notes.len() == 1, "note not popped"); // 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_note(&mut context, caller, to)); + let msg_sender_keys = get_public_keys(context.msg_sender()); + let to_keys = get_public_keys(to); + storage.balances.add(to, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, msg_sender_keys.ovpk_m, to_keys.ivpk_m, to)); } #[private] @@ -199,7 +201,8 @@ contract TokenBlacklist { assert(nonce == 0, "invalid nonce"); } - storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, from)); + let from_keys = get_public_keys(from); + storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); TokenBlacklist::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); } @@ -218,9 +221,14 @@ contract TokenBlacklist { assert(nonce == 0, "invalid nonce"); } + let from_keys = get_public_keys(from); + let to_keys = get_public_keys(to); + let amount = U128::from_integer(amount); - storage.balances.sub(from, amount).emit(encode_and_encrypt_note_unconstrained(&mut context, from, from)); - storage.balances.add(to, amount).emit(encode_and_encrypt_note_unconstrained(&mut context, from, to)); + storage.balances.sub(from, amount).emit( + encode_and_encrypt_note_unconstrained(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from) + ); + storage.balances.add(to, amount).emit(encode_and_encrypt_note_unconstrained(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to)); } #[private] @@ -234,7 +242,8 @@ contract TokenBlacklist { assert(nonce == 0, "invalid nonce"); } - storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from, from)); + let from_keys = get_public_keys(from); + storage.balances.sub(from, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, 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 a5ec69962f3..1883c140173 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/main.nr @@ -22,7 +22,7 @@ contract Token { context::{PrivateContext, PrivateCallInterface}, hash::compute_secret_hash, prelude::{NoteGetterOptions, Map, PublicMutable, SharedImmutable, PrivateSet, AztecAddress, FunctionSelector}, encrypted_logs::{ - encrypted_note_emission::{encode_and_encrypt_note_with_keys, encode_and_encrypt_note_with_keys_unconstrained}, + encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_unconstrained}, encrypted_event_emission::encode_and_encrypt_event_with_keys_unconstrained }, keys::getters::get_public_keys, @@ -208,9 +208,7 @@ contract Token { fn privately_mint_private_note(amount: Field) { let caller = context.msg_sender(); let caller_keys = get_public_keys(caller); - storage.balances.at(caller).add(caller_keys.npk_m, U128::from_integer(amount)).emit( - encode_and_encrypt_note_with_keys(&mut context, caller_keys.ovpk_m, caller_keys.ivpk_m, caller) - ); + storage.balances.at(caller).add(caller_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, caller_keys.ovpk_m, caller_keys.ivpk_m, caller)); Token::at(context.this_address()).assert_minter_and_mint(context.msg_sender(), amount).enqueue(&mut context); } #[public] @@ -289,7 +287,7 @@ contract Token { let from = context.msg_sender(); let from_keys = get_public_keys(from); let to_keys = get_public_keys(to); - storage.balances.at(to).add(to_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note_with_keys(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to)); + storage.balances.at(to).add(to_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to)); } // docs:end:redeem_shield // docs:start:unshield @@ -302,7 +300,7 @@ contract Token { } let from_keys = get_public_keys(from); - storage.balances.at(from).sub(from_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note_with_keys(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); + storage.balances.at(from).sub(from_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); Token::at(context.this_address())._increase_public_balance(to, amount).enqueue(&mut context); } // docs:end:unshield @@ -329,11 +327,9 @@ contract Token { INITIAL_TRANSFER_CALL_MAX_NOTES ); storage.balances.at(from).add(from_keys.npk_m, change).emit( - encode_and_encrypt_note_with_keys_unconstrained(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from) - ); - storage.balances.at(to).add(to_keys.npk_m, amount).emit( - encode_and_encrypt_note_with_keys_unconstrained(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to) + encode_and_encrypt_note_unconstrained(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from) ); + storage.balances.at(to).add(to_keys.npk_m, amount).emit(encode_and_encrypt_note_unconstrained(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to)); // We don't constrain encryption of the note log in `transfer` (unlike in `transfer_from`) because the transfer // function is only designed to be used in situations where the event is not strictly necessary (e.g. payment to // another person where the payment is considered to be successful when the other party successfully decrypts a @@ -420,10 +416,10 @@ contract Token { let amount = U128::from_integer(amount); // docs:start:increase_private_balance // docs:start:encrypted - storage.balances.at(from).sub(from_keys.npk_m, amount).emit(encode_and_encrypt_note_with_keys(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); + storage.balances.at(from).sub(from_keys.npk_m, amount).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); // docs:end:encrypted // docs:end:increase_private_balance - storage.balances.at(to).add(to_keys.npk_m, amount).emit(encode_and_encrypt_note_with_keys(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to)); + storage.balances.at(to).add(to_keys.npk_m, amount).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, to_keys.ivpk_m, to)); } // docs:end:transfer_from // docs:start:burn @@ -435,7 +431,7 @@ contract Token { assert(nonce == 0, "invalid nonce"); } let from_keys = get_public_keys(from); - storage.balances.at(from).sub(from_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note_with_keys(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); + storage.balances.at(from).sub(from_keys.npk_m, U128::from_integer(amount)).emit(encode_and_encrypt_note(&mut context, from_keys.ovpk_m, from_keys.ivpk_m, from)); Token::at(context.this_address())._reduce_total_supply(amount).enqueue(&mut context); } // docs:end:burn @@ -485,7 +481,7 @@ contract Token { INITIAL_TRANSFER_CALL_MAX_NOTES ); storage.balances.at(user).add(user_keys.npk_m, change).emit( - encode_and_encrypt_note_with_keys_unconstrained(&mut context, user_keys.ovpk_m, user_keys.ivpk_m, user) + encode_and_encrypt_note_unconstrained(&mut context, user_keys.ovpk_m, user_keys.ivpk_m, user) ); // 4. Now we get the note hiding points. let mut fee_payer_point = TokenNote::hiding_point().new( From df270ee0c888b5014c955df34132cfda78e50f57 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 26 Sep 2024 10:20:48 +0000 Subject: [PATCH 2/3] refactor: nuking encryption oracles --- .../aztec-nr/aztec/src/oracle/logs.nr | 73 +---------------- .../simulator/src/acvm/oracle/oracle.ts | 69 +--------------- .../simulator/src/acvm/oracle/typed_oracle.ts | 25 ------ .../src/client/client_execution_context.ts | 79 +------------------ yarn-project/txe/src/oracle/txe_oracle.ts | 43 +--------- .../txe/src/txe_service/txe_service.ts | 36 --------- 6 files changed, 6 insertions(+), 319 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/oracle/logs.nr b/noir-projects/aztec-nr/aztec/src/oracle/logs.nr index e1384ffc17a..83a0cc5ad89 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/logs.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/logs.nr @@ -1,4 +1,4 @@ -use dep::protocol_types::{address::AztecAddress, point::Point}; +use dep::protocol_types::address::AztecAddress; // = 480 + 32 * N bytes #[oracle(emitEncryptedNoteLog)] @@ -23,77 +23,6 @@ unconstrained pub fn emit_encrypted_event_log( ) { emit_encrypted_event_log_oracle(contract_address, randomness, encrypted_event, counter) } - -// = 480 + 32 * N bytes -#[oracle(computeEncryptedNoteLog)] -unconstrained fn compute_encrypted_note_log_oracle( - _contract_address: AztecAddress, - _storage_slot: Field, - _note_type_id: Field, - _ovsk_app: Field, - _ovpk_m: Point, - _ivpk_m: Point, - _recipient: AztecAddress, - _preimage: [Field; N] -) -> [u8; M] {} - -unconstrained pub fn compute_encrypted_note_log( - contract_address: AztecAddress, - storage_slot: Field, - note_type_id: Field, - ovsk_app: Field, - ovpk_m: Point, - ivpk_m: Point, - recipient: AztecAddress, - preimage: [Field; N] -) -> [u8; M] { - compute_encrypted_note_log_oracle( - contract_address, - storage_slot, - note_type_id, - ovsk_app, - ovpk_m, - ivpk_m, - recipient, - preimage - ) -} - -// = 480 + 32 * N bytes -#[oracle(computeEncryptedEventLog)] -unconstrained fn compute_encrypted_event_log_oracle( - _contract_address: AztecAddress, - _randomness: Field, - _event_type_id: Field, - _ovsk_app: Field, - _ovpk_m: Point, - _ivpk_m: Point, - _recipient: AztecAddress, - _preimage: [Field; N] -) -> [u8; M] {} - -unconstrained pub fn compute_encrypted_event_log( - contract_address: AztecAddress, - randomness: Field, - event_type_id: Field, - ovsk_app: Field, - ovpk_m: Point, - ivpk_m: Point, - recipient: AztecAddress, - preimage: [Field; N] -) -> [u8; M] { - compute_encrypted_event_log_oracle( - contract_address, - randomness, - event_type_id, - ovsk_app, - ovpk_m, - ivpk_m, - recipient, - preimage - ) -} - #[oracle(emitUnencryptedLog)] unconstrained fn emit_unencrypted_log_oracle_private(_contract_address: AztecAddress, _message: T, _counter: u32) -> Field {} diff --git a/yarn-project/simulator/src/acvm/oracle/oracle.ts b/yarn-project/simulator/src/acvm/oracle/oracle.ts index cab30078dc7..9bb83f060e0 100644 --- a/yarn-project/simulator/src/acvm/oracle/oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/oracle.ts @@ -1,8 +1,7 @@ import { MerkleTreeId, UnencryptedL2Log } from '@aztec/circuit-types'; -import { KeyValidationRequest } from '@aztec/circuits.js'; import { FunctionSelector, NoteSelector } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr, Point } from '@aztec/foundation/fields'; +import { Fr } from '@aztec/foundation/fields'; import { type ACVMField } from '../acvm_types.js'; import { frToBoolean, frToNumber, fromACVMField } from '../deserialize.js'; @@ -329,72 +328,6 @@ export class Oracle { this.typedOracle.emitEncryptedNoteLog(+noteHashCounter, processedInput, +counter); } - computeEncryptedEventLog( - [contractAddress]: ACVMField[], - [randomness]: ACVMField[], - [eventTypeId]: ACVMField[], - [ovskApp]: ACVMField[], - [ovpkMX]: ACVMField[], - [ovpkMY]: ACVMField[], - [ovpkMIsInfinite]: ACVMField[], - [ivpkMX]: ACVMField[], - [ivpkMY]: ACVMField[], - [ivpkMIsInfinite]: ACVMField[], - [recipient]: ACVMField[], - preimage: ACVMField[], - ): ACVMField[] { - const ovpkM = new Point(fromACVMField(ovpkMX), fromACVMField(ovpkMY), !fromACVMField(ovpkMIsInfinite).isZero()); - const ovKeys = new KeyValidationRequest(ovpkM, Fr.fromString(ovskApp)); - const ivpkM = new Point(fromACVMField(ivpkMX), fromACVMField(ivpkMY), !fromACVMField(ivpkMIsInfinite).isZero()); - const encLog = this.typedOracle.computeEncryptedEventLog( - AztecAddress.fromString(contractAddress), - Fr.fromString(randomness), - Fr.fromString(eventTypeId), - ovKeys, - ivpkM, - AztecAddress.fromString(recipient), - preimage.map(fromACVMField), - ); - const bytes: ACVMField[] = []; - encLog.forEach(v => { - bytes.push(toACVMField(v)); - }); - return bytes; - } - - computeEncryptedNoteLog( - [contractAddress]: ACVMField[], - [storageSlot]: ACVMField[], - [noteTypeId]: ACVMField[], - [ovskApp]: ACVMField[], - [ovpkMX]: ACVMField[], - [ovpkMY]: ACVMField[], - [ovpkMIsInfinite]: ACVMField[], - [ivpkMX]: ACVMField[], - [ivpkMY]: ACVMField[], - [ivpkMIsInfinite]: ACVMField[], - [recipient]: ACVMField[], - preimage: ACVMField[], - ): ACVMField[] { - const ovpkM = new Point(fromACVMField(ovpkMX), fromACVMField(ovpkMY), !fromACVMField(ovpkMIsInfinite).isZero()); - const ovKeys = new KeyValidationRequest(ovpkM, Fr.fromString(ovskApp)); - const ivpkM = new Point(fromACVMField(ivpkMX), fromACVMField(ivpkMY), !fromACVMField(ivpkMIsInfinite).isZero()); - const encLog = this.typedOracle.computeEncryptedNoteLog( - AztecAddress.fromString(contractAddress), - Fr.fromString(storageSlot), - NoteSelector.fromField(Fr.fromString(noteTypeId)), - ovKeys, - ivpkM, - AztecAddress.fromString(recipient), - preimage.map(fromACVMField), - ); - const bytes: ACVMField[] = []; - encLog.forEach(v => { - bytes.push(toACVMField(v)); - }); - return bytes; - } - emitUnencryptedLog([contractAddress]: ACVMField[], message: ACVMField[], [counter]: ACVMField[]): ACVMField { const logPayload = Buffer.concat(message.map(fromACVMField).map(f => f.toBuffer())); const log = new UnencryptedL2Log(AztecAddress.fromString(contractAddress), logPayload); diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index 1b9dae81e8a..dadb3603e70 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -5,7 +5,6 @@ import { type NoteStatus, type NullifierMembershipWitness, type PublicDataWitness, - type PublicKey, type SiblingPath, type UnencryptedL2Log, } from '@aztec/circuit-types'; @@ -204,30 +203,6 @@ export abstract class TypedOracle { throw new OracleMethodNotAvailableError('emitEncryptedNoteLog'); } - computeEncryptedEventLog( - _contractAddress: AztecAddress, - _randomness: Fr, - _eventTypeId: Fr, - _ovKeys: KeyValidationRequest, - _ivpkM: PublicKey, - _recipient: AztecAddress, - _preimage: Fr[], - ): Buffer { - throw new OracleMethodNotAvailableError('computeEncryptedEventLog'); - } - - computeEncryptedNoteLog( - _contractAddress: AztecAddress, - _storageSlot: Fr, - _noteTypeId: NoteSelector, - _ovKeys: KeyValidationRequest, - _ivpkM: PublicKey, - _recipient: AztecAddress, - _preimage: Fr[], - ): Buffer { - throw new OracleMethodNotAvailableError('computeEncryptedNoteLog'); - } - emitUnencryptedLog(_log: UnencryptedL2Log, _counter: number): void { throw new OracleMethodNotAvailableError('emitUnencryptedLog'); } diff --git a/yarn-project/simulator/src/client/client_execution_context.ts b/yarn-project/simulator/src/client/client_execution_context.ts index 79b1f913ef4..eb18ef764d8 100644 --- a/yarn-project/simulator/src/client/client_execution_context.ts +++ b/yarn-project/simulator/src/client/client_execution_context.ts @@ -3,35 +3,18 @@ import { type AztecNode, EncryptedL2Log, EncryptedL2NoteLog, - Event, - L1EventPayload, - L1NotePayload, Note, type NoteStatus, PublicExecutionRequest, - TaggedLog, type UnencryptedL2Log, } from '@aztec/circuit-types'; -import { - CallContext, - FunctionSelector, - type Header, - type KeyValidationRequest, - PrivateContextInputs, - type TxContext, -} from '@aztec/circuits.js'; +import { CallContext, FunctionSelector, type Header, PrivateContextInputs, type TxContext } from '@aztec/circuits.js'; import { Aes128 } from '@aztec/circuits.js/barretenberg'; import { computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash'; -import { - EventSelector, - type FunctionAbi, - type FunctionArtifact, - type NoteSelector, - countArgumentsSize, -} from '@aztec/foundation/abi'; +import { type FunctionAbi, type FunctionArtifact, type NoteSelector, countArgumentsSize } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; import { poseidon2HashWithSeparator } from '@aztec/foundation/crypto'; -import { Fr, GrumpkinScalar, type Point } from '@aztec/foundation/fields'; +import { Fr } from '@aztec/foundation/fields'; import { applyStringFormatting, createDebugLogger } from '@aztec/foundation/log'; import { type NoteData, toACVMWitness } from '../acvm/index.js'; @@ -373,62 +356,6 @@ export class ClientExecutionContext extends ViewDataOracle { this.noteEncryptedLogs.push(encryptedLog); } - /** - * Encrypt an event - * @param contractAddress - The contract emitting the encrypted event. - * @param randomness - A value used to mask the contract address we are siloing with. - * @param eventTypeId - The type ID of the event (function selector). - * @param ovKeys - The outgoing viewing keys to use to encrypt. - * @param ivpkM - The master incoming viewing public key. - * @param recipient - The recipient of the encrypted event log. - * @param preimage - The event preimage. - */ - public override computeEncryptedEventLog( - contractAddress: AztecAddress, - randomness: Fr, - eventTypeId: Fr, - ovKeys: KeyValidationRequest, - ivpkM: Point, - recipient: AztecAddress, - preimage: Fr[], - ) { - const event = new Event(preimage); - const l1EventPayload = new L1EventPayload(event, contractAddress, randomness, EventSelector.fromField(eventTypeId)); - const taggedEvent = new TaggedLog(l1EventPayload); - - const ephSk = GrumpkinScalar.random(); - - return taggedEvent.encrypt(ephSk, recipient, ivpkM, ovKeys); - } - - /** - * Encrypt a note - * @param contractAddress - The contract address of the note. - * @param storageSlot - The storage slot the note is at. - * @param noteTypeId - The type ID of the note. - * @param ovKeys - The outgoing viewing keys to use to encrypt. - * @param ivpkM - The master incoming viewing public key. - * @param recipient - The recipient of the encrypted note log. - * @param preimage - The note preimage. - */ - public override computeEncryptedNoteLog( - contractAddress: AztecAddress, - storageSlot: Fr, - noteTypeId: NoteSelector, - ovKeys: KeyValidationRequest, - ivpkM: Point, - recipient: AztecAddress, - preimage: Fr[], - ) { - const note = new Note(preimage); - const l1NotePayload = new L1NotePayload(note, contractAddress, storageSlot, noteTypeId); - const taggedNote = new TaggedLog(l1NotePayload); - - const ephSk = GrumpkinScalar.random(); - - return taggedNote.encrypt(ephSk, recipient, ivpkM, ovKeys); - } - /** * Emit an unencrypted log. * @param log - The unencrypted log to be emitted. diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index a2fdb9df74d..b2bfe0079b7 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -1,8 +1,5 @@ import { AuthWitness, - Event, - L1EventPayload, - L1NotePayload, MerkleTreeId, Note, type NoteStatus, @@ -10,7 +7,6 @@ import { PublicDataWitness, PublicDataWrite, PublicExecutionRequest, - TaggedLog, type UnencryptedL2Log, } from '@aztec/circuit-types'; import { type CircuitWitnessGenerationStats } from '@aztec/circuit-types/stats'; @@ -38,14 +34,13 @@ import { Aes128, Schnorr } from '@aztec/circuits.js/barretenberg'; import { computePublicDataTreeLeafSlot, siloNoteHash, siloNullifier } from '@aztec/circuits.js/hash'; import { type ContractArtifact, - EventSelector, type FunctionAbi, FunctionSelector, type NoteSelector, countArgumentsSize, } from '@aztec/foundation/abi'; import { AztecAddress } from '@aztec/foundation/aztec-address'; -import { Fr, GrumpkinScalar, type Point } from '@aztec/foundation/fields'; +import { Fr } from '@aztec/foundation/fields'; import { type Logger, applyStringFormatting } from '@aztec/foundation/log'; import { Timer } from '@aztec/foundation/timer'; import { type KeyStore } from '@aztec/key-store'; @@ -543,24 +538,6 @@ export class TXE implements TypedOracle { return; } - computeEncryptedNoteLog( - contractAddress: AztecAddress, - storageSlot: Fr, - noteTypeId: NoteSelector, - ovKeys: KeyValidationRequest, - ivpkM: Point, - recipient: AztecAddress, - preimage: Fr[], - ): Buffer { - const note = new Note(preimage); - const l1NotePayload = new L1NotePayload(note, contractAddress, storageSlot, noteTypeId); - const taggedNote = new TaggedLog(l1NotePayload); - - const ephSk = GrumpkinScalar.random(); - - return taggedNote.encrypt(ephSk, recipient, ivpkM, ovKeys); - } - emitUnencryptedLog(_log: UnencryptedL2Log, counter: number): void { this.sideEffectsCounter = counter + 1; return; @@ -856,22 +833,4 @@ export class TXE implements TypedOracle { this.sideEffectsCounter = counter + 1; return; } - - computeEncryptedEventLog( - contractAddress: AztecAddress, - randomness: Fr, - eventTypeId: Fr, - ovKeys: KeyValidationRequest, - ivpkM: Point, - recipient: AztecAddress, - preimage: Fr[], - ): Buffer { - const event = new Event(preimage); - const l1EventPayload = new L1EventPayload(event, contractAddress, randomness, EventSelector.fromField(eventTypeId)); - const taggedEvent = new TaggedLog(l1EventPayload); - - const ephSk = GrumpkinScalar.random(); - - return taggedEvent.encrypt(ephSk, recipient, ivpkM, ovKeys); - } } diff --git a/yarn-project/txe/src/txe_service/txe_service.ts b/yarn-project/txe/src/txe_service/txe_service.ts index ea4b2c0088f..f9c0db63d40 100644 --- a/yarn-project/txe/src/txe_service/txe_service.ts +++ b/yarn-project/txe/src/txe_service/txe_service.ts @@ -4,9 +4,7 @@ import { Fr, FunctionSelector, Header, - KeyValidationRequest, PUBLIC_DATA_SUBTREE_HEIGHT, - Point, PublicDataTreeLeaf, computePartialAddress, getContractInstanceFromDeployParams, @@ -614,40 +612,6 @@ export class TXEService { return toForeignCallResult([toArray(keyValidationRequest.toFields())]); } - computeEncryptedNoteLog( - contractAddress: ForeignCallSingle, - storageSlot: ForeignCallSingle, - noteTypeId: ForeignCallSingle, - ovskApp: ForeignCallSingle, - ovpkMX: ForeignCallSingle, - ovpkMY: ForeignCallSingle, - ovpkMIsInfinite: ForeignCallSingle, - ivpkMX: ForeignCallSingle, - ivpkMY: ForeignCallSingle, - ivpkMIsInfinite: ForeignCallSingle, - recipient: ForeignCallSingle, - preimage: ForeignCallArray, - ) { - const ovpkM = new Point(fromSingle(ovpkMX), fromSingle(ovpkMY), !fromSingle(ovpkMIsInfinite).isZero()); - const ovKeys = new KeyValidationRequest(ovpkM, Fr.fromString(fromSingle(ovskApp).toString())); - const ivpkM = new Point(fromSingle(ivpkMX), fromSingle(ivpkMY), !fromSingle(ivpkMIsInfinite).isZero()); - const encLog = this.typedOracle.computeEncryptedNoteLog( - AztecAddress.fromString(fromSingle(contractAddress).toString()), - Fr.fromString(fromSingle(storageSlot).toString()), - NoteSelector.fromField(Fr.fromString(fromSingle(noteTypeId).toString())), - ovKeys, - ivpkM, - AztecAddress.fromString(fromSingle(recipient).toString()), - fromArray(preimage), - ); - const bytes: Fr[] = []; - - encLog.forEach(v => { - bytes.push(new Fr(v)); - }); - return toForeignCallResult([toArray(bytes)]); - } - emitEncryptedLog( _contractAddress: ForeignCallSingle, _randomness: ForeignCallSingle, From bb0f6d91956fbfefcaaffeec5e2fb0a86663f9e3 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 26 Sep 2024 10:39:06 +0000 Subject: [PATCH 3/3] nuking aes128_encrypt oracle --- .../aztec-nr/aztec/src/oracle/encryption.nr | 12 ------- .../aztec-nr/aztec/src/oracle/logs.nr | 32 ++++++------------- .../aztec-nr/aztec/src/oracle/mod.nr | 1 - .../contracts/test_contract/src/main.nr | 20 +++++------- .../aes128_encrypt/src/main.nr | 27 ++++++---------- .../simulator/src/acvm/oracle/oracle.ts | 13 -------- .../simulator/src/acvm/oracle/typed_oracle.ts | 4 --- .../src/client/client_execution_context.ts | 6 ---- yarn-project/txe/src/oracle/txe_oracle.ts | 7 +--- 9 files changed, 28 insertions(+), 94 deletions(-) delete mode 100644 noir-projects/aztec-nr/aztec/src/oracle/encryption.nr diff --git a/noir-projects/aztec-nr/aztec/src/oracle/encryption.nr b/noir-projects/aztec-nr/aztec/src/oracle/encryption.nr deleted file mode 100644 index cc9969fa7c7..00000000000 --- a/noir-projects/aztec-nr/aztec/src/oracle/encryption.nr +++ /dev/null @@ -1,12 +0,0 @@ -#[oracle(aes128Encrypt)] -unconstrained pub fn aes128_encrypt_oracle( - input: [u8; N], - iv: [u8; 16], - key: [u8; 16] -) -> [u8; M] {} - -// AES 128 CBC with PKCS7 is padding to multiples of 16 bytes so M has to be a multiple of 16! -// (e.g. from 65 bytes long input you get 80 bytes long output and M has to be set to `80`) -unconstrained pub fn aes128_encrypt(input: [u8; N], iv: [u8; 16], key: [u8; 16]) -> [u8; M] { - aes128_encrypt_oracle(input, iv, key) -} diff --git a/noir-projects/aztec-nr/aztec/src/oracle/logs.nr b/noir-projects/aztec-nr/aztec/src/oracle/logs.nr index 83a0cc5ad89..a84be6e54b8 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/logs.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/logs.nr @@ -4,42 +4,30 @@ use dep::protocol_types::address::AztecAddress; #[oracle(emitEncryptedNoteLog)] unconstrained fn emit_encrypted_note_log_oracle(_note_hash_counter: u32, _encrypted_note: [u8; M], _counter: u32) {} -unconstrained pub fn emit_encrypted_note_log( - note_hash_counter: u32, - encrypted_note: [u8; M], - counter: u32 -) { +unconstrained pub fn emit_encrypted_note_log(note_hash_counter: u32, encrypted_note: [u8; M], counter: u32) { emit_encrypted_note_log_oracle(note_hash_counter, encrypted_note, counter) } #[oracle(emitEncryptedEventLog)] -unconstrained fn emit_encrypted_event_log_oracle(_contract_address: AztecAddress, _randomness: Field, _encrypted_event: [u8; M], _counter: u32) {} +unconstrained fn emit_encrypted_event_log_oracle( + _contract_address: AztecAddress, + _randomness: Field, + _encrypted_event: [u8; M], + _counter: u32 +) {} -unconstrained pub fn emit_encrypted_event_log( - contract_address: AztecAddress, - randomness: Field, - encrypted_event: [u8; M], - counter: u32 -) { +unconstrained pub fn emit_encrypted_event_log(contract_address: AztecAddress, randomness: Field, encrypted_event: [u8; M], counter: u32) { emit_encrypted_event_log_oracle(contract_address, randomness, encrypted_event, counter) } #[oracle(emitUnencryptedLog)] unconstrained fn emit_unencrypted_log_oracle_private(_contract_address: AztecAddress, _message: T, _counter: u32) -> Field {} -unconstrained pub fn emit_unencrypted_log_private_internal( - contract_address: AztecAddress, - message: T, - counter: u32 -) -> Field { +unconstrained pub fn emit_unencrypted_log_private_internal(contract_address: AztecAddress, message: T, counter: u32) -> Field { emit_unencrypted_log_oracle_private(contract_address, message, counter) } #[oracle(emitContractClassUnencryptedLog)] -unconstrained fn emit_contract_class_unencrypted_log_private( - contract_address: AztecAddress, - message: [Field; N], - counter: u32 -) -> Field {} +unconstrained fn emit_contract_class_unencrypted_log_private(contract_address: AztecAddress, message: [Field; N], counter: u32) -> Field {} unconstrained pub fn emit_contract_class_unencrypted_log_private_internal(contract_address: AztecAddress, message: [Field; N], counter: u32) -> Field { emit_contract_class_unencrypted_log_private(contract_address, message, counter) diff --git a/noir-projects/aztec-nr/aztec/src/oracle/mod.nr b/noir-projects/aztec-nr/aztec/src/oracle/mod.nr index 81d06c48a63..140742b555f 100644 --- a/noir-projects/aztec-nr/aztec/src/oracle/mod.nr +++ b/noir-projects/aztec-nr/aztec/src/oracle/mod.nr @@ -4,7 +4,6 @@ mod arguments; mod call_private_function; -mod encryption; mod execution; mod get_contract_instance; mod get_l1_to_l2_membership_witness; 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 6ea2ad6f0a0..e87a1ddcf4f 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -7,17 +7,13 @@ use dep::aztec::macros::aztec; contract Test { use dep::aztec::prelude::{ - AztecAddress, EthAddress, FunctionSelector, NoteHeader, NoteGetterOptions, NoteViewerOptions, - PrivateContext, PrivateImmutable, PrivateSet, SharedImmutable + AztecAddress, EthAddress, FunctionSelector, NoteGetterOptions, NoteViewerOptions, + PrivateImmutable, PrivateSet }; 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_with_randomness; - use dep::aztec::protocol_types::{ - abis::private_circuit_public_inputs::PrivateCircuitPublicInputs, - constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::{Serialize, ToField, FromField}, - point::Point, scalar::Scalar, storage::map::derive_storage_slot_in_map - }; + use dep::aztec::protocol_types::{constants::MAX_NOTE_HASH_READ_REQUESTS_PER_CALL, traits::Serialize, point::Point, scalar::Scalar}; use dep::aztec::encrypted_logs::header::EncryptedLogHeader; use dep::aztec::encrypted_logs::payload::{compute_incoming_body_ciphertext, compute_outgoing_body_ciphertext}; @@ -26,14 +22,13 @@ contract Test { use dep::aztec::keys::getters::get_public_keys; use dep::aztec::{ - context::inputs::private_context_inputs::PrivateContextInputs, hash::{pedersen_hash, compute_secret_hash, ArgsHasher}, keys::public_keys::IvpkM, note::{ lifecycle::{create_note, destroy_note_unsafe}, note_getter::{get_notes, view_notes}, note_getter_options::NoteStatus }, - deploy::deploy_contract as aztec_deploy_contract, - oracle::{encryption::aes128_encrypt, unsafe_rand::unsafe_rand}, utils::comparison::Comparator, + deploy::deploy_contract as aztec_deploy_contract, oracle::unsafe_rand::unsafe_rand, + utils::comparison::Comparator, macros::{storage::storage, events::event, functions::{private, public, internal}} }; use dep::token_portal_content_hash_lib::{get_mint_private_content_hash, get_mint_public_content_hash}; @@ -41,6 +36,7 @@ contract Test { // TODO investigate why the macros require EmbeddedCurvePoint and EmbeddedCurveScalar use std::embedded_curve_ops::{EmbeddedCurveScalar, EmbeddedCurvePoint, fixed_base_scalar_mul as derive_public_key}; use std::meta::derive; + use std::aes128::aes128_encrypt; use crate::test_note::TestNote; @@ -406,12 +402,12 @@ contract Test { #[private] fn encrypt(input: [u8; 64], iv: [u8; 16], key: [u8; 16]) -> [u8; 80] { - aes128_encrypt(input, iv, key) + aes128_encrypt(input, iv, key).as_array() } #[private] fn encrypt_with_padding(input: [u8; 65], iv: [u8; 16], key: [u8; 16]) -> [u8; 80] { - aes128_encrypt(input, iv, key) + aes128_encrypt(input, iv, key).as_array() } #[private] diff --git a/noir/noir-repo/test_programs/execution_success/aes128_encrypt/src/main.nr b/noir/noir-repo/test_programs/execution_success/aes128_encrypt/src/main.nr index b937c801860..31d907fea10 100644 --- a/noir/noir-repo/test_programs/execution_success/aes128_encrypt/src/main.nr +++ b/noir/noir-repo/test_programs/execution_success/aes128_encrypt/src/main.nr @@ -21,29 +21,20 @@ unconstrained fn decode_hex(s: str) -> [u8; M] { } unconstrained fn cipher(plaintext: [u8; 12], iv: [u8; 16], key: [u8; 16]) -> [u8; 16] { - let slice_res = std::aes128::aes128_encrypt(plaintext, iv, key); - let mut result = [0; 16]; - for i in 0..16 { - result[i] = slice_res[i]; - } - result + let result = std::aes128::aes128_encrypt(plaintext, iv, key); + result.as_array() } fn main(inputs: str<12>, iv: str<16>, key: str<16>, output: str<32>) { - let result = std::aes128::aes128_encrypt(inputs.as_bytes(), iv.as_bytes(), key.as_bytes()); + let result: [u8; 16] = std::aes128::aes128_encrypt(inputs.as_bytes(), iv.as_bytes(), key.as_bytes()).as_array(); let output_bytes: [u8; 16] = unsafe { - let output_bytes: [u8; 16] = decode_hex(output); - for i in 0..16 { - assert(result[i] == output_bytes[i]); - } - output_bytes + decode_hex(output) }; + assert(result == output_bytes); - unsafe { - let unconstrained_result = cipher(inputs.as_bytes(), iv.as_bytes(), key.as_bytes()); - for i in 0..16 { - assert(unconstrained_result[i] == output_bytes[i]); - } - } + let unconstrained_result = unsafe { + cipher(inputs.as_bytes(), iv.as_bytes(), key.as_bytes()) + }; + assert(unconstrained_result == output_bytes); } diff --git a/yarn-project/simulator/src/acvm/oracle/oracle.ts b/yarn-project/simulator/src/acvm/oracle/oracle.ts index 9bb83f060e0..cb3bf8041b2 100644 --- a/yarn-project/simulator/src/acvm/oracle/oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/oracle.ts @@ -412,17 +412,4 @@ export class Oracle { notifySetMinRevertibleSideEffectCounter([minRevertibleSideEffectCounter]: ACVMField[]) { this.typedOracle.notifySetMinRevertibleSideEffectCounter(frToNumber(fromACVMField(minRevertibleSideEffectCounter))); } - - aes128Encrypt(input: ACVMField[], initializationVector: ACVMField[], key: ACVMField[]): ACVMField[] { - // Convert each field to a number and then to a buffer (1 byte is stored in 1 field) - const processedInput = Buffer.from(input.map(fromACVMField).map(f => f.toNumber())); - const processedIV = Buffer.from(initializationVector.map(fromACVMField).map(f => f.toNumber())); - const processedKey = Buffer.from(key.map(fromACVMField).map(f => f.toNumber())); - - // Encrypt the input - const ciphertext = this.typedOracle.aes128Encrypt(processedInput, processedIV, processedKey); - - // Convert each byte of ciphertext to a field and return it - return Array.from(ciphertext).map(byte => toACVMField(byte)); - } } diff --git a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts index dadb3603e70..63dcb34a735 100644 --- a/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts +++ b/yarn-project/simulator/src/acvm/oracle/typed_oracle.ts @@ -248,10 +248,6 @@ export abstract class TypedOracle { throw new OracleMethodNotAvailableError('notifySetMinRevertibleSideEffectCounter'); } - aes128Encrypt(_input: Buffer, _initializationVector: Buffer, _key: Buffer): Buffer { - throw new OracleMethodNotAvailableError('encrypt'); - } - debugLog(_message: string, _fields: Fr[]): void { throw new OracleMethodNotAvailableError('debugLog'); } diff --git a/yarn-project/simulator/src/client/client_execution_context.ts b/yarn-project/simulator/src/client/client_execution_context.ts index eb18ef764d8..3fc8a537916 100644 --- a/yarn-project/simulator/src/client/client_execution_context.ts +++ b/yarn-project/simulator/src/client/client_execution_context.ts @@ -9,7 +9,6 @@ import { type UnencryptedL2Log, } from '@aztec/circuit-types'; import { CallContext, FunctionSelector, type Header, PrivateContextInputs, type TxContext } from '@aztec/circuits.js'; -import { Aes128 } from '@aztec/circuits.js/barretenberg'; import { computeUniqueNoteHash, siloNoteHash } from '@aztec/circuits.js/hash'; import { type FunctionAbi, type FunctionArtifact, type NoteSelector, countArgumentsSize } from '@aztec/foundation/abi'; import { type AztecAddress } from '@aztec/foundation/aztec-address'; @@ -625,11 +624,6 @@ export class ClientExecutionContext extends ViewDataOracle { return values; } - public override aes128Encrypt(input: Buffer, initializationVector: Buffer, key: Buffer): Buffer { - const aes128 = new Aes128(); - return aes128.encryptBufferCBC(input, initializationVector, key); - } - public override debugLog(message: string, fields: Fr[]) { this.log.verbose(`debug_log ${applyStringFormatting(message, fields)}`); } diff --git a/yarn-project/txe/src/oracle/txe_oracle.ts b/yarn-project/txe/src/oracle/txe_oracle.ts index b2bfe0079b7..6bd3aef063e 100644 --- a/yarn-project/txe/src/oracle/txe_oracle.ts +++ b/yarn-project/txe/src/oracle/txe_oracle.ts @@ -30,7 +30,7 @@ import { deriveKeys, getContractClassFromArtifact, } from '@aztec/circuits.js'; -import { Aes128, Schnorr } from '@aztec/circuits.js/barretenberg'; +import { Schnorr } from '@aztec/circuits.js/barretenberg'; import { computePublicDataTreeLeafSlot, siloNoteHash, siloNullifier } from '@aztec/circuits.js/hash'; import { type ContractArtifact, @@ -815,11 +815,6 @@ export class TXE implements TypedOracle { this.noteCache.setMinRevertibleSideEffectCounter(minRevertibleSideEffectCounter); } - aes128Encrypt(input: Buffer, initializationVector: Buffer, key: Buffer): Buffer { - const aes128 = new Aes128(); - return aes128.encryptBufferCBC(input, initializationVector, key); - } - debugLog(message: string, fields: Fr[]): void { this.logger.verbose(`debug_log ${applyStringFormatting(message, fields)}`); }