diff --git a/packages/groth16/package.json b/packages/groth16/package.json index 5e8c282e7..f4024d30c 100644 --- a/packages/groth16/package.json +++ b/packages/groth16/package.json @@ -1,6 +1,6 @@ { "name": "@zk-kit/groth16", - "version": "0.3.0", + "version": "0.4.0", "description": "A snippet of SnarkJS code for verifying and generating Groth16 proofs only.", "license": "GPL-3.0", "main": "dist/index.node.js", diff --git a/packages/groth16/src/buildBn128.ts b/packages/groth16/src/buildBn128.ts new file mode 100644 index 000000000..06ee286fb --- /dev/null +++ b/packages/groth16/src/buildBn128.ts @@ -0,0 +1,13 @@ +/* istanbul ignore file */ + +import { buildBn128 as _buildBn128 } from "ffjavascript" + +/** + * Builds and returns a BN 128 curve. If the curve has been cached, + * it returns the cached curve. + * @returns BN 128 ffjavascript curve. + */ +export default async function buildBn128(): Promise { + // @ts-ignore + return globalThis.curve_bn128 ?? (await _buildBn128()) +} diff --git a/packages/groth16/src/index.ts b/packages/groth16/src/index.ts index d2ac9dc31..7b93ac0b3 100644 --- a/packages/groth16/src/index.ts +++ b/packages/groth16/src/index.ts @@ -1,5 +1,6 @@ import prove from "./prove" import verify from "./verify" +import buildBn128 from "./buildBn128" -export { prove, verify } +export { prove, verify, buildBn128 } export * from "./types" diff --git a/packages/groth16/src/verify.ts b/packages/groth16/src/verify.ts index 016ee202e..851be5bd2 100644 --- a/packages/groth16/src/verify.ts +++ b/packages/groth16/src/verify.ts @@ -2,7 +2,8 @@ /* eslint-disable @typescript-eslint/naming-convention */ /* istanbul ignore file */ -import { Scalar, utils, buildBn128 } from "ffjavascript" +import { Scalar, utils } from "ffjavascript" +import buildBn128 from "./buildBn128" import { Groth16Proof, PublicSignals } from "./types" const { unstringifyBigInts } = utils @@ -43,8 +44,7 @@ export default async function verify( proof = unstringifyBigInts(proof) publicSignals = unstringifyBigInts(publicSignals) - // @ts-ignore - const curve = globalThis.curve_bn128 ?? (await buildBn128(undefined, undefined)) + const curve = await buildBn128() const IC0 = curve.G1.fromObject(verificationKey.IC[0]) const IC = new Uint8Array(curve.G1.F.n8 * 2 * publicSignals.length) diff --git a/packages/groth16/src/zkey-utils.ts b/packages/groth16/src/zkey-utils.ts index c3b245a28..90839da87 100644 --- a/packages/groth16/src/zkey-utils.ts +++ b/packages/groth16/src/zkey-utils.ts @@ -3,7 +3,7 @@ // @ts-ignore import * as binFileUtils from "@iden3/binfileutils" -import { buildBn128 } from "ffjavascript" +import buildBn128 from "./buildBn128" import { log2 } from "./utils" async function readG1(fd: any, curve: any, toObject: any) { @@ -32,8 +32,7 @@ async function readHeaderGroth16(fd: any, sections: any, toObject: any) { zkey.n8r = n8r zkey.r = await binFileUtils.readBigInt(fd, n8r) - // @ts-ignore - zkey.curve = globalThis.curve_bn128 ?? (await buildBn128(undefined, undefined)) + zkey.curve = await buildBn128() zkey.nVars = await fd.readULE32() zkey.nPublic = await fd.readULE32() diff --git a/packages/groth16/tests/index.test.ts b/packages/groth16/tests/index.test.ts index c2f04e379..a9c185083 100644 --- a/packages/groth16/tests/index.test.ts +++ b/packages/groth16/tests/index.test.ts @@ -1,5 +1,4 @@ -import { getCurveFromName } from "ffjavascript" -import { verify, prove } from "../src" +import { verify, prove, buildBn128 } from "../src" import verificationKey from "../snark-artifacts/index.json" describe("Groth16", () => { @@ -10,7 +9,7 @@ describe("Groth16", () => { let proof: any beforeAll(async () => { - curve = await getCurveFromName("bn128") + curve = await buildBn128() }) afterAll(async () => {