Skip to content

Commit

Permalink
feat(utils): add new utils package
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Dec 20, 2023
1 parent 40483e7 commit afce4fe
Show file tree
Hide file tree
Showing 39 changed files with 578 additions and 663 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
72 changes: 60 additions & 12 deletions packages/baby-jubjub/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,17 +41,6 @@
</h4>
</div>

| WIP |
| --- |

- Super lightweight: [**~33kB**](https://bundlephobia.com/package/@zk-kit/baby-jubjub) (minified)
- Compatible with browsers and NodeJS
- TS type support
- Comprehensive code [documentation](https://zkkit.pse.dev/modules/_zk_kit_baby_jubjub.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.
Expand Down Expand Up @@ -87,4 +76,63 @@ or [JSDelivr](https://www.jsdelivr.com/):

## 📜 Usage

WIP
\# **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
```
3 changes: 3 additions & 0 deletions packages/baby-jubjub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,8 @@
"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"
}
}
8 changes: 1 addition & 7 deletions packages/baby-jubjub/rollup.config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
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"
Expand All @@ -20,13 +18,9 @@ export default {
{ file: pkg.exports.require, format: "cjs", banner },
{ file: pkg.exports.import, format: "es", banner }
],
external: [],
external: Object.keys(pkg.dependencies),
plugins: [
typescript({ tsconfig: "./build.tsconfig.json", useTsconfigDeclarationDir: true }),
commonjs(),
nodeResolve({
preferBuiltins: true
}),
cleanup({ comments: "jsdoc" })
]
}
26 changes: 12 additions & 14 deletions packages/baby-jubjub/src/baby-jubjub.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,14 @@
import Field from "./field"
import * as scalar from "./scalar"
import { Point } from "./types"
import * as utils from "./utils"
import { F1Field, bigintToHexadecimal, bufferToBigint, leBigintToBuffer, leBufferToBigint, scalar } from "@zk-kit/utils"
import * as sqrt from "./sqrt"
import { Point } from "./types"

// Spec: https://eips.ethereum.org/EIPS/eip-2494

// 'r' is the alt_bn128 prime order.
export const r = BigInt("21888242871839275222246405745257275088548364400416034343698204186575808495617")

// 'F' (F_r) is the prime finite field with r elements.
export const Fr = new Field(r)
export const Fr = new F1Field(r)

// Base8 is the base point used to generate other points on the curve.
export const Base8: Point<bigint> = [
Expand Down Expand Up @@ -72,7 +70,7 @@ export function addPoint(p1: Point<bigint>, p2: Point<bigint>): Point<bigint> {
*/
export function mulPointEscalar(base: Point<bigint>, e: bigint): Point<bigint> {
let res: Point<bigint> = [Fr.e(BigInt(0)), Fr.e(BigInt(1))]
let rem: bigint = Fr.e(e)
let rem: bigint = e
let exp: Point<bigint> = base

while (!scalar.isZero(rem)) {
Expand All @@ -87,7 +85,7 @@ export function mulPointEscalar(base: Point<bigint>, e: bigint): Point<bigint> {
return res
}

export function inCurve(p: Point) {
export function inCurve(p: Point): boolean {
const x1 = BigInt(p[0])
const y1 = BigInt(p[1])

Expand All @@ -98,17 +96,17 @@ export function inCurve(p: Point) {
}

export function packPoint(unpackedPoint: Point<bigint>): bigint {
const buffer = utils.leInt2Buff(unpackedPoint[1])
const buffer = leBigintToBuffer(unpackedPoint[1])

if (Fr.lt(unpackedPoint[0], Fr.zero)) {
buffer[31] |= 0x80
}

return utils.buff2int(buffer)
return bufferToBigint(buffer)
}

export function unpackPoint(packedPoint: bigint): Point | null {
const buffer = Buffer.from(utils.int2hex(packedPoint), "hex")
export function unpackPoint(packedPoint: bigint): Point<bigint> | null {
const buffer = Buffer.from(bigintToHexadecimal(packedPoint), "hex")
const unpackedPoint = new Array(2)

let sign = false
Expand All @@ -118,15 +116,15 @@ export function unpackPoint(packedPoint: bigint): Point | null {
buffer[31] &= 0x7f
}

unpackedPoint[1] = utils.leBuff2int(buffer)
unpackedPoint[1] = leBufferToBigint(buffer)

if (scalar.gt(unpackedPoint[1], r)) {
return null
}

const y2 = Fr.square(unpackedPoint[1])

let x = sqrt.tonelliShanks(Fr.div(Fr.sub(Fr.one, y2), Fr.sub(a, Fr.mul(d, y2))))
let x = sqrt.tonelliShanks(Fr.div(Fr.sub(Fr.one, y2), Fr.sub(a, Fr.mul(d, y2))), r)

if (x == null) {
return null
Expand All @@ -138,5 +136,5 @@ export function unpackPoint(packedPoint: bigint): Point | null {

unpackedPoint[0] = x

return unpackedPoint as Point
return unpackedPoint as Point<bigint>
}
7 changes: 3 additions & 4 deletions packages/baby-jubjub/src/sqrt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { r } from "./baby-jubjub"
import Field from "./field"
import { F1Field } from "@zk-kit/utils"

// Based on https://eprint.iacr.org/2012/685.pdf
// and https://github.com/iden3/ffjavascript/blob/6f37a93fabddf45100bf221de6a1399599497e5d/src/fsqrt.js#L38
Expand All @@ -11,8 +10,8 @@ import Field from "./field"
* @returns The square root.
*/
// eslint-disable-next-line import/prefer-default-export
export function tonelliShanks(n: bigint) {
const Fr = new Field(r)
export function tonelliShanks(n: bigint, order: bigint) {
const Fr = new F1Field(order)

const sqrt_s = 28
const sqrt_z = BigInt("5978345932401256595026418116861078668372907927053715034645334559810731495452")
Expand Down
9 changes: 1 addition & 8 deletions packages/baby-jubjub/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,3 @@
export type BigNumber = bigint | string

export type BigNumberish = BigNumber | number | Buffer
import { BigNumber } from "@zk-kit/utils"

export type Point<N = BigNumber> = [N, N]

export type Signature<N = BigNumber> = {
R8: Point<N>
S: N
}
Loading

0 comments on commit afce4fe

Please sign in to comment.