Merkle Mountain Ranges
An implementation of Merkle Mountain Ranges in TypeScript using the Pedersen hashing function.
Note: for optimization reasons, the Pedersen hashing function used here is derived from Rust via WASM, thanks to the work of Geometry.
If you would like to know why a Merkle Mountain Range data structure could be potentially beneficial to you, read our article and Twitter thread.
This can be used in-pair with an on-chain contract to prove a tree's integrity.
An example of such contract can be found here(Starknet/Cairo).
Installing the package
$> yarn add merkle-mountain-ranges
RAM (in-memory) example
import { InMemoryMMR } from "merkle-mountain-ranges";
async function main() {
const mmr = new InMemoryMMR();
mmr.append("1");
const proof = await mmr.getProof(1);
mmr.verifyProof(proof);
}
main().catch(console.error);
Redis usage example
import { RedisMMR } from 'merkle-mountain-ranges';
const mmrConfig = {
withRootHash: true,
};
async function main() {
const mmr = new RedisMMR(mmrConfig);
await mmr.init();
console.log('Tree uuid', mmr.uuid);
const leaves = 11;
for (let idx = 0; idx < leaves; ++idx) {
await mmr.append((idx + 1).toString());
}
const proof = await mmr.getProof(9);
console.log('Inclusion proof of leaf no.9', proof);
await mmr.verifyProof(proof);
console.log('Valid proof!');
await mmr.disconnectDb();
}
main().catch(console.error);
RocksDB usage example
import { RocksDBMMR } from 'merkle-mountain-ranges';
const mmrConfig = {
withRootHash: true,
location: './rocksdb-db',
};
async function main() {
const mmr = new RocksDBMMR(mmrConfig);
await mmr.init();
}
Running tests
$> npx ts-mocha test/ram/*.ts
$> npx ts-mocha test/redis/*.ts
$> npx ts-mocha test/rocksdb/*.ts
$> yarn test
2023 - Herodotus Dev Ltd
License
GNU GPLv3