Skip to content

Commit

Permalink
finally ultrahonk
Browse files Browse the repository at this point in the history
  • Loading branch information
signorecello committed Dec 18, 2024
1 parent b96d47b commit 943780d
Show file tree
Hide file tree
Showing 14 changed files with 67 additions and 698 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ package-lock.json

# To use with nektos/act
.github/event.json
bun.lockb
4 changes: 3 additions & 1 deletion vite-hardhat/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ crs
artifacts
.yarn/

circuit/target/
noir/target/
contracts
dist
deployment.json
bun.lockb
Binary file modified vite-hardhat/bun.lockb
Binary file not shown.
143 changes: 22 additions & 121 deletions vite-hardhat/hardhat.config.cts
Original file line number Diff line number Diff line change
@@ -1,130 +1,15 @@
import '@nomicfoundation/hardhat-toolbox-viem';
import '@nomicfoundation/hardhat-viem';
import '@nomicfoundation/hardhat-chai-matchers';
import 'hardhat-plugin-noir';
import 'hardhat-noirenberg';

import hre, { vars, HardhatUserConfig, task } from 'hardhat/config';
import { writeFileSync } from 'fs';
import { Chain } from 'viem';

task('deploy', 'Deploys the verifier contract').setAction(async ({ attach }, hre) => {
const verifier = await hre.viem.deployContract('UltraVerifier');

const networkConfig = (await import(`viem/chains`))[hre.network.name] as Chain;
const config = {
name: hre.network.name,
address: verifier.address,
networkConfig: {
...networkConfig,
id: hre.network.config.chainId,
},
};

<<<<<<< HEAD
return superRes;
});

// export async function compileCircuit(path = './circuit') {
// const basePath = resolve(join(path));
// const fm = createFileManager(basePath);
// const result = await compile(fm);
// if (!('program' in result)) {
// throw new Error('Compilation failed');
// }
// return result as CompiledCircuit;
// }

export async function generateArtifacts(path = './circuit', crsPath = './crs') {
// const circuit = await compileCircuit(path);

const circuitFile = readFileSync(resolve('artifacts/circuit.json'), 'utf-8');
const circuit = JSON.parse(circuitFile);
const backend = new UltraHonkBackend(circuit);
// const contract = await backend.acirGetSolidityVerifier(acirComposer);
return { circuit };
}

task('compile', 'Compile and generate circuits and contracts').setAction(
async (_, __, runSuper) => {
const { circuit } = await generateArtifacts();
mkdirSync('artifacts', { recursive: true });
writeFileSync('artifacts/circuit.json', JSON.stringify(circuit), { flag: 'w' });
// writeFileSync('artifacts/contract.sol', contract, { flag: 'w' });
await runSuper();
},
);

task('deploy', 'Deploys the verifier contract')
.addOptionalParam('attach', 'Attach to an existing address', '', types.string)
.setAction(async ({ attach }, hre) => {
let verifier;
if (attach) {
verifier = await hre.viem.getContractAt('UltraVerifier', attach);
} else {
verifier = await hre.viem.deployContract('UltraVerifier');
}

const networkConfig = (await import(`viem/chains`))[hre.network.name] as Chain;
console.log(networkConfig);
const config = {
name: hre.network.name,
address: verifier.address,
networkConfig: {
...networkConfig,
id: hre.network.config.chainId || networkConfig.id,
},
};

console.log(
`Attached to address ${verifier.address} at network ${hre.network.name} with chainId ${config.networkConfig.id}...`,
);
writeFileSync('artifacts/deployment.json', JSON.stringify(config), { flag: 'w' });
});

subtask('generateHooks', 'Generates hooks for the verifier contract').setAction(async (_, hre) => {
exec('wagmi generate');
});

subtask('prep', 'Compiles and deploys the verifier contract')
.addParam('attach', 'Attach to an already deployed contract', '', types.string)
.setAction(async ({ attach }, hre) => {
console.log('Preparing...');
console.log('Compiling circuits and generating contracts...');

await hre.run('compile');
await hre.run('deploy', { attach });

console.log('Generating hooks...');
await hre.run('generateHooks');
});

task('dev', 'Deploys and starts in a development environment')
.addOptionalParam('attach', 'Attach to an existing address', '', types.string)
.setAction(async ({ attach }, hre) => {
await hre.run('prep', { attach });
exec('vite dev');
});

