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 d44961600f8cdf70c6688379a19e20ef1ca8df6f
parent 2e9ba1d345d5016372267545af561800806b52f1
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 20:34:20 +0400

lib: allocation-free proof verification

The hardware branch hashes become primitives (hash_node_split#,
hash_node_aggregate#) over caller-supplied state and block arrays,
which they overwrite in full. verify_with allocates one pair of arrays
and threads them through its 256-level loop, so verification no
longer allocates per level: 472 bytes per call, down from ~33KB.

Diffstat:
Mlib/Data/MSSMT/Internal.hs | 129+++++++++++++++++++++++++++++++++++++++++++++++++++----------------------------
1 file changed, 84 insertions(+), 45 deletions(-)

diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs @@ -197,28 +197,50 @@ iv = Hash 0x6a09e667bb67ae85 0x3c6ef372a54ff53a 0x510e527f9b05688c 0x1f83d9ab5be0cd19 --- | 'hash_node_split', via hardware SHA-256. Callers check 'native'. -hash_node_split_native :: Hash -> Word64 -> Hash -> Word64 -> Hash -hash_node_split_native l sl (Hash b0 b1 b2 b3) sr = case Exts.runRW# go of - (# _, h #) -> h +-- the hardware block function's state and block arrays +type Buffer = MutableByteArray# Exts.RealWorld + +-- run a computation over fresh state and block arrays +with_scratch + :: (Buffer -> Buffer -> State# Exts.RealWorld + -> (# State# Exts.RealWorld, a #)) + -> a +with_scratch f = case Exts.runRW# go of + (# _, a #) -> a 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 }}}}}}}}}}}}}}} + f st bk s2 }} +{-# INLINE with_scratch #-} + +-- | 'hash_node_split', via hardware SHA-256. Callers check 'native'. +hash_node_split_native :: Hash -> Word64 -> Hash -> Word64 -> Hash +hash_node_split_native l sl r sr = + with_scratch (\st bk -> hash_node_split# st bk l sl r sr) + +-- 'hash_node_split' via hardware SHA-256, over the given state and +-- block arrays, both of which it overwrites in full. callers check +-- 'native' +hash_node_split# + :: Buffer -> Buffer -> Hash -> Word64 -> Hash -> Word64 + -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Hash #) +hash_node_split# st bk l sl (Hash b0 b1 b2 b3) sr s0 = + case put_hash st 0# iv s0 of { s1 -> + case put_hash bk 0# l s1 of { s2 -> + case put_w64 bk 4# sl s2 of { s3 -> + case put_w64 bk 5# b0 s3 of { s4 -> + case put_w64 bk 6# b1 s4 of { s5 -> + case put_w64 bk 7# b2 s5 of { s6 -> + case unIO (c_sha256_block st bk) s6 of { (# s7, () #) -> + case put_w64 bk 0# b3 s7 of { s8 -> + case put_w64 bk 1# sr s8 of { s9 -> + case Exts.writeWord32Array# bk 4# (w32 0x80000000) s9 of { s10 -> + case Exts.setByteArray# bk 20# 40# 0# s10 of { s11 -> + case Exts.writeWord32Array# bk 15# (w32 640) s11 of { s12 -> + case unIO (c_sha256_block st bk) s12 of { (# s13, () #) -> + get_hash st s13 }}}}}}}}}}}}} +{-# INLINE hash_node_split# #-} -- | 'hash_node_split', in pure Haskell. hash_node_split_pure :: Hash -> Word64 -> Hash -> Word64 -> Hash @@ -236,22 +258,27 @@ hash_node_split_pure (Hash a0 a1 a2 a3) sl (Hash b0 b1 b2 b3) sr = -- | '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_native l r s = + with_scratch (\st bk -> hash_node_aggregate# st bk l r s) + +-- 'hash_node_aggregate' via hardware SHA-256, over the given state and +-- block arrays, both of which it overwrites in full. callers check +-- 'native' +hash_node_aggregate# + :: Buffer -> Buffer -> Hash -> Hash -> Word64 + -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Hash #) +hash_node_aggregate# st bk l r s s0 = + case put_hash st 0# iv s0 of { s1 -> + case put_hash bk 0# l s1 of { s2 -> + case put_hash bk 4# r s2 of { s3 -> + case unIO (c_sha256_block st bk) s3 of { (# s4, () #) -> + case put_w64 bk 0# s s4 of { s5 -> + case Exts.writeWord32Array# bk 2# (w32 0x80000000) s5 of { s6 -> + case Exts.setByteArray# bk 12# 48# 0# s6 of { s7 -> + case Exts.writeWord32Array# bk 15# (w32 576) s7 of { s8 -> + case unIO (c_sha256_block st bk) s8 of { (# s9, () #) -> + get_hash st s9 }}}}}}}}} +{-# INLINE hash_node_aggregate# #-} -- | 'hash_node_aggregate', in pure Haskell. hash_node_aggregate_pure :: Hash -> Hash -> Word64 -> Hash @@ -334,6 +361,18 @@ hash_branch Split l sl r sr = hash_node_split l sl r sr hash_branch Aggregate l sl r sr = hash_node_aggregate l r (sl + sr) {-# INLINE hash_branch #-} +-- 'hash_branch', over the given state and block arrays where hardware +-- SHA-256 is available +hash_branch# + :: Scheme -> Buffer -> Buffer -> Hash -> Word64 -> Hash -> Word64 + -> State# Exts.RealWorld -> (# State# Exts.RealWorld, Hash #) +hash_branch# sc st bk l sl r sr s0 + | native = case sc of + Split -> hash_node_split# st bk l sl r sr s0 + Aggregate -> hash_node_aggregate# st bk l r (sl + sr) s0 + | otherwise = (# s0, hash_branch sc l sl r sr #) +{-# INLINE hash_branch# #-} + -- structure ------------------------------------------------------------------ -- leaf depth @@ -492,14 +531,14 @@ verify = verify_with Split 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 +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