Skip to content

Commit

Permalink
refactor: split log emission to encrypt and a log, remove address input
Browse files Browse the repository at this point in the history
  • Loading branch information
LHerskind committed Jun 10, 2024
1 parent 4ba553b commit 11555ee
Show file tree
Hide file tree
Showing 11 changed files with 22 additions and 22 deletions.
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/address-note/src/address_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
// docs:start:encrypted
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
32 changes: 22 additions & 10 deletions noir-projects/aztec-nr/aztec/src/context/private_context.nr
Original file line number Diff line number Diff line change
Expand Up @@ -313,17 +313,16 @@ impl PrivateContext {
// used in siloing later on e.g. 'handshaking' contract w/ known address.
pub fn encrypt_and_emit_log<N, M>(
&mut self,
contract_address: AztecAddress,
randomness: Field, // Secret random value used later for masked_contract_address
event_type_id: Field,
ovpk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
preimage: [Field; N]
) where [Field; N]: LensForEncryptedLog<N, M> {
let ovsk_app = self.request_ovsk_app(ovpk_m.hash());
let contract_address = self.this_address();

// We are currently just encrypting it unconstrained, but otherwise the same way as if it was a note.
let counter = self.next_counter();
let encrypted_log: [u8; M] = compute_encrypted_log(
contract_address,
randomness,
Expand All @@ -333,16 +332,27 @@ impl PrivateContext {
ivpk_m,
preimage
);
emit_encrypted_log(contract_address, randomness, encrypted_log, counter);
let len = 32 + 32 + 64 + 48 + 48 + 176 + 64 + (preimage.len() as Field * 32) + 16 + 4;

self.emit_raw_log_with_masked_address(randomness, encrypted_log);
}

pub fn emit_raw_log_with_masked_address<M>(
&mut self,
randomness: Field,
encrypted_log: [u8; M]
) {
let counter = self.next_counter();
let contract_address = self.this_address();
let len = encrypted_log.len() as Field + 4;
let log_hash = sha256_to_field(encrypted_log);
let side_effect = EncryptedLogHash { value: log_hash, counter, length: len, randomness };
self.encrypted_logs_hashes.push(side_effect);

emit_encrypted_log(contract_address, randomness, encrypted_log, counter);
}

pub fn encrypt_and_emit_note<Note, N, NB, M>(
&mut self,
contract_address: AztecAddress,
storage_slot: Field,
ovpk_m: GrumpkinPoint,
ivpk_m: GrumpkinPoint,
Expand All @@ -357,8 +367,7 @@ impl PrivateContext {
note_exists_index as u32 != MAX_NEW_NOTE_HASHES_PER_CALL, "Can only emit a note log for an existing note."
);

let counter = self.next_counter();

let contract_address = self.this_address();
let ovsk_app = self.request_ovsk_app(ovpk_m.hash());

// Current unoptimized size of the encrypted log
Expand All @@ -371,14 +380,17 @@ impl PrivateContext {
// incoming_body_fixed (64 bytes)
// incoming_body_variable (N * 32 bytes + 16 bytes padding)
let encrypted_log: [u8; M] = compute_encrypted_note_log(contract_address, storage_slot, ovsk_app, ovpk_m, ivpk_m, note);
emit_encrypted_note_log(note_hash_counter, encrypted_log, counter);
self.emit_raw_log(note_hash_counter, encrypted_log);
}

// len of processed log (4 bytes)
pub fn emit_raw_log<M>(&mut self, note_hash_counter: u32, encrypted_log: [u8; M]) {
let counter = self.next_counter();
let len = encrypted_log.len() as Field + 4;

let log_hash = sha256_to_field(encrypted_log);
let side_effect = NoteLogHash { value: log_hash, counter, length: len, note_hash_counter };
self.note_encrypted_logs_hashes.push(side_effect);

emit_encrypted_note_log(note_hash_counter, encrypted_log, counter);
}

pub fn call_private_function<ARGS_COUNT>(
Expand Down
1 change: 0 additions & 1 deletion noir-projects/aztec-nr/value-note/src/value_note.nr
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ impl NoteInterface<VALUE_NOTE_LEN, VALUE_NOTE_BYTES_LEN> for ValueNote {
// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl NoteInterface<SUBSCRIPTION_NOTE_LEN, SUBSCRIPTION_NOTE_BYTES_LEN> for Subsc
// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ impl NoteInterface<CARD_NOTE_LEN, CARD_NOTE_BYTES_LEN> for CardNote {
// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,6 @@ impl NoteInterface<ECDSA_PUBLIC_KEY_NOTE_LEN, ECDSA_PUBLIC_KEY_NOTE_BYTES_LEN> f
// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,6 @@ contract PendingNoteHashes {

// Emit note again
context.encrypt_and_emit_note(
context.this_address(),
note.get_header().storage_slot,
outgoing_viewer_ovpk_m,
owner_ivpk_m,
Expand Down Expand Up @@ -369,7 +368,6 @@ contract PendingNoteHashes {
bad_note.set_header(existing_note_header);

context.encrypt_and_emit_note(
context.this_address(),
existing_note_header.storage_slot,
outgoing_viewer_ovpk_m,
owner_ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,6 @@ impl NoteInterface<PUBLIC_KEY_NOTE_LEN, PUBLIC_KEY_NOTE_BYTES_LEN> for PublicKey
// Broadcasts the note as an encrypted log on L1.
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,6 @@ contract Test {
let outgoing_viewer_ovpk_m = header.get_ovpk_m(&mut context, outgoing_viewer);
let owner_ivpk_m = header.get_ivpk_m(&mut context, owner);
context.encrypt_and_emit_log(
context.this_address(),
5, // testing only - this should be a secret random value to salt the addr
1,
outgoing_viewer_ovpk_m,
Expand All @@ -282,7 +281,6 @@ contract Test {
if nest {
Test::at(context.this_address()).emit_array_as_encrypted_log([0, 0, 0, 0, 0], owner, outgoing_viewer, false).call(&mut context);
context.encrypt_and_emit_log(
context.this_address(),
0, // testing only - this signals to the kerels to not mask the address
1,
outgoing_viewer_ovpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl NoteInterface<TOKEN_NOTE_LEN, TOKEN_NOTE_BYTES_LEN> for TokenNote {
// TODO: (#5901) This will be changed a lot, as it should use the updated encrypted log format
if !(self.amount == U128::from_integer(0)) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ impl NoteInterface<TOKEN_NOTE_LEN, TOKEN_NOTE_BYTES_LEN> for TokenNote {
// TODO: (#5901) This will be changed a lot, as it should use the updated encrypted log format
if !(self.amount == U128::from_integer(0)) {
context.encrypt_and_emit_note(
(*context).this_address(),
slot,
ovpk_m,
ivpk_m,
Expand Down

0 comments on commit 11555ee

Please sign in to comment.