@unirep/circuits
Version:
Client library for circuit related functions which are used in UniRep protocol.
76 lines (75 loc) • 2.87 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BaseProof = void 0;
const utils_1 = require("./utils");
/**
* We build proofs using a `BaseProof` class that optionally supports verification.
* Proof data can be expressed in one of two formats:
*
* 1. `SnarkProof` objects for verification by `snarkjs`
* 2. `string[]` for contract verification.
*
* The `BaseProof` class can be used to convert between the two formats.
* This class should not be used directly, but should instead be inherited.
*
* The base class for a proof that can be verified using a [`Prover`](https://developer.unirep.io/docs/circuits-api/interfaces/src.Prover).
*/
class BaseProof {
/**
* Create a new instance of the class.
* @param publicSignals The public signals of the proof that can be verified by the prover
* @param proof The proof that can be verified by the prover
* @param prover The prover that can verify the public signals and the proof
* @example
* ```ts
* import { BaseProof } from '@unirep/circuits'
*
* class MyCustomProof extends BaseProof {
* constructor(publicSignals, proof, prover) {
* super(publicSignals, proof, prover)
*
* // Specify a circuit name for the Prover
* // This is typically a filename
* this.circuit = 'MyCustomProof'
* }
* }
* ```
*/
constructor(publicSignals, proof, prover) {
if (Array.isArray(proof)) {
// assume it's formatted for verifier contract
this.proof = proof.map((v) => BigInt(v));
this._snarkProof = (0, utils_1.formatProofForSnarkjsVerification)(proof.map((p) => p.toString()));
}
else if (typeof proof === 'object') {
// assume it's a SnarkProof
const formattedProof = (0, utils_1.formatProofForVerifierContract)(proof);
this._snarkProof = proof;
this.proof = formattedProof;
}
else {
throw new Error('Invalid proof supplied');
}
this.publicSignals = publicSignals.map((v) => BigInt(v));
this.prover = prover;
}
/**
* A function to verify the proof with the supplied `Prover`.
* The `prover` property must be set either in the constructor or manually, otherwise this will throw.
* @returns True if the proof is valid, false otherwise
* @example
* ```ts
* const isValid: boolean = await proof.verify()
* ```
*/
async verify() {
if (!this.prover) {
throw new Error('No prover set');
}
if (!this.circuit) {
throw new Error('No circuit specified');
}
return this.prover.verifyProof(this.circuit, this.publicSignals.map((n) => BigInt(n.toString())), this._snarkProof);
}
}
exports.BaseProof = BaseProof;