Skip to content

Commit 2bbcc7b

Browse files
authored
feat: removing redundant key fetching (#8043)
Fixes #7954 I replaced the use of `encode_and_encrypt_note` with `encode_and_encrypt_note_with_keys` in most of the places as it allowed for reusing the obtained keys. Note that there is only 1 legimate place remaining where it made sense to keep on using `encode_and_encrypt_note` and that is [here](https://github.com/AztecProtocol/aztec-packages/blob/34ae51df5d45973deb3408075a50070c781f7a48/noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr#L47). All the other places are either test contracts or the token blacklist contract which is very outdated by now and hence it didn't seem to be worth it to update it. Given this I think we should nuke `encode_and_encrypt_note` to keep the API simpler and to make devs write efficient code. Does the reviewer agree? (possibly also `encode_and_encrypt_event`) Token::transfer(...) gates before were 49296 and after 38903. Diff of **10393 gates**.
1 parent 4c568b0 commit 2bbcc7b

File tree

12 files changed

+100
-70
lines changed

12 files changed

+100
-70
lines changed

noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr

+23-7
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use dep::aztec::{
22
context::PrivateContext, protocol_types::{address::AztecAddress},
33
note::note_getter_options::NoteGetterOptions, state_vars::PrivateSet,
4-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
4+
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
55
keys::getters::get_current_public_keys
66
};
77
use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote};
@@ -24,19 +24,28 @@ impl<Context> EasyPrivateUint<Context> {
2424
impl<Context> EasyPrivateUint<&mut PrivateContext> {
2525
// Very similar to `value_note::utils::increment`.
2626
pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
27-
let owner_npk_m_hash = get_current_public_keys(self.context, owner).npk_m.hash();
27+
let owner_keys = get_current_public_keys(self.context, owner);
28+
let outgoing_viewer_keys = get_current_public_keys(self.context, outgoing_viewer);
2829
// Creates new note for the owner.
29-
let mut addend_note = ValueNote::new(addend as Field, owner_npk_m_hash);
30+
let mut addend_note = ValueNote::new(addend as Field, owner_keys.npk_m.hash());
3031

3132
// Insert the new note to the owner's set of notes.
3233
// docs:start:insert
33-
self.set.insert(&mut addend_note).emit(encode_and_encrypt_note(self.context, outgoing_viewer, owner));
34+
self.set.insert(&mut addend_note).emit(
35+
encode_and_encrypt_note_with_keys(
36+
self.context,
37+
outgoing_viewer_keys.ovpk_m,
38+
owner_keys.ivpk_m,
39+
owner
40+
)
41+
);
3442
// docs:end:insert
3543
}
3644

3745
// Very similar to `value_note::utils::decrement`.
3846
pub fn sub(self, subtrahend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
39-
let owner_npk_m_hash = get_current_public_keys(self.context, owner).npk_m.hash();
47+
let owner_keys = get_current_public_keys(self.context, owner);
48+
let outgoing_viewer_keys = get_current_public_keys(self.context, outgoing_viewer);
4049

4150
// docs:start:pop_notes
4251
let options = NoteGetterOptions::with_filter(filter_notes_min_sum, subtrahend as Field);
@@ -56,7 +65,14 @@ impl<Context> EasyPrivateUint<&mut PrivateContext> {
5665

5766
// Creates change note for the owner.
5867
let result_value = minuend - subtrahend;
59-
let mut result_note = ValueNote::new(result_value as Field, owner_npk_m_hash);
60-
self.set.insert(&mut result_note).emit(encode_and_encrypt_note(self.context, outgoing_viewer, owner));
68+
let mut result_note = ValueNote::new(result_value as Field, owner_keys.npk_m.hash());
69+
self.set.insert(&mut result_note).emit(
70+
encode_and_encrypt_note_with_keys(
71+
self.context,
72+
outgoing_viewer_keys.ovpk_m,
73+
owner_keys.ivpk_m,
74+
owner
75+
)
76+
);
6177
}
6278
}

noir-projects/aztec-nr/value-note/src/utils.nr

+12-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use dep::aztec::prelude::{AztecAddress, PrivateContext, PrivateSet, NoteGetterOptions};
22
use dep::aztec::note::note_getter_options::SortOrder;
3-
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note;
3+
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
44
use dep::aztec::keys::getters::get_current_public_keys;
55
use crate::{filter::filter_notes_min_sum, value_note::{ValueNote, VALUE_NOTE_LEN, VALUE_NOTE_BYTES_LEN}};
66

@@ -19,11 +19,19 @@ pub fn increment(
1919
recipient: AztecAddress,
2020
outgoing_viewer: AztecAddress // docs:end:increment_args
2121
) {
22-
let recipient_npk_m_hash = get_current_public_keys(balance.context, recipient).npk_m.hash();
22+
let recipient_keys = get_current_public_keys(balance.context, recipient);
23+
let outgoing_viewer_ovpk_m = get_current_public_keys(balance.context, outgoing_viewer).ovpk_m;
2324

24-
let mut note = ValueNote::new(amount, recipient_npk_m_hash);
25+
let mut note = ValueNote::new(amount, recipient_keys.npk_m.hash());
2526
// Insert the new note to the owner's set of notes and emit the log if value is non-zero.
26-
balance.insert(&mut note).emit(encode_and_encrypt_note(balance.context, outgoing_viewer, recipient));
27+
balance.insert(&mut note).emit(
28+
encode_and_encrypt_note_with_keys(
29+
balance.context,
30+
outgoing_viewer_ovpk_m,
31+
recipient_keys.ivpk_m,
32+
recipient
33+
)
34+
);
2735
}
2836

2937
// Find some of the `owner`'s notes whose values add up to the `amount`.

noir-projects/noir-contracts/contracts/app_subscription_contract/src/main.nr

+16-13
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ contract AppSubscription {
99
AztecAddress, FunctionSelector, PrivateContext, NoteHeader, Map, PrivateMutable, PublicMutable,
1010
SharedImmutable
1111
},
12-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
12+
encrypted_logs::encrypted_note_emission::{encode_and_encrypt_note, encode_and_encrypt_note_with_keys},
1313
keys::getters::get_current_public_keys, protocol_types::constants::MAX_FIELD_VALUE
1414
};
1515
use authwit::{auth_witness::get_auth_witness, auth::assert_current_call_valid_authwit};
@@ -92,12 +92,7 @@ contract AppSubscription {
9292
}
9393

