mirror of
https://github.com/privacy-scaling-explorations/zk-kit.git
synced 2026-04-22 03:00:15 -04:00
feat: add update method
Former-commit-id: 1c16ddd7d3e563aa39124a6560eb3908db8d2539 [formerly 717dbaf7498b5a5e83512fd244add4adf8801ec7] [formerly bd57797a69750bbf9d0443868287b0182272b5cf [formerly e70da91bb78e42fdb2d30e7f09242e0bd7d65a45]] [formerly 2991b7e2919f588ad925be4c9669543c51475452 [formerly cbff397c329ff2376ac02c185e8eed7290d5046c] [formerly 62219ab43e4f0ef425f6aa84246b1bc21051473b [formerly 95e1cbda9a]]]
Former-commit-id: 4f0134d98027b080e34421dad9783c098e991e6b [formerly c3822f9a1bef29faff31ea04ad2f4ff66b964cc4] [formerly b22100da591642ff09be45130c94a98cc5256ab8 [formerly 3d8ce75a37bff810e9ad1aeb1728e24b30ab2985]]
Former-commit-id: 63b25944e154d83a279f413c335dabca92335a28 [formerly 03c7be7d87b81512887ab5bce3be9cb6c6e6b567]
Former-commit-id: b188074d2e4736a0ceded7db16174b7de3a2c734
This commit is contained in:
@@ -90,6 +90,12 @@ const tree = new IncrementalMerkleTree(poseidon, 16, BigInt(0), 2) // Binary tre
|
||||
tree.insert(BigInt(1))
|
||||
```
|
||||
|
||||
\# **update**(index: _number_, newLeaf: _Node_)
|
||||
|
||||
```typescript
|
||||
tree.update(0, BigInt(2))
|
||||
```
|
||||
|
||||
\# **delete**(index: _number_)
|
||||
|
||||
```typescript
|
||||
|
||||
@@ -130,6 +130,16 @@ export default class IncrementalMerkleTree {
|
||||
this._root = _update(index, this.zeroes[0], this.depth, this.arity, this._nodes, this.zeroes, this._hash)
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a leaf from the tree. It does not remove the leaf from
|
||||
* the data structure. It set the leaf to be deleted to a zero value.
|
||||
* @param index Index of the leaf to be deleted.
|
||||
* @param newLeaf New leaf value.
|
||||
*/
|
||||
public update(index: number, newLeaf: Node) {
|
||||
this._root = _update(index, newLeaf, this.depth, this.arity, this._nodes, this.zeroes, this._hash)
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a proof of membership.
|
||||
* @param index Index of the proof's leaf.
|
||||
|
||||
@@ -89,6 +89,22 @@ describe("Incremental Merkle Tree", () => {
|
||||
}
|
||||
})
|
||||
|
||||
it(`Should update ${numberOfLeaves} leaves`, () => {
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
tree.insert(BigInt(1))
|
||||
oldTree.insert(BigInt(1))
|
||||
}
|
||||
|
||||
for (let i = 0; i < numberOfLeaves; i += 1) {
|
||||
tree.update(i, BigInt(0))
|
||||
oldTree.update(i, BigInt(0))
|
||||
|
||||
const { root } = oldTree.genMerklePath(0)
|
||||
|
||||
expect(tree.root).toEqual(root)
|
||||
}
|
||||
})
|
||||
|
||||
it("Should return the index of a leaf", () => {
|
||||
tree.insert(BigInt(1))
|
||||
tree.insert(BigInt(2))
|
||||
|
||||
Reference in New Issue
Block a user