Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: nuking encryption oracles #8817

Merged
merged 3 commits into from
Sep 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
12 changes: 0 additions & 12 deletions noir-projects/aztec-nr/aztec/src/oracle/encryption.nr

This file was deleted.

103 changes: 10 additions & 93 deletions noir-projects/aztec-nr/aztec/src/oracle/logs.nr
Original file line number Diff line number Diff line change
@@ -1,116 +1,33 @@
use dep::protocol_types::{address::AztecAddress, point::Point};
use dep::protocol_types::address::AztecAddress;

// = 480 + 32 * N bytes
#[oracle(emitEncryptedNoteLog)]
unconstrained fn emit_encrypted_note_log_oracle<let M: u32>(_note_hash_counter: u32, _encrypted_note: [u8; M], _counter: u32) {}

unconstrained pub fn emit_encrypted_note_log<let M: u32>(
note_hash_counter: u32,
encrypted_note: [u8; M],
counter: u32
) {
unconstrained pub fn emit_encrypted_note_log<let M: u32>(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<let M: u32>(_contract_address: AztecAddress, _randomness: Field, _encrypted_event: [u8; M], _counter: u32) {}

unconstrained pub fn emit_encrypted_event_log<let M: u32>(
contract_address: AztecAddress,
randomness: Field,
encrypted_event: [u8; M],
counter: u32
) {
emit_encrypted_event_log_oracle(contract_address, randomness, encrypted_event, counter)
}

// = 480 + 32 * N bytes
#[oracle(computeEncryptedNoteLog)]
unconstrained fn compute_encrypted_note_log_oracle<let N: u32, let M: u32>(
_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<let N: u32, let M: u32>(
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<let N: u32, let M: u32>(
unconstrained fn emit_encrypted_event_log_oracle<let M: u32>(
_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] {}
_encrypted_event: [u8; M],
_counter: u32
) {}

unconstrained pub fn compute_encrypted_event_log<let N: u32, let M: u32>(
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
)
unconstrained pub fn emit_encrypted_event_log<let M: u32>(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<T>(_contract_address: AztecAddress, _message: T, _counter: u32) -> Field {}

unconstrained pub fn emit_unencrypted_log_private_internal<T>(
contract_address: AztecAddress,
message: T,
counter: u32
) -> Field {
unconstrained pub fn emit_unencrypted_log_private_internal<T>(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<let N: u32>(
contract_address: AztecAddress,
message: [Field; N],
counter: u32
) -> Field {}
unconstrained fn emit_contract_class_unencrypted_log_private<let N: u32>(contract_address: AztecAddress, message: [Field; N], counter: u32) -> Field {}

unconstrained pub fn emit_contract_class_unencrypted_log_private_internal<let N: u32>(contract_address: AztecAddress, message: [Field; N], counter: u32) -> Field {
emit_contract_class_unencrypted_log_private(contract_address, message, counter)
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -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;
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
Loading
Loading