{-# LANGUAGE AllowAmbiguousTypes #-}
{-# LANGUAGE TypeApplications #-}
module Evaluation.Builtins.BLS12_381.Utils
where
import Data.Bits (complement, xor, (.&.), (.|.))
import Data.ByteString as BS (ByteString, cons, uncons)
import Data.Word (Word8)
compressionBit :: Word8
compressionBit :: Word8
compressionBit = Word8
0x80
infinityBit :: Word8
infinityBit :: Word8
infinityBit = Word8
0x40
signBit :: Word8
signBit :: Word8
signBit = Word8
0x20
unsafeUnconsBS :: ByteString -> (Word8, ByteString)
unsafeUnconsBS :: ByteString -> (Word8, ByteString)
unsafeUnconsBS ByteString
b =
case ByteString -> Maybe (Word8, ByteString)
BS.uncons ByteString
b of
Maybe (Word8, ByteString)
Nothing -> [Char] -> (Word8, ByteString)
forall a. HasCallStack => [Char] -> a
error [Char]
"Tried to uncons empty bytestring"
Just (Word8, ByteString)
p -> (Word8, ByteString)
p
modifyMSB :: (Word8 -> Word8) -> ByteString -> ByteString
modifyMSB :: (Word8 -> Word8) -> ByteString -> ByteString
modifyMSB Word8 -> Word8
f ByteString
s =
let (Word8
w,ByteString
rest) = ByteString -> (Word8, ByteString)
unsafeUnconsBS ByteString
s
in Word8 -> ByteString -> ByteString
BS.cons (Word8 -> Word8
f Word8
w) ByteString
rest
flipBits :: Word8 -> ByteString -> ByteString
flipBits :: Word8 -> ByteString -> ByteString
flipBits Word8
mask = (Word8 -> Word8) -> ByteString -> ByteString
modifyMSB (Word8
mask Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
`xor`)
clearBits :: Word8 -> ByteString -> ByteString
clearBits :: Word8 -> ByteString -> ByteString
clearBits Word8
mask = (Word8 -> Word8) -> ByteString -> ByteString
modifyMSB ((Word8 -> Word8
forall a. Bits a => a -> a
complement Word8
mask) Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&.)
setBits :: Word8 -> ByteString -> ByteString
setBits :: Word8 -> ByteString -> ByteString
setBits Word8
mask = (Word8 -> Word8) -> ByteString -> ByteString
modifyMSB (Word8
mask Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.|.)
isSet :: Word8 -> ByteString -> Bool
isSet :: Word8 -> ByteString -> Bool
isSet Word8
mask ByteString
s =
let (Word8
w,ByteString
_) = ByteString -> (Word8, ByteString)
unsafeUnconsBS ByteString
s
in Word8
w Word8 -> Word8 -> Word8
forall a. Bits a => a -> a -> a
.&. Word8
mask Word8 -> Word8 -> Bool
forall a. Eq a => a -> a -> Bool
== Word8
mask