Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Oct 22, 2024
1 parent b08d105 commit c6e3cf1
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,45 @@ use dep::protocol_types::{
abis::note_hash::NoteHash
};

fn compute_raw_note_log<Note, let N: u32>(
// Note: the following function is an ugly hack but it's very isolated so it seemed like the best way to do it for now.
// Having `compute_encrypted_log` insert the flag would require a much larger refactor as that function is used
// for events as well (it would also require a change in EncryptedLogPayload in TS which is just too much work
// given that all that code is most likely going to die).
// TODO(benesjan): Refactor this when working on the large PXE refactor.
pub fn compute_note_log<let P: u32, let M: u32>(
contract_address: AztecAddress,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress,
plaintext: [u8; P],
public_values_appended: bool // Indicates whether there are values to be appended to the log in public (used in partial note flow).
) -> [u8; M] {
let encrypted_log_without_public_values_flag: [u8; M - 1] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);

// Note: the following is an ugly hack but it's very isolated so it seemed like the best way to do it for now.
// Having `compute_encrypted_log` insert the flag would require a much larger refactor as that function is used
// for events as well (it would also require a change in EncryptedLogPayload in TS which is just too much work
// given that all that code is most likely going to die).
// TODO(benesjan): Refactor this when working on the large PXE refactor.
let mut encrypted_log = [0 as u8; M];
encrypted_log[0] = public_values_appended as u8;
for i in 1..M {
encrypted_log[i] = encrypted_log_without_public_values_flag[i];
}

encrypted_log
}

fn compute_values_to_emit<Note, let N: u32>(
context: PrivateContext,
note: Note,
ovsk_app: Field,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> (u32, [u8; 416 + N * 32], Field) where Note: NoteInterface<N> {
recipient: AztecAddress,
public_values_appended: bool // Indicates whether there are values to be appended to the log in public (used in partial note flow).
) -> (u32, [u8; 417 + N * 32], Field) where Note: NoteInterface<N> {
let note_header = note.get_header();
let note_hash_counter = note_header.note_hash_counter;
let storage_slot = note_header.storage_slot;
Expand All @@ -26,21 +57,39 @@ fn compute_raw_note_log<Note, let N: u32>(
let contract_address: AztecAddress = context.this_address();

let plaintext = note.to_be_bytes(storage_slot);
let encrypted_log: [u8; 416 + N * 32] = compute_encrypted_log(contract_address, ovsk_app, ovpk, ivpk, recipient, plaintext);
let encrypted_log: [u8; 417 + N * 32] = compute_note_log(
contract_address,
ovsk_app,
ovpk,
ivpk,
recipient,
plaintext,
public_values_appended
);

let log_hash = sha256_to_field(encrypted_log);

(note_hash_counter, encrypted_log, log_hash)
}

unconstrained fn compute_raw_note_log_unconstrained<Note, let N: u32>(
unconstrained fn compute_values_to_emit_unconstrained<Note, let N: u32>(
context: PrivateContext,
note: Note,
ovpk: OvpkM,
ivpk: IvpkM,
recipient: AztecAddress
) -> (u32, [u8; 416 + N * 32], Field) where Note: NoteInterface<N> {
recipient: AztecAddress,
public_values_appended: bool // Indicates whether there are values to be appended to the log in public (used in partial note flow).
) -> (u32, [u8; 417 + N * 32], Field) where Note: NoteInterface<N> {
let ovsk_app = get_ovsk_app(ovpk.hash());
compute_raw_note_log(context, note, ovsk_app, ovpk, ivpk, recipient)
compute_values_to_emit(
context,
note,
ovsk_app,
ovpk,
ivpk,
recipient,
public_values_appended
)
}

// This function seems to be affected by the following Noir bug:
Expand All @@ -55,7 +104,7 @@ pub fn encode_and_encrypt_note<Note, let N: u32>(
| e: NoteEmission<Note> | {
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, recipient);
let (note_hash_counter, encrypted_log, log_hash) = compute_values_to_emit(*context, e.note, ovsk_app, ovpk, ivpk, recipient, false);
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
}
Expand Down Expand Up @@ -87,7 +136,7 @@ pub fn encode_and_encrypt_note_unconstrained<Note, let N: u32>(
// 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_raw_note_log_unconstrained(*context, e.note, ovpk, ivpk, recipient)
compute_values_to_emit_unconstrained(*context, e.note, ovpk, ivpk, recipient, false)
};
context.emit_raw_note_log(note_hash_counter, encrypted_log, log_hash);
}
Expand All @@ -105,13 +154,14 @@ pub fn encrypt_and_emit_partial_log<let M: u32>(
) {
let ovsk_app: Field = context.request_ovsk_app(recipient_keys.ovpk_m.hash());

let encrypted_log: [u8; 352 + M] = compute_encrypted_log(
let encrypted_log: [u8; 353 + M] = compute_note_log(
context.this_address(),
ovsk_app,
recipient_keys.ovpk_m,
recipient_keys.ivpk_m,
recipient,
log_plaintext
log_plaintext,
true
);
let log_hash = sha256_to_field(encrypted_log);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,21 @@ const OUTGOING_BODY_SIZE = 144;
*/
export class EncryptedLogPayload {
constructor(
/**
* Note discovery tag used by the recipient of the log.
*/
public readonly incomingTag: Fr,
/**
* Note discovery tag used by the sender of the log.
*/
public readonly outgoingTag: Fr,
/**
* Address of a contract that emitted the log.
*/
public readonly contractAddress: AztecAddress,
/**
* Decrypted incoming body.
*/
public readonly incomingBodyPlaintext: Buffer,
) {}

Expand Down Expand Up @@ -71,6 +83,7 @@ export class EncryptedLogPayload {
return serializeToBuffer(
this.incomingTag,
this.outgoingTag,
this.publicValuesAppended,
ephPk.toCompressedBuffer(),
incomingHeaderCiphertext,
outgoingHeaderCiphertext,
Expand Down

0 comments on commit c6e3cf1

Please sign in to comment.