Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions noir-projects/aztec-nr/aztec/src/messages/processing/mod.nr
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,8 @@ pub unconstrained fn validate_and_store_enqueued_notes_and_events(contract_addre
contract_address,
NOTE_VALIDATION_REQUESTS_ARRAY_BASE_SLOT,
EVENT_VALIDATION_REQUESTS_ARRAY_BASE_SLOT,
MAX_NOTE_PACKED_LEN as Field,
MAX_EVENT_SERIALIZED_LEN as Field,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,15 @@ pub(crate) unconstrained fn validate_and_store_enqueued_notes_and_events(
contract_address: AztecAddress,
note_validation_requests_array_base_slot: Field,
event_validation_requests_array_base_slot: Field,
max_note_packed_len: Field,
max_event_serialized_len: Field,
) {
validate_and_store_enqueued_notes_and_events_oracle(
contract_address,
note_validation_requests_array_base_slot,
event_validation_requests_array_base_slot,
max_note_packed_len,
max_event_serialized_len,
);
}

Expand All @@ -28,6 +32,8 @@ unconstrained fn validate_and_store_enqueued_notes_and_events_oracle(
contract_address: AztecAddress,
note_validation_requests_array_base_slot: Field,
event_validation_requests_array_base_slot: Field,
max_note_packed_len: Field,
max_event_serialized_len: Field,
) {}

pub(crate) unconstrained fn bulk_retrieve_logs(
Expand Down
2 changes: 1 addition & 1 deletion noir-projects/aztec-nr/aztec/src/oracle/version.nr
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
/// @dev Whenever a contract function or Noir test is run, the `aztec_utl_assertCompatibleOracleVersion` oracle is
/// called
/// and if the oracle version is incompatible an error is thrown.
pub global ORACLE_VERSION: Field = 13;
pub global ORACLE_VERSION: Field = 14;

/// Asserts that the version of the oracle is compatible with the version expected by the contract.
pub fn assert_compatible_oracle_version() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ describe('EventValidationRequest', () => {
8, // recipient
].map(n => new Fr(n));

const request = EventValidationRequest.fromFields(serialized);
const request = EventValidationRequest.fromFields(serialized, 10);

expect(request.contractAddress).toEqual(AztecAddress.fromBigInt(1n));
expect(request.eventTypeId).toEqual(new EventSelector(2));
Expand All @@ -37,4 +37,31 @@ describe('EventValidationRequest', () => {
expect(request.txHash).toEqual(TxHash.fromBigInt(7n));
expect(request.recipient).toEqual(AztecAddress.fromBigInt(8n));
});

it('throws if fed more fields than expected', () => {
const serialized = [
1, // contract_address
2, // event_type_id
3, // randomness
4, // serialized_event[0]
5, // serialized_event[1]
0, // serialized_event padding (11 storage fields total, but maxEventSerializedLen=10)
0,
0,
0,
0,
0,
0,
0,
0,
2, // bounded_vec_len
6, // event_commitment
7, // tx_hash
8, // recipient
].map(n => new Fr(n));

expect(() => EventValidationRequest.fromFields(serialized, 10)).toThrow(
'Error converting array of fields to EventValidationRequest: expected 17 fields but received 18 (maxEventSerializedLen=10).',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ import { EventSelector } from '@aztec/stdlib/abi';
import { AztecAddress } from '@aztec/stdlib/aztec-address';
import { TxHash } from '@aztec/stdlib/tx';

// TODO(#14617): should we compute this from constants? This value is aztec-nr specific.
const MAX_EVENT_SERIALIZED_LEN = 10;

/**
* Intermediate struct used to perform batch event validation by PXE. The `utilityValidateAndStoreEnqueuedNotesAndEvents` oracle
* expects for values of this type to be stored in a `CapsuleArray`.
Expand All @@ -22,22 +19,28 @@ export class EventValidationRequest {
public recipient: AztecAddress,
) {}

static fromFields(fields: Fr[] | FieldReader): EventValidationRequest {
static fromFields(fields: Fr[], maxEventSerializedLen: number): EventValidationRequest {
const reader = FieldReader.asReader(fields);

const contractAddress = AztecAddress.fromField(reader.readField());
const eventTypeId = EventSelector.fromField(reader.readField());

const randomness = reader.readField();

const eventStorage = reader.readFieldArray(MAX_EVENT_SERIALIZED_LEN);
const eventStorage = reader.readFieldArray(maxEventSerializedLen);
const eventLen = reader.readField().toNumber();
const serializedEvent = eventStorage.slice(0, eventLen);

const eventCommitment = reader.readField();
const txHash = TxHash.fromField(reader.readField());
const recipient = AztecAddress.fromField(reader.readField());

if (reader.remainingFields() !== 0) {
throw new Error(
`Error converting array of fields to EventValidationRequest: expected ${reader.cursor} fields but received ${fields.length} (maxEventSerializedLen=${maxEventSerializedLen}).`,
);
}

return new EventValidationRequest(
contractAddress,
eventTypeId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,7 @@ import { range } from '@aztec/foundation/array';
import { Fr } from '@aztec/foundation/curves/bn254';
import type { TxHash } from '@aztec/stdlib/tx';

import { MAX_NOTE_PACKED_LEN } from './note_validation_request.js';

const MAX_PUBLIC_LOG_LEN_FOR_NOTE_COMPLETION = MAX_NOTE_PACKED_LEN;
const MAX_LOG_CONTENT_LEN = Math.max(MAX_PUBLIC_LOG_LEN_FOR_NOTE_COMPLETION, PRIVATE_LOG_CIPHERTEXT_LEN);
const MAX_LOG_CONTENT_LEN = PRIVATE_LOG_CIPHERTEXT_LEN;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This implicitly assumes that notes will not be longer than logs no?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh, I wrote this on the other PR

I believe it didn't make sense to have this. MAX_NOTE_PACKED_LEN is calculated in aztec-nr from PRIVATE_LOG_CIPHERTEXT_LEN, and it should always be lower for it. But please correct me if I'm wrong or I'm missing something

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But please correct me if I'm wrong or I'm missing something

Nial mentioned that this limit might not make sense as there is no reason for notes to not be longer in case they are delivered via offchain messages.

Could you expand on why this change was done here? I didn't review your other PRs that I assumed led to this so I don't have context here. Thanks

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I made this change here because:

  • MAX_NOTE_PACKED_LEN and PRIVATE_LOG_CIPHERTEXT_LEN are actually calculated on the aztec-nr side of things, and the values are just copied to typescript
  • PRIVATE_LOG_CIPHERTEXT_LEN = 15, while MAX_NOTE_PACKED_LEN = 8. This is because: `
MAX_NOTE_PACKED_LEN = 15 (PRIVATE_LOG_CIPHERTEXT_LEN) 
                      - 3 (aes stuff) 
                      - 1 (message metadata)
                      - 3 (private note metadata: owner, storage slot, randomness)
                    = 8 

So if MAX_NOTE_PACKED_LEN is PRIVATE_LOG_CIPHERTEXT_LEN minus some fields, max(PRIVATE_LOG_CIPHERTEXT_LEN, MAX_NOTE_PACKED_LEN) would always be PRIVATE_LOG_CIPHERTEXT_LEN

That's why I thought it didn't make much sense to do that, and asked if I was missing something. About off-chain messages, would they go though this path? Do they emit logs somehow? Not very familiar with how they work today


/**
* Intermediate struct used to perform batch log retrieval by PXE. The `utilityBulkRetrieveLogs` oracle stores values of this
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,15 @@ describe('NoteValidationRequest', () => {
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000', // content end (MAX_NOTE_PACKED_LEN = 8)
'0x0000000000000000000000000000000000000000000000000000000000000000', // content end (8 storage fields)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this 8? Wouldn't this be higher, up to PRIVATE_LOG_CIPHERTEXT_LEN?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Right now, if you look at aztec-nr, the max length/capacity for notes is 8. That's why this test was using this particular value

This test could test other values though, but I thought it made sense to test what we would receive from contracts today (8)

'0x0000000000000000000000000000000000000000000000000000000000000002', // content length
'0x0000000000000000000000000000000000000000000000000000000000000006', // note hash
'0x0000000000000000000000000000000000000000000000000000000000000007', // nullifier
'0x0000000000000000000000000000000000000000000000000000000000000008', // tx hash
'0x0000000000000000000000000000000000000000000000000000000000000009', // recipient
].map(Fr.fromHexString);

const request = NoteValidationRequest.fromFields(serialized);
const request = NoteValidationRequest.fromFields(serialized, 8);

expect(request.contractAddress).toEqual(AztecAddress.fromBigInt(1n));
expect(request.owner).toEqual(AztecAddress.fromBigInt(50n));
Expand Down Expand Up @@ -56,17 +56,16 @@ describe('NoteValidationRequest', () => {
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000',
'0x0000000000000000000000000000000000000000000000000000000000000000', // content end (MAX_NOTE_PACKED_LEN = 8)
'0x0000000000000000000000000000000000000000000000000000000000000000', // extra field beyond MAX_NOTE_PACKED_LEN, this is a malformed serialization
'0x0000000000000000000000000000000000000000000000000000000000000000', // content end (9 storage fields, but maxNotePackedLen=8)
'0x0000000000000000000000000000000000000000000000000000000000000002', // content length
'0x0000000000000000000000000000000000000000000000000000000000000006', // note hash
'0x0000000000000000000000000000000000000000000000000000000000000007', // nullifier
'0x0000000000000000000000000000000000000000000000000000000000000008', // tx hash
'0x0000000000000000000000000000000000000000000000000000000000000009', // recipient
].map(Fr.fromHexString);

expect(() => NoteValidationRequest.fromFields(serialized)).toThrow(
/Error converting array of fields to NoteValidationRequest/,
expect(() => NoteValidationRequest.fromFields(serialized, 8)).toThrow(
'Error converting array of fields to NoteValidationRequest: expected 18 fields but received 19 (maxNotePackedLen=8).',
);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,6 @@ import { FieldReader } from '@aztec/foundation/serialize';
import { AztecAddress } from '@aztec/stdlib/aztec-address';
import { TxHash } from '@aztec/stdlib/tx';

// TODO(#14617): should we compute this from constants? This value is aztec-nr specific.
export const MAX_NOTE_PACKED_LEN = 8;

/**
* Intermediate struct used to perform batch note validation by PXE. The `utilityValidateAndStoreEnqueuedNotesAndEvents` oracle
* expects for values of this type to be stored in a `CapsuleArray`.
Expand All @@ -24,7 +21,7 @@ export class NoteValidationRequest {
public recipient: AztecAddress,
) {}

static fromFields(fields: Fr[] | FieldReader): NoteValidationRequest {
static fromFields(fields: Fr[], maxNotePackedLen: number): NoteValidationRequest {
const reader = FieldReader.asReader(fields);

const contractAddress = AztecAddress.fromField(reader.readField());
Expand All @@ -33,7 +30,7 @@ export class NoteValidationRequest {
const randomness = reader.readField();
const noteNonce = reader.readField();

const contentStorage = reader.readFieldArray(MAX_NOTE_PACKED_LEN);
const contentStorage = reader.readFieldArray(maxNotePackedLen);
const contentLen = reader.readField().toNumber();
const content = contentStorage.slice(0, contentLen);

Expand All @@ -44,7 +41,7 @@ export class NoteValidationRequest {

if (reader.remainingFields() !== 0) {
throw new Error(
`Error converting array of fields to NoteValidationRequest. Hint: check that MAX_NOTE_PACKED_LEN is consistent with private_notes::MAX_NOTE_PACKED_LEN in Aztec-nr.`,
`Error converting array of fields to NoteValidationRequest: expected ${reader.cursor} fields but received ${fields.length} (maxNotePackedLen=${maxNotePackedLen}).`,
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ export interface IUtilityExecutionOracle {
contractAddress: AztecAddress,
noteValidationRequestsArrayBaseSlot: Fr,
eventValidationRequestsArrayBaseSlot: Fr,
maxNotePackedLen: number,
maxEventSerializedLen: number,
): Promise<void>;
bulkRetrieveLogs(
contractAddress: AztecAddress,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -530,11 +530,15 @@ export class Oracle {
[contractAddress]: ACVMField[],
[noteValidationRequestsArrayBaseSlot]: ACVMField[],
[eventValidationRequestsArrayBaseSlot]: ACVMField[],
[maxNotePackedLen]: ACVMField[],
[maxEventSerializedLen]: ACVMField[],
): Promise<ACVMField[]> {
await this.handlerAsUtility().validateAndStoreEnqueuedNotesAndEvents(
AztecAddress.fromString(contractAddress),
Fr.fromString(noteValidationRequestsArrayBaseSlot),
Fr.fromString(eventValidationRequestsArrayBaseSlot),
Fr.fromString(maxNotePackedLen).toNumber(),
Fr.fromString(maxEventSerializedLen).toNumber(),
);

return [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,6 +448,8 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
contractAddress: AztecAddress,
noteValidationRequestsArrayBaseSlot: Fr,
eventValidationRequestsArrayBaseSlot: Fr,
maxNotePackedLen: number,
maxEventSerializedLen: number,
) {
// TODO(#10727): allow other contracts to store notes
if (!this.contractAddress.equals(contractAddress)) {
Expand All @@ -458,11 +460,11 @@ export class UtilityExecutionOracle implements IMiscOracle, IUtilityExecutionOra
// faster as we don't need to wait for the network round-trip.
const noteValidationRequests = (
await this.capsuleStore.readCapsuleArray(contractAddress, noteValidationRequestsArrayBaseSlot, this.jobId)
).map(NoteValidationRequest.fromFields);
).map(fields => NoteValidationRequest.fromFields(fields, maxNotePackedLen));

const eventValidationRequests = (
await this.capsuleStore.readCapsuleArray(contractAddress, eventValidationRequestsArrayBaseSlot, this.jobId)
).map(EventValidationRequest.fromFields);
).map(fields => EventValidationRequest.fromFields(fields, maxEventSerializedLen));

const noteService = new NoteService(this.noteStore, this.aztecNode, this.anchorBlockHeader, this.jobId);
const noteStorePromises = noteValidationRequests.map(request =>
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/pxe/src/oracle_version.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@
///
/// @dev Whenever a contract function or Noir test is run, the `aztec_utl_assertCompatibleOracleVersion` oracle is called
/// and if the oracle version is incompatible an error is thrown.
export const ORACLE_VERSION = 13;
export const ORACLE_VERSION = 14;

/// This hash is computed as by hashing the Oracle interface and it is used to detect when the Oracle interface changes,
/// which in turn implies that you need to update the ORACLE_VERSION constant in this file and in
/// `noir-projects/aztec-nr/aztec/src/oracle/version.nr`.
export const ORACLE_INTERFACE_HASH = '9fb918682455c164ce8dd3acb71c751e2b9b2fc48913604069c9ea885fa378ca';
export const ORACLE_INTERFACE_HASH = '34238ab576c377b4ffc1ddbf557560f07a66baf879920191897bc784fa5a97ee';
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Missing version bump?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, was left unstaged 😅

6 changes: 6 additions & 0 deletions yarn-project/txe/src/rpc_translator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -766,15 +766,21 @@ export class RPCTranslator {
foreignContractAddress: ForeignCallSingle,
foreignNoteValidationRequestsArrayBaseSlot: ForeignCallSingle,
foreignEventValidationRequestsArrayBaseSlot: ForeignCallSingle,
foreignMaxNotePackedLen: ForeignCallSingle,
foreignMaxEventSerializedLen: ForeignCallSingle,
) {
const contractAddress = AztecAddress.fromField(fromSingle(foreignContractAddress));
const noteValidationRequestsArrayBaseSlot = fromSingle(foreignNoteValidationRequestsArrayBaseSlot);
const eventValidationRequestsArrayBaseSlot = fromSingle(foreignEventValidationRequestsArrayBaseSlot);
const maxNotePackedLen = fromSingle(foreignMaxNotePackedLen).toNumber();
const maxEventSerializedLen = fromSingle(foreignMaxEventSerializedLen).toNumber();

await this.handlerAsUtility().validateAndStoreEnqueuedNotesAndEvents(
contractAddress,
noteValidationRequestsArrayBaseSlot,
eventValidationRequestsArrayBaseSlot,
maxNotePackedLen,
maxEventSerializedLen,
);

return toForeignCallResult([]);
Expand Down
Loading