[number] add further gmp optional compat

This commit is contained in:
Vincent Hanquez 2015-05-11 07:09:30 +01:00
parent ed8fbe8f69
commit 427541064d

View File

@ -18,6 +18,9 @@ module Crypto.Number.Compat
, gmpInverse
, gmpNextPrime
, gmpTestPrimeMillerRabin
, gmpSizeInBytes
, gmpExportInteger
, gmpImportInteger
) where
#ifndef MIN_VERSION_integer_gmp
@ -29,6 +32,8 @@ import GHC.Integer.GMP.Internals
import GHC.Base
import GHC.Integer.Logarithms (integerLog2#)
#endif
import Data.Word
import GHC.Ptr (Ptr(..))
data GmpSupported a = GmpSupported a
| GmpUnsupported
@ -94,3 +99,34 @@ gmpTestPrimeMillerRabin (I# tries) !n = GmpSupported $
#else
gmpTestPrimeMillerRabin _ _ = GmpUnsupported
#endif
gmpSizeInBytes :: Integer -> GmpSupported Int
#if MIN_VERSION_integer_gmp(0,5,1)
gmpSizeInBytes n = GmpSupported (I# (word2Int# (sizeInBaseInteger n 256#)))
#else
gmpSizeInBytes _ = GmpUnsupported
#endif
gmpExportInteger :: Integer -> Ptr Word8 -> GmpSupported (IO ())
#if __GLASGOW_HASKELL__ >= 710
gmpExportInteger n (Ptr addr) = GmpSupported $
_ <- 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, () #)
#else
gmpExportInteger _ _ = GmpUnsupported
#endif
gmpImportInteger :: Int -> Ptr Word8 -> GmpSupported (IO Integer)
#if __GLASGOW_HASKELL__ >= 710
gmpImportInteger n (Ptr addr) = GmpSupported $
importIntegerFromAddr addr (int2Word# n) 1#
#elif MIN_VERSION_integer_gmp(0,5,1)
gmpImportInteger n (Ptr addr) = GmpSupported $ IO $ \s ->
importIntegerFromAddr addr (int2Word# n) 1# s
#else
gmpImportInteger _ _ = GmpUnsupported
#endif