task('build', 'Builds the frontend project')
.addOptionalParam('attach', 'Attach to an existing address', '', types.string)
.setAction(async ({ attach }, hre) => {
await hre.run('prep', { attach });
exec('vite build');
});

task('serve', 'Serves the frontend project').setAction(async (_, hre) => {
exec('vite preview');
=======
console.log(
`Attached to address ${verifier.address} at network ${hre.network.name} with chainId ${networkConfig.id}...`,
);
writeFileSync('deployment.json', JSON.stringify(config), { flag: 'w' });
>>>>>>> main
});
import { HardhatUserConfig, task, vars } from 'hardhat/config';

const config: HardhatUserConfig = {
solidity: {
version: '0.8.21',
version: '0.8.28',
settings: {
optimizer: { enabled: true, runs: 5000 },
},
Expand All @@ -147,13 +32,29 @@ const config: HardhatUserConfig = {
accounts: vars.has('holesky') ? [vars.get('holesky')] : [],
},
},
noir: {
version: '0.36.0',
},
paths: {
root: 'packages',
tests: 'tests',
},
};

task('deploy', 'Deploys the verifier contract').setAction(async (taskArgs, hre) => {
const verifier = await hre.viem.deployContract('HonkVerifier');

const networkConfig = (await import(`viem/chains`))[hre.network.name] as Chain;
const config = {
name: hre.network.name,
address: verifier.address,
networkConfig: {
...networkConfig,
id: hre.network.config.chainId,
},
};

console.log(
`Attached to address ${verifier.address} at network ${hre.network.name} with chainId ${networkConfig.id}...`,
);
writeFileSync('deployment.json', JSON.stringify(config), { flag: 'w' });
});

export default config;
20 changes: 6 additions & 14 deletions vite-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,27 +13,18 @@
},
"type": "module",
"devDependencies": {
"hardhat-plugin-noir": "0.1.3",
"@types/bun": "^1.1.12",
"hardhat": "^2.19.2"
},
"dependencies": {
"@aztec/bb.js": "^0.62.0",
"@noir-lang/noir_js": "0.36.0",
"@noir-lang/noir_wasm": "0.36.0",
"@noir-lang/types": "0.36.0",
"@noir-lang/noir_js": "1.0.0-beta.0",
"@noir-lang/noir_wasm": "1.0.0-beta.0",
"@noir-lang/types": "1.0.0-beta.0",
"@aztec/bb.js": "0.63.1",
"@nomicfoundation/hardhat-ignition": "^0.15.5",
"@nomicfoundation/hardhat-ignition-viem": "^0.15.5",
"commander": "^12.1.0",
"dotenv": "^16.0.3",
<<<<<<< HEAD
"fflate": "^0.8.2",
"hardhat": "^2.19.2",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-toastify": "^9.1.1",
=======
>>>>>>> main
"shelljs": "^0.8.5",
"@nomicfoundation/hardhat-ethers": "^3.0.6",
"@nomicfoundation/hardhat-network-helpers": "^1.0.11",
Expand All @@ -43,7 +34,8 @@
"@types/mocha": "^10.0.1",
"@types/shelljs": "^0.8.7",
"hardhat-gas-reporter": "^1.0.9",
"solidity-coverage": "^0.8.5"
"solidity-coverage": "^0.8.5",
"hardhat-noirenberg": "^0.1.4"
},
"peerDependencies": {
"typescript": "^5.0.0"
Expand Down
4 changes: 2 additions & 2 deletions vite-hardhat/packages/vite/hooks/useOffChainVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@
import { ProofData } from '@noir-lang/types';
import { useEffect } from 'react';
import { toast } from 'react-toastify';
import { UltraPlonkBackend } from '@aztec/bb.js';
import { UltraHonkBackend } from '@aztec/bb.js';
import { Noir } from '@noir-lang/noir_js';

export function useOffChainVerification(
backend: UltraPlonkBackend,
backend: UltraHonkBackend,
noir?: Noir,
proofData?: ProofData,
) {
Expand Down
4 changes: 2 additions & 2 deletions vite-hardhat/packages/vite/hooks/useOnChainVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { useAccount, useConnect, useDisconnect, useSwitchChain } from 'wagmi';
import { bytesToHex } from 'viem';
import { useEffect, useState } from 'react';
import { Id, toast } from 'react-toastify';
import { useReadUltraVerifierVerify } from '../artifacts/generated.js';
import { useReadHonkVerifierVerify } from '../artifacts/generated.js';
import deployment from '../../../deployment.json';

export function useOnChainVerification(proofData?: ProofData) {
Expand All @@ -14,7 +14,7 @@ export function useOnChainVerification(proofData?: ProofData) {
const [args, setArgs] = useState<[`0x${string}`, `0x${string}`[]] | undefined>();

const { chains, switchChain } = useSwitchChain();
const { data, error } = useReadUltraVerifierVerify({
const { data, error } = useReadHonkVerifierVerify({
args,
query: {
enabled: !!args,
Expand Down
6 changes: 5 additions & 1 deletion vite-hardhat/packages/vite/hooks/useProofGeneration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ export function useProofGeneration(inputs?: { [key: string]: string }) {
error: 'Error initializing Noir',
});

const { witness } = await noir.execute(inputs);
const { witness } = await toast.promise(noir.execute(inputs), {
pending: 'Generating witness...',
success: 'Witness generated!',
error: 'Error generating witness',
});

const data = await toast.promise(backend.generateProof(witness), {
pending: 'Generating proof',
Expand Down
34 changes: 11 additions & 23 deletions vite-hardhat/packages/vite/index.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
// @ts-ignore
import acvm from '@noir-lang/acvm_js/web/acvm_js_bg.wasm?url';
// @ts-ignore
import noirc from '@noir-lang/noirc_abi/web/noirc_abi_wasm_bg.wasm?url';
import initNoirC from '@noir-lang/noirc_abi';
import initACVM from '@noir-lang/acvm_js';
// @ts-ignore
await Promise.all([initACVM(fetch(acvm)), initNoirC(fetch(noirc))]);

import React, { ReactNode, useEffect } from 'react';
import ReactDOM from 'react-dom/client';
import './App.css';
import 'react-toastify/dist/ReactToastify.css';
import { ToastContainer } from 'react-toastify';
import Component from './components/index.jsx';
import initNoirC from '@noir-lang/noirc_abi';
import initACVM from '@noir-lang/acvm_js';
import { QueryClient, QueryClientProvider } from '@tanstack/react-query';
import { WagmiProvider, createConfig, http } from 'wagmi';
import { defineChain, createClient } from 'viem';
Expand All @@ -30,23 +37,6 @@ const config = createConfig({
},
});

const InitWasm = ({ children }: any) => {
const [init, setInit] = React.useState(false);
useEffect(() => {
(async () => {
await Promise.all([
initACVM(new URL('@noir-lang/acvm_js/web/acvm_js_bg.wasm', import.meta.url).toString()),
initNoirC(
new URL('@noir-lang/noirc_abi/web/noirc_abi_wasm_bg.wasm', import.meta.url).toString(),
),
]);
setInit(true);
})();
});

return <div>{init && children}</div>;
};

export function Providers({ children }: { children: React.ReactNode }) {
const [mounted, setMounted] = React.useState(false);
React.useEffect(() => setMounted(true), []);
Expand All @@ -59,9 +49,7 @@ export function Providers({ children }: { children: React.ReactNode }) {

ReactDOM.createRoot(document.getElementById('root')!).render(
<Providers>
<InitWasm>
<Component />
<ToastContainer />
</InitWasm>
<Component />
<ToastContainer />
</Providers>,
);
8 changes: 0 additions & 8 deletions vite-hardhat/packages/vite/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,6 @@
"wagmi": "wagmi generate"
},
"dependencies": {
"@noir-lang/backend_barretenberg": "0.36.0",
"@noir-lang/noir_js": "0.36.0",
"@noir-lang/noir_wasm": "0.36.0",
"@noir-lang/types": "0.36.0",
"@aztec/bb.js": "^0.62.0",
"react": "^18.2.0",
"react-dom": "^18.2.0",
"react-toastify": "^9.1.1",
Expand All @@ -34,8 +29,5 @@
"typechain": "^8.1.0",
"typescript": "^4.9.3",
"vite": "^5.0.6"
},
"engines": {
"node": ">=18.19.0"
}
}
11 changes: 2 additions & 9 deletions vite-hardhat/packages/vite/vite.config.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react-swc';

export default defineConfig({
export default {
optimizeDeps: {
esbuildOptions: {
target: 'esnext',
},
},
build: {
target: 'esnext',
},
plugins: [react()],
server: {
port: 1337,
},
});
};
Loading

0 comments on commit 943780d

Please sign in to comment.