Skip to content

Commit

Permalink
refactor: nuking encode_and_encrypt_note(...) (#8815)
Browse files Browse the repository at this point in the history
`encode_and_encrypt_note(...)` was not was not used very much as it was impractical for use when you want to produce efficient circuits (it commonly led to devs fetching the same keys twice: once for note, once inside the func). This PR nukes it and renames `encode_and_encrypt_note_with_keys` as `encode_and_encrypt_note`.
  • Loading branch information
benesjan authored and Rumata888 committed Sep 27, 2024
1 parent e4b7817 commit 73427b8
Show file tree
Hide file tree
Showing 24 changed files with 244 additions and 157 deletions.
6 changes: 3 additions & 3 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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]
Expand All @@ -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 {
Expand Down
6 changes: 3 additions & 3 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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]
Expand All @@ -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 {
Expand Down
13 changes: 13 additions & 0 deletions docs/docs/migration_notes.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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};
Expand Down Expand Up @@ -42,41 +42,6 @@ unconstrained fn compute_raw_note_log_unconstrained<Note, let N: u32>(
}

pub fn encode_and_encrypt_note<Note, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N> {
| e: NoteEmission<Note> | {
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<Note, let N: u32>(
context: &mut PrivateContext,
ov: AztecAddress,
iv: AztecAddress
) -> fn[(AztecAddress, AztecAddress, &mut PrivateContext)](NoteEmission<Note>) -> () where Note: NoteInterface<N> {
| e: NoteEmission<Note> | {
// 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<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
Expand All @@ -90,7 +55,7 @@ pub fn encode_and_encrypt_note_with_keys<Note, let N: u32>(
}
}

pub fn encode_and_encrypt_note_with_keys_unconstrained<Note, let N: u32>(
pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
ivpk: IvpkM,
Expand Down
Original file line number Diff line number Diff line change
@@ -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};

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions noir-projects/aztec-nr/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_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}};

Expand All @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -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,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}}
};
Expand Down Expand Up @@ -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
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}},
Expand Down Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
Loading

0 comments on commit 73427b8

Please sign in to comment.