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

Groth16 package #69

Merged
merged 7 commits into from
Oct 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 25 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,31 @@
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/groth16">
@zk-kit/groth16
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://npmjs.org/package/@zk-kit/groth16">
<img src="https://img.shields.io/npm/v/@zk-kit/groth16.svg?style=flat-square" alt="NPM version" />
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@zk-kit/groth16">
<img src="https://img.shields.io/npm/dm/@zk-kit/groth16.svg?style=flat-square" alt="Downloads" />
</a>
</td>
<td>
<!-- Size -->
<a href="https://bundlephobia.com/package/@zk-kit/groth16">
<img src="https://img.shields.io/bundlephobia/minzip/@zk-kit/groth16" alt="npm bundle size (scoped)" />
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/rollup-plugin-rust">
Expand Down
674 changes: 674 additions & 0 deletions packages/groth16/LICENSE

Large diffs are not rendered by default.

125 changes: 125 additions & 0 deletions packages/groth16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<p align="center">
<h1 align="center">
SnarkJS Groth16
</h1>
<p align="center">A snippet of SnarkJS code for verifying and generating Groth16 proofs only.</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/groth16/LICENSE">
<img alt="NPM license" src="https://img.shields.io/npm/l/%40zk-kit%2Fgroth16?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@zk-kit/groth16">
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/groth16?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@zk-kit/groth16">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/groth16.svg?style=flat-square" />
</a>
<a href="https://bundlephobia.com/package/@zk-kit/groth16">
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/groth16" />
</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_groth16.html">
📘 Docs
</a>
</h4>
</div>

| This package contains [SnarkJS](https://github.com/iden3/snarkjs) functions for generating and verifying zero knowledge proofs with Groth16 specifically. In addition to the original code it also uses the cached `bn128` curve if it already exists, making verification and generation of consecutive proofs faster. |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

Some advantages of using this package instead of `snarkjs` directly are:
* It only includes code to verify and generate Groth16 proofs, making your final bundle lighter.
* It doesn't call the [ffjavascript](https://github.com/iden3/ffjavascript) `buildBn128` function if a `bn128` cached curve already exists, making verification and generation of consecutive proofs much faster (e.g. verification seems to be ~9 times faster after the first one).
* It includes TS types.
* It provides an ESM bundle that is compatible with browsers. So there is no need to add any polyfill or additional configuration.

## 🛠 Install

### npm or yarn

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

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

or yarn:

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

## 📜 Usage

\# **prove**(input: _CircuitSignals_, wasmFile: _ZKArtifact_, zkeyFile: _ZKArtifact_): Promise\<_{
proof: Groth16Proof
publicSignals: PublicSignals
}_>

```typescript
import { prove } from "@zk-kit/groth16"

const input = {
message: 12,
scope: 122
}

const proof = await prove(input, "./circuit.zkey", "./circuit.wasm")

console.log(proof)
/*
{
proof: {
pi_a: [
'8259885706934172848141475422209230656096448508815982888010519325096632035723',
'3142099172052192611205205328157407975469005554072266974009053708782134081166',
'1'
],
pi_b: [ [Array], [Array], [Array] ],
pi_c: [
'13863804425308906943736719856399634046638544298517159271373916818387594277305',
'21340646707244019956779928177502771923632450548108204371058275686712196195969',
'1'
],
protocol: 'groth16',
curve: 'bn128'
},
publicSignals: [
'527758365153958423212195330785598453331596731388181860789801455413116800554',
'19104626566001952573667666924569656871967113105870778077087237826253896482830',
'122'
]
}
*/
```

\# **verify**(verificationKey: _any_, proof: _{
proof: Groth16Proof
publicSignals: PublicSignals
}_): Promise\<_boolean_>

```typescript
import { verify } from "@zk-kit/groth16"
import verificationKey from "./circuit.json"

const response = await verify(verificationKey, proof)

console.log(response) // true
```
8 changes: 8 additions & 0 deletions packages/groth16/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"]
}
52 changes: 52 additions & 0 deletions packages/groth16/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
{
"name": "@zk-kit/groth16",
"version": "0.2.0",
"description": "A snippet of SnarkJS code for verifying and generating Groth16 proofs only.",
"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"
],
"repository": "https://github.com/privacy-scaling-explorations/zk-kit",
"homepage": "https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/groth16",
"bugs": {
"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",
"prepublishOnly": "yarn build"
},
"publishConfig": {
"access": "public"
},
"devDependencies": {
"@iden3/binfileutils": "0.0.11",
"@rollup/plugin-commonjs": "^24.1.0",
"@rollup/plugin-node-resolve": "^15.0.2",
"@rollup/plugin-virtual": "^3.0.2",
"fastfile": "0.0.20",
"rimraf": "^5.0.5",
"rollup": "^4.0.2",
"rollup-plugin-cleanup": "^3.2.1",
"rollup-plugin-typescript2": "^0.31.2"
},
"dependencies": {
"circom_runtime": "0.1.24",
"ffjavascript": "0.2.60"
}
}
56 changes: 56 additions & 0 deletions packages/groth16/rollup.browser.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import commonjs from "@rollup/plugin-commonjs"
import { nodeResolve } from "@rollup/plugin-node-resolve"
import virtual from "@rollup/plugin-virtual"
import * as fs from "fs"
import cleanup from "rollup-plugin-cleanup"
import typescript from "rollup-plugin-typescript2"

// Needed by fastfile.
import { O_CREAT, O_EXCL, O_RDONLY, O_RDWR, O_TRUNC } from "constants"

const constants = `
export const O_TRUNC = ${O_TRUNC};
export const O_CREAT = ${O_CREAT};
export const O_RDWR = ${O_RDWR};
export const O_EXCL = ${O_EXCL};
export const O_RDONLY = ${O_RDONLY}
`

const empty = "export default {}"

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: [
typescript({
tsconfig: "./build.tsconfig.json",
useTsconfigDeclarationDir: true
}),
virtual({
fs: empty,
constants
}),
nodeResolve(),
commonjs({
esmExternals: true
}),
cleanup({ comments: "jsdoc" })
]
}
44 changes: 44 additions & 0 deletions packages/groth16/rollup.node.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import commonjs from "@rollup/plugin-commonjs"
import { nodeResolve } from "@rollup/plugin-node-resolve"
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
}),
nodeResolve(),
commonjs({
esmExternals: true
}),
cleanup({ comments: "jsdoc" })
]
}
Loading