Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Sep 27, 2024
1 parent 513ae37 commit 814db86
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 45 deletions.
1 change: 1 addition & 0 deletions yarn-project/circuit-types/src/logs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ export * from './get_unencrypted_logs_response.js';
export * from './function_l2_logs.js';
export * from './l2_block_l2_logs.js';
export * from './l2_logs_source.js';
export * from './l2_log.js';
export * from './log_id.js';
export * from './log_type.js';
export * from './log_filter.js';
Expand Down
121 changes: 76 additions & 45 deletions yarn-project/circuit-types/src/logs/l2_log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import {
Fr,
GrumpkinScalar,
type KeyValidationRequest,
NotOnCurveError,
Point,
type PublicKey,
computeOvskApp,
Expand Down Expand Up @@ -90,24 +91,39 @@ export class L2Log {
* @param ivsk - The incoming viewing secret key, used to decrypt the logs
* @returns The decrypted log payload
*/
public static decryptAsIncoming(ciphertext: Buffer | BufferReader, ivsk: GrumpkinScalar) {
public static decryptAsIncoming(ciphertext: Buffer | BufferReader, ivsk: GrumpkinScalar): L2Log | undefined {
const reader = BufferReader.asReader(ciphertext);

const incomingTag = reader.readObject(Fr);
const outgoingTag = reader.readObject(Fr);

const ephPk = Point.fromCompressedBuffer(reader.readBytes(Point.COMPRESSED_SIZE_IN_BYTES));

const incomingHeader = decrypt(reader.readBytes(HEADER_SIZE), ivsk, ephPk);

// Skipping the outgoing header and body
reader.readBytes(HEADER_SIZE);
reader.readBytes(OUTGOING_BODY_SIZE);

// The incoming can be of variable size, so we read until the end
const incomingBodyPlaintext = decrypt(reader.readToEnd(), ivsk, ephPk);

return new L2Log(incomingTag, outgoingTag, AztecAddress.fromBuffer(incomingHeader), incomingBodyPlaintext);
try {
const incomingTag = reader.readObject(Fr);
const outgoingTag = reader.readObject(Fr);

const ephPk = Point.fromCompressedBuffer(reader.readBytes(Point.COMPRESSED_SIZE_IN_BYTES));

const incomingHeader = decrypt(reader.readBytes(HEADER_SIZE), ivsk, ephPk);

// Skipping the outgoing header and body
reader.readBytes(HEADER_SIZE);
reader.readBytes(OUTGOING_BODY_SIZE);

// The incoming can be of variable size, so we read until the end
const incomingBodyPlaintext = decrypt(reader.readToEnd(), ivsk, ephPk);

return new L2Log(incomingTag, outgoingTag, AztecAddress.fromBuffer(incomingHeader), incomingBodyPlaintext);
} catch (e: any) {
// Following error messages are expected to occur when decryption fails
if (
!(e instanceof NotOnCurveError) &&
!e.message.endsWith('is greater or equal to field modulus.') &&
!e.message.startsWith('Invalid AztecAddress length') &&
!e.message.startsWith('Selector must fit in') &&
!e.message.startsWith('Attempted to read beyond buffer length')
) {
// If we encounter an unexpected error, we rethrow it
throw e;
}
return;
}
}

/**
Expand All @@ -123,38 +139,53 @@ export class L2Log {
* @param ovsk - The outgoing viewing secret key, used to decrypt the logs
* @returns The decrypted log payload
*/
public static decryptAsOutgoing(ciphertext: Buffer | BufferReader, ovsk: GrumpkinScalar) {
public static decryptAsOutgoing(ciphertext: Buffer | BufferReader, ovsk: GrumpkinScalar): L2Log | undefined {
const reader = BufferReader.asReader(ciphertext);

const incomingTag = reader.readObject(Fr);
const outgoingTag = reader.readObject(Fr);

const ephPk = Point.fromCompressedBuffer(reader.readBytes(Point.COMPRESSED_SIZE_IN_BYTES));

// We skip the incoming header
reader.readBytes(HEADER_SIZE);

const outgoingHeader = decrypt(reader.readBytes(HEADER_SIZE), ovsk, ephPk);
const contractAddress = AztecAddress.fromBuffer(outgoingHeader);

const ovskApp = computeOvskApp(ovsk, contractAddress);

let ephSk: GrumpkinScalar;
let recipientIvpk: PublicKey;
{
const outgoingBody = decrypt(reader.readBytes(OUTGOING_BODY_SIZE), ovskApp, ephPk, derivePoseidonAESSecret);
const obReader = BufferReader.asReader(outgoingBody);

// From outgoing body we extract ephSk, recipient and recipientIvpk
ephSk = GrumpkinScalar.fromHighLow(obReader.readObject(Fr), obReader.readObject(Fr));
const _recipient = obReader.readObject(AztecAddress);
recipientIvpk = Point.fromCompressedBuffer(obReader.readBytes(Point.COMPRESSED_SIZE_IN_BYTES));
try {
const incomingTag = reader.readObject(Fr);
const outgoingTag = reader.readObject(Fr);

const ephPk = Point.fromCompressedBuffer(reader.readBytes(Point.COMPRESSED_SIZE_IN_BYTES));

// We skip the incoming header
reader.readBytes(HEADER_SIZE);

const outgoingHeader = decrypt(reader.readBytes(HEADER_SIZE), ovsk, ephPk);
const contractAddress = AztecAddress.fromBuffer(outgoingHeader);

const ovskApp = computeOvskApp(ovsk, contractAddress);

let ephSk: GrumpkinScalar;
let recipientIvpk: PublicKey;
{
const outgoingBody = decrypt(reader.readBytes(OUTGOING_BODY_SIZE), ovskApp, ephPk, derivePoseidonAESSecret);
const obReader = BufferReader.asReader(outgoingBody);

// From outgoing body we extract ephSk, recipient and recipientIvpk
ephSk = GrumpkinScalar.fromHighLow(obReader.readObject(Fr), obReader.readObject(Fr));
const _recipient = obReader.readObject(AztecAddress);
recipientIvpk = Point.fromCompressedBuffer(obReader.readBytes(Point.COMPRESSED_SIZE_IN_BYTES));
}

// Now we decrypt the incoming body using the ephSk and recipientIvpk
const incomingBody = decrypt(reader.readToEnd(), ephSk, recipientIvpk);

return new L2Log(incomingTag, outgoingTag, contractAddress, incomingBody);
} catch (e: any) {
// Following error messages are expected to occur when decryption fails
if (
!(e instanceof NotOnCurveError) &&
!e.message.endsWith('is greater or equal to field modulus.') &&
!e.message.startsWith('Invalid AztecAddress length') &&
!e.message.startsWith('Selector must fit in') &&
!e.message.startsWith('Attempted to read beyond buffer length')
) {
// If we encounter an unexpected error, we rethrow it
throw e;
}
return;
}

// Now we decrypt the incoming body using the ephSk and recipientIvpk
const incomingBody = decrypt(reader.readToEnd(), ephSk, recipientIvpk);

return new L2Log(incomingTag, outgoingTag, contractAddress, incomingBody);
}

public toBuffer() {
Expand Down

0 comments on commit 814db86

Please sign in to comment.