diff --git a/benchmarks/imt.ts b/benchmarks/imt.ts index c4ad1cb40..0d5528179 100644 --- a/benchmarks/imt.ts +++ b/benchmarks/imt.ts @@ -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" diff --git a/benchmarks/index.ts b/benchmarks/index.ts index 425a294dd..fbe825681 100644 --- a/benchmarks/index.ts +++ b/benchmarks/index.ts @@ -1,4 +1,5 @@ import runIMT from "./imt" +import runPoseidon from "./poseidon" const [benchmark] = process.argv.slice(2) @@ -8,6 +9,9 @@ switch (benchmark) { case "imt": runIMT() break + case "poseidon": + runPoseidon() + break default: runIMT() } diff --git a/benchmarks/poseidon.ts b/benchmarks/poseidon.ts new file mode 100644 index 000000000..1a0b0131f --- /dev/null +++ b/benchmarks/poseidon.ts @@ -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 }) + ) +} diff --git a/package.json b/package.json index b63c399af..be5f7c5dd 100644 --- a/package.json +++ b/package.json @@ -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" @@ -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" diff --git a/packages/poseidon/package.json b/packages/poseidon/package.json index 3c0135f98..ae862e67c 100644 --- a/packages/poseidon/package.json +++ b/packages/poseidon/package.json @@ -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": [ @@ -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", diff --git a/packages/poseidon/rollup.config.ts b/packages/poseidon/rollup.config.ts index 16eb05269..0ce5db3f0 100644 --- a/packages/poseidon/rollup.config.ts +++ b/packages/poseidon/rollup.config.ts @@ -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 = `/** @@ -25,6 +26,7 @@ export default [ ], external: [ ...Object.keys(pkg.dependencies), + "@zk-kit/utils/conversions", "@noble/curves/abstract/modular", "@noble/curves/abstract/poseidon" ], @@ -52,6 +54,7 @@ export default [ tsconfig: "./build.tsconfig.json" }), nodeResolve({ preferBuiltins: false, browser: true }), + nodePolyfills(), cleanup({ comments: "jsdoc" }) ] }, @@ -69,6 +72,7 @@ export default [ ], external: [ ...Object.keys(pkg.dependencies), + "@zk-kit/utils/conversions", "@noble/curves/abstract/modular", "@noble/curves/abstract/poseidon" ], diff --git a/packages/poseidon/src/poseidon-1/constants.ts b/packages/poseidon/src/1/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-1/constants.ts rename to packages/poseidon/src/1/constants.ts diff --git a/packages/poseidon/src/1/index.ts b/packages/poseidon/src/1/index.ts new file mode 100644 index 000000000..f9b07a04d --- /dev/null +++ b/packages/poseidon/src/1/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-10/constants.ts b/packages/poseidon/src/10/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-10/constants.ts rename to packages/poseidon/src/10/constants.ts diff --git a/packages/poseidon/src/10/index.ts b/packages/poseidon/src/10/index.ts new file mode 100644 index 000000000..834c01b58 --- /dev/null +++ b/packages/poseidon/src/10/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-11/constants.ts b/packages/poseidon/src/11/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-11/constants.ts rename to packages/poseidon/src/11/constants.ts diff --git a/packages/poseidon/src/11/index.ts b/packages/poseidon/src/11/index.ts new file mode 100644 index 000000000..bec39077c --- /dev/null +++ b/packages/poseidon/src/11/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-12/constants.ts b/packages/poseidon/src/12/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-12/constants.ts rename to packages/poseidon/src/12/constants.ts diff --git a/packages/poseidon/src/12/index.ts b/packages/poseidon/src/12/index.ts new file mode 100644 index 000000000..59c6bbbc2 --- /dev/null +++ b/packages/poseidon/src/12/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-13/constants.ts b/packages/poseidon/src/13/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-13/constants.ts rename to packages/poseidon/src/13/constants.ts diff --git a/packages/poseidon/src/13/index.ts b/packages/poseidon/src/13/index.ts new file mode 100644 index 000000000..e53510f5c --- /dev/null +++ b/packages/poseidon/src/13/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-14/constants.ts b/packages/poseidon/src/14/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-14/constants.ts rename to packages/poseidon/src/14/constants.ts diff --git a/packages/poseidon/src/14/index.ts b/packages/poseidon/src/14/index.ts new file mode 100644 index 000000000..ca5a5ccba --- /dev/null +++ b/packages/poseidon/src/14/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-15/constants.ts b/packages/poseidon/src/15/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-15/constants.ts rename to packages/poseidon/src/15/constants.ts diff --git a/packages/poseidon/src/15/index.ts b/packages/poseidon/src/15/index.ts new file mode 100644 index 000000000..1326a36ab --- /dev/null +++ b/packages/poseidon/src/15/index.ts @@ -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] +} diff --git a/packages/poseidon/src/poseidon-16/constants.ts b/packages/poseidon/src/16/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-16/constants.ts rename to packages/poseidon/src/16/constants.ts diff --git a/packages/poseidon/src/16/index.ts b/packages/poseidon/src/16/index.ts new file mode 100644 index 000000000..33a98785b --- /dev/null +++ b/packages/poseidon/src/16/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(16, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 16 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] +} diff --git a/packages/poseidon/src/poseidon-2/constants.ts b/packages/poseidon/src/2/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-2/constants.ts rename to packages/poseidon/src/2/constants.ts diff --git a/packages/poseidon/src/2/index.ts b/packages/poseidon/src/2/index.ts new file mode 100644 index 000000000..2218fcc8b --- /dev/null +++ b/packages/poseidon/src/2/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(2, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 2 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] +} diff --git a/packages/poseidon/src/poseidon-3/constants.ts b/packages/poseidon/src/3/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-3/constants.ts rename to packages/poseidon/src/3/constants.ts diff --git a/packages/poseidon/src/3/index.ts b/packages/poseidon/src/3/index.ts new file mode 100644 index 000000000..3a96cca8d --- /dev/null +++ b/packages/poseidon/src/3/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(3, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 3 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] +} diff --git a/packages/poseidon/src/poseidon-4/constants.ts b/packages/poseidon/src/4/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-4/constants.ts rename to packages/poseidon/src/4/constants.ts diff --git a/packages/poseidon/src/4/index.ts b/packages/poseidon/src/4/index.ts new file mode 100644 index 000000000..1c0e3c284 --- /dev/null +++ b/packages/poseidon/src/4/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(4, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 4 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] +} diff --git a/packages/poseidon/src/poseidon-5/constants.ts b/packages/poseidon/src/5/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-5/constants.ts rename to packages/poseidon/src/5/constants.ts diff --git a/packages/poseidon/src/5/index.ts b/packages/poseidon/src/5/index.ts new file mode 100644 index 000000000..60c426a87 --- /dev/null +++ b/packages/poseidon/src/5/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(5, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 5 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] +} diff --git a/packages/poseidon/src/poseidon-6/constants.ts b/packages/poseidon/src/6/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-6/constants.ts rename to packages/poseidon/src/6/constants.ts diff --git a/packages/poseidon/src/6/index.ts b/packages/poseidon/src/6/index.ts new file mode 100644 index 000000000..57ab8abc0 --- /dev/null +++ b/packages/poseidon/src/6/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(6, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 6 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] +} diff --git a/packages/poseidon/src/poseidon-7/constants.ts b/packages/poseidon/src/7/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-7/constants.ts rename to packages/poseidon/src/7/constants.ts diff --git a/packages/poseidon/src/7/index.ts b/packages/poseidon/src/7/index.ts new file mode 100644 index 000000000..c4bd66677 --- /dev/null +++ b/packages/poseidon/src/7/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(7, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 7 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] +} diff --git a/packages/poseidon/src/poseidon-8/constants.ts b/packages/poseidon/src/8/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-8/constants.ts rename to packages/poseidon/src/8/constants.ts diff --git a/packages/poseidon/src/8/index.ts b/packages/poseidon/src/8/index.ts new file mode 100644 index 000000000..05f24ff39 --- /dev/null +++ b/packages/poseidon/src/8/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(8, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 8 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] +} diff --git a/packages/poseidon/src/poseidon-9/constants.ts b/packages/poseidon/src/9/constants.ts similarity index 100% rename from packages/poseidon/src/poseidon-9/constants.ts rename to packages/poseidon/src/9/constants.ts diff --git a/packages/poseidon/src/9/index.ts b/packages/poseidon/src/9/index.ts new file mode 100644 index 000000000..7ec0cb24b --- /dev/null +++ b/packages/poseidon/src/9/index.ts @@ -0,0 +1,17 @@ +import getConstants from "../getConstants" +import poseidonBasic from "../poseidon-basic" +import { C, M } from "./constants" + +const instance = poseidonBasic(9, getConstants(C, M)) + +/** + * Calculate the poseidon hash of 9 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] +} diff --git a/packages/poseidon/src/index.ts b/packages/poseidon/src/index.ts index 4cee4a150..3c306935a 100644 --- a/packages/poseidon/src/index.ts +++ b/packages/poseidon/src/index.ts @@ -1,19 +1,19 @@ -import poseidon1 from "./poseidon-1" -import poseidon10 from "./poseidon-10" -import poseidon11 from "./poseidon-11" -import poseidon12 from "./poseidon-12" -import poseidon13 from "./poseidon-13" -import poseidon14 from "./poseidon-14" -import poseidon15 from "./poseidon-15" -import poseidon16 from "./poseidon-16" -import poseidon2 from "./poseidon-2" -import poseidon3 from "./poseidon-3" -import poseidon4 from "./poseidon-4" -import poseidon5 from "./poseidon-5" -import poseidon6 from "./poseidon-6" -import poseidon7 from "./poseidon-7" -import poseidon8 from "./poseidon-8" -import poseidon9 from "./poseidon-9" +import poseidon1 from "./1" +import poseidon10 from "./10" +import poseidon11 from "./11" +import poseidon12 from "./12" +import poseidon13 from "./13" +import poseidon14 from "./14" +import poseidon15 from "./15" +import poseidon16 from "./16" +import poseidon2 from "./2" +import poseidon3 from "./3" +import poseidon4 from "./4" +import poseidon5 from "./5" +import poseidon6 from "./6" +import poseidon7 from "./7" +import poseidon8 from "./8" +import poseidon9 from "./9" import poseidon from "./poseidon" diff --git a/packages/poseidon/src/poseidon-1/index.ts b/packages/poseidon/src/poseidon-1/index.ts deleted file mode 100644 index b86a4770f..000000000 --- a/packages/poseidon/src/poseidon-1/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon1(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(1, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-10/index.ts b/packages/poseidon/src/poseidon-10/index.ts deleted file mode 100644 index 7b58b0ac9..000000000 --- a/packages/poseidon/src/poseidon-10/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon10(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(10, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-11/index.ts b/packages/poseidon/src/poseidon-11/index.ts deleted file mode 100644 index c4d5ac1a1..000000000 --- a/packages/poseidon/src/poseidon-11/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon11(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(11, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-12/index.ts b/packages/poseidon/src/poseidon-12/index.ts deleted file mode 100644 index 47c606b50..000000000 --- a/packages/poseidon/src/poseidon-12/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon12(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(12, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-13/index.ts b/packages/poseidon/src/poseidon-13/index.ts deleted file mode 100644 index 94bee05b5..000000000 --- a/packages/poseidon/src/poseidon-13/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon13(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(13, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-14/index.ts b/packages/poseidon/src/poseidon-14/index.ts deleted file mode 100644 index 359348404..000000000 --- a/packages/poseidon/src/poseidon-14/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon14(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(14, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-15/index.ts b/packages/poseidon/src/poseidon-15/index.ts deleted file mode 100644 index d2d5a2a82..000000000 --- a/packages/poseidon/src/poseidon-15/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * 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 every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon15(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(15, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-16/index.ts b/packages/poseidon/src/poseidon-16/index.ts deleted file mode 100644 index 0b903b041..000000000 --- a/packages/poseidon/src/poseidon-16/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 16 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon16(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(16, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-2/index.ts b/packages/poseidon/src/poseidon-2/index.ts deleted file mode 100644 index b41acf4eb..000000000 --- a/packages/poseidon/src/poseidon-2/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 2 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon2(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(2, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-3/index.ts b/packages/poseidon/src/poseidon-3/index.ts deleted file mode 100644 index dd50a86db..000000000 --- a/packages/poseidon/src/poseidon-3/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 3 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon3(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(3, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-4/index.ts b/packages/poseidon/src/poseidon-4/index.ts deleted file mode 100644 index c6f321b38..000000000 --- a/packages/poseidon/src/poseidon-4/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 4 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon4(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(4, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-5/index.ts b/packages/poseidon/src/poseidon-5/index.ts deleted file mode 100644 index 298a62f24..000000000 --- a/packages/poseidon/src/poseidon-5/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 5 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon5(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(5, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-6/index.ts b/packages/poseidon/src/poseidon-6/index.ts deleted file mode 100644 index e6917cb11..000000000 --- a/packages/poseidon/src/poseidon-6/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 6 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon6(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(6, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-7/index.ts b/packages/poseidon/src/poseidon-7/index.ts deleted file mode 100644 index 0c3630b97..000000000 --- a/packages/poseidon/src/poseidon-7/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 7 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon7(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(7, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-8/index.ts b/packages/poseidon/src/poseidon-8/index.ts deleted file mode 100644 index feb9483ac..000000000 --- a/packages/poseidon/src/poseidon-8/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 8 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon8(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(8, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon-9/index.ts b/packages/poseidon/src/poseidon-9/index.ts deleted file mode 100644 index c088d49e7..000000000 --- a/packages/poseidon/src/poseidon-9/index.ts +++ /dev/null @@ -1,24 +0,0 @@ -import getConstants from "../getConstants" -import poseidonBasic from "../poseidon-basic" -import { Constants } from "../types" -import { C, M } from "./constants" - -let cachedConstants: Constants - -/** - * Calculate the poseidon hash of 9 inputs. - * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. - * @param inputs List of values to be hashed. - * @param cacheConstants Boolean to allow constants to be cached. - * @returns The Poseidon hash. - */ -export default function poseidon9(inputs: bigint[], cacheConstants = false) { - if (cacheConstants && !cachedConstants) { - cachedConstants = getConstants(C, M) - } - - return poseidonBasic(9, cachedConstants ?? getConstants(C, M))([0n, ...inputs])[0] -} diff --git a/packages/poseidon/src/poseidon.ts b/packages/poseidon/src/poseidon.ts index 8221c498a..0601e184a 100644 --- a/packages/poseidon/src/poseidon.ts +++ b/packages/poseidon/src/poseidon.ts @@ -1,64 +1,62 @@ -import poseidon1 from "./poseidon-1" -import poseidon10 from "./poseidon-10" -import poseidon11 from "./poseidon-11" -import poseidon12 from "./poseidon-12" -import poseidon13 from "./poseidon-13" -import poseidon14 from "./poseidon-14" -import poseidon15 from "./poseidon-15" -import poseidon16 from "./poseidon-16" -import poseidon2 from "./poseidon-2" -import poseidon3 from "./poseidon-3" -import poseidon4 from "./poseidon-4" -import poseidon5 from "./poseidon-5" -import poseidon6 from "./poseidon-6" -import poseidon7 from "./poseidon-7" -import poseidon8 from "./poseidon-8" -import poseidon9 from "./poseidon-9" +import poseidon1 from "./1" +import poseidon10 from "./10" +import poseidon11 from "./11" +import poseidon12 from "./12" +import poseidon13 from "./13" +import poseidon14 from "./14" +import poseidon15 from "./15" +import poseidon16 from "./16" +import poseidon2 from "./2" +import poseidon3 from "./3" +import poseidon4 from "./4" +import poseidon5 from "./5" +import poseidon6 from "./6" +import poseidon7 from "./7" +import poseidon8 from "./8" +import poseidon9 from "./9" /** * Calculate the poseidon hash of N inputs, where 0 < N < 17. * The constants used in this implementation are encoded as base64 strings to save space, - * and are converted into bigints every time a hash is calculated. When this function is called - * several times, it may be useful to cache the constants converted to bigints by passing true - * as a second parameter. + * 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[], cacheConstants = false) { +export default function poseidon(inputs: bigint[]) { switch (inputs.length) { case 1: - return poseidon1(inputs, cacheConstants) + return poseidon1(inputs) case 2: - return poseidon2(inputs, cacheConstants) + return poseidon2(inputs) case 3: - return poseidon3(inputs, cacheConstants) + return poseidon3(inputs) case 4: - return poseidon4(inputs, cacheConstants) + return poseidon4(inputs) case 5: - return poseidon5(inputs, cacheConstants) + return poseidon5(inputs) case 6: - return poseidon6(inputs, cacheConstants) + return poseidon6(inputs) case 7: - return poseidon7(inputs, cacheConstants) + return poseidon7(inputs) case 8: - return poseidon8(inputs, cacheConstants) + return poseidon8(inputs) case 9: - return poseidon9(inputs, cacheConstants) + return poseidon9(inputs) case 10: - return poseidon10(inputs, cacheConstants) + return poseidon10(inputs) case 11: - return poseidon11(inputs, cacheConstants) + return poseidon11(inputs) case 12: - return poseidon12(inputs, cacheConstants) + return poseidon12(inputs) case 13: - return poseidon13(inputs, cacheConstants) + return poseidon13(inputs) case 14: - return poseidon14(inputs, cacheConstants) + return poseidon14(inputs) case 15: - return poseidon15(inputs, cacheConstants) + return poseidon15(inputs) case 16: - return poseidon16(inputs, cacheConstants) + return poseidon16(inputs) default: throw new Error(`Input length '${inputs.length}' is not supported`) } diff --git a/packages/poseidon/tests/index.test.ts b/packages/poseidon/tests/index.test.ts index bd73b6047..4cd06fc8a 100644 --- a/packages/poseidon/tests/index.test.ts +++ b/packages/poseidon/tests/index.test.ts @@ -1,6 +1,5 @@ import { poseidon as expectedPoseidon } from "circomlibjs" import { poseidon } from "../src" -import * as p from "../src" describe("Poseidon", () => { for (let i = 1; i < 17; i += 1) { @@ -13,17 +12,6 @@ describe("Poseidon", () => { expect(result).toBe(expectedResult) }) - - it(`Should cache the constants (poseidon1)`, () => { - const inputs = Array.from({ length: i }, (_, j) => BigInt(j) + 1n) - - const expectedResult = expectedPoseidon(inputs) - - ;(p as any)[`poseidon${i}`](inputs, true) - const result = (p as any)[`poseidon${i}`](inputs) - - expect(result).toBe(expectedResult) - }) } it(`Should throw an error if the number of inputs is not supported`, () => { diff --git a/yarn.lock b/yarn.lock index f7de71ce5..1306704ce 100644 --- a/yarn.lock +++ b/yarn.lock @@ -781,6 +781,167 @@ __metadata: languageName: node linkType: hard +"@esbuild/aix-ppc64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/aix-ppc64@npm:0.19.12" + conditions: os=aix & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/android-arm64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/android-arm64@npm:0.19.12" + conditions: os=android & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/android-arm@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/android-arm@npm:0.19.12" + conditions: os=android & cpu=arm + languageName: node + linkType: hard + +"@esbuild/android-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/android-x64@npm:0.19.12" + conditions: os=android & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/darwin-arm64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/darwin-arm64@npm:0.19.12" + conditions: os=darwin & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/darwin-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/darwin-x64@npm:0.19.12" + conditions: os=darwin & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/freebsd-arm64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/freebsd-arm64@npm:0.19.12" + conditions: os=freebsd & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/freebsd-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/freebsd-x64@npm:0.19.12" + conditions: os=freebsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/linux-arm64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-arm64@npm:0.19.12" + conditions: os=linux & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/linux-arm@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-arm@npm:0.19.12" + conditions: os=linux & cpu=arm + languageName: node + linkType: hard + +"@esbuild/linux-ia32@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-ia32@npm:0.19.12" + conditions: os=linux & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/linux-loong64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-loong64@npm:0.19.12" + conditions: os=linux & cpu=loong64 + languageName: node + linkType: hard + +"@esbuild/linux-mips64el@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-mips64el@npm:0.19.12" + conditions: os=linux & cpu=mips64el + languageName: node + linkType: hard + +"@esbuild/linux-ppc64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-ppc64@npm:0.19.12" + conditions: os=linux & cpu=ppc64 + languageName: node + linkType: hard + +"@esbuild/linux-riscv64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-riscv64@npm:0.19.12" + conditions: os=linux & cpu=riscv64 + languageName: node + linkType: hard + +"@esbuild/linux-s390x@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-s390x@npm:0.19.12" + conditions: os=linux & cpu=s390x + languageName: node + linkType: hard + +"@esbuild/linux-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/linux-x64@npm:0.19.12" + conditions: os=linux & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/netbsd-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/netbsd-x64@npm:0.19.12" + conditions: os=netbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/openbsd-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/openbsd-x64@npm:0.19.12" + conditions: os=openbsd & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/sunos-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/sunos-x64@npm:0.19.12" + conditions: os=sunos & cpu=x64 + languageName: node + linkType: hard + +"@esbuild/win32-arm64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/win32-arm64@npm:0.19.12" + conditions: os=win32 & cpu=arm64 + languageName: node + linkType: hard + +"@esbuild/win32-ia32@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/win32-ia32@npm:0.19.12" + conditions: os=win32 & cpu=ia32 + languageName: node + linkType: hard + +"@esbuild/win32-x64@npm:0.19.12": + version: 0.19.12 + resolution: "@esbuild/win32-x64@npm:0.19.12" + conditions: os=win32 & cpu=x64 + languageName: node + linkType: hard + "@eslint-community/eslint-utils@npm:^4.2.0, @eslint-community/eslint-utils@npm:^4.4.0": version: 4.4.0 resolution: "@eslint-community/eslint-utils@npm:4.4.0" @@ -3604,13 +3765,13 @@ __metadata: "@types/mocha": "npm:^10.0.6" "@zk-kit/baby-jubjub": "npm:1.0.0-beta" "@zk-kit/eddsa-poseidon": "npm:0.11.0" + "@zk-kit/poseidon": "npm:1.0.0-alpha" "@zk-kit/poseidon-cipher": "npm:0.3.0" "@zk-kit/smt": "npm:1.0.0" "@zk-kit/utils": "npm:1.0.0-beta" circomkit: "npm:0.0.24" circomlib: "npm:^2.0.5" mocha: "npm:^10.2.0" - poseidon-lite: "npm:^0.2.0" languageName: unknown linkType: soft @@ -3632,11 +3793,11 @@ __metadata: "@rollup/plugin-terser": "npm:^0.4.4" "@rollup/plugin-typescript": "npm:^11.1.6" "@zk-kit/baby-jubjub": "npm:1.0.0-beta" + "@zk-kit/poseidon": "npm:1.0.0-alpha" "@zk-kit/utils": "npm:1.0.0-beta" buffer: "npm:6.0.3" circomlibjs: "npm:0.0.8" ffjavascript: "npm:0.2.38" - poseidon-lite: "npm:0.2.0" rimraf: "npm:^5.0.5" rollup: "npm:^4.12.0" rollup-plugin-cleanup: "npm:^3.2.1" @@ -3658,8 +3819,8 @@ __metadata: "@types/snarkjs": "npm:^0" "@types/tmp": "npm:^0.2.6" "@zk-kit/eddsa-poseidon": "npm:0.11.0" + "@zk-kit/poseidon": "npm:1.0.0-alpha" "@zk-kit/utils": "npm:1.0.0-beta" - poseidon-lite: "npm:^0.2.0" rimraf: "npm:^5.0.5" rollup: "npm:^4.12.0" rollup-plugin-cleanup: "npm:^3.2.1" @@ -3699,8 +3860,8 @@ __metadata: dependencies: "@rollup/plugin-terser": "npm:^0.4.4" "@rollup/plugin-typescript": "npm:^11.1.6" + "@zk-kit/poseidon": "npm:1.0.0-alpha" incrementalquintree: "npm:^1.0.9" - poseidon-lite: "npm:^0.2.0" rimraf: "npm:^5.0.5" rollup: "npm:^4.12.0" rollup-plugin-cleanup: "npm:^3.2.1" @@ -3711,9 +3872,9 @@ __metadata: version: 0.0.0-use.local resolution: "@zk-kit/lazytower.circom@workspace:packages/lazytower.circom" dependencies: + "@zk-kit/poseidon": "npm:1.0.0-alpha" circom_tester: "npm:^0.0.19" circomlib: "npm:^2.0.5" - poseidon-lite: "npm:^0.2.0" tmp-promise: "npm:^3.0.3" languageName: unknown linkType: soft @@ -3732,7 +3893,7 @@ __metadata: dependencies: "@rollup/plugin-terser": "npm:^0.4.4" "@rollup/plugin-typescript": "npm:^11.1.6" - poseidon-lite: "npm:^0.2.0" + "@zk-kit/poseidon": "npm:1.0.0-alpha" rimraf: "npm:^5.0.5" rollup: "npm:^4.12.0" rollup-plugin-cleanup: "npm:^3.2.1" @@ -3767,7 +3928,7 @@ __metadata: "@types/download": "npm:^8.0.5" "@types/snarkjs": "npm:^0" "@types/tmp": "npm:^0.2.6" - poseidon-lite: "npm:^0.2.0" + "@zk-kit/poseidon": "npm:1.0.0-alpha" rimraf: "npm:^5.0.5" rollup: "npm:^4.12.0" rollup-plugin-cleanup: "npm:^3.2.1" @@ -3775,7 +3936,7 @@ __metadata: languageName: unknown linkType: soft -"@zk-kit/poseidon@workspace:packages/poseidon": +"@zk-kit/poseidon@npm:1.0.0-alpha, @zk-kit/poseidon@workspace:packages/poseidon": version: 0.0.0-use.local resolution: "@zk-kit/poseidon@workspace:packages/poseidon" dependencies: @@ -3788,6 +3949,7 @@ __metadata: rimraf: "npm:^5.0.5" rollup: "npm:^4.12.0" rollup-plugin-cleanup: "npm:^3.2.1" + rollup-plugin-polyfill-node: "npm:^0.13.0" languageName: unknown linkType: soft @@ -7395,6 +7557,86 @@ __metadata: languageName: node linkType: hard +"esbuild@npm:~0.19.10": + version: 0.19.12 + resolution: "esbuild@npm:0.19.12" + dependencies: + "@esbuild/aix-ppc64": "npm:0.19.12" + "@esbuild/android-arm": "npm:0.19.12" + "@esbuild/android-arm64": "npm:0.19.12" + "@esbuild/android-x64": "npm:0.19.12" + "@esbuild/darwin-arm64": "npm:0.19.12" + "@esbuild/darwin-x64": "npm:0.19.12" + "@esbuild/freebsd-arm64": "npm:0.19.12" + "@esbuild/freebsd-x64": "npm:0.19.12" + "@esbuild/linux-arm": "npm:0.19.12" + "@esbuild/linux-arm64": "npm:0.19.12" + "@esbuild/linux-ia32": "npm:0.19.12" + "@esbuild/linux-loong64": "npm:0.19.12" + "@esbuild/linux-mips64el": "npm:0.19.12" + "@esbuild/linux-ppc64": "npm:0.19.12" + "@esbuild/linux-riscv64": "npm:0.19.12" + "@esbuild/linux-s390x": "npm:0.19.12" + "@esbuild/linux-x64": "npm:0.19.12" + "@esbuild/netbsd-x64": "npm:0.19.12" + "@esbuild/openbsd-x64": "npm:0.19.12" + "@esbuild/sunos-x64": "npm:0.19.12" + "@esbuild/win32-arm64": "npm:0.19.12" + "@esbuild/win32-ia32": "npm:0.19.12" + "@esbuild/win32-x64": "npm:0.19.12" + dependenciesMeta: + "@esbuild/aix-ppc64": + optional: true + "@esbuild/android-arm": + optional: true + "@esbuild/android-arm64": + optional: true + "@esbuild/android-x64": + optional: true + "@esbuild/darwin-arm64": + optional: true + "@esbuild/darwin-x64": + optional: true + "@esbuild/freebsd-arm64": + optional: true + "@esbuild/freebsd-x64": + optional: true + "@esbuild/linux-arm": + optional: true + "@esbuild/linux-arm64": + optional: true + "@esbuild/linux-ia32": + optional: true + "@esbuild/linux-loong64": + optional: true + "@esbuild/linux-mips64el": + optional: true + "@esbuild/linux-ppc64": + optional: true + "@esbuild/linux-riscv64": + optional: true + "@esbuild/linux-s390x": + optional: true + "@esbuild/linux-x64": + optional: true + "@esbuild/netbsd-x64": + optional: true + "@esbuild/openbsd-x64": + optional: true + "@esbuild/sunos-x64": + optional: true + "@esbuild/win32-arm64": + optional: true + "@esbuild/win32-ia32": + optional: true + "@esbuild/win32-x64": + optional: true + bin: + esbuild: bin/esbuild + checksum: 10/861fa8eb2428e8d6521a4b7c7930139e3f45e8d51a86985cc29408172a41f6b18df7b3401e7e5e2d528cdf83742da601ddfdc77043ddc4f1c715a8ddb2d8a255 + languageName: node + linkType: hard + "escalade@npm:^3.1.1": version: 3.1.2 resolution: "escalade@npm:3.1.2" @@ -8964,7 +9206,7 @@ __metadata: languageName: node linkType: hard -"fsevents@npm:^2.1.2, fsevents@npm:^2.3.2, fsevents@npm:~2.3.1, fsevents@npm:~2.3.2": +"fsevents@npm:^2.1.2, fsevents@npm:^2.3.2, fsevents@npm:~2.3.1, fsevents@npm:~2.3.2, fsevents@npm:~2.3.3": version: 2.3.3 resolution: "fsevents@npm:2.3.3" dependencies: @@ -8974,7 +9216,7 @@ __metadata: languageName: node linkType: hard -"fsevents@patch:fsevents@npm%3A^2.1.2#optional!builtin, fsevents@patch:fsevents@npm%3A^2.3.2#optional!builtin, fsevents@patch:fsevents@npm%3A~2.3.1#optional!builtin, fsevents@patch:fsevents@npm%3A~2.3.2#optional!builtin": +"fsevents@patch:fsevents@npm%3A^2.1.2#optional!builtin, fsevents@patch:fsevents@npm%3A^2.3.2#optional!builtin, fsevents@patch:fsevents@npm%3A~2.3.1#optional!builtin, fsevents@patch:fsevents@npm%3A~2.3.2#optional!builtin, fsevents@patch:fsevents@npm%3A~2.3.3#optional!builtin": version: 2.3.3 resolution: "fsevents@patch:fsevents@npm%3A2.3.3#optional!builtin::version=2.3.3&hash=df0bf1" dependencies: @@ -9107,6 +9349,15 @@ __metadata: languageName: node linkType: hard +"get-tsconfig@npm:^4.7.2": + version: 4.7.3 + resolution: "get-tsconfig@npm:4.7.3" + dependencies: + resolve-pkg-maps: "npm:^1.0.0" + checksum: 10/7397bb4f8aef936df4d9016555b662dcf5279f3c46428b7c7c1ff5e94ab2b87d018b3dda0f4bc1a28b154d5affd0eac5d014511172c085fd8a9cdff9ea7fe043 + languageName: node + linkType: hard + "get-value@npm:^2.0.3, get-value@npm:^2.0.6": version: 2.0.6 resolution: "get-value@npm:2.0.6" @@ -10153,11 +10404,11 @@ __metadata: "@types/mocha": "npm:^10.0.6" "@types/node": "npm:^20.10.7" "@zk-kit/imt": "workspace:packages/imt" + "@zk-kit/poseidon": "npm:1.0.0-alpha" chai: "npm:^4.2.0" ethers: "npm:^6.4.0" hardhat: "npm:^2.19.4" hardhat-gas-reporter: "npm:^1.0.8" - poseidon-lite: "npm:^0.2.0" poseidon-solidity: "npm:0.0.5" prettier-plugin-solidity: "npm:^1.3.1" solhint: "npm:^3.3.6" @@ -12318,11 +12569,11 @@ __metadata: "@types/chai": "npm:^4.2.0" "@types/mocha": "npm:^10.0.6" "@types/node": "npm:^20.10.7" + "@zk-kit/poseidon": "npm:1.0.0-alpha" chai: "npm:^4.2.0" ethers: "npm:^6.4.0" hardhat: "npm:^2.19.4" hardhat-gas-reporter: "npm:^1.0.8" - poseidon-lite: "npm:^0.2.0" poseidon-solidity: "npm:0.0.5" prettier-plugin-solidity: "npm:^1.3.1" solhint: "npm:^3.3.6" @@ -14395,7 +14646,7 @@ __metadata: languageName: node linkType: hard -"poseidon-lite@npm:0.2.0, poseidon-lite@npm:^0.2.0": +"poseidon-lite@npm:^0.2.0": version: 0.2.0 resolution: "poseidon-lite@npm:0.2.0" checksum: 10/63c7668b480ee3d57aaca0eda7e56d563ab2bfcc40bbce0e4bccdc9deed4c0d68255749356e328b622ce8715b1f1ba689fe1a86ca78eb1056a51a18daa252ee1 @@ -15070,6 +15321,13 @@ __metadata: languageName: node linkType: hard +"resolve-pkg-maps@npm:^1.0.0": + version: 1.0.0 + resolution: "resolve-pkg-maps@npm:1.0.0" + checksum: 10/0763150adf303040c304009231314d1e84c6e5ebfa2d82b7d94e96a6e82bacd1dcc0b58ae257315f3c8adb89a91d8d0f12928241cba2df1680fbe6f60bf99b0e + languageName: node + linkType: hard + "resolve-url@npm:^0.2.1": version: 0.2.1 resolution: "resolve-url@npm:0.2.1" @@ -17175,6 +17433,22 @@ __metadata: languageName: node linkType: hard +"tsx@npm:^4.7.2": + version: 4.7.2 + resolution: "tsx@npm:4.7.2" + dependencies: + esbuild: "npm:~0.19.10" + fsevents: "npm:~2.3.3" + get-tsconfig: "npm:^4.7.2" + dependenciesMeta: + fsevents: + optional: true + bin: + tsx: dist/cli.mjs + checksum: 10/3a06564f6926cc26bcdbf2752c361408d8e594a30abf15dc4639d4b964e98eedd3358c34ccd56ea15021519ebafe27a003d6cf26de0f530ea2cc34cb63fd0bfb + languageName: node + linkType: hard + "tunnel-agent@npm:^0.6.0": version: 0.6.0 resolution: "tunnel-agent@npm:0.6.0" @@ -18865,11 +19139,13 @@ __metadata: jest: "npm:^29.7.0" jest-config: "npm:^29.7.0" lint-staged: "npm:^15.2.2" + poseidon-lite: "npm:^0.2.0" prettier: "npm:^3.2.5" rimraf: "npm:^5.0.5" ts-jest: "npm:^29.1.2" ts-node: "npm:^10.9.2" tslib: "npm:^2.6.2" + tsx: "npm:^4.7.2" turbo: "npm:^1.13.2" typedoc: "npm:^0.25.8" typescript: "npm:^5.3.3"