Skip to content

Commit

Permalink
chore(poseidon): add benchmarks and update conditional exports
Browse files Browse the repository at this point in the history
re #238
  • Loading branch information
cedoor committed Apr 11, 2024
1 parent 033a5ed commit 5da772a
Show file tree
Hide file tree
Showing 58 changed files with 711 additions and 513 deletions.
2 changes: 1 addition & 1 deletion benchmarks/imt.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import b from "benny"
import { poseidon2 } from "poseidon-lite"
import { IMT, LeanIMT } from "../packages/imt/src"
import { IMT, LeanIMT } from "@zk-kit/imt"

const name = "incremental-merkle-trees"

Expand Down
4 changes: 4 additions & 0 deletions benchmarks/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import runIMT from "./imt"
import runPoseidon from "./poseidon"

const [benchmark] = process.argv.slice(2)

Expand All @@ -8,6 +9,9 @@ switch (benchmark) {
case "imt":
runIMT()
break
case "poseidon":
runPoseidon()
break
default:
runIMT()
}
37 changes: 37 additions & 0 deletions benchmarks/poseidon.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import b from "benny"
import { poseidon2 as _poseidon2 } from "poseidon-lite"
import { poseidon as _poseidon } from "circomlibjs"
import { poseidon2 } from "@zk-kit/poseidon"

const name = "poseidon"

