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 88f312c1bd1f5074783f2dff864eaed30cd8b5d3
parent d23eb4112bc87f46d8c68278f691c1156deda52e
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 21:59:19 +0400

lib: fewer allocations in insert, delete, and verify

A split leaf's hash is now computed over the state and block arrays
that insert and verification already hold, via hash_leaf_split#,
rather than over its own. insert's overflow check and delete's
presence check walk to the leaf without building lookup's result, and
the public insert wrappers coerce rather than fmap their result.

Per operation on the 1024-leaf fixture: insert ~1.7-2.0KB, down from
~1.9-2.1KB; delete ~1.1KB, down from ~1.15KB; verify ~0.7KB, down
from ~0.78KB.

Diffstat:
Mlib/Data/MSSMT/Aggregate.hs | 3++-
Mlib/Data/MSSMT/Hash.hs | 11+++++++++++
Mlib/Data/MSSMT/Internal.hs | 115++++++++++++++++++++++++++++++++++++++++++-------------------------------------
Mlib/Data/MSSMT/Split.hs | 3++-
4 files changed, 76 insertions(+), 56 deletions(-)

diff --git a/lib/Data/MSSMT/Aggregate.hs b/lib/Data/MSSMT/Aggregate.hs @@ -43,6 +43,7 @@ module Data.MSSMT.Aggregate ( ) where import qualified Data.ByteString as BS +import Data.Coerce (coerce) import Data.MSSMT.Internal ( Commitment(..), Hash, Key, Scheme(..), key, parse_hash , serialize_hash @@ -64,7 +65,7 @@ empty = Tree I.empty -- -- Returns 'Nothing' if the tree's sum would overflow. insert :: Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree -insert k v s (Tree t) = fmap Tree (I.insert_with Aggregate k v s t) +insert k v s (Tree t) = coerce (I.insert_with Aggregate k v s t) -- | Delete the leaf at a key. delete :: Key -> Tree -> Tree diff --git a/lib/Data/MSSMT/Hash.hs b/lib/Data/MSSMT/Hash.hs @@ -39,6 +39,7 @@ module Data.MSSMT.Hash ( , Buffer , with_scratch , hash_branch# + , hash_leaf_split# ) where import qualified Crypto.Hash.SHA256 as SHA256 @@ -346,3 +347,13 @@ hash_branch# sc st bk l sl r sr s0 Aggregate -> hash_node_aggregate# st bk l r (sl + sr) s0 | otherwise = (# s0, hash_branch sc l sl r sr #) {-# INLINE hash_branch# #-} + +-- | 'hash_leaf_split', over the given state and block arrays where +-- hardware SHA-256 is available. +hash_leaf_split# + :: Buffer -> Buffer -> Hash -> Hash -> Word64 + -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Hash #) +hash_leaf_split# st bk k vh s s0 + | native = hash_node_aggregate# st bk k vh s s0 + | otherwise = (# s0, hash_leaf_split k vh s #) +{-# INLINE hash_leaf_split# #-} diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs @@ -139,10 +139,25 @@ agree k k' d = all (\j -> bit k j == bit k' j) [0 .. d - 1] key_hash :: Key -> Hash key_hash (Key k) = unsafe_hash k --- a leaf's hash under a scheme -hash_leaf_with :: Scheme -> Key -> BS.ByteString -> Word64 -> Hash -hash_leaf_with Split k v s = hash_leaf_split (key_hash k) (hash_value v) s -hash_leaf_with Aggregate _ v s = hash_leaf_aggregate v s +-- a leaf's hash under a scheme, over the given state and block arrays +hash_leaf# + :: Scheme -> Buffer -> Buffer -> Key -> BS.ByteString -> Word64 + -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Hash #) +hash_leaf# Split st bk k v s = + hash_leaf_split# st bk (key_hash k) (hash_value v) s +hash_leaf# Aggregate _ _ _ v s = \s0 -> (# s0, hash_leaf_aggregate v s #) +{-# INLINE hash_leaf# #-} + +-- the leaf at a key, if there's one, passed its sum and value +with_leaf :: Key -> Tree -> r -> (Word64 -> BS.ByteString -> r) -> r +with_leaf k t0 none some = go 0 t0 where + go !d t = case t of + Empty -> none + Leaf (Commitment _ s) _ k' v + | k' == k -> some s v + | otherwise -> none + Node l _ r -> go (d + 1) (if bit k d then r else l) +{-# INLINE with_leaf #-} -- the first depth, at or below the given one, at which two keys' paths -- diverge; callers ensure the keys are distinct, and agree above it @@ -208,11 +223,11 @@ lift# lift# sc st bk d c lh k v s0 = case raise# sc st bk k d (d + 1) c s0 of (# s1, c' #) -> let !t = Leaf c' lh k v in (# s1, t #) --- insert a leaf, given its hash, rehashing the path to it over one pair +-- insert a leaf, hashing it and rehashing the path to it over one pair -- of state and block arrays -insert_leaf - :: Scheme -> Key -> Hash -> Word64 -> BS.ByteString -> Tree -> Tree -insert_leaf sc k lh s v t0 = with_scratch $ \st bk -> +insert_leaf :: Scheme -> Key -> BS.ByteString -> Word64 -> Tree -> Tree +insert_leaf sc k v s t0 = with_scratch $ \st bk s00 -> + case hash_leaf# sc st bk k v s s00 of { (# s01, lh #) -> let go !d t s0 = case t of Empty -> leaf# sc st bk d k lh s v s0 Leaf (Commitment _ s') lh' k' v' @@ -243,7 +258,7 @@ insert_leaf sc k lh s v t0 = with_scratch $ \st bk -> (# s1, r' #) -> node# sc st bk d l r' s1 | otherwise -> case go (d + 1) l s0 of (# s1, l' #) -> node# sc st bk d l' r s1 - in go 0 t0 + in go 0 t0 s01 } -- tree ----------------------------------------------------------------------- @@ -261,15 +276,13 @@ insert_with sc k v s t | s > maxBound - rest = Nothing -- an empty value with zero sum is the empty leaf | BS.null v && s == 0 = Just (delete_with sc k t) - | otherwise = Just (insert_leaf sc k (hash_leaf_with sc k v s) s v t) + | otherwise = Just (insert_leaf sc k v s t) where - rest = total t - maybe 0 snd (lookup k t) + rest = total t - with_leaf k t 0 const -- | Delete the leaf at a key under a scheme. delete_with :: Scheme -> Key -> Tree -> Tree -delete_with sc k t0 = case lookup k t0 of - Nothing -> t0 - Just _ -> with_scratch $ \st bk -> +delete_with sc k t0 = with_leaf k t0 t0 $ \_ _ -> with_scratch $ \st bk -> let go !d t s0 = case t of -- the key is present, so a leaf on its path is its own Empty -> (# s0, Empty #) @@ -283,13 +296,7 @@ delete_with sc k t0 = case lookup k t0 of -- | Look up the value and sum at a key. lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) -lookup k = go 0 where - go !d t = case t of - Empty -> Nothing - Leaf (Commitment _ s) _ k' v - | k' == k -> Just (v, s) - | otherwise -> Nothing - Node l _ r -> go (d + 1) (if bit k d then r else l) +lookup k t = with_leaf k t Nothing (\s v -> Just (v, s)) -- | The tree's root commitment under a scheme. root_with :: Scheme -> Tree -> Commitment @@ -302,24 +309,24 @@ root_with sc = commit sc 0 data Neighbor = Neighbor !Key {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 deriving (Eq, Show) --- the root above a commitment at the given depth on a key's path, given --- that many siblings, from the deepest up; 'Nothing' if a branch's sum --- overflows. callers ensure the depth is at most 256 -climb - :: Scheme -> Key -> Int -> Commitment -> [Commitment] -> Maybe Commitment -climb sc k d0 (Commitment h0 n0) sibs = with_scratch $ \st bk -> - let go !d !h !hs ps s0 = case ps of - [] - | d == -1 -> (# s0, Just (Commitment h hs) #) - | otherwise -> (# s0, Nothing #) - Commitment g gs : rest - | d < 0 || gs > maxBound - hs -> (# s0, Nothing #) - | otherwise -> - case if bit k d - then hash_branch# sc st bk g gs h hs s0 - else hash_branch# sc st bk h hs g gs s0 of - (# s1, p #) -> go (d - 1) p (hs + gs) rest s1 - in go (d0 - 1) h0 n0 sibs +-- whether climbing from a commitment at the given depth on a key's +-- path, over that many siblings from the deepest up, reaches a root, +-- hashing over the given state and block arrays; a branch's sum mustn't +-- overflow. callers ensure the depth is at most 256 +climb# + :: Scheme -> Buffer -> Buffer -> Key -> Int -> Commitment + -> [Commitment] -> Commitment + -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Bool #) +climb# sc st bk k d0 (Commitment h0 n0) sibs r = go (d0 - 1) h0 n0 sibs where + go !d !h !hs ps s0 = case ps of + [] -> (# s0, d == -1 && Commitment h hs == r #) + Commitment g gs : rest + | d < 0 || gs > maxBound - hs -> (# s0, False #) + | otherwise -> + case if bit k d + then hash_branch# sc st bk g gs h hs s0 + else hash_branch# sc st bk h hs g gs s0 of + (# s1, p #) -> go (d - 1) p (hs + gs) rest s1 -- | The siblings along a key's path in a split tree, from the deepest up -- to the root's, and the leaf the path ends at, where that's another @@ -346,20 +353,20 @@ prove_split k = go 0 [] where verify_split :: Key -> BS.ByteString -> Word64 -> Maybe Neighbor -> [Commitment] -> Commitment -> Bool -verify_split k v s nb sibs r = d <= _HEIGHT && case start of - Nothing -> False - Just c -> climb Split k d c sibs == Just r - where - d = length sibs - vacant = BS.null v && s == 0 - start = case nb of - Nothing - | vacant -> Just (commit Split d Empty) - | otherwise -> Just (Commitment (hash_leaf_with Split k v s) s) - Just (Neighbor k' h s') - | vacant && k' /= k && agree k k' d -> - Just (Commitment (hash_leaf_split (key_hash k') h s') s') - | otherwise -> Nothing +verify_split k v s nb sibs r = d <= _HEIGHT && with_scratch go where + d = length sibs + vacant = BS.null v && s == 0 + up st bk c = climb# Split st bk k d c sibs r + go st bk s0 = case nb of + Nothing + | vacant -> up st bk (commit Split d Empty) s0 + | otherwise -> case hash_leaf# Split st bk k v s s0 of + (# s1, h #) -> up st bk (Commitment h s) s1 + Just (Neighbor k' h s') + | vacant && k' /= k && agree k k' d -> + case hash_leaf_split# st bk (key_hash k') h s' s0 of + (# s1, h' #) -> up st bk (Commitment h' s') s1 + | otherwise -> (# s0, False #) -- | The siblings along a key's path in an aggregate tree: all 256 of -- them, from the leaf up to the root's. @@ -395,6 +402,6 @@ verify_aggregate :: Key -> BS.ByteString -> Word64 -> [Commitment] -> Commitment -> Bool verify_aggregate k v s sibs r = length sibs == _HEIGHT - && climb Aggregate k _HEIGHT leaf sibs == Just r + && with_scratch (\st bk -> climb# Aggregate st bk k _HEIGHT leaf sibs r) where leaf = Commitment (hash_leaf_aggregate v s) s diff --git a/lib/Data/MSSMT/Split.hs b/lib/Data/MSSMT/Split.hs @@ -46,6 +46,7 @@ module Data.MSSMT.Split ( ) where import qualified Data.ByteString as BS +import Data.Coerce (coerce) import Data.MSSMT.Internal ( Commitment(..), Hash, Key, Neighbor(..), Scheme(..), key, parse_hash , serialize_hash @@ -67,7 +68,7 @@ empty = Tree I.empty -- -- Returns 'Nothing' if the tree's sum would overflow. insert :: Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree -insert k v s (Tree t) = fmap Tree (I.insert_with Split k v s t) +insert k v s (Tree t) = coerce (I.insert_with Split k v s t) -- | Delete the leaf at a key. delete :: Key -> Tree -> Tree