From 427541064dcf567f338d4ee9f5626b2105232e5c Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Mon, 11 May 2015 07:09:30 +0100 Subject: [PATCH] [number] add further gmp optional compat --- Crypto/Number/Compat.hs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Crypto/Number/Compat.hs b/Crypto/Number/Compat.hs index 0ff0305..acf28ee 100644 --- a/Crypto/Number/Compat.hs +++ b/Crypto/Number/Compat.hs @@ -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