Skip to content

Commit

Permalink
chore(poseidon-proof): add conditional bundles
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Nov 24, 2023
1 parent 9af8926 commit 2d1346a
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 66 deletions.
1 change: 0 additions & 1 deletion packages/groth16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues"
},
"scripts": {
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
"build": "rimraf dist && yarn build:browser && yarn build:node",
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
Expand Down
19 changes: 12 additions & 7 deletions packages/poseidon-proof/package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
{
"name": "@zk-kit/poseidon-proof",
"version": "0.4.1",
"version": "0.5.0",
"description": "A library to generate and verify Poseidon proofs.",
"license": "MIT",
"main": "dist/index.js",
"main": "dist/index.node.js",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.js",
"types": "./dist/types/index.d.ts"
"node": {
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.js"
},
"browser": "./dist/index.browser.mjs",
"default": "./dist/index.browser.mjs"
},
"types": "dist/types/index.d.ts",
"files": [
Expand All @@ -25,14 +28,16 @@
"url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues"
},
"scripts": {
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
"build": "rimraf dist && yarn build:browser && yarn build:node",
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@rollup/plugin-alias": "^5.0.1",
"@rollup/plugin-json": "^5.0.1",
"@types/download": "^8.0.5",
"@types/tmp": "^0.2.6",
Expand Down
38 changes: 38 additions & 0 deletions packages/poseidon-proof/rollup.browser.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import alias from "@rollup/plugin-alias"
import json from "@rollup/plugin-json"
import * as fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import typescript from "rollup-plugin-typescript2"

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf-8"))
const banner = `/**
* @module ${pkg.name}
* @version ${pkg.version}
* @file ${pkg.description}
* @copyright Ethereum Foundation 2023
* @license ${pkg.license}
* @see [Github]{@link ${pkg.homepage}}
*/`

export default {
input: "src/index.ts",
output: [
{
file: pkg.exports.browser,
format: "es",
banner
}
],
external: Object.keys(pkg.dependencies),
plugins: [
alias({
entries: [{ find: "./get-snark-artifacts.node", replacement: "./get-snark-artifacts.browser" }]
}),
typescript({
tsconfig: "./build.tsconfig.json",
useTsconfigDeclarationDir: true
}),
cleanup({ comments: "jsdoc" }),
json()
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,13 @@ export default {
input: "src/index.ts",
output: [
{
file: pkg.exports.require,
file: pkg.exports.node.require,
format: "cjs",
banner,
exports: "auto"
},
{
file: pkg.exports.import,
file: pkg.exports.node.import,
format: "es",
banner
}
Expand Down
36 changes: 3 additions & 33 deletions packages/poseidon-proof/src/generate.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import { BigNumber } from "@ethersproject/bignumber"
import { BytesLike, Hexable } from "@ethersproject/bytes"
import { NumericString, prove } from "@zk-kit/groth16"

import { defaultSnarkArtifacts } from "./config"
import getSnarkArtifacts from "./get-snark-artifacts.node"
import hash from "./hash"
import packProof from "./packProof"
import packProof from "./pack-proof"
import { PoseidonProof, SnarkArtifacts } from "./types"
import { isBrowser, isNode } from "./utils"

/**
* Creates a zero-knowledge proof to prove that you have the pre-image of a hash,
Expand All @@ -31,35 +29,7 @@ export default async function generate(
// If the Snark artifacts are not defined they will be automatically downloaded.
/* istanbul ignore next */
if (!snarkArtifacts) {
if (isNode()) {
const { default: download } = await import("download")
const { default: fs } = await import("fs")
const { default: tmp } = await import("tmp")

const tmpDir = "poseidon-proof"
const tmpPath = `${tmp.tmpdir}/${tmpDir}`

if (!fs.existsSync(tmpPath)) {
tmp.dirSync({ name: tmpDir })

await download(defaultSnarkArtifacts.wasmFilePath, tmpPath)
await download(defaultSnarkArtifacts.zkeyFilePath, tmpPath)
}

snarkArtifacts = {
wasmFilePath: `${tmpPath}/poseidon-proof.wasm`,
zkeyFilePath: `${tmpPath}/poseidon-proof.zkey`
}
}

if (isBrowser()) {
snarkArtifacts = defaultSnarkArtifacts
}
}

/* istanbul ignore next */
if (!snarkArtifacts) {
throw new Error("Error: Missing Snark artifacts. Please ensure all necessary Snark artifacts are included.")
snarkArtifacts = await getSnarkArtifacts()
}

const { proof, publicSignals } = await prove(
Expand Down
6 changes: 6 additions & 0 deletions packages/poseidon-proof/src/get-snark-artifacts.browser.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/* istanbul ignore file */
import { defaultSnarkArtifacts } from "./config"

export default async function getSnarkArtifacts() {
return defaultSnarkArtifacts
}
22 changes: 22 additions & 0 deletions packages/poseidon-proof/src/get-snark-artifacts.node.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
/* istanbul ignore file */
import download from "download"
import tmp from "tmp"
import fs from "fs"
import { defaultSnarkArtifacts } from "./config"

export default async function getSnarkArtifacts() {
const tmpDir = "poseidon-proof"
const tmpPath = `${tmp.tmpdir}/${tmpDir}`

if (!fs.existsSync(tmpPath)) {
tmp.dirSync({ name: tmpDir })

await download(defaultSnarkArtifacts.wasmFilePath, tmpPath)
await download(defaultSnarkArtifacts.zkeyFilePath, tmpPath)
}

return {
wasmFilePath: `${tmpPath}/poseidon-proof.wasm`,
zkeyFilePath: `${tmpPath}/poseidon-proof.zkey`
}
}
File renamed without changes.
File renamed without changes.
20 changes: 0 additions & 20 deletions packages/poseidon-proof/src/utils.ts

This file was deleted.

2 changes: 1 addition & 1 deletion packages/poseidon-proof/src/verify.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { verify as _verify } from "@zk-kit/groth16"
import hash from "./hash"
import { PoseidonProof } from "./types"
import unpackProof from "./unpackProof"
import unpackProof from "./unpack-proof"
import verificationKey from "./verification-key.json"

/**
Expand Down
4 changes: 2 additions & 2 deletions packages/poseidon-proof/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { buildBn128 } from "@zk-kit/groth16"
import { poseidon1, poseidon2 } from "poseidon-lite"
import generate from "../src/generate"
import packProof from "../src/packProof"
import packProof from "../src/pack-proof"
import { PoseidonProof } from "../src/types"
import unpackProof from "../src/unpackProof"
import unpackProof from "../src/unpack-proof"
import verify from "../src/verify"
import hash from "../src/hash"

Expand Down
22 changes: 22 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3386,6 +3386,20 @@ __metadata:
languageName: node
linkType: hard

"@rollup/plugin-alias@npm:^5.0.1":
version: 5.0.1
resolution: "@rollup/plugin-alias@npm:5.0.1"
dependencies:
slash: ^4.0.0
peerDependencies:
rollup: ^1.20.0||^2.0.0||^3.0.0||^4.0.0
peerDependenciesMeta:
rollup:
optional: true
checksum: dab5bba16a0425c8412ece858df677c9f792bb0c5a7789c1677ee34b03533c1cda407191fcc40f3e93eb5f992cd8ed0e980e49dad3e38346547a2723b0f56fa1
languageName: node
linkType: hard

"@rollup/plugin-commonjs@npm:^24.1.0":
version: 24.1.0
resolution: "@rollup/plugin-commonjs@npm:24.1.0"
Expand Down Expand Up @@ -4513,6 +4527,7 @@ __metadata:
"@ethersproject/bignumber": ^5.7.0
"@ethersproject/bytes": ^5.7.0
"@ethersproject/keccak256": ^5.7.0
"@rollup/plugin-alias": ^5.0.1
"@rollup/plugin-json": ^5.0.1
"@types/download": ^8.0.5
"@types/tmp": ^0.2.6
Expand Down Expand Up @@ -17832,6 +17847,13 @@ __metadata:
languageName: node
linkType: hard

"slash@npm:^4.0.0":
version: 4.0.0
resolution: "slash@npm:4.0.0"
checksum: da8e4af73712253acd21b7853b7e0dbba776b786e82b010a5bfc8b5051a1db38ed8aba8e1e8f400dd2c9f373be91eb1c42b66e91abb407ff42b10feece5e1d2d
languageName: node
linkType: hard

"slice-ansi@npm:^2.1.0":
version: 2.1.0
resolution: "slice-ansi@npm:2.1.0"
Expand Down

0 comments on commit 2d1346a

Please sign in to comment.