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 1606f7b6e93260d538b724204d053b9bc4e99815
parent d328b9729948c585d38f3842c47ae5712e9fc0f6
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 21:06:27 +0400

lib: per-scheme tree modules

Data.MSSMT.Split and Data.MSSMT.Aggregate each export a complete tree
API over their own Tree newtype, so that trees of different schemes
can't be mixed; keys, hashes, commitments, and proofs are shared.
Data.MSSMT documents both and re-exports Split, as the default.
Data.MSSMT.Internal keeps only the scheme-parameterized core.

The taproot-assets vectors now run through the public Aggregate API.

Diffstat:
Mbench/Fixtures.hs | 8++++++--
Mlib/Data/MSSMT.hs | 42+++++++++++++-----------------------------
Alib/Data/MSSMT/Aggregate.hs | 92+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mlib/Data/MSSMT/Internal.hs | 37+++++++------------------------------
Alib/Data/MSSMT/Split.hs | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmssmt.cabal | 2++
Mtest/Main.hs | 26+++++++++++++++++---------
Mtest/Vectors.hs | 24+++++++++---------------
8 files changed, 236 insertions(+), 85 deletions(-)

diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs @@ -56,11 +56,15 @@ 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 + n = near k raw + raw = case foldM ins I.empty (leaves 1024) of + Just r -> r + Nothing -> error "bang" + ins r (k', v', s') = I.insert_with I.Split k' v' s' r -- 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 :: Key -> I.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) diff --git a/lib/Data/MSSMT.hs b/lib/Data/MSSMT.hs @@ -6,38 +6,22 @@ -- License: MIT -- Maintainer: Jared Tobin <jared@jtobin.io> -- --- A merkle-sum sparse merkle tree (MS-SMT). +-- Merkle-sum sparse merkle trees (MS-SMTs). -- --- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf --- holds a value and a sum, and commits to both via sha256(value || --- sum). Each branch commits to the hash and sum of each of its --- children, via sha256(left || left sum || right || right sum), and --- its sum is the sum of theirs. A subtree holding no leaves is the --- full subtree of empty leaves (an empty value with zero sum), hashed --- as any other. +-- An MS-SMT has 2^256 leaves, addressed by 256-bit keys. Each leaf +-- holds a value and a sum, and each branch commits to its children +-- and to the sum of their sums. Branches can commit to their children +-- in one of two ways, each with its own tree type: +-- +-- * "Data.MSSMT.Split": each branch commits to the hash and sum of +-- each of its children. This is the default, re-exported here. +-- +-- * "Data.MSSMT.Aggregate": each branch commits to its children's +-- hashes and the sum of their sums, as in Taproot Assets. module Data.MSSMT ( - -- * Keys, hashes, and commitments - Key - , key - , Hash - , parse_hash - , serialize_hash - , Commitment(..) - - -- * Tree - , Tree - , empty - , insert - , delete - , lookup - , root - - -- * Proofs - , Proof(..) - , prove - , verify + module Data.MSSMT.Split ) where -import Data.MSSMT.Internal +import Data.MSSMT.Split import Prelude hiding (lookup) diff --git a/lib/Data/MSSMT/Aggregate.hs b/lib/Data/MSSMT/Aggregate.hs @@ -0,0 +1,92 @@ +{-# OPTIONS_HADDOCK prune #-} + +-- | +-- Module: Data.MSSMT.Aggregate +-- Copyright: (c) 2026 Jared Tobin +-- License: MIT +-- Maintainer: Jared Tobin <jared@jtobin.io> +-- +-- A merkle-sum sparse merkle tree (MS-SMT) whose branches commit to +-- their children's hashes and the sum of their sums, as in Taproot +-- Assets. +-- +-- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf +-- holds a value and a sum, and commits to both via sha256(value || +-- sum). Each branch commits to the hashes of its children and the sum +-- of their sums, via sha256(left || right || sum). A subtree holding +-- no leaves is the full subtree of empty leaves (an empty value with +-- zero sum), hashed as any other. +-- +-- Roots and proofs agree with those of the Taproot Assets MS-SMT. + +module Data.MSSMT.Aggregate ( + -- * Keys, hashes, and commitments + Key + , key + , Hash + , parse_hash + , serialize_hash + , Commitment(..) + + -- * Tree + , Tree + , empty + , insert + , delete + , lookup + , root + + -- * Proofs + , Proof(..) + , prove + , verify + ) where + +import qualified Data.ByteString as BS +import Data.MSSMT.Internal ( + Commitment(..), Hash, Key, Proof(..), Scheme(..), key, parse_hash + , serialize_hash + ) +import qualified Data.MSSMT.Internal as I +import Data.Word (Word64) +import Prelude hiding (lookup) + +-- | A merkle-sum sparse merkle tree whose branches commit to their +-- children's hashes and the sum of their sums. +newtype Tree = Tree I.Tree + deriving (Eq, Show) + +-- | The empty tree. +empty :: Tree +empty = Tree I.empty + +-- | Insert a value and sum at a key, replacing any existing leaf. +-- +-- 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) + +-- | Delete the leaf at a key. +delete :: Key -> Tree -> Tree +delete k (Tree t) = Tree (I.delete_with Aggregate k t) + +-- | Look up the value and sum at a key. +lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) +lookup k (Tree t) = I.lookup k t + +-- | The tree's root commitment. +root :: Tree -> Commitment +root (Tree t) = I.root_with Aggregate t + +-- | Produce a proof for the leaf at a key. +-- +-- If no leaf exists at the key, this proves its exclusion. +prove :: Key -> Tree -> Proof +prove k (Tree t) = I.prove_with Aggregate k t + +-- | Verify that a value and sum sit at a key, under a root. +-- +-- An empty value with zero sum verifies the key's exclusion. Proofs +-- whose branch sums overflow are rejected. +verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool +verify = I.verify_with Aggregate diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs @@ -36,19 +36,14 @@ module Data.MSSMT.Internal ( -- * Tree , Tree(..) , empty - , insert , insert_with - , delete , delete_with , lookup - , root , root_with -- * Proofs , Proof(..) - , prove , prove_with - , verify , verify_with ) where @@ -244,13 +239,10 @@ insert_leaf sc k lh s v t0 = with_scratch $ \st bk -> empty :: Tree empty = Empty --- | Insert a value and sum at a key, replacing any existing leaf. +-- | Insert a value and sum at a key under a scheme, replacing any +-- existing leaf. -- -- Returns 'Nothing' if the tree's sum would overflow. -insert :: Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree -insert = insert_with Split - --- | 'insert', under a scheme. insert_with :: Scheme -> Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree insert_with sc k v s t @@ -261,11 +253,7 @@ insert_with sc k v s t where rest = total t - maybe 0 snd (lookup k t) --- | Delete the leaf at a key. -delete :: Key -> Tree -> Tree -delete = delete_with Split - --- | 'delete', under a scheme. +-- | 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 @@ -291,11 +279,7 @@ lookup k = go 0 where | otherwise -> Nothing Node l _ r -> go (d + 1) (if bit k d then r else l) --- | The tree's root commitment. -root :: Tree -> Commitment -root = root_with Split - --- | 'root', under a scheme. +-- | The tree's root commitment under a scheme. root_with :: Scheme -> Tree -> Commitment root_with sc = commit sc 0 @@ -306,13 +290,9 @@ root_with sc = commit sc 0 newtype Proof = Proof [Commitment] deriving (Eq, Show) --- | Produce a proof for the leaf at a key. +-- | Produce a proof for the leaf at a key under a scheme. -- -- If no leaf exists at the key, this proves its exclusion. -prove :: Key -> Tree -> Proof -prove = prove_with Split - --- | 'prove', under a scheme. prove_with :: Scheme -> Key -> Tree -> Proof prove_with sc k t0 = with_scratch $ \st bk -> let -- empty siblings at depths j through j1 - 1 @@ -336,14 +316,11 @@ prove_with sc k t0 = with_scratch $ \st bk -> | otherwise -> go (d + 1) (commit sc (d + 1) r : acc) l s0 in go 0 [] t0 --- | Verify that a value and sum sit at a key, under a root. +-- | Verify that a value and sum sit at a key, under a root and a +-- scheme. -- -- An empty value with zero sum verifies the key's exclusion. Proofs -- whose branch sums overflow are rejected. -verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool -verify = verify_with Split - --- | 'verify', under a scheme. verify_with :: Scheme -> Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool diff --git a/lib/Data/MSSMT/Split.hs b/lib/Data/MSSMT/Split.hs @@ -0,0 +1,90 @@ +{-# OPTIONS_HADDOCK prune #-} + +-- | +-- Module: Data.MSSMT.Split +-- Copyright: (c) 2026 Jared Tobin +-- License: MIT +-- Maintainer: Jared Tobin <jared@jtobin.io> +-- +-- A merkle-sum sparse merkle tree (MS-SMT) whose branches commit to +-- the hash and sum of each of their children. +-- +-- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf +-- holds a value and a sum, and commits to both via sha256(value || +-- sum). Each branch commits to the hash and sum of each of its +-- children, via sha256(left || left sum || right || right sum), and +-- its sum is the sum of theirs. A subtree holding no leaves is the +-- full subtree of empty leaves (an empty value with zero sum), hashed +-- as any other. + +module Data.MSSMT.Split ( + -- * Keys, hashes, and commitments + Key + , key + , Hash + , parse_hash + , serialize_hash + , Commitment(..) + + -- * Tree + , Tree + , empty + , insert + , delete + , lookup + , root + + -- * Proofs + , Proof(..) + , prove + , verify + ) where + +import qualified Data.ByteString as BS +import Data.MSSMT.Internal ( + Commitment(..), Hash, Key, Proof(..), Scheme(..), key, parse_hash + , serialize_hash + ) +import qualified Data.MSSMT.Internal as I +import Data.Word (Word64) +import Prelude hiding (lookup) + +-- | A merkle-sum sparse merkle tree whose branches commit to the +-- hash and sum of each of their children. +newtype Tree = Tree I.Tree + deriving (Eq, Show) + +-- | The empty tree. +empty :: Tree +empty = Tree I.empty + +-- | Insert a value and sum at a key, replacing any existing leaf. +-- +-- 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) + +-- | Delete the leaf at a key. +delete :: Key -> Tree -> Tree +delete k (Tree t) = Tree (I.delete_with Split k t) + +-- | Look up the value and sum at a key. +lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) +lookup k (Tree t) = I.lookup k t + +-- | The tree's root commitment. +root :: Tree -> Commitment +root (Tree t) = I.root_with Split t + +-- | Produce a proof for the leaf at a key. +-- +-- If no leaf exists at the key, this proves its exclusion. +prove :: Key -> Tree -> Proof +prove k (Tree t) = I.prove_with Split k t + +-- | Verify that a value and sum sit at a key, under a root. +-- +-- An empty value with zero sum verifies the key's exclusion. Proofs +-- whose branch sums overflow are rejected. +verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool +verify = I.verify_with Split diff --git a/mssmt.cabal b/mssmt.cabal @@ -25,8 +25,10 @@ library -Wall exposed-modules: Data.MSSMT + , Data.MSSMT.Aggregate , Data.MSSMT.Hash , Data.MSSMT.Internal + , Data.MSSMT.Split build-depends: base >= 4.11 && < 5 , bytestring >= 0.10.2 && < 0.13 diff --git a/test/Main.hs b/test/Main.hs @@ -9,6 +9,7 @@ import qualified Data.ByteString as BS import qualified Data.Map.Strict as M import Data.Maybe (isJust) import Data.MSSMT +import qualified Data.MSSMT.Aggregate as Agg import qualified Data.MSSMT.Internal as I import Data.Word (Word32, Word64) import Prelude hiding (lookup) @@ -161,9 +162,9 @@ kbit (I.Key bs) d = testBit (BS.index bs (d `quot` 8)) (d `rem` 8) -- every branch holds two or more leaves, and every leaf's key follows -- its path -compact :: Tree -> Bool +compact :: I.Tree -> Bool compact = isJust . go 0 [] where - go :: Int -> [Bool] -> Tree -> Maybe Int + go :: Int -> [Bool] -> I.Tree -> Maybe Int go _ _ I.Empty = Just 0 go d path (I.Leaf _ _ k _) | reverse path == fmap (kbit k) [0 .. d - 1] = Just 1 @@ -174,13 +175,17 @@ compact = isJust . go 0 [] where if a + b >= 2 then Just (a + b) else Nothing -- whether a key's path ends at another key's leaf -at_leaf :: Key -> Tree -> Bool +at_leaf :: Key -> I.Tree -> Bool at_leaf k = go 0 where go d t = case t of I.Empty -> False I.Leaf _ _ k' _ -> k' /= k I.Node l _ r -> go (d + 1) (if kbit k d then r else l) +-- 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 + with_tree :: [E] -> (Tree -> Q.Property) -> Q.Property with_tree es f = case build es of Nothing -> Q.counterexample "overflow" False @@ -204,17 +209,19 @@ properties = testGroup "properties" [ in (serialize_hash h, s) Q.=== R.root (raw (model es)) , Q.testProperty "aggregate root agrees with the reference" $ \(Es _ es) -> - let ins t (E k v s) = I.insert_with I.Aggregate k v s t - in case foldM ins empty es of + let ins t (E k v s) = Agg.insert k v s t + in case foldM ins Agg.empty es of Nothing -> Q.counterexample "overflow" False Just t -> - let Commitment h s = I.root_with I.Aggregate t + let Commitment h s = Agg.root t in (serialize_hash h, s) Q.=== R.root_aggregate (raw (model es)) , Q.testProperty "trees are compact" $ \(Es _ es) -> Q.forAll (Q.sublistOf (M.keys (model es))) $ \ks -> - with_tree es $ \t -> - compact t Q..&&. compact (foldr delete t ks) + case build_raw es of + Nothing -> Q.counterexample "overflow" False + Just t -> + compact t Q..&&. compact (foldr (I.delete_with I.Split) t ks) , Q.testProperty "trees are independent of insertion history" $ \(Es _ es) -> @@ -239,7 +246,8 @@ properties = testGroup "properties" [ Q.forAll (pick x) $ \k -> M.notMember k (model es) Q.==> with_tree es $ \t -> - Q.classify (at_leaf k t) "ends at another leaf" $ + Q.classify (maybe False (at_leaf k) (build_raw es)) + "ends at another leaf" $ verify k mempty 0 (prove k t) (root t) , Q.testProperty "proofs reject a wrong value or sum" $ \(Es _ es) -> diff --git a/test/Vectors.hs b/test/Vectors.hs @@ -19,11 +19,7 @@ import qualified Data.ByteString.Base16 as B16 import qualified Data.ByteString.Char8 as B8 import qualified Data.Map.Strict as M import Data.Maybe (fromMaybe, isJust, isNothing) -import Data.MSSMT -import Data.MSSMT.Internal ( - Scheme(..), delete_with, insert_with, prove_with, root_with - , verify_with - ) +import Data.MSSMT.Aggregate import qualified Data.Set as S import Data.Word (Word64) import Test.Tasty (TestTree, testGroup) @@ -67,34 +63,32 @@ inserting ls ks = filter (\(Leaf k _ _) -> S.member k set) ls where set = S.fromList ks insert_all :: Tree -> [Leaf] -> Maybe Tree -insert_all = foldM (\t (Leaf k v s) -> insert_with Aggregate k v s t) +insert_all = foldM (\t (Leaf k v s) -> insert k v s t) execute_valid :: [Leaf] -> Valid -> TestTree execute_valid ls Valid {..} = testGroup v_comment [ testCase "root" $ - assertEqual mempty (Just v_root) (fmap (root_with Aggregate) tree) + assertEqual mempty (Just v_root) (fmap root tree) , testCase ("inclusion proofs (" <> show (length v_included) <> ")") $ forM_ (zip [0 :: Int ..] v_included) $ \(i, (k, p)) -> case (tree, M.lookup k leaves) of (Just t, Just (Leaf _ v s)) -> do - assertEqual ("prove " <> show i) p (prove_with Aggregate k t) - assertBool ("verify " <> show i) $ - verify_with Aggregate k v s p (root_with Aggregate t) + assertEqual ("prove " <> show i) p (prove k t) + assertBool ("verify " <> show i) (verify k v s p (root t)) _ -> assertFailure ("missing tree or leaf " <> show i) , testCase ("exclusion proofs (" <> show (length v_excluded) <> ")") $ forM_ (zip [0 :: Int ..] v_excluded) $ \(i, (k, p)) -> case tree of Just t -> do - assertEqual ("prove " <> show i) p (prove_with Aggregate k t) - assertBool ("verify " <> show i) $ - verify_with Aggregate k mempty 0 p (root_with Aggregate t) + assertEqual ("prove " <> show i) p (prove k t) + assertBool ("verify " <> show i) (verify k mempty 0 p (root t)) Nothing -> assertFailure "missing tree" ] where leaves = M.fromList [(k, l) | l@(Leaf k _ _) <- ls] tree = do t <- insert_all empty (inserting ls v_inserted) - insert_all (foldr (delete_with Aggregate) t v_deleted) v_replaced + insert_all (foldr delete t v_deleted) v_replaced -- every insert but the last succeeds; the last overflows execute_invalid :: [Leaf] -> Invalid -> TestTree @@ -185,7 +179,7 @@ decompress bs0 = do -- key in the empty tree empties = do k <- key (BS.replicate 32 0) - let Proof es = prove_with Aggregate k empty + let Proof es = prove k empty pure es siblings :: Int -> BS.ByteString -> Maybe ([Commitment], BS.ByteString)