9494
#[aztec(private)]
95-
fn subscribe(
96-
subscriber_address: AztecAddress,
97-
nonce: Field,
98-
expiry_block_number: Field,
99-
tx_count: Field
100-
) {
95+
fn subscribe(subscriber: AztecAddress, nonce: Field, expiry_block_number: Field, tx_count: Field) {
10196
assert(tx_count as u64 <= SUBSCRIPTION_TXS as u64);
10297

10398
Token::at(storage.subscription_token_address.read_private()).transfer_from(
@@ -109,13 +104,21 @@ contract AppSubscription {
109104

110105
// Assert that the given expiry_block_number < current_block_number + SUBSCRIPTION_DURATION_IN_BLOCKS.
111106
AppSubscription::at(context.this_address()).assert_block_number(expiry_block_number).enqueue_view(&mut context);
112-
let subscriber_npk_m_hash = get_current_public_keys(&mut context, subscriber_address).npk_m.hash();
113-
114-
let mut subscription_note = SubscriptionNote::new(subscriber_npk_m_hash, expiry_block_number, tx_count);
115-
storage.subscriptions.at(subscriber_address).initialize_or_replace(&mut subscription_note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), subscriber_address));
107+
let subscriber_keys = get_current_public_keys(&mut context, subscriber);
108+
let msg_sender_ovpk_m = get_current_public_keys(&mut context, context.msg_sender()).ovpk_m;
109+
110+
let mut subscription_note = SubscriptionNote::new(subscriber_keys.npk_m.hash(), expiry_block_number, tx_count);
111+
storage.subscriptions.at(subscriber).initialize_or_replace(&mut subscription_note).emit(
112+
encode_and_encrypt_note_with_keys(
113+
&mut context,
114+
msg_sender_ovpk_m,
115+
subscriber_keys.ivpk_m,
116+
subscriber
117+
)
118+
);
116119
}
117120

118-
unconstrained fn is_initialized(subscriber_address: AztecAddress) -> pub bool {
119-
storage.subscriptions.at(subscriber_address).is_initialized()
121+
unconstrained fn is_initialized(subscriber: AztecAddress) -> pub bool {
122+
storage.subscriptions.at(subscriber).is_initialized()
120123
}
121124
}

noir-projects/noir-contracts/contracts/child_contract/src/main.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ contract Child {
55
use dep::aztec::{
66
context::gas::GasOpts, protocol_types::{abis::call_context::CallContext},
77
note::{note_getter_options::NoteGetterOptions, note_header::NoteHeader},
8-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
8+
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
99
keys::getters::get_current_public_keys
1010
};
1111
use dep::value_note::value_note::ValueNote;
@@ -52,10 +52,10 @@ contract Child {
5252

5353
#[aztec(private)]
5454
fn private_set_value(new_value: Field, owner: AztecAddress) -> Field {
55-
let owner_npk_m_hash = get_current_public_keys(&mut context, owner).npk_m.hash();
55+
let owner_keys = get_current_public_keys(&mut context, owner);
5656

57-
let mut note = ValueNote::new(new_value, owner_npk_m_hash);
58-
storage.a_map_with_private_values.at(owner).insert(&mut note).emit(encode_and_encrypt_note(&mut context, owner, owner));
57+
let mut note = ValueNote::new(new_value, owner_keys.npk_m.hash());
58+
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));
5959
new_value
6060
}
6161

noir-projects/noir-contracts/contracts/crowdfunding_contract/src/main.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ contract Crowdfunding {
55
// docs:start:all-deps
66
use dep::aztec::{
77
protocol_types::address::AztecAddress,
8-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
8+
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
99
keys::getters::get_current_public_keys,
1010
state_vars::{PrivateSet, PublicImmutable, SharedImmutable}
1111
};
@@ -79,11 +79,11 @@ contract Crowdfunding {
7979

8080
// 3) Create a value note for the donor so that he can later on claim a rewards token in the Claim
8181
// contract by proving that the hash of this note exists in the note hash tree.
82-
let donor_npk_m_hash = get_current_public_keys(&mut context, donor).npk_m.hash();
82+
let donor_keys = get_current_public_keys(&mut context, donor);
8383
// docs:start:valuenote_new
84-
let mut note = ValueNote::new(amount as Field, donor_npk_m_hash);
84+
let mut note = ValueNote::new(amount as Field, donor_keys.npk_m.hash());
8585
// docs:end:valuenote_new
86-
storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note(&mut context, donor, donor));
86+
storage.donation_receipts.insert(&mut note).emit(encode_and_encrypt_note_with_keys(&mut context, donor_keys.ovpk_m, donor_keys.ivpk_m, donor));
8787
}
8888
// docs:end:donate
8989

noir-projects/noir-contracts/contracts/ecdsa_k_account_contract/src/main.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
contract EcdsaKAccount {
44
use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, NoteGetterOptions, PrivateContext, PrivateImmutable};
55
use dep::aztec::{
6-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
6+
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
77
keys::getters::get_current_public_keys
88
};
99

@@ -27,13 +27,13 @@ contract EcdsaKAccount {
2727
#[aztec(initializer)]
2828
fn constructor(signing_pub_key_x: [u8; 32], signing_pub_key_y: [u8; 32]) {
2929
let this = context.this_address();
30-
let this_npk_m_hash = get_current_public_keys(&mut context, this).npk_m.hash();
30+
let this_keys = get_current_public_keys(&mut context, this);
3131
// Not emitting outgoing for msg_sender here to not have to register keys for the contract through which we
3232
// deploy this (typically MultiCallEntrypoint). I think it's ok here as I feel the outgoing here is not that
3333
// important.
3434

35-
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_npk_m_hash);
36-
storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this, this));
35+
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_keys.npk_m.hash());
36+
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));
3737
}
3838

