Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 16, 2024
1 parent 6d79f1e commit 33a7693
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 13 deletions.
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/encrypted_logs/payload.nr
Original file line number Diff line number Diff line change
Expand Up @@ -109,9 +109,9 @@ pub fn compute_encrypted_note_log<Note, let N: u32, let NB: u32, let M: u32>(
}

// Current unoptimized size of the encrypted log
// num_publicly_delivered_values (1 bytes)
// incoming_tag (32 bytes)
// outgoing_tag (32 bytes)
// num_publicly_delivered_values (1 byte)
// eph_pk (32 bytes)
// incoming_header (48 bytes)
// outgoing_header (48 bytes)
Expand Down
46 changes: 34 additions & 12 deletions yarn-project/circuit-types/src/logs/l1_payload/l1_note_payload.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,18 +96,18 @@ export class L1NotePayload extends L1Payload {
*/
public static decryptAsIncoming(ciphertext: Buffer | bigint[], ivsk: GrumpkinScalar) {
const input = Buffer.isBuffer(ciphertext) ? ciphertext : Buffer.from(ciphertext.map((x: bigint) => Number(x)));
const reader = BufferReader.asReader(input);

// TODO(benesjan): implement pub values extraction.
const _numPubliclyDeliveredValues = reader.readUInt8();
const [publicValues, remainingCiphertext] = this.#getPublicValuesAndRemainingCipherText(input);

const [address, incomingBody] = super._decryptAsIncoming(
reader.readToEnd(),
remainingCiphertext,
ivsk,
EncryptedNoteLogIncomingBody.fromCiphertext,
);

return new L1NotePayload(incomingBody.note, address, incomingBody.storageSlot, incomingBody.noteTypeId);
// Partial fields are expected to be at the end of the note
const note = new Note([...incomingBody.note.items, ...publicValues]);

return new L1NotePayload(note, address, incomingBody.storageSlot, incomingBody.noteTypeId);
}

/**
Expand All @@ -125,18 +125,18 @@ export class L1NotePayload extends L1Payload {
*/
public static decryptAsOutgoing(ciphertext: Buffer | bigint[], ovsk: GrumpkinScalar) {
const input = Buffer.isBuffer(ciphertext) ? ciphertext : Buffer.from(ciphertext.map((x: bigint) => Number(x)));
const reader = BufferReader.asReader(input);

// TODO(benesjan): implement pub values extraction.
const _numPubliclyDeliveredValues = reader.readUInt8();
const [publicValues, remainingCiphertext] = this.#getPublicValuesAndRemainingCipherText(input);

const [address, incomingBody] = super._decryptAsOutgoing(
reader.readToEnd(),
remainingCiphertext,
ovsk,
EncryptedNoteLogIncomingBody.fromCiphertext,
);

return new L1NotePayload(incomingBody.note, address, incomingBody.storageSlot, incomingBody.noteTypeId);
// Partial fields are expected to be at the end of the note
const note = new Note([...incomingBody.note.items, ...publicValues]);

return new L1NotePayload(note, address, incomingBody.storageSlot, incomingBody.noteTypeId);
}

public equals(other: L1NotePayload) {
Expand All @@ -147,4 +147,26 @@ export class L1NotePayload extends L1Payload {
this.noteTypeId.equals(other.noteTypeId)
);
}

static #getPublicValuesAndRemainingCipherText(input: Buffer): [Fr[], Buffer] {
const reader = BufferReader.asReader(input);
const numPubliclyDeliveredValues = reader.readUInt8();

const remainingData = reader.readToEnd();
const publicValuesData = remainingData.subarray(
remainingData.length - numPubliclyDeliveredValues * Fr.SIZE_IN_BYTES,
remainingData.length,
);
if (publicValuesData.length % Fr.SIZE_IN_BYTES !== 0) {
throw new Error('Public values byte length is not a multiple of Fr size');
}
const publicValues = [];
for (let i = 0; i < publicValuesData.length; i += Fr.SIZE_IN_BYTES) {
publicValues.push(Fr.fromBuffer(publicValuesData.subarray(i, i + Fr.SIZE_IN_BYTES)));
}

const remainingCiphertext = remainingData.subarray(0, remainingData.length - publicValuesData.length);

return [publicValues, remainingCiphertext];
}
}

0 comments on commit 33a7693

Please sign in to comment.