From ed8fbe8f694c7f33ceb4bfc7ff22f6fa5006d5a1 Mon Sep 17 00:00:00 2001 From: Vincent Hanquez Date: Sun, 10 May 2015 15:27:52 +0100 Subject: [PATCH] [Number] add module to cleanup CPP around --- Crypto/Number/Compat.hs | 96 +++++++++++++++++++++++++++++++++++++++++ cryptonite.cabal | 1 + 2 files changed, 97 insertions(+) create mode 100644 Crypto/Number/Compat.hs diff --git a/Crypto/Number/Compat.hs b/Crypto/Number/Compat.hs new file mode 100644 index 0000000..0ff0305 --- /dev/null +++ b/Crypto/Number/Compat.hs @@ -0,0 +1,96 @@ +-- | +-- Module : Crypto.Number.Compat +-- License : BSD-style +-- Maintainer : Vincent Hanquez +-- Stability : experimental +-- Portability : Good +-- +{-# LANGUAGE CPP #-} +{-# LANGUAGE MagicHash #-} +{-# LANGUAGE BangPatterns #-} +{-# LANGUAGE UnboxedTuples #-} +module Crypto.Number.Compat + ( GmpSupported(..) + , gmpGcde + , gmpLog2 + , gmpPowModSecInteger + , gmpPowModInteger + , gmpInverse + , gmpNextPrime + , gmpTestPrimeMillerRabin + ) 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.Internals +import GHC.Base +import GHC.Integer.Logarithms (integerLog2#) +#endif + +data GmpSupported a = GmpSupported a + | GmpUnsupported + deriving (Show,Eq) + +gmpGcde :: 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 +#else +gmpGcde _ _ = GmpUnsupported +#endif + +gmpLog2 :: Integer -> GmpSupported Int +#ifdef VERSION_integer_gmp +gmpLog2 0 = GmpSupported 0 +gmpLog2 x = GmpSupported (I# (integerLog2# x)) +#else +gmpLog2 _ = GmpUnsupported +#endif + +gmpPowModSecInteger :: Integer -> Integer -> Integer -> GmpSupported Integer +#if MIN_VERSION_integer_gmp(1,0,0) +gmpPowModSecInteger b e m = GmpUnsupported +#elif MIN_VERSION_integer_gmp(0,5,1) +gmpPowModSecInteger b e m = GmpSupported (powModSecInteger b e m) +#else +gmpPowModSecInteger _ _ _ = GmpUnsupported +#endif + +gmpPowModInteger :: Integer -> Integer -> Integer -> GmpSupported Integer +#if MIN_VERSION_integer_gmp(0,5,1) +gmpPowModInteger b e m = GmpSupported (powModInteger b e m) +#else +gmpPowModInteger _ _ _ = GmpUnsupported +#endif + +gmpInverse :: 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 +#else +gmpInverse _ _ = GmpUnsupported +#endif + +gmpNextPrime :: Integer -> GmpSupported Integer +#if MIN_VERSION_integer_gmp(0,5,1) +gmpNextPrime n = GmpSupported (nextPrimeInteger n) +#else +gmpNextPrime _ = GmpUnsupported +#endif + +gmpTestPrimeMillerRabin :: Int -> Integer -> GmpSupported Bool +#if MIN_VERSION_integer_gmp(0,5,1) +gmpTestPrimeMillerRabin (I# tries) !n = GmpSupported $ + case testPrimeInteger n tries of + 0# -> False + _ -> True +#else +gmpTestPrimeMillerRabin _ _ = GmpUnsupported +#endif diff --git a/cryptonite.cabal b/cryptonite.cabal index 86191b6..2ff7952 100644 --- a/cryptonite.cabal +++ b/cryptonite.cabal @@ -100,6 +100,7 @@ Library Crypto.Cipher.Types.Stream Crypto.Cipher.Types.Utils Crypto.Error.Types + Crypto.Number.Compat Crypto.Hash.Utils Crypto.Hash.Utils.Cpu Crypto.Hash.Types