From 4e0c5c5a9e32e44426bc16d283430de51d6493e1 Mon Sep 17 00:00:00 2001 From: ioedeveloper Date: Mon, 12 Aug 2024 16:04:32 +0100 Subject: [PATCH] Add zk logger --- .../circuit-compiler/src/app/actions/index.ts | 35 ++++++++++++------- 1 file changed, 23 insertions(+), 12 deletions(-) diff --git a/apps/circuit-compiler/src/app/actions/index.ts b/apps/circuit-compiler/src/app/actions/index.ts index aba210d85c..2bde44a559 100644 --- a/apps/circuit-compiler/src/app/actions/index.ts +++ b/apps/circuit-compiler/src/app/actions/index.ts @@ -50,31 +50,31 @@ export const runSetupAndExport = async (plugin: CircomPluginClient, appState: Ap const zkey_final = { type: "mem" } if (appState.provingScheme === 'groth16') { - await snarkjs.zKey.newZKey(r1cs, ptau_final, zkey_final) - await snarkjs.zKey.verifyFromR1cs(r1cs, ptau_final, zkey_final) - const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final) + await snarkjs.zKey.newZKey(r1cs, ptau_final, zkey_final, zkLogger(plugin)) + await snarkjs.zKey.verifyFromR1cs(r1cs, ptau_final, zkey_final, zkLogger(plugin)) + const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin)) if (appState.exportVerificationKey) { await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/keys/verification_key.json`, JSON.stringify(vKey, null, 2)) } if (appState.exportVerificationContract) { const templates = { groth16: GROTH16_VERIFIER } - const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates) + const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates, zkLogger(plugin)) await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/build/zk_verifier.sol`, solidityContract) } dispatch({ type: 'SET_ZKEY', payload: zkey_final }) dispatch({ type: 'SET_VERIFICATION_KEY', payload: vKey }) } else if (appState.provingScheme === 'plonk') { - await snarkjs.plonk.setup(r1cs, ptau_final, zkey_final) - const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final) + await snarkjs.plonk.setup(r1cs, ptau_final, zkey_final, zkLogger(plugin)) + const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin)) if (appState.exportVerificationKey) { await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/keys/verification_key.json`, JSON.stringify(vKey, null, 2)) } if (appState.exportVerificationContract) { const templates = { plonk: PLONK_VERIFIER } - const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates) + const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates, zkLogger(plugin)) await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/build/zk_verifier.sol`, solidityContract) } @@ -107,10 +107,10 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta const zkey_final = appState.zKey const vKey = appState.verificationKey - await snarkjs.wtns.check(r1cs, wtns) + await snarkjs.wtns.check(r1cs, wtns, zkLogger(plugin)) if (appState.provingScheme === 'groth16') { - const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns) - const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof) + const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns, zkLogger(plugin)) + const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof, zkLogger(plugin)) console.log('zk proof validity', verified) await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/build/input.json`, JSON.stringify({ @@ -120,8 +120,8 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta _pubSignals: publicSignals, }, null, 2)) } else if (appState.provingScheme === 'plonk') { - const { proof, publicSignals } = await snarkjs.plonk.prove(zkey_final, wtns) - const verified = await snarkjs.plonk.verify(vKey, publicSignals, proof) + const { proof, publicSignals } = await snarkjs.plonk.prove(zkey_final, wtns, zkLogger(plugin)) + const verified = await snarkjs.plonk.verify(vKey, publicSignals, proof, zkLogger(plugin)) console.log('zk proof validity', verified) await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/build/input.json`, JSON.stringify({ @@ -162,3 +162,14 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta console.error(e) } } + +function zkLogger(plugin: CircomPluginClient) { + return { + info: (...args) => plugin.call('terminal', 'log', { type: 'log', value: args.join(' ') }), + debug: (...args) => plugin.call('terminal', 'log', { type: 'log', value: args.join(' ') }), + error: (...args) => { + plugin.call('terminal', 'log', { type: 'error', value: args.join(' ') }) + plugin.emit('statusChanged', { key: args.length, title: `You have ${args.length} problem${args.length === 1 ? '' : 's'}`, type: 'error' }) + } + } +}