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 729efb7649715237653d5153b45c604fd5654cbf
parent 485b72aab2e5246dc778ae04f0dbd024ba65f36a
Author: Jared Tobin <jared@jtobin.io>
Date:   Wed,  4 Nov 2026 23:02:27 +0400

test: proofs whose sums overflow are rejected

Forges, under both schemes, a proof whose deepest sibling's sum makes
the leaf's path wrap, along with the root it'd reach under wrapping
arithmetic, and checks that verification rejects it, while accepting
the same construction one short of overflowing.

Diffstat:
Mtest/Main.hs | 38+++++++++++++++++++++++++++++++++++++-
1 file changed, 37 insertions(+), 1 deletion(-)

diff --git a/test/Main.hs b/test/Main.hs @@ -11,6 +11,7 @@ import qualified Data.Map.Strict as M import Data.Maybe (isJust) import Data.MSSMT import qualified Data.MSSMT.Aggregate as Agg +import qualified Data.MSSMT.Hash as H import qualified Data.MSSMT.Internal as I import Data.Word (Word32, Word64) import Prelude hiding (lookup) @@ -96,7 +97,41 @@ units = testGroup "units" [ 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) + , testCase "proofs whose sums overflow are rejected" $ + case key z of + Nothing -> assertFailure "key" + Just k -> do + -- roots over the leaf ("a", 1) at the all-zero key, whose path + -- runs left at every level, and siblings of hash g, the + -- deepest of the given sum and the rest of zero sum, with + -- sums added as words, which wrap + let g = H.unsafe_hash (BS.replicate 32 7) + gb = serialize_hash g + split_root n = + let h = R.hash_node_split (R.hash_leaf_split z "a" 1) 1 gb n + in Commitment (H.unsafe_hash h) (1 + n) + aggregate_root n = + let up (h, t) gs = + (R.hash_node_aggregate h gb (t + gs), t + gs) + (rh, rt) = foldl up (R.hash_leaf_aggregate "a" 1, 1) + (n : replicate 255 0) + in Commitment (H.unsafe_hash rh) rt + split_proof n = Proof Nothing [Commitment g n] + aggregate_proof n = + Agg.Proof (Commitment g n : replicate 255 (Commitment g 0)) + fits = maxBound - 1 + assertBool "split, fits" $ + verify k "a" 1 (split_proof fits) (split_root fits) + assertBool "split, overflows" . not $ + verify k "a" 1 (split_proof maxBound) (split_root maxBound) + assertBool "aggregate, fits" $ + Agg.verify k "a" 1 (aggregate_proof fits) (aggregate_root fits) + assertBool "aggregate, overflows" . not $ + Agg.verify k "a" 1 (aggregate_proof maxBound) + (aggregate_root maxBound) ] + where + z = BS.replicate 32 0 -- hashing -------------------------------------------------------------------- @@ -417,7 +452,8 @@ partial partial name sc pf deleting = testGroup ("partial trees (" <> name <> ")") [ Q.testProperty "lookup" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> with es $ \t (h, m) -> - fmap (I.lookup k) (I.expand (from_store m) k h) Q.=== Just (I.lookup k t) + fmap (I.lookup k) (I.expand (from_store m) k h) + Q.=== Just (I.lookup k t) , Q.testProperty "prove" $ \x@(Es _ es) -> Q.forAll (pick x) $ \k -> with es $ \t (h, m) ->