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

New Poseidon library #245

Open
wants to merge 12 commits into
base: main
Choose a base branch
from
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 @@ -21,9 +21,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 && git config --local core.editor cat",
"style": "turbo lint:eslint format:prettier"
Expand Down Expand Up @@ -60,11 +60,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
21 changes: 21 additions & 0 deletions packages/poseidon/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2024 Ethereum Foundation

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
103 changes: 103 additions & 0 deletions packages/poseidon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
<p align="center">
<h1 align="center">
Baby Jubjub
</h1>
<p align="center">A JavaScript Poseidon implementation over alt_bn128 (aka BN254).</p>
</p>

<p align="center">
<a href="https://github.com/privacy-scaling-explorations/zk-kit">
<img src="https://img.shields.io/badge/project-zk--kit-blue.svg?style=flat-square">
</a>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/poseidon/LICENSE">
<img alt="NPM license" src="https://img.shields.io/npm/l/%40zk-kit%2Fposeidon?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@zk-kit/poseidon">
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/poseidon?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@zk-kit/poseidon">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/poseidon.svg?style=flat-square" />
</a>
<a href="https://bundlephobia.com/package/@zk-kit/poseidon">
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/poseidon" />
</a>
<a href="https://eslint.org/">
<img alt="Linter eslint" src="https://img.shields.io/badge/linter-eslint-8080f2?style=flat-square&logo=eslint" />
</a>
<a href="https://prettier.io/">
<img alt="Code style prettier" src="https://img.shields.io/badge/code%20style-prettier-f8bc45?style=flat-square&logo=prettier" />
</a>
</p>

<div align="center">
<h4>
<a href="https://appliedzkp.org/discord">
🗣️ Chat &amp; Support
</a>
<span>&nbsp;&nbsp;|&nbsp;&nbsp;</span>
<a href="https://zkkit.pse.dev/modules/_zk_kit_poseidon.html">
📘 Docs
</a>
</h4>
</div>

Poseidon is a ZK-friendly cryptographic hash function designed for efficiency and security. The constants and the other parameters are the same as those used in the Poseidon implementation of [`circomlibjs`](https://github.com/iden3/circomlibjs).

> [!NOTE]
> This library depends on the [`@noble/curves`](https://github.com/paulmillr/noble-curves)'s audited Poseidon library.

## References

1. Poseidon Website: https://www.poseidon-hash.info.
2. Lorenzo Grassi1, Dmitry Khovratovich2, Christian Rechberger3, Arnab Roy4, and Markus Schofnegger. OSEIDON: A New Hash Function for Zero-Knowledge Proof Systems (Updated Version). https://eprint.iacr.org/2019/458.pdf.
3. Barry WhiteHat, Marta Bellés, Jordi Baylina. _ERC-2494: Baby Jubjub Elliptic Curve_. 2020-01-29. https://eips.ethereum.org/EIPS/eip-2494.

---

## 🛠 Install

### npm or yarn

Install the `@zk-kit/poseidon` package and its peer dependencies with npm:

```bash
npm i @zk-kit/poseidon
```

or yarn:

```bash
yarn add @zk-kit/poseidon
```

### CDN

You can also load it using a `script` tag using [unpkg](https://unpkg.com/):

```html
<script src="https://unpkg.com/@zk-kit/poseidon"></script>
```

or [JSDelivr](https://www.jsdelivr.com/):

```html
<script src="https://cdn.jsdelivr.net/npm/@zk-kit/poseidon"></script>
```

## 📜 Usage

```typescript
import { poseidon, poseidon1, poseidon2 } from "@zk-kit/poseidon"

poseidon([1n, 2n])
// Or:
poseidon2([1n, 2n])
```

This library also supports conditional exports:

```typescript
import poseidon2 from "@zk-kit/poseidon/2"

poseidon2([1n, 2n])
```
8 changes: 8 additions & 0 deletions packages/poseidon/build.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"declarationDir": "dist/types"
},
"include": ["src"]
}
131 changes: 131 additions & 0 deletions packages/poseidon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
{
"name": "@zk-kit/poseidon",
"version": "1.0.0-alpha",
"description": "A Poseidon implementation over alt_bn128 (aka BN254).",
"type": "module",
"license": "MIT",
"iife": "dist/index.iife.js",
"unpkg": "dist/index.iife.min.js",
"jsdelivr": "dist/index.iife.min.js",
"main": "dist/index.js",
"types": "dist/types/index.d.ts",
"exports": {
".": {
"types": "./dist/types/index.d.ts",
"require": "./dist/index.cjs",
"default": "./dist/index.js"
},
"./1": {
"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/2/index.d.ts",
"require": "./dist/lib.commonjs/2/index.cjs",
"default": "./dist/lib.esm/2/index.js"
},
"./3": {
"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/4/index.d.ts",
"require": "./dist/lib.commonjs/4/index.cjs",
"default": "./dist/lib.esm/4/index.js"
},
"./5": {
"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/6/index.d.ts",
"require": "./dist/lib.commonjs/6/index.cjs",
"default": "./dist/lib.esm/6/index.js"
},
"./7": {
"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/8/index.d.ts",
"require": "./dist/lib.commonjs/8/index.cjs",
"default": "./dist/lib.esm/8/index.js"
},
"./9": {
"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/10/index.d.ts",
"require": "./dist/lib.commonjs/10/index.cjs",
"default": "./dist/lib.esm/10/index.js"
},
"./11": {
"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/12/index.d.ts",
"require": "./dist/lib.commonjs/12/index.cjs",
"default": "./dist/lib.esm/12/index.js"
},
"./13": {
"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/14/index.d.ts",
"require": "./dist/lib.commonjs/14/index.cjs",
"default": "./dist/lib.esm/14/index.js"
},
"./15": {
"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/16/index.d.ts",
"require": "./dist/lib.commonjs/16/index.cjs",
"default": "./dist/lib.esm/16/index.js"
}
},
"files": [
"dist/",
"src/",
"LICENSE",
"README.md"
],
"repository": "[email protected]:privacy-scaling-explorations/zk-kit.git",
"homepage": "https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/poseidon",
"bugs": {
"url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues"
},
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@rollup/plugin-node-resolve": "^15.2.3",
"@rollup/plugin-terser": "^0.4.4",
"@rollup/plugin-typescript": "^11.1.6",
"circomlibjs": "0.0.8",
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-polyfill-node": "^0.13.0"
},
"dependencies": {
"@noble/curves": "1.4.0",
"@zk-kit/utils": "1.2.0"
}
}
Loading
Loading