Aggregate.hs (3390B)
1 {-# OPTIONS_HADDOCK prune #-} 2 3 -- | 4 -- Module: Data.MSSMT.Aggregate 5 -- Copyright: (c) 2026 Jared Tobin 6 -- License: MIT 7 -- Maintainer: Jared Tobin <jared@jtobin.io> 8 -- 9 -- A merkle-sum sparse merkle tree (MS-SMT) whose branches commit to 10 -- their children's hashes and the sum of their sums, as in Taproot 11 -- Assets. 12 -- 13 -- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf 14 -- holds a value and a sum, and commits to both via sha256(value || 15 -- sum). Each branch commits to the hashes of its children and the sum 16 -- of their sums, via sha256(left || right || sum). A subtree holding 17 -- no leaves is the full subtree of empty leaves (an empty value with 18 -- zero sum), hashed as any other. 19 -- 20 -- Roots and proofs agree with those of the Taproot Assets MS-SMT. 21 22 module Data.MSSMT.Aggregate ( 23 -- * Keys, hashes, and commitments 24 Key 25 , key 26 , Hash 27 , parse_hash 28 , serialize_hash 29 , Commitment(..) 30 31 -- * Tree 32 , Tree 33 , empty 34 , insert 35 , insert_many 36 , delete 37 , lookup 38 , root 39 40 -- * Proofs 41 , Proof(..) 42 , prove 43 , verify 44 ) where 45 46 import qualified Data.ByteString as BS 47 import Data.Coerce (coerce) 48 import Data.MSSMT.Internal ( 49 Commitment(..), Hash, Key, Scheme(..), key, parse_hash 50 , serialize_hash 51 ) 52 import qualified Data.MSSMT.Internal as I 53 import Data.Void (Void) 54 import Data.Word (Word64) 55 import Prelude hiding (lookup) 56 57 -- | A merkle-sum sparse merkle tree whose branches commit to their 58 -- children's hashes and the sum of their sums. 59 newtype Tree = Tree (I.Tree Void) 60 deriving (Eq, Show) 61 62 -- | The empty tree. 63 empty :: Tree 64 empty = Tree I.empty 65 66 -- | Insert a value and sum at a key, replacing any existing leaf. 67 -- 68 -- Returns 'Nothing' if the tree's sum would overflow. 69 insert :: Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree 70 insert k v s (Tree t) = coerce (I.complete (I.insert_with Aggregate k v s t)) 71 72 -- | Insert many values and sums, as if one at a time, but rehashing 73 -- each changed node once. 74 -- 75 -- Where a key appears more than once, its last appearance wins, and 76 -- an empty value with zero sum deletes whatever is at its key. 77 -- Returns 'Nothing' if the resulting tree's sum would overflow. 78 insert_many :: [(Key, BS.ByteString, Word64)] -> Tree -> Maybe Tree 79 insert_many kvs (Tree t) = 80 coerce (I.complete (I.insert_many_with Aggregate kvs t)) 81 82 -- | Delete the leaf at a key. 83 delete :: Key -> Tree -> Tree 84 delete k (Tree t) = Tree (I.complete (I.delete_with Aggregate k t)) 85 86 -- | Look up the value and sum at a key. 87 lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) 88 lookup k (Tree t) = I.complete (I.lookup k t) 89 90 -- | The tree's root commitment. 91 root :: Tree -> Commitment 92 root (Tree t) = I.root_with Aggregate t 93 94 -- | A merkle proof for a key: the commitments of the 256 siblings along 95 -- its path, from the leaf up to the root's. 96 newtype Proof = Proof [Commitment] 97 deriving (Eq, Show) 98 99 -- | Produce a proof for the leaf at a key. 100 -- 101 -- If no leaf exists at the key, this proves its exclusion. 102 prove :: Key -> Tree -> Proof 103 prove k (Tree t) = Proof (I.complete (I.prove_aggregate k t)) 104 105 -- | Verify that a value and sum sit at a key, under a root. 106 -- 107 -- An empty value with zero sum verifies the key's exclusion. Proofs 108 -- whose branch sums overflow are rejected. 109 verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool 110 verify k v s (Proof ps) = I.verify_aggregate k v s ps