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 9ceca3857e0d7ad4dfdf9535d3e0b3e3791cb667
parent 195ac5c34ef7471024cf0a2fef662a7f76556f69
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 20:17:48 +0400

lib: commit branches to each child's sum

Branches now hash (left || left sum || right || right sum), committing
to each child's full commitment, rather than (left || right || sum).
Empty subtrees are the full subtrees of empty leaves under the same
hash, so the table of their hashes is purely a cache.

The Taproot Assets branch hash is kept as the Aggregate scheme, for
testing against its vectors. The implementation moves to
Data.MSSMT.Internal, where tree operations take a Scheme (Split or
Aggregate); Data.MSSMT exports the Split API.

Tests add a byte-level reference implementation (test/Reference.hs),
against which the leaf and branch hashes (native and pure) and roots
under both schemes are checked. The taproot-assets vectors now run
under Aggregate.

Diffstat:
Mlib/Data/MSSMT.hs | 354++-----------------------------------------------------------------------------
Alib/Data/MSSMT/Internal.hs | 505+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mmssmt.cabal | 5++++-
Mtest/Main.hs | 54++++++++++++++++++++++++++++++++++++++++++++++++++++++
Atest/Reference.hs | 71+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Mtest/Vectors.hs | 27++++++++++++++++-----------
6 files changed, 658 insertions(+), 358 deletions(-)

