Skip to content

Commit

Permalink
upgraded snarkjs version, added termination fix
Browse files Browse the repository at this point in the history
  • Loading branch information
mllwchrry committed Oct 29, 2024
1 parent e04194c commit 1dfa07a
Show file tree
Hide file tree
Showing 7 changed files with 85 additions and 44 deletions.
66 changes: 28 additions & 38 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
},
"dependencies": {
"ejs": "3.1.10",
"snarkjs": "0.7.3"
"snarkjs": "0.7.5"
},
"devDependencies": {
"@nomicfoundation/hardhat-ethers": "3.0.5",
Expand Down
1 change: 1 addition & 0 deletions src/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const BN128_CURVE_NAME = "bn128";
10 changes: 9 additions & 1 deletion src/core/CircuitZKit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ArtifactsFileType, CircuitZKitConfig, VerifierLanguageType } from "../t
import { Signals } from "../types/proof-utils";
import { CalldataByProtocol, IProtocolImplementer, ProofStructByProtocol, ProvingSystemType } from "../types/protocols";

import { getBn128Curve } from "../utils";

/**
* `CircuitZKit` represents a single circuit and provides a high-level API to work with it.
*/
Expand Down Expand Up @@ -45,9 +47,15 @@ export class CircuitZKit<Type extends ProvingSystemType> {
const wtnsFile = path.join(tmpDir, `${this.getCircuitName()}.wtns`);
const wasmFile = this.mustGetArtifactsFilePath("wasm");

const curve = await getBn128Curve();

await snarkjs.wtns.calculate(inputs, wasmFile, wtnsFile);

return (await snarkjs.wtns.exportJson(wtnsFile)) as bigint[];
const wtnsJson = await snarkjs.wtns.exportJson(wtnsFile);

curve.terminate();

return wtnsJson as bigint[];
}

/**
Expand Down
22 changes: 20 additions & 2 deletions src/core/protocols/Groth16Implementer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,38 @@ import { AbstractProtocolImplementer } from "./AbstractImplementer";
import { Signals } from "../../types/proof-utils";
import { Groth16ProofStruct, ProvingSystemType, Groth16Calldata } from "../../types/protocols";

import { getBn128Curve } from "../../utils";

export class Groth16Implementer extends AbstractProtocolImplementer<"groth16"> {
public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<Groth16ProofStruct> {
return (await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath)) as Groth16ProofStruct;
const curve = await getBn128Curve();

const fullProof = await snarkjs.groth16.fullProve(inputs, wasmFilePath, zKeyFilePath);

curve.terminate();

return fullProof as Groth16ProofStruct;
}

public async verifyProof(proof: Groth16ProofStruct, vKeyFilePath: string): Promise<boolean> {
const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString());

return await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);
const curve = await getBn128Curve();

const proofVerification = await snarkjs.groth16.verify(verifier, proof.publicSignals, proof.proof);

curve.terminate();

return proofVerification;
}

public async generateCalldata(proof: Groth16ProofStruct): Promise<Groth16Calldata> {
const curve = await getBn128Curve();

const calldata = await snarkjs.groth16.exportSolidityCallData(proof.proof, proof.publicSignals);

curve.terminate();

return JSON.parse(`[${calldata}]`) as Groth16Calldata;
}

Expand Down
22 changes: 20 additions & 2 deletions src/core/protocols/PlonkImplementer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,39 @@ import { AbstractProtocolImplementer } from "./AbstractImplementer";
import { Signals } from "../../types/proof-utils";
import { PlonkCalldata, PlonkProofStruct, ProvingSystemType } from "../../types/protocols";

import { getBn128Curve } from "../../utils";

export class PlonkImplementer extends AbstractProtocolImplementer<"plonk"> {
public async generateProof(inputs: Signals, zKeyFilePath: string, wasmFilePath: string): Promise<PlonkProofStruct> {
return (await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath)) as PlonkProofStruct;
const curve = await getBn128Curve();

const fullProof = await snarkjs.plonk.fullProve(inputs, wasmFilePath, zKeyFilePath);

curve.terminate();

return fullProof as PlonkProofStruct;
}

public async verifyProof(proof: PlonkProofStruct, vKeyFilePath: string): Promise<boolean> {
const curve = await getBn128Curve();

const verifier = JSON.parse(fs.readFileSync(vKeyFilePath).toString());

return await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof);
const proofVerification = await snarkjs.plonk.verify(verifier, proof.publicSignals, proof.proof);

curve.terminate();

return proofVerification;
}

public async generateCalldata(proof: PlonkProofStruct): Promise<PlonkCalldata> {
const curve = await getBn128Curve();

const calldata = await snarkjs.plonk.exportSolidityCallData(proof.proof, proof.publicSignals);
const proofArrEndIndex: number = calldata.indexOf("]") + 1;

curve.terminate();

return JSON.parse(
`[${calldata.slice(0, proofArrEndIndex)},${calldata.slice(proofArrEndIndex, calldata.length)}]`,
) as PlonkCalldata;
Expand Down
6 changes: 6 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import * as snarkjs from "snarkjs";
import { BN128_CURVE_NAME } from "./constants";

export async function getBn128Curve() {
return (snarkjs as any).curves.getCurveFromName(BN128_CURVE_NAME);
}

0 comments on commit 1dfa07a

Please sign in to comment.