From 873ec52f0197c4998144ddaa5366a3f8cbfdcf6f Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Tue, 27 Aug 2024 12:25:34 +0100 Subject: [PATCH] Export witness as json --- .../circuit-compiler/src/app/actions/index.ts | 14 +++++++++-- .../src/app/components/container.tsx | 2 +- .../src/app/components/witness.tsx | 25 +++++++++++++++---- .../src/app/reducers/state.ts | 9 ++++++- .../src/app/services/circomPluginClient.ts | 3 ++- apps/circuit-compiler/src/app/types/index.ts | 2 ++ .../src/app/tabs/locales/en/circuit.json | 1 + 7 files changed, 46 insertions(+), 10 deletions(-) diff --git a/apps/circuit-compiler/src/app/actions/index.ts b/apps/circuit-compiler/src/app/actions/index.ts index 2f48faf2fb..67ac5813d4 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') } @@ -110,6 +118,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 +129,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} + /> + +