packages feed

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

{-# OPTIONS_HADDOCK prune #-}

-- |
-- Module: Data.MSSMT.Aggregate
-- 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
-- their children's hashes and the sum of their sums, as in Taproot
-- Assets.
--
-- The tree has 2^256 leaves, addressed by 256-bit keys. Each leaf
-- holds a value and a sum, and commits to both via sha256(value ||
-- sum). Each branch commits to the hashes of its children and the sum
-- of their sums, via sha256(left || right || sum). A subtree holding
-- no leaves is the full subtree of empty leaves (an empty value with
-- zero sum), hashed as any other.
--
-- Roots and proofs agree with those of the Taproot Assets MS-SMT.

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

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

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

import qualified Data.ByteString as BS
import Data.Coerce (coerce)
import Data.MSSMT.Internal (
    Commitment(..), Hash, Key, 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 their
--   children's hashes and the sum of their sums.
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 Aggregate 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 Aggregate kvs t))

-- | Delete the leaf at a key.
delete :: Key -> Tree -> Tree
delete k (Tree t) = Tree (I.complete (I.delete_with Aggregate 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 Aggregate t

-- | A merkle proof for a key: the commitments of the 256 siblings along
--   its path, from the leaf up to the root's.
newtype Proof = Proof [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) = Proof (I.complete (I.prove_aggregate k t))

-- | 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
--   whose branch sums overflow are rejected.
verify :: Key -> BS.ByteString -> Word64 -> Proof -> Commitment -> Bool
verify k v s (Proof ps) = I.verify_aggregate k v s ps