@unirep/circuits
Version:
Client library for circuit related functions which are used in UniRep protocol.
82 lines (81 loc) • 3.53 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.defaultProver = void 0;
const path_1 = __importDefault(require("path"));
const snarkjs = __importStar(require("snarkjs"));
const buildPath = '../zksnarkBuild';
/**
* The default prover that uses the circuits in default built folder `zksnarkBuild/`
* @note
* :::caution
* The keys included are not safe for production use. A phase 2 trusted setup needs to be done before use.
* :::
* @example
* ```ts
* import { Circuit } from '@unirep/circuits'
* import prover from '@unirep/circuits/provers/defaultProver'
*
* await prover.genProofAndPublicSignals(Circuit.signup, {
* // circuit inputs
* })
* ```
*/
exports.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, inputs) => {
const circuitWasmPath = path_1.default.join(__dirname, buildPath, `${circuitName}.wasm`);
const zkeyPath = path_1.default.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 are 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, publicSignals, proof) => {
const vkey = require(path_1.default.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) => {
return require(path_1.default.join(buildPath, `${name}.vkey.json`));
},
};