commit 4b8eee2bcb5df7d8826ff037a48dcfec5d154ee4
parent 408b06214aa1baad1155c91a9ec6d2da43230bdd
Author: Jared Tobin <jared@jtobin.io>
Date: Wed, 2 Dec 2026 14:06:08 +0400
lib: keys as unpacked words
Diffstat:
3 files changed, 36 insertions(+), 21 deletions(-)
diff --git a/bench/Fixtures.hs b/bench/Fixtures.hs
@@ -69,7 +69,8 @@ fixtures = Fixtures t (root t) k v s (prove k t) o (prove o t) n where
-- a key whose path ends at the given key's leaf, diverging from it just
-- beneath: the given key, with the bit beneath its leaf flipped
near :: Key -> I.Tree Void -> Key
-near (I.Key bs) = flip_at . go 0 where
+near (I.Key h) = flip_at . go 0 where
+ bs = I.serialize_hash h
go :: Int -> I.Tree Void -> Int
go !d t = case t of
I.Node l _ r -> go (d + 1) (if kbit d then r else l)
diff --git a/lib/Data/MSSMT/Internal.hs b/lib/Data/MSSMT/Internal.hs
@@ -59,9 +59,8 @@ module Data.MSSMT.Internal (
, verify_aggregate
) where
-import Data.Bits (testBit)
+import Data.Bits (unsafeShiftR, xor, (.&.))
import qualified Data.ByteString as BS
-import qualified Data.ByteString.Unsafe as BU
import Data.MSSMT.Hash
import Data.List (sortBy)
import qualified Data.Primitive.PrimArray as PA
@@ -72,15 +71,14 @@ import qualified GHC.Exts as Exts
import GHC.Exts (State#)
import Prelude hiding (lookup)
--- | A 256-bit key.
-newtype Key = Key BS.ByteString
+-- | A 256-bit key, held as a hash's big-endian words, so that keys
+-- are ordered as their bytes are.
+newtype Key = Key Hash
deriving (Eq, Ord, Show)
-- | Parse a 32-byte key.
key :: BS.ByteString -> Maybe Key
-key bs
- | BS.length bs == 32 = Just (Key bs)
- | otherwise = Nothing
+key = fmap Key . parse_hash
-- | A node's commitment: its hash, and its sum.
data Commitment = Commitment {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64
@@ -98,7 +96,9 @@ data Tree s =
Empty
-- a subtree holding exactly one leaf: the subtree's commitment at
-- its height, the leaf's hash, and the leaf's key and value
- | Leaf {-# UNPACK #-} !Commitment {-# UNPACK #-} !Hash !Key !BS.ByteString
+ | Leaf
+ {-# UNPACK #-} !Commitment {-# UNPACK #-} !Hash {-# UNPACK #-} !Key
+ !BS.ByteString
| Node !(Tree s) {-# UNPACK #-} !Commitment !(Tree s)
-- a subtree held elsewhere: what stands for it, and its commitment
| Stub !s {-# UNPACK #-} !Commitment
@@ -170,9 +170,19 @@ total (Node _ (Commitment _ s) _) = s
total (Stub _ (Commitment _ s)) = s
{-# INLINE total #-}
--- path bit at the given depth; LSB-first within each byte
+-- path bit at the given depth; LSB-first within each byte. a word
+-- holds its bytes big-endian, so the bit at depth d is at 8 * (7 - j) +
+-- i in word d / 64, for j = d / 8 mod 8 and i = d mod 8: d mod 64 with
+-- j's bits flipped
bit :: Key -> Int -> Bool
-bit (Key k) d = testBit (BU.unsafeIndex k (d `quot` 8)) (d `rem` 8)
+bit (Key (Hash w0 w1 w2 w3)) d =
+ w `unsafeShiftR` ((d .&. 63) `xor` 56) .&. 1 /= 0
+ where
+ w = case d `unsafeShiftR` 6 of
+ 0 -> w0
+ 1 -> w1
+ 2 -> w2
+ _ -> w3
{-# INLINE bit #-}
-- whether two keys' paths agree above the given depth
@@ -181,7 +191,7 @@ agree k k' d = all (\j -> bit k j == bit k' j) [0 .. d - 1]
-- a key, as a hash's words
key_hash :: Key -> Hash
-key_hash (Key k) = unsafe_hash k
+key_hash (Key h) = h
-- a leaf's hash under a scheme, over the given state and block arrays
hash_leaf#
@@ -226,12 +236,13 @@ diverge k k' = go where
-- raise a single-leaf subtree's commitment from one depth to a
-- shallower one: in a split tree, it's the same at any depth, and in an
-- aggregate tree, it's paired with an empty sibling at each level
--- between
+-- between. strict in the key and depths under either scheme, so that
+-- they're passed unboxed
raise#
:: Scheme -> Buffer -> Buffer -> Key -> Int -> Int -> Commitment
-> State# Exts.RealWorld -> (# State# Exts.RealWorld, Commitment #)
-raise# Split _ _ _ _ _ c = \s0 -> (# s0, c #)
-raise# sc st bk k to from (Commitment h0 s) = go (from - 1) h0 where
+raise# Split _ _ !_ !_ !_ c = \s0 -> (# s0, c #)
+raise# sc st bk !k !to from (Commitment h0 s) = go (from - 1) h0 where
go !d !h s0
| d < to = let !c = Commitment h s in (# s0, c #)
| otherwise =
@@ -336,9 +347,11 @@ insert_leaf sc k v s t0 = with_scratch $ \st bk s00 ->
-- a leaf in a subtree being rebuilt in a batch: one being put, one
-- being deleted, or an existing one, kept with its hash
data Item =
- Put !Key !BS.ByteString {-# UNPACK #-} !Word64
- | Del !Key
- | Kept !Key {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64 !BS.ByteString
+ Put {-# UNPACK #-} !Key !BS.ByteString {-# UNPACK #-} !Word64
+ | Del {-# UNPACK #-} !Key
+ | Kept
+ {-# UNPACK #-} !Key {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64
+ !BS.ByteString
item_key :: Item -> Key
item_key (Put k _ _) = k
@@ -518,7 +531,8 @@ root_with sc = commit sc 0
-- | Another key's leaf, at which a key's path ends: its key, the hash
-- of its value, and its sum.
-data Neighbor = Neighbor !Key {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64
+data Neighbor =
+ Neighbor {-# UNPACK #-} !Key {-# UNPACK #-} !Hash {-# UNPACK #-} !Word64
deriving (Eq, Show)
-- whether climbing from a commitment at the given depth on a key's
diff --git a/test/Main.hs b/test/Main.hs
@@ -256,7 +256,7 @@ big = Q.oneof [
-- a key's raw bytes
raw_key :: Key -> BS.ByteString
-raw_key (I.Key bs) = bs
+raw_key (I.Key h) = I.serialize_hash h
-- key the model by raw bytes
raw :: M.Map Key a -> M.Map BS.ByteString a
@@ -264,7 +264,7 @@ raw = M.mapKeysMonotonic raw_key
-- path bit at the given depth; LSB-first within each byte
kbit :: Key -> Int -> Bool
-kbit (I.Key bs) d = testBit (BS.index bs (d `quot` 8)) (d `rem` 8)
+kbit k d = testBit (BS.index (raw_key k) (d `quot` 8)) (d `rem` 8)
-- every branch holds two or more leaves, and every leaf's key follows
-- its path