From 04b6a55d9ec7c40fd8adb8c9dc4ceb216c795675 Mon Sep 17 00:00:00 2001 From: benesjan Date: Wed, 29 May 2024 15:24:11 +0000 Subject: [PATCH 1/4] refactor: nuking broadcast param --- .../aztec-nr/aztec/src/note/lifecycle.nr | 5 +--- .../aztec/src/state_vars/private_immutable.nr | 3 +- .../aztec/src/state_vars/private_mutable.nr | 28 ++++--------------- .../src/state_vars/private_mutable/test.nr | 3 +- .../aztec/src/state_vars/private_set.nr | 3 +- .../src/easy_private_uint.nr | 9 ++---- .../aztec-nr/value-note/src/utils.nr | 7 +---- .../app_subscription_contract/src/main.nr | 11 ++------ .../contracts/card_game_contract/src/cards.nr | 2 +- .../contracts/child_contract/src/main.nr | 2 +- .../crowdfunding_contract/src/main.nr | 2 +- .../delegated_on_contract/src/main.nr | 2 +- .../docs_example_contract/src/main.nr | 14 +++++----- .../ecdsa_account_contract/src/main.nr | 2 +- .../contracts/escrow_contract/src/main.nr | 2 +- .../inclusion_proofs_contract/src/main.nr | 2 +- .../pending_note_hashes_contract/src/main.nr | 8 +++--- .../schnorr_account_contract/src/main.nr | 2 +- .../static_child_contract/src/main.nr | 4 +-- .../contracts/test_contract/src/main.nr | 4 +-- .../src/types/balances_map.nr | 2 +- .../token_contract/src/types/balances_map.nr | 2 +- .../src/e2e_non_contract_account.test.ts | 2 ++ 23 files changed, 41 insertions(+), 80 deletions(-) diff --git a/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr b/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr index df5d98f63c3f..376da2650b3a 100644 --- a/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr +++ b/noir-projects/aztec-nr/aztec/src/note/lifecycle.nr @@ -10,7 +10,6 @@ pub fn create_note( context: &mut PrivateContext, storage_slot: Field, note: &mut Note, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) where Note: NoteInterface { @@ -38,9 +37,7 @@ pub fn create_note( context.push_new_note_hash(inner_note_hash); - if broadcast { - Note::broadcast(*note, context, storage_slot, ovpk_m, ivpk_m); - } + Note::broadcast(*note, context, storage_slot, ovpk_m, ivpk_m); } pub fn create_note_hash_from_public( diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr b/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr index e257a307ad8c..3c70465a8d8b 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/private_immutable.nr @@ -47,7 +47,6 @@ impl PrivateImmutable { pub fn initialize( self, note: &mut Note, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) where Note: NoteInterface { @@ -55,7 +54,7 @@ impl PrivateImmutable { let nullifier = self.compute_initialization_nullifier(); self.context.push_new_nullifier(nullifier, 0); - create_note(self.context, self.storage_slot, note, broadcast, ovpk_m, ivpk_m); + create_note(self.context, self.storage_slot, note, ovpk_m, ivpk_m); } // docs:end:initialize diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr b/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr index cbd2b0e5cbf9..9e7934c3bf1d 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable.nr @@ -51,7 +51,6 @@ impl PrivateMutable { pub fn initialize( self, note: &mut Note, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) where Note: NoteInterface { @@ -59,7 +58,7 @@ impl PrivateMutable { let nullifier = self.compute_initialization_nullifier(); self.context.push_new_nullifier(nullifier, 0); - create_note(self.context, self.storage_slot, note, broadcast, ovpk_m, ivpk_m); + create_note(self.context, self.storage_slot, note, ovpk_m, ivpk_m); } // docs:end:initialize @@ -67,7 +66,6 @@ impl PrivateMutable { pub fn replace( self, new_note: &mut Note, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) where Note: NoteInterface { @@ -77,21 +75,13 @@ impl PrivateMutable { destroy_note(self.context, prev_note); // Add replacement note. - create_note( - self.context, - self.storage_slot, - new_note, - broadcast, - ovpk_m, - ivpk_m - ); + create_note(self.context, self.storage_slot, new_note, ovpk_m, ivpk_m); } // docs:end:replace pub fn initialize_or_replace( self, note: &mut Note, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) where Note: NoteInterface { @@ -108,16 +98,15 @@ impl PrivateMutable { // This means that an honest oracle will assist the prover to produce a valid proof, while a malicious oracle // (i.e. one that returns an incorrect value for is_initialized) will simply fail to produce a proof. if (!is_initialized) { - self.initialize(note, broadcast, ovpk_m, ivpk_m); + self.initialize(note, ovpk_m, ivpk_m); } else { - self.replace(note, broadcast, ovpk_m, ivpk_m) + self.replace(note, ovpk_m, ivpk_m) } } // docs:start:get_note pub fn get_note( self, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) -> Note where Note: NoteInterface { @@ -128,14 +117,7 @@ impl PrivateMutable { // Add the same note again. // Because a nonce is added to every note in the kernel, its nullifier will be different. - create_note( - self.context, - self.storage_slot, - &mut note, - broadcast, - ovpk_m, - ivpk_m - ); + create_note(self.context, self.storage_slot, &mut note, ovpk_m, ivpk_m); note } diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable/test.nr b/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable/test.nr index 7f69a314d315..f0272af161f1 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable/test.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/private_mutable/test.nr @@ -22,13 +22,12 @@ fn test_initialize_or_replace_without_nullifier() { let ovpk_m: GrumpkinPoint = zeroed(); let ivpk_m: GrumpkinPoint = zeroed(); - let broadcast = false; let value = 42; let mut note = MockNote::new(value).contract_address(contract_address).storage_slot(storage_slot).build(); OracleMock::mock("checkNullifierExists").returns(0); - state_var.initialize_or_replace(&mut note, broadcast, ovpk_m, ivpk_m); + state_var.initialize_or_replace(&mut note, ovpk_m, ivpk_m); // Since we reported there was no nullifier, we should initialize and see the following side-effects: // - a new note being created diff --git a/noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr b/noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr index 3c90bb6562dd..3029df8bcdaf 100644 --- a/noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr +++ b/noir-projects/aztec-nr/aztec/src/state_vars/private_set.nr @@ -42,11 +42,10 @@ impl PrivateSet { pub fn insert( self, note: &mut Note, - broadcast: bool, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint ) where Note: NoteInterface { - create_note(self.context, self.storage_slot, note, broadcast, ovpk_m, ivpk_m); + create_note(self.context, self.storage_slot, note, ovpk_m, ivpk_m); } // docs:end:insert 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 7dcad5341b52..2f49acdf5787 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 @@ -31,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, true, outgoing_viewer, owner_ivpk_m); + self.set.insert(&mut addend_note, outgoing_viewer, owner_ivpk_m); // docs:end:insert } @@ -66,11 +66,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, - result_value != 0, - outgoing_viewer_ovpk_m, - owner_ivpk_m - ); + self.set.insert(&mut result_note, outgoing_viewer_ovpk_m, owner_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 252ca2e862ce..8c3de1814c58 100644 --- a/noir-projects/aztec-nr/value-note/src/utils.nr +++ b/noir-projects/aztec-nr/value-note/src/utils.nr @@ -24,12 +24,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, - amount != 0, - outgoing_viewer_ovpk_m, - recipient_ivpk_m - ); + balance.insert(&mut note, outgoing_viewer_ovpk_m, recipient_ivpk_m); } // 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 71bcaaa679ba..dd48604ea089 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 @@ -37,7 +37,7 @@ contract AppSubscription { assert(context.msg_sender().to_field() == 0); assert_current_call_valid_authwit(&mut context, user_address); - let mut note = storage.subscriptions.at(user_address).get_note(false, GrumpkinPoint::zero(), GrumpkinPoint::zero()); + let mut note = storage.subscriptions.at(user_address).get_note(GrumpkinPoint::zero(), GrumpkinPoint::zero()); assert(note.remaining_txs as u64 > 0, "you're out of txs"); note.remaining_txs -= 1; @@ -47,7 +47,7 @@ contract AppSubscription { // outgoing_viewer arg to entrypoint function is impractical and the outgoing are not so valuable here. let subscriber_ovpk_m = header.get_ovpk_m(&mut context, user_address); let subscriber_ivpk_m = header.get_ivpk_m(&mut context, user_address); - storage.subscriptions.at(user_address).replace(&mut note, true, subscriber_ovpk_m, subscriber_ivpk_m); + storage.subscriptions.at(user_address).replace(&mut note, subscriber_ovpk_m, subscriber_ivpk_m); context.set_as_fee_payer(); @@ -120,12 +120,7 @@ contract AppSubscription { let subscriber_ivpk_m = header.get_ivpk_m(&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, - true, - msg_sender_ovpk_m, - subscriber_ivpk_m - ); + storage.subscriptions.at(subscriber_address).initialize_or_replace(&mut subscription_note, msg_sender_ovpk_m, subscriber_ivpk_m); } 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 71b2237d7d2a..0f4daf039e6e 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 @@ -112,7 +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, true, msg_sender_ovpk_m, owner_ivpk_m); + self.set.insert(&mut card_note.note, 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 602dd63be859..8542d7a0ffd0 100644 --- a/noir-projects/noir-contracts/contracts/child_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/child_contract/src/main.nr @@ -57,7 +57,7 @@ contract Child { let owner_ivpk_m = header.get_ivpk_m(&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, true, msg_sender_ovpk_m, owner_ivpk_m); + storage.a_map_with_private_values.at(owner).insert(&mut note, msg_sender_ovpk_m, owner_ivpk_m); 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 38237792c284..3daa552c1009 100644 --- a/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr @@ -88,7 +88,7 @@ contract Crowdfunding { let donor_ovpk_m = header.get_ovpk_m(&mut context, donor); let donor_ivpk_m = header.get_ivpk_m(&mut context, donor); let mut note = ValueNote::new(amount as Field, donor_npk_m_hash); - storage.donation_receipts.insert(&mut note, true, donor_ovpk_m, donor_ivpk_m); + storage.donation_receipts.insert(&mut note, donor_ovpk_m, donor_ivpk_m); } // 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 3c405557d737..595cc922ffd9 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 @@ -21,7 +21,7 @@ contract DelegatedOn { let owner_ivpk_m = header.get_ivpk_m(&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, true, msg_sender_ovpk_m, owner_ivpk_m); + storage.a_map_with_private_values.at(owner).insert(&mut note, msg_sender_ovpk_m, owner_ivpk_m); 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 6ff223245f65..6930945ec8b7 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 @@ -174,7 +174,7 @@ contract DocsExample { let msg_sender_ivpk_m = header.get_ivpk_m(&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, true, msg_sender_ovpk_m, msg_sender_ivpk_m); + storage.private_immutable.initialize(&mut new_card, msg_sender_ovpk_m, msg_sender_ivpk_m); } // docs:end:initialize-private-mutable @@ -188,7 +188,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, true, msg_sender_ovpk_m, msg_sender_ivpk_m); + storage.legendary_card.initialize(&mut legendary_card, msg_sender_ovpk_m, msg_sender_ivpk_m); } #[aztec(private)] @@ -200,7 +200,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, true, msg_sender_ovpk_m, msg_sender_ivpk_m); + storage.set.insert(&mut note, msg_sender_ovpk_m, msg_sender_ivpk_m); } } @@ -212,7 +212,7 @@ contract DocsExample { let msg_sender_ivpk_m = header.get_ivpk_m(&mut context, context.msg_sender()); let mut note = CardNote::new(amount, randomness, msg_sender_npk_m_hash); - storage.set.insert(&mut note, true, msg_sender_ovpk_m, msg_sender_ivpk_m); + storage.set.insert(&mut note, msg_sender_ovpk_m, msg_sender_ivpk_m); } // docs:start:state_vars-NoteGetterOptionsComparatorExampleNoir @@ -238,7 +238,7 @@ contract DocsExample { let msg_sender_ivpk_m = header.get_ivpk_m(&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, true, msg_sender_ovpk_m, msg_sender_ivpk_m); + storage.legendary_card.replace(&mut new_card, msg_sender_ovpk_m, msg_sender_ivpk_m); DocsExample::at(context.this_address()).update_leader(context.msg_sender(), points).enqueue(&mut context); } @@ -248,7 +248,7 @@ contract DocsExample { // Also serves as a e2e test that you can `get_note()` and then `replace()` // docs:start:state_vars-PrivateMutableGet - let card = storage.legendary_card.get_note(false, GrumpkinPoint::zero(), GrumpkinPoint::zero()); + let card = storage.legendary_card.get_note(GrumpkinPoint::zero(), GrumpkinPoint::zero()); // docs:end:state_vars-PrivateMutableGet let points = card.points + 1; @@ -260,7 +260,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, true, msg_sender_ovpk_m, msg_sender_ivpk_m); + storage.legendary_card.replace(&mut new_card, msg_sender_ovpk_m, msg_sender_ivpk_m); // 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 56c655248219..2c2635acbd33 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 @@ -36,7 +36,7 @@ contract EcdsaAccount { let this_ivpk_m = header.get_ivpk_m(&mut context, this); 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, true, this_ovpk_m, this_ivpk_m); + storage.public_key.initialize(&mut pub_key_note, this_ovpk_m, this_ivpk_m); } // 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 7559371ace2d..6fbc11a494d9 100644 --- a/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr @@ -21,7 +21,7 @@ contract Escrow { let owner_ivpk_m = header.get_ivpk_m(&mut context, owner); let mut note = AddressNote::new(owner, owner_npk_m_hash); - storage.owner.initialize(&mut note, true, msg_sender_ovpk_m, owner_ivpk_m); + storage.owner.initialize(&mut note, msg_sender_ovpk_m, owner_ivpk_m); } // 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 1e303704b3c8..c7a3a9249f69 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 @@ -37,7 +37,7 @@ contract InclusionProofs { let owner_ivpk_m = header.get_ivpk_m(&mut context, owner); let mut note = ValueNote::new(value, owner_npk_m_hash); - owner_private_values.insert(&mut note, true, msg_sender_ovpk_m, owner_ivpk_m); + owner_private_values.insert(&mut note, msg_sender_ovpk_m, owner_ivpk_m); } // 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 060dad539841..e45b8a0c0fcb 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 @@ -36,7 +36,7 @@ contract PendingNoteHashes { let mut note = ValueNote::new(amount, owner_npk_m_hash); // Insert note - owner_balance.insert(&mut note, true, outgoing_viewer_ovpk_m, owner_ivpk_m); + owner_balance.insert(&mut note, outgoing_viewer_ovpk_m, owner_ivpk_m); let options = NoteGetterOptions::with_filter(filter_notes_min_sum, amount); // get note inserted above @@ -70,7 +70,7 @@ contract PendingNoteHashes { // Insert note let mut note = ValueNote::new(amount, owner_npk_m_hash); - owner_balance.insert(&mut note, true, msg_sender_ovpk_m, owner_ivpk_m); + owner_balance.insert(&mut note, msg_sender_ovpk_m, owner_ivpk_m); 0 } @@ -92,7 +92,7 @@ contract PendingNoteHashes { let mut note = ValueNote::new(amount, owner_npk_m_hash); // Insert note - owner_balance.insert(&mut note, true, outgoing_viewer_ovpk_m, owner_ivpk_m); + owner_balance.insert(&mut note, outgoing_viewer_ovpk_m, owner_ivpk_m); } // Nested/inner function to get a note and confirm it matches the expected value @@ -312,7 +312,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, true, outgoing_viewer_ovpk_m, owner_ivpk_m); + owner_balance.insert(&mut note, 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 f677b2cba5bb..1bacb629def1 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 @@ -39,7 +39,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, true, this_ovpk_m, this_ivpk_m); + storage.signing_public_key.initialize(&mut pub_key_note, this_ovpk_m, this_ivpk_m); // 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 6442b07ff86d..f94e574e7bd3 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 @@ -45,7 +45,7 @@ contract StaticChild { let msg_sender_ovpk_m = header.get_ovpk_m(&mut context, context.msg_sender()); let owner_ivpk_m = header.get_ivpk_m(&mut context, owner); let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_private_value.insert(&mut note, true, msg_sender_ovpk_m, owner_ivpk_m); + storage.a_private_value.insert(&mut note, msg_sender_ovpk_m, owner_ivpk_m); new_value } @@ -61,7 +61,7 @@ contract StaticChild { let outgoing_viewer_ovpk_m = header.get_ovpk_m(&mut context, outgoing_viewer); let owner_ivpk_m = header.get_ivpk_m(&mut context, owner); let mut note = ValueNote::new(new_value, owner_npk_m_hash); - storage.a_private_value.insert(&mut note, true, outgoing_viewer_ovpk_m, owner_ivpk_m); + storage.a_private_value.insert(&mut note, outgoing_viewer_ovpk_m, owner_ivpk_m); 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 9f8ff806a89a..e886d5ea40c7 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -102,7 +102,6 @@ contract Test { &mut context, storage_slot, &mut note, - true, outgoing_viewer_ovpk_m, owner_ivpk_m ); @@ -328,7 +327,6 @@ contract Test { &mut context, storage_slot, &mut note, - true, msg_sender_ovpk, owner_ivpk_m ); @@ -405,7 +403,7 @@ contract Test { #[aztec(private)] fn set_constant(value: Field) { let mut note = TestNote::new(value); - storage.example_constant.initialize(&mut note, false, GrumpkinPoint::zero(), GrumpkinPoint::zero()); + storage.example_constant.initialize(&mut note, GrumpkinPoint::zero(), GrumpkinPoint::zero()); } #[aztec(private)] diff --git a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr index dfc1249aacda..bee8167a299a 100644 --- a/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr +++ b/noir-projects/noir-contracts/contracts/token_blacklist_contract/src/types/balances_map.nr @@ -74,7 +74,7 @@ impl BalancesMap { let mut addend_note = T::new(addend, owner_npk_m_hash); // docs:start:insert - self.map.at(owner).insert(&mut addend_note, true, outgoing_viewer_ovpk_m, owner_ivpk_m); + self.map.at(owner).insert(&mut addend_note, outgoing_viewer_ovpk_m, owner_ivpk_m); // docs:end:insert } diff --git a/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr b/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr index dfc1249aacda..bee8167a299a 100644 --- a/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr +++ b/noir-projects/noir-contracts/contracts/token_contract/src/types/balances_map.nr @@ -74,7 +74,7 @@ impl BalancesMap { let mut addend_note = T::new(addend, owner_npk_m_hash); // docs:start:insert - self.map.at(owner).insert(&mut addend_note, true, outgoing_viewer_ovpk_m, owner_ivpk_m); + self.map.at(owner).insert(&mut addend_note, outgoing_viewer_ovpk_m, owner_ivpk_m); // docs:end:insert } diff --git a/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts b/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts index 878112b259a7..90efa7d08827 100644 --- a/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts +++ b/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts @@ -81,6 +81,8 @@ describe('e2e_non_contract_account', () => { // Add the note const note = new Note([new Fr(value)]); + // We have to manually add the note because the note ciphertext is encrypted with zero keys since there + // is no account registered. const extendedNote = new ExtendedNote( note, wallet.getCompleteAddress().address, From 09747373f6f990754344412393abba5e9ef4aff7 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 30 May 2024 08:27:10 +0000 Subject: [PATCH 2/4] fixes --- boxes/boxes/react/src/contracts/src/main.nr | 4 ++-- boxes/boxes/vanilla/src/contracts/src/main.nr | 4 ++-- .../aztec-nr/aztec/src/test/mocks/mock_note.nr | 6 +++--- .../contracts/docs_example_contract/src/main.nr | 12 ++++++------ .../contracts/test_contract/src/test_note.nr | 6 +++--- 5 files changed, 16 insertions(+), 16 deletions(-) diff --git a/boxes/boxes/react/src/contracts/src/main.nr b/boxes/boxes/react/src/contracts/src/main.nr index e3665ad336f8..bfde3de6056d 100644 --- a/boxes/boxes/react/src/contracts/src/main.nr +++ b/boxes/boxes/react/src/contracts/src/main.nr @@ -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, true, owner_ovpk_m, owner_ivpk_m); + numbers.at(owner).initialize(&mut new_number, 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, true, owner_ovpk_m, owner_ivpk_m); + numbers.at(owner).replace(&mut new_number, 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 86aec3339879..4a5110946593 100644 --- a/boxes/boxes/vanilla/src/contracts/src/main.nr +++ b/boxes/boxes/vanilla/src/contracts/src/main.nr @@ -14,14 +14,14 @@ contract Vanilla { fn constructor(number: Field, owner: AztecAddress, owner_npk_m_hash: Field, owner_ovpk_m: GrumpkinPoint, owner_ivpk_m: GrumpkinPoint) { let numbers = storage.numbers; let mut new_number = ValueNote::new(number, owner_npk_m_hash); - numbers.at(owner).initialize(&mut new_number, true, owner_ovpk_m, owner_ivpk_m); + numbers.at(owner).initialize(&mut new_number, owner_ovpk_m, owner_ivpk_m); } #[aztec(private)] fn setNumber(number: Field, owner: AztecAddress, owner_npk_m_hash: Field, owner_ovpk_m: GrumpkinPoint, owner_ivpk_m: GrumpkinPoint) { let numbers = storage.numbers; let mut new_number = ValueNote::new(number, owner_npk_m_hash); - numbers.at(owner).replace(&mut new_number, true, owner_ovpk_m, owner_ivpk_m); + numbers.at(owner).replace(&mut new_number, owner_ovpk_m, owner_ivpk_m); } unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote { diff --git a/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr b/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr index 5ad60b301a0d..beb67311aee2 100644 --- a/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr +++ b/noir-projects/aztec-nr/aztec/src/test/mocks/mock_note.nr @@ -48,9 +48,9 @@ impl NoteInterface for MockNote { } fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - assert( - false, "MockNote does not support broadcast." - ); + // MockNote does not support broadcasting. Since this function gets called in various places anyway we will verify + // that the dev really did not intend to broadcast by checking that zero keys were passed in. + assert(ovpk_m.is_zero() & ivpk_m.is_zero(), "MockNote does not support broadcast."); } fn to_be_bytes(self, storage_slot: Field) -> [u8; MOCK_NOTE_BYTES_LENGTH] { 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 6930945ec8b7..1f30d2e214f9 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 @@ -247,17 +247,17 @@ contract DocsExample { // Ensure `points` > current value // Also serves as a e2e test that you can `get_note()` and then `replace()` - // docs:start:state_vars-PrivateMutableGet - let card = storage.legendary_card.get_note(GrumpkinPoint::zero(), GrumpkinPoint::zero()); - // docs:end:state_vars-PrivateMutableGet - - let points = card.points + 1; - let header = context.get_header(); let msg_sender_npk_m_hash = header.get_npk_m_hash(&mut context, context.msg_sender()); let msg_sender_ovpk_m = header.get_ovpk_m(&mut context, context.msg_sender()); let msg_sender_ivpk_m = header.get_ivpk_m(&mut context, context.msg_sender()); + // docs:start:state_vars-PrivateMutableGet + let card = storage.legendary_card.get_note(msg_sender_ovpk_m, msg_sender_ivpk_m); + // docs:end:state_vars-PrivateMutableGet + + let points = card.points + 1; + 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, msg_sender_ovpk_m, msg_sender_ivpk_m); diff --git a/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr b/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr index c58059b7050a..f5569783319b 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/test_note.nr @@ -29,9 +29,9 @@ impl NoteInterface for TestNote { } fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) { - assert( - false, "TestNote does not support broadcast. Add it to PXE directly using the `.addNote` function." - ); + // TestNote does not support broadcasting. Since this function gets called in various places anyway we will verify + // that the dev really did not intend to broadcast by checking that zero keys were passed in. + assert(ovpk_m.is_zero() & ivpk_m.is_zero(), "TestNote does not support broadcast."); } } From 456cc7715ca2e57d1a5ed6c922f4e8ad14fbcafe Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 30 May 2024 10:20:39 +0000 Subject: [PATCH 3/4] fix --- .../contracts/app_subscription_contract/src/main.nr | 11 ++++++----- .../src/logs/l1_note_payload/encryption_utils.ts | 6 ++++++ .../src/logs/l1_note_payload/l1_note_payload.ts | 7 +++++++ 3 files changed, 19 insertions(+), 5 deletions(-) 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 dd48604ea089..f3fca6cfb155 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 @@ -37,16 +37,17 @@ contract AppSubscription { assert(context.msg_sender().to_field() == 0); assert_current_call_valid_authwit(&mut context, user_address); - let mut note = storage.subscriptions.at(user_address).get_note(GrumpkinPoint::zero(), GrumpkinPoint::zero()); - assert(note.remaining_txs as u64 > 0, "you're out of txs"); - - note.remaining_txs -= 1; - let header = context.get_header(); // 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. let subscriber_ovpk_m = header.get_ovpk_m(&mut context, user_address); let subscriber_ivpk_m = header.get_ivpk_m(&mut context, user_address); + + let mut note = storage.subscriptions.at(user_address).get_note(subscriber_ovpk_m, subscriber_ivpk_m); + assert(note.remaining_txs as u64 > 0, "you're out of txs"); + + note.remaining_txs -= 1; + storage.subscriptions.at(user_address).replace(&mut note, subscriber_ovpk_m, subscriber_ivpk_m); context.set_as_fee_payer(); diff --git a/yarn-project/circuit-types/src/logs/l1_note_payload/encryption_utils.ts b/yarn-project/circuit-types/src/logs/l1_note_payload/encryption_utils.ts index 2673af92c61d..202c7e22e922 100644 --- a/yarn-project/circuit-types/src/logs/l1_note_payload/encryption_utils.ts +++ b/yarn-project/circuit-types/src/logs/l1_note_payload/encryption_utils.ts @@ -12,10 +12,16 @@ import { numToUInt8 } from '@aztec/foundation/serialize'; * @param secretKey - The secret key used to derive shared secret. * @param publicKey - The public key used to derive shared secret. * @returns A derived AES secret key. + * @throws If the public key is zero. * TODO(#5726): This function is called point_to_symmetric_key in Noir. I don't like that name much since point is not * the only input of the function. Unify naming once we have a better name. */ export function deriveAESSecret(secretKey: GrumpkinPrivateKey, publicKey: PublicKey): Buffer { + if (publicKey.isZero()) { + throw new Error( + `Attempting to derive AES secret with a zero public key. You have probably passed a zero public key in your Noir code somewhere thinking that the note won't broadcasted... but it was.`, + ); + } const curve = new Grumpkin(); const sharedSecret = curve.mul(publicKey, secretKey); const secretBuffer = Buffer.concat([sharedSecret.toBuffer(), numToUInt8(GeneratorIndex.SYMMETRIC_KEY)]); diff --git a/yarn-project/circuit-types/src/logs/l1_note_payload/l1_note_payload.ts b/yarn-project/circuit-types/src/logs/l1_note_payload/l1_note_payload.ts index b5a4c31d9269..89551558d737 100644 --- a/yarn-project/circuit-types/src/logs/l1_note_payload/l1_note_payload.ts +++ b/yarn-project/circuit-types/src/logs/l1_note_payload/l1_note_payload.ts @@ -89,8 +89,15 @@ export class L1NotePayload { * @param ivpk - The incoming viewing public key of the recipient * @param ovKeys - The outgoing viewing keys of the sender * @returns A buffer containing the encrypted log payload + * @throws If the ivpk is zero. */ public encrypt(ephSk: GrumpkinPrivateKey, recipient: AztecAddress, ivpk: PublicKey, ovKeys: KeyValidationRequest) { + if (ivpk.isZero()) { + throw new Error( + `Attempting to encrypt with a zero ivpk. You have probably passed a zero value in your Noir code somewhere thinking that the note won't broadcasted... but it was.`, + ); + } + const ephPk = derivePublicKeyFromSecretKey(ephSk); const header = new EncryptedLogHeader(this.contractAddress); From 283507d030e523b4fba77ccd3387ac56925d0654 Mon Sep 17 00:00:00 2001 From: benesjan Date: Thu, 30 May 2024 12:18:07 +0000 Subject: [PATCH 4/4] better comments --- .../noir-contracts/contracts/test_contract/src/main.nr | 3 +++ yarn-project/end-to-end/src/e2e_non_contract_account.test.ts | 3 +-- 2 files changed, 4 insertions(+), 2 deletions(-) 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 e886d5ea40c7..344adb9491ba 100644 --- a/noir-projects/noir-contracts/contracts/test_contract/src/main.nr +++ b/noir-projects/noir-contracts/contracts/test_contract/src/main.nr @@ -403,6 +403,9 @@ contract Test { #[aztec(private)] fn set_constant(value: Field) { let mut note = TestNote::new(value); + // The test note is not being broadcasted (TestNote::broadcast(...) does not actually broadcast) and for this + // reason we are passing zero values for the public keys below. This means that if we want to interact with + // this note in the future, we will need to manually add it to PXE by calling pxe.addNote(...). storage.example_constant.initialize(&mut note, GrumpkinPoint::zero(), GrumpkinPoint::zero()); } diff --git a/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts b/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts index 90efa7d08827..1728f637e1e7 100644 --- a/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts +++ b/yarn-project/end-to-end/src/e2e_non_contract_account.test.ts @@ -81,8 +81,7 @@ describe('e2e_non_contract_account', () => { // Add the note const note = new Note([new Fr(value)]); - // We have to manually add the note because the note ciphertext is encrypted with zero keys since there - // is no account registered. + // We have to manually add the note because the note was not broadcasted. const extendedNote = new ExtendedNote( note, wallet.getCompleteAddress().address,