export default async function run() {
const numberOfHashes = 50

b.suite(
name,

b.add(`PoseidonLite - ${numberOfHashes} hashes`, () => {
for (let i = 0; i < numberOfHashes; i += 1) {
_poseidon2([1n, 2n])
}
}),
b.add(`CircomlibJS Poseidon - ${numberOfHashes} hashes`, () => {
for (let i = 0; i < numberOfHashes; i += 1) {
_poseidon([1n, 2n])
}
}),
b.add(`ZK-Kit Poseidon - ${numberOfHashes} hashes`, () => {
for (let i = 0; i < numberOfHashes; i += 1) {
poseidon2([1n, 2n])
}
}),

b.cycle(),
b.complete(),

b.save({ folder: "benchmarks/results", file: name, version: "1.0.0", details: true }),
b.save({ folder: "benchmarks/results", file: name, format: "chart.html", details: true }),
b.save({ folder: "benchmarks/results", file: name, format: "table.html", details: true })
)
}
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,9 @@
"format:prettier:write": "prettier -w .",
"format": "turbo format:prettier",
"format:write": "turbo format:prettier:write",
"benchmarks": "rimraf benchmarks/results && ts-node benchmarks/index.ts",
"benchmarks": "rimraf benchmarks/results && tsx benchmarks/index.ts",
"docs": "typedoc --cname zkkit.pse.dev --githubPages true",
"remove:stable-version-field": "ts-node scripts/remove-stable-version-field.ts ${0} && yarn format:prettier:write",
"remove:stable-version-field": "tsx scripts/remove-stable-version-field.ts ${0} && yarn format:prettier:write",
"precommit": "lint-staged",
"postinstall": "husky install",
"style": "turbo lint:eslint lint format:prettier"
Expand Down Expand Up @@ -69,11 +69,13 @@
"jest": "^29.7.0",
"jest-config": "^29.7.0",
"lint-staged": "^15.2.2",
"poseidon-lite": "^0.2.0",
"prettier": "^3.2.5",
"rimraf": "^5.0.5",
"ts-jest": "^29.1.2",
"ts-node": "^10.9.2",
"tslib": "^2.6.2",
"tsx": "^4.7.2",
"turbo": "^1.13.2",
"typedoc": "^0.25.8",
"typescript": "^5.3.3"
Expand Down
99 changes: 50 additions & 49 deletions packages/poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,84 +16,84 @@
"default": "./dist/index.js"
},
"./1": {
"types": "./dist/types/poseidon-1/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-1/index.cjs",
"default": "./dist/lib.esm/poseidon-1/index.js"
"types": "./dist/types/1/index.d.ts",
"require": "./dist/lib.commonjs/1/index.cjs",
"default": "./dist/lib.esm/1/index.js"
},
"./2": {
"types": "./dist/types/poseidon-2/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-2/index.cjs",
"default": "./dist/lib.esm/poseidon-2/index.js"
"types": "./dist/types/2/index.d.ts",
"require": "./dist/lib.commonjs/2/index.cjs",
"default": "./dist/lib.esm/2/index.js"
},
"./3": {
"types": "./dist/types/poseidon-3/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-3/index.cjs",
"default": "./dist/lib.esm/poseidon-3/index.js"
"types": "./dist/types/3/index.d.ts",
"require": "./dist/lib.commonjs/3/index.cjs",
"default": "./dist/lib.esm/3/index.js"
},
"./4": {
"types": "./dist/types/poseidon-4/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-4/index.cjs",
"default": "./dist/lib.esm/poseidon-4/index.js"
"types": "./dist/types/4/index.d.ts",
"require": "./dist/lib.commonjs/4/index.cjs",
"default": "./dist/lib.esm/4/index.js"
},
"./5": {
"types": "./dist/types/poseidon-5/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-5/index.cjs",
"default": "./dist/lib.esm/poseidon-5/index.js"
"types": "./dist/types/5/index.d.ts",
"require": "./dist/lib.commonjs/5/index.cjs",
"default": "./dist/lib.esm/5/index.js"
},
"./6": {
"types": "./dist/types/poseidon-6/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-6/index.cjs",
"default": "./dist/lib.esm/poseidon-6/index.js"
"types": "./dist/types/6/index.d.ts",
"require": "./dist/lib.commonjs/6/index.cjs",
"default": "./dist/lib.esm/6/index.js"
},
"./7": {
"types": "./dist/types/poseidon-7/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-7/index.cjs",
"default": "./dist/lib.esm/poseidon-7/index.js"
"types": "./dist/types/7/index.d.ts",
"require": "./dist/lib.commonjs/7/index.cjs",
"default": "./dist/lib.esm/7/index.js"
},
"./8": {
"types": "./dist/types/poseidon-8/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-8/index.cjs",
"default": "./dist/lib.esm/poseidon-8/index.js"
"types": "./dist/types/8/index.d.ts",
"require": "./dist/lib.commonjs/8/index.cjs",
"default": "./dist/lib.esm/8/index.js"
},
"./9": {
"types": "./dist/types/poseidon-9/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-9/index.cjs",
"default": "./dist/lib.esm/poseidon-9/index.js"
"types": "./dist/types/9/index.d.ts",
"require": "./dist/lib.commonjs/9/index.cjs",
"default": "./dist/lib.esm/9/index.js"
},
"./10": {
"types": "./dist/types/poseidon-10/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-10/index.cjs",
"default": "./dist/lib.esm/poseidon-10/index.js"
"types": "./dist/types/10/index.d.ts",
"require": "./dist/lib.commonjs/10/index.cjs",
"default": "./dist/lib.esm/10/index.js"
},
"./11": {
"types": "./dist/types/poseidon-11/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-11/index.cjs",
"default": "./dist/lib.esm/poseidon-11/index.js"
"types": "./dist/types/11/index.d.ts",
"require": "./dist/lib.commonjs/11/index.cjs",
"default": "./dist/lib.esm/11/index.js"
},
"./12": {
"types": "./dist/types/poseidon-12/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-12/index.cjs",
"default": "./dist/lib.esm/poseidon-12/index.js"
"types": "./dist/types/12/index.d.ts",
"require": "./dist/lib.commonjs/12/index.cjs",
"default": "./dist/lib.esm/12/index.js"
},
"./13": {
"types": "./dist/types/poseidon-13/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-13/index.cjs",
"default": "./dist/lib.esm/poseidon-13/index.js"
"types": "./dist/types/13/index.d.ts",
"require": "./dist/lib.commonjs/13/index.cjs",
"default": "./dist/lib.esm/13/index.js"
},
"./14": {
"types": "./dist/types/poseidon-14/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-14/index.cjs",
"default": "./dist/lib.esm/poseidon-14/index.js"
"types": "./dist/types/14/index.d.ts",
"require": "./dist/lib.commonjs/14/index.cjs",
"default": "./dist/lib.esm/14/index.js"
},
"./15": {
"types": "./dist/types/poseidon-15/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-15/index.cjs",
"default": "./dist/lib.esm/poseidon-15/index.js"
"types": "./dist/types/15/index.d.ts",
"require": "./dist/lib.commonjs/15/index.cjs",
"default": "./dist/lib.esm/15/index.js"
},
"./16": {
"types": "./dist/types/poseidon-16/index.d.ts",
"require": "./dist/lib.commonjs/poseidon-16/index.cjs",
"default": "./dist/lib.esm/poseidon-16/index.js"
"types": "./dist/types/16/index.d.ts",
"require": "./dist/lib.commonjs/16/index.cjs",
"default": "./dist/lib.esm/16/index.js"
}
},
"files": [
Expand Down Expand Up @@ -121,7 +121,8 @@
"circomlibjs": "0.0.8",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-cleanup": "^3.2.1"
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-polyfill-node": "^0.13.0"
},
"dependencies": {
"@noble/curves": "1.4.0",
Expand Down
4 changes: 4 additions & 0 deletions packages/poseidon/rollup.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import terser from "@rollup/plugin-terser"
import typescript from "@rollup/plugin-typescript"
import fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import nodePolyfills from "rollup-plugin-polyfill-node"

const pkg = JSON.parse(fs.readFileSync("./package.json", "utf8"))
const banner = `/**
Expand All @@ -25,6 +26,7 @@ export default [
],
external: [
...Object.keys(pkg.dependencies),
"@zk-kit/utils/conversions",
"@noble/curves/abstract/modular",
"@noble/curves/abstract/poseidon"
],
Expand Down Expand Up @@ -52,6 +54,7 @@ export default [
tsconfig: "./build.tsconfig.json"
}),
nodeResolve({ preferBuiltins: false, browser: true }),
nodePolyfills(),
cleanup({ comments: "jsdoc" })
]
},
Expand All @@ -69,6 +72,7 @@ export default [
],
external: [
...Object.keys(pkg.dependencies),
"@zk-kit/utils/conversions",
"@noble/curves/abstract/modular",
"@noble/curves/abstract/poseidon"
],
Expand Down
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/1/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(1, getConstants(C, M))

/**
* Calculate the poseidon hash of 1 input.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/10/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(10, getConstants(C, M))

/**
* Calculate the poseidon hash of 10 inputs.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/11/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(11, getConstants(C, M))

/**
* Calculate the poseidon hash of 11 inputs.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/12/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(12, getConstants(C, M))

/**
* Calculate the poseidon hash of 12 inputs.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/13/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(13, getConstants(C, M))

/**
* Calculate the poseidon hash of 13 inputs.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/14/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(14, getConstants(C, M))

/**
* Calculate the poseidon hash of 14 inputs.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
17 changes: 17 additions & 0 deletions packages/poseidon/src/15/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import getConstants from "../getConstants"
import poseidonBasic from "../poseidon-basic"
import { C, M } from "./constants"

const instance = poseidonBasic(15, getConstants(C, M))

/**
* Calculate the poseidon hash of 15 inputs.
* The constants used in this implementation are encoded as base64 strings to save space,
* and are converted into bigints when this module is imported, to save time when hashes are calculated.
* @param inputs List of values to be hashed.
* @param cacheConstants Boolean to allow constants to be cached.
* @returns The Poseidon hash.
*/
export default function poseidon(inputs: (bigint | string | number)[]) {
return instance([0n, ...inputs.map(BigInt)])[0]
}
File renamed without changes.
Loading

0 comments on commit 5da772a

Please sign in to comment.