3939
// Note: If you globally change the entrypoint signature don't forget to update default_entrypoint.ts

noir-projects/noir-contracts/contracts/ecdsa_r_account_contract/src/main.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
contract EcdsaRAccount {
33
use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, NoteGetterOptions, PrivateContext, PrivateImmutable};
44
use dep::aztec::{
5-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
5+
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
66
keys::getters::get_current_public_keys
77
};
88

@@ -26,13 +26,13 @@ contract EcdsaRAccount {
2626
#[aztec(initializer)]
2727
fn constructor(signing_pub_key_x: [u8; 32], signing_pub_key_y: [u8; 32]) {
2828
let this = context.this_address();
29-
let this_npk_m_hash = get_current_public_keys(&mut context, this).npk_m.hash();
29+
let this_keys = get_current_public_keys(&mut context, this);
3030
// Not emitting outgoing for msg_sender here to not have to register keys for the contract through which we
3131
// deploy this (typically MultiCallEntrypoint). I think it's ok here as I feel the outgoing here is not that
3232
// important.
3333

34-
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_npk_m_hash);
35-
storage.public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this, this));
34+
let mut pub_key_note = EcdsaPublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_keys.npk_m.hash());
35+
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));
3636
}
3737

3838
// Note: If you globally change the entrypoint signature don't forget to update default_entrypoint.ts

