Skip to content

Commit

Permalink
refactor: add sender to encode and encrypt (#9562)
Browse files Browse the repository at this point in the history
This PR is purely plumbing, as we add a sender to encode_and_encrypt,
changing the API because in the future we will require the sender as a
param to request the shared secret from the PXE that will be used to
compute the tag.

We are using a placeholder of `msg_sender` and trying to infer from
context who the sender should be whenever possible.

For the fee / partial note stuff, it may be good to have a better
understanding who the sender is.

Finally, some patterns arise, namely that we should probably add self as
an implicit member of the address book :D.

To note: this pollutes the API even more, and is beyond the standard of
sane. as discussed on Slack, we should abstract this pollution wherever
possible.
  • Loading branch information
sklppy88 authored Oct 31, 2024
1 parent 6526069 commit 8ce6834
Show file tree
Hide file tree
Showing 30 changed files with 183 additions and 55 deletions.
4 changes: 2 additions & 2 deletions boxes/boxes/react/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract BoxReact {
let mut new_number = ValueNote::new(number, owner);

let owner_ovpk_m = get_public_keys(owner).ovpk_m;
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner, context.msg_sender()));
}

#[private]
Expand All @@ -38,7 +38,7 @@ contract BoxReact {
let mut new_number = ValueNote::new(number, owner);

let owner_ovpk_m = get_public_keys(owner).ovpk_m;
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner, context.msg_sender()));
}

unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote {
Expand Down
4 changes: 2 additions & 2 deletions boxes/boxes/vanilla/src/contracts/src/main.nr
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ contract Vanilla {
let mut new_number = ValueNote::new(number, owner);

let owner_ovpk_m = get_public_keys(owner).ovpk_m;
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
numbers.at(owner).initialize(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner, context.msg_sender()));
}

#[private]
Expand All @@ -38,7 +38,7 @@ contract Vanilla {
let mut new_number = ValueNote::new(number, owner);

let owner_ovpk_m = get_public_keys(owner).ovpk_m;
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner));
numbers.at(owner).replace(&mut new_number).emit(encode_and_encrypt_note(&mut context, owner_ovpk_m, owner, context.msg_sender()));
}

unconstrained fn getNumber(owner: AztecAddress) -> pub ValueNote {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ fn compute_payload_and_hash<Event, let N: u32>(
ovsk_app: Field,
ovpk: OvpkM,
recipient: AztecAddress,
sender: AztecAddress,
) -> ([u8; 384 + N * 32], Field)
where
Event: EventInterface<N>,
Expand All @@ -25,6 +26,7 @@ where
ovsk_app,
ovpk,
recipient,
sender,
plaintext,
false,
);
Expand All @@ -38,19 +40,29 @@ unconstrained fn compute_payload_and_hash_unconstrained<Event, let N: u32>(
randomness: Field,
ovpk: OvpkM,
recipient: AztecAddress,
sender: AztecAddress,
) -> ([u8; 384 + N * 32], Field)
where
Event: EventInterface<N>,
{
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_payload_and_hash(context, event, randomness, ovsk_app, ovpk, recipient)
compute_payload_and_hash(
context,
event,
randomness,
ovsk_app,
ovpk,
recipient,
sender,
)
}

pub fn encode_and_encrypt_event<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](Event) -> ()
sender: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
Expand All @@ -62,7 +74,7 @@ where
let randomness = unsafe { random() };
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
let (encrypted_log, log_hash) =
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, recipient);
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, recipient, sender);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
}
Expand All @@ -71,7 +83,8 @@ pub fn encode_and_encrypt_event_unconstrained<Event, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](Event) -> ()
sender: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
Expand All @@ -82,7 +95,7 @@ where
// value generation.
let randomness = unsafe { random() };
let (encrypted_log, log_hash) = unsafe {
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, recipient)
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, recipient, sender)
};
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
Expand All @@ -96,14 +109,15 @@ pub fn encode_and_encrypt_event_with_randomness<Event, let N: u32>(
randomness: Field,
ovpk: OvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, Field, AztecAddress)](Event) -> ()
sender: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, Field, AztecAddress, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
|e: Event| {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());
let (encrypted_log, log_hash) =
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, recipient);
compute_payload_and_hash(*context, e, randomness, ovsk_app, ovpk, recipient, sender);
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
}
Expand All @@ -113,7 +127,8 @@ pub fn encode_and_encrypt_event_with_randomness_unconstrained<Event, let N: u32>
randomness: Field,
ovpk: OvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, Field, OvpkM, AztecAddress)](Event) -> ()
sender: AztecAddress,
) -> fn[(&mut PrivateContext, Field, OvpkM, AztecAddress, AztecAddress)](Event) -> ()
where
Event: EventInterface<N>,
{
Expand All @@ -133,7 +148,7 @@ where
// return the log from this function to the app, otherwise it could try to do stuff with it and then that might
// be wrong.
let (encrypted_log, log_hash) = unsafe {
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, recipient)
compute_payload_and_hash_unconstrained(*context, e, randomness, ovpk, recipient, sender)
};
context.emit_raw_event_log_with_masked_address(randomness, encrypted_log, log_hash);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ fn compute_payload_and_hash<Note, let N: u32>(
ovsk_app: Field,
ovpk: OvpkM,
recipient: AztecAddress,
sender: AztecAddress,
) -> (u32, [u8; 385 + N * 32], Field)
where
Note: NoteInterface<N>,
Expand All @@ -32,8 +33,15 @@ where
let plaintext = note.to_be_bytes(storage_slot);

