Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/mpf-write/MPF.hs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ module MPF

-- * Re-exports from Proof
, module MPF.Proof.Insertion
, module MPF.Proof.Exclusion

-- * Re-exports from Backend
, module MPF.Backend.Standalone
Expand All @@ -32,4 +33,5 @@ import MPF.Hashes.Aiken
import MPF.Hashes.CBOR
import MPF.Insertion
import MPF.Interface
import MPF.Proof.Exclusion
import MPF.Proof.Insertion
29 changes: 23 additions & 6 deletions lib/mpf-write/MPF/Hashes/Aiken.hs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ encodeBranch branchJump (HexDigit pos) siblingHashes =
-- | Encode a Fork step.
--
-- @skip@ = length of the fork's branch jump prefix.
-- Inner Neighbor: nibble (int), prefix (packed nibbles), root (32 bytes).
-- Inner Neighbor: nibble (int), prefix (one byte per nibble),
-- root (32 bytes).
encodeFork
:: HexKey -> HexDigit -> HexKey -> MPFHash -> Builder.Builder
encodeFork branchJump (HexDigit nibble) neighborPrefix neighborRoot =
Expand All @@ -135,11 +136,20 @@ encodeFork branchJump (HexDigit nibble) neighborPrefix neighborRoot =
<> cborTag 121
<> listBegin
<> cborUInt (fromIntegral nibble)
<> cborBytes (packHexKey neighborPrefix)
<> cborBytes (encodeNibblePrefix neighborPrefix)
<> cborBytes (renderMPFHash neighborRoot)
<> cborBreak
<> cborBreak

-- | Encode a fork prefix as one byte per nibble.
--
-- This matches the upstream JS/Aiken codec, where a prefix
-- like @[1,2]@ becomes bytes @01 02@ rather than a single
-- packed byte @12@.
encodeNibblePrefix :: HexKey -> ByteString
encodeNibblePrefix =
B.pack . map (\(HexDigit d) -> d)

-- | Encode a Leaf step.
--
-- @skip@ = length of the leaf's branch jump prefix.
Expand Down Expand Up @@ -305,9 +315,15 @@ unpackFullKeyPath = concatMap byteToNibbles . B.unpack
, HexDigit (b `mod` 16)
]

-- | Unpack nibble-packed prefix to HexKey
unpackNibblePrefix :: ByteString -> HexKey
unpackNibblePrefix = unpackFullKeyPath
-- | Unpack a fork prefix encoded as one byte per nibble.
unpackNibblePrefix :: ByteString -> Maybe HexKey
unpackNibblePrefix =
mapM toNibble . B.unpack
where
toNibble :: Word8 -> Maybe HexDigit
toNibble w
| w < 16 = Just (HexDigit w)
| otherwise = Nothing

-- | Parse a Branch step (after tag 121 and list begin)
parseBranchStep :: Parser (MPFProofStep MPFHash)
Expand Down Expand Up @@ -357,12 +373,13 @@ parseForkStep bs = do
(rootBS, bs6) <- parseCBORBytes bs5
((), bs7) <- parseBreak bs6
((), bs8) <- parseBreak bs7
neighborPrefix <- unpackNibblePrefix prefixBS
let branchJump = replicate skip (HexDigit 0)
Just
( ProofStepFork
{ psfBranchJump = branchJump
, psfOurPosition = HexDigit 0
, psfNeighborPrefix = unpackNibblePrefix prefixBS
, psfNeighborPrefix = neighborPrefix
, psfNeighborIndex = HexDigit (fromIntegral nibble)
, psfMerkleRoot = MPFHash rootBS
}
Expand Down
Loading