npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

optimisedmt

v0.0.9

Published

An incremental Merkle tree with optimsed updates

Downloads

96

Readme

Optimised Incremental Merkle Tree

The problem

Ordinary incremental Merkle tree implementations have inefficient update operations. Every time a leaf is updated, the entire tree is recomputed by inserting each leaf all over again.

The solution

Trade off storage/memory for speed.

Represent nodes as a key-value map where the key is the node index and the value is the node.

Use a simple key-value map to store data. This may be on disk or in memory. There should be plenty of mature and fast options like LMDB.

Node index The node index is the index of a node array which represents the Merkle tree.

For example, a binary Merkle tree with 2 levels and leaves a, b, c, d can be represented with this node array:

[a, b, c, d, h(a, b), h(c, d), root]

where h() is a hash function.

a is at index 0.

h(a, b) is at index 4.

Zero values

Upon initialisation of the tree, we compute the zero values. This is the empty node per level.

[z, h(z, z), h(h(z, z), h(z, z))]

If c and d are empty leaves, then the nodes can be represented as such:

{
    0: a,
    1: b,
    4: h(a, b),
    6: root = h(h(a, b), h(z, z))
}

There is no need to store the values of items at indices 2, 3, and 5, since those are precalculated zero values.

Computing Merkle proofs

Example 1: there are 4 leaves in a binary MT and all leaves are non-zero. The nodes array is:

[a, b, c, d, h(a, b), h(c, d), root]

The representation of the node array as a key-value map is as such:

{
    0: a,
    1: b,
    2: c,
    3: d,
    4: h(a, b),
    5: h(c, d),
    6: root = h(h(a, b), h(c, d))
}

We want to compute the Merkle proof of leaf 2 (whose value is c).

First, compute the path indices.

let r = Math.floor(index / arity)
indices = []
for i in range(0, levels):
    p = r % arity // e.g. p % 2 for a binary tree
    indices.push(p)
    r = Math.floor(r /leavesPerNode)

When index == 2, indices == [1, 0].

Next, compute the path elements.


pathElements = []
j = 1
for i in indices:
    e = nodeArray[j * arity + i]
    pathElements.push(e)
    j ++

As such:

pathElements = [
    nodeArray[1 * 2 + 1 = 3] = d,
    nodeArray[2 * 2 + 0 = 4] = h(a, b),
]

To verify the Merkle path:

root == h(d, h(a, b))

Updates

To perform an update, only depth - 1 hashes have to be computed.

Consider again this binary Merkle tree with 4 leaves:

[a, b, c, d, h(a, b), h(c, d), h(...)]

To update the leaf at index 2 (whose value is c), only 2 hash operations are needed:

[a, b, c*, d, h(a, b), h(c*, d), h(...)*]