-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
65d82aa
commit cdf911d
Showing
14 changed files
with
1,574 additions
and
1,261 deletions.
There are no files selected for viewing
File renamed without changes.
486 changes: 354 additions & 132 deletions
486
...ircuits/contract/noirstarter/plonk_vk.sol → ...circuit/contract/noirstarter/plonk_vk.sol
Large diffs are not rendered by default.
Oops, something went wrong.
File renamed without changes.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { ProofData } from '@noir-lang/types'; | ||
import { useAccount, useConnect, useContractRead } from 'wagmi'; | ||
import { contractCallConfig } from '../utils/wagmi.jsx'; | ||
import { bytesToHex } from 'viem'; | ||
import { useEffect, useState } from 'react'; | ||
import { Id, toast } from 'react-toastify'; | ||
|
||
export function useOnChainVerification(proofData?: ProofData) { | ||
const { connect, connectors } = useConnect(); | ||
const { isConnected } = useAccount(); | ||
const [args, setArgs] = useState<[string, string[]] | undefined>(); | ||
|
||
const { data, error } = useContractRead({ | ||
...contractCallConfig, | ||
functionName: 'verify', | ||
args, | ||
}); | ||
|
||
const [onChainToast, setOnChainToast] = useState<Id>(0); | ||
|
||
useEffect(() => { | ||
if (!isConnected) { | ||
console.log("not connected, won't attempt on-chain verification"); | ||
} | ||
if (!proofData || !isConnected) { | ||
return; | ||
} | ||
|
||
setArgs([bytesToHex(proofData.proof), proofData.publicInputs]); | ||
|
||
if (!onChainToast) | ||
setOnChainToast(toast.loading('Verifying proof on-chain', { autoClose: 10000 })); | ||
}, [proofData]); | ||
|
||
useEffect(() => { | ||
if (!isConnected) { | ||
connectors.map(c => c.ready && connect({ connector: c })); | ||
} | ||
}, [isConnected]); | ||
|
||
useEffect(() => { | ||
console.log(data); | ||
if (data) { | ||
toast.update(onChainToast, { | ||
type: 'success', | ||
render: 'Proof verified on-chain!', | ||
isLoading: false, | ||
}); | ||
} else if (error) { | ||
toast.update(onChainToast, { | ||
type: 'error', | ||
render: 'Error verifying proof on-chain!', | ||
isLoading: false, | ||
}); | ||
console.error(error); | ||
} | ||
}, [data, error]); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
import { toast } from 'react-toastify'; | ||
import { useEffect, useState } from 'react'; | ||
import { getCircuit } from '../utils/compile.js'; | ||
import { BarretenbergBackend, ProofData } from '@noir-lang/backend_barretenberg'; | ||
import { Noir } from '@noir-lang/noir_js'; | ||
|
||
export function useProofGeneration(inputs?: { [key: string]: string }) { | ||
const [proofData, setProofData] = useState<ProofData | 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); | ||
|
||
await toast.promise(noir.init, { | ||
pending: 'Initializing Noir...', | ||
success: 'Noir initialized!', | ||
error: 'Error initializing Noir', | ||
}); | ||
|
||
const data = await toast.promise(noir.generateFinalProof(inputs), { | ||
pending: 'Generating proof', | ||
success: 'Proof generated', | ||
error: 'Error generating proof', | ||
}); | ||
|
||
const res = await toast.promise(noir.verifyFinalProof(data), { | ||
pending: 'Verifying proof off-chain', | ||
success: 'Proof verified off-chain', | ||
error: 'Error verifying proof off-chain', | ||
}); | ||
|
||
console.log(res); | ||
}; | ||
|
||
useEffect(() => { | ||
if (!inputs) return; | ||
proofGeneration(); | ||
}, [inputs]); | ||
|
||
return { proofData }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"chainId":31337,"verifier":"0x5fc8d32690cc91d4c39d9d3abcbd16989f875707"} | ||
{"chainId":31337,"verifier":"0x4ed7c70f96b99c776995fb64377f0d4ab3b0e1c1"} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { compile, createFileManager } from '@noir-lang/noir_wasm'; | ||
import { CompiledCircuit } from '@noir-lang/types'; | ||
|
||
export async function getFile(file_path: string): Promise<ReadableStream<Uint8Array>> { | ||
const file_url = new URL(file_path, import.meta.url); | ||
console.log(file_url); | ||
const response = await fetch(file_url); | ||
|
||
if (!response.ok) throw new Error('Network response was not OK'); | ||
|
||
return response.body as ReadableStream<Uint8Array>; | ||
} | ||
|
||
export async function getCircuit() { | ||
const fm = createFileManager('/'); | ||
fm.writeFile('./src/main.nr', await getFile(`../circuit/src/main.nr`)); | ||
fm.writeFile('./Nargo.toml', await getFile(`../circuit/Nargo.toml`)); | ||
const result = await compile(fm); | ||
if (!('program' in result)) { | ||
throw new Error('Compilation failed'); | ||
} | ||
return result.program as CompiledCircuit; | ||
} |
Oops, something went wrong.