Skip to content

Commit

Permalink
Merge pull request #109 from privacy-scaling-explorations/feat/baby-j…
Browse files Browse the repository at this point in the history
…ubjub

New packages: Baby Jubjub and Utils
  • Loading branch information
cedoor authored Dec 21, 2023
2 parents fb7281e + afce4fe commit bf9b249
Show file tree
Hide file tree
Showing 40 changed files with 860 additions and 142 deletions.
56 changes: 56 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,62 @@
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/baby-jubjub">
@zk-kit/baby-jubjub
</a>
<a href="https://zkkit.pse.dev/modules/_zk_kit_baby_jubjub.html">
(docs)
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://npmjs.org/package/@zk-kit/baby-jubjub">
<img src="https://img.shields.io/npm/v/@zk-kit/baby-jubjub.svg?style=flat-square" alt="NPM version" />
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@zk-kit/baby-jubjub">
<img src="https://img.shields.io/npm/dm/@zk-kit/baby-jubjub.svg?style=flat-square" alt="Downloads" />
</a>
</td>
<td>
<!-- Size -->
<a href="https://bundlephobia.com/package/@zk-kit/baby-jubjub">
<img src="https://img.shields.io/bundlephobia/minzip/@zk-kit/baby-jubjub" alt="npm bundle size (scoped)" />
</a>
</td>
</tr>
<tr>
<td>
<a href="https://github.com/privacy-scaling-explorations/zk-kit/tree/main/packages/utils">
@zk-kit/utils
</a>
<a href="https://zkkit.pse.dev/modules/_zk_kit_utils.html">
(docs)
</a>
</td>
<td>
<!-- NPM version -->
<a href="https://npmjs.org/package/@zk-kit/utils">
<img src="https://img.shields.io/npm/v/@zk-kit/utils.svg?style=flat-square" alt="NPM version" />
</a>
</td>
<td>
<!-- Downloads -->
<a href="https://npmjs.org/package/@zk-kit/utils">
<img src="https://img.shields.io/npm/dm/@zk-kit/utils.svg?style=flat-square" alt="Downloads" />
</a>
</td>
<td>
<!-- Size -->
<a href="https://bundlephobia.com/package/@zk-kit/utils">
<img src="https://img.shields.io/bundlephobia/minzip/@zk-kit/utils" 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
2 changes: 1 addition & 1 deletion jest.config.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import fs from "fs"
import type { Config } from "@jest/types"

const exclude = ["circuits", "imt.sol", "rollup-plugin-rust", "lazytower.sol", "lazytower.circom"]
const exclude = ["circuits", "imt.sol", "rollup-plugin-rust", "lazytower.sol", "lazytower.circom", "utils"]

const projects: any = fs
.readdirSync("./packages", { withFileTypes: true })
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
"private": true,
"scripts": {
"build": "yarn build:libraries && yarn compile:contracts",
"build:libraries": "yarn workspaces foreach --no-private run build",
"build:libraries": "yarn workspaces foreach -t --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",
Expand Down
21 changes: 21 additions & 0 deletions packages/baby-jubjub/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.
138 changes: 138 additions & 0 deletions packages/baby-jubjub/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
<p align="center">
<h1 align="center">
Baby Jubjub
</h1>
<p align="center">A JavaScript library for adding points to the <a href="https://eips.ethereum.org/EIPS/eip-2494">Baby Jubjub</a> 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/baby-jubjub/LICENSE">
<img alt="NPM license" src="https://img.shields.io/npm/l/%40zk-kit%2Fbaby-jubjub?style=flat-square">
</a>
<a href="https://www.npmjs.com/package/@zk-kit/baby-jubjub">
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/baby-jubjub?style=flat-square" />
</a>
<a href="https://npmjs.org/package/@zk-kit/baby-jubjub">
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/baby-jubjub.svg?style=flat-square" />
</a>
<a href="https://bundlephobia.com/package/@zk-kit/baby-jubjub">
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/baby-jubjub" />
</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_baby_jubjub.html">
📘 Docs
</a>
</h4>
</div>

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

### npm or yarn

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

```bash
npm i @zk-kit/baby-jubjub
```

or yarn:

```bash
yarn add @zk-kit/baby-jubjub
```

### CDN

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

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

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

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

## 📜 Usage

\# **addPoint**(p1: _Point\<bigint>_, p2: _Point\<bigint>_): _bigint_

```typescript
import { addPoint } from "@zk-kit/baby-jubjub"

const p1: Point<bigint> = [BigInt(0), BigInt(1)]

const newPoint = addPoint(p1, Base8)
```

\# **mulPointEscalar**(base: _Point\<bigint>_, e: _bigint_): _Point\<bigint>_

```typescript
import { Base8, mulPointEscalar } from "@zk-kit/baby-jubjub"

const secretScalar = BigInt(324)

const publicKey = mulPointEscalar(Base8, secretScalar)
```

\# **mulPointEscalar**(base: _Point\<bigint>_, e: _bigint_): _Point\<bigint>_

```typescript
import { inCurve, Base8, mulPointEscalar } from "@zk-kit/baby-jubjub"

const secretScalar = BigInt(324)

const publicKey = mulPointEscalar(Base8, secretScalar)

const isInCurve = inCurve(publicKey)
```

\# **packPoint**(unpackedPoint: _Point\<bigint>_): _bigint_

```typescript
import { packPoint, Base8, mulPointEscalar } from "@zk-kit/baby-jubjub"

const secretScalar = BigInt(324)

const publicKey = mulPointEscalar(Base8, secretScalar)

const packedPoint = packPoint(publicKey)
```

\# **unpackPoint**(packedPoint: _bigint_): _Point\<bigint>_ | _null_

```typescript
import { packPoint, unpackPoint, Base8, mulPointEscalar } from "@zk-kit/baby-jubjub"

const secretScalar = BigInt(324)

const publicKey = mulPointEscalar(Base8, secretScalar)

const packedPoint = packPoint(publicKey)

const unpackedPoint = unpackPoint(packedPoint)

console.log(publicKey[0] === unpackedPoint[0]) // true
console.log(publicKey[1] === unpackedPoint[1]) // true
```
8 changes: 8 additions & 0 deletions packages/baby-jubjub/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"]
}
47 changes: 47 additions & 0 deletions packages/baby-jubjub/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
{
"name": "@zk-kit/baby-jubjub",
"version": "0.1.0",
"description": "A JavaScript library for adding points to the 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/baby-jubjub",
"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",
"circomlibjs": "0.0.8",
"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"
},
"dependencies": {
"@zk-kit/utils": "0.1.0"
}
}
26 changes: 26 additions & 0 deletions packages/baby-jubjub/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
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: Object.keys(pkg.dependencies),
plugins: [
typescript({ tsconfig: "./build.tsconfig.json", useTsconfigDeclarationDir: true }),
cleanup({ comments: "jsdoc" })
]
}
47 changes: 47 additions & 0 deletions packages/baby-jubjub/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 bf9b249

Please sign in to comment.