|
|
@ -1,6 +1,6 @@ |
|
|
|
import * as snarkjs from 'snarkjs' |
|
|
|
import * as snarkjs from 'snarkjs' |
|
|
|
import type { CircomPluginClient } from "../services/circomPluginClient" |
|
|
|
import type { CircomPluginClient } from "../services/circomPluginClient" |
|
|
|
import { AppState, ICircuitAppContext } from "../types" |
|
|
|
import { ActionPayloadTypes, AppState, ICircuitAppContext } from "../types" |
|
|
|
import { GROTH16_VERIFIER, PLONK_VERIFIER } from './constant' |
|
|
|
import { GROTH16_VERIFIER, PLONK_VERIFIER } from './constant' |
|
|
|
import { extractNameFromKey, extractParentFromKey } from '@remix-ui/helper' |
|
|
|
import { extractNameFromKey, extractParentFromKey } from '@remix-ui/helper' |
|
|
|
import { ethers } from 'ethers' |
|
|
|
import { ethers } from 'ethers' |
|
|
@ -50,31 +50,30 @@ 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, zkLogger(plugin)) |
|
|
|
await snarkjs.zKey.newZKey(r1cs, ptau_final, zkey_final, zkLogger(plugin, dispatch, 'SET_SETUP_EXPORT_FEEDBACK')) |
|
|
|
await snarkjs.zKey.verifyFromR1cs(r1cs, ptau_final, zkey_final, zkLogger(plugin)) |
|
|
|
const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin, dispatch, 'SET_SETUP_EXPORT_FEEDBACK')) |
|
|
|
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, zkLogger(plugin)) |
|
|
|
const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates, zkLogger(plugin, dispatch, 'SET_SETUP_EXPORT_FEEDBACK')) |
|
|
|
|
|
|
|
|
|
|
|
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, zkLogger(plugin)) |
|
|
|
await snarkjs.plonk.setup(r1cs, ptau_final, zkey_final, zkLogger(plugin, dispatch, 'SET_SETUP_EXPORT_FEEDBACK')) |
|
|
|
const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin)) |
|
|
|
const vKey = await snarkjs.zKey.exportVerificationKey(zkey_final, zkLogger(plugin, dispatch, 'SET_SETUP_EXPORT_FEEDBACK')) |
|
|
|
|
|
|
|
|
|
|
|
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, zkLogger(plugin)) |
|
|
|
const solidityContract = await snarkjs.zKey.exportSolidityVerifier(zkey_final, templates, zkLogger(plugin, dispatch, 'SET_SETUP_EXPORT_FEEDBACK')) |
|
|
|
|
|
|
|
|
|
|
|
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) |
|
|
|
} |
|
|
|
} |
|
|
@ -85,7 +84,6 @@ export const runSetupAndExport = async (plugin: CircomPluginClient, appState: Ap |
|
|
|
dispatch({ type: 'SET_SETUP_EXPORT_STATUS', payload: 'done' }) |
|
|
|
dispatch({ type: 'SET_SETUP_EXPORT_STATUS', payload: 'done' }) |
|
|
|
} catch (e) { |
|
|
|
} catch (e) { |
|
|
|
dispatch({ type: 'SET_COMPILER_STATUS', payload: 'errored' }) |
|
|
|
dispatch({ type: 'SET_COMPILER_STATUS', payload: 'errored' }) |
|
|
|
dispatch({ type: 'SET_SETUP_EXPORT_FEEDBACK', payload: e.message }) |
|
|
|
|
|
|
|
console.error(e) |
|
|
|
console.error(e) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
@ -107,10 +105,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, zkLogger(plugin)) |
|
|
|
await snarkjs.wtns.check(r1cs, wtns, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) |
|
|
|
if (appState.provingScheme === 'groth16') { |
|
|
|
if (appState.provingScheme === 'groth16') { |
|
|
|
const { proof, publicSignals } = await snarkjs.groth16.prove(zkey_final, wtns, zkLogger(plugin)) |
|
|
|
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)) |
|
|
|
const verified = await snarkjs.groth16.verify(vKey, publicSignals, proof, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) |
|
|
|
|
|
|
|
|
|
|
|
plugin.call('terminal', 'log', { type: 'log', value: 'zk proof validity ' + verified }) |
|
|
|
plugin.call('terminal', 'log', { type: 'log', value: 'zk proof validity ' + verified }) |
|
|
|
if (appState.exportVerifierCalldata) { |
|
|
|
if (appState.exportVerifierCalldata) { |
|
|
@ -119,8 +117,8 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta |
|
|
|
plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/build/verifierCalldata.json`, calldata) |
|
|
|
plugin.call('fileManager', 'writeFile', `${extractParentFromKey(appState.filePath)}/groth16/zk/build/verifierCalldata.json`, calldata) |
|
|
|
} |
|
|
|
} |
|
|
|
} else if (appState.provingScheme === 'plonk') { |
|
|
|
} else if (appState.provingScheme === 'plonk') { |
|
|
|
const { proof, publicSignals } = await snarkjs.plonk.prove(zkey_final, wtns, zkLogger(plugin)) |
|
|
|
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)) |
|
|
|
const verified = await snarkjs.plonk.verify(vKey, publicSignals, proof, zkLogger(plugin, dispatch, 'SET_PROOF_FEEDBACK')) |
|
|
|
|
|
|
|
|
|
|
|
plugin.call('terminal', 'log', { type: 'log', value: 'zk proof validity ' + verified }) |
|
|
|
plugin.call('terminal', 'log', { type: 'log', value: 'zk proof validity ' + verified }) |
|
|
|
if (appState.exportVerifierCalldata) { |
|
|
|
if (appState.exportVerifierCalldata) { |
|
|
@ -138,12 +136,13 @@ export const generateProof = async (plugin: CircomPluginClient, appState: AppSta |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
function zkLogger(plugin: CircomPluginClient) { |
|
|
|
function zkLogger(plugin: CircomPluginClient, dispatch: ICircuitAppContext['dispatch'], dispatchType: keyof ActionPayloadTypes) { |
|
|
|
return { |
|
|
|
return { |
|
|
|
info: (...args) => plugin.call('terminal', 'log', { type: 'log', value: args.join(' ') }), |
|
|
|
info: (...args) => plugin.call('terminal', 'log', { type: 'log', value: args.join(' ') }), |
|
|
|
debug: (...args) => plugin.call('terminal', 'log', { type: 'log', value: args.join(' ') }), |
|
|
|
debug: (...args) => plugin.call('terminal', 'log', { type: 'log', value: args.join(' ') }), |
|
|
|
error: (...args) => { |
|
|
|
error: (...args) => { |
|
|
|
plugin.call('terminal', 'log', { type: 'error', value: args.join(' ') }) |
|
|
|
plugin.call('terminal', 'log', { type: 'error', value: args.join(' ') }) |
|
|
|
|
|
|
|
dispatch({ type: dispatchType as any, payload: args.join(' ') }) |
|
|
|
plugin.emit('statusChanged', { key: args.length, title: `You have ${args.length} problem${args.length === 1 ? '' : 's'}`, type: 'error' }) |
|
|
|
plugin.emit('statusChanged', { key: args.length, title: `You have ${args.length} problem${args.length === 1 ? '' : 's'}`, type: 'error' }) |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|