Add zk logger

pull/5057/head
ioedeveloper 3 months ago committed by Aniket
parent 7e9bca43f5
commit 4e0c5c5a9e
  1. 35
      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" } const zkey_final = { type: "mem" }
if (appState.provingScheme === 'groth16') { if (appState.provingScheme === 'groth16') {
await snarkjs.zKey.newZKey(r1cs, ptau_final, zkey_final) await snarkjs.zKey.newZKey(r1cs, ptau_final, zkey_final, zkLogger(plugin))
await snarkjs.zKey.verifyFromR1cs(r1cs, ptau_final, zkey_final) await snarkjs.zKey.verifyFromR1cs(r1cs, ptau_final, zkey_final, zkLogger(plugin))
const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final) const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin))
if (appState.exportVerificationKey) { if (appState.exportVerificationKey) {
await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/keys/verification_key.json`, JSON.stringify(vKey, null, 2)) await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/keys/verification_key.json`, JSON.stringify(vKey, null, 2))
} }
if (appState.exportVerificationContract) { if (appState.exportVerificationContract) {
const templates = { groth16: GROTH16_VERIFIER } 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) 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_ZKEY', payload: zkey_final })
dispatch({ type: 'SET_VERIFICATION_KEY', payload: vKey }) dispatch({ type: 'SET_VERIFICATION_KEY', payload: vKey })
} else if (appState.provingScheme === 'plonk') { } else if (appState.provingScheme === 'plonk') {
await snarkjs.plonk.setup(r1cs, ptau_final, zkey_final) await snarkjs.plonk.setup(r1cs, ptau_final, zkey_final, zkLogger(plugin))
const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final) const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin))
if (appState.exportVerificationKey) { if (appState.exportVerificationKey) {
await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/keys/verification_key.json`, JSON.stringify(vKey, null, 2)) await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/keys/verification_key.json`, JSON.stringify(vKey, null, 2))
} }
if (appState.exportVerificationContract) { if (appState.exportVerificationContract) {
const templates = { plonk: PLONK_VERIFIER } 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) 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 zkey_final = appState.zKey
const vKey = appState.verificationKey const vKey = appState.verificationKey
await snarkjs.wtns.check(r1cs, wtns) await snarkjs.wtns.check(r1cs, wtns, zkLogger(plugin))
if (appState.provingScheme === 'groth16') { if (appState.provingScheme === 'groth16') {
const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns) const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns, zkLogger(plugin))
const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof) const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof, zkLogger(plugin))
console.log('zk proof validity', verified) console.log('zk proof validity', verified)
await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/build/input.json`, JSON.stringify({ 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, _pubSignals: publicSignals,
}, null, 2)) }, null, 2))
} else if (appState.provingScheme === 'plonk') { } else if (appState.provingScheme === 'plonk') {
const { proof, publicSignals } = await snarkjs.plonk.prove(zkey_final, wtns) const { proof, publicSignals } = await snarkjs.plonk.prove(zkey_final, wtns, zkLogger(plugin))
const verified = await snarkjs.plonk.verify(vKey, publicSignals, proof) const verified = await snarkjs.plonk.verify(vKey, publicSignals, proof, zkLogger(plugin))
console.log('zk proof validity', verified) console.log('zk proof validity', verified)
await plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/plonk/zk/build/input.json`, JSON.stringify({ 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) 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' })
}
}
}

Loading…
Cancel
Save