Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Export function to build bn128 ffjavascript curve #70

Merged
merged 1 commit into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion packages/groth16/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
13 changes: 13 additions & 0 deletions packages/groth16/src/buildBn128.ts
Original file line number Diff line number Diff line change
@@ -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<any> {
// @ts-ignore
return globalThis.curve_bn128 ?? (await _buildBn128())
}
3 changes: 2 additions & 1 deletion packages/groth16/src/index.ts
Original file line number Diff line number Diff line change
@@ -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"
6 changes: 3 additions & 3 deletions packages/groth16/src/verify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
5 changes: 2 additions & 3 deletions packages/groth16/src/zkey-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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()
Expand Down
5 changes: 2 additions & 3 deletions packages/groth16/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -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", () => {
Expand All @@ -10,7 +9,7 @@ describe("Groth16", () => {
let proof: any

beforeAll(async () => {
curve = await getCurveFromName("bn128")
curve = await buildBn128()
})

afterAll(async () => {
Expand Down
Loading