Skip to content

Commit ca0e084

Browse files
authored
refactor: split log emission to encrypt and a log, remove address input (#6987)
Removes the `contract_address` input from the context emission functions as it is always the contract itself if honest, so can just as well be auto populated for less error. Split the log emission into two variants, namely one that is just emitting whatever passed, and another that is encrypting first. This is to make it straight forward to encrypt with a different scheme if desired.
1 parent 42f8af5 commit ca0e084

13 files changed

Lines changed: 31 additions & 34 deletions

File tree

noir-projects/aztec-nr/address-note/src/address_note.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,6 @@ impl NoteInterface<ADDRESS_NOTE_LEN, ADDRESS_NOTE_BYTES_LEN> for AddressNote {
4747
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
4848
// docs:start:encrypted
4949
context.encrypt_and_emit_note(
50-
(*context).this_address(),
5150
slot,
5251
ovpk_m,
5352
ivpk_m,

noir-projects/aztec-nr/aztec/src/context/private_context.nr

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -313,17 +313,16 @@ impl PrivateContext {
313313
// used in siloing later on e.g. 'handshaking' contract w/ known address.
314314
pub fn encrypt_and_emit_event<N, M>(
315315
&mut self,
316-
contract_address: AztecAddress,
317316
randomness: Field, // Secret random value used later for masked_contract_address
318317
event_type_id: Field,
319318
ovpk_m: GrumpkinPoint,
320319
ivpk_m: GrumpkinPoint,
321320
preimage: [Field; N]
322321
) where [Field; N]: LensForEncryptedLog<N, M> {
323322
let ovsk_app = self.request_ovsk_app(ovpk_m.hash());
323+
let contract_address = self.this_address();
324324

325325
// We are currently just encrypting it unconstrained, but otherwise the same way as if it was a note.
326-
let counter = self.next_counter();
327326
let encrypted_log: [u8; M] = compute_encrypted_event_log(
328327
contract_address,
329328
randomness,
@@ -333,16 +332,27 @@ impl PrivateContext {
333332
ivpk_m,
334333
preimage
335334
);
336-
emit_encrypted_event_log(contract_address, randomness, encrypted_log, counter);
337-
let len = 32 + 32 + 64 + 48 + 48 + 176 + 64 + (preimage.len() as Field * 32) + 16 + 4;
335+
336+
self.emit_raw_event_log_with_masked_address(randomness, encrypted_log);
337+
}
338+
339+
pub fn emit_raw_event_log_with_masked_address<M>(
340+
&mut self,
341+
randomness: Field,
342+
encrypted_log: [u8; M]
343+
) {
344+
let counter = self.next_counter();
345+
let contract_address = self.this_address();
346+
let len = encrypted_log.len() as Field + 4;
338347
let log_hash = sha256_to_field(encrypted_log);
339348
let side_effect = EncryptedLogHash { value: log_hash, counter, length: len, randomness };
340349
self.encrypted_logs_hashes.push(side_effect);
350+
351+
emit_encrypted_event_log(contract_address, randomness, encrypted_log, counter);
341352
}
342353

343354
pub fn encrypt_and_emit_note<Note, N, NB, M>(
344355
&mut self,
345-
contract_address: AztecAddress,
346356
storage_slot: Field,
347357
ovpk_m: GrumpkinPoint,
348358
ivpk_m: GrumpkinPoint,
@@ -357,28 +367,21 @@ impl PrivateContext {
357367
note_exists_index as u32 != MAX_NEW_NOTE_HASHES_PER_CALL, "Can only emit a note log for an existing note."
358368
);
359369

360-
let counter = self.next_counter();
361-
370+
let contract_address = self.this_address();
362371
let ovsk_app = self.request_ovsk_app(ovpk_m.hash());
363372

364-
// Current unoptimized size of the encrypted log
365-
// incoming_tag (32 bytes)
366-
// outgoing_tag (32 bytes)
367-
// eph_pk (64 bytes)
368-
// incoming_header (48 bytes)
369-
// outgoing_header (48 bytes)
370-
// outgoing_body (176 bytes)
371-
// incoming_body_fixed (64 bytes)
372-
// incoming_body_variable (N * 32 bytes + 16 bytes padding)
373373
let encrypted_log: [u8; M] = compute_encrypted_note_log(contract_address, storage_slot, ovsk_app, ovpk_m, ivpk_m, note);
374-
emit_encrypted_note_log(note_hash_counter, encrypted_log, counter);
374+
self.emit_raw_note_log(note_hash_counter, encrypted_log);
375+
}
375376

376-
// len of processed log (4 bytes)
377+
pub fn emit_raw_note_log<M>(&mut self, note_hash_counter: u32, encrypted_log: [u8; M]) {
378+
let counter = self.next_counter();
377379
let len = encrypted_log.len() as Field + 4;
378-
379380
let log_hash = sha256_to_field(encrypted_log);
380381
let side_effect = NoteLogHash { value: log_hash, counter, length: len, note_hash_counter };
381382
self.note_encrypted_logs_hashes.push(side_effect);
383+
384+
emit_encrypted_note_log(note_hash_counter, encrypted_log, counter);
382385
}
383386

384387
pub fn call_private_function<ARGS_COUNT>(

noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,15 @@ pub fn compute_encrypted_note_log<Note, N, NB, M>(
5959
encrypted_bytes[400 + i] = incoming_body_ciphertext[i];
6060
}
6161

62+
// Current unoptimized size of the encrypted log
63+
// incoming_tag (32 bytes)
64+
// outgoing_tag (32 bytes)
65+
// eph_pk (64 bytes)
66+
// incoming_header (48 bytes)
67+
// outgoing_header (48 bytes)
68+
// outgoing_body (176 bytes)
69+
// incoming_body_fixed (64 bytes)
70+
// incoming_body_variable (N * 32 bytes + 16 bytes padding)
6271
encrypted_bytes
6372
}
6473

noir-projects/aztec-nr/value-note/src/value_note.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,6 @@ impl NoteInterface<VALUE_NOTE_LEN, VALUE_NOTE_BYTES_LEN> for ValueNote {
4949
// Broadcasts the note as an encrypted log on L1.
5050
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
5151
context.encrypt_and_emit_note(
52-
(*context).this_address(),
5352
slot,
5453
ovpk_m,
5554
ivpk_m,

noir-projects/noir-contracts/contracts/app_subscription_contract/src/subscription_note.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ impl NoteInterface<SUBSCRIPTION_NOTE_LEN, SUBSCRIPTION_NOTE_BYTES_LEN> for Subsc
4242
// Broadcasts the note as an encrypted log on L1.
4343
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
4444
context.encrypt_and_emit_note(
45-
(*context).this_address(),
4645
slot,
4746
ovpk_m,
4847
ivpk_m,

noir-projects/noir-contracts/contracts/docs_example_contract/src/types/card_note.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,6 @@ impl NoteInterface<CARD_NOTE_LEN, CARD_NOTE_BYTES_LEN> for CardNote {
5353
// Broadcasts the note as an encrypted log on L1.
5454
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
5555
context.encrypt_and_emit_note(
56-
(*context).this_address(),
5756
slot,
5857
ovpk_m,
5958
ivpk_m,

noir-projects/noir-contracts/contracts/ecdsa_account_contract/src/ecdsa_public_key_note.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,6 @@ impl NoteInterface<ECDSA_PUBLIC_KEY_NOTE_LEN, ECDSA_PUBLIC_KEY_NOTE_BYTES_LEN> f
8888
// Broadcasts the note as an encrypted log on L1.
8989
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
9090
context.encrypt_and_emit_note(
91-
(*context).this_address(),
9291
slot,
9392
ovpk_m,
9493
ivpk_m,

noir-projects/noir-contracts/contracts/pending_note_hashes_contract/src/main.nr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,6 @@ contract PendingNoteHashes {
136136

137137
// Emit note again
138138
context.encrypt_and_emit_note(
139-
context.this_address(),
140139
note.get_header().storage_slot,
141140
outgoing_viewer_ovpk_m,
142141
owner_ivpk_m,
@@ -369,7 +368,6 @@ contract PendingNoteHashes {
369368
bad_note.set_header(existing_note_header);
370369

371370
context.encrypt_and_emit_note(
372-
context.this_address(),
373371
existing_note_header.storage_slot,
374372
outgoing_viewer_ovpk_m,
375373
owner_ivpk_m,

noir-projects/noir-contracts/contracts/schnorr_account_contract/src/public_key_note.nr

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ impl NoteInterface<PUBLIC_KEY_NOTE_LEN, PUBLIC_KEY_NOTE_BYTES_LEN> for PublicKey
4242
// Broadcasts the note as an encrypted log on L1.
4343
fn broadcast(self, context: &mut PrivateContext, slot: Field, ovpk_m: GrumpkinPoint, ivpk_m: GrumpkinPoint) {
4444
context.encrypt_and_emit_note(
45-
(*context).this_address(),
4645
slot,
4746
ovpk_m,
4847
ivpk_m,

noir-projects/noir-contracts/contracts/test_contract/src/main.nr

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,6 @@ contract Test {
270270
let outgoing_viewer_ovpk_m = header.get_ovpk_m(&mut context, outgoing_viewer);
271271
let owner_ivpk_m = header.get_ivpk_m(&mut context, owner);
272272
context.encrypt_and_emit_event(
273-
context.this_address(),
274273
5, // testing only - this should be a secret random value to salt the addr
275274
1,
276275
outgoing_viewer_ovpk_m,
@@ -282,7 +281,6 @@ contract Test {
282281
if nest {
283282
Test::at(context.this_address()).emit_array_as_encrypted_log([0, 0, 0, 0, 0], owner, outgoing_viewer, false).call(&mut context);
284283
context.encrypt_and_emit_event(
285-
context.this_address(),
286284
0, // testing only - this signals to the kerels to not mask the address
287285
1,
288286
outgoing_viewer_ovpk_m,

0 commit comments

Comments
 (0)