Fixtures.hs (2860B)
1 {-# OPTIONS_GHC -fno-warn-orphans #-} 2 {-# LANGUAGE BangPatterns #-} 3 4 module Fixtures ( 5 Fixtures(..) 6 , fixtures 7 , leaves 8 , build 9 ) where 10 11 import Control.DeepSeq 12 import Control.Monad (foldM) 13 import qualified Crypto.Hash.SHA256 as SHA256 14 import Data.Bits (bit, testBit, xor) 15 import qualified Data.ByteString as BS 16 import qualified Data.ByteString.Char8 as B8 17 import Data.MSSMT 18 import qualified Data.MSSMT.Internal as I 19 import Data.Void (Void) 20 import Data.Word (Word64) 21 22 -- all fields are strict, so WHNF is NF 23 instance NFData Key where 24 rnf = rwhnf 25 26 instance NFData Commitment where 27 rnf = rwhnf 28 29 instance NFData Tree where 30 rnf = rwhnf 31 32 instance NFData Neighbor where 33 rnf = rwhnf 34 35 instance NFData Proof where 36 rnf (Proof nb ps) = rnf nb `seq` rnf ps 37 38 -- | A tree of 1024 leaves, its root, a leaf in it, a key not in it 39 -- whose slot is empty, their proofs, and a key not in it whose slot 40 -- the leaf occupies. 41 data Fixtures = Fixtures { 42 f_tree :: !Tree 43 , f_root :: !Commitment 44 , f_key :: !Key 45 , f_value :: !BS.ByteString 46 , f_sum :: !Word64 47 , f_proof :: !Proof 48 , f_out :: !Key 49 , f_xproof :: !Proof 50 , f_near :: !Key 51 } 52 53 instance NFData Fixtures where 54 rnf (Fixtures t r k v s p o x n) = 55 rnf t `seq` rnf r `seq` rnf k `seq` rnf v `seq` rnf s 56 `seq` rnf p `seq` rnf o `seq` rnf x `seq` rnf n 57 58 fixtures :: Fixtures 59 fixtures = Fixtures t (root t) k v s (prove k t) o (prove o t) n where 60 t = build (leaves 1024) 61 (k, v, s) = leaf_of 1 62 o = key_of 0 63 n = near k raw 64 raw = case foldM ins I.empty (leaves 1024) of 65 Just r -> r 66 Nothing -> error "bang" 67 ins r (k', v', s') = I.complete (I.insert_with I.Split k' v' s' r) 68 69 -- a key whose path ends at the given key's leaf, diverging from it just 70 -- beneath: the given key, with the bit beneath its leaf flipped 71 near :: Key -> I.Tree Void -> Key 72 near (I.Key h) = flip_at . go 0 where 73 bs = I.serialize_hash h 74 go :: Int -> I.Tree Void -> Int 75 go !d t = case t of 76 I.Node l _ r -> go (d + 1) (if kbit d then r else l) 77 I.Leaf {} -> d 78 I.Empty -> d 79 kbit d = testBit (BS.index bs (d `quot` 8)) (d `rem` 8) 80 flip_at d = case BS.splitAt (d `quot` 8) bs of 81 (a, b) | Just (w, c) <- BS.uncons b 82 , Just n <- key (a <> BS.cons (w `xor` bit (d `rem` 8)) c) -> n 83 _ -> error "bang" 84 85 -- | n distinct leaves. 86 leaves :: Int -> [(Key, BS.ByteString, Word64)] 87 leaves n = fmap leaf_of [1 .. n] 88 89 leaf_of :: Int -> (Key, BS.ByteString, Word64) 90 leaf_of i = 91 (key_of i, SHA256.hash (B8.pack ('v' : show i)), fromIntegral i) 92 93 key_of :: Int -> Key 94 key_of i = case key (SHA256.hash (B8.pack (show i))) of 95 Just k -> k 96 Nothing -> error "bang" 97 98 -- | Build a tree from leaves. 99 build :: [(Key, BS.ByteString, Word64)] -> Tree 100 build ls = case foldM (\t (k, v, s) -> insert k v s t) empty ls of 101 Just t -> t 102 Nothing -> error "bang"