mssmt

A high-performance Merkle-Sum Sparse Merkle Tree.
git clone git://git.jtobin.io/mssmt.git
Log | Files | Refs | README | LICENSE | git.jtobin.io

README.md (2588B)


      1 # mssmt
      2 
      3 [![](https://img.shields.io/hackage/v/mssmt?color=blue)](https://hackage.haskell.org/package/mssmt)
      4 ![](https://img.shields.io/badge/license-MIT-brightgreen)
      5 
      6 A high-performance, in-memory Merkle-Sum Sparse Merkle Tree (MS-SMT).
      7 
      8 An MS-SMT maps 256-bit keys to values carrying sums. Its root commits
      9 to every leaf and to the total of their sums, and it can prove any
     10 key's inclusion or exclusion. There are two kinds, each with its own
     11 tree type:
     12 
     13 * `Data.MSSMT.Split`, re-exported by `Data.MSSMT`: each branch commits
     14   to the hash and sum of each of its children, and a subtree holding a
     15   single leaf commits to that leaf directly, so that paths and proofs
     16   run only as deep as is needed to set a key apart.
     17 
     18 * `Data.MSSMT.Aggregate`: each branch commits to its children's hashes
     19   and the sum of their sums, at a fixed depth of 256.
     20 
     21 ## Usage
     22 
     23 A sample GHCi session:
     24 
     25 ```
     26   > :set -XOverloadedStrings
     27   >
     28   > import qualified Crypto.Hash.SHA256 as SHA256
     29   > import qualified Data.MSSMT as M
     30   >
     31   > -- keys are 32 bytes
     32   > let Just alice = M.key (SHA256.hash "alice")
     33   > let Just bob = M.key (SHA256.hash "bob")
     34   >
     35   > -- insert a value and sum at a key
     36   > let Just t = M.insert alice "alice's leaf" 100 M.empty
     37   > M.lookup alice t
     38   Just ("alice's leaf",100)
     39   >
     40   > -- the root commits to every leaf, and to the total of their sums
     41   > let M.Commitment _ total = M.root t
     42   > total
     43   100
     44   >
     45   > -- prove a key's inclusion, or, via an empty value with zero sum,
     46   > -- its exclusion
     47   > M.verify alice "alice's leaf" 100 (M.prove alice t) (M.root t)
     48   True
     49   > M.verify bob mempty 0 (M.prove bob t) (M.root t)
     50   True
     51   > M.verify alice "alice's leaf" 99 (M.prove alice t) (M.root t)
     52   False
     53 ```
     54 
     55 ## Performance
     56 
     57 Hashing uses hardware SHA-256 instructions wherever the CPU supports
     58 them, via [ppad-sha256][sha256]. On a split tree of 1024 leaves, on an
     59 Apple M4, operations take roughly:
     60 
     61 ```
     62   insert    0.9 μs
     63   delete    0.6 μs
     64   lookup     17 ns
     65   prove      60 ns
     66   verify   0.75 μs
     67 ```
     68 
     69 and the tree holds about 190 bytes per leaf. Use `cabal bench` to run
     70 the benchmark suites. You should compile with the 'llvm' flag for
     71 maximum performance.
     72 
     73 ## Development
     74 
     75 You'll require [Nix][nixos] with [flake][flake] support enabled. Enter a
     76 development shell with:
     77 
     78 ```
     79 $ nix develop
     80 ```
     81 
     82 Then do e.g.:
     83 
     84 ```
     85 $ cabal repl mssmt
     86 ```
     87 
     88 to get a REPL for the main library, or `cabal test` to run the test
     89 suite.
     90 
     91 [sha256]: https://github.com/ppad-tech/sha256
     92 [nixos]: https://nixos.org/
     93 [flake]: https://nixos.org/manual/nix/unstable/command-ref/new-cli/nix3-flake.html