Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Lazy wasm pt4 #11491

Merged
merged 49 commits into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
49 commits
Select commit Hold shift + click to select a range
14af224
grumpkin math async
Thunkar Jan 23, 2025
4992d11
fmt
Thunkar Jan 23, 2025
b3f696c
Merge branch 'master' into gj/lazy_wasm_pt3
Thunkar Jan 23, 2025
36c38dd
wip
Thunkar Jan 23, 2025
ecaf467
fmt
Thunkar Jan 23, 2025
0001174
Merge branch 'gj/lazy_wasm_pt3' of github.com:AztecProtocol/aztec-pac…
Thunkar Jan 23, 2025
db661a6
wip
Thunkar Jan 23, 2025
a9754a1
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 23, 2025
72299dc
wip
Thunkar Jan 23, 2025
a5df41d
don't mess with the trees
Thunkar Jan 23, 2025
8e9655a
kill me
Thunkar Jan 23, 2025
54a7035
maybe
Thunkar Jan 23, 2025
727d4db
fmt and fixes
Thunkar Jan 24, 2025
8a01427
fixing tests
Thunkar Jan 24, 2025
17b5a9e
fixes
Thunkar Jan 24, 2025
5a15198
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 24, 2025
f441fce
more test fixes
Thunkar Jan 24, 2025
8531c7f
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 24, 2025
50633b5
fixes
Thunkar Jan 24, 2025
f5a0d2d
fixes
Thunkar Jan 24, 2025
337c0c9
more fixes
Thunkar Jan 24, 2025
92f1400
more fixes
Thunkar Jan 24, 2025
12e1f7a
wip
Thunkar Jan 24, 2025
bb55e12
fix
Thunkar Jan 24, 2025
5dbd1fd
fixed stats
Thunkar Jan 24, 2025
1ad7cfa
more fixes
Thunkar Jan 26, 2025
0dc360d
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 26, 2025
bbaf649
fix
Thunkar Jan 26, 2025
dd53866
fixes
Thunkar Jan 26, 2025
a2c56ec
fix e2e
Thunkar Jan 26, 2025
156bf34
fixed broadcasting functions
Thunkar Jan 26, 2025
dfb4167
cleanup
Thunkar Jan 26, 2025
5d44545
more e2e
Thunkar Jan 26, 2025
9da825c
restored completeaddress validation
Thunkar Jan 26, 2025
f7994be
lazy everything
Thunkar Jan 27, 2025
8e4fcf3
better api
Thunkar Jan 27, 2025
b9d9655
simplified API
Thunkar Jan 27, 2025
b0d2b80
minor fixes
Thunkar Jan 27, 2025
2e8d083
fmt
Thunkar Jan 27, 2025
77cceb1
fmt
Thunkar Jan 27, 2025
aa3734e
fixed test
Thunkar Jan 27, 2025
e7e43a6
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 27, 2025
b7499b0
Merge branch 'master' of github.com:AztecProtocol/aztec-packages into…
Thunkar Jan 27, 2025
9bd9f58
fixes
Thunkar Jan 27, 2025
911cfa8
removed gz
Thunkar Jan 27, 2025
8c350c7
fixed gitignore
Thunkar Jan 27, 2025
9ae069f
fixes
Thunkar Jan 27, 2025
20deb03
fmt
Thunkar Jan 27, 2025
262c7e9
Merge branch 'master' into gj/lazy_wasm_pt4
Thunkar Jan 27, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
2 changes: 1 addition & 1 deletion barretenberg/ts/src/barretenberg/blake2s.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ describe('blake2s sync', () => {
let api: BarretenbergSync;

beforeAll(async () => {
api = await BarretenbergSync.new();
api = await BarretenbergSync.initSingleton();
});

it('blake2s', () => {
Expand Down
46 changes: 8 additions & 38 deletions barretenberg/ts/src/barretenberg/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,26 +89,28 @@ export class Barretenberg extends BarretenbergApi {
}
}

let barrentenbergSyncSingletonPromise: Promise<BarretenbergSync>;
let barretenbergSyncSingleton: BarretenbergSync;
let barretenbergSyncSingletonPromise: Promise<BarretenbergSync>;

export class BarretenbergSync extends BarretenbergApiSync {
private constructor(wasm: BarretenbergWasmMain) {
super(wasm);
}

static async new() {
private static async new() {
const wasm = new BarretenbergWasmMain();
const { module, threads } = await fetchModuleAndThreads(1);
await wasm.init(module, threads);
return new BarretenbergSync(wasm);
}

static initSingleton() {
if (!barretenbergSyncSingletonPromise) {
barretenbergSyncSingletonPromise = BarretenbergSync.new().then(s => (barretenbergSyncSingleton = s));
static async initSingleton() {
if (!barrentenbergSyncSingletonPromise) {
barrentenbergSyncSingletonPromise = BarretenbergSync.new();
}
return barretenbergSyncSingletonPromise;

barretenbergSyncSingleton = await barrentenbergSyncSingletonPromise;
return barretenbergSyncSingleton;
}

static getSingleton() {
Expand All @@ -122,35 +124,3 @@ export class BarretenbergSync extends BarretenbergApiSync {
return this.wasm;
}
}

let barrentenbergLazySingleton: BarretenbergLazy;

export class BarretenbergLazy extends BarretenbergApi {
private constructor(wasm: BarretenbergWasmMain) {
super(wasm);
}

private static async new() {
const wasm = new BarretenbergWasmMain();
const { module, threads } = await fetchModuleAndThreads(1);
await wasm.init(module, threads);
return new BarretenbergLazy(wasm);
}

static async getSingleton() {
if (!barrentenbergLazySingleton) {
barrentenbergLazySingleton = await BarretenbergLazy.new();
}
return barrentenbergLazySingleton;
}

getWasm() {
return this.wasm;
}
}

// If we're in ESM environment, use top level await. CJS users need to call it manually.
// Need to ignore for cjs build.
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore
await BarretenbergSync.initSingleton(); // POSTPROCESS ESM ONLY
2 changes: 1 addition & 1 deletion barretenberg/ts/src/barretenberg/pedersen.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('pedersen sync', () => {
let api: BarretenbergSync;

beforeAll(async () => {
api = await BarretenbergSync.new();
api = await BarretenbergSync.initSingleton();
});

it('pedersenHash', () => {
Expand Down
2 changes: 1 addition & 1 deletion barretenberg/ts/src/barretenberg/poseidon.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ describe('poseidon sync', () => {
let api: BarretenbergSync;

beforeAll(async () => {
api = await BarretenbergSync.new();
api = await BarretenbergSync.initSingleton();
});

it('poseidonHash', () => {
Expand Down
12 changes: 12 additions & 0 deletions barretenberg/ts/src/barretenberg_api/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,18 @@ export class BarretenbergApi {
return out[0];
}

async poseidon2HashAccumulate(inputsBuffer: Fr[]): Promise<Fr> {
const inArgs = [inputsBuffer].map(serializeBufferable);
const outTypes: OutputType[] = [Fr];
const result = await this.wasm.callWasmExport(
'poseidon2_hash_accumulate',
inArgs,
outTypes.map(t => t.SIZE_IN_BYTES),
);
const out = result.map((r, i) => outTypes[i].fromBuffer(r));
return out[0];
}

async poseidon2Hashes(inputsBuffer: Fr[]): Promise<Fr> {
const inArgs = [inputsBuffer].map(serializeBufferable);
const outTypes: OutputType[] = [Fr];
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,9 @@
import barretenbergModule from '../../barretenberg.wasm.gz';
import barretenbergThreadsModule from '../../barretenberg-threads.wasm.gz';
import pako from 'pako';

// Annoyingly the wasm declares if it's memory is shared or not. So now we need two wasms if we want to be
// able to fallback on "non shared memory" situations.
export async function fetchCode(multithreaded: boolean) {
const res = await fetch(multithreaded ? barretenbergThreadsModule : barretenbergModule);
const compressedData = await res.arrayBuffer();
const decompressedData = pako.ungzip(new Uint8Array(compressedData));
return decompressedData.buffer;
return res.arrayBuffer();
}
2 changes: 1 addition & 1 deletion barretenberg/ts/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ export {
BackendOptions,
Barretenberg,
BarretenbergSync,
BarretenbergLazy,
BarretenbergVerifier,
UltraPlonkBackend,
UltraHonkBackend,
AztecClientBackend,
} from './barretenberg/index.js';

export { RawBuffer, Fr } from './types/index.js';
export { splitHonkProof, reconstructHonkProof, ProofData } from './proof/index.js';
2 changes: 1 addition & 1 deletion barretenberg/ts/tsconfig.browser.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@
"outDir": "dest/browser",
"tsBuildInfoFile": ".tsbuildinfo.browser"
},
"include": ["src", "src/barretenberg_wasm/barretenberg_wasm_thread/factory/browser/thread.worker.ts"]
"include": ["!src/main.ts", "src", "src/barretenberg_wasm/barretenberg_wasm_thread/factory/browser/thread.worker.ts"]
}
2 changes: 1 addition & 1 deletion barretenberg/ts/webpack.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export default {
rules: [
{
test: /\.wasm\.gz$/,
type: 'asset/inline',
type: 'asset/resource'
},
{
test: /\.worker\.ts$/,
Expand Down
3 changes: 2 additions & 1 deletion gaztec/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"typescript": "~5.7.3",
"typescript-eslint": "^8.11.0",
"vite": "^6.0.7",
"vite-plugin-node-polyfills": "^0.22.0"
"vite-plugin-node-polyfills": "^0.22.0",
"vite-plugin-static-copy": "^2.2.0"
}
}
9 changes: 7 additions & 2 deletions gaztec/src/App.tsx
Original file line number Diff line number Diff line change
@@ -1,13 +1,18 @@
import { Home } from "./components/home/home";
import { Global } from "@emotion/react";
import { ThemeProvider } from "@mui/material/styles";
import { globalStyle, theme } from "./common.styles";
import { Suspense, lazy } from "react";
import { LinearProgress } from "@mui/material";

const Home = lazy(() => import("./components/home/home"));

function App() {
return (
<ThemeProvider theme={theme}>
<Global styles={globalStyle}></Global>
<Home />
<Suspense fallback={<LinearProgress />}>
<Home />
</Suspense>
</ThemeProvider>
);
}
Expand Down
46 changes: 46 additions & 0 deletions gaztec/src/config.ts → gaztec/src/aztecEnv.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import {
createAztecNodeClient,
type PXE,
AztecNode,
AccountWalletWithSecretKey,
AztecAddress,
Contract,
} from "@aztec/aztec.js";
import { PXEService } from "@aztec/pxe/service";
import { PXEServiceConfig, getPXEServiceConfig } from "@aztec/pxe/config";
Expand All @@ -13,6 +16,9 @@ import { createStore } from "@aztec/kv-store/indexeddb";
import { BBWASMLazyPrivateKernelProver } from "@aztec/bb-prover/wasm/lazy";
import { WASMSimulator } from "@aztec/simulator/client";
import { debug } from "debug";
import { createContext } from "react";
import { WalletDB } from "./utils/storage";
import { ContractFunctionInteractionTx } from "./utils/txs";

process.env = Object.keys(import.meta.env).reduce((acc, key) => {
acc[key.replace("VITE_", "")] = import.meta.env[key];
Expand All @@ -21,6 +27,46 @@ process.env = Object.keys(import.meta.env).reduce((acc, key) => {

debug.enable("*");

export const AztecContext = createContext<{
pxe: PXE | null;
nodeURL: string;
node: AztecNode;
wallet: AccountWalletWithSecretKey | null;
isPXEInitialized: boolean;
walletDB: WalletDB | null;
currentContractAddress: AztecAddress;
currentContract: Contract;
currentTx: ContractFunctionInteractionTx;
setWalletDB: (walletDB: WalletDB) => void;
setPXEInitialized: (isPXEInitialized: boolean) => void;
setWallet: (wallet: AccountWalletWithSecretKey) => void;
setAztecNode: (node: AztecNode) => void;
setPXE: (pxe: PXE) => void;
setNodeURL: (nodeURL: string) => void;
setCurrentTx: (currentTx: ContractFunctionInteractionTx) => void;
setCurrentContract: (currentContract: Contract) => void;
setCurrentContractAddress: (currentContractAddress: AztecAddress) => void;
}>({
pxe: null,
nodeURL: "",
node: null,
wallet: null,
isPXEInitialized: false,
walletDB: null,
currentContract: null,
currentContractAddress: null,
currentTx: null,
setWalletDB: (walletDB: WalletDB) => {},
setPXEInitialized: (isPXEInitialized: boolean) => {},
setWallet: (wallet: AccountWalletWithSecretKey) => {},
setNodeURL: (nodeURL: string) => {},
setPXE: (pxe: PXE) => {},
setAztecNode: (node: AztecNode) => {},
setCurrentTx: (currentTx: ContractFunctionInteractionTx) => {},
setCurrentContract: (currentContract: Contract) => {},
setCurrentContractAddress: (currentContractAddress: AztecAddress) => {},
});

export class AztecEnv {
static async connectToNode(nodeURL: string): Promise<AztecNode> {
const aztecNode = await createAztecNodeClient(nodeURL);
Expand Down
4 changes: 2 additions & 2 deletions gaztec/src/components/common/fnParameter.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,9 @@ import {
formatFrAsString,
parseAliasedBuffersAsString,
} from "../../utils/conversion";
import { Fragment, useContext, useState } from "react";
import { useContext, useState } from "react";
import EditIcon from "@mui/icons-material/Edit";
import { AztecContext } from "../home/home";
import { AztecContext } from "../../aztecEnv";

const container = css({
display: "flex",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
css,
} from "@mui/material";
import { useContext, useState } from "react";
import { AztecContext } from "../../home/home";
import { AztecContext } from "../../../aztecEnv";
import { FunctionParameter } from "../../common/fnParameter";

const creationForm = css({
Expand Down Expand Up @@ -61,13 +61,11 @@ export function CreateAuthwitDialog({
action,
});
} else {
await wallet
.setPublicAuthWit(
{ caller: AztecAddress.fromString(caller), action },
true
)
.send()
.wait();
const validateActionInteraction = await wallet.setPublicAuthWit(
{ caller: AztecAddress.fromString(caller), action },
true
);
await validateActionInteraction.send().wait();
}
setAlias("");
setCreating(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import {
getDefaultInitializer,
getInitializer,
} from "@aztec/foundation/abi";
import { AztecContext } from "../../home/home";
import { AztecContext } from "../../../aztecEnv";
import { parseAliasedBuffersAsString } from "../../../utils/conversion";
import { FunctionParameter } from "../../common/fnParameter";
import { GITHUB_TAG_PREFIX } from "../../../utils/constants";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
css,
} from "@mui/material";
import { useContext, useState } from "react";
import { AztecContext } from "../../home/home";
import { AztecContext } from "../../../aztecEnv";
import { GITHUB_TAG_PREFIX } from "../../../utils/constants";

const creationForm = css({
Expand Down
4 changes: 2 additions & 2 deletions gaztec/src/components/contract/contract.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
ContractInstanceWithAddress,
loadContractArtifact,
} from "@aztec/aztec.js";
import { AztecContext } from "../home/home";
import { AztecContext } from "../../aztecEnv";
import {
Button,
Card,
Expand Down Expand Up @@ -241,7 +241,7 @@ export function ContractComponent() {
const call = currentContract.methods[fnName](...parameters[fnName]);

const provenCall = await call.prove();
txHash = provenCall.getTxHash();
txHash = await provenCall.getTxHash();
setCurrentTx({
...currentTx,
...{ txHash, status: "sending" },
Expand Down
Loading
Loading