mssmt-0.1.0: test/Reference.hs
-- Reference MS-SMT hashing over bytes, straight from the definitions.
module Reference (
Leaves
, hash_value
, hash_leaf_split
, hash_leaf_aggregate
, hash_node_split
, hash_node_aggregate
, root
, root_aggregate
, slot
) where
import qualified Crypto.Hash.SHA256 as SHA256
import Data.Bits (testBit)
import qualified Data.ByteString as BS
import qualified Data.ByteString.Builder as BSB
import qualified Data.ByteString.Lazy as BL
import Data.List (partition)
import qualified Data.Map.Strict as M
import Data.Word (Word64)
-- leaves, by 32-byte key
type Leaves = M.Map BS.ByteString (BS.ByteString, Word64)
sha256 :: BSB.Builder -> BS.ByteString
sha256 = SHA256.hash . BL.toStrict . BSB.toLazyByteString
-- sha256(value)
hash_value :: BS.ByteString -> BS.ByteString
hash_value = SHA256.hash
-- sha256(key || sha256(value) || sum)
hash_leaf_split :: BS.ByteString -> BS.ByteString -> Word64 -> BS.ByteString
hash_leaf_split k v s = sha256 $
BSB.byteString k <> BSB.byteString (hash_value v) <> BSB.word64BE s
-- sha256(value || sum)
hash_leaf_aggregate :: BS.ByteString -> Word64 -> BS.ByteString
hash_leaf_aggregate v s = sha256 (BSB.byteString v <> BSB.word64BE s)
-- sha256(left || left sum || right || right sum)
hash_node_split
:: BS.ByteString -> Word64 -> BS.ByteString -> Word64 -> BS.ByteString
hash_node_split l sl r sr = sha256 $
BSB.byteString l <> BSB.word64BE sl <> BSB.byteString r <> BSB.word64BE sr
-- sha256(left || right || sum)
hash_node_aggregate
:: BS.ByteString -> BS.ByteString -> Word64 -> BS.ByteString
hash_node_aggregate l r s = sha256 $
BSB.byteString l <> BSB.byteString r <> BSB.word64BE s
-- the root hash and sum of a split tree holding the given leaves: an
-- empty subtree commits to the zero hash, a single-leaf subtree to its
-- leaf, and any other subtree to its children
root :: Leaves -> (BS.ByteString, Word64)
root = go 0 . M.toList where
go :: Int -> [(BS.ByteString, (BS.ByteString, Word64))]
-> (BS.ByteString, Word64)
go _ [] = (BS.replicate 32 0, 0)
go _ [(k, (v, s))] = (hash_leaf_split k v s, s)
go d ls =
let (rs, ls') = partition (\(k, _) -> bit k d) ls
(hl, sl) = go (d + 1) ls'
(hr, sr) = go (d + 1) rs
in (hash_node_split hl sl hr sr, sl + sr)
-- the root hash and sum of an aggregate tree holding the given leaves:
-- every leaf sits at depth 256, and an empty subtree is the full
-- subtree of empty leaves
root_aggregate :: Leaves -> (BS.ByteString, Word64)
root_aggregate = go empties 0 . M.toList where
branch l sl r sr = hash_node_aggregate l r (sl + sr)
-- the full subtrees of empty leaves, by depth
empties = reverse . take 257 $
iterate (\(h, _) -> (branch h 0 h 0, 0)) (hash_leaf_aggregate mempty 0, 0)
go (e : es) d ls = case ls of
[] -> e
[(_, (v, s))] | d == 256 -> (hash_leaf_aggregate v s, s)
_ ->
let (rs, ls') = partition (\(k, _) -> bit k d) ls
(hl, sl) = go es (d + 1) ls'
(hr, sr) = go es (d + 1) rs
in (branch hl sl hr sr, sl + sr)
go [] _ _ = error "reference: below leaf depth"
-- the depth at which a key's path ends in a split tree holding the given
-- leaves, and the other key whose leaf it ends at, if any
slot :: Leaves -> BS.ByteString -> (Int, Maybe BS.ByteString)
slot m k = go 0 (M.keys m) where
go :: Int -> [BS.ByteString] -> (Int, Maybe BS.ByteString)
go d ks = case ks of
[] -> (d, Nothing)
[x] -> (d, if x == k then Nothing else Just x)
_ -> go (d + 1) (filter (\x -> bit x d == bit k d) ks)
-- path bit at the given depth; LSB-first within each byte
bit :: BS.ByteString -> Int -> Bool
bit k d = testBit (BS.index k (d `quot` 8)) (d `rem` 8)