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 195ac5c34ef7471024cf0a2fef662a7f76556f69
parent db4a04829328b623b0a7c7adcf7e20352ea292f1
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 19:31:49 +0400

lib: rename Pair to Commitment, height to _HEIGHT

Diffstat:
Mbench/Fixtures.hs | 4++--
Mlib/Data/MSSMT.hs | 48++++++++++++++++++++++++------------------------
Mtest/Main.hs | 2+-
Mtest/Vectors.hs | 8++++----
4 files changed, 31 insertions(+), 31 deletions(-)

diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs @@ -19,7 +19,7 @@ import Data.Word (Word64) instance NFData Key where rnf = rwhnf -instance NFData Pair where +instance NFData Commitment where rnf = rwhnf instance NFData Tree where @@ -32,7 +32,7 @@ instance NFData Proof where -- and their proofs. data Fixtures = Fixtures { f_tree :: !Tree - , f_root :: !Pair + , f_root :: !Commitment , f_key :: !Key , f_value :: !BS.ByteString , f_sum :: !Word64 diff --git a/lib/Data/MSSMT.hs b/lib/Data/MSSMT.hs @@ -25,7 +25,7 @@ module Data.MSSMT ( , Hash , parse_hash , serialize_hash - , Pair(..) + , Commitment(..) -- * Tree , Tree @@ -90,7 +90,7 @@ serialize_hash (Hash a b c d) = BI.unsafeCreate 32 $ \p -> do poke_be64 p 24 d -- | A node's commitment: its hash, and its sum. -data Pair = Pair {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 +data Commitment = Commitment {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 deriving (Eq, Show) -- | A merkle-sum sparse merkle tree. @@ -99,8 +99,8 @@ data Pair = Pair {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 -- 'Empty', so a tree holding n leaves has at most 256 * n branches. data Tree = Empty - | Leaf {-# UNPACK #-} !Pair !BS.ByteString - | Node !Tree {-# UNPACK #-} !Pair !Tree + | Leaf {-# UNPACK #-} !Commitment !BS.ByteString + | Node !Tree {-# UNPACK #-} !Commitment !Tree deriving (Eq, Show) -- bytes ---------------------------------------------------------------------- @@ -255,20 +255,20 @@ get_hash a s0 = -- structure ------------------------------------------------------------------ -- leaf depth -height :: Int -height = 256 +_HEIGHT :: Int +_HEIGHT = 256 -- hashes of empty subtrees, four words per depth empties :: PA.PrimArray Word64 -empties = PA.primArrayFromListN (4 * (height + 1)) +empties = PA.primArrayFromListN (4 * (_HEIGHT + 1)) . concatMap (\(Hash a b c d) -> [a, b, c, d]) - . reverse . take (height + 1) $ + . reverse . take (_HEIGHT + 1) $ iterate (\h -> hash_node h h 0) (hash_leaf mempty 0) {-# NOINLINE empties #-} -- commitment of a subtree at the given depth -commit :: Int -> Tree -> Pair -commit d Empty = Pair (Hash (ix 0) (ix 1) (ix 2) (ix 3)) 0 where +commit :: Int -> Tree -> Commitment +commit d Empty = Commitment (Hash (ix 0) (ix 1) (ix 2) (ix 3)) 0 where ix i = PA.indexPrimArray empties (4 * d + i) commit _ (Leaf p _) = p commit _ (Node _ p _) = p @@ -283,16 +283,16 @@ bit (Key k) d = testBit (BU.unsafeIndex k (d `quot` 8)) (d `rem` 8) leaf :: BS.ByteString -> Word64 -> Tree leaf v s | BS.null v && s == 0 = Empty - | otherwise = Leaf (Pair (hash_leaf v s) s) v + | otherwise = Leaf (Commitment (hash_leaf v s) s) v -- branch at the given depth; callers rule out sum overflow node :: Int -> Tree -> Tree -> Tree node _ Empty Empty = Empty node d l r = - let !(Pair hl sl) = commit (d + 1) l - !(Pair hr sr) = commit (d + 1) r + let !(Commitment hl sl) = commit (d + 1) l + !(Commitment hr sr) = commit (d + 1) r !s = sl + sr - in Node l (Pair (hash_node hl hr s) s) r + in Node l (Commitment (hash_node hl hr s) s) r {-# INLINE node #-} -- children of a subtree above leaf depth @@ -306,7 +306,7 @@ children (Leaf _ _) = (# Empty, Empty #) -- unreachable above leaf depth update :: Key -> Tree -> Tree -> Tree update k new = go 0 where go !d t - | d == height = new + | d == _HEIGHT = new | otherwise = case children t of (# l, r #) | bit k d -> node d l (go (d + 1) r) @@ -326,7 +326,7 @@ insert k v s t | s > maxBound - rest = Nothing | otherwise = Just (update k (leaf v s) t) where - Pair _ total = root t + Commitment _ total = root t rest = total - maybe 0 snd (lookup k t) -- | Delete the leaf at a key. @@ -338,18 +338,18 @@ lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) lookup k = go 0 where go !d t = case t of Empty -> Nothing - Leaf (Pair _ s) v -> Just (v, s) + Leaf (Commitment _ s) v -> Just (v, s) Node l _ r -> go (d + 1) (if bit k d then r else l) -- | The tree's root commitment. -root :: Tree -> Pair +root :: Tree -> Commitment root = commit 0 -- proofs --------------------------------------------------------------------- -- | A merkle proof: the commitments of a leaf's 256 siblings, ordered -- from the leaf up to the root. -newtype Proof = Proof [Pair] +newtype Proof = Proof [Commitment] deriving (Eq, Show) -- | Produce a proof for the leaf at a key. @@ -358,7 +358,7 @@ newtype Proof = Proof [Pair] prove :: Key -> Tree -> Proof prove k = Proof . go 0 [] where go !d acc t - | d == height = acc + | d == _HEIGHT = acc | otherwise = case children t of (# l, r #) | bit k d -> go (d + 1) (commit (d + 1) l : acc) r @@ -368,11 +368,11 @@ prove k = Proof . go 0 [] where -- -- An empty value with zero sum verifies the key's exclusion. Proofs -- whose branch sums overflow are rejected. -verify :: Key -> BS.ByteString -> Word64 -> Proof -> Pair -> Bool -verify k v s (Proof sibs) r = go (height - 1) (hash_leaf v s) s sibs where +verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool +verify k v s (Proof sibs) r = go (_HEIGHT - 1) (hash_leaf v s) s sibs where go !d !h !hs ps = case ps of - [] -> d == -1 && Pair h hs == r - Pair g gs : rest + [] -> d == -1 && Commitment h hs == r + Commitment g gs : rest | d < 0 || gs > maxBound - hs -> False | otherwise -> let !t = hs + gs diff --git a/test/Main.hs b/test/Main.hs @@ -127,7 +127,7 @@ properties = testGroup "properties" [ , Q.testProperty "root sum is the sum of leaf sums" $ \(Es _ es) -> with_tree es $ \t -> - let Pair _ s = root t + let Commitment _ s = root t in toInteger s Q.=== sum (fmap (toInteger . snd) (model es)) , Q.testProperty "trees are independent of insertion history" $ diff --git a/test/Vectors.hs b/test/Vectors.hs @@ -28,7 +28,7 @@ import Test.Tasty.HUnit data Leaf = Leaf !Key !BS.ByteString !Word64 data Valid = Valid { - v_root :: !Pair + v_root :: !Commitment , v_inserted :: ![Key] , v_deleted :: ![Key] , v_replaced :: ![Leaf] @@ -121,7 +121,7 @@ instance A.FromJSON Valid where parseJSON = A.withObject "Valid" $ \o -> do h <- o .: "root_hash" >>= parse_hex >>= parse_digest s <- o .: "root_sum" >>= parse_sum - Valid (Pair h s) + Valid (Commitment h s) <$> (list o "inserted_leaves" >>= traverse parse_key) <*> (list o "deleted_leaves" >>= traverse parse_key) <*> list o "replaced_leaves" @@ -183,7 +183,7 @@ decompress bs0 = do let Proof es = prove k empty pure es - siblings :: Int -> BS.ByteString -> Maybe ([Pair], BS.ByteString) + siblings :: Int -> BS.ByteString -> Maybe ([Commitment], BS.ByteString) siblings 0 bs = Just ([], bs) siblings n bs | BS.length bs < 40 = Nothing @@ -192,7 +192,7 @@ decompress bs0 = do (s, rest1) = BS.splitAt 8 rest0 d <- parse_hash h (ps, rest) <- siblings (n - 1) rest1 - pure (Pair d (be s) : ps, rest) + pure (Commitment d (be s) : ps, rest) fill (b:bs) ns (e:es) | b = (e :) <$> fill bs ns es