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

commit d328b9729948c585d38f3842c47ae5712e9fc0f6
parent f49cc0c4d13d6b4a84a4f3bfd1a7bee2d1b14023
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 21:02:20 +0400

bench: occupied-slot fixtures

Adds a key absent from the 1024-leaf fixture tree whose slot is held
by one of its leaves, and benchmarks inserting it and proving its
exclusion: the cases in which a compacted leaf's path is rehashed.

Diffstat:
Mbench/Fixtures.hs | 30+++++++++++++++++++++++++-----
Mbench/Main.hs | 4++++
Mbench/Weight.hs | 2++
3 files changed, 31 insertions(+), 5 deletions(-)

diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs @@ -1,4 +1,5 @@ {-# OPTIONS_GHC -fno-warn-orphans #-} +{-# LANGUAGE BangPatterns #-} module Fixtures ( Fixtures(..) @@ -10,9 +11,11 @@ module Fixtures ( import Control.DeepSeq import Control.Monad (foldM) import qualified Crypto.Hash.SHA256 as SHA256 +import Data.Bits (bit, testBit, xor) import qualified Data.ByteString as BS import qualified Data.ByteString.Char8 as B8 import Data.MSSMT +import qualified Data.MSSMT.Internal as I import Data.Word (Word64) -- all fields are strict, so WHNF is NF @@ -28,8 +31,9 @@ instance NFData Tree where instance NFData Proof where rnf (Proof ps) = rnf ps --- | A tree of 1024 leaves, its root, a leaf in it, a key not in it, --- and their proofs. +-- | A tree of 1024 leaves, its root, a leaf in it, a key not in it +-- whose slot is empty, their proofs, and a key not in it whose slot +-- the leaf occupies. data Fixtures = Fixtures { f_tree :: !Tree , f_root :: !Commitment @@ -39,18 +43,34 @@ data Fixtures = Fixtures { , f_proof :: !Proof , f_out :: !Key , f_xproof :: !Proof + , f_near :: !Key } instance NFData Fixtures where - rnf (Fixtures t r k v s p o x) = + rnf (Fixtures t r k v s p o x n) = rnf t `seq` rnf r `seq` rnf k `seq` rnf v `seq` rnf s - `seq` rnf p `seq` rnf o `seq` rnf x + `seq` rnf p `seq` rnf o `seq` rnf x `seq` rnf n fixtures :: Fixtures -fixtures = Fixtures t (root t) k v s (prove k t) o (prove o t) where +fixtures = Fixtures t (root t) k v s (prove k t) o (prove o t) n where t = build (leaves 1024) (k, v, s) = leaf_of 1 o = key_of 0 + n = near k t + +-- a key whose path ends at the given key's leaf, diverging from it just +-- beneath: the given key, with the bit beneath its leaf flipped +near :: Key -> Tree -> Key +near (I.Key bs) = flip_at . go 0 where + go !d t = case t of + I.Node l _ r -> go (d + 1) (if kbit d then r else l) + I.Leaf {} -> d + I.Empty -> d + kbit d = testBit (BS.index bs (d `quot` 8)) (d `rem` 8) + flip_at d = case BS.splitAt (d `quot` 8) bs of + (a, b) | Just (w, c) <- BS.uncons b + , Just n <- key (a <> BS.cons (w `xor` bit (d `rem` 8)) c) -> n + _ -> error "bang" -- | n distinct leaves. leaves :: Int -> [(Key, BS.ByteString, Word64)] diff --git a/bench/Main.hs b/bench/Main.hs @@ -24,6 +24,8 @@ bench_insert :: Fixtures -> Benchmark bench_insert ~Fixtures {..} = bgroup "insert" [ bench "empty tree" $ nf (insert f_out "v" 1) empty , bench "1024 leaves, fresh key" $ nf (insert f_out "v" 1) f_tree + , bench "1024 leaves, fresh key in a leaf's slot" $ + nf (insert f_near "v" 1) f_tree , bench "1024 leaves, replace" $ nf (insert f_key "v" 1) f_tree ] @@ -43,6 +45,8 @@ bench_prove :: Fixtures -> Benchmark bench_prove ~Fixtures {..} = bgroup "prove" [ bench "1024 leaves, inclusion" $ nf (prove f_key) f_tree , bench "1024 leaves, exclusion" $ nf (prove f_out) f_tree + , bench "1024 leaves, exclusion in a leaf's slot" $ + nf (prove f_near) f_tree ] bench_verify :: Fixtures -> Benchmark diff --git a/bench/Weight.hs b/bench/Weight.hs @@ -27,6 +27,7 @@ insert_ :: Fixtures -> Weigh () insert_ Fixtures {..} = wgroup "insert" $ do func "empty tree" (insert f_out "v" 1) empty func "1024 leaves, fresh key" (insert f_out "v" 1) f_tree + func "1024 leaves, fresh key in a leaf's slot" (insert f_near "v" 1) f_tree func "1024 leaves, replace" (insert f_key "v" 1) f_tree delete_ :: Fixtures -> Weigh () @@ -43,6 +44,7 @@ prove_ :: Fixtures -> Weigh () prove_ Fixtures {..} = wgroup "prove" $ do func "1024 leaves, inclusion" (prove f_key) f_tree func "1024 leaves, exclusion" (prove f_out) f_tree + func "1024 leaves, exclusion in a leaf's slot" (prove f_near) f_tree verify_ :: Fixtures -> Weigh () verify_ Fixtures {..} = wgroup "verify" $ do