From adc192ac17777014d851c9b6fe390f534c104fe2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Olivier=20Ch=C3=A9ron?= Date: Sun, 25 Jun 2017 17:11:47 +0200 Subject: [PATCH] Add constAllZero --- Crypto/Internal/ByteArray.hs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/Crypto/Internal/ByteArray.hs b/Crypto/Internal/ByteArray.hs index 3a23152..57ab57a 100644 --- a/Crypto/Internal/ByteArray.hs +++ b/Crypto/Internal/ByteArray.hs @@ -7,13 +7,33 @@ -- -- Simple and efficient byte array types -- +{-# LANGUAGE BangPatterns #-} {-# OPTIONS_HADDOCK hide #-} module Crypto.Internal.ByteArray ( module Data.ByteArray , module Data.ByteArray.Mapping , module Data.ByteArray.Encoding + , constAllZero ) where import Data.ByteArray import Data.ByteArray.Mapping import Data.ByteArray.Encoding + +import Data.Bits ((.|.)) +import Data.Word (Word8) +import Foreign.Ptr (Ptr) +import Foreign.Storable (peekByteOff) + +import Crypto.Internal.Compat (unsafeDoIO) + +constAllZero :: ByteArrayAccess ba => ba -> Bool +constAllZero b = unsafeDoIO $ withByteArray b $ \p -> loop p 0 0 + where + loop :: Ptr b -> Int -> Word8 -> IO Bool + loop p i !acc + | i == len = return $! acc == 0 + | otherwise = do + e <- peekByteOff p i + loop p (i+1) (acc .|. e) + len = Data.ByteArray.length b