-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #84 from zkFold/eitan-binary2
Binary package and class
- Loading branch information
Showing
13 changed files
with
106 additions
and
92 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,52 +1,46 @@ | ||
module ZkFold.Base.Data.ByteString where | ||
|
||
import Data.ByteString (ByteString, cons, empty, uncons) | ||
import Data.Map (Map, toList) | ||
import qualified Data.Vector as V | ||
import Data.Word (Word8) | ||
{-# LANGUAGE DerivingStrategies #-} | ||
|
||
module ZkFold.Base.Data.ByteString | ||
( Binary (..) | ||
, toByteString | ||
, fromByteString | ||
, putWord8 | ||
, getWord8 | ||
, LittleEndian (..) | ||
) where | ||
|
||
import Control.Applicative (many) | ||
import Data.Binary | ||
import Data.Binary.Get | ||
import Data.Binary.Put | ||
import qualified Data.ByteString as Strict | ||
import qualified Data.ByteString.Lazy as Lazy | ||
import Data.Foldable (foldl') | ||
import Numeric.Natural (Natural) | ||
import Prelude | ||
|
||
-- This module is currently used for transcripts in non-interactive proof protocols. | ||
-- TODO (Issue #19): use a serialisation library instead | ||
|
||
class ToByteString a where | ||
toByteString :: a -> ByteString | ||
|
||
instance ToByteString ByteString where | ||
toByteString = id | ||
|
||
-- Little-endian encoding for unsigned integers | ||
instance ToByteString Integer where | ||
toByteString n | ||
| n < 0 = error "ToByteString: Negative numbers are not supported" | ||
| n == 0 = empty | ||
| otherwise = | ||
let bs = toByteString (n `div` 256) | ||
b = fromIntegral (n `mod` 256) :: Word8 | ||
in b `cons` bs | ||
|
||
instance (ToByteString a, ToByteString b) => ToByteString (a, b) where | ||
toByteString (a, b) = toByteString a <> toByteString b | ||
|
||
instance ToByteString a => ToByteString [a] where | ||
toByteString = foldMap toByteString | ||
|
||
instance ToByteString a => ToByteString (V.Vector a) where | ||
toByteString = V.foldMap toByteString | ||
|
||
instance (ToByteString k, ToByteString a) => ToByteString (Map k a) where | ||
toByteString = toByteString . toList | ||
|
||
class FromByteString a where | ||
fromByteString :: ByteString -> Maybe a | ||
|
||
instance FromByteString ByteString where | ||
fromByteString = Just | ||
|
||
instance FromByteString Integer where | ||
fromByteString bs | ||
| bs == empty = Just 0 | ||
| otherwise = do | ||
(b, bs') <- uncons bs | ||
let r = fromIntegral b :: Integer | ||
(r +) . (256 *) <$> fromByteString bs' | ||
toByteString :: Binary a => a -> Strict.ByteString | ||
toByteString = Lazy.toStrict . runPut . put | ||
|
||
fromByteString :: Binary a => Strict.ByteString -> Maybe a | ||
fromByteString x = case runGetOrFail get (Lazy.fromStrict x) of | ||
Left _ -> Nothing | ||
Right (leftover, _, a) -> | ||
if Lazy.null leftover then Just a else Nothing | ||
|
||
-- Little-endian encoding for unsigned & unsized integers | ||
-- un little, deux little, trois little endians | ||
newtype LittleEndian = LittleEndian {unLittleEndian :: Natural} | ||
deriving stock (Read, Show) | ||
deriving newtype (Eq, Ord, Num, Enum, Real, Integral) | ||
instance Binary LittleEndian where | ||
get = do | ||
ns <- many getWord8 | ||
let accum n w8 = n * 256 + fromIntegral w8 | ||
littleEndian = LittleEndian (foldl' accum 0 ns) | ||
return littleEndian | ||
put (LittleEndian n) | ||
| n == 0 = mempty | ||
| otherwise = | ||
let (n', r) = n `divMod` 256 | ||
in putWord8 (fromIntegral r) <> put (LittleEndian n') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters