Skip to content

Commit

Permalink
Merge pull request #83 from noir-lang/jc/update-to-31
Browse files Browse the repository at this point in the history
update vite to 0.31
  • Loading branch information
critesjosh authored Jul 31, 2024
2 parents 6ac86ad + 580dc22 commit c4e799c
Show file tree
Hide file tree
Showing 7 changed files with 7,196 additions and 31 deletions.
29 changes: 14 additions & 15 deletions vite-hardhat/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@ Want to get started in a pinch? Start your project in a free Github Codespace!

## Locally

1. Install your favorite package manager. We'll use [bun](https://bun.sh/docs/installation) but feel free to use `yarn` or others:
1. Install your favorite package manager. We'll use [bun](https://bun.sh/docs/installation) but feel
free to use `yarn` or others:

```bash
curl -fsSL https://bun.sh/install | bash
Expand All @@ -25,13 +26,7 @@ Want to get started in a pinch? Start your project in a free Github Codespace!
bun i # "npm i" or "yarn"
```

3. Run a development Ethereum node

```bash
bunx hardhat node # "npx hardhat dev node" or "yarn hardhat dev node"
```

4. Run the app
3. Run the app

You can run a separate Ethereum node from the dev environment:

Expand All @@ -55,21 +50,23 @@ bun test

This test shows the basic usage of Noir in a TypeScript Node.js environment.

> [!NOTE]
> The test is a script, not an executable (we're running `bun test` or `yarn test` instead of `bunx` or `npx`). This is because the test runs its own network and executables.
> [!NOTE] The test is a script, not an executable (we're running `bun test` or `yarn test` instead
> of `bunx` or `npx`). This is because the test runs its own network and executables.
### Deploying on other networks

The default scripting targets a local environment. For convenience, we added some configurations for deployment on various other networks. You can see the existing list by running:
The default scripting targets a local environment. For convenience, we added some configurations for
deployment on various other networks. You can see the existing list by running:

```bash
bunx hardhat vars setup
```

If you want to deploy on any of them, just pass in a private key, for example for the holesky network:
If you want to deploy on any of them, just pass in a private key, for example for the holesky
network:

```bash
bunx hardhat vars set holesky <your_testnet_private_key>
bunx hardhat vars set holesky <your_testnet_private_key>
```

You can then run all the commands using that network by passing the `--network` flag. For example:
Expand All @@ -80,15 +77,17 @@ bunx hardhat deploy --network holesky # deploys on holesky
bunx hardhat build --network holesky # builds the frontend with the holesky target
```

Feel free to add more networks, as long as they're supported by `wagmi` ([list here](https://wagmi.sh/react/api/chains#available-chains)). Just make sure you:
Feel free to add more networks, as long as they're supported by `wagmi`
([list here](https://wagmi.sh/react/api/chains#available-chains)). Just make sure you:

- Have funds in these accounts
- Add their configuration in the `networks` property in `hardhat.config.cts`
- Use the name that wagmi expects (for example `ethereum` won't work, as `wagmi` calls it `mainnet`)

#### Attaching to an existing contract

You probably don't want to redeploy everytime you build your project. To attach the build to an already deployed contract, pass the `--attach` flag:
You probably don't want to redeploy everytime you build your project. To attach the build to an
already deployed contract, pass the `--attach` flag:

```bash
bunx hardhat deploy --network mainnet # deploys on ethereum mainnet $$$$$!
Expand Down
4 changes: 2 additions & 2 deletions vite-hardhat/components/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ import { useOffChainVerification } from '../hooks/useOffChainVerification.jsx';

function Component() {
const [input, setInput] = useState<{ x: string; y: string } | undefined>();
const { noir, proofData } = useProofGeneration(input);
useOffChainVerification(noir, proofData);
const { noir, proofData, backend } = useProofGeneration(input);
useOffChainVerification(backend!, noir, proofData);
const verifyButton = useOnChainVerification(proofData);

const submit = (e: React.FormEvent<HTMLFormElement>) => {
Expand Down
8 changes: 6 additions & 2 deletions vite-hardhat/hooks/useOffChainVerification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,15 @@ import { toast } from 'react-toastify';
import { BarretenbergBackend } from '@noir-lang/backend_barretenberg';
import { Noir } from '@noir-lang/noir_js';

export function useOffChainVerification(noir?: Noir, proofData?: ProofData) {
export function useOffChainVerification(
backend: BarretenbergBackend,
noir?: Noir,
proofData?: ProofData,
) {
useEffect(() => {
if (!proofData || !noir) return;

toast.promise(noir.verifyProof(proofData), {
toast.promise(backend.verifyProof(proofData), {
pending: 'Verifying proof off-chain',
success: 'Proof verified off-chain',
error: 'Error verifying proof off-chain',
Expand Down
10 changes: 7 additions & 3 deletions vite-hardhat/hooks/useProofGeneration.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,34 +6,38 @@ import { Noir } from '@noir-lang/noir_js';

export function useProofGeneration(inputs?: { [key: string]: string }) {
const [proofData, setProofData] = useState<ProofData | undefined>();
const [backend, setBackend] = useState<BarretenbergBackend>();
const [noir, setNoir] = useState<Noir | undefined>();

const proofGeneration = async () => {
if (!inputs) return;
const circuit = await getCircuit();
const backend = new BarretenbergBackend(circuit, { threads: navigator.hardwareConcurrency });
const noir = new Noir(circuit, backend);
const noir = new Noir(circuit);

await toast.promise(noir.init, {
pending: 'Initializing Noir...',
success: 'Noir initialized!',
error: 'Error initializing Noir',
});

const data = await toast.promise(noir.generateProof(inputs), {
const { witness } = await noir.execute(inputs);

const data = await toast.promise(backend.generateProof(witness), {
pending: 'Generating proof',
success: 'Proof generated',
error: 'Error generating proof',
});

setProofData(data);
setNoir(noir);
setBackend(backend);
};

useEffect(() => {
if (!inputs) return;
proofGeneration();
}, [inputs]);

return { noir, proofData };
return { noir, proofData, backend };
}
8 changes: 4 additions & 4 deletions vite-hardhat/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
"test": "test/test.sh"
},
"dependencies": {
"@noir-lang/backend_barretenberg": "^0.30.0",
"@noir-lang/noir_js": "^0.30.0",
"@noir-lang/noir_wasm": "^0.30.0",
"@noir-lang/types": "^0.30.0",
"@noir-lang/backend_barretenberg": "^0.31.0",
"@noir-lang/noir_js": "^0.31.0",
"@noir-lang/noir_wasm": "^0.31.0",
"@noir-lang/types": "^0.31.0",
"@nomicfoundation/hardhat-ignition": "^0.15.5",
"@nomicfoundation/hardhat-ignition-viem": "^0.15.5",
"@tanstack/query-sync-storage-persister": "5.0.5",
Expand Down
13 changes: 8 additions & 5 deletions vite-hardhat/test/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,32 +11,35 @@ shelljs.exec('npx hardhat compile');

describe('It compiles noir program code, receiving circuit bytes and abi object.', () => {
let noir: Noir;
let backend: BarretenbergBackend;
let correctProof: ProofData;

beforeEach(async () => {
const circuitFile = readFileSync(resolve('artifacts/circuit.json'), 'utf-8');
const circuit = JSON.parse(circuitFile);

const backend = new BarretenbergBackend(circuit);
noir = new Noir(circuit, backend);
backend = new BarretenbergBackend(circuit);
noir = new Noir(circuit);
});

it('Should generate valid proof for correct input', async () => {
const input = { x: 1, y: 2 };
// Generate proof
correctProof = await noir.generateProof(input);
const { witness } = await noir.execute(input);
correctProof = await backend.generateProof(witness);
expect(correctProof.proof instanceof Uint8Array).to.be.true;
});

it('Should verify valid proof for correct input', async () => {
const verification = await noir.verifyProof(correctProof);
const verification = await backend.verifyProof(correctProof);
expect(verification).to.be.true;
});

it('Should fail to generate valid proof for incorrect input', async () => {
try {
const input = { x: 1, y: 1 };
const incorrectProof = await noir.generateProof(input);
const { witness } = await noir.execute(input);
const incorrectProof = await backend.generateProof(witness);
} catch (err) {
// TODO(Ze): Not sure how detailed we want this it to be
expect(err instanceof Error).to.be.true;
Expand Down
Loading

0 comments on commit c4e799c

Please sign in to comment.