diff --git a/apps/circuit-compiler/src/app/actions/index.ts b/apps/circuit-compiler/src/app/actions/index.ts index 2f48faf2fb..96d0330efb 100644 --- a/apps/circuit-compiler/src/app/actions/index.ts +++ b/apps/circuit-compiler/src/app/actions/index.ts @@ -19,12 +19,20 @@ export const compileCircuit = async (plugin: CircomPluginClient, appState: AppSt } } -export const computeWitness = async (plugin: CircomPluginClient, status: string, witnessValues: Record) => { +export const computeWitness = async (plugin: CircomPluginClient, appState: AppState, dispatch: ICircuitAppContext['dispatch'], status: string, witnessValues: Record) => { try { if (status !== "computing") { const input = JSON.stringify(witnessValues) + const witness = await plugin.computeWitness(input) - await plugin.computeWitness(input) + if (appState.exportWtnsJson) { + const wtns = await snarkjs.wtns.exportJson(witness) + const wtnsJson = wtns.map(wtn => wtn.toString()) + const fileName = extractNameFromKey(appState.filePath) + const writePath = extractParentFromKey(appState.filePath) + `/.bin/${fileName.replace('.circom', '.wtn.json')}` + + await plugin.call('fileManager', 'writeFile', writePath, JSON.stringify(wtnsJson, null, 2)) + } } else { console.log('Existing witness computation in progress') } @@ -38,6 +46,8 @@ export const computeWitness = async (plugin: CircomPluginClient, status: string, export const runSetupAndExport = async (plugin: CircomPluginClient, appState: AppState, dispatch: ICircuitAppContext['dispatch']) => { try { dispatch({ type: 'SET_COMPILER_STATUS', payload: 'exporting' }) + dispatch({ type: 'SET_SETUP_EXPORT_FEEDBACK', payload: null }) + plugin.emit('statusChanged', { key: 'none' }) const ptau_final = `https://ipfs-cluster.ethdevops.io/ipfs/${appState.ptauList.find(ptau => ptau.name === appState.ptauValue)?.ipfsHash}` await plugin.generateR1cs(appState.filePath, { version: appState.version, prime: appState.primeValue }) @@ -91,6 +101,8 @@ export const runSetupAndExport = async (plugin: CircomPluginClient, appState: Ap export const generateProof = async (plugin: CircomPluginClient, appState: AppState, dispatch: ICircuitAppContext['dispatch']) => { try { dispatch({ type: 'SET_COMPILER_STATUS', payload: 'proving' }) + dispatch({ type: 'SET_PROOF_FEEDBACK', payload: null }) + plugin.emit('statusChanged', { key: 'none' }) const fileName = extractNameFromKey(appState.filePath) const r1csPath = extractParentFromKey(appState.filePath) + `/.bin/${fileName.replace('.circom', '.r1cs')}` // @ts-ignore @@ -110,6 +122,7 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) + plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/build/proof.json`, JSON.stringify(proof, null, 2)) plugin.call('terminal', 'log', { type: 'log', value: 'zk proof validity ' + verified }) if (appState.exportVerifierCalldata) { const calldata = await snarkjs.groth16.exportSolidityCallData(proof, publicSignals) @@ -120,6 +133,7 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta const { proof, publicSignals } = await snarkjs.plonk.prove(zkey_final, wtns, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) const verified = await snarkjs.plonk.verify(vKey, publicSignals, proof, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) + plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/build/proof.json`, JSON.stringify(proof, null, 2)) plugin.call('terminal', 'log', { type: 'log', value: 'zk proof validity ' + verified }) if (appState.exportVerifierCalldata) { const calldata = await snarkjs.plonk.exportSolidityCallData(proof, publicSignals) diff --git a/apps/circuit-compiler/src/app/components/container.tsx b/apps/circuit-compiler/src/app/components/container.tsx index 982395d11e..3a18465fa4 100644 --- a/apps/circuit-compiler/src/app/components/container.tsx +++ b/apps/circuit-compiler/src/app/components/container.tsx @@ -140,7 +140,7 @@ export function Container () { 0}> <> - + diff --git a/apps/circuit-compiler/src/app/components/witness.tsx b/apps/circuit-compiler/src/app/components/witness.tsx index 0a14537cde..dd40b9b2d0 100644 --- a/apps/circuit-compiler/src/app/components/witness.tsx +++ b/apps/circuit-compiler/src/app/components/witness.tsx @@ -1,12 +1,14 @@ import { RenderIf, RenderIfNot } from "@remix-ui/helper"; import { FormattedMessage } from "react-intl"; -import { CompilerStatus } from "../types"; import { computeWitness } from "../actions"; -import { useState } from "react"; -import type { CircomPluginClient } from "../services/circomPluginClient"; +import { useContext, useState } from "react"; import * as remixLib from '@remix-project/remix-lib' +import { CircuitAppContext } from "../contexts"; -export function WitnessSection ({ plugin, signalInputs, status }: {plugin: CircomPluginClient, signalInputs: string[], status: CompilerStatus}) { +export function WitnessSection () { + const circuitApp = useContext(CircuitAppContext) + const { signalInputs, status, exportWtnsJson } = circuitApp.appState + const { plugin, dispatch, appState } = circuitApp const [witnessValues, setWitnessValues] = useState>({}) const handleSignalInput = (e: any) => { @@ -47,9 +49,22 @@ export function WitnessSection ({ plugin, signalInputs, status }: {plugin: Circo )) } +
+ { dispatch({ type: 'SET_EXPORT_WTNS_JSON', payload: !exportWtnsJson }) }} + checked={exportWtnsJson} + /> + +