commit 0cbf7c84735427e7d32bdfd81d7ca7d0eb082f94
parent 3037475683448f83af4676f0b28e20c93705ae55
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 4 Nov 2026 21:31:52 +0400
lib: shortcut commitments for split trees
A split tree's subtree holding a single leaf now commits to that
leaf, at any height, and an empty subtree to the zero hash and a zero
sum, so that a key's path runs only as deep as is needed to set it
apart. Leaves commit to their keys, via sha256(key || sha256(value) ||
sum); leaf and branch hash inputs are exactly 72 and 80 bytes
respectively, so no input to one is an input to the other.
Split proofs carry the siblings along a key's path, from the deepest
up, and, where the path ends at another key's leaf, that leaf's key,
value hash, and sum (a Neighbor); verification rejects proofs of more
than 256 siblings. Aggregate trees, and their 256-sibling proofs, are
unchanged; each module now defines its own Proof type.
On a 1024-leaf tree, split inserts take ~0.9us, down from ~14.5us
(~28us into an occupied slot), verification ~0.75us, down from
~14.8us, and proofs ~60-130ns, down from ~1.2-15us.
Tests add a reference split root, check that proofs end at their
key's slot, and that a present key's own leaf, or a forged neighbor,
can't prove a key's exclusion.
Diffstat:
7 files changed, 309 insertions(+), 116 deletions(-)
diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs
@@ -28,8 +28,11 @@ instance NFData Commitment where
instance NFData Tree where
rnf = rwhnf
+instance NFData Neighbor where
+ rnf = rwhnf
+
instance NFData Proof where
- rnf (Proof ps) = rnf ps
+ rnf (Proof nb ps) = rnf nb `seq` rnf ps
-- | A tree of 1024 leaves, its root, a leaf in it, a key not in it
-- whose slot is empty, their proofs, and a key not in it whose slot
diff --git a/lib/Data/MSSMT/Aggregate.hs b/lib/Data/MSSMT/Aggregate.hs
@@ -44,7 +44,7 @@ module Data.MSSMT.Aggregate (
import qualified Data.ByteString as BS
import Data.MSSMT.Internal (
- Commitment(..), Hash, Key, Proof(..), Scheme(..), key, parse_hash
+ Commitment(..), Hash, Key, Scheme(..), key, parse_hash
, serialize_hash
)
import qualified Data.MSSMT.Internal as I
@@ -78,15 +78,20 @@ lookup k (Tree t) = I.lookup k t
root :: Tree -> Commitment
root (Tree t) = I.root_with Aggregate t
+-- | A merkle proof for a key: the commitments of the 256 siblings along
+-- its path, from the leaf up to the root's.
+newtype Proof = Proof [Commitment]
+ deriving (Eq, Show)
+
-- | 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
+prove k (Tree t) = Proof (I.prove_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
+verify k v s (Proof ps) = I.verify_aggregate k v s ps
diff --git a/lib/Data/MSSMT/Hash.hs b/lib/Data/MSSMT/Hash.hs
@@ -17,10 +17,13 @@ module Data.MSSMT.Hash (
Hash(..)
, parse_hash
, serialize_hash
+ , unsafe_hash
-- * Leaf and branch hashes
, native
- , hash_leaf
+ , hash_value
+ , hash_leaf_split
+ , hash_leaf_aggregate
, hash_node_split
, hash_node_split_native
, hash_node_split_pure
@@ -76,7 +79,7 @@ serialize_hash (Hash a b c d) = BI.unsafeCreate 32 $ \p -> do
-- bytes ----------------------------------------------------------------------
--- read a hash from 32 bytes; callers check the length
+-- | Read a hash from 32 bytes. Callers check the length.
unsafe_hash :: BS.ByteString -> Hash
unsafe_hash bs = Hash (be64 00) (be64 08) (be64 16) (be64 24) where
be64 i =
@@ -101,9 +104,21 @@ poke_be64 p i w = do
-- hashing --------------------------------------------------------------------
--- | sha256(value || sum).
-hash_leaf :: BS.ByteString -> Word64 -> Hash
-hash_leaf v s = unsafe_hash . SHA256.hash $
+-- | sha256(value).
+hash_value :: BS.ByteString -> Hash
+hash_value = unsafe_hash . SHA256.hash
+
+-- | sha256(key || value hash || sum), a split leaf's hash.
+--
+-- The 72-byte message has the same two-block layout as that of
+-- 'hash_node_aggregate'.
+hash_leaf_split :: Hash -> Hash -> Word64 -> Hash
+hash_leaf_split = hash_node_aggregate
+{-# INLINE hash_leaf_split #-}
+
+-- | sha256(value || sum), an aggregate leaf's hash.
+hash_leaf_aggregate :: BS.ByteString -> Word64 -> Hash
+hash_leaf_aggregate v s = unsafe_hash . SHA256.hash $
BI.unsafeCreate (l + 8) $ \p -> do
BU.unsafeUseAsCString v $ \q -> copyBytes p (castPtr q) l
poke_be64 p l s
diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs
@@ -22,7 +22,9 @@ module Data.MSSMT.Internal (
-- * Hashing
, native
- , hash_leaf
+ , hash_value
+ , hash_leaf_split
+ , hash_leaf_aggregate
, hash_node_split
, hash_node_split_native
, hash_node_split_pure
@@ -42,9 +44,11 @@ module Data.MSSMT.Internal (
, root_with
-- * Proofs
- , Proof(..)
- , prove_with
- , verify_with
+ , Neighbor(..)
+ , prove_split
+ , verify_split
+ , prove_aggregate
+ , verify_aggregate
) where
import Data.Bits (testBit)
@@ -90,30 +94,22 @@ data Tree =
_HEIGHT :: Int
_HEIGHT = 256
--- hashes of empty subtrees under a scheme, four words per depth; an
--- empty subtree is the full subtree of empty leaves, hashed as any
+-- hashes of empty subtrees in an aggregate tree, four words per depth:
+-- an empty subtree is the full subtree of empty leaves, hashed as any
-- other subtree
-empties_with :: Scheme -> PA.PrimArray Word64
-empties_with sc = PA.primArrayFromListN (4 * (_HEIGHT + 1))
+empties :: PA.PrimArray Word64
+empties = PA.primArrayFromListN (4 * (_HEIGHT + 1))
. concatMap (\(Hash a b c d) -> [a, b, c, d])
. reverse . take (_HEIGHT + 1) $
- iterate (\h -> hash_branch sc h 0 h 0) (hash_leaf mempty 0)
-
-empties :: PA.PrimArray Word64
-empties = empties_with Split
+ iterate (\h -> hash_node_aggregate h h 0) (hash_leaf_aggregate mempty 0)
{-# NOINLINE empties #-}
-empties_aggregate :: PA.PrimArray Word64
-empties_aggregate = empties_with Aggregate
-{-# NOINLINE empties_aggregate #-}
-
--- hash of the empty subtree at the given depth
+-- hash of the empty subtree at the given depth; in a split tree, it's
+-- the zero hash at any depth
empty_hash :: Scheme -> Int -> Hash
-empty_hash sc d = Hash (ix 0) (ix 1) (ix 2) (ix 3) where
- es = case sc of
- Split -> empties
- Aggregate -> empties_aggregate
- ix i = PA.indexPrimArray es (4 * d + i)
+empty_hash Split _ = Hash 0 0 0 0
+empty_hash Aggregate d = Hash (ix 0) (ix 1) (ix 2) (ix 3) where
+ ix i = PA.indexPrimArray empties (4 * d + i)
{-# INLINE empty_hash #-}
-- commitment of a subtree at the given depth
@@ -135,6 +131,19 @@ bit :: Key -> Int -> Bool
bit (Key k) d = testBit (BU.unsafeIndex k (d `quot` 8)) (d `rem` 8)
{-# INLINE bit #-}
+-- whether two keys' paths agree above the given depth
+agree :: Key -> Key -> Int -> Bool
+agree k k' d = all (\j -> bit k j == bit k' j) [0 .. d - 1]
+
+-- a key, as a hash's words
+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
+
-- 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
@@ -144,10 +153,13 @@ diverge k k' = go where
| otherwise = go (d + 1)
-- raise a single-leaf subtree's commitment from one depth to a
--- shallower one, pairing it with an empty sibling at each level between
+-- shallower one: in a split tree, it's the same at any depth, and in an
+-- aggregate tree, it's paired with an empty sibling at each level
+-- between
raise#
:: Scheme -> Buffer -> Buffer -> Key -> Int -> Int -> Commitment
-> State# Exts.RealWorld -> (# State# Exts.RealWorld, Commitment #)
+raise# Split _ _ _ _ _ c = \s0 -> (# s0, c #)
raise# sc st bk k to from (Commitment h0 s) = go (from - 1) h0 where
go !d !h s0
| d < to = let !c = Commitment h s in (# s0, c #)
@@ -249,7 +261,7 @@ 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 v s) s v t)
+ | otherwise = Just (insert_leaf sc k (hash_leaf_with sc k v s) s v t)
where
rest = total t - maybe 0 snd (lookup k t)
@@ -285,53 +297,104 @@ root_with sc = commit sc 0
-- proofs ---------------------------------------------------------------------
--- | A merkle proof: the commitments of a leaf's 256 siblings, ordered
--- from the leaf up to the root.
-newtype Proof = Proof [Commitment]
+-- | Another key's leaf, at which a key's path ends: its key, the hash
+-- of its value, and its sum.
+data Neighbor = Neighbor !Key {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64
deriving (Eq, Show)
--- | Produce a proof for the leaf at a key under a scheme.
+-- 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
+
+-- | 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
+-- key's.
+prove_split :: Key -> Tree -> (Maybe Neighbor, [Commitment])
+prove_split k = go 0 [] where
+ go !d acc t = case t of
+ Empty -> (Nothing, acc)
+ Leaf (Commitment _ s) _ k' v
+ | k' == k -> (Nothing, acc)
+ | otherwise -> (Just (Neighbor k' (hash_value v) s), acc)
+ 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
+
+-- | 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
+-- ends at, where that's another key's.
--
--- If no leaf exists at the key, this proves its exclusion.
-prove_with :: Scheme -> Key -> Tree -> Proof
-prove_with sc k t0 = with_scratch $ \st bk ->
+-- An empty value with zero sum verifies the key's exclusion, for which
+-- the path must end at an empty subtree or at another key's leaf.
+-- Proofs with more than 256 siblings, or whose branch sums overflow,
+-- are rejected.
+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
+
+-- | The siblings along a key's path in an aggregate tree: all 256 of
+-- them, from the leaf up to the root's.
+prove_aggregate :: Key -> Tree -> [Commitment]
+prove_aggregate k t0 = with_scratch $ \st bk ->
let -- empty siblings at depths j through j1 - 1
pad j j1 acc
| j == j1 = acc
| otherwise =
- pad (j + 1) j1 (Commitment (empty_hash sc (j + 1)) 0 : acc)
+ pad (j + 1) j1 (commit Aggregate (j + 1) Empty : acc)
go !d acc t s0 = case t of
- Empty -> (# s0, Proof (pad d _HEIGHT acc) #)
+ Empty -> (# s0, pad d _HEIGHT acc #)
Leaf (Commitment _ s') lh' k' _
- | k' == k -> (# s0, Proof (pad d _HEIGHT acc) #)
+ | k' == k -> (# s0, pad d _HEIGHT acc #)
| otherwise ->
-- the leaf's subtree is the sibling where the paths diverge
let p = diverge k k' d
c0 = Commitment lh' s'
- in case raise# sc st bk k' (p + 1) _HEIGHT c0 s0 of
+ in case raise# Aggregate st bk k' (p + 1) _HEIGHT c0 s0 of
(# s1, c #) ->
- (# s1, Proof (pad (p + 1) _HEIGHT (c : pad d p acc)) #)
+ (# s1, pad (p + 1) _HEIGHT (c : pad d p acc) #)
Node l _ r
- | bit k d -> go (d + 1) (commit sc (d + 1) l : acc) r s0
- | otherwise -> go (d + 1) (commit sc (d + 1) r : acc) l s0
+ | 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
in go 0 [] t0
--- | Verify that a value and sum sit at a key, under a root and a
--- scheme.
+-- | Verify that a value and sum sit at a key in an aggregate tree, under
+-- a root, given the 256 siblings along the key's path.
--
-- An empty value with zero sum verifies the key's exclusion. Proofs
-- whose branch sums overflow are rejected.
-verify_with
- :: Scheme -> Key -> BS.ByteString -> Word64 -> Proof -> Commitment
- -> Bool
-verify_with sc k v s (Proof sibs) r = with_scratch $ \st bk ->
- let 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
- in go (_HEIGHT - 1) (hash_leaf v s) s sibs
+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
+ where
+ leaf = Commitment (hash_leaf_aggregate v s) s
diff --git a/lib/Data/MSSMT/Split.hs b/lib/Data/MSSMT/Split.hs
@@ -10,12 +10,16 @@
-- 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.
+-- holds a value and a sum, and commits to its key, value, and sum via
+-- sha256(key || 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 commits to the zero hash and a zero sum, and a
+-- subtree holding a single leaf commits to that leaf, so that a key's
+-- path runs only as deep as is needed to set it apart from the others.
+--
+-- Leaf and branch hashes are over exactly 72 and 80 bytes
+-- respectively, so that no input to one is an input to the other.
module Data.MSSMT.Split (
-- * Keys, hashes, and commitments
@@ -36,13 +40,14 @@ module Data.MSSMT.Split (
-- * Proofs
, Proof(..)
+ , Neighbor(..)
, prove
, verify
) where
import qualified Data.ByteString as BS
import Data.MSSMT.Internal (
- Commitment(..), Hash, Key, Proof(..), Scheme(..), key, parse_hash
+ Commitment(..), Hash, Key, Neighbor(..), Scheme(..), key, parse_hash
, serialize_hash
)
import qualified Data.MSSMT.Internal as I
@@ -76,15 +81,25 @@ lookup k (Tree t) = I.lookup k t
root :: Tree -> Commitment
root (Tree t) = I.root_with Split t
+-- | A merkle proof for a key: the commitments of the siblings along its
+-- path, from the deepest up to the root's, and the leaf the path ends
+-- at, where that's another key's.
+--
+-- The number of siblings is the depth at which the path ends.
+data Proof = Proof !(Maybe Neighbor) [Commitment]
+ deriving (Eq, Show)
+
-- | 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
+prove k (Tree t) = case I.prove_split k t of
+ (nb, ps) -> Proof nb ps
-- | 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.
+-- with more than 256 siblings, or whose branch sums overflow, are
+-- rejected.
verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool
-verify = I.verify_with Split
+verify k v s (Proof nb ps) = I.verify_split k v s nb ps
diff --git a/test/Main.hs b/test/Main.hs
@@ -28,6 +28,7 @@ main = do
, hashing
, properties "split" split_ops
, properties "aggregate" aggregate_ops
+ , split_proofs
, structure
, testGroup "taproot-assets vectors" (zipWith V.execute files vs)
]
@@ -60,21 +61,39 @@ units = testGroup "units" [
case key (BS.replicate 32 0) of
Nothing -> assertFailure "key"
Just k -> assertEqual mempty (Just empty) (insert k mempty 0 empty)
- , testCase "the empty tree is the full tree of empty leaves" $
+ , testCase "empty roots agree with the reference" $ do
let Commitment h s = root empty
- in assertEqual mempty (R.root M.empty) (serialize_hash h, s)
- , testCase "proofs have 256 siblings" $
+ Commitment g n = Agg.root Agg.empty
+ assertEqual "split" (R.root M.empty) (serialize_hash h, s)
+ assertEqual "aggregate" (R.root_aggregate M.empty) (serialize_hash g, n)
+ , testCase "aggregate proofs have 256 siblings" $
case key (BS.replicate 32 0) of
Nothing -> assertFailure "key"
- Just k -> case insert k "a" 1 empty of
+ Just k -> case Agg.insert k "a" 1 Agg.empty of
Nothing -> assertFailure "insert"
Just t -> do
- let Proof ps = prove k t
+ let Agg.Proof ps = Agg.prove k t
+ r = Agg.root t
assertEqual "length" 256 (length ps)
assertBool "short" $
- not (verify k "a" 1 (Proof (drop 1 ps)) (root t))
+ not (Agg.verify k "a" 1 (Agg.Proof (drop 1 ps)) r)
assertBool "long" $
- not (verify k "a" 1 (Proof (ps <> take 1 ps)) (root t))
+ not (Agg.verify k "a" 1 (Agg.Proof (ps <> take 1 ps)) r)
+ , testCase "split proofs run only as deep as their key's slot" $
+ case key (BS.replicate 32 0) of
+ Nothing -> assertFailure "key"
+ Just k -> case insert k "a" 1 empty of
+ Nothing -> assertFailure "insert"
+ Just t -> do
+ let p@(Proof nb ps) = prove k t
+ r = root t
+ e = root empty
+ assertEqual "siblings" 0 (length ps)
+ assertEqual "neighbor" Nothing nb
+ assertBool "verify" (verify k "a" 1 p r)
+ assertBool "long" $ not (verify k "a" 1 (Proof nb [e]) r)
+ assertBool "too long" $
+ not (verify k "a" 1 (Proof nb (replicate 257 e)) r)
]
-- hashing --------------------------------------------------------------------
@@ -86,9 +105,14 @@ gen_hash = fmap BS.pack (Q.vector 32) `Q.suchThatMap` parse_hash
-- only where hardware SHA-256 is available
hashing :: TestTree
hashing = testGroup "hashing" $ [
- Q.testProperty "hash_leaf ~ reference" $ \v -> Q.forAll big $ \s ->
- serialize_hash (I.hash_leaf (BS.pack v) s)
- Q.=== R.hash_leaf (BS.pack v) s
+ Q.testProperty "hash_leaf_split ~ reference" $
+ Q.forAll gen_hash $ \k v -> Q.forAll big $ \s ->
+ serialize_hash (I.hash_leaf_split k (I.hash_value (BS.pack v)) s)
+ Q.=== R.hash_leaf_split (serialize_hash k) (BS.pack v) s
+ , Q.testProperty "hash_leaf_aggregate ~ reference" $
+ \v -> Q.forAll big $ \s ->
+ serialize_hash (I.hash_leaf_aggregate (BS.pack v) s)
+ Q.=== R.hash_leaf_aggregate (BS.pack v) s
, split "hash_node_split_pure" I.hash_node_split_pure
, aggregate "hash_node_aggregate_pure" I.hash_node_aggregate_pure
]
@@ -136,29 +160,26 @@ instance Q.Arbitrary Es where
shrink (Es pre es) = Es pre <$> Q.shrinkList (const []) es
-- a tree module's operations, and its reference root
-data Ops t = Ops {
+data Ops t p = 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)
+ , o_prove :: Key -> t -> p
+ , o_verify :: Key -> BS.ByteString -> Word64 -> p -> Commitment -> Bool
+ , o_ref :: R.Leaves -> (BS.ByteString, Word64)
}
--- leaves by raw key
-type Leaves = M.Map BS.ByteString (BS.ByteString, Word64)
-
-split_ops :: Ops Tree
+split_ops :: Ops Tree Proof
split_ops = Ops empty insert delete lookup root prove verify R.root
-aggregate_ops :: Ops Agg.Tree
+aggregate_ops :: Ops Agg.Tree Agg.Proof
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 t p -> [E] -> Maybe t
build Ops {..} = foldM (\t (E k v s) -> o_insert k v s t) o_empty
-- later leaves replace earlier ones
@@ -178,9 +199,13 @@ big = Q.oneof [
, (maxBound -) . fromIntegral <$> (Q.arbitrary :: Q.Gen Word32)
]
+-- a key's raw bytes
+raw_key :: Key -> BS.ByteString
+raw_key (I.Key bs) = bs
+
-- key the model by raw bytes
raw :: M.Map Key a -> M.Map BS.ByteString a
-raw = M.mapKeysMonotonic (\(I.Key bs) -> bs)
+raw = M.mapKeysMonotonic raw_key
-- path bit at the given depth; LSB-first within each byte
kbit :: Key -> Int -> Bool
@@ -212,12 +237,12 @@ 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 :: Ops t -> [E] -> (t -> Q.Property) -> Q.Property
+with_tree :: Ops t p -> [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 :: (Eq t, Show t) => String -> Ops t -> TestTree
+properties :: (Eq t, Show t) => String -> Ops t p -> TestTree
properties name o@Ops {..} = testGroup ("properties (" <> name <> ")") [
Q.testProperty "lookup agrees with a map model" $ \(Es _ es) ->
with_tree o es $ \t ->
@@ -281,6 +306,41 @@ properties name o@Ops {..} = testGroup ("properties (" <> name <> ")") [
in Q.classify fits "fits" $ isJust (o_insert k "v" s t) Q.=== fits
]
+-- split proofs, which run only as deep as their key's slot
+split_proofs :: TestTree
+split_proofs = testGroup "split proofs" [
+ Q.testProperty "proofs end at their key's slot" $ \x@(Es _ es) ->
+ Q.forAll (pick x) $ \k ->
+ with_tree split_ops es $ \t ->
+ let Proof nb ps = prove k t
+ neighbor (Neighbor k' _ _) = raw_key k'
+ in (length ps, fmap neighbor nb)
+ Q.=== R.slot (raw (model es)) (raw_key k)
+
+ , Q.testProperty "a present key's own leaf proves nothing" $
+ \(Es _ es) ->
+ with_tree split_ops es $ \t ->
+ Q.property $ all
+ (\(k, (v, s)) ->
+ let Proof _ ps = prove k t
+ own = Proof (Just (Neighbor k (I.hash_value v) s)) ps
+ in not (verify k mempty 0 own (root t))
+ && not (verify k v s own (root t)))
+ (M.toList (model es))
+
+ , Q.testProperty "exclusion proofs reject a forged neighbor" $
+ \x@(Es _ es) -> Q.forAll (pick x) $ \k ->
+ with_tree split_ops es $ \t -> case prove k t of
+ Proof (Just (Neighbor k' h s)) ps ->
+ let r = root t
+ with n = verify k mempty 0 (Proof (Just n) ps) r
+ in Q.property $
+ with (Neighbor k' h s)
+ && not (with (Neighbor k' h (s + 1)))
+ && not (with (Neighbor k h s))
+ Proof Nothing _ -> Q.discard
+ ]
+
-- the shape of the underlying tree, which doesn't depend on the scheme
structure :: TestTree
structure = testGroup "structure" [
diff --git a/test/Reference.hs b/test/Reference.hs
@@ -1,11 +1,15 @@
-- Reference MS-SMT hashing over bytes, straight from the definitions.
module Reference (
- hash_leaf
+ Leaves
+ , hash_value
+ , hash_leaf_split
+ , hash_leaf_aggregate
, hash_node_split
, hash_node_aggregate
, root
, root_aggregate
+ , slot
) where
import qualified Crypto.Hash.SHA256 as SHA256
@@ -17,18 +21,28 @@ import Data.List (partition)
import qualified Data.Map.Strict as M
import Data.Word (Word64)
-type Branch =
- BS.ByteString -> Word64 -> BS.ByteString -> Word64 -> BS.ByteString
+-- leaves, by 32-byte key
+type Leaves = M.Map BS.ByteString (BS.ByteString, Word64)
sha256 :: BSB.Builder -> BS.ByteString
sha256 = SHA256.hash . BL.toStrict . BSB.toLazyByteString
+-- sha256(value)
+hash_value :: BS.ByteString -> BS.ByteString
+hash_value = SHA256.hash
+
+-- sha256(key || sha256(value) || sum)
+hash_leaf_split :: BS.ByteString -> BS.ByteString -> Word64 -> BS.ByteString
+hash_leaf_split k v s = sha256 $
+ BSB.byteString k <> BSB.byteString (hash_value v) <> BSB.word64BE s
+
-- sha256(value || sum)
-hash_leaf :: BS.ByteString -> Word64 -> BS.ByteString
-hash_leaf v s = sha256 (BSB.byteString v <> BSB.word64BE s)
+hash_leaf_aggregate :: BS.ByteString -> Word64 -> BS.ByteString
+hash_leaf_aggregate v s = sha256 (BSB.byteString v <> BSB.word64BE s)
-- sha256(left || left sum || right || right sum)
-hash_node_split :: Branch
+hash_node_split
+ :: BS.ByteString -> Word64 -> BS.ByteString -> Word64 -> BS.ByteString
hash_node_split l sl r sr = sha256 $
BSB.byteString l <> BSB.word64BE sl <> BSB.byteString r <> BSB.word64BE sr
@@ -38,27 +52,35 @@ hash_node_aggregate
hash_node_aggregate l r s = sha256 $
BSB.byteString l <> BSB.byteString r <> BSB.word64BE s
--- the root hash and sum of the tree holding the given leaves, keyed
--- by 32-byte keys
-root :: M.Map BS.ByteString (BS.ByteString, Word64) -> (BS.ByteString, Word64)
-root = root_by hash_node_split
+-- the root hash and sum of a split tree holding the given leaves: an
+-- empty subtree commits to the zero hash, a single-leaf subtree to its
+-- leaf, and any other subtree to its children
+root :: Leaves -> (BS.ByteString, Word64)
+root = go 0 . M.toList where
+ go :: Int -> [(BS.ByteString, (BS.ByteString, Word64))]
+ -> (BS.ByteString, Word64)
+ go _ [] = (BS.replicate 32 0, 0)
+ go _ [(k, (v, s))] = (hash_leaf_split k v s, s)
+ go d ls =
+ let (rs, ls') = partition (\(k, _) -> bit k d) ls
+ (hl, sl) = go (d + 1) ls'
+ (hr, sr) = go (d + 1) rs
+ in (hash_node_split hl sl hr sr, sl + sr)
-root_aggregate
- :: M.Map BS.ByteString (BS.ByteString, Word64) -> (BS.ByteString, Word64)
-root_aggregate = root_by (\l sl r sr -> hash_node_aggregate l r (sl + sr))
+-- the root hash and sum of an aggregate tree holding the given leaves:
+-- every leaf sits at depth 256, and an empty subtree is the full
+-- subtree of empty leaves
+root_aggregate :: Leaves -> (BS.ByteString, Word64)
+root_aggregate = go empties 0 . M.toList where
+ branch l sl r sr = hash_node_aggregate l r (sl + sr)
-root_by
- :: Branch
- -> M.Map BS.ByteString (BS.ByteString, Word64)
- -> (BS.ByteString, Word64)
-root_by branch = go empties 0 . M.toList where
-- the full subtrees of empty leaves, by depth
empties = reverse . take 257 $
- iterate (\(h, _) -> (branch h 0 h 0, 0)) (hash_leaf mempty 0, 0)
+ iterate (\(h, _) -> (branch h 0 h 0, 0)) (hash_leaf_aggregate mempty 0, 0)
go (e : es) d ls = case ls of
[] -> e
- [(_, (v, s))] | d == 256 -> (hash_leaf v s, s)
+ [(_, (v, s))] | d == 256 -> (hash_leaf_aggregate v s, s)
_ ->
let (rs, ls') = partition (\(k, _) -> bit k d) ls
(hl, sl) = go es (d + 1) ls'
@@ -66,6 +88,16 @@ root_by branch = go empties 0 . M.toList where
in (branch hl sl hr sr, sl + sr)
go [] _ _ = error "reference: below leaf depth"
+-- the depth at which a key's path ends in a split tree holding the given
+-- leaves, and the other key whose leaf it ends at, if any
+slot :: Leaves -> BS.ByteString -> (Int, Maybe BS.ByteString)
+slot m k = go 0 (M.keys m) where
+ go :: Int -> [BS.ByteString] -> (Int, Maybe BS.ByteString)
+ go d ks = case ks of
+ [] -> (d, Nothing)
+ [x] -> (d, if x == k then Nothing else Just x)
+ _ -> go (d + 1) (filter (\x -> bit x d == bit k d) ks)
+
-- path bit at the given depth; LSB-first within each byte
bit :: BS.ByteString -> Int -> Bool
bit k d = testBit (BS.index k (d `quot` 8)) (d `rem` 8)