Skip to content

Commit

Permalink
Merge pull request #88 from privacy-scaling-explorations/feat/eddsa-p…
Browse files Browse the repository at this point in the history
…oseidon

JS EdDSA Poseidon implementation
  • Loading branch information
cedoor authored Dec 4, 2023
2 parents a599569 + 23314a7 commit 6df4769
Show file tree
Hide file tree
Showing 22 changed files with 1,055 additions and 23 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/production.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,10 @@ jobs:
run: yarn

- name: Compile contracts
run: yarn compile:sol
run: yarn compile:contracts

- name: Build libraries
run: yarn build:js
run: yarn build:libraries

- name: Run Prettier
run: yarn prettier
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/pull-requests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ jobs:
run: yarn

- name: Compile contracts
run: yarn compile:sol
run: yarn compile:contracts

- name: Build libraries
run: yarn build:js
run: yarn build:libraries

- name: Run Prettier
run: yarn prettier
Expand Down
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,34 @@
</td>
<td></td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon">
@zk-kit/eddsa-poseidon
</a>
<a href="https://zkkit.pse.dev/modules/_zk_kit_eddsa_poseidon.html">
(docs)
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://npmjs.org/package/@zk-kit/eddsa-poseidon">
<img src="https://img.shields.io/npm/v/@zk-kit/eddsa-poseidon.svg?style=flat-square" alt="NPM version" />
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@zk-kit/eddsa-poseidon">
<img src="https://img.shields.io/npm/dm/@zk-kit/eddsa-poseidon.svg?style=flat-square" alt="Downloads" />
</a>
</td>
<td>
<!-- Size -->
<a href="https://bundlephobia.com/package/@zk-kit/eddsa-poseidon">
<img src="https://img.shields.io/bundlephobia/minzip/@zk-kit/eddsa-poseidon" alt="npm bundle size (scoped)" />
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/imt">
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
"bugs": "https://github.com/privacy-scaling-explorations/zk-kit/issues",
"private": true,
"scripts": {
"build": "yarn build:js && yarn compile:sol",
"build:js": "yarn workspaces foreach --no-private run build",
"compile:sol": "yarn workspaces foreach run compile",
"build": "yarn build:libraries && yarn compile:contracts",
"build:libraries": "yarn workspaces foreach --no-private run build",
"compile:contracts": "yarn workspaces foreach run compile",
"test": "yarn test:libraries && yarn test:contracts && yarn test:circuits",
"test:libraries": "jest --coverage",
"test:circuits": "yarn workspace @zk-kit/circuits test",
Expand Down
21 changes: 21 additions & 0 deletions packages/eddsa-poseidon/LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
MIT License

Copyright (c) 2023 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.
144 changes: 144 additions & 0 deletions packages/eddsa-poseidon/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
<p align="center">
<h1 align="center">
EdDSA Poseidon
</h1>
<p align="center">A JavaScript EdDSA library for secure signing and verification using <a href="https://www.poseidon-hash.info">Poseidon</a> and the <a href="https://eips.ethereum.org/EIPS/eip-2494">Baby Jubjub</a> elliptic curve.</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/eddsa-poseidon/LICENSE">
<img alt="NPM license" src="https://img.shields.io/npm/l/%40zk-kit%2Feddsa-poseidon?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@zk-kit/eddsa-poseidon">
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/eddsa-poseidon?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@zk-kit/eddsa-poseidon">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/eddsa-poseidon.svg?style=flat-square" />
</a>
<a href="https://bundlephobia.com/package/@zk-kit/eddsa-poseidon">
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/eddsa-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_eddsa_poseidon.html">
📘 Docs
</a>
</h4>
</div>

| This package offers a simplified JavaScript codebase essential for creating and validating digital signatures using EdDSA and Poseidon. It's built upon the Baby Jubjub elliptic curve, ensuring seamless integration with [Circom](https://github.com/iden3/circom) and enhancing the developer experience. |
| ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |

- Super lightweight: [**~33kB**](https://bundlephobia.com/package/@zk-kit/eddsa-poseidon) (minified)
- Compatible with browsers and NodeJS
- TS type support
- Comprehensive code [documentation](https://zkkit.pse.dev/modules/_zk_kit_eddsa_poseidon.html)
- Full test coverage

👾 Would you like to try it now? Explore it now on [Ceditor](https://ceditor.cedoor.dev/52787e4ad57d2f2076648d509efc3448)!

> [!WARNING]
> This library has **not** been audited.
## 🛠 Install

### npm or yarn

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

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

or yarn:

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

### CDN

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

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

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

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

## 📜 Usage

\# **derivePublicKey**(privateKey: _BigNumberish_): _Point\<string>_

```typescript
import { derivePublicKey } from "@zk-kit/eddsa-poseidon"

const privateKey = "secret"
const publicKey = derivePublicKey(privateKey)

console.log(publicKey)
/*
[
'17191193026255111087474416516591393721975640005415762645730433950079177536248',
'13751717961795090314625781035919035073474308127816403910435238282697898234143'
]
*/
```

\# **signMessage**(privateKey: _BigNumberish_, message: _BigNumberish_): _Signature\<string>_

```typescript
import { derivePublicKey, signMessage } from "@zk-kit/eddsa-poseidon"

const privateKey = "secret"
const publicKey = derivePublicKey(privateKey)

const message = "message"
const signature = signMessage(privateKey, message)

console.log(signature)
/*
{
R8: [
'12949573675545142400102669657964360005184873166024880859462384824349649539693',
'18253636630408169174294927826710424418689461166073329946402765380454102840608'
],
S: '701803947557694254685424075312408605924670918868054593580245088593184746870'
}
*/
```

\# **verifySignature**(message: _BigNumberish_, signature: _Signature_, publicKey: _Point_): _boolean_

```typescript
import { derivePublicKey, signMessage, verifySignature } from "@zk-kit/eddsa-poseidon"

const privateKey = "secret"
const publicKey = derivePublicKey(privateKey)

const message = "message"
const signature = signMessage(privateKey, message)

const response = verifySignature(message, signature, publicKey)

console.log(response) // true
```
8 changes: 8 additions & 0 deletions packages/eddsa-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"]
}
46 changes: 46 additions & 0 deletions packages/eddsa-poseidon/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
{
"name": "@zk-kit/eddsa-poseidon",
"version": "0.2.0",
"description": "A JavaScript EdDSA library for secure signing and verification using Poseidon the Baby Jubjub elliptic curve.",
"license": "MIT",
"iife": "dist/index.js",
"unpkg": "dist/index.min.js",
"jsdelivr": "dist/index.min.js",
"main": "dist/index.node.js",
"exports": {
"import": "./dist/index.mjs",
"require": "./dist/index.node.js",
"types": "./dist/types/index.d.ts"
},
"types": "dist/types/index.d.ts",
"files": [
"dist/",
"src/",
"LICENSE",
"README.md"
],
"repository": "https://github.com/privacy-scaling-explorations/zk-kit",
"homepage": "https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/eddsa-poseidon",
"bugs": {
"url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues"
},
"scripts": {
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript && yarn build:iife",
"build:iife": "rollup -c rollup.iife.config.ts --configPlugin typescript",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@rollup/plugin-commonjs": "^25.0.7",
"@rollup/plugin-node-resolve": "^15.2.3",
"blake-hash": "2.0.0",
"circomlibjs": "0.0.8",
"poseidon-lite": "0.2.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-polyfill-node": "^0.13.0",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.31.2"
}
}
32 changes: 32 additions & 0 deletions packages/eddsa-poseidon/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import commonjs from "@rollup/plugin-commonjs"
import { nodeResolve } from "@rollup/plugin-node-resolve"
import fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import typescript from "rollup-plugin-typescript2"

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

export default {
input: "src/index.ts",
output: [
{ file: pkg.exports.require, format: "cjs", banner },
{ file: pkg.exports.import, format: "es", banner }
],
external: [],
plugins: [
typescript({ tsconfig: "./build.tsconfig.json", useTsconfigDeclarationDir: true }),
commonjs(),
nodeResolve({
preferBuiltins: true
}),
cleanup({ comments: "jsdoc" })
]
}
47 changes: 47 additions & 0 deletions packages/eddsa-poseidon/rollup.iife.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import commonjs from "@rollup/plugin-commonjs"
import { nodeResolve } from "@rollup/plugin-node-resolve"
import fs from "fs"
import nodePolyfills from "rollup-plugin-polyfill-node"
import cleanup from "rollup-plugin-cleanup"
import { terser } from "rollup-plugin-terser"
import typescript from "rollup-plugin-typescript2"

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

const name = pkg.name.split("/")[1].replace(/[-/]./g, (x: string) => x.toUpperCase()[1])

export default {
input: "src/index.ts",
output: [
{
file: pkg.iife,
name,
format: "iife",
banner
},
{
file: pkg.unpkg,
name,
format: "iife",
plugins: [terser({ output: { preamble: banner } })]
}
],
external: [],
plugins: [
typescript({ tsconfig: "./build.tsconfig.json", useTsconfigDeclarationDir: true }),
commonjs(),
nodeResolve({
preferBuiltins: true
}),
nodePolyfills({ include: null }),
cleanup({ comments: "jsdoc" })
]
}
Loading

0 comments on commit 6df4769

Please sign in to comment.