packages feed

mssmt-0.1.0: lib/Data/MSSMT/Split.hs

{-# OPTIONS_HADDOCK prune #-}

-- |
-- Module: Data.MSSMT.Split
-- Copyright: (c) 2026 Jared Tobin
-- License: MIT
-- Maintainer: Jared Tobin <jared@jtobin.io>
--
-- A merkle-sum sparse merkle tree (MS-SMT) whose branches commit to
-- the hash and sum of each of their children.
--
-- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf
-- holds a value and a sum, and commits to its key, value, and sum via
-- sha256(key || 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 commits to the zero hash and a zero sum, and a
-- subtree holding a single leaf commits to that leaf, so that a key's
-- path runs only as deep as is needed to set it apart from the others.
--
-- Leaf and branch hashes are over exactly 72 and 80 bytes
-- respectively, so that no input to one is an input to the other.

module Data.MSSMT.Split (
  -- * Keys, hashes, and commitments
    Key
  , key
  , Hash
  , parse_hash
  , serialize_hash
  , Commitment(..)

  -- * Tree
  , Tree
  , empty
  , insert
  , insert_many
  , delete
  , lookup
  , root

  -- * Proofs
  , Proof(..)
  , Neighbor(..)
  , prove
  , verify
  ) where

import qualified Data.ByteString as BS
import Data.Coerce (coerce)
import Data.MSSMT.Internal (
    Commitment(..), Hash, Key, Neighbor(..), Scheme(..), key, parse_hash
  , serialize_hash
  )
import qualified Data.MSSMT.Internal as I
import Data.Void (Void)
import Data.Word (Word64)
import Prelude hiding (lookup)

-- | A merkle-sum sparse merkle tree whose branches commit to the
--   hash and sum of each of their children.
newtype Tree = Tree (I.Tree Void)
  deriving (Eq, Show)

-- | The empty tree.
empty :: Tree
empty = Tree I.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 (Tree t) = coerce (I.complete (I.insert_with Split k v s t))

-- | Insert many values and sums, as if one at a time, but rehashing
--   each changed node once.
--
--   Where a key appears more than once, its last appearance wins, and
--   an empty value with zero sum deletes whatever is at its key.
--   Returns 'Nothing' if the resulting tree's sum would overflow.
insert_many :: [(Key, BS.ByteString, Word64)] -> Tree -> Maybe Tree
insert_many kvs (Tree t) =
  coerce (I.complete (I.insert_many_with Split kvs t))

-- | Delete the leaf at a key.
delete :: Key -> Tree -> Tree
delete k (Tree t) = Tree (I.complete (I.delete_with Split k t))

-- | Look up the value and sum at a key.
lookup :: Key -> Tree -> Maybe (BS.ByteString, Word64)
lookup k (Tree t) = I.complete (I.lookup k t)

-- | The tree's root commitment.
root :: Tree -> Commitment
root (Tree t) = I.root_with Split t

-- | A merkle proof for a key: the commitments of the siblings along its
--   path, from the deepest up to the root's, and the leaf the path ends
--   at, where that's another key's.
--
--   The number of siblings is the depth at which the path ends.
data Proof = Proof !(Maybe Neighbor) [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 (Tree t) = case I.complete (I.prove_split k t) of
  (nb, ps) -> Proof nb ps

-- | 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
--   with more than 256 siblings, or whose branch sums overflow, are
--   rejected.
verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool
verify k v s (Proof nb ps) = I.verify_split k v s nb ps