Skip to content

Commit

Permalink
feat(groth16): create new groth16 package
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Oct 12, 2023
1 parent 385d123 commit 0dc08b8
Show file tree
Hide file tree
Showing 22 changed files with 2,017 additions and 20 deletions.
21 changes: 21 additions & 0 deletions packages/groth16/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.
119 changes: 119 additions & 0 deletions packages/groth16/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
<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/blob/main/LICENSE">
<img alt="Github license" src="https://img.shields.io/github/license/privacy-scaling-explorations/zk-kit.svg?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. |
| ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |

## 🛠 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.1.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

0 comments on commit 0dc08b8

Please sign in to comment.