mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
refactor: update pkg name
This commit is contained in:
@@ -1,8 +1,8 @@
|
||||
<p align="center">
|
||||
<h1 align="center">
|
||||
Merkle Tree
|
||||
Incremental Merkle Tree
|
||||
</h1>
|
||||
<p align="center">Merkle tree implementation in TypeScript.</p>
|
||||
<p align="center">Incremental Merkle tree implementation in TypeScript.</p>
|
||||
</p>
|
||||
|
||||
<p align="center">
|
||||
@@ -12,14 +12,14 @@
|
||||
<a href="https://github.com/appliedzkp/zk-kit/blob/main/LICENSE">
|
||||
<img alt="Github license" src="https://img.shields.io/github/license/appliedzkp/zk-kit.svg?style=flat-square">
|
||||
</a>
|
||||
<a href="https://www.npmjs.com/package/@zk-kit/merkle-tree">
|
||||
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/merkle-tree?style=flat-square" />
|
||||
<a href="https://www.npmjs.com/package/@zk-kit/incremental-merkle-tree">
|
||||
<img alt="NPM version" src="https://img.shields.io/npm/v/@zk-kit/incremental-merkle-tree?style=flat-square" />
|
||||
</a>
|
||||
<a href="https://npmjs.org/package/@zk-kit/merkle-tree">
|
||||
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/merkle-tree.svg?style=flat-square" />
|
||||
<a href="https://npmjs.org/package/@zk-kit/incremental-merkle-tree">
|
||||
<img alt="Downloads" src="https://img.shields.io/npm/dm/@zk-kit/incremental-merkle-tree.svg?style=flat-square" />
|
||||
</a>
|
||||
<a href="https://bundlephobia.com/package/@zk-kit/merkle-tree">
|
||||
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/merkle-tree" />
|
||||
<a href="https://bundlephobia.com/package/@zk-kit/incremental-merkle-tree">
|
||||
<img alt="npm bundle size (scoped)" src="https://img.shields.io/bundlephobia/minzip/@zk-kit/incremental-merkle-tree" />
|
||||
</a>
|
||||
<a href="https://eslint.org/">
|
||||
<img alt="Linter eslint" src="https://img.shields.io/badge/linter-eslint-8080f2?style=flat-square&logo=eslint" />
|
||||
@@ -35,16 +35,16 @@
|
||||
|
||||
### npm or yarn
|
||||
|
||||
Install the `@zk-kit/merkle-tree` package with npm:
|
||||
Install the `@zk-kit/incremental-merkle-tree` package with npm:
|
||||
|
||||
```bash
|
||||
npm i @zk-kit/merkle-tree --save
|
||||
npm i @zk-kit/incremental-merkle-tree --save
|
||||
```
|
||||
|
||||
or yarn:
|
||||
|
||||
```bash
|
||||
yarn add @zk-kit/merkle-tree
|
||||
yarn add @zk-kit/incremental-merkle-tree
|
||||
```
|
||||
|
||||
### CDN
|
||||
@@ -52,13 +52,13 @@ yarn add @zk-kit/merkle-tree
|
||||
You can also load it using a `script` tag using [unpkg](https://unpkg.com/):
|
||||
|
||||
```html
|
||||
<script src="https://unpkg.com/@zk-kit/merkle-tree/"></script>
|
||||
<script src="https://unpkg.com/@zk-kit/incremental-merkle-tree/"></script>
|
||||
```
|
||||
|
||||
or [JSDelivr](https://www.jsdelivr.com/):
|
||||
|
||||
```html
|
||||
<script src="https://cdn.jsdelivr.net/npm/@zk-kit/merkle-tree/"></script>
|
||||
<script src="https://cdn.jsdelivr.net/npm/@zk-kit/incremental-merkle-tree/"></script>
|
||||
```
|
||||
|
||||
## 📜 Usage
|
||||
@@ -66,7 +66,7 @@ or [JSDelivr](https://www.jsdelivr.com/):
|
||||
\# **new MerkleTree**(hash: _HashFunction_, depth: _number_, zero: _Node_): _MerkleTree_
|
||||
|
||||
```typescript
|
||||
import { MerkleTree } from "@zk-kit/merkle-tree"
|
||||
import { MerkleTree } from "@zk-kit/incremental-merkle-tree"
|
||||
import { poseidon } from "circomlibjs" // v0.0.8
|
||||
|
||||
const tree = new MerkleTree(poseidon, 16, BigInt(0))
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
{
|
||||
"displayName": "merkle-tree",
|
||||
"displayName": "incremental-merkle-tree",
|
||||
"transform": {
|
||||
"\\.ts$": "ts-jest"
|
||||
}
|
||||
|
||||
42978
packages/incremental-merkle-tree/package-lock.json
generated
42978
packages/incremental-merkle-tree/package-lock.json
generated
File diff suppressed because it is too large
Load Diff
@@ -1,7 +1,7 @@
|
||||
{
|
||||
"name": "@zk-kit/merkle-tree",
|
||||
"name": "@zk-kit/incremental-merkle-tree",
|
||||
"version": "0.1.0",
|
||||
"description": "Merkle tree implementation in TypeScript.",
|
||||
"description": "Incremental Merkle tree implementation in TypeScript.",
|
||||
"iife": "dist/index.js",
|
||||
"unpkg": "dist/index.min.js",
|
||||
"jsdelivr": "dist/index.min.js",
|
||||
@@ -18,19 +18,13 @@
|
||||
"README.md"
|
||||
],
|
||||
"repository": "git@github.com:appliedzkp/zk-kit.git",
|
||||
"homepage": "https://github.com/appliedzkp/zk-kit/tree/main/packages/merkle-tree",
|
||||
"homepage": "https://github.com/appliedzkp/zk-kit/tree/main/packages/incremental-merkle-tree",
|
||||
"author": {
|
||||
"name": "Omar Desogus",
|
||||
"email": "me@cedoor.dev",
|
||||
"url": "https://cedoor.dev"
|
||||
},
|
||||
"license": "MIT",
|
||||
"scripts": {
|
||||
"benchmark": "node -r ts-node/register benchmarks/index.ts",
|
||||
"build:watch": "rollup -c rollup.config.ts -w --configPlugin typescript",
|
||||
"build": "rimraf dist && rollup -c rollup.config.ts --configPlugin typescript",
|
||||
"prepublishOnly": "yarn build"
|
||||
},
|
||||
"publishConfig": {
|
||||
"access": "public"
|
||||
},
|
||||
|
||||
@@ -1,6 +1,13 @@
|
||||
import checkParameter from "./checkParameter"
|
||||
import { HashFunction, Proof, Node } from "./types"
|
||||
|
||||
/**
|
||||
* A Merkle tree is a tree in which every leaf node is labelled with the cryptographic hash of a
|
||||
* data block, and every non-leaf node is labelled with the cryptographic hash of the labels of its child nodes.
|
||||
* It allows efficient and secure verification of the contents of large data structures.
|
||||
* The MerkleTree class is a TypeScript implementation of Merkle tree and it provides all the functions to create
|
||||
* efficient trees and to generate and verify proofs of membership.
|
||||
*/
|
||||
export default class IncrementalMerkleTree {
|
||||
static readonly maxDepth = 32
|
||||
|
||||
@@ -11,10 +18,18 @@ export default class IncrementalMerkleTree {
|
||||
protected readonly _depth: number
|
||||
protected readonly _arity: number
|
||||
|
||||
/**
|
||||
* Initializes the Merkle tree with the hash function, the depth and the zero value to use for zeroes.
|
||||
* @param hash Hash function.
|
||||
* @param depth Tree depth.
|
||||
* @param zeroValue Zero values for zeroes.
|
||||
* @param arity The number of children for each node.
|
||||
*/
|
||||
constructor(hash: HashFunction, depth: number, zeroValue: Node, arity: number) {
|
||||
checkParameter(hash, "hash", "function")
|
||||
checkParameter(depth, "depth", "number")
|
||||
checkParameter(zeroValue, "zeroValue", "number", "string", "bigint")
|
||||
checkParameter(arity, "arity", "number")
|
||||
|
||||
if (depth < 1 || depth > IncrementalMerkleTree.maxDepth) {
|
||||
throw new Error("The tree depth must be between 1 and 32")
|
||||
|
||||
@@ -6,5 +6,5 @@ export type Proof = {
|
||||
root: Node
|
||||
leaf: Node
|
||||
siblingNodes: Node[]
|
||||
path: Array<number>
|
||||
path: number[]
|
||||
}
|
||||
|
||||
@@ -1,9 +1,10 @@
|
||||
import { poseidon } from "circomlibjs"
|
||||
import { IncrementalMerkleTree } from "../src"
|
||||
|
||||
describe("Nary Merkle Tree", () => {
|
||||
describe("Incremental Merkle Tree", () => {
|
||||
const depth = 20
|
||||
const arity = 5
|
||||
|
||||
let tree: IncrementalMerkleTree
|
||||
|
||||
describe("Merkle Tree class", () => {
|
||||
@@ -44,18 +45,18 @@ describe("Nary Merkle Tree", () => {
|
||||
|
||||
expect(() => fullTree.insert(BigInt(4))).toThrow("The tree is full")
|
||||
})
|
||||
})
|
||||
|
||||
it("Should create a valid proof", () => {
|
||||
const numberOfLeaves = 50
|
||||
it("Should create a valid proof", () => {
|
||||
const numberOfLeaves = 50
|
||||
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
tree.insert(BigInt(i + 1))
|
||||
}
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
tree.insert(BigInt(i + 1))
|
||||
}
|
||||
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
const proof = tree.createProof(i)
|
||||
expect(tree.verifyProof(proof)).toBeTruthy()
|
||||
}
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
const proof = tree.createProof(i)
|
||||
expect(tree.verifyProof(proof)).toBeTruthy()
|
||||
}
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
Reference in New Issue
Block a user