// For note logs we always include public values prefix
let encrypted_log: [u8; 385 + N * 32] =
compute_private_log_payload(contract_address, ovsk_app, ovpk, recipient, plaintext, true);
let encrypted_log: [u8; 385 + N * 32] = compute_private_log_payload(
contract_address,
ovsk_app,
ovpk,
recipient,
sender,
plaintext,
true,
);
let log_hash = sha256_to_field(encrypted_log);

(note_hash_counter, encrypted_log, log_hash)
Expand All @@ -44,12 +52,13 @@ unconstrained fn compute_payload_and_hash_unconstrained<Note, let N: u32>(
note: Note,
ovpk: OvpkM,
recipient: AztecAddress,
sender: AztecAddress,
) -> (u32, [u8; 385 + N * 32], Field)
where
Note: NoteInterface<N>,
{
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_payload_and_hash(context, note, ovsk_app, ovpk, recipient)
compute_payload_and_hash(context, note, ovsk_app, ovpk, recipient, sender)
}

// This function seems to be affected by the following Noir bug:
Expand All @@ -59,15 +68,17 @@ pub fn encode_and_encrypt_note<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](NoteEmission<Note>) -> ()
// TODO: We need this because to compute a tagging secret, we require a sender. Should we have the tagging secret oracle take a ovpk_m as input instead of the address?
sender: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress, AztecAddress)](NoteEmission<Note>) -> ()
where
Note: NoteInterface<N>,
{
|e: NoteEmission<Note>| {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());

let (note_hash_counter, encrypted_log, log_hash) =
compute_payload_and_hash(*context, e.note, ovsk_app, ovpk, recipient);
compute_payload_and_hash(*context, e.note, ovsk_app, ovpk, recipient, sender);
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
}
Expand All @@ -76,7 +87,9 @@ pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32>(
context: &mut PrivateContext,
ovpk: OvpkM,
recipient: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress)](NoteEmission<Note>) -> ()
// TODO: We need this because to compute a tagging secret, we require a sender. Should we have the tagging secret oracle take a ovpk_m as input instead of the address?
sender: AztecAddress,
) -> fn[(&mut PrivateContext, OvpkM, AztecAddress, AztecAddress)](NoteEmission<Note>) -> ()
where
Note: NoteInterface<N>,
{
Expand All @@ -100,8 +113,9 @@ where
// for the log to be deleted when it shouldn't have (which is fine - they can already make the content be
// whatever), or cause for the log to not be deleted when it should have (which is also fine - it'll be a log
// for a note that doesn't exist).
let (note_hash_counter, encrypted_log, log_hash) =
unsafe { compute_payload_and_hash_unconstrained(*context, e.note, ovpk, recipient) };
let (note_hash_counter, encrypted_log, log_hash) = unsafe {
compute_payload_and_hash_unconstrained(*context, e.note, ovpk, recipient, sender)
};
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
}
6 changes: 6 additions & 0 deletions noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ fn compute_private_log_payload<let P: u32, let M: u32>(
ovsk_app: Field,
ovpk: OvpkM,
recipient: AztecAddress,
sender: AztecAddress,
plaintext: [u8; P],
include_public_values_prefix: bool,
) -> [u8; M] {
Expand Down Expand Up @@ -206,11 +207,16 @@ mod test {
0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c,
);

let sender = AztecAddress::from_field(
0x25afb798ea6d0b8c1618e50fdeafa463059415013d3b7c75d46abf5e242be70c,
);

let log = compute_private_log_payload(
contract_address,
ovsk_app,
ovpk_m,
recipient,
sender,
plaintext,
false,
);
Expand Down
3 changes: 2 additions & 1 deletion noir-projects/aztec-nr/aztec/src/macros/notes/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -463,14 +463,15 @@ comptime fn generate_setup_payload(
}
}

fn encrypt_log(self, context: &mut PrivateContext, ovpk: aztec::protocol_types::public_keys::OvpkM, recipient: aztec::protocol_types::address::AztecAddress) -> [Field; $encrypted_log_field_length] {
fn encrypt_log(self, context: &mut PrivateContext, ovpk: aztec::protocol_types::public_keys::OvpkM, recipient: aztec::protocol_types::address::AztecAddress, sender: aztec::protocol_types::address::AztecAddress) -> [Field; $encrypted_log_field_length] {
let ovsk_app: Field = context.request_ovsk_app(ovpk.hash());

let encrypted_log_bytes: [u8; $encrypted_log_byte_length] = aztec::encrypted_logs::payload::compute_private_log_payload(
context.this_address(),
ovsk_app,
ovpk,
recipient,
sender,
self.log_plaintext,
true
);
Expand Down
18 changes: 16 additions & 2 deletions noir-projects/aztec-nr/easy-private-state/src/easy_private_uint.nr
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ impl<Context> EasyPrivateUint<Context> {

impl EasyPrivateUint<&mut PrivateContext> {
// Very similar to `value_note::utils::increment`.
pub fn add(self, addend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
pub fn add(
self,
addend: u64,
owner: AztecAddress,
outgoing_viewer: AztecAddress,
sender: AztecAddress,
) {
let outgoing_viewer_keys = get_public_keys(outgoing_viewer);
// Creates new note for the owner.
let mut addend_note = ValueNote::new(addend as Field, owner);
Expand All @@ -33,12 +39,19 @@ impl EasyPrivateUint<&mut PrivateContext> {
self.context,
outgoing_viewer_keys.ovpk_m,
owner,
sender,
));
// docs:end:insert
}

// Very similar to `value_note::utils::decrement`.
pub fn sub(self, subtrahend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
pub fn sub(
self,
subtrahend: u64,
owner: AztecAddress,
outgoing_viewer: AztecAddress,
sender: AztecAddress,
) {
let outgoing_viewer_keys = get_public_keys(outgoing_viewer);

// docs:start:pop_notes
Expand All @@ -63,6 +76,7 @@ impl EasyPrivateUint<&mut PrivateContext> {
self.context,
outgoing_viewer_keys.ovpk_m,
owner,
sender,
));
}
}
8 changes: 6 additions & 2 deletions noir-projects/aztec-nr/value-note/src/utils.nr
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pub fn increment(
amount: Field,
recipient: AztecAddress,
outgoing_viewer: AztecAddress, // docs:end:increment_args
sender: AztecAddress,
) {
let outgoing_viewer_ovpk_m = get_public_keys(outgoing_viewer).ovpk_m;

Expand All @@ -32,6 +33,7 @@ pub fn increment(
balance.context,
outgoing_viewer_ovpk_m,
recipient,
sender,
));
}

Expand All @@ -44,8 +46,9 @@ pub fn decrement(
amount: Field,
owner: AztecAddress,
outgoing_viewer: AztecAddress,
sender: AztecAddress,
) {
let sum = decrement_by_at_most(balance, amount, owner, outgoing_viewer);
let sum = decrement_by_at_most(balance, amount, owner, outgoing_viewer, sender);
assert(sum == amount, "Balance too low");
}

Expand All @@ -62,6 +65,7 @@ pub fn decrement_by_at_most(
max_amount: Field,
owner: AztecAddress,
outgoing_viewer: AztecAddress,
sender: AztecAddress,
) -> Field {
let options = create_note_getter_options_for_decreasing_balance(max_amount);
let notes = balance.pop_notes(options);
Expand All @@ -80,7 +84,7 @@ pub fn decrement_by_at_most(
change_value = decremented - max_amount;
decremented -= change_value;
}
increment(balance, change_value, owner, outgoing_viewer);
increment(balance, change_value, owner, outgoing_viewer, sender);

decremented
}
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ contract AppSubscription {
&mut context,
keys.ovpk_m,
user_address,
user_address,
));

context.set_as_fee_payer();
Expand Down Expand Up @@ -116,7 +117,12 @@ contract AppSubscription {
let mut subscription_note =
SubscriptionNote::new(subscriber, expiry_block_number, tx_count);
storage.subscriptions.at(subscriber).initialize_or_replace(&mut subscription_note).emit(
encode_and_encrypt_note(&mut context, msg_sender_ovpk_m, subscriber),
encode_and_encrypt_note(
&mut context,
msg_sender_ovpk_m,
subscriber,
context.msg_sender(),
),
);
}

Expand Down
Loading

0 comments on commit 8ce6834

Please sign in to comment.