Files
social-tw-website/packages/circuits/provers/defaultProver.ts
yanlong f62775347b [Enhancement][Frontend] add userstate db & update unirep to 2.0.0 (#224)
* update unirep to v2.0.0

* fix: lint

* fix: update unirep address in relay/src/config.ts

* fix: fix hashUserId not exist

* chore: delete unused import and console.log

* chore: lint:fix

* fix: fix bug in getData

* fix(frontend): fix schema and logout

* fix(frontend): fix typo

* fix: fix type of proof

* fix: fix publicSignal type

* chore: update yarn.lock

---------

Co-authored-by: Ya-wen, Jeng <vivi432@yahoo.com.tw>
2023-12-27 00:08:51 -06:00

61 lines
2.1 KiB
TypeScript

import path from 'path'
import { Circuit } from '@unirep/circuits'
import * as snarkjs from 'snarkjs'
const buildPath = '../zksnarkBuild'
/**
* The default prover that uses the circuits in default built folder `zksnarkBuild/`
*/
export const defaultProver = {
/**
* Generate proof and public signals with `snarkjs.groth16.fullProve`
* @param circuitName Name of the circuit, which can be chosen from `Circuit`
* @param inputs The user inputs of the circuit
* @returns snark proof and public signals
*/
genProofAndPublicSignals: async (
circuitName: string | Circuit,
inputs: any
): Promise<any> => {
const circuitWasmPath = path.join(
__dirname,
buildPath,
`${circuitName}.wasm`
)
const zkeyPath = path.join(__dirname, buildPath, `${circuitName}.zkey`)
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
inputs,
circuitWasmPath,
zkeyPath
)
return { proof, publicSignals }
},
/**
* Verify the snark proof and public signals with `snarkjs.groth16.verify`
* @param circuitName Name of the circuit, which can be chosen from `Circuit`
* @param publicSignals The snark public signals that is generated from `genProofAndPublicSignals`
* @param proof The snark proof that is generated from `genProofAndPublicSignals`
* @returns True if the proof is valid, false otherwise
*/
verifyProof: async (
circuitName: string | Circuit,
publicSignals: snarkjs.PublicSignals,
proof: snarkjs.Groth16Proof
): Promise<boolean> => {
const vkey = require(path.join(buildPath, `${circuitName}.vkey.json`))
return snarkjs.groth16.verify(vkey, publicSignals, proof)
},
/**
* Get vkey from default built folder `zksnarkBuild/`
* @param name Name of the circuit, which can be chosen from `Circuit`
* @returns vkey of the circuit
*/
getVKey: async (name: string | Circuit) => {
return require(path.join(buildPath, `${name}.vkey.json`))
},
}