merkle-t
Version:
Merkle Tree with Poseidon Hash in TypeScript
62 lines (44 loc) • 1.58 kB
Markdown
# Merkle Tree by Poseidon Hash with Typescript
[](https://github.com/inverse-technology/merkle-tree-ts/blob/master/LICENSE)
[](https://www.typescriptlang.org)  [](https://github.com/inverse-technology/merkle-tree-ts/actions/workflows/index.yml)
A merkle tree implementation with poseidon hash and circom circuit.
**Use at your own risk**.
## Install
```shell
$ npm i merkle-t
```
## Usage
```ts
import { MerkleTree, LeafInputs, Leaf } from "merkle-t";
import { poseidon, randomFieldElement } from "poseidon-h";
class MyLeaf implements LeafInputs {
private inputs: [bigint, bigint, bigint];
constructor(inputs: [bigint, bigint, bigint]) {
this.inputs = inputs;
}
hash(): Leaf {
return poseidon(this.inputs);
}
zeroHash(): Leaf {
return poseidon(this.inputs.map(() => BigInt(0)));
}
toInputs(): bigint[] {
return this.inputs;
}
}
const leaves = Array.from({ length: n }, () =>
new TestLeaf([
randomFieldElement(),
randomFieldElement(),
randomFieldElement(),
]).hash(),
);
const merkleTree = new MerkleTree(leaves, 3);
const proof = merkleTree.prove(leaves[13]);
const isValid = merkleTree.verify(proof);
expect(isValid).toBe(true);
```
## Test
```shell
$ yarn test
```