noir-projects/noir-contracts/contracts/escrow_contract/src/main.nr

+7-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
contract Escrow {
33
use dep::aztec::prelude::{AztecAddress, EthAddress, FunctionSelector, NoteHeader, PrivateContext, PrivateImmutable};
44
use dep::aztec::{
5-
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note,
5+
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
66
keys::getters::get_current_public_keys
77
};
88

@@ -20,11 +20,14 @@ contract Escrow {
2020
#[aztec(private)]
2121
#[aztec(initializer)]
2222
fn constructor(owner: AztecAddress) {
23-
let owner_npk_m_hash = get_current_public_keys(&mut context, owner).npk_m.hash();
23+
let owner_keys = get_current_public_keys(&mut context, owner);
24+
let msg_sender_keys = get_current_public_keys(&mut context, context.msg_sender());
2425
// docs:start:addressnote_new
25-
let mut note = AddressNote::new(owner, owner_npk_m_hash);
26+
let mut note = AddressNote::new(owner, owner_keys.npk_m.hash());
2627
// docs:end:addressnote_new
27-
storage.owner.initialize(&mut note).emit(encode_and_encrypt_note(&mut context, context.msg_sender(), owner));
28+
storage.owner.initialize(&mut note).emit(
29+
encode_and_encrypt_note_with_keys(&mut context, msg_sender_keys.ovpk_m, owner_keys.ivpk_m, owner)
30+
);
2831
}
2932

3033
// Withdraws balance. Requires that msg.sender is the owner.

noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr

-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,6 @@ contract PendingNoteHashes {
5959

6060
assert(notes.len() == 0);
6161

62-
let header = context.get_header();
6362
let owner_npk_m_hash = get_current_public_keys(&mut context, owner).npk_m.hash();
6463

6564
// Insert note

noir-projects/noir-contracts/contracts/schnorr_account_contract/src/main.nr

+4-4
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ contract SchnorrAccount {
66
use dep::std;
77

88
use dep::aztec::prelude::{AztecAddress, FunctionSelector, NoteHeader, PrivateContext, PrivateImmutable};
9-
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note;
9+
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
1010
use dep::authwit::{
1111
entrypoint::{app::AppPayload, fee::FeePayload}, account::AccountActions,
1212
auth_witness::get_auth_witness, auth::{compute_authwit_nullifier, compute_authwit_message_hash}
@@ -28,14 +28,14 @@ contract SchnorrAccount {
2828
#[aztec(initializer)]
2929
fn constructor(signing_pub_key_x: Field, signing_pub_key_y: Field) {
3030
let this = context.this_address();
31-
let this_npk_m_hash = get_current_public_keys(&mut context, this).npk_m.hash();
31+
let this_keys = get_current_public_keys(&mut context, this);
3232
// Not emitting outgoing for msg_sender here to not have to register keys for the contract through which we
3333
// deploy this (typically MultiCallEntrypoint). I think it's ok here as I feel the outgoing here is not that
3434
// important.
3535

3636
// docs:start:initialize
37-
let mut pub_key_note = PublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_npk_m_hash);
38-
storage.signing_public_key.initialize(&mut pub_key_note).emit(encode_and_encrypt_note(&mut context, this, this));
37+
let mut pub_key_note = PublicKeyNote::new(signing_pub_key_x, signing_pub_key_y, this_keys.npk_m.hash());
38+
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));
3939
// docs:end:initialize
4040
}
4141

0 commit comments

Comments
 (0)