Reference.hs (3698B)
1 -- Reference MS-SMT hashing over bytes, straight from the definitions. 2 3 module Reference ( 4 Leaves 5 , hash_value 6 , hash_leaf_split 7 , hash_leaf_aggregate 8 , hash_node_split 9 , hash_node_aggregate 10 , root 11 , root_aggregate 12 , slot 13 ) where 14 15 import qualified Crypto.Hash.SHA256 as SHA256 16 import Data.Bits (testBit) 17 import qualified Data.ByteString as BS 18 import qualified Data.ByteString.Builder as BSB 19 import qualified Data.ByteString.Lazy as BL 20 import Data.List (partition) 21 import qualified Data.Map.Strict as M 22 import Data.Word (Word64) 23 24 -- leaves, by 32-byte key 25 type Leaves = M.Map BS.ByteString (BS.ByteString, Word64) 26 27 sha256 :: BSB.Builder -> BS.ByteString 28 sha256 = SHA256.hash . BL.toStrict . BSB.toLazyByteString 29 30 -- sha256(value) 31 hash_value :: BS.ByteString -> BS.ByteString 32 hash_value = SHA256.hash 33 34 -- sha256(key || sha256(value) || sum) 35 hash_leaf_split :: BS.ByteString -> BS.ByteString -> Word64 -> BS.ByteString 36 hash_leaf_split k v s = sha256 $ 37 BSB.byteString k <> BSB.byteString (hash_value v) <> BSB.word64BE s 38 39 -- sha256(value || sum) 40 hash_leaf_aggregate :: BS.ByteString -> Word64 -> BS.ByteString 41 hash_leaf_aggregate v s = sha256 (BSB.byteString v <> BSB.word64BE s) 42 43 -- sha256(left || left sum || right || right sum) 44 hash_node_split 45 :: BS.ByteString -> Word64 -> BS.ByteString -> Word64 -> BS.ByteString 46 hash_node_split l sl r sr = sha256 $ 47 BSB.byteString l <> BSB.word64BE sl <> BSB.byteString r <> BSB.word64BE sr 48 49 -- sha256(left || right || sum) 50 hash_node_aggregate 51 :: BS.ByteString -> BS.ByteString -> Word64 -> BS.ByteString 52 hash_node_aggregate l r s = sha256 $ 53 BSB.byteString l <> BSB.byteString r <> BSB.word64BE s 54 55 -- the root hash and sum of a split tree holding the given leaves: an 56 -- empty subtree commits to the zero hash, a single-leaf subtree to its 57 -- leaf, and any other subtree to its children 58 root :: Leaves -> (BS.ByteString, Word64) 59 root = go 0 . M.toList where 60 go :: Int -> [(BS.ByteString, (BS.ByteString, Word64))] 61 -> (BS.ByteString, Word64) 62 go _ [] = (BS.replicate 32 0, 0) 63 go _ [(k, (v, s))] = (hash_leaf_split k v s, s) 64 go d ls = 65 let (rs, ls') = partition (\(k, _) -> bit k d) ls 66 (hl, sl) = go (d + 1) ls' 67 (hr, sr) = go (d + 1) rs 68 in (hash_node_split hl sl hr sr, sl + sr) 69 70 -- the root hash and sum of an aggregate tree holding the given leaves: 71 -- every leaf sits at depth 256, and an empty subtree is the full 72 -- subtree of empty leaves 73 root_aggregate :: Leaves -> (BS.ByteString, Word64) 74 root_aggregate = go empties 0 . M.toList where 75 branch l sl r sr = hash_node_aggregate l r (sl + sr) 76 77 -- the full subtrees of empty leaves, by depth 78 empties = reverse . take 257 $ 79 iterate (\(h, _) -> (branch h 0 h 0, 0)) (hash_leaf_aggregate mempty 0, 0) 80 81 go (e : es) d ls = case ls of 82 [] -> e 83 [(_, (v, s))] | d == 256 -> (hash_leaf_aggregate v s, s) 84 _ -> 85 let (rs, ls') = partition (\(k, _) -> bit k d) ls 86 (hl, sl) = go es (d + 1) ls' 87 (hr, sr) = go es (d + 1) rs 88 in (branch hl sl hr sr, sl + sr) 89 go [] _ _ = error "reference: below leaf depth" 90 91 -- the depth at which a key's path ends in a split tree holding the given 92 -- leaves, and the other key whose leaf it ends at, if any 93 slot :: Leaves -> BS.ByteString -> (Int, Maybe BS.ByteString) 94 slot m k = go 0 (M.keys m) where 95 go :: Int -> [BS.ByteString] -> (Int, Maybe BS.ByteString) 96 go d ks = case ks of 97 [] -> (d, Nothing) 98 [x] -> (d, if x == k then Nothing else Just x) 99 _ -> go (d + 1) (filter (\x -> bit x d == bit k d) ks) 100 101 -- path bit at the given depth; LSB-first within each byte 102 bit :: BS.ByteString -> Int -> Bool 103 bit k d = testBit (BS.index k (d `quot` 8)) (d `rem` 8)