From 862cf1645a668eb441a54cd8faee3cb2670df69e Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 6 Aug 2024 17:19:47 +0100 Subject: [PATCH] simple setup --- .../circuit-compiler/src/app/actions/index.ts | 64 +++++++++++++++++++ .../src/app/components/container.tsx | 11 ++++ .../src/app/components/generateProof.tsx | 37 +++++++++++ .../src/app/reducers/state.ts | 7 ++ apps/circuit-compiler/src/app/types/index.ts | 4 +- .../src/app/tabs/locales/en/circuit.json | 1 + 6 files changed, 123 insertions(+), 1 deletion(-) create mode 100644 apps/circuit-compiler/src/app/components/generateProof.tsx diff --git a/apps/circuit-compiler/src/app/actions/index.ts b/apps/circuit-compiler/src/app/actions/index.ts index a994b18ba1..f3dbfdaafd 100644 --- a/apps/circuit-compiler/src/app/actions/index.ts +++ b/apps/circuit-compiler/src/app/actions/index.ts @@ -76,3 +76,67 @@ export const runSetupAndExport = async (plugin: CircomPluginClient, appState: Ap } } } + +export const generateProof = async (plugin: CircomPluginClient, appState: AppState) => { +// try { +// // @ts-ignore +// const r1csBuffer = await remix.call('fileManager', 'readFile', 'circuits/.bin/calculate_hash.r1cs', { encoding: null }); +// // @ts-ignore +// const r1cs = new Uint8Array(r1csBuffer); +// // @ts-ignore +// await remix.call('circuit-compiler', 'compile', 'circuits/calculate_hash.circom'); +// // @ts-ignore +// const wasmBuffer = await remix.call('fileManager', 'readFile', 'circuits/.bin/calculate_hash.wasm', { encoding: null }); +// // @ts-ignore +// const wasm = new Uint8Array(wasmBuffer); + +// const zkey_final = { +// type: "mem", +// data: new Uint8Array(JSON.parse(await remix.call('fileManager', 'readFile', 'scripts/groth16/zk/keys/zkey_final.txt'))) +// } +// const wtns = { type: "mem" }; + +// const vKey = JSON.parse(await remix.call('fileManager', 'readFile', 'scripts/groth16/zk/keys/verification_key.json')) + +// const value1 = '1234' +// const value2 = '2' +// const value3 = '3' +// const value4 = '4' + +// const wrongValue = '5' // put this in the poseidon hash calculation to simulate a non matching hash. + +// const signals = { +// value1, +// value2, +// value3, +// value4, +// hash: poseidon([value1, value2, value3, value4]) +// } + +// console.log('calculate') +// await snarkjs.wtns.calculate(signals, wasm, wtns); + +// console.log('check') +// await snarkjs.wtns.check(r1cs, wtns, logger); + +// console.log('prove') +// const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns); + +// const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof, logger); +// console.log('zk proof validity', verified); + +// const templates = { +// groth16: await remix.call('fileManager', 'readFile', 'templates/groth16_verifier.sol.ejs') +// } +// const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates) + +// await remix.call('fileManager', 'writeFile', 'scripts/groth16/zk/build/zk_verifier.sol', solidityContract) +// await remix.call('fileManager', 'writeFile', 'scripts/groth16/zk/build/input.json', JSON.stringify({ +// _pA: [proof.pi_a[0], proof.pi_a[1]], +// _pB: [[proof.pi_b[0][1], proof.pi_b[0][0]], [proof.pi_b[1][1], proof.pi_b[1][0]]], +// _pC: [proof.pi_c[0], proof.pi_c[1]], +// _pubSignals: publicSignals, +// }, null, 2)) +// } catch (e) { +// } +} diff --git a/apps/circuit-compiler/src/app/components/container.tsx b/apps/circuit-compiler/src/app/components/container.tsx index 15daa60f7a..982395d11e 100644 --- a/apps/circuit-compiler/src/app/components/container.tsx +++ b/apps/circuit-compiler/src/app/components/container.tsx @@ -11,6 +11,7 @@ import { WitnessSection } from './witness' import { CompilerFeedback } from './feedback' import { CompilerReport, PrimeValue } from '../types' import { SetupExports } from './setupExports' +import { GenerateProof } from './generateProof' export function Container () { const circuitApp = useContext(CircuitAppContext) @@ -146,6 +147,16 @@ export function Container () { + 0}> + + <> + + + + + + + diff --git a/apps/circuit-compiler/src/app/components/generateProof.tsx b/apps/circuit-compiler/src/app/components/generateProof.tsx new file mode 100644 index 0000000000..ef663b01db --- /dev/null +++ b/apps/circuit-compiler/src/app/components/generateProof.tsx @@ -0,0 +1,37 @@ +import { RenderIf } from "@remix-ui/helper" +import { FormattedMessage } from "react-intl" +import { generateProof } from "../actions" +import { CircuitAppContext } from "../contexts" +import { useContext } from "react" + +export function GenerateProof () { + const circuitApp = useContext(CircuitAppContext) + const status = circuitApp.appState.status + + const handleGenerateProof = async () => { + try { + circuitApp.dispatch({ type: 'SET_COMPILER_STATUS', payload: 'proving' }) + await generateProof(circuitApp.plugin, circuitApp.appState) + circuitApp.dispatch({ type: 'SET_COMPILER_STATUS', payload: 'idle' }) + } catch (e) { + circuitApp.dispatch({ type: 'SET_COMPILER_STATUS', payload: 'errored' }) + console.error(e) + } + } + + return ( +
+ +
+ ) +} diff --git a/apps/circuit-compiler/src/app/reducers/state.ts b/apps/circuit-compiler/src/app/reducers/state.ts index ceee26f60e..db967007ad 100644 --- a/apps/circuit-compiler/src/app/reducers/state.ts +++ b/apps/circuit-compiler/src/app/reducers/state.ts @@ -14,6 +14,7 @@ export const appInitialState: AppState = { signalInputs: [], compilerFeedback: null, computeFeedback: null, + proofFeedback: null, setupExportFeedback: null, setupExportStatus: null, provingScheme: 'groth16', @@ -86,6 +87,12 @@ export const appReducer = (state = appInitialState, action: Actions): AppState = setupExportFeedback: action.payload } + case 'SET_PROOF_FEEDBACK': + return { + ...state, + proofFeedback: action.payload + } + case 'SET_FILE_PATH_TO_ID': return { ...state, diff --git a/apps/circuit-compiler/src/app/types/index.ts b/apps/circuit-compiler/src/app/types/index.ts index 4be4b6a9f6..22e3160ad1 100644 --- a/apps/circuit-compiler/src/app/types/index.ts +++ b/apps/circuit-compiler/src/app/types/index.ts @@ -2,7 +2,7 @@ import { compiler_list } from 'circom_wasm' import { Dispatch } from 'react' import type { CircomPluginClient } from '../services/circomPluginClient' -export type CompilerStatus = "compiling" | "computing" | "idle" | "errored" | "warning" | "exporting" +export type CompilerStatus = "compiling" | "computing" | "idle" | "errored" | "warning" | "exporting" | "proving" export type ProvingScheme = 'groth16' | 'plonk' @@ -31,6 +31,7 @@ export interface ActionPayloadTypes { SET_SIGNAL_INPUTS: string[], SET_COMPILER_FEEDBACK: string | CompilerReport[], SET_COMPUTE_FEEDBACK: string | CompilerReport[], + SET_PROOF_FEEDBACK: string | CompilerReport[], SET_SETUP_EXPORT_FEEDBACK: string | CompilerReport[], SET_FILE_PATH_TO_ID: Record, SET_PROVING_SCHEME: ProvingScheme, @@ -58,6 +59,7 @@ export interface AppState { signalInputs: string[], compilerFeedback: string | CompilerReport[], computeFeedback: string | CompilerReport[], + proofFeedback: string | CompilerReport[], setupExportFeedback: string | CompilerReport[], setupExportStatus: SetupExportStatus, provingScheme: ProvingScheme, diff --git a/apps/remix-ide/src/app/tabs/locales/en/circuit.json b/apps/remix-ide/src/app/tabs/locales/en/circuit.json index 4a10ba5f43..a37afa1e68 100644 --- a/apps/remix-ide/src/app/tabs/locales/en/circuit.json +++ b/apps/remix-ide/src/app/tabs/locales/en/circuit.json @@ -10,6 +10,7 @@ "circuit.noFileSelected": "no file selected", "circuit.generateR1cs": "Generate R1CS", "circuit.computeWitness": "Compute Witness", + "circuit.generateProof": "Generate Proof", "circuit.signalInput": "Signal Input", "circuit.compute": "Compute", "circuit.setupExports": "Setup and Exports",