4. Epoch key proof

Before generating an epoch key proof, we should generate a current user state to know the current global state trees and the attestation histories.

DB

We should initialize a storage to save the state.

import { DB, SQLiteConnector } from 'anondb/node'
import { schema } from '@unirep/core'

// construct a memory db
const db = await SQLiteConnector.create(schema, ':memory:')
// or construct a SQLite db
// const db = await SQLiteConnector.create(schema, 'test.sqlite')

Prover

Also we have to initialize a prover to generate proofs and verify proofs.

const snarkjs = require('snarkjs')
import { Circuit, Prover } from '@unirep/circuits'
import { SnarkProof, SnarkPublicSignals } from '@unirep/crypto'
const buildPath = 'PATH/TO/THE/KEYS/'

const prover: Prover = {
    genProofAndPublicSignals: async (
        proofType: string | Circuit,
        inputs: any
    ): Promise<{
        proof: any,
        publicSignals: any
    }> => {
        const circuitWasmPath = buildPath + `${proofType}.wasm`
        const zkeyPath = buildPath + `${proofType}.zkey`
        const { proof, publicSignals } = await snarkjs.groth16.fullProve(
            inputs,
            circuitWasmPath,
            zkeyPath
        )
        return { proof, publicSignals }
    },
    verifyProof: async (
        name: string | Circuit,
        publicSignals: SnarkPublicSignals,
        proof: SnarkProof
    ): Promise<boolean> => {
        const vkey = require(buildPath + `${name}.vkey.json`)
        return snarkjs.groth16.verify(vkey, publicSignals, proof)
    },
}

Identity

Also, the ZkIdentity can be unserialized with a serialized identity, for example:

Generate current user state

And then we can use a genUserState to perform synchronization.

Generate epoch key proof

Use the user state to generate epoch key proof.

Submit epoch key proof

Get proof index

Then get the proof index with the proof hash

Verify epoch key proof

Verify the proof with UniRep smart contract

Verify the proof with local prover

Verify UniRep state

Check if the global state tree exists in the current UniRep state.

It can be verified by either a Synchronizer or a UserState object. But the Synchronizer doesn't take ZkIdentity as an input. For example

Then we can use the identity-free object to verify the global state tree root.

Last updated

Was this helpful?