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 485b72aab2e5246dc778ae04f0dbd024ba65f36a
parent 123b0ba085342d371ad8ef9fefca5b8309a169e4
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 22:30:10 +0400

lib: partially loaded trees

A Stub is a subtree held elsewhere, as in a store: its kind (a single
leaf, or a branch) and its commitment. Operations use a stub's
commitment and sum where they meet one beside their path, and require
their path itself to be loaded; expand loads it via a given loader,
and expand_deleting loads too the single leaves beside it, which
deleting from an aggregate tree can move. A split leaf's commitment
is the same at any height, so a stubbed one moves up as it is.

Tests hold whole trees in an in-memory store, load the paths each
operation touches, and check under both schemes that lookup, prove,
insert, delete, and insert_many agree with the fully loaded tree.

Diffstat:
Mbench/Fixtures.hs | 1+
Mlib/Data/MSSMT/Internal.hs | 68++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/Main.hs | 77++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-
3 files changed, 145 insertions(+), 1 deletion(-)

diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs @@ -73,6 +73,7 @@ near (I.Key bs) = flip_at . go 0 where I.Node l _ r -> go (d + 1) (if kbit d then r else l) I.Leaf {} -> d I.Empty -> d + I.Stub {} -> 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 diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs @@ -37,6 +37,9 @@ module Data.MSSMT.Internal ( -- * Tree , Tree(..) + , Kind(..) + , expand + , expand_deleting , empty , insert_with , insert_many_with @@ -89,6 +92,13 @@ data Tree = -- its height, the leaf's hash, and the leaf's key and value | Leaf {-# UNPACK #-} !Commitment {-# UNPACK #-} !Hash !Key !BS.ByteString | Node !Tree {-# UNPACK #-} !Commitment !Tree + -- a subtree held elsewhere, as in a store: what it holds, and its + -- commitment + | Stub !Kind {-# UNPACK #-} !Commitment + deriving (Eq, Show) + +-- | What a subtree holds: a single leaf, or a branch over two or more. +data Kind = Single | Branch deriving (Eq, Show) -- structure ------------------------------------------------------------------ @@ -120,6 +130,7 @@ commit :: Scheme -> Int -> Tree -> Commitment commit sc d Empty = Commitment (empty_hash sc d) 0 commit _ _ (Leaf c _ _ _) = c commit _ _ (Node _ c _) = c +commit _ _ (Stub _ c) = c {-# INLINE commit #-} -- sum of a subtree @@ -127,6 +138,7 @@ total :: Tree -> Word64 total Empty = 0 total (Leaf (Commitment _ s) _ _ _) = s total (Node _ (Commitment _ s) _) = s +total (Stub _ (Commitment _ s)) = s {-# INLINE total #-} -- path bit at the given depth; LSB-first within each byte @@ -160,8 +172,13 @@ with_leaf k t0 none some = go 0 t0 where | k' == k -> some s v | otherwise -> none Node l _ r -> go (d + 1) (if bit k d then r else l) + Stub {} -> error unloaded {-# INLINE with_leaf #-} +-- operations at a key require its path to be loaded +unloaded :: String +unloaded = "mssmt: unloaded subtree on an operation's path" + -- 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 diverge :: Key -> Key -> Int -> Int @@ -205,6 +222,12 @@ node# :: Scheme -> Buffer -> Buffer -> Int -> Tree -> Tree -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Tree #) node# _ _ _ _ Empty Empty s0 = (# s0, Empty #) +-- a split leaf's commitment is the same at any height, so a lone leaf +-- held elsewhere moves up as it is; an aggregate one must be loaded +node# Split _ _ _ Empty t@(Stub Single _) s0 = (# s0, t #) +node# Split _ _ _ t@(Stub Single _) Empty s0 = (# s0, t #) +node# Aggregate _ _ _ Empty (Stub Single _) _ = error unloaded +node# Aggregate _ _ _ (Stub Single _) Empty _ = error unloaded node# sc st bk d Empty (Leaf c lh k v) s0 = lift# sc st bk d c lh k v s0 node# sc st bk d (Leaf c lh k v) Empty s0 = lift# sc st bk d c lh k v s0 node# sc st bk d l r s0 = @@ -261,6 +284,7 @@ insert_leaf sc k v s t0 = with_scratch $ \st bk s00 -> (# 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 + Stub {} -> error unloaded in go 0 t0 s01 } -- a leaf in a subtree being rebuilt in a batch: one being put, one @@ -293,6 +317,7 @@ batch sc items0 t0 = with_scratch $ \st bk -> (# s1, l' #) -> case if null rs then (# s1, r #) else go (d + 1) r rs s1 of (# s2, r' #) -> node# sc st bk d l' r' s2 + Stub {} -> error unloaded -- the subtree at a depth holding exactly the given items' leaves build !d items s0 = case filter live items of @@ -377,12 +402,53 @@ delete_with sc k t0 = with_leaf k t0 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 + Stub {} -> error unloaded in go 0 t0 -- | Look up the value and sum at a key. lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) lookup k t = with_leaf k t Nothing (\s v -> Just (v, s)) +-- | Load the subtrees held elsewhere along a key's path, down to the +-- key's slot, via a loader given each one's kind and commitment, which +-- returns its contents with its own subtrees held elsewhere. +-- +-- Operations at a key require its path to be loaded. +expand + :: Monad m => (Kind -> Commitment -> m Tree) -> Key -> Tree -> m Tree +expand = expand_with False + +-- | 'expand', loading too each single leaf held elsewhere beside the +-- key's path. +-- +-- Deleting at a key in an aggregate tree requires these to be loaded, +-- since a deletion can move them. +expand_deleting + :: Monad m => (Kind -> Commitment -> m Tree) -> Key -> Tree -> m Tree +expand_deleting = expand_with True + +expand_with + :: Monad m + => Bool -> (Kind -> Commitment -> m Tree) -> Key -> Tree -> m Tree +expand_with singles load k = go 0 where + go !d t = case t of + Stub kd c -> load kd c >>= go d + Node l c r + | bit k d -> do + r' <- go (d + 1) r + l' <- beside l + pure (Node l' c r') + | otherwise -> do + l' <- go (d + 1) l + r' <- beside r + pure (Node l' c r') + Empty -> pure t + Leaf {} -> pure t + + beside t = case t of + Stub Single c | singles -> load Single c + _ -> pure t + -- | The tree's root commitment under a scheme. root_with :: Scheme -> Tree -> Commitment root_with sc = commit sc 0 @@ -426,6 +492,7 @@ prove_split k = go 0 [] where Node l _ r | bit k d -> go (d + 1) (commit Split (d + 1) l : acc) r | otherwise -> go (d + 1) (commit Split (d + 1) r : acc) l + Stub {} -> error unloaded -- | Verify that a value and sum sit at a key in a split tree, under a -- root, given the siblings along the key's path, and the leaf the path @@ -476,6 +543,7 @@ prove_aggregate k t0 = with_scratch $ \st bk -> Node l _ r | bit k d -> go (d + 1) (commit Aggregate (d + 1) l : acc) r s0 | otherwise -> go (d + 1) (commit Aggregate (d + 1) r : acc) l s0 + Stub {} -> error unloaded in go 0 [] t0 -- | Verify that a value and sum sit at a key in an aggregate tree, under diff --git a/test/Main.hs b/test/Main.hs @@ -29,6 +29,8 @@ main = do , properties "split" split_ops , properties "aggregate" aggregate_ops , split_proofs + , partial "split" I.Split I.prove_split I.expand + , partial "aggregate" I.Aggregate I.prove_aggregate I.expand_deleting , structure , testGroup "taproot-assets vectors" (zipWith V.execute files vs) ] @@ -239,6 +241,7 @@ compact = isJust . go 0 [] where a <- go (d + 1) (False : path) l b <- go (d + 1) (True : path) r if a + b >= 2 then Just (a + b) else Nothing + go _ _ (I.Stub {}) = Nothing -- whether a key's path ends at another key's leaf at_leaf :: Key -> I.Tree -> Bool @@ -247,10 +250,38 @@ at_leaf k = go 0 where I.Empty -> False I.Leaf _ _ k' _ -> k' /= k I.Node l _ r -> go (d + 1) (if kbit k d then r else l) + I.Stub {} -> False -- the internal tree underlying a split tree build_raw :: [E] -> Maybe I.Tree -build_raw = foldM (\t (E k v s) -> I.insert_with I.Split k v s t) I.empty +build_raw = build_with I.Split + +build_with :: I.Scheme -> [E] -> Maybe I.Tree +build_with sc = foldM (\t (E k v s) -> I.insert_with sc k v s t) I.empty + +-- a tree held entirely elsewhere: its root as a stub, and a store of its +-- nodes by commitment, each with its subtrees as stubs +held :: I.Tree -> (I.Tree, M.Map (Hash, Word64) I.Tree) +held t0 = (stub t0, go t0 M.empty) where + stub t = case t of + I.Empty -> I.Empty + I.Leaf c _ _ _ -> I.Stub I.Single c + I.Node _ c _ -> I.Stub I.Branch c + I.Stub {} -> t + go t m = case t of + I.Empty -> m + I.Leaf c _ _ _ -> M.insert (ckey c) t m + I.Node l c r -> + go l (go r (M.insert (ckey c) (I.Node (stub l) c (stub r)) m)) + I.Stub {} -> m + +ckey :: Commitment -> (Hash, Word64) +ckey (Commitment h s) = (h, s) + +-- load a stub's contents from a store +from_store + :: M.Map (Hash, Word64) I.Tree -> I.Kind -> Commitment -> Maybe I.Tree +from_store m _ c = M.lookup (ckey c) m with_tree :: Ops t p -> [E] -> (t -> Q.Property) -> Q.Property with_tree o es f = case build o es of @@ -373,6 +404,50 @@ split_proofs = testGroup "split proofs" [ Proof Nothing _ -> Q.discard ] +-- operations on a tree held entirely elsewhere, loaded along the paths +-- they touch, agree with those on the tree itself +partial + :: (Eq p, Show p) + => String + -> I.Scheme + -> (Key -> I.Tree -> p) + -> ( (I.Kind -> Commitment -> Maybe I.Tree) + -> Key -> I.Tree -> Maybe I.Tree) + -> TestTree +partial name sc pf deleting = testGroup ("partial trees (" <> name <> ")") [ + Q.testProperty "lookup" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> + with es $ \t (h, m) -> + fmap (I.lookup k) (I.expand (from_store m) k h) Q.=== Just (I.lookup k t) + + , Q.testProperty "prove" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> + with es $ \t (h, m) -> + fmap (pf k) (I.expand (from_store m) k h) Q.=== Just (pf k t) + + , Q.testProperty "insert" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> + with es $ \t (h, m) -> + let ins = I.insert_with sc k "v" 1 + in fmap root_of (I.expand (from_store m) k h >>= ins) + Q.=== fmap root_of (ins t) + + , Q.testProperty "delete" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> + with es $ \t (h, m) -> + let del = I.delete_with sc k + in fmap (root_of . del) (deleting (from_store m) k h) + Q.=== Just (root_of (del t)) + + , Q.testProperty "insert_many" $ \x@(Es _ es) -> + Q.forAll (gen_batch small x) $ \b -> + with es $ \t (h, m) -> + let ins = I.insert_many_with sc b + loaded = foldM (\u (k, _, _) -> deleting (from_store m) k u) h b + in fmap root_of (loaded >>= ins) Q.=== fmap root_of (ins t) + ] + where + root_of = I.root_with sc + with es f = case build_with sc es of + Nothing -> Q.counterexample "overflow" False + Just t -> f t (held t) + -- the shape of the underlying tree, which doesn't depend on the scheme structure :: TestTree structure = testGroup "structure" [