packages feed

crypton-2.3.1 / Crypto / Number / Compat.hs

{-# LANGUAGE BangPatterns #-}{-# LANGUAGE CPP #-}{-# LANGUAGE MagicHash #-}{-# LANGUAGE UnboxedTuples #-}{-# OPTIONS_GHC -Wno-deprecations #-}-- |-- Module      : Crypto.Number.Compat-- License     : BSD-style-- Maintainer  : Vincent Hanquez <vincent@snarc.org>-- Stability   : experimental-- Portability : Goodmodule Crypto.Number.Compat (    GmpSupported (..),    onGmpUnsupported,    gmpGcde,    gmpLog2,    gmpPowModSecInteger,    gmpPowModInteger,    gmpInverse,    gmpNextPrime,    gmpTestPrimeMillerRabin,    gmpSizeInBytes,    gmpSizeInBits,    gmpExportInteger,    gmpExportIntegerLE,    gmpImportInteger,    gmpImportIntegerLE,) where#ifndef MIN_VERSION_integer_gmp#define MIN_VERSION_integer_gmp(a,b,c) 0#endif#if MIN_VERSION_integer_gmp(0,5,1)import GHC.Integer.GMP.Internalsimport GHC.Baseimport GHC.Integer.Logarithms (integerLog2#)#endifimport Data.Wordimport GHC.Ptr (Ptr (..))-- | GMP Supported / Unsupporteddata GmpSupported a    = GmpSupported a    | GmpUnsupported    deriving (Show, Eq)-- | Simple combinator in case the operation is not supported through GMPonGmpUnsupported :: GmpSupported a -> a -> aonGmpUnsupported (GmpSupported a) _ = aonGmpUnsupported GmpUnsupported f = f-- | Compute the GCDE of a two integer through GMPgmpGcde :: Integer -> Integer -> GmpSupported (Integer, Integer, Integer)#if MIN_VERSION_integer_gmp(0,5,1)gmpGcde a b =    GmpSupported (s, t, g)  where (# g, s #) = gcdExtInteger a b        t = (g - s * a) `div` b#elsegmpGcde _ _ = GmpUnsupported#endif-- | Compute the binary logarithm of an integer through GMPgmpLog2 :: Integer -> GmpSupported Int#if MIN_VERSION_integer_gmp(0,5,1)gmpLog2 0 = GmpSupported 0gmpLog2 x = GmpSupported (I# (integerLog2# x))#elsegmpLog2 _ = GmpUnsupported#endif-- | Compute the power modulus using extra security to remain constant-- time wise through GMPgmpPowModSecInteger :: Integer -> Integer -> Integer -> GmpSupported Integer#if MIN_VERSION_integer_gmp(1,1,0)gmpPowModSecInteger _ _ _ = GmpUnsupported#elif MIN_VERSION_integer_gmp(1,0,2)gmpPowModSecInteger b e m = GmpSupported (powModSecInteger b e m)#elif MIN_VERSION_integer_gmp(1,0,0)gmpPowModSecInteger _ _ _ = GmpUnsupported#elif MIN_VERSION_integer_gmp(0,5,1)gmpPowModSecInteger b e m = GmpSupported (powModSecInteger b e m)#elsegmpPowModSecInteger _ _ _ = GmpUnsupported#endif-- | Compute the power modulus through GMPgmpPowModInteger :: Integer -> Integer -> Integer -> GmpSupported Integer#if MIN_VERSION_integer_gmp(0,5,1)gmpPowModInteger b e m = GmpSupported (powModInteger b e m)#elsegmpPowModInteger _ _ _ = GmpUnsupported#endif-- | Inverse modulus of a number through GMPgmpInverse :: Integer -> Integer -> GmpSupported (Maybe Integer)#if MIN_VERSION_integer_gmp(0,5,1)gmpInverse g m    | r == 0    = GmpSupported Nothing    | otherwise = GmpSupported (Just r)  where r = recipModInteger g m#elsegmpInverse _ _ = GmpUnsupported#endif-- | Get the next prime from a specific value through GMPgmpNextPrime :: Integer -> GmpSupported Integer#if MIN_VERSION_integer_gmp(1,1,0)gmpNextPrime _ = GmpUnsupported#elif MIN_VERSION_integer_gmp(0,5,1)gmpNextPrime n = GmpSupported (nextPrimeInteger n)#elsegmpNextPrime _ = GmpUnsupported#endif-- | Test if a number is prime using Miller RabingmpTestPrimeMillerRabin :: Int -> Integer -> GmpSupported Bool#if MIN_VERSION_integer_gmp(1,1,0)gmpTestPrimeMillerRabin _ _ = GmpUnsupported#elif MIN_VERSION_integer_gmp(0,5,1)gmpTestPrimeMillerRabin (I# tries) !n = GmpSupported $    case testPrimeInteger n tries of        0# -> False        _  -> True#elsegmpTestPrimeMillerRabin _ _ = GmpUnsupported#endif-- | Return the size in bytes of an integergmpSizeInBytes :: Integer -> GmpSupported Int#if MIN_VERSION_integer_gmp(0,5,1)gmpSizeInBytes n = GmpSupported (I# (word2Int# (sizeInBaseInteger n 256#)))#elsegmpSizeInBytes _ = GmpUnsupported#endif-- | Return the size in bits of an integergmpSizeInBits :: Integer -> GmpSupported Int#if MIN_VERSION_integer_gmp(0,5,1)gmpSizeInBits n = GmpSupported (I# (word2Int# (sizeInBaseInteger n 2#)))#elsegmpSizeInBits _ = GmpUnsupported#endif-- | Export an integer to a memory (big-endian)gmpExportInteger :: Integer -> Ptr Word8 -> GmpSupported (IO ())#if MIN_VERSION_integer_gmp(1,0,0)gmpExportInteger n (Ptr addr) = GmpSupported $ do    _ <- exportIntegerToAddr n addr 1#    return ()#elif MIN_VERSION_integer_gmp(0,5,1)gmpExportInteger n (Ptr addr) = GmpSupported $ IO $ \s ->    case exportIntegerToAddr n addr 1# s of        (# s2, _ #) -> (# s2, () #)#elsegmpExportInteger _ _ = GmpUnsupported#endif-- | Export an integer to a memory (little-endian)gmpExportIntegerLE :: Integer -> Ptr Word8 -> GmpSupported (IO ())#if MIN_VERSION_integer_gmp(1,0,0)gmpExportIntegerLE n (Ptr addr) = GmpSupported $ do    _ <- exportIntegerToAddr n addr 0#    return ()#elif MIN_VERSION_integer_gmp(0,5,1)gmpExportIntegerLE n (Ptr addr) = GmpSupported $ IO $ \s ->    case exportIntegerToAddr n addr 0# s of        (# s2, _ #) -> (# s2, () #)#elsegmpExportIntegerLE _ _ = GmpUnsupported#endif-- | Import an integer from a memory (big-endian)gmpImportInteger :: Int -> Ptr Word8 -> GmpSupported (IO Integer)#if MIN_VERSION_integer_gmp(1,0,0)gmpImportInteger (I# n) (Ptr addr) = GmpSupported $    importIntegerFromAddr addr (int2Word# n) 1##elif MIN_VERSION_integer_gmp(0,5,1)gmpImportInteger (I# n) (Ptr addr) = GmpSupported $ IO $ \s ->    importIntegerFromAddr addr (int2Word# n) 1# s#elsegmpImportInteger _ _ = GmpUnsupported#endif-- | Import an integer from a memory (little-endian)gmpImportIntegerLE :: Int -> Ptr Word8 -> GmpSupported (IO Integer)#if MIN_VERSION_integer_gmp(1,0,0)gmpImportIntegerLE (I# n) (Ptr addr) = GmpSupported $    importIntegerFromAddr addr (int2Word# n) 0##elif MIN_VERSION_integer_gmp(0,5,1)gmpImportIntegerLE (I# n) (Ptr addr) = GmpSupported $ IO $ \s ->    importIntegerFromAddr addr (int2Word# n) 0# s#elsegmpImportIntegerLE _ _ = GmpUnsupported#endif