Split.hs (3800B)
1 {-# OPTIONS_HADDOCK prune #-} 2 3 -- | 4 -- Module: Data.MSSMT.Split 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 -- the hash and sum of each of their children. 11 -- 12 -- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf 13 -- holds a value and a sum, and commits to its key, value, and sum via 14 -- sha256(key || sha256(value) || sum). Each branch commits to the hash 15 -- and sum of each of its children, via sha256(left || left sum || 16 -- right || right sum), and its sum is the sum of theirs. A subtree 17 -- holding no leaves commits to the zero hash and a zero sum, and a 18 -- subtree holding a single leaf commits to that leaf, so that a key's 19 -- path runs only as deep as is needed to set it apart from the others. 20 -- 21 -- Leaf and branch hashes are over exactly 72 and 80 bytes 22 -- respectively, so that no input to one is an input to the other. 23 24 module Data.MSSMT.Split ( 25 -- * Keys, hashes, and commitments 26 Key 27 , key 28 , Hash 29 , parse_hash 30 , serialize_hash 31 , Commitment(..) 32 33 -- * Tree 34 , Tree 35 , empty 36 , insert 37 , insert_many 38 , delete 39 , lookup 40 , root 41 42 -- * Proofs 43 , Proof(..) 44 , Neighbor(..) 45 , prove 46 , verify 47 ) where 48 49 import qualified Data.ByteString as BS 50 import Data.Coerce (coerce) 51 import Data.MSSMT.Internal ( 52 Commitment(..), Hash, Key, Neighbor(..), Scheme(..), key, parse_hash 53 , serialize_hash 54 ) 55 import qualified Data.MSSMT.Internal as I 56 import Data.Void (Void) 57 import Data.Word (Word64) 58 import Prelude hiding (lookup) 59 60 -- | A merkle-sum sparse merkle tree whose branches commit to the 61 -- hash and sum of each of their children. 62 newtype Tree = Tree (I.Tree Void) 63 deriving (Eq, Show) 64 65 -- | The empty tree. 66 empty :: Tree 67 empty = Tree I.empty 68 69 -- | Insert a value and sum at a key, replacing any existing leaf. 70 -- 71 -- Returns 'Nothing' if the tree's sum would overflow. 72 insert :: Key -> BS.ByteString -> Word64 -> Tree -> Maybe Tree 73 insert k v s (Tree t) = coerce (I.complete (I.insert_with Split k v s t)) 74 75 -- | Insert many values and sums, as if one at a time, but rehashing 76 -- each changed node once. 77 -- 78 -- Where a key appears more than once, its last appearance wins, and 79 -- an empty value with zero sum deletes whatever is at its key. 80 -- Returns 'Nothing' if the resulting tree's sum would overflow. 81 insert_many :: [(Key, BS.ByteString, Word64)] -> Tree -> Maybe Tree 82 insert_many kvs (Tree t) = 83 coerce (I.complete (I.insert_many_with Split kvs t)) 84 85 -- | Delete the leaf at a key. 86 delete :: Key -> Tree -> Tree 87 delete k (Tree t) = Tree (I.complete (I.delete_with Split k t)) 88 89 -- | Look up the value and sum at a key. 90 lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64) 91 lookup k (Tree t) = I.complete (I.lookup k t) 92 93 -- | The tree's root commitment. 94 root :: Tree -> Commitment 95 root (Tree t) = I.root_with Split t 96 97 -- | A merkle proof for a key: the commitments of the siblings along its 98 -- path, from the deepest up to the root's, and the leaf the path ends 99 -- at, where that's another key's. 100 -- 101 -- The number of siblings is the depth at which the path ends. 102 data Proof = Proof !(Maybe Neighbor) [Commitment] 103 deriving (Eq, Show) 104 105 -- | Produce a proof for the leaf at a key. 106 -- 107 -- If no leaf exists at the key, this proves its exclusion. 108 prove :: Key -> Tree -> Proof 109 prove k (Tree t) = case I.complete (I.prove_split k t) of 110 (nb, ps) -> Proof nb ps 111 112 -- | Verify that a value and sum sit at a key, under a root. 113 -- 114 -- An empty value with zero sum verifies the key's exclusion. Proofs 115 -- with more than 256 siblings, or whose branch sums overflow, are 116 -- rejected. 117 verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool 118 verify k v s (Proof nb ps) = I.verify_split k v s nb ps