{-# OPTIONS_GHC -O2 -feager-blackholing #-}
{-# LANGUAGE Trustworthy, NoImplicitPrelude, MagicHash #-}
module Crypto.Common ( wordMax
, wordSize
, sizeinWords
, log2len
, testcond
)
where
import safe Prelude (Num(..),Int,($),(+),(-),fromInteger,Integral,Integer,(>),toInteger,maxBound,quotRem)
import safe qualified Data.Bits as B (Bits(..),FiniteBits(..))
import safe qualified Data.Word as W (Word)
import GHC.Exts
import GHC.Integer.Logarithms
wordMax :: (Integral a) => a
wordMax :: a
wordMax = Integer -> a
forall a. Num a => Integer -> a
fromInteger (Integer -> a) -> Integer -> a
forall a b. (a -> b) -> a -> b
$ Word -> Integer
forall a. Integral a => a -> Integer
toInteger (Word
forall a. Bounded a => a
maxBound::W.Word)
wordSize :: Int
wordSize :: Int
wordSize = Word -> Int
forall b. FiniteBits b => b -> Int
B.finiteBitSize (Word
0::W.Word)
{-# INLINE wordSize #-}
sizeinWords :: Int -> Int
sizeinWords :: Int -> Int
sizeinWords Int
0 = Int
1
sizeinWords Int
t = let (Int
w,Int
r) = Int -> Int
forall a. Num a => a -> a
abs Int
t Int -> Int -> (Int, Int)
forall a. Integral a => a -> a -> (a, a)
`quotRem` Int
wordSize
in if Int
r Int -> Int -> Bool
forall a. Ord a => a -> a -> Bool
> Int
0 then Int
w Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1 else Int
w
log2len :: Integer -> Int
log2len :: Integer -> Int
log2len Integer
x = (Int# -> Int
I# (Integer -> Int#
integerLog2# Integer
x)) Int -> Int -> Int
forall a. Num a => a -> a -> a
+ Int
1
{-# INLINABLE log2len #-}
testcond :: W.Word -> Int -> W.Word
testcond :: Word -> Int -> Word
testcond Word
w Int
i = Word -> Int -> Word
forall a. Bits a => a -> Int -> a
B.shift (Word -> Int -> Word
forall a. Bits a => a -> Int -> a
B.shift Word
w (Int
wordSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
i Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1)) (-(Int
wordSize Int -> Int -> Int
forall a. Num a => a -> a -> a
- Int
1))