From 364943ca8b5a97a5ff6fa7d0db0b0ba434da7632 Mon Sep 17 00:00:00 2001 From: benesjan Date: Mon, 6 May 2024 13:14:00 +0000 Subject: [PATCH] test fix --- .../structs/private_circuit_public_inputs.ts | 1 - .../src/client/private_execution.test.ts | 112 +++++++++++------- 2 files changed, 71 insertions(+), 42 deletions(-) diff --git a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts index 8697c01cbdda..03c228469109 100644 --- a/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts +++ b/yarn-project/circuits.js/src/structs/private_circuit_public_inputs.ts @@ -39,7 +39,6 @@ import { TxContext } from './tx_context.js'; /** * Public inputs to a private circuit. - * @see abis/private_circuit_public_inputs.hpp. */ export class PrivateCircuitPublicInputs { constructor( diff --git a/yarn-project/simulator/src/client/private_execution.test.ts b/yarn-project/simulator/src/client/private_execution.test.ts index 3b93537e10c2..5fce8a9829e5 100644 --- a/yarn-project/simulator/src/client/private_execution.test.ts +++ b/yarn-project/simulator/src/client/private_execution.test.ts @@ -4,6 +4,8 @@ import { type L1ToL2Message, Note, PackedValues, + PublicDataWitness, + SiblingPath, TxExecutionRequest, } from '@aztec/circuit-types'; import { @@ -13,13 +15,13 @@ import { FunctionData, GasSettings, GeneratorIndex, - type GrumpkinPrivateKey, Header, L1_TO_L2_MSG_TREE_HEIGHT, NOTE_HASH_TREE_HEIGHT, + PUBLIC_DATA_TREE_HEIGHT, PartialStateReference, PublicCallRequest, - type PublicKey, + PublicDataTreeLeafPreimage, StateReference, TxContext, computeAppNullifierSecretKey, @@ -39,7 +41,7 @@ import { Fr } from '@aztec/foundation/fields'; import { type DebugLogger, createDebugLogger } from '@aztec/foundation/log'; import { type FieldsOf } from '@aztec/foundation/types'; import { openTmpStore } from '@aztec/kv-store/utils'; -import { type AppendOnlyTree, Pedersen, StandardTree, newTree } from '@aztec/merkle-tree'; +import { type AppendOnlyTree, INITIAL_LEAF, Pedersen, StandardTree, newTree } from '@aztec/merkle-tree'; import { ChildContractArtifact, ImportTestContractArtifact, @@ -79,14 +81,14 @@ describe('Private Execution test suite', () => { let ownerCompleteAddress: CompleteAddress; let recipientCompleteAddress: CompleteAddress; - let ownerMasterNullifierPublicKey: PublicKey; - let recipientMasterNullifierPublicKey: PublicKey; - let ownerMasterNullifierSecretKey: GrumpkinPrivateKey; - let recipientMasterNullifierSecretKey: GrumpkinPrivateKey; + // TODO(#5834): Nuke the following once complete address is refactored + let allOwnerKeys: any; + let allRecipientKeys: any; const treeHeights: { [name: string]: number } = { noteHash: NOTE_HASH_TREE_HEIGHT, l1ToL2Messages: L1_TO_L2_MSG_TREE_HEIGHT, + publicData: PUBLIC_DATA_TREE_HEIGHT, }; let trees: { [name: keyof typeof treeHeights]: AppendOnlyTree } = {}; @@ -139,7 +141,7 @@ describe('Private Execution test suite', () => { // Create a new snapshot. const newSnap = new AppendOnlyTreeSnapshot(Fr.fromBuffer(tree.getRoot(true)), Number(tree.getNumLeaves(true))); - if (name === 'noteHash' || name === 'l1ToL2Messages') { + if (name === 'noteHash' || name === 'l1ToL2Messages' || 'publicData') { header = new Header( header.lastArchive, header.contentCommitment, @@ -148,7 +150,7 @@ describe('Private Execution test suite', () => { new PartialStateReference( name === 'noteHash' ? newSnap : header.state.partial.noteHashTree, header.state.partial.nullifierTree, - header.state.partial.publicDataTree, + name === 'publicData' ? newSnap : header.state.partial.publicDataTree, ), ), header.globalVariables, @@ -175,42 +177,83 @@ describe('Private Execution test suite', () => { const ownerPartialAddress = Fr.random(); ownerCompleteAddress = CompleteAddress.fromSecretKeyAndPartialAddress(ownerSk, ownerPartialAddress); - - const allOwnerKeys = deriveKeys(ownerSk); - ownerMasterNullifierPublicKey = allOwnerKeys.masterNullifierPublicKey; - ownerMasterNullifierSecretKey = allOwnerKeys.masterNullifierSecretKey; + allOwnerKeys = deriveKeys(ownerSk); const recipientPartialAddress = Fr.random(); recipientCompleteAddress = CompleteAddress.fromSecretKeyAndPartialAddress(recipientSk, recipientPartialAddress); - - const allRecipientKeys = deriveKeys(recipientSk); - recipientMasterNullifierPublicKey = allRecipientKeys.masterNullifierPublicKey; - recipientMasterNullifierSecretKey = allRecipientKeys.masterNullifierSecretKey; + allRecipientKeys = deriveKeys(recipientSk); owner = ownerCompleteAddress.address; recipient = recipientCompleteAddress.address; }); - beforeEach(() => { + beforeEach(async () => { trees = {}; oracle = mock(); oracle.getNullifierKeys.mockImplementation((accountAddress: AztecAddress, contractAddress: AztecAddress) => { if (accountAddress.equals(ownerCompleteAddress.address)) { return Promise.resolve({ - masterNullifierPublicKey: ownerMasterNullifierPublicKey, - appNullifierSecretKey: computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), + masterNullifierPublicKey: allOwnerKeys.masterNullifierPublicKey, + appNullifierSecretKey: computeAppNullifierSecretKey(allOwnerKeys.masterNullifierSecretKey, contractAddress), }); } if (accountAddress.equals(recipientCompleteAddress.address)) { return Promise.resolve({ - masterNullifierPublicKey: recipientMasterNullifierPublicKey, - appNullifierSecretKey: computeAppNullifierSecretKey(recipientMasterNullifierSecretKey, contractAddress), + masterNullifierPublicKey: allRecipientKeys.masterNullifierPublicKey, + appNullifierSecretKey: computeAppNullifierSecretKey( + allRecipientKeys.masterNullifierSecretKey, + contractAddress, + ), }); } throw new Error(`Unknown address ${accountAddress}`); }); + + // We call insertLeaves here with no leaves to populate empty public data tree root --> this is necessary to be + // able to get ivpk_m during execution + await insertLeaves([], 'publicData'); oracle.getHeader.mockResolvedValue(header); + oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => { + if (address.equals(owner)) { + return Promise.resolve(ownerCompleteAddress); + } + if (address.equals(recipient)) { + return Promise.resolve(recipientCompleteAddress); + } + throw new Error(`Unknown address ${address}`); + }); + // TODO(#5834): The following oracle should be unnecessary + oracle.getPublicKeysForAddress.mockImplementation((address: AztecAddress) => { + if (address.equals(owner)) { + return Promise.resolve([ + allOwnerKeys.masterNullifierPublicKey, + allOwnerKeys.masterIncomingViewingPublicKey, + allOwnerKeys.masterOutgoingViewingPublicKey, + allOwnerKeys.masterTaggingPublicKey, + ]); + } + if (address.equals(recipient)) { + return Promise.resolve([ + allRecipientKeys.masterNullifierPublicKey, + allRecipientKeys.masterIncomingViewingPublicKey, + allRecipientKeys.masterOutgoingViewingPublicKey, + allRecipientKeys.masterTaggingPublicKey, + ]); + } + throw new Error(`Unknown address ${address}`); + }); + // This oracle gets called when reading ivpk_m from key registry --> we return zero witness indicating that + // the keys were not registered. This triggers non-registered keys flows in which getCompleteAddress oracle + // gets called and we constrain the result by hashing address preimage and checking it matches. + oracle.getPublicDataTreeWitness.mockResolvedValue( + new PublicDataWitness( + 0n, + PublicDataTreeLeafPreimage.empty(), + SiblingPath.ZERO(PUBLIC_DATA_TREE_HEIGHT, INITIAL_LEAF, new Pedersen()), + ), + ); + acirSimulator = new AcirSimulator(oracle, node); }); @@ -286,16 +329,6 @@ describe('Private Execution test suite', () => { }; beforeEach(() => { - oracle.getCompleteAddress.mockImplementation((address: AztecAddress) => { - if (address.equals(owner)) { - return Promise.resolve(ownerCompleteAddress); - } - if (address.equals(recipient)) { - return Promise.resolve(recipientCompleteAddress); - } - throw new Error(`Unknown address ${address}`); - }); - oracle.getFunctionArtifactByName.mockImplementation((_, functionName: string) => Promise.resolve(getFunctionArtifact(StatefulTestContractArtifact, functionName)), ); @@ -929,7 +962,7 @@ describe('Private Execution test suite', () => { const nullifier = result.callStackItem.publicInputs.newNullifiers[0]; const expectedNullifier = poseidon2Hash([ innerNoteHash, - computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), + computeAppNullifierSecretKey(allOwnerKeys.masterNullifierSecretKey, contractAddress), GeneratorIndex.NOTE_NULLIFIER, ]); expect(nullifier.value).toEqual(expectedNullifier); @@ -1008,7 +1041,7 @@ describe('Private Execution test suite', () => { const nullifier = execGetThenNullify.callStackItem.publicInputs.newNullifiers[0]; const expectedNullifier = poseidon2Hash([ innerNoteHash, - computeAppNullifierSecretKey(ownerMasterNullifierSecretKey, contractAddress), + computeAppNullifierSecretKey(allOwnerKeys.masterNullifierSecretKey, contractAddress), GeneratorIndex.NOTE_NULLIFIER, ]); expect(nullifier.value).toEqual(expectedNullifier); @@ -1034,17 +1067,14 @@ describe('Private Execution test suite', () => { }); }); - describe('get public key', () => { + describe('get master incoming viewing public key', () => { it('gets the public key for an address', async () => { // Tweak the contract artifact so we can extract return values - const artifact = getFunctionArtifact(TestContractArtifact, 'get_public_key'); + const artifact = getFunctionArtifact(TestContractArtifact, 'get_master_incoming_viewing_public_key'); - // Generate a partial address, pubkey, and resulting address - const completeAddress = CompleteAddress.random(); - const args = [completeAddress.address]; - const pubKey = completeAddress.publicKey; + const args = [recipientCompleteAddress.address]; + const pubKey = recipientCompleteAddress.publicKey; - oracle.getCompleteAddress.mockResolvedValue(completeAddress); const result = await runSimulator({ artifact, args }); expect(result.returnValues).toEqual([pubKey.x, pubKey.y]); });