diff --git a/lib/Data/MSSMT.hs b/lib/Data/MSSMT.hs @@ -1,8 +1,4 @@ {-# OPTIONS_HADDOCK prune #-} -{-# LANGUAGE BangPatterns #-} -{-# LANGUAGE MagicHash #-} -{-# LANGUAGE UnboxedTuples #-} -{-# LANGUAGE UnliftedFFITypes #-} -- | -- Module: Data.MSSMT @@ -10,13 +6,15 @@ -- License: MIT -- Maintainer: Jared Tobin <jared@jtobin.io> -- --- A merkle-sum sparse merkle tree (MS-SMT), after the construction --- used by Taproot Assets. +-- A merkle-sum sparse merkle tree (MS-SMT). -- -- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf --- holds a value and a sum; each branch commits to its children's --- hashes and to the sum of their sums. Subtrees holding no leaves are --- empty, and have fixed hashes at each depth. +-- 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 ( -- * Keys, hashes, and commitments @@ -41,341 +39,5 @@ module Data.MSSMT ( , verify ) where -import qualified Crypto.Hash.SHA256 as SHA256 -import qualified Crypto.Hash.SHA256.Internal as SHA256I -import Data.Bits (testBit, unsafeShiftL, unsafeShiftR, (.|.)) -import qualified Data.ByteString as BS -import qualified Data.ByteString.Internal as BI -import qualified Data.ByteString.Unsafe as BU -import qualified Data.Primitive.PrimArray as PA -import Data.Word (Word8, Word64) -import Foreign.C.Types (CInt(..)) -import Foreign.Marshal.Utils (copyBytes) -import Foreign.Ptr (Ptr, castPtr) -import Foreign.Storable (pokeByteOff) -import qualified GHC.Exts as Exts -import GHC.Exts (Int#, MutableByteArray#, State#, (+#), (*#)) -import GHC.IO (unIO, unsafeDupablePerformIO) -import GHC.Word (Word32(..)) +import Data.MSSMT.Internal import Prelude hiding (lookup) - --- | A 256-bit key. -newtype Key = Key BS.ByteString - deriving (Eq, Ord, Show) - --- | Parse a 32-byte key. -key :: BS.ByteString -> Maybe Key -key bs - | BS.length bs == 32 = Just (Key bs) - | otherwise = Nothing - --- | A 256-bit hash. -data Hash = Hash - {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64 - {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64 - deriving (Eq, Ord, Show) - --- | Parse a 32-byte hash. -parse_hash :: BS.ByteString -> Maybe Hash -parse_hash bs - | BS.length bs == 32 = Just (unsafe_hash bs) - | otherwise = Nothing - --- | Serialize a hash to 32 bytes. -serialize_hash :: Hash -> BS.ByteString -serialize_hash (Hash a b c d) = BI.unsafeCreate 32 $ \p -> do - poke_be64 p 00 a - poke_be64 p 08 b - poke_be64 p 16 c - poke_be64 p 24 d - --- | A node's commitment: its hash, and its sum. -data Commitment = Commitment {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 - deriving (Eq, Show) - --- | A merkle-sum sparse merkle tree. --- --- Leaves sit at depth 256, and empty subtrees are collapsed to --- 'Empty', so a tree holding n leaves has at most 256 * n branches. -data Tree = - Empty - | Leaf {-# UNPACK #-} !Commitment !BS.ByteString - | Node !Tree {-# UNPACK #-} !Commitment !Tree - deriving (Eq, Show) - --- bytes ---------------------------------------------------------------------- - --- 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 = - byte i `unsafeShiftL` 56 .|. byte (i + 1) `unsafeShiftL` 48 - .|. byte (i + 2) `unsafeShiftL` 40 .|. byte (i + 3) `unsafeShiftL` 32 - .|. byte (i + 4) `unsafeShiftL` 24 .|. byte (i + 5) `unsafeShiftL` 16 - .|. byte (i + 6) `unsafeShiftL` 08 .|. byte (i + 7) - byte i = fromIntegral (BU.unsafeIndex bs i) :: Word64 -{-# INLINE unsafe_hash #-} - -poke_be64 :: Ptr Word8 -> Int -> Word64 -> IO () -poke_be64 p i w = do - pokeByteOff p (i + 0) (fromIntegral (w `unsafeShiftR` 56) :: Word8) - pokeByteOff p (i + 1) (fromIntegral (w `unsafeShiftR` 48) :: Word8) - pokeByteOff p (i + 2) (fromIntegral (w `unsafeShiftR` 40) :: Word8) - pokeByteOff p (i + 3) (fromIntegral (w `unsafeShiftR` 32) :: Word8) - pokeByteOff p (i + 4) (fromIntegral (w `unsafeShiftR` 24) :: Word8) - pokeByteOff p (i + 5) (fromIntegral (w `unsafeShiftR` 16) :: Word8) - pokeByteOff p (i + 6) (fromIntegral (w `unsafeShiftR` 08) :: Word8) - pokeByteOff p (i + 7) (fromIntegral w :: Word8) -{-# INLINE poke_be64 #-} - --- hashing -------------------------------------------------------------------- - --- sha256(value || sum) -hash_leaf :: BS.ByteString -> Word64 -> Hash -hash_leaf 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 - where - l = BS.length v - --- sha256(left || right || sum) --- --- the 72-byte message is exactly two blocks: left || right, then --- sum || 0x80 || 0^44 || 576 (the message length in bits, big-endian) -hash_node :: Hash -> Hash -> Word64 -> Hash -hash_node l r s - | native = hash_node_native l r s - | otherwise = hash_node_pure l r s -{-# INLINE hash_node #-} - --- ppad-sha256's hardware block function, and its availability check; --- the block function updates eight native-endian state words in --- place, given sixteen native-endian message words -foreign import ccall unsafe "sha256_block_native" - c_sha256_block - :: MutableByteArray# Exts.RealWorld - -> MutableByteArray# Exts.RealWorld - -> IO () - -foreign import ccall unsafe "sha256_native_available" - c_sha256_native_available :: IO CInt - -native :: Bool -native = unsafeDupablePerformIO c_sha256_native_available /= 0 -{-# NOINLINE native #-} - --- initial SHA-256 state -iv :: Hash -iv = Hash - 0x6a09e667bb67ae85 0x3c6ef372a54ff53a - 0x510e527f9b05688c 0x1f83d9ab5be0cd19 - -hash_node_native :: Hash -> Hash -> Word64 -> Hash -hash_node_native l r s = case Exts.runRW# go of - (# _, h #) -> h - where - go s0 = - case Exts.newByteArray# 32# s0 of { (# s1, st #) -> - case Exts.newByteArray# 64# s1 of { (# s2, bk #) -> - case put_hash st 0# iv s2 of { s3 -> - case put_hash bk 0# l s3 of { s4 -> - case put_hash bk 4# r s4 of { s5 -> - case unIO (c_sha256_block st bk) s5 of { (# s6, () #) -> - case put_w64 bk 0# s s6 of { s7 -> - case Exts.writeWord32Array# bk 2# (w32 0x80000000) s7 of { s8 -> - case Exts.setByteArray# bk 12# 48# 0# s8 of { s9 -> - case Exts.writeWord32Array# bk 15# (w32 576) s9 of { s10 -> - case unIO (c_sha256_block st bk) s10 of { (# s11, () #) -> - get_hash st s11 }}}}}}}}}}} - -hash_node_pure :: Hash -> Hash -> Word64 -> Hash -hash_node_pure (Hash a0 a1 a2 a3) (Hash b0 b1 b2 b3) s = - let !r0 = SHA256I.iv () - !r1 = SHA256I.update r0 $ SHA256I.B - (hi a0) (lo a0) (hi a1) (lo a1) (hi a2) (lo a2) (hi a3) (lo a3) - (hi b0) (lo b0) (hi b1) (lo b1) (hi b2) (lo b2) (hi b3) (lo b3) - !(SHA256I.R x0 x1 x2 x3 x4 x5 x6 x7) = SHA256I.update r1 $ SHA256I.B - (hi s) (lo s) (w32 0x80000000) z z z z z z z z z z z z (w32 576) - in Hash (w64 x0 x1) (w64 x2 x3) (w64 x4 x5) (w64 x6 x7) - where - z = w32 0 - --- words ---------------------------------------------------------------------- - -w32 :: Word -> Exts.Word32# -w32 (Exts.W# w) = Exts.wordToWord32# w -{-# INLINE w32 #-} - -hi :: Word64 -> Exts.Word32# -hi w = case fromIntegral (w `unsafeShiftR` 32) of W32# x -> x -{-# INLINE hi #-} - -lo :: Word64 -> Exts.Word32# -lo w = case fromIntegral w of W32# x -> x -{-# INLINE lo #-} - -w64 :: Exts.Word32# -> Exts.Word32# -> Word64 -w64 h l = fromIntegral (W32# h) `unsafeShiftL` 32 .|. fromIntegral (W32# l) -{-# INLINE w64 #-} - --- write a word as two 32-bit words, high first, at a 64-bit offset -put_w64 - :: MutableByteArray# s -> Int# -> Word64 -> State# s -> State# s -put_w64 a i w s0 = - case Exts.writeWord32Array# a (2# *# i) (hi w) s0 of - s1 -> Exts.writeWord32Array# a (2# *# i +# 1#) (lo w) s1 -{-# INLINE put_w64 #-} - --- write a hash at a 64-bit offset -put_hash - :: MutableByteArray# s -> Int# -> Hash -> State# s -> State# s -put_hash a i (Hash w0 w1 w2 w3) s0 = - case put_w64 a i w0 s0 of { s1 -> - case put_w64 a (i +# 1#) w1 s1 of { s2 -> - case put_w64 a (i +# 2#) w2 s2 of { s3 -> - put_w64 a (i +# 3#) w3 s3 }}} -{-# INLINE put_hash #-} - --- read a hash from eight 32-bit words -get_hash :: MutableByteArray# s -> State# s -> (# State# s, Hash #) -get_hash a s0 = - case Exts.readWord32Array# a 0# s0 of { (# s1, x0 #) -> - case Exts.readWord32Array# a 1# s1 of { (# s2, x1 #) -> - case Exts.readWord32Array# a 2# s2 of { (# s3, x2 #) -> - case Exts.readWord32Array# a 3# s3 of { (# s4, x3 #) -> - case Exts.readWord32Array# a 4# s4 of { (# s5, x4 #) -> - case Exts.readWord32Array# a 5# s5 of { (# s6, x5 #) -> - case Exts.readWord32Array# a 6# s6 of { (# s7, x6 #) -> - case Exts.readWord32Array# a 7# s7 of { (# s8, x7 #) -> - (# s8, Hash (w64 x0 x1) (w64 x2 x3) (w64 x4 x5) (w64 x6 x7) #) - }}}}}}}} -{-# INLINE get_hash #-} - --- structure ------------------------------------------------------------------ - --- leaf depth -_HEIGHT :: Int -_HEIGHT = 256 - --- hashes of empty subtrees, four words per depth -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_node h h 0) (hash_leaf mempty 0) -{-# NOINLINE empties #-} - --- commitment of a subtree at the given depth -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 -{-# INLINE commit #-} - --- path bit at the given depth; LSB-first within each byte -bit :: Key -> Int -> Bool -bit (Key k) d = testBit (BU.unsafeIndex k (d `quot` 8)) (d `rem` 8) -{-# INLINE bit #-} - --- an empty value with zero sum is the empty leaf -leaf :: BS.ByteString -> Word64 -> Tree -leaf v s - | BS.null v && s == 0 = Empty - | 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 !(Commitment hl sl) = commit (d + 1) l - !(Commitment hr sr) = commit (d + 1) r - !s = sl + sr - in Node l (Commitment (hash_node hl hr s) s) r -{-# INLINE node #-} - --- children of a subtree above leaf depth -children :: Tree -> (# Tree, Tree #) -children (Node l _ r) = (# l, r #) -children Empty = (# Empty, Empty #) -children (Leaf _ _) = (# Empty, Empty #) -- unreachable above leaf depth -{-# INLINE children #-} - --- replace the subtree at the given key's leaf -update :: Key -> Tree -> Tree -> Tree -update k new = go 0 where - go !d t - | d == _HEIGHT = new - | otherwise = case children t of - (# l, r #) - | bit k d -> node d l (go (d + 1) r) - | otherwise -> node d (go (d + 1) l) r - --- tree ----------------------------------------------------------------------- - --- | The empty tree. -empty :: Tree -empty = 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 t - | s > maxBound - rest = Nothing - | otherwise = Just (update k (leaf v s) t) - where - Commitment _ total = root t - rest = total - maybe 0 snd (lookup k t) - --- | Delete the leaf at a key. -delete :: Key -> Tree -> Tree -delete k = update k Empty - --- | Look up the value and sum at a key. -lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) -lookup k = go 0 where - go !d t = case t of - Empty -> Nothing - 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 -> 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 [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 = Proof . go 0 [] where - go !d acc t - | d == _HEIGHT = acc - | otherwise = case children t of - (# l, r #) - | bit k d -> go (d + 1) (commit (d + 1) l : acc) r - | otherwise -> go (d + 1) (commit (d + 1) r : acc) l - --- | 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 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 && Commitment h hs == r - Commitment g gs : rest - | d < 0 || gs > maxBound - hs -> False - | otherwise -> - let !t = hs + gs - !p | bit k d = hash_node g h t - | otherwise = hash_node h g t - in go (d - 1) p t rest diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs @@ -0,0 +1,505 @@ +{-# OPTIONS_HADDOCK hide #-} +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE UnboxedTuples #-} +{-# LANGUAGE UnliftedFFITypes #-} + +-- | +-- Module: Data.MSSMT.Internal +-- Copyright: (c) 2026 Jared Tobin +-- License: MIT +-- Maintainer: Jared Tobin <jared@jtobin.io> +-- +-- MS-SMT internals. + +module Data.MSSMT.Internal ( + -- * Keys, hashes, and commitments + Key(..) + , key + , Hash(..) + , parse_hash + , serialize_hash + , Commitment(..) + + -- * Hashing + , native + , hash_leaf + , hash_node + , hash_node_native + , hash_node_pure + , hash_node_aggregate + , hash_node_aggregate_native + , hash_node_aggregate_pure + + -- * Schemes + , Scheme(..) + + -- * Tree + , Tree(..) + , empty + , insert + , insert_with + , delete + , delete_with + , lookup + , root + , root_with + + -- * Proofs + , Proof(..) + , prove + , prove_with + , verify + , verify_with + ) where + +import qualified Crypto.Hash.SHA256 as SHA256 +import qualified Crypto.Hash.SHA256.Internal as SHA256I +import Data.Bits (testBit, unsafeShiftL, unsafeShiftR, (.|.)) +import qualified Data.ByteString as BS +import qualified Data.ByteString.Internal as BI +import qualified Data.ByteString.Unsafe as BU +import qualified Data.Primitive.PrimArray as PA +import Data.Word (Word8, Word64) +import Foreign.C.Types (CInt(..)) +import Foreign.Marshal.Utils (copyBytes) +import Foreign.Ptr (Ptr, castPtr) +import Foreign.Storable (pokeByteOff) +import qualified GHC.Exts as Exts +import GHC.Exts (Int#, MutableByteArray#, State#, (+#), (*#)) +import GHC.IO (unIO, unsafeDupablePerformIO) +import GHC.Word (Word32(..)) +import Prelude hiding (lookup) + +-- | A 256-bit key. +newtype Key = Key BS.ByteString + deriving (Eq, Ord, Show) + +-- | Parse a 32-byte key. +key :: BS.ByteString -> Maybe Key +key bs + | BS.length bs == 32 = Just (Key bs) + | otherwise = Nothing + +-- | A 256-bit hash. +data Hash = Hash + {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64 + {-# UNPACK #-} !Word64 {-# UNPACK #-} !Word64 + deriving (Eq, Ord, Show) + +-- | Parse a 32-byte hash. +parse_hash :: BS.ByteString -> Maybe Hash +parse_hash bs + | BS.length bs == 32 = Just (unsafe_hash bs) + | otherwise = Nothing + +-- | Serialize a hash to 32 bytes. +serialize_hash :: Hash -> BS.ByteString +serialize_hash (Hash a b c d) = BI.unsafeCreate 32 $ \p -> do + poke_be64 p 00 a + poke_be64 p 08 b + poke_be64 p 16 c + poke_be64 p 24 d + +-- | A node's commitment: its hash, and its sum. +data Commitment = Commitment {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 + deriving (Eq, Show) + +-- | A merkle-sum sparse merkle tree. +-- +-- Leaves sit at depth 256, and empty subtrees are collapsed to +-- 'Empty', so a tree holding n leaves has at most 256 * n branches. +data Tree = + Empty + | Leaf {-# UNPACK #-} !Commitment !BS.ByteString + | Node !Tree {-# UNPACK #-} !Commitment !Tree + deriving (Eq, Show) + +-- bytes ---------------------------------------------------------------------- + +-- 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 = + byte i `unsafeShiftL` 56 .|. byte (i + 1) `unsafeShiftL` 48 + .|. byte (i + 2) `unsafeShiftL` 40 .|. byte (i + 3) `unsafeShiftL` 32 + .|. byte (i + 4) `unsafeShiftL` 24 .|. byte (i + 5) `unsafeShiftL` 16 + .|. byte (i + 6) `unsafeShiftL` 08 .|. byte (i + 7) + byte i = fromIntegral (BU.unsafeIndex bs i) :: Word64 +{-# INLINE unsafe_hash #-} + +poke_be64 :: Ptr Word8 -> Int -> Word64 -> IO () +poke_be64 p i w = do + pokeByteOff p (i + 0) (fromIntegral (w `unsafeShiftR` 56) :: Word8) + pokeByteOff p (i + 1) (fromIntegral (w `unsafeShiftR` 48) :: Word8) + pokeByteOff p (i + 2) (fromIntegral (w `unsafeShiftR` 40) :: Word8) + pokeByteOff p (i + 3) (fromIntegral (w `unsafeShiftR` 32) :: Word8) + pokeByteOff p (i + 4) (fromIntegral (w `unsafeShiftR` 24) :: Word8) + pokeByteOff p (i + 5) (fromIntegral (w `unsafeShiftR` 16) :: Word8) + pokeByteOff p (i + 6) (fromIntegral (w `unsafeShiftR` 08) :: Word8) + pokeByteOff p (i + 7) (fromIntegral w :: Word8) +{-# INLINE poke_be64 #-} + +-- hashing -------------------------------------------------------------------- + +-- | sha256(value || sum). +hash_leaf :: BS.ByteString -> Word64 -> Hash +hash_leaf 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 + where + l = BS.length v + +-- | sha256(left || left sum || right || right sum). +-- +-- The 80-byte message is exactly two blocks: left || left sum || the +-- first 24 bytes of right, then the last 8 bytes of right || right +-- sum || 0x80 || 0^39 || 640 (the message length in bits, as a +-- big-endian 64-bit word). +hash_node :: Hash -> Word64 -> Hash -> Word64 -> Hash +hash_node l sl r sr + | native = hash_node_native l sl r sr + | otherwise = hash_node_pure l sl r sr +{-# INLINE hash_node #-} + +-- | sha256(left || right || sum), the Taproot Assets branch hash. +-- +-- The 72-byte message is exactly two blocks: left || right, then +-- sum || 0x80 || 0^47 || 576 (the message length in bits, as a +-- big-endian 64-bit word). +hash_node_aggregate :: Hash -> Hash -> Word64 -> Hash +hash_node_aggregate l r s + | native = hash_node_aggregate_native l r s + | otherwise = hash_node_aggregate_pure l r s +{-# INLINE hash_node_aggregate #-} + +-- ppad-sha256's hardware block function, and its availability check; +-- the block function updates eight native-endian state words in +-- place, given sixteen native-endian message words +foreign import ccall unsafe "sha256_block_native" + c_sha256_block + :: MutableByteArray# Exts.RealWorld + -> MutableByteArray# Exts.RealWorld + -> IO () + +foreign import ccall unsafe "sha256_native_available" + c_sha256_native_available :: IO CInt + +-- | Are hardware SHA-256 instructions available? +native :: Bool +native = unsafeDupablePerformIO c_sha256_native_available /= 0 +{-# NOINLINE native #-} + +-- initial SHA-256 state +iv :: Hash +iv = Hash + 0x6a09e667bb67ae85 0x3c6ef372a54ff53a + 0x510e527f9b05688c 0x1f83d9ab5be0cd19 + +-- | 'hash_node', via hardware SHA-256. Callers check 'native'. +hash_node_native :: Hash -> Word64 -> Hash -> Word64 -> Hash +hash_node_native l sl (Hash b0 b1 b2 b3) sr = case Exts.runRW# go of + (# _, h #) -> h + where + go s0 = + case Exts.newByteArray# 32# s0 of { (# s1, st #) -> + case Exts.newByteArray# 64# s1 of { (# s2, bk #) -> + case put_hash st 0# iv s2 of { s3 -> + case put_hash bk 0# l s3 of { s4 -> + case put_w64 bk 4# sl s4 of { s5 -> + case put_w64 bk 5# b0 s5 of { s6 -> + case put_w64 bk 6# b1 s6 of { s7 -> + case put_w64 bk 7# b2 s7 of { s8 -> + case unIO (c_sha256_block st bk) s8 of { (# s9, () #) -> + case put_w64 bk 0# b3 s9 of { s10 -> + case put_w64 bk 1# sr s10 of { s11 -> + case Exts.writeWord32Array# bk 4# (w32 0x80000000) s11 of { s12 -> + case Exts.setByteArray# bk 20# 40# 0# s12 of { s13 -> + case Exts.writeWord32Array# bk 15# (w32 640) s13 of { s14 -> + case unIO (c_sha256_block st bk) s14 of { (# s15, () #) -> + get_hash st s15 }}}}}}}}}}}}}}} + +-- | 'hash_node', in pure Haskell. +hash_node_pure :: Hash -> Word64 -> Hash -> Word64 -> Hash +hash_node_pure (Hash a0 a1 a2 a3) sl (Hash b0 b1 b2 b3) sr = + let !r0 = SHA256I.iv () + !r1 = SHA256I.update r0 $ SHA256I.B + (hi a0) (lo a0) (hi a1) (lo a1) (hi a2) (lo a2) (hi a3) (lo a3) + (hi sl) (lo sl) (hi b0) (lo b0) (hi b1) (lo b1) (hi b2) (lo b2) + !(SHA256I.R x0 x1 x2 x3 x4 x5 x6 x7) = SHA256I.update r1 $ SHA256I.B + (hi b3) (lo b3) (hi sr) (lo sr) (w32 0x80000000) + z z z z z z z z z z (w32 640) + in Hash (w64 x0 x1) (w64 x2 x3) (w64 x4 x5) (w64 x6 x7) + where + z = w32 0 + +-- | 'hash_node_aggregate', via hardware SHA-256. Callers check 'native'. +hash_node_aggregate_native :: Hash -> Hash -> Word64 -> Hash +hash_node_aggregate_native l r s = case Exts.runRW# go of + (# _, h #) -> h + where + go s0 = + case Exts.newByteArray# 32# s0 of { (# s1, st #) -> + case Exts.newByteArray# 64# s1 of { (# s2, bk #) -> + case put_hash st 0# iv s2 of { s3 -> + case put_hash bk 0# l s3 of { s4 -> + case put_hash bk 4# r s4 of { s5 -> + case unIO (c_sha256_block st bk) s5 of { (# s6, () #) -> + case put_w64 bk 0# s s6 of { s7 -> + case Exts.writeWord32Array# bk 2# (w32 0x80000000) s7 of { s8 -> + case Exts.setByteArray# bk 12# 48# 0# s8 of { s9 -> + case Exts.writeWord32Array# bk 15# (w32 576) s9 of { s10 -> + case unIO (c_sha256_block st bk) s10 of { (# s11, () #) -> + get_hash st s11 }}}}}}}}}}} + +-- | 'hash_node_aggregate', in pure Haskell. +hash_node_aggregate_pure :: Hash -> Hash -> Word64 -> Hash +hash_node_aggregate_pure (Hash a0 a1 a2 a3) (Hash b0 b1 b2 b3) s = + let !r0 = SHA256I.iv () + !r1 = SHA256I.update r0 $ SHA256I.B + (hi a0) (lo a0) (hi a1) (lo a1) (hi a2) (lo a2) (hi a3) (lo a3) + (hi b0) (lo b0) (hi b1) (lo b1) (hi b2) (lo b2) (hi b3) (lo b3) + !(SHA256I.R x0 x1 x2 x3 x4 x5 x6 x7) = SHA256I.update r1 $ SHA256I.B + (hi s) (lo s) (w32 0x80000000) z z z z z z z z z z z z (w32 576) + in Hash (w64 x0 x1) (w64 x2 x3) (w64 x4 x5) (w64 x6 x7) + where + z = w32 0 + +-- words ---------------------------------------------------------------------- + +w32 :: Word -> Exts.Word32# +w32 (Exts.W# w) = Exts.wordToWord32# w +{-# INLINE w32 #-} + +hi :: Word64 -> Exts.Word32# +hi w = case fromIntegral (w `unsafeShiftR` 32) of W32# x -> x +{-# INLINE hi #-} + +lo :: Word64 -> Exts.Word32# +lo w = case fromIntegral w of W32# x -> x +{-# INLINE lo #-} + +w64 :: Exts.Word32# -> Exts.Word32# -> Word64 +w64 h l = fromIntegral (W32# h) `unsafeShiftL` 32 .|. fromIntegral (W32# l) +{-# INLINE w64 #-} + +-- write a word as two 32-bit words, high first, at a 64-bit offset +put_w64 + :: MutableByteArray# s -> Int# -> Word64 -> State# s -> State# s +put_w64 a i w s0 = + case Exts.writeWord32Array# a (2# *# i) (hi w) s0 of + s1 -> Exts.writeWord32Array# a (2# *# i +# 1#) (lo w) s1 +{-# INLINE put_w64 #-} + +-- write a hash at a 64-bit offset +put_hash + :: MutableByteArray# s -> Int# -> Hash -> State# s -> State# s +put_hash a i (Hash w0 w1 w2 w3) s0 = + case put_w64 a i w0 s0 of { s1 -> + case put_w64 a (i +# 1#) w1 s1 of { s2 -> + case put_w64 a (i +# 2#) w2 s2 of { s3 -> + put_w64 a (i +# 3#) w3 s3 }}} +{-# INLINE put_hash #-} + +-- read a hash from eight 32-bit words +get_hash :: MutableByteArray# s -> State# s -> (# State# s, Hash #) +get_hash a s0 = + case Exts.readWord32Array# a 0# s0 of { (# s1, x0 #) -> + case Exts.readWord32Array# a 1# s1 of { (# s2, x1 #) -> + case Exts.readWord32Array# a 2# s2 of { (# s3, x2 #) -> + case Exts.readWord32Array# a 3# s3 of { (# s4, x3 #) -> + case Exts.readWord32Array# a 4# s4 of { (# s5, x4 #) -> + case Exts.readWord32Array# a 5# s5 of { (# s6, x5 #) -> + case Exts.readWord32Array# a 6# s6 of { (# s7, x6 #) -> + case Exts.readWord32Array# a 7# s7 of { (# s8, x7 #) -> + (# s8, Hash (w64 x0 x1) (w64 x2 x3) (w64 x4 x5) (w64 x6 x7) #) + }}}}}}}} +{-# INLINE get_hash #-} + +-- schemes -------------------------------------------------------------------- + +-- | A branch hashing scheme. +data Scheme = + -- | Branches commit to each child's hash and sum, via 'hash_node'. + Split + -- | Branches commit to their children's hashes and the sum of their + -- sums, via 'hash_node_aggregate', as in Taproot Assets. + | Aggregate + deriving (Eq, Show) + +-- branch hash under a scheme; callers rule out sum overflow +hash_branch :: Scheme -> Hash -> Word64 -> Hash -> Word64 -> Hash +hash_branch Split l sl r sr = hash_node l sl r sr +hash_branch Aggregate l sl r sr = hash_node_aggregate l r (sl + sr) +{-# INLINE hash_branch #-} + +-- structure ------------------------------------------------------------------ + +-- leaf depth +_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 +-- other subtree +empties_with :: Scheme -> PA.PrimArray Word64 +empties_with sc = 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 +{-# NOINLINE empties #-} + +empties_aggregate :: PA.PrimArray Word64 +empties_aggregate = empties_with Aggregate +{-# NOINLINE empties_aggregate #-} + +-- commitment of a subtree at the given depth +commit :: Scheme -> Int -> Tree -> Commitment +commit sc d Empty = Commitment (Hash (ix 0) (ix 1) (ix 2) (ix 3)) 0 where + es = case sc of + Split -> empties + Aggregate -> empties_aggregate + ix i = PA.indexPrimArray es (4 * d + i) +commit _ _ (Leaf p _) = p +commit _ _ (Node _ p _) = p +{-# INLINE commit #-} + +-- sum of a subtree +total :: Tree -> Word64 +total Empty = 0 +total (Leaf (Commitment _ s) _) = s +total (Node _ (Commitment _ s) _) = s +{-# INLINE total #-} + +-- path bit at the given depth; LSB-first within each byte +bit :: Key -> Int -> Bool +bit (Key k) d = testBit (BU.unsafeIndex k (d `quot` 8)) (d `rem` 8) +{-# INLINE bit #-} + +-- an empty value with zero sum is the empty leaf +leaf :: BS.ByteString -> Word64 -> Tree +leaf v s + | BS.null v && s == 0 = Empty + | otherwise = Leaf (Commitment (hash_leaf v s) s) v + +-- branch at the given depth; two empty children make an empty subtree, +-- as hashing them would. callers rule out sum overflow +node :: Scheme -> Int -> Tree -> Tree -> Tree +node _ _ Empty Empty = Empty +node sc d l r = + let !(Commitment hl sl) = commit sc (d + 1) l + !(Commitment hr sr) = commit sc (d + 1) r + in Node l (Commitment (hash_branch sc hl sl hr sr) (sl + sr)) r +{-# INLINE node #-} + +-- children of a subtree above leaf depth +children :: Tree -> (# Tree, Tree #) +children (Node l _ r) = (# l, r #) +children Empty = (# Empty, Empty #) +children (Leaf _ _) = (# Empty, Empty #) -- unreachable above leaf depth +{-# INLINE children #-} + +-- replace the subtree at the given key's leaf +update :: Scheme -> Key -> Tree -> Tree -> Tree +update sc k new = go 0 where + go !d t + | d == _HEIGHT = new + | otherwise = case children t of + (# l, r #) + | bit k d -> node sc d l (go (d + 1) r) + | otherwise -> node sc d (go (d + 1) l) r + +-- tree ----------------------------------------------------------------------- + +-- | The empty tree. +empty :: Tree +empty = 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 = insert_with Split + +-- | 'insert', under a scheme. +insert_with + :: Scheme -> Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree +insert_with sc k v s t + | s > maxBound - rest = Nothing + | otherwise = Just (update sc k (leaf 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_with :: Scheme -> Key -> Tree -> Tree +delete_with sc k = update sc k Empty + +-- | Look up the value and sum at a key. +lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) +lookup k = go 0 where + go !d t = case t of + Empty -> Nothing + 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 -> Commitment +root = root_with Split + +-- | 'root', under a scheme. +root_with :: Scheme -> Tree -> Commitment +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] + 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 = prove_with Split + +-- | 'prove', under a scheme. +prove_with :: Scheme -> Key -> Tree -> Proof +prove_with sc k = Proof . go 0 [] where + go !d acc t + | d == _HEIGHT = acc + | otherwise = case children t of + (# l, r #) + | bit k d -> go (d + 1) (commit sc (d + 1) l : acc) r + | otherwise -> go (d + 1) (commit sc (d + 1) r : acc) l + +-- | 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 = verify_with Split + +-- | 'verify', under a scheme. +verify_with + :: Scheme -> Key -> BS.ByteString -> Word64 -> Proof -> Commitment + -> Bool +verify_with sc 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 && Commitment h hs == r + Commitment g gs : rest + | d < 0 || gs > maxBound - hs -> False + | otherwise -> + let !p | bit k d = hash_branch sc g gs h hs + | otherwise = hash_branch sc h hs g gs + in go (d - 1) p (hs + gs) rest diff --git a/mssmt.cabal b/mssmt.cabal @@ -25,6 +25,7 @@ library -Wall exposed-modules: Data.MSSMT + , Data.MSSMT.Internal build-depends: base >= 4.11 && < 5 , bytestring >= 0.10.2 && < 0.13 @@ -37,7 +38,8 @@ test-suite mssmt-tests hs-source-dirs: test main-is: Main.hs other-modules: - Vectors + Reference + , Vectors ghc-options: -rtsopts -Wall -O2 @@ -49,6 +51,7 @@ test-suite mssmt-tests , containers , mssmt , ppad-base16 + , ppad-sha256 , tasty , tasty-hunit , tasty-quickcheck diff --git a/test/Main.hs b/test/Main.hs @@ -8,11 +8,13 @@ import qualified Data.ByteString as BS import qualified Data.Map.Strict as M import Data.Maybe (isJust) import Data.MSSMT +import qualified Data.MSSMT.Internal as I import Data.Word (Word32, Word64) import Prelude hiding (lookup) import Test.Tasty import Test.Tasty.HUnit import qualified Test.Tasty.QuickCheck as Q +import qualified Reference as R import qualified Vectors as V main :: IO () @@ -20,6 +22,7 @@ main = do vs <- traverse load files defaultMain $ testGroup "mssmt" [ units + , hashing , properties , testGroup "taproot-assets vectors" (zipWith V.execute files vs) ] @@ -52,6 +55,9 @@ 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" $ + let Commitment h s = root empty + in assertEqual mempty (R.root M.empty) (serialize_hash h, s) , testCase "proofs have 256 siblings" $ case key (BS.replicate 32 0) of Nothing -> assertFailure "key" @@ -66,6 +72,37 @@ units = testGroup "units" [ not (verify k "a" 1 (Proof (ps <> take 1 ps)) (root t)) ] +-- hashing -------------------------------------------------------------------- + +gen_hash :: Q.Gen Hash +gen_hash = fmap BS.pack (Q.vector 32) `Q.suchThatMap` parse_hash + +-- each hash agrees with the reference; the native paths are checked +-- 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 + , split "hash_node_pure" I.hash_node_pure + , aggregate "hash_node_aggregate_pure" I.hash_node_aggregate_pure + ] + <> [ split "hash_node_native" I.hash_node_native | I.native ] + <> [ aggregate "hash_node_aggregate_native" I.hash_node_aggregate_native + | I.native + ] + where + split name f = Q.testProperty (name <> " ~ reference") $ + Q.forAll gen_hash $ \l -> Q.forAll big $ \sl -> + Q.forAll gen_hash $ \r -> Q.forAll big $ \sr -> + serialize_hash (f l sl r sr) + Q.=== R.hash_node (serialize_hash l) sl (serialize_hash r) sr + aggregate name f = Q.testProperty (name <> " ~ reference") $ + Q.forAll gen_hash $ \l -> Q.forAll gen_hash $ \r -> + Q.forAll big $ \s -> + serialize_hash (f l r s) + Q.=== R.hash_node_aggregate (serialize_hash l) (serialize_hash r) s + -- properties ----------------------------------------------------------------- -- a key with the given prefix @@ -113,6 +150,10 @@ big = Q.oneof [ , (maxBound -) . fromIntegral <$> (Q.arbitrary :: Q.Gen Word32) ] +-- key the model by raw bytes +raw :: M.Map Key a -> M.Map BS.ByteString a +raw = M.mapKeysMonotonic (\(I.Key bs) -> bs) + with_tree :: [E] -> (Tree -> Q.Property) -> Q.Property with_tree es f = case build es of Nothing -> Q.counterexample "overflow" False @@ -130,6 +171,19 @@ properties = testGroup "properties" [ let Commitment _ s = 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) = I.insert_with I.Aggregate k v s t + in case foldM ins empty es of + Nothing -> Q.counterexample "overflow" False + Just t -> + let Commitment h s = I.root_with I.Aggregate t + in (serialize_hash h, s) Q.=== R.root_aggregate (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)] diff --git a/test/Reference.hs b/test/Reference.hs @@ -0,0 +1,71 @@ +-- Reference MS-SMT hashing over bytes, straight from the definitions. + +module Reference ( + hash_leaf + , hash_node + , hash_node_aggregate + , root + , root_aggregate + ) where + +import qualified Crypto.Hash.SHA256 as SHA256 +import Data.Bits (testBit) +import qualified Data.ByteString as BS +import qualified Data.ByteString.Builder as BSB +import qualified Data.ByteString.Lazy as BL +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 + +sha256 :: BSB.Builder -> BS.ByteString +sha256 = SHA256.hash . BL.toStrict . BSB.toLazyByteString + +-- sha256(value || sum) +hash_leaf :: BS.ByteString -> Word64 -> BS.ByteString +hash_leaf v s = sha256 (BSB.byteString v <> BSB.word64BE s) + +-- sha256(left || left sum || right || right sum) +hash_node :: Branch +hash_node l sl r sr = sha256 $ + BSB.byteString l <> BSB.word64BE sl <> BSB.byteString r <> BSB.word64BE sr + +-- sha256(left || right || sum) +hash_node_aggregate + :: BS.ByteString -> BS.ByteString -> Word64 -> BS.ByteString +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 + +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)) + +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) + + go (e : es) d ls = case ls of + [] -> e + [(_, (v, s))] | d == 256 -> (hash_leaf v s, s) + _ -> + let (rs, ls') = partition (\(k, _) -> bit k d) ls + (hl, sl) = go es (d + 1) ls' + (hr, sr) = go es (d + 1) rs + in (branch hl sl hr sr, sl + sr) + go [] _ _ = error "reference: below leaf depth" + +-- 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) diff --git a/test/Vectors.hs b/test/Vectors.hs @@ -20,6 +20,10 @@ 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 qualified Data.Set as S import Data.Word (Word64) import Test.Tasty (TestTree, testGroup) @@ -63,33 +67,34 @@ 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 k v s t) +insert_all = foldM (\t (Leaf k v s) -> insert_with Aggregate 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 tree) + assertEqual mempty (Just v_root) (fmap (root_with Aggregate) 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 k t) - assertBool ("verify " <> show i) (verify k v s p (root t)) + 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) _ -> 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 k t) - assertBool ("verify " <> show i) - (verify k mempty 0 p (root t)) + 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) 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 t v_deleted) v_replaced + insert_all (foldr (delete_with Aggregate) t v_deleted) v_replaced -- every insert but the last succeeds; the last overflows execute_invalid :: [Leaf] -> Invalid -> TestTree @@ -176,11 +181,11 @@ decompress bs0 = do es <- empties Proof <$> fill bits nodes es where - -- empty-subtree commitments, leaf up: the siblings of any key in - -- the empty tree + -- aggregate empty-subtree commitments, leaf up: the siblings of any + -- key in the empty tree empties = do k <- key (BS.replicate 32 0) - let Proof es = prove k empty + let Proof es = prove_with Aggregate k empty pure es siblings :: Int -> BS.ByteString -> Maybe ([Commitment], BS.ByteString)