Skip to content

Commit

Permalink
Merge pull request #78 from privacy-scaling-explorations/feat/poseido…
Browse files Browse the repository at this point in the history
…n-proof

New package for generating Poseidon proofs
  • Loading branch information
cedoor authored Nov 27, 2023
2 parents db79e42 + 3ce32cf commit b58dbe0
Show file tree
Hide file tree
Showing 25 changed files with 1,476 additions and 22 deletions.
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,34 @@
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/poseidon-proof">
@zk-kit/poseidon-proof
</a>
<a href="https://zkkit.pse.dev/modules/_zk_kit_poseidon_proof.html">
(docs)
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://npmjs.org/package/@zk-kit/poseidon-proof">
<img src="https://img.shields.io/npm/v/@zk-kit/poseidon-proof.svg?style=flat-square" alt="NPM version" />
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@zk-kit/poseidon-proof">
<img src="https://img.shields.io/npm/dm/@zk-kit/poseidon-proof.svg?style=flat-square" alt="Downloads" />
</a>
</td>
<td>
<!-- Size -->
<a href="https://bundlephobia.com/package/@zk-kit/poseidon-proof">
<img src="https://img.shields.io/bundlephobia/minzip/@zk-kit/poseidon-proof" alt="npm bundle size (scoped)" />
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/groth16">
Expand Down
1 change: 0 additions & 1 deletion packages/groth16/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
"url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues"
},
"scripts": {
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
"build": "rimraf dist && yarn build:browser && yarn build:node",
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
Expand Down
1 change: 1 addition & 0 deletions packages/poseidon-proof/.npmrc
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
engine-strict=true
21 changes: 21 additions & 0 deletions packages/poseidon-proof/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.
155 changes: 155 additions & 0 deletions packages/poseidon-proof/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
<p align="center">
<h1 align="center">
Poseidon proof
</h1>
<p align="center">A library to generate and verify Poseidon proofs.</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-proof/LICENSE">
<img alt="NPM license" src="https://img.shields.io/npm/l/%40zk-kit%2Fposeidon-proof?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@zk-kit/poseidon-proof">
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/poseidon-proof?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@zk-kit/poseidon-proof">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/poseidon-proof.svg?style=flat-square" />
</a>
<a href="https://bundlephobia.com/package/@zk-kit/poseidon-proof">
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/poseidon-proof" />
</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_proof.html">
📘 Docs
</a>
</h4>
</div>

| This zero-knowledge library facilitates the demonstration of having a Poseidon hash pre-image while keeping the pre-image value confidential. Additionally, it offers a mechanism to prevent the same proof from being reused. The circuit that forms the foundation of this library is accessible via this [link](https://github.com/privacy-scaling-explorations/zk-kit/blob/main/packages/circuits/templates/poseidon-proof.circom). |
| --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Originally developed for integration with [Semaphore V4](https://github.com/semaphore-protocol/semaphore), this library also functions effectively as a standalone tool. Notable use cases in connection with Semaphore can be:

- allowing a Semaphore user to prove that they possess the secret value associated with the identity commitment of their Semaphore identity,
- unmasking the identity of zero-knowledge proofs earlier created using Semaphore.

The Snark artifacts (`.wasm` and `.zkey` files) can be specified or not in the `generate` function parameters and can possibly be downloaded using the following URLs:

https://github.com/privacy-scaling-explorations/zk-kit/blob/ee457299d36d2601e5bf520237977a9f16b1b599/packages/poseidon-proof/src/config.ts#L5-L8

> [!WARNING]
> The Snark artifacts currently used to generate zero-knowledge proofs are the result of an unsecure trusted setup, and the library has not yet been audited. Therefore, it is advised not to use it in production.
## 🛠 Install

### npm or yarn

Install the `@zk-kit/poseidon-proof` package:

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

or yarn:

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

## 📜 Usage

\# **generate**(
message: _BigNumberish_,
scope: _BigNumberish_,
snarkArtifacts?: _SnarkArtifacts_
): Promise\<_PoseidonProof_>

```typescript
import { generate } from "@zk-kit/poseidon-proof"

const scope = 1
const message = 2

const fullProof = await generate(message, scope)

// If not specified, the Snark artifacts are downloaded automatically.
// You can also specify them.
const fullProof2 = await generate(message, scope, {
zkeyFilePath: "./poseidon-proof.zkey",
wasmFilePath: "./poseidon-proof.wasm"
})

console.log(fullProof)
/*
{
scope: '1',
hash: '8645981980787649023086883978738420856660271013038108762834452721572614684349',
nullifier: '7853200120776062878684798364095072458815029376092732009249414926327459813530',
proof: [
'8439157877715136449451128027974582198942685548176158240742469858289217640295',
'7742778415084033741154475792823260193410560424636243716622958831215275441500',
'19005176609208302398813682947612978224483117018101271134113308439452561466691',
'6209272538382784759793219866517186573065390524051102537220446165983489601194',
'12222537243697573476419214640884921904066033266502734603198915705889607365883',
'20066191345466355816238393590466176790809335456890624395337970890893401456064',
'4851746896803117511000878727783002155680855098198108089136166702412365578625',
'18391775232946751568173446051923718267369779168471637305003186214102154164036'
]
}
*/
```

\# **verify**(poseidonProof: _PoseidonProof_): Promise\<_boolean_>

```typescript
import { verify } from "@zk-kit/poseidon-proof"

const response = await verify(fullProof)

console.log(response) // true or false

// Eventually you may want to check the nullifier.
```

## 📈 Benchmarks

Benchmarks were run on an Intel Core i7-1165G7, 16 GB RAM machine, after initializing the BN128 curve with [`@zk-kit/groth16`](https://github.com/privacy-scaling-explorations/zk-kit/edit/main/packages/groth16)-`buildBn128` (~230ms).

| Generate proof | Verify proof | Constraints |
| -------------- | ------------ | ----------- |
| `80ms` | `10ms` | `141` |

```js
import { generate, verify } from "@zk-kit/poseidon-proof"
import { buildBn128 } from "@zk-kit/groth16"

await buildBn128()

console.time("generate")

const proof = await generate(1, 2)

console.timeEnd("generate")

console.time("verify")

console.log(await verify(proof))

console.timeEnd("verify")
```
9 changes: 9 additions & 0 deletions packages/poseidon-proof/build.tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
{
"extends": "../../tsconfig.json",
"compilerOptions": {
"baseUrl": ".",
"declarationDir": "dist/types",
"resolveJsonModule": true
},
"include": ["src"]
}
57 changes: 57 additions & 0 deletions packages/poseidon-proof/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
{
"name": "@zk-kit/poseidon-proof",
"version": "0.5.0",
"description": "A library to generate and verify Poseidon proofs.",
"license": "MIT",
"main": "dist/index.node.js",
"exports": {
"node": {
"import": "./dist/index.node.mjs",
"require": "./dist/index.node.js"
},
"browser": "./dist/index.browser.mjs",
"default": "./dist/index.browser.mjs"
},
"types": "dist/types/index.d.ts",
"files": [
"dist/",
"src/",
"LICENSE",
"README.md"
],
"engines": {
"node": ">=18.0.0"
},
"repository": "[email protected]:privacy-scaling-explorations/zk-kit.git",
"homepage": "https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/poseidon-proof",
"bugs": {
"url": "https://github.com/privacy-scaling-explorations/zk-kit.git/issues"
},
"scripts": {
"build": "rimraf dist && yarn build:browser && yarn build:node",
"build:browser": "rollup -c rollup.browser.config.ts --configPlugin typescript",
"build:node": "rollup -c rollup.node.config.ts --configPlugin typescript",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@rollup/plugin-alias": "^5.0.1",
"@rollup/plugin-json": "^5.0.1",
"@types/download": "^8.0.5",
"@types/tmp": "^0.2.6",
"poseidon-lite": "^0.2.0",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-terser": "^7.0.2",
"rollup-plugin-typescript2": "^0.32.1"
},
"dependencies": {
"@ethersproject/bignumber": "^5.7.0",
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0",
"@zk-kit/groth16": "0.4.0",
"download": "^8.0.0",
"tmp": "^0.2.1"
}
}
38 changes: 38 additions & 0 deletions packages/poseidon-proof/rollup.browser.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import alias from "@rollup/plugin-alias"
import json from "@rollup/plugin-json"
import * as fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import typescript from "rollup-plugin-typescript2"

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

export default {
input: "src/index.ts",
output: [
{
file: pkg.exports.browser,
format: "es",
banner
}
],
external: Object.keys(pkg.dependencies),
plugins: [
alias({
entries: [{ find: "./get-snark-artifacts.node", replacement: "./get-snark-artifacts.browser" }]
}),
typescript({
tsconfig: "./build.tsconfig.json",
useTsconfigDeclarationDir: true
}),
cleanup({ comments: "jsdoc" }),
json()
]
}
40 changes: 40 additions & 0 deletions packages/poseidon-proof/rollup.node.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import json from "@rollup/plugin-json"
import * as fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import typescript from "rollup-plugin-typescript2"

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

export default {
input: "src/index.ts",
output: [
{
file: pkg.exports.node.require,
format: "cjs",
banner,
exports: "auto"
},
{
file: pkg.exports.node.import,
format: "es",
banner
}
],
external: Object.keys(pkg.dependencies),
plugins: [
typescript({
tsconfig: "./build.tsconfig.json",
useTsconfigDeclarationDir: true
}),
cleanup({ comments: "jsdoc" }),
json()
]
}
5 changes: 5 additions & 0 deletions packages/poseidon-proof/src/config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line import/prefer-default-export
export const defaultSnarkArtifacts = {
wasmFilePath: "https://zkkit.cedoor.dev/poseidon-proof.wasm",
zkeyFilePath: "https://zkkit.cedoor.dev/poseidon-proof.zkey"
}
Loading

0 comments on commit b58dbe0

Please sign in to comment.