Skip to content

Commit

Permalink
chore(utils): v1.0.0-beta.1
Browse files Browse the repository at this point in the history
  • Loading branch information
cedoor committed Apr 16, 2024
1 parent 10423bc commit d5e0a94
Show file tree
Hide file tree
Showing 12 changed files with 56 additions and 46 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
"format:write": "turbo format:prettier:write",
"benchmarks": "rimraf benchmarks/results && ts-node benchmarks/index.ts",
"docs": "typedoc --cname zkkit.pse.dev --githubPages true",
"remove:stable-version-field": "ts-node scripts/remove-stable-version-field.ts ${0} && yarn prettier:write",
"remove:stable-version-field": "ts-node scripts/remove-stable-version-field.ts ${0} && yarn format:prettier:write",
"precommit": "lint-staged",
"postinstall": "husky install",
"style": "turbo lint:eslint lint format:prettier"
Expand Down
2 changes: 1 addition & 1 deletion packages/baby-jubjub/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
"rollup-plugin-polyfill-node": "^0.13.0"
},
"dependencies": {
"@zk-kit/utils": "1.0.0-beta"
"@zk-kit/utils": "1.0.0-beta.1"
}
}
2 changes: 1 addition & 1 deletion packages/circuits/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"@zk-kit/eddsa-poseidon": "0.11.0",
"@zk-kit/poseidon-cipher": "0.3.0",
"@zk-kit/smt": "1.0.0",
"@zk-kit/utils": "1.0.0-beta",
"@zk-kit/utils": "1.0.0-beta.1",
"circomkit": "0.0.24",
"mocha": "^10.2.0",
"poseidon-lite": "^0.2.0"
Expand Down
2 changes: 1 addition & 1 deletion packages/eddsa-poseidon/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
},
"dependencies": {
"@zk-kit/baby-jubjub": "1.0.0-beta",
"@zk-kit/utils": "1.0.0-beta",
"@zk-kit/utils": "1.0.0-beta.1",
"buffer": "6.0.3"
}
}
2 changes: 1 addition & 1 deletion packages/eddsa-proof/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@
"@ethersproject/bytes": "^5.7.0",
"@ethersproject/keccak256": "^5.7.0",
"@zk-kit/eddsa-poseidon": "0.11.0",
"@zk-kit/utils": "1.0.0-beta",
"@zk-kit/utils": "1.0.0-beta.1",
"snarkjs": "^0.7.3"
}
}
3 changes: 3 additions & 0 deletions packages/imt/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,8 @@
"rimraf": "^5.0.5",
"rollup": "^4.12.0",
"rollup-plugin-cleanup": "^3.2.1"
},
"dependencies": {
"@zk-kit/utils": "1.0.0-beta.1"
}
}
30 changes: 15 additions & 15 deletions packages/imt/src/imt/imt.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import checkParameter from "./checkParameter"
import { requireArray, requireFunction, requireNumber, requireObject, requireTypes } from "@zk-kit/utils/error-handlers"
import { IMTHashFunction, IMTMerkleProof, IMTNode } from "./types"

