Skip to content

Commit

Permalink
naming fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed Dec 17, 2024
1 parent 36d3b9f commit b4a6666
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 33 deletions.
5 changes: 1 addition & 4 deletions yarn-project/pxe/src/simulator_oracle/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ import { type IncomingNoteDao } from '../database/incoming_note_dao.js';
import { type PxeDatabase } from '../database/index.js';
import { produceNoteDaos } from '../note_decryption_utils/produce_note_daos.js';
import { getAcirSimulator } from '../simulator/index.js';
import { getIndexedTaggingSecretsForTheWindow, getInitialIndexesMap } from './tagging_utils.js';
import { WINDOW_HALF_SIZE, getIndexedTaggingSecretsForTheWindow, getInitialIndexesMap } from './tagging_utils.js';

/**
* A data oracle that provides information needed for simulating a transaction.
Expand Down Expand Up @@ -418,9 +418,6 @@ export class SimulatorOracle implements DBOracle {
maxBlockNumber: number,
scopes?: AztecAddress[],
): Promise<Map<string, TxScopedL2Log[]>> {
// Half the size of the window we slide over the tagging secret indexes.
const WINDOW_HALF_SIZE = 10;

// Ideally this algorithm would be implemented in noir, exposing its building blocks as oracles.
// However it is impossible at the moment due to the language not supporting nested slices.
// This nesting is necessary because for a given set of tags we don't
Expand Down
60 changes: 31 additions & 29 deletions yarn-project/pxe/src/simulator_oracle/simulator_oracle.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import { type PxeDatabase } from '../database/index.js';
import { KVPxeDatabase } from '../database/kv_pxe_database.js';
import { ContractDataOracle } from '../index.js';
import { SimulatorOracle } from './index.js';
import { WINDOW_HALF_SIZE } from './tagging_utils.js';

const TXS_PER_BLOCK = 4;
const NUM_NOTE_HASHES_PER_BLOCK = TXS_PER_BLOCK * MAX_NOTE_HASHES_PER_TX;
Expand Down Expand Up @@ -138,16 +139,15 @@ describe('Simulator oracle', () => {

describe('sync tagged logs', () => {
const NUM_SENDERS = 10;
const SENDER_OFFSET_WINDOW_SIZE = 10;
let senders: { completeAddress: CompleteAddress; ivsk: Fq; secretKey: Fr }[];

function generateMockLogs(senderOffset: number) {
function generateMockLogs(tagIndex: number) {
const logs: { [k: string]: TxScopedL2Log[] } = {};

// Add a random note from every address in the address book for our account with index senderOffset
// Add a random note from every address in the address book for our account with index tagIndex
// Compute the tag as sender (knowledge of preaddress and ivsk)
for (const sender of senders) {
const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, senderOffset);
const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, tagIndex);
const blockNumber = 1;
const randomNote = new MockNoteRequest(
getRandomNoteLogPayload(tag, contractAddress),
Expand All @@ -164,18 +164,18 @@ describe('Simulator oracle', () => {
// Add a random note from the first sender in the address book, repeating the tag
// Compute the tag as sender (knowledge of preaddress and ivsk)
const firstSender = senders[0];
const tag = computeSiloedTagForIndex(firstSender, recipient.address, contractAddress, senderOffset);
const tag = computeSiloedTagForIndex(firstSender, recipient.address, contractAddress, tagIndex);
const payload = getRandomNoteLogPayload(tag, contractAddress);
const logData = payload.generatePayload(GrumpkinScalar.random(), recipient.address).toBuffer();
const log = new TxScopedL2Log(TxHash.random(), 1, 0, false, logData);
logs[tag.toString()].push(log);
// Accumulated logs intended for recipient: NUM_SENDERS + 1

// Add a random note from half the address book for our account with index senderOffset + 1
// Add a random note from half the address book for our account with index tagIndex + 1
// Compute the tag as sender (knowledge of preaddress and ivsk)
for (let i = NUM_SENDERS / 2; i < NUM_SENDERS; i++) {
const sender = senders[i];
const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, senderOffset + 1);
const tag = computeSiloedTagForIndex(sender, recipient.address, contractAddress, tagIndex + 1);
const blockNumber = 2;
const randomNote = new MockNoteRequest(
getRandomNoteLogPayload(tag, contractAddress),
Expand All @@ -189,13 +189,13 @@ describe('Simulator oracle', () => {
}
// Accumulated logs intended for recipient: NUM_SENDERS + 1 + NUM_SENDERS / 2

// Add a random note from every address in the address book for a random recipient with index senderOffset
// Add a random note from every address in the address book for a random recipient with index tagIndex
// Compute the tag as sender (knowledge of preaddress and ivsk)
for (const sender of senders) {
const keys = deriveKeys(Fr.random());
const partialAddress = Fr.random();
const randomRecipient = computeAddress(keys.publicKeys, partialAddress);
const tag = computeSiloedTagForIndex(sender, randomRecipient, contractAddress, senderOffset);
const tag = computeSiloedTagForIndex(sender, randomRecipient, contractAddress, tagIndex);
const blockNumber = 3;
const randomNote = new MockNoteRequest(
getRandomNoteLogPayload(tag, contractAddress),
Expand Down Expand Up @@ -232,8 +232,8 @@ describe('Simulator oracle', () => {
});

it('should sync tagged logs', async () => {
const senderOffset = 0;
generateMockLogs(senderOffset);
const tagIndex = 0;
generateMockLogs(tagIndex);
const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3);
// We expect to have all logs intended for the recipient, one per sender + 1 with a duplicated tag for the first
// one + half of the logs for the second index
Expand Down Expand Up @@ -266,8 +266,8 @@ describe('Simulator oracle', () => {
await keyStore.addAccount(sender.secretKey, sender.completeAddress.partialAddress);
}

let senderOffset = 0;
generateMockLogs(senderOffset);
let tagIndex = 0;
generateMockLogs(tagIndex);

// Recompute the secrets (as recipient) to ensure indexes are updated
const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address);
Expand Down Expand Up @@ -298,8 +298,8 @@ describe('Simulator oracle', () => {

// We add more logs to the second half of the window to test that a second iteration in `syncTaggedLogsAsSender`
// is handled correctly.
senderOffset = 11;
generateMockLogs(senderOffset);
tagIndex = 11;
generateMockLogs(tagIndex);
for (let i = 0; i < senders.length; i++) {
await simulatorOracle.syncTaggedLogsAsSender(
contractAddress,
Expand All @@ -315,8 +315,8 @@ describe('Simulator oracle', () => {
});

it('should sync tagged logs with a sender index offset', async () => {
const senderOffset = 5;
generateMockLogs(senderOffset);
const tagIndex = 5;
generateMockLogs(tagIndex);
const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3);
// We expect to have all logs intended for the recipient, one per sender + 1 with a duplicated tag for the first one + half of the logs for the second index
expect(syncedLogs.get(recipient.address.toString())).toHaveLength(NUM_SENDERS + 1 + NUM_SENDERS / 2);
Expand All @@ -342,8 +342,8 @@ describe('Simulator oracle', () => {
});

it("should sync tagged logs for which indexes are not updated if they're inside the window", async () => {
const senderOffset = 1;
generateMockLogs(senderOffset);
const tagIndex = 1;
generateMockLogs(tagIndex);

// Recompute the secrets (as recipient) to update indexes
const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address);
Expand All @@ -362,8 +362,8 @@ describe('Simulator oracle', () => {
expect(syncedLogs.get(recipient.address.toString())).toHaveLength(NUM_SENDERS + 1 + NUM_SENDERS / 2);

// First sender should have 2 logs, but keep index 2 since they were built using the same tag
// Next 4 senders should also have index 2 = offset + 1
// Last 5 senders should have index 3 = offset + 2
// Next 4 senders should also have index 2 = tagIndex + 1
// Last 5 senders should have index 3 = tagIndex + 2
const indexes = await database.getTaggingSecretsIndexesAsRecipient(secrets);

expect(indexes).toHaveLength(NUM_SENDERS);
Expand All @@ -375,8 +375,8 @@ describe('Simulator oracle', () => {
});

it("should not sync tagged logs for which indexes are not updated if they're outside the window", async () => {
const senderOffset = 0;
generateMockLogs(senderOffset);
const tagIndex = 0;
generateMockLogs(tagIndex);

// Recompute the secrets (as recipient) to update indexes
const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address);
Expand All @@ -385,8 +385,10 @@ describe('Simulator oracle', () => {
return poseidon2Hash([firstSenderSecretPoint.x, firstSenderSecretPoint.y, contractAddress]);
});

// We set the indexes to WINDOW_HALF_SIZE + 1 so that it's outside the window and for this reason no updates
// should be triggered.
await database.setTaggingSecretsIndexesAsRecipient(
secrets.map(secret => new IndexedTaggingSecret(secret, SENDER_OFFSET_WINDOW_SIZE + 1)),
secrets.map(secret => new IndexedTaggingSecret(secret, WINDOW_HALF_SIZE + 1)),
);

const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3);
Expand All @@ -405,8 +407,8 @@ describe('Simulator oracle', () => {
});

it('should sync tagged logs from scratch after a DB wipe', async () => {
const senderOffset = 0;
generateMockLogs(senderOffset);
const tagIndex = 0;
generateMockLogs(tagIndex);

// Recompute the secrets (as recipient) to update indexes
const ivsk = await keyStore.getMasterIncomingViewingSecretKey(recipient.address);
Expand All @@ -416,7 +418,7 @@ describe('Simulator oracle', () => {
});

await database.setTaggingSecretsIndexesAsRecipient(
secrets.map(secret => new IndexedTaggingSecret(secret, SENDER_OFFSET_WINDOW_SIZE + 2)),
secrets.map(secret => new IndexedTaggingSecret(secret, WINDOW_HALF_SIZE + 2)),
);

let syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 3);
Expand Down Expand Up @@ -448,8 +450,8 @@ describe('Simulator oracle', () => {
});

it('should not sync tagged logs with a blockNumber > maxBlockNumber', async () => {
const senderOffset = 0;
generateMockLogs(senderOffset);
const tagIndex = 0;
generateMockLogs(tagIndex);
const syncedLogs = await simulatorOracle.syncTaggedLogs(contractAddress, 1);

// Only NUM_SENDERS + 1 logs should be synched, since the rest have blockNumber > 1
Expand Down
3 changes: 3 additions & 0 deletions yarn-project/pxe/src/simulator_oracle/tagging_utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { type Fr, IndexedTaggingSecret } from '@aztec/circuits.js';

// Half the size of the window we slide over the tagging secret indexes.
export const WINDOW_HALF_SIZE = 10;

export function getIndexedTaggingSecretsForTheWindow(
secretsAndWindows: { appTaggingSecret: Fr; leftMostIndex: number; rightMostIndex: number }[],
): IndexedTaggingSecret[] {
Expand Down

0 comments on commit b4a6666

Please sign in to comment.