Skip to main content
Version: 1.0.1

@unirep/circuits

Github license NPM version Downloads Linter eslint Code style prettier

🛠 Install

npm or yarn

Install the @unirep/circuits package with npm:

npm i @unirep/circuits

or yarn:

yarn add @unirep/circuits

📔 Usage

Prover

Build a prover for unirep protocol

import * as snarkjs from 'snarkjs'
import { Circuit, Prover } from '@unirep/circuits'
import { SnarkProof, SnarkPublicSignals } from '@unirep/crypto'

const buildPath = 'PATH/TO/CIRCUIT/FOLDER/'

const prover: Prover = {
genProofAndPublicSignals: async (
circuitName: string | Circuit,
inputs: any
): Promise<any> => {
const circuitWasmPath = path.join(buildPath, `${circuitName}.wasm`)
const zkeyPath = path.join(buildPath, `${circuitName}.zkey`)
const { proof, publicSignals } = await snarkjs.groth16.fullProve(
inputs,
circuitWasmPath,
zkeyPath
)

return { proof, publicSignals }
},

verifyProof: async (
circuitName: string | Circuit,
publicSignals: SnarkPublicSignals,
proof: SnarkProof
): Promise<boolean> => {
const vkey = require(path.join(buildPath, `${circuitName}.vkey.json`))
return snarkjs.groth16.verify(vkey, publicSignals, proof)
},

getVKey: (name: string | Circuit) => {
return require(path.join(buildPath, `${name}.vkey.json`))
}
}

Generate proof and verify it with the above prover

import { Circuit } from '@unirep/circuits'

// See ./test/verifyEpochKey.test.ts for generating circuit inputs
const circuitInputs = {
identity_nullifier: ...,
identity_trapdoor: ...,
user_tree_root: ...,
...
}
const { proof, publicSignals } = await prover.genProofAndPublicSignals(
Circuit.verifyEpochKey,
circuitInputs
)

const isValid = await prover.verifyProof(
Circuit.verifyEpochKey,
publicSignals,
proof
)