/**
Expand Down Expand Up @@ -53,11 +53,11 @@ export default class IMT {
* @param leaves The list of initial leaves.
*/
constructor(hash: IMTHashFunction, depth: number, zeroValue: IMTNode, arity = 2, leaves: IMTNode[] = []) {
checkParameter(hash, "hash", "function")
checkParameter(depth, "depth", "number")
checkParameter(zeroValue, "zeroValue", "number", "string", "bigint")
checkParameter(arity, "arity", "number")
checkParameter(leaves, "leaves", "object")
requireFunction(hash, "hash")
requireNumber(depth, "depth")
requireTypes(zeroValue, "zeroValue", ["number", "string", "bigint"])
requireNumber(arity, "arity")
requireObject(leaves, "leaves")

if (leaves.length > arity ** depth) {
throw new Error(`The tree cannot contain more than ${arity ** depth} leaves`)
Expand Down Expand Up @@ -155,7 +155,7 @@ export default class IMT {
* @returns The index of the leaf.
*/
public indexOf(leaf: IMTNode): number {
checkParameter(leaf, "leaf", "number", "string", "bigint")
requireTypes(leaf, "leaf", ["number", "string", "bigint"])

return this._nodes[0].indexOf(leaf)
}
Expand All @@ -171,7 +171,7 @@ export default class IMT {
* @param leaf The new leaf to be inserted in the tree.
*/
public insert(leaf: IMTNode) {
checkParameter(leaf, "leaf", "number", "string", "bigint")
requireTypes(leaf, "leaf", ["number", "string", "bigint"])

if (this._nodes[0].length >= this.arity ** this.depth) {
throw new Error("The tree is full")
Expand Down Expand Up @@ -218,7 +218,7 @@ export default class IMT {
* @param newLeaf The new leaf to be inserted.
*/
public update(index: number, newLeaf: IMTNode) {
checkParameter(index, "index", "number")
requireNumber(index, "index")

if (index < 0 || index >= this._nodes[0].length) {
throw new Error("The leaf does not exist in this tree")
Expand Down Expand Up @@ -256,7 +256,7 @@ export default class IMT {
* @returns The Merkle proof of the leaf.
*/
public createProof(index: number): IMTMerkleProof {
checkParameter(index, "index", "number")
requireNumber(index, "index")

if (index < 0 || index >= this._nodes[0].length) {
throw new Error("The leaf does not exist in this tree")
Expand Down Expand Up @@ -310,11 +310,11 @@ export default class IMT {
* @returns True if the leaf is part of the tree, and false otherwise.
*/
public static verifyProof(proof: IMTMerkleProof, hash: IMTHashFunction): boolean {
checkParameter(proof, "proof", "object")
checkParameter(proof.root, "proof.root", "number", "string", "bigint")
checkParameter(proof.leaf, "proof.leaf", "number", "string", "bigint")
checkParameter(proof.siblings, "proof.siblings", "object")
checkParameter(proof.pathIndices, "proof.pathElements", "object")
requireObject(proof, "proof")
requireTypes(proof.root, "proof.root", ["number", "string", "bigint"])
requireTypes(proof.leaf, "proof.leaf", ["number", "string", "bigint"])
requireArray(proof.siblings, "proof.siblings")
requireArray(proof.pathIndices, "proof.pathIndices")

let node = proof.leaf

Expand Down
36 changes: 21 additions & 15 deletions packages/imt/src/lean-imt/lean-imt.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
import {
requireArray,
requireFunction,
requireDefined,
requireNumber,
requireString
} from "@zk-kit/utils/error-handlers"
import { LeanIMTHashFunction, LeanIMTMerkleProof } from "./types"
import { requireArray, requireDefinedParameter, requireFunction, requireNumber, requireString } from "./utils"

/**
* The {@link LeanIMT} is an optimized binary version of the {@link IMT}.
Expand Down Expand Up @@ -31,7 +37,7 @@ export default class LeanIMT<N = bigint> {
* @param leaves The list of leaves.
*/
constructor(hash: LeanIMTHashFunction<N>, leaves: N[] = []) {
requireDefinedParameter(hash, "hash")
requireDefined(hash, "hash")
requireFunction(hash, "hash")
requireArray(leaves, "leaves")

Expand Down Expand Up @@ -88,7 +94,7 @@ export default class LeanIMT<N = bigint> {
* @returns The index of the leaf.
*/
public indexOf(leaf: N): number {
requireDefinedParameter(leaf, "leaf")
requireDefined(leaf, "leaf")

return this._nodes[0].indexOf(leaf)
}
Expand All @@ -99,7 +105,7 @@ export default class LeanIMT<N = bigint> {
* @returns True if the tree has the leaf, and false otherwise.
*/
public has(leaf: N): boolean {
requireDefinedParameter(leaf, "leaf")
requireDefined(leaf, "leaf")

return this._nodes[0].includes(leaf)
}
Expand All @@ -114,7 +120,7 @@ export default class LeanIMT<N = bigint> {
* @param leaf The new leaf to be inserted in the tree.
*/
public insert(leaf: N) {
requireDefinedParameter(leaf, "leaf")
requireDefined(leaf, "leaf")

// If the next depth is greater, a new tree level will be added.
if (this.depth < Math.ceil(Math.log2(this.size + 1))) {
Expand Down Expand Up @@ -154,7 +160,7 @@ export default class LeanIMT<N = bigint> {
* @param leaves The list of leaves to be inserted.
*/
public insertMany(leaves: N[]) {
requireDefinedParameter(leaves, "leaves")
requireDefined(leaves, "leaves")
requireArray(leaves, "leaves")

if (leaves.length === 0) {
Expand Down Expand Up @@ -197,8 +203,8 @@ export default class LeanIMT<N = bigint> {
* @param newLeaf The new leaf to be inserted.
*/
public update(index: number, newLeaf: N) {
requireDefinedParameter(index, "index")
requireDefinedParameter(newLeaf, "newLeaf")
requireDefined(index, "index")
requireDefined(newLeaf, "newLeaf")
requireNumber(index, "index")

let node = newLeaf
Expand Down Expand Up @@ -234,7 +240,7 @@ export default class LeanIMT<N = bigint> {
* @returns The Merkle proof of the leaf.
*/
public generateProof(index: number): LeanIMTMerkleProof<N> {
requireDefinedParameter(index, "index")
requireDefined(index, "index")
requireNumber(index, "index")

if (index < 0 || index >= this.size) {
Expand Down Expand Up @@ -285,14 +291,14 @@ export default class LeanIMT<N = bigint> {
* @returns True if the leaf is part of the tree, and false otherwise.
*/
public static verifyProof<N>(proof: LeanIMTMerkleProof<N>, hash: LeanIMTHashFunction<N>): boolean {
requireDefinedParameter(proof, "proof")
requireDefined(proof, "proof")

const { root, leaf, siblings, index } = proof

requireDefinedParameter(proof.root, "proof.root")
requireDefinedParameter(proof.leaf, "proof.leaf")
requireDefinedParameter(proof.siblings, "proof.siblings")
requireDefinedParameter(proof.index, "proof.index")
requireDefined(proof.root, "proof.root")
requireDefined(proof.leaf, "proof.leaf")
requireDefined(proof.siblings, "proof.siblings")
requireDefined(proof.index, "proof.index")

requireArray(proof.siblings, "proof.siblings")
requireNumber(proof.index, "proof.index")
Expand Down Expand Up @@ -328,7 +334,7 @@ export default class LeanIMT<N = bigint> {
* @param nodes The stringified JSON of the tree.
*/
public import(nodes: string) {
requireDefinedParameter(nodes, "nodes")
requireDefined(nodes, "nodes")
requireString(nodes, "nodes")

if (this.size !== 0) {
Expand Down
6 changes: 3 additions & 3 deletions packages/imt/tests/lean-imt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ describe("Lean IMT", () => {

expect(fun1).toThrow("Parameter 'hash' is not defined")
expect(fun2).toThrow("Parameter 'hash' is not a function")
expect(fun3).toThrow("Parameter 'leaves' is not an array")
expect(fun3).toThrow("Parameter 'leaves' is not an Array instance")
})

it("Should initialize a tree", () => {
Expand Down Expand Up @@ -162,7 +162,7 @@ describe("Lean IMT", () => {

const fun = () => tree.insertMany("uoe" as any)

expect(fun).toThrow("Parameter 'leaves' is not an array")
expect(fun).toThrow("Parameter 'leaves' is not an Array instance")
})

it(`Should not insert any leaf if the list of leaves is empty`, () => {
Expand Down Expand Up @@ -298,7 +298,7 @@ describe("Lean IMT", () => {

const fun = () => tree.verifyProof({ ...proof, siblings: "string" as any })

expect(fun).toThrow("Parameter 'proof.siblings' is not an array")
expect(fun).toThrow("Parameter 'proof.siblings' is not an Array instance")
})

it("Should not verify any proof if proof.index is not a number", () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/poseidon-cipher/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,6 @@
},
"dependencies": {
"@zk-kit/baby-jubjub": "1.0.0-beta",
"@zk-kit/utils": "1.0.0-beta"
"@zk-kit/utils": "1.0.0-beta.1"
}
}
2 changes: 1 addition & 1 deletion packages/utils/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@zk-kit/utils",
"version": "1.0.0-beta",
"version": "1.0.0-beta.1",
"description": "Essential zero-knowledge utility library for JavaScript developers.",
"type": "module",
"license": "MIT",
Expand Down
13 changes: 7 additions & 6 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3571,7 +3571,7 @@ __metadata:
"@rollup/plugin-node-resolve": "npm:^15.2.3"
"@rollup/plugin-terser": "npm:^0.4.4"
"@rollup/plugin-typescript": "npm:^11.1.6"
"@zk-kit/utils": "npm:1.0.0-beta"
"@zk-kit/utils": "npm:1.0.0-beta.1"
circomlibjs: "npm:0.0.8"
ffjavascript: "npm:0.2.38"
rimraf: "npm:^5.0.5"
Expand All @@ -3590,7 +3590,7 @@ __metadata:
"@zk-kit/eddsa-poseidon": "npm:0.11.0"
"@zk-kit/poseidon-cipher": "npm:0.3.0"
"@zk-kit/smt": "npm:1.0.0"
"@zk-kit/utils": "npm:1.0.0-beta"
"@zk-kit/utils": "npm:1.0.0-beta.1"
circomkit: "npm:0.0.24"
circomlib: "npm:^2.0.5"
mocha: "npm:^10.2.0"
Expand All @@ -3616,7 +3616,7 @@ __metadata:
"@rollup/plugin-terser": "npm:^0.4.4"
"@rollup/plugin-typescript": "npm:^11.1.6"
"@zk-kit/baby-jubjub": "npm:1.0.0-beta"
"@zk-kit/utils": "npm:1.0.0-beta"
"@zk-kit/utils": "npm:1.0.0-beta.1"
buffer: "npm:6.0.3"
circomlibjs: "npm:0.0.8"
ffjavascript: "npm:0.2.38"
Expand All @@ -3642,7 +3642,7 @@ __metadata:
"@types/snarkjs": "npm:^0"
"@types/tmp": "npm:^0.2.6"
"@zk-kit/eddsa-poseidon": "npm:0.11.0"
"@zk-kit/utils": "npm:1.0.0-beta"
"@zk-kit/utils": "npm:1.0.0-beta.1"
poseidon-lite: "npm:^0.2.0"
rimraf: "npm:^5.0.5"
rollup: "npm:^4.12.0"
Expand Down Expand Up @@ -3683,6 +3683,7 @@ __metadata:
dependencies:
"@rollup/plugin-terser": "npm:^0.4.4"
"@rollup/plugin-typescript": "npm:^11.1.6"
"@zk-kit/utils": "npm:1.0.0-beta.1"
incrementalquintree: "npm:^1.0.9"
poseidon-lite: "npm:^0.2.0"
rimraf: "npm:^5.0.5"
Expand Down Expand Up @@ -3730,7 +3731,7 @@ __metadata:
"@rollup/plugin-typescript": "npm:^11.1.6"
"@zk-kit/baby-jubjub": "npm:1.0.0-beta"
"@zk-kit/eddsa-poseidon": "npm:0.1.0"
"@zk-kit/utils": "npm:1.0.0-beta"
"@zk-kit/utils": "npm:1.0.0-beta.1"
circomlibjs: "npm:^0.0.8"
rimraf: "npm:^5.0.5"
rollup: "npm:^4.12.0"
Expand Down Expand Up @@ -3789,7 +3790,7 @@ __metadata:
languageName: unknown
linkType: soft

"@zk-kit/utils@npm:1.0.0-beta, @zk-kit/utils@workspace:packages/utils":
"@zk-kit/utils@npm:1.0.0-beta.1, @zk-kit/utils@workspace:packages/utils":
version: 0.0.0-use.local
resolution: "@zk-kit/utils@workspace:packages/utils"
dependencies:
Expand Down

0 comments on commit d5e0a94

Please sign in to comment.