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: 1 addition & 1 deletion yarn-project/acir-simulator/src/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ describe('ACIR simulator', () => {
const acirSimulator = new AcirSimulator(oracle as unknown as DBOracle);

beforeAll(async () => {
bbWasm = await BarretenbergWasm.new();
bbWasm = await BarretenbergWasm.get();
});

describe('empty constructor', () => {
Expand Down
18 changes: 8 additions & 10 deletions yarn-project/aztec-node/src/aztec-node/aztec-node.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
import { default as levelup } from 'levelup';
import { default as memdown } from 'memdown';
import { Archiver } from '@aztec/archiver';
import { AztecAddress } from '@aztec/foundation';
import { ContractData, L2Block, L2BlockSource } from '@aztec/l2-block';
import { SiblingPath } from '@aztec/merkle-tree';
import { P2P, P2PClient } from '@aztec/p2p';
import { Tx, TxHash } from '@aztec/tx';
import { MerkleTrees, WorldStateSynchroniser, ServerWorldStateSynchroniser, MerkleTreeId } from '@aztec/world-state';
import { SequencerClient } from '@aztec/sequencer-client';
import { AztecNodeConfig } from './config.js';
import { SiblingPath } from '@aztec/merkle-tree';
import { AztecAddress } from '@aztec/foundation';
import { CircuitsWasm } from '@aztec/circuits.js';
import { Tx, TxHash } from '@aztec/tx';
import { UnverifiedData, UnverifiedDataSource } from '@aztec/unverified-data';
import { MerkleTreeId, MerkleTrees, ServerWorldStateSynchroniser, WorldStateSynchroniser } from '@aztec/world-state';
import { default as levelup } from 'levelup';

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Unrelated but we should banish this default as pattern, there is dedicated default import syntax

import { default as memdown } from 'memdown';
import { AztecNodeConfig } from './config.js';

/* eslint-disable @typescript-eslint/ban-ts-comment */
// @ts-ignore
Expand Down Expand Up @@ -49,8 +48,7 @@ export class AztecNode {
await Promise.all([p2pClient.start(), worldStateSynchroniser.start()]);

// now create the sequencer
const wasm = await CircuitsWasm.new();
const sequencer = await SequencerClient.new(config, p2pClient, worldStateSynchroniser, wasm);
const sequencer = await SequencerClient.new(config, p2pClient, worldStateSynchroniser);
return new AztecNode(p2pClient, archiver, archiver, merkleTreeDB, worldStateSynchroniser, sequencer);
}

Expand Down
2 changes: 1 addition & 1 deletion yarn-project/aztec-node/test/aztec_node.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ describe('AztecNode', () => {
let wasm: CircuitsWasm;

beforeAll(async () => {
wasm = await CircuitsWasm.new();
wasm = await CircuitsWasm.get();
});

beforeEach(async () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ describe('Account State', () => {
};

beforeAll(async () => {
const wasm = await BarretenbergWasm.new();
const wasm = await BarretenbergWasm.get();
grumpkin = new Grumpkin(wasm);
owner = ConstantKeyPair.random(grumpkin);
});
Expand Down
23 changes: 7 additions & 16 deletions yarn-project/aztec-rpc/src/aztec_rpc_server/aztec_rpc_server.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { AcirSimulator, encodeArguments } from '@aztec/acir-simulator';
import { AztecNode } from '@aztec/aztec-node';
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';
import {
AztecAddress,
CONTRACT_TREE_HEIGHT,
Expand Down Expand Up @@ -46,11 +45,9 @@ export class AztecRPCServer implements AztecRPCClient {
private kernelProver: KernelProver,
private node: AztecNode,
private db: Database,
private circuitsWasm: CircuitsWasm,
bbWasm: BarretenbergWasm,
private log = createDebugLogger('aztec:rpc_server'),
) {
this.synchroniser = new Synchroniser(node, db, acirSimulator, bbWasm);
this.synchroniser = new Synchroniser(node, db, acirSimulator);
this.synchroniser.start();
}

Expand Down Expand Up @@ -109,6 +106,7 @@ export class AztecRPCServer implements AztecRPCClient {
contractAddressSalt = Fr.random(),
from?: AztecAddress,
) {
const wasm = await CircuitsWasm.get();
const fromAddress = this.ensureAccountOrDefault(from);

const constructorAbi = abi.functions.find(f => f.name === 'constructor');
Expand All @@ -121,22 +119,15 @@ export class AztecRPCServer implements AztecRPCClient {
}

const flatArgs = encodeArguments(constructorAbi, args);
const contractTree = await ContractTree.new(
abi,
flatArgs,
portalContract,
contractAddressSalt,
fromAddress,
this.circuitsWasm,
);
const contractTree = await ContractTree.new(abi, flatArgs, portalContract, contractAddressSalt, fromAddress);

const functionData = new FunctionData(
generateFunctionSelector(constructorAbi.name, constructorAbi.parameters),
true,
true,
);

const constructorVkHash = await hashVK(this.circuitsWasm, Buffer.from(constructorAbi.verificationKey, 'hex'));
const constructorVkHash = await hashVK(wasm, Buffer.from(constructorAbi.verificationKey, 'hex'));

const functionTreeRoot = await contractTree.getFunctionTreeRoot();

Expand Down Expand Up @@ -218,7 +209,6 @@ export class AztecRPCServer implements AztecRPCClient {
signature,
executionResult,
oldRoots,
this.circuitsWasm,
(callStackItem: PrivateCallStackItem) => {
return this.getFunctionTreeInfo(contract, callStackItem);
},
Expand Down Expand Up @@ -247,7 +237,8 @@ export class AztecRPCServer implements AztecRPCClient {
}

private async computeFunctionTreeInfo(contract: ContractDao, callStackItem: PrivateCallStackItem) {
const tree = new ContractTree(contract, this.circuitsWasm);
const wasm = await CircuitsWasm.get();
const tree = new ContractTree(contract, wasm);
const root = await tree.getFunctionTreeRoot();
const functionIndex =
contract.functions.findIndex(f => f.selector.equals(callStackItem.functionData.functionSelector)) - 1;
Expand All @@ -259,7 +250,7 @@ export class AztecRPCServer implements AztecRPCClient {
}

const leaves = await tree.getFunctionLeaves();
const functionTree = await computeFunctionTree(this.circuitsWasm, leaves);
const functionTree = await computeFunctionTree(wasm, leaves);
const functionTreeData = computeFunctionTreeData(functionTree, functionIndex);
const membershipWitness = new MembershipWitness<typeof FUNCTION_TREE_HEIGHT>(
FUNCTION_TREE_HEIGHT,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import { AcirSimulator } from '@aztec/acir-simulator';
import { AztecNode } from '@aztec/aztec-node';
import { CircuitsWasm } from '@aztec/circuits.js/wasm';
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';
import { Grumpkin } from '@aztec/barretenberg.js/crypto';
import { KernelProver } from '@aztec/kernel-prover';
import { MemoryDB } from '../database/index.js';
import { KeyStore, TestKeyStore } from '../key_store/index.js';
import { SimulatorOracle } from '../simulator_oracle/index.js';
import { AztecRPCServer } from './aztec_rpc_server.js';
import { Grumpkin } from '@aztec/barretenberg.js/crypto';

export async function createAztecRPCServer(
aztecNode: AztecNode,
Expand All @@ -16,25 +14,19 @@ export async function createAztecRPCServer(
db,
acirSimulator,
kernelProver,
circuitsWasm,
bbWasm,
}: {
keyStore?: KeyStore;
db?: MemoryDB;
acirSimulator?: AcirSimulator;
kernelProver?: KernelProver;
circuitsWasm?: CircuitsWasm;
bbWasm?: BarretenbergWasm;
} = {},
) {
bbWasm = bbWasm || (await BarretenbergWasm.new());
circuitsWasm = circuitsWasm || (await CircuitsWasm.new());
keyStore = keyStore || new TestKeyStore(new Grumpkin(bbWasm));
keyStore = keyStore || new TestKeyStore(await Grumpkin.new());
db = db || new MemoryDB();
acirSimulator = acirSimulator || new AcirSimulator(new SimulatorOracle(db, keyStore));
kernelProver = kernelProver || new KernelProver();

const server = new AztecRPCServer(keyStore, acirSimulator, kernelProver, aztecNode, db, circuitsWasm, bbWasm);
const server = new AztecRPCServer(keyStore, acirSimulator, kernelProver, aztecNode, db);
await server.start();
return server;
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('encrypt buffer', () => {
let grumpkin: Grumpkin;

beforeAll(async () => {
grumpkin = new Grumpkin(await BarretenbergWasm.new());
grumpkin = new Grumpkin(await BarretenbergWasm.get());
});

it('derive shared secret', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ describe('tx_aux_data', () => {
let grumpkin: Grumpkin;

beforeAll(async () => {
grumpkin = new Grumpkin(await BarretenbergWasm.new());
grumpkin = new Grumpkin(await BarretenbergWasm.get());
});

it('convert to and from buffer', () => {
Expand Down
4 changes: 2 additions & 2 deletions yarn-project/aztec-rpc/src/contract_tree/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ import {
} from '@aztec/circuits.js/abis';
import { CircuitsWasm } from '@aztec/circuits.js/wasm';
import { AztecAddress, EthAddress, Fr, keccak } from '@aztec/foundation';
import { ContractAbi, FunctionType } from '@aztec/noir-contracts';
import { generateFunctionSelector } from '../abi_coder/index.js';
import { ContractDao, ContractFunctionDao } from '../contract_database/index.js';
import { ContractAbi, FunctionType } from '@aztec/noir-contracts';

function isConstructor({ name }: { name: string }) {
return name === 'constructor';
Expand Down Expand Up @@ -47,8 +47,8 @@ export class ContractTree {
portalContract: EthAddress,
contractAddressSalt: Fr,
from: AztecAddress,
wasm: CircuitsWasm,
) {
const wasm = await CircuitsWasm.get();
const constructorFunc = abi.functions.find(isConstructor);
if (!constructorFunc) {
throw new Error('Constructor not found.');
Expand Down
6 changes: 2 additions & 4 deletions yarn-project/aztec-rpc/src/synchroniser/synchroniser.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { AcirSimulator } from '@aztec/acir-simulator';
import { AztecNode } from '@aztec/aztec-node';
import { Grumpkin } from '@aztec/barretenberg.js/crypto';
import { BarretenbergWasm } from '@aztec/barretenberg.js/wasm';
import { mock } from 'jest-mock-extended';
import { Database, MemoryDB } from '../database/index.js';
import { ConstantKeyPair } from '../key_store/index.js';
Expand All @@ -15,15 +14,14 @@ describe('Synchroniser', () => {
let synchroniser: Synchroniser;

beforeAll(async () => {
const wasm = await BarretenbergWasm.new();
grumpkin = new Grumpkin(wasm);
grumpkin = await Grumpkin.new();

aztecNode = mock<AztecNode>();
aztecNode.getUnverifiedData.mockResolvedValue([]);

database = new MemoryDB();
simulator = mock<AcirSimulator>();
synchroniser = new Synchroniser(aztecNode, database, simulator, wasm);
synchroniser = new Synchroniser(aztecNode, database, simulator);
});

it('Should create account state', async () => {
Expand Down
3 changes: 1 addition & 2 deletions yarn-project/aztec-rpc/src/synchroniser/synchroniser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ export class Synchroniser {
private node: AztecNode,
private db: Database,
private simulator: AcirSimulator,
private bbWasm: BarretenbergWasm,
private log = createDebugLogger('aztec:aztec_rpc_synchroniser'),
) {}

Expand Down Expand Up @@ -80,7 +79,7 @@ export class Synchroniser {
}

public async addAccount(privKey: Buffer) {
this.accountStates.push(new AccountState(privKey, this.db, this.simulator, this.node, new Grumpkin(this.bbWasm)));
this.accountStates.push(new AccountState(privKey, this.db, this.simulator, this.node, await Grumpkin.new()));
await Promise.resolve();
}

Expand Down
5 changes: 5 additions & 0 deletions yarn-project/barretenberg.js/src/crypto/grumpkin/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { WasmWrapper } from '@aztec/foundation/wasm';
import { BarretenbergWasm } from '../../index.js';

export class Grumpkin {
public static async new() {
return new this(await BarretenbergWasm.get());
}

constructor(private wasm: WasmWrapper) {}

// prettier-ignore
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('pedersen', () => {
const values: Buffer[] = [];

beforeAll(async () => {
barretenbergWasm = await BarretenbergWasm.new();
barretenbergWasm = await BarretenbergWasm.get();

// TODO was originally 2 ** 12
for (let i = 0; i < 2 ** 2; ++i) {
Expand Down
14 changes: 7 additions & 7 deletions yarn-project/barretenberg.js/src/wasm/barretenberg_wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ const NAME = 'barretenberg';
export class BarretenbergWasm extends AsyncWasmWrapper {
codePath = isNode ? join(dirname(fileURLToPath(import.meta.url)), `${NAME}.wasm`) : `${NAME}.wasm`;

static instance: Promise<BarretenbergWasm>;

/**
* Create and initialize a BarretenbergWasm module.
* @param initial - Initial memory pages.
* @returns The module.
* Get a singleton instance of the module.
* @returns The singleton.
*/
public static async new(initial?: number) {
const barretenberg = new BarretenbergWasm();
await barretenberg.init(initial);
return barretenberg;
public static get(): Promise<BarretenbergWasm> {
if (!this.instance) this.instance = new BarretenbergWasm().init();
return this.instance;
}

constructor(loggerName?: string) {
Expand Down
1 change: 0 additions & 1 deletion yarn-project/barretenberg.js/src/wasm/primitives_wasm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ const NAME = 'primitives';
* A low-level wrapper for an instance of the barretenberg primitives wasm.
*/
export class PrimitivesWasm extends WasmWrapper {
// TODO: Load primitives.wasm instead of bb.wasm
codePath = isNode ? join(dirname(fileURLToPath(import.meta.url)), `${NAME}.wasm`) : `${NAME}.wasm`;

static instance: Promise<PrimitivesWasm>;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ AztecAddress {

exports[`abis wasm bindings computes a function leaf 1`] = `
Fr {
"value": 21718073846539784036456101390499474428767331519255676962044216461737209091935n,
"value": 16255853943620434246084547000263971056928483888227499632732320436242372959484n,
}
`;

Expand Down
6 changes: 3 additions & 3 deletions yarn-project/circuits.js/src/abis/abis.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ import {

describe('abis wasm bindings', () => {
let wasm: CircuitsWasm;
beforeEach(async () => {
wasm = await CircuitsWasm.new();
beforeAll(async () => {
wasm = await CircuitsWasm.get();
});

it('hashes a tx request', async () => {
Expand All @@ -40,7 +40,7 @@ describe('abis wasm bindings', () => {
});

it('computes a function leaf', async () => {
const leaf = Buffer.alloc(32);
const leaf = Buffer.alloc(32 + 1 + 32 + 32, 0);
const res = await computeFunctionLeaf(wasm, leaf);
expect(res).toMatchSnapshot();
});
Expand Down
2 changes: 2 additions & 0 deletions yarn-project/circuits.js/src/abis/abis.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ export async function hashVK(wasm: CircuitsWasm, vkBuf: Buffer) {
}

export async function computeFunctionLeaf(wasm: CircuitsWasm, fnLeaf: Buffer) {
// Size must match circuits/cpp/src/aztec3/circuits/abis/function_leaf_preimage.hpp
if (fnLeaf.length !== 32 + 1 + 32 + 32) throw new Error(`Invalid length for function leaf`);
wasm.call('pedersen__init');
return Fr.fromBuffer(await wasmAsyncCall(wasm, 'abis__compute_function_leaf', { toBuffer: () => fnLeaf }, 32));
}
Expand Down
2 changes: 1 addition & 1 deletion yarn-project/circuits.js/src/kernel/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ describe('abis wasm bindings', () => {
let wasm: CircuitsWasm;

beforeAll(async () => {
wasm = await CircuitsWasm.new();
wasm = await CircuitsWasm.get();
});

it('gets dummy kernel data', async () => {
Expand Down
6 changes: 0 additions & 6 deletions yarn-project/circuits.js/src/structs/kernel.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,33 +4,27 @@ import { CircuitsWasm } from '../wasm/circuits_wasm.js';

describe('structs/kernel', () => {
it(`serializes and prints previous_kernel_data`, async () => {
const wasm = await CircuitsWasm.new();
const previousKernelData = makePreviousKernelData();
await expectSerializeToMatchSnapshot(
previousKernelData.toBuffer(),
'abis__test_roundtrip_serialize_previous_kernel_data',
wasm,
);
});

it(`serializes and prints private_kernel_inputs`, async () => {
const wasm = await CircuitsWasm.new();
const kernelInputs = makePrivateKernelInputs();
await expectSerializeToMatchSnapshot(
kernelInputs.toBuffer(),
'abis__test_roundtrip_serialize_private_kernel_inputs',
wasm,
);
});

// TODO: Reenable once we can move back to circuits master and have this c_bind available
it.skip(`serializes and prints private_kernel_public_inputs`, async () => {
const wasm = await CircuitsWasm.new();
const kernelInputs = makePrivateKernelPublicInputs();
await expectSerializeToMatchSnapshot(
kernelInputs.toBuffer(),
'abis__test_roundtrip_serialize_private_kernel_public_inputs',
wasm,
);
});
});
Loading