Skip to content

Commit

Permalink
feat: removing redundant key fetching (#8043)
Browse files Browse the repository at this point in the history
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**.
  • Loading branch information
benesjan authored and AztecBot committed Aug 21, 2024
1 parent 5fb740f commit 55423e4
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
30 changes: 23 additions & 7 deletions easy-private-state/src/easy_private_uint.nr
Original file line number Diff line number Diff line change
@@ -1,7 +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,
encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys,
keys::getters::get_current_public_keys
};
use dep::value_note::{filter::filter_notes_min_sum, value_note::ValueNote};
Expand All @@ -24,19 +24,28 @@ impl<Context> EasyPrivateUint<Context> {
impl<Context> EasyPrivateUint<&mut PrivateContext> {
// Very similar to `value_note::utils::increment`.
pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let owner_npk_m_hash = get_current_public_keys(self.context, owner).npk_m.hash();
let owner_keys = get_current_public_keys(self.context, owner);
let outgoing_viewer_keys = get_current_public_keys(self.context, outgoing_viewer);
// Creates new note for the owner.
let mut addend_note = ValueNote::new(addend as Field, owner_npk_m_hash);
let mut addend_note = ValueNote::new(addend as Field, owner_keys.npk_m.hash());

// 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(self.context, outgoing_viewer, owner));
self.set.insert(&mut addend_note).emit(
encode_and_encrypt_note_with_keys(
self.context,
outgoing_viewer_keys.ovpk_m,
owner_keys.ivpk_m,
owner
)
);
// docs:end:insert
}

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

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

// Creates change note for the owner.
let result_value = minuend - subtrahend;
let mut result_note = ValueNote::new(result_value as Field, owner_npk_m_hash);
self.set.insert(&mut result_note).emit(encode_and_encrypt_note(self.context, outgoing_viewer, owner));
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(
self.context,
outgoing_viewer_keys.ovpk_m,
owner_keys.ivpk_m,
owner
)
);
}
}
16 changes: 12 additions & 4 deletions value-note/src/utils.nr
Original file line number Diff line number Diff line change
@@ -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;
use dep::aztec::encrypted_logs::encrypted_note_emission::encode_and_encrypt_note_with_keys;
use dep::aztec::keys::getters::get_current_public_keys;
use crate::{filter::filter_notes_min_sum, value_note::{ValueNote, VALUE_NOTE_LEN, VALUE_NOTE_BYTES_LEN}};

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

let mut note = ValueNote::new(amount, recipient_npk_m_hash);
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(balance.context, outgoing_viewer, recipient));
balance.insert(&mut note).emit(
encode_and_encrypt_note_with_keys(
balance.context,
outgoing_viewer_ovpk_m,
recipient_keys.ivpk_m,
recipient
)
);
}

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

0 comments on commit 55423e4

Please sign in to comment.