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

test: run properties under both schemes

The tree properties are parameterized by a module's operations and
its reference root, and run against both Data.MSSMT.Split and
Data.MSSMT.Aggregate. The compactness check, which doesn't depend on
the scheme, moves to its own group.

Diffstat:
Mtest/Main.hs | 118+++++++++++++++++++++++++++++++++++++++++++++++--------------------------------
1 file changed, 71 insertions(+), 47 deletions(-)

diff --git a/test/Main.hs b/test/Main.hs @@ -1,4 +1,5 @@ {-# LANGUAGE OverloadedStrings #-} +{-# LANGUAGE RecordWildCards #-} module Main where @@ -25,7 +26,9 @@ main = do defaultMain $ testGroup "mssmt" [ units , hashing - , properties + , properties "split" split_ops + , properties "aggregate" aggregate_ops + , structure , testGroup "taproot-assets vectors" (zipWith V.execute files vs) ] where @@ -132,8 +135,31 @@ instance Q.Arbitrary Es where <*> fmap fromIntegral (Q.arbitrary :: Q.Gen Word32)) shrink (Es pre es) = Es pre <$> Q.shrinkList (const []) es -build :: [E] -> Maybe Tree -build = foldM (\t (E k v s) -> insert k v s t) empty +-- a tree module's operations, and its reference root +data Ops t = Ops { + o_empty :: t + , o_insert :: Key -> BS.ByteString -> Word64 -> t -> Maybe t + , o_delete :: Key -> t -> t + , o_lookup :: Key -> t -> Maybe (BS.ByteString, Word64) + , o_root :: t -> Commitment + , o_prove :: Key -> t -> Proof + , o_verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool + , o_ref :: Leaves -> (BS.ByteString, Word64) + } + +-- leaves by raw key +type Leaves = M.Map BS.ByteString (BS.ByteString, Word64) + +split_ops :: Ops Tree +split_ops = Ops empty insert delete lookup root prove verify R.root + +aggregate_ops :: Ops Agg.Tree +aggregate_ops = Ops + Agg.empty Agg.insert Agg.delete Agg.lookup Agg.root Agg.prove Agg.verify + R.root_aggregate + +build :: Ops t -> [E] -> Maybe t +build Ops {..} = foldM (\t (E k v s) -> o_insert k v s t) o_empty -- later leaves replace earlier ones model :: [E] -> M.Map Key (BS.ByteString, Word64) @@ -186,84 +212,82 @@ at_leaf k = go 0 where 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 +with_tree :: Ops t -> [E] -> (t -> Q.Property) -> Q.Property +with_tree o es f = case build o es of Nothing -> Q.counterexample "overflow" False Just t -> f t -properties :: TestTree -properties = testGroup "properties" [ +properties :: (Eq t, Show t) => String -> Ops t -> TestTree +properties name o@Ops {..} = testGroup ("properties (" <> name <> ")") [ Q.testProperty "lookup agrees with a map model" $ \(Es _ es) -> - with_tree es $ \t -> + with_tree o es $ \t -> Q.property $ - all (\(k, l) -> lookup k t == Just l) (M.toList (model es)) + all (\(k, l) -> o_lookup k t == Just l) (M.toList (model es)) , Q.testProperty "root sum is the sum of leaf sums" $ \(Es _ es) -> - with_tree es $ \t -> - let Commitment _ s = root t + with_tree o es $ \t -> + let Commitment _ s = o_root t in toInteger s Q.=== sum (fmap (toInteger . snd) (model es)) , Q.testProperty "root agrees with the reference" $ \(Es _ es) -> - with_tree es $ \t -> - let Commitment h s = root t - 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) = 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 = 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 -> - case build_raw es of - Nothing -> Q.counterexample "overflow" False - Just t -> - compact t Q..&&. compact (foldr (I.delete_with I.Split) t ks) + with_tree o es $ \t -> + let Commitment h s = o_root t + in (serialize_hash h, s) Q.=== o_ref (raw (model es)) , Q.testProperty "trees are independent of insertion history" $ \(Es _ es) -> - build es Q.=== build [E k v s | (k, (v, s)) <- M.toList (model es)] + let canonical = [E k v s | (k, (v, s)) <- M.toList (model es)] + in build o es Q.=== build o canonical , Q.testProperty "delete k . insert k v s == delete k" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> - with_tree es $ \t -> - fmap (delete k) (insert k "v" 1 t) Q.=== Just (delete k t) + with_tree o es $ \t -> + fmap (o_delete k) (o_insert k "v" 1 t) Q.=== Just (o_delete k t) , Q.testProperty "deleting every leaf yields the empty tree" $ \(Es _ es) -> - with_tree es $ \t -> - foldr delete t (M.keys (model es)) Q.=== empty + with_tree o es $ \t -> + foldr o_delete t (M.keys (model es)) Q.=== o_empty , Q.testProperty "inclusion proofs verify" $ \(Es _ es) -> - with_tree es $ \t -> + with_tree o es $ \t -> Q.property $ all - (\(k, (v, s)) -> verify k v s (prove k t) (root t)) + (\(k, (v, s)) -> o_verify k v s (o_prove k t) (o_root t)) (M.toList (model es)) , Q.testProperty "exclusion proofs verify" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> M.notMember k (model es) Q.==> - with_tree es $ \t -> + with_tree o es $ \t -> Q.classify (maybe False (at_leaf k) (build_raw es)) "ends at another leaf" $ - verify k mempty 0 (prove k t) (root t) + o_verify k mempty 0 (o_prove k t) (o_root t) , Q.testProperty "proofs reject a wrong value or sum" $ \(Es _ es) -> - with_tree es $ \t -> - Q.property $ all - (\(k, (v, s)) -> - not (verify k (v <> "x") s (prove k t) (root t)) - && not (verify k v (s + 1) (prove k t) (root t)) - && not (verify k mempty 0 (prove k t) (root t))) - (M.toList (model es)) + with_tree o es $ \t -> + let r = o_root t + in Q.property $ all + (\(k, (v, s)) -> + not (o_verify k (v <> "x") s (o_prove k t) r) + && not (o_verify k v (s + 1) (o_prove k t) r) + && not (o_verify k mempty 0 (o_prove k t) r)) + (M.toList (model es)) , Q.testProperty "insert fails iff the root sum would overflow" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> Q.forAll big $ \s -> - with_tree es $ \t -> + with_tree o es $ \t -> let m = model es rest = sum (fmap (toInteger . snd) (M.delete k m)) fits = rest + toInteger s <= toInteger (maxBound :: Word64) - in Q.classify fits "fits" $ isJust (insert k "v" s t) Q.=== fits + in Q.classify fits "fits" $ isJust (o_insert k "v" s t) Q.=== fits + ] + +-- the shape of the underlying tree, which doesn't depend on the scheme +structure :: TestTree +structure = testGroup "structure" [ + Q.testProperty "trees are compact" $ \(Es _ es) -> + Q.forAll (Q.sublistOf (M.keys (model es))) $ \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) ]