Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/kevincharm/smt_fe
https://github.com/kevincharm/smt_fe
Last synced: about 4 hours ago
JSON representation
- Host: GitHub
- URL: https://github.com/kevincharm/smt_fe
- Owner: kevincharm
- Created: 2023-11-18T11:29:04.000Z (12 months ago)
- Default Branch: master
- Last Pushed: 2023-11-18T12:13:33.000Z (12 months ago)
- Last Synced: 2024-04-14T13:46:01.820Z (7 months ago)
- Size: 1.95 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Efficient Sparse Merkle Tree Library for Fe
An optimised implementation of a Sparse Merkle Tree written in Fe that omits empty subtrees when computing roots. Empty subtrees are assumed to have a special hash value of zero. See [post on ethresear.ch](https://ethresear.ch/t/optimizing-sparse-merkle-trees/3751) for the original idea proposed by Vitalik.
## How to generate insert & update proofs
Below is an example JavaScript snippet that instantiates an SMT and generates proofs that can be used with this Fe lib.
```sh
npm install --save @kevincharm/sparse-merkle-tree @noble/hashes
``````ts
import { SparseMerkleTreeKV } from "@kevincharm/sparse-merkle-tree";
import { ZeroHash, concat, hashMessage, Wallet, Contract } from "ethers";// Initialise client representation of an empty SMT with depth=160
const smt = new SparseMerkleTree(160);// Insert a new (K,V) entry
const index = BigInt(Wallet.createRandom().address);
const value = hashMessage("Fred Fredburger");// Get SMT proof of insertion
const {
newLeaf,
leaf: oldLeaf
index,
enables,
siblings,
} = smt.insert(index, value);
```Then once we have these values, we can compute some roots:
```fe
use smt_fe::compute_address_smt_rootcontract Main {
root: u256pub fn main(
mut self,
newLeaf: u256,
oldLeaf: u256,
index: address,
enables: u256,
siblings: Array
) {
let current_root: u256 = compute_address_smt_root(
leaf: oldLeaf,
index,
enables,
siblings
)
assert self.root = current_rootlet new_root: u256 = compute_address_smt_root(
leaf: newLeaf,
index,
enables,
siblings
)
self.root = new_root
}
}
```