Skip to content

Commit

Permalink
init
Browse files Browse the repository at this point in the history
  • Loading branch information
sklppy88 committed Oct 31, 2024
1 parent 3ae059f commit 27b1edc
Show file tree
Hide file tree
Showing 16 changed files with 134 additions and 44 deletions.
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
16 changes: 14 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, sender: 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 @@ -39,7 +45,13 @@ impl EasyPrivateUint<&mut PrivateContext> {
}

// Very similar to `value_note::utils::decrement`.
pub fn sub(self, subtrahend: u64, owner: AztecAddress, outgoing_viewer: AztecAddress, sender: 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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -117,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, context.msg_sender()),
encode_and_encrypt_note(
&mut context,
msg_sender_ovpk_m,
subscriber,
context.msg_sender(),
),
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,13 @@ contract Benchmarking {
#[private]
fn create_note(owner: AztecAddress, outgoing_viewer: AztecAddress, value: Field) {
// docs:start:increment_valuenote
increment(storage.notes.at(owner), value, owner, outgoing_viewer);
increment(
storage.notes.at(owner),
value,
owner,
outgoing_viewer,
outgoing_viewer,
);
// docs:end:increment_valuenote
}
// Deletes a note at a specific index in the set and creates a new one with the same value.
Expand All @@ -36,7 +42,13 @@ contract Benchmarking {
let mut getter_options = NoteGetterOptions::new();
let notes = owner_notes.pop_notes(getter_options.set_limit(1).set_offset(index));
let note = notes.get(0);
increment(owner_notes, note.value, owner, outgoing_viewer);
increment(
owner_notes,
note.value,
owner,
outgoing_viewer,
outgoing_viewer,
);
}

// Reads and writes to public storage and enqueues a call to another public function.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ contract Counter {
// We can name our initializer anything we want as long as it's marked as aztec(initializer)
fn initialize(headstart: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let counters = storage.counters;
counters.at(owner).add(headstart, owner, outgoing_viewer);
counters.at(owner).add(headstart, owner, outgoing_viewer, context.msg_sender());
}
// docs:end:constructor

Expand All @@ -38,7 +38,7 @@ contract Counter {
);
}
let counters = storage.counters;
counters.at(owner).add(1, owner, outgoing_viewer);
counters.at(owner).add(1, owner, outgoing_viewer, context.msg_sender());
}
// docs:end:increment
// docs:start:get_counter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,15 +21,15 @@ contract EasyPrivateToken {
fn constructor(initial_supply: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let balances = storage.balances;

balances.at(owner).add(initial_supply, owner, outgoing_viewer);
balances.at(owner).add(initial_supply, owner, outgoing_viewer, context.msg_sender());
}

// Mints `amount` of tokens to `owner`.
#[private]
fn mint(amount: u64, owner: AztecAddress, outgoing_viewer: AztecAddress) {
let balances = storage.balances;

balances.at(owner).add(amount, owner, outgoing_viewer);
balances.at(owner).add(amount, owner, outgoing_viewer, context.msg_sender());
}

// Transfers `amount` of tokens from `sender` to a `recipient`.
Expand All @@ -42,8 +42,8 @@ contract EasyPrivateToken {
) {
let balances = storage.balances;

balances.at(sender).sub(amount, sender, outgoing_viewer);
balances.at(recipient).add(amount, recipient, outgoing_viewer);
balances.at(sender).sub(amount, sender, outgoing_viewer, sender);
balances.at(recipient).add(amount, recipient, outgoing_viewer, sender);
}

// Helper function to get the balance of a user ("unconstrained" is a Noir alternative of Solidity's "view" function).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,8 @@ contract NFT {

// We set the ovpk to the message sender's ovpk and we encrypt the log.
let from_ovpk = get_public_keys(context.msg_sender()).ovpk_m;
let setup_log = note_setup_payload.encrypt_log(context, from_ovpk, to);
let setup_log =
note_setup_payload.encrypt_log(context, from_ovpk, to, context.msg_sender());

// Using the x-coordinate as a hiding point slot is safe against someone else interfering with it because
// we have a guarantee that the public functions of the transaction are executed right after the private ones
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -139,10 +139,20 @@ contract PendingNoteHashes {
// Insert note
let emission = owner_balance.insert(&mut note);

emission.emit(encode_and_encrypt_note(&mut context, outgoing_viewer_ovpk_m, owner, context.msg_sender()));
emission.emit(encode_and_encrypt_note(
&mut context,
outgoing_viewer_ovpk_m,
owner,
context.msg_sender(),
));

// Emit note again
emission.emit(encode_and_encrypt_note(&mut context, outgoing_viewer_ovpk_m, owner, context.msg_sender()));
emission.emit(encode_and_encrypt_note(
&mut context,
outgoing_viewer_ovpk_m,
owner,
context.msg_sender(),
));
}

// Nested/inner function to get a note and confirm it matches the expected value
Expand Down
Loading

0 comments on commit 27b1edc

Please sign in to comment.