Skip to content

Commit

Permalink
test fix
Browse files Browse the repository at this point in the history
  • Loading branch information
benesjan committed May 9, 2024
1 parent 6ee3ced commit 364943c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
112 changes: 71 additions & 41 deletions yarn-project/simulator/src/client/private_execution.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import {
type L1ToL2Message,
Note,
PackedValues,
PublicDataWitness,
SiblingPath,
TxExecutionRequest,
} from '@aztec/circuit-types';
import {
Expand All @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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<Fr> } = {};
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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<DBOracle>();
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);
});

Expand Down Expand Up @@ -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)),
);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand All @@ -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]);
});
Expand Down

0 comments on commit 364943c

Please sign in to comment.