Skip to content
Browse files

Added a configuration flag 'fast-bignum', fixes #16.

  • Loading branch information...
1 parent 1acab7b commit 09d32af84a0e3f8c9eafc1a152e3f82e093a248c @phonohawk phonohawk committed Jul 21, 2012
Showing with 37 additions and 43 deletions.
  1. +12 −4 HsOpenSSL.cabal
  2. +1 −0 NEWS
  3. +24 −39 OpenSSL/BN.hsc
View
16 HsOpenSSL.cabal
@@ -48,6 +48,12 @@ Source-Repository head
Type: git
Location: git://github.com/phonohawk/HsOpenSSL.git
+Flag fast-bignum
+ Description:
+ Enable fast moving of bignums between OpenSSL and GMP (GHC Only).
+ Default:
+ True
+
Library
Build-Depends:
base >= 4 && < 5,
@@ -57,10 +63,12 @@ Library
old-locale,
time >= 1.1.1
- if impl(ghc >= 6.11)
- Build-Depends: integer-gmp
- else
- Build-Depends: integer
+ if flag(fast-bignum)
+ CPP-Options: -DFAST_BIGNUM
+ if impl(ghc >= 6.11)
+ Build-Depends: integer-gmp
+ else
+ Build-Depends: integer
if os(mingw32)
Extra-Libraries: eay32 ssl32
View
1 NEWS
@@ -3,6 +3,7 @@
Changes from 0.10.3.1 to 0.10.3.2
---------------------------------
* Merged #15 "Fixed build with base-4.6" by Mikhail Vorozhtsov.
+* Added a configuration flag 'fast-bignum', fixes #16.
Changes from 0.10.3 to 0.10.3.1
View
63 OpenSSL/BN.hsc
@@ -22,10 +22,8 @@ module OpenSSL.BN
-- * Conversion from\/to Integer
, peekBN
-#ifdef __GLASGOW_HASKELL__
, integerToBN
, bnToInteger
-#endif
, integerToMPI
, mpiToInteger
@@ -50,7 +48,7 @@ import Foreign.Storable
import OpenSSL.Utils
import System.IO.Unsafe
-#ifdef __GLASGOW_HASKELL__
+#ifdef FAST_BIGNUM
import Foreign.C.Types
import GHC.Base
# if MIN_VERSION_integer_gmp(0,2,0)
@@ -92,7 +90,7 @@ wrapBN :: Ptr BIGNUM -> BigNum
wrapBN = BigNum
-#ifndef __GLASGOW_HASKELL__
+#ifndef FAST_BIGNUM
{- slow, safe functions ----------------------------------------------------- -}
@@ -105,36 +103,23 @@ foreign import ccall unsafe "BN_dec2bn"
foreign import ccall unsafe "HsOpenSSL_OPENSSL_free"
_openssl_free :: Ptr a -> IO ()
--- |@'withBN' n f@ converts n to a 'BigNum' and computes @f@. Then it
--- frees the 'BigNum'.
-withBN :: Integer -> (BigNum -> IO a) -> IO a
-withBN dec m
- = withCString (show dec) $ \ strPtr ->
- alloca $ \ bnPtr ->
- do poke bnPtr nullPtr
- _dec2bn bnPtr strPtr
- >>= failIf (== 0)
- bracket (peek bnPtr) _free m
-
--- |@'peekBN' bn@ converts a 'BigNum' to an 'Prelude.Integer'.
-peekBN :: BigNum -> IO Integer
-peekBN bn
- = do strPtr <- _bn2dec bn
- when (strPtr == nullPtr) $ fail "BN_bn2dec failed"
- str <- peekCString strPtr
- _openssl_free strPtr
-
- return $ read str
-
-
--- | Return a new, alloced bignum
-newBN :: Integer -> IO BigNum
-newBN i = do
+-- |Convert a BIGNUM to an 'Integer'.
+bnToInteger :: BigNum -> IO Integer
+bnToInteger bn
+ = bracket (do strPtr <- _bn2dec (unwrapBN bn)
+ when (strPtr == nullPtr) $ fail "BN_bn2dec failed"
+ return strPtr)
+ _openssl_free
+ ((read `fmap`) . peekCString)
+
+-- |Return a new, alloced BIGNUM.
+integerToBN :: Integer -> IO BigNum
+integerToBN i = do
withCString (show i) (\str -> do
alloca (\bnptr -> do
poke bnptr nullPtr
- _dec2bn bnptr str >>= failIf (== 0)
- peek bnptr))
+ _ <- _dec2bn bnptr str >>= failIf (== 0)
+ wrapBN `fmap` peek bnptr))
#else
@@ -240,6 +225,8 @@ integerToBN v@(J## nlimbs_ bytearray)
(#poke BIGNUM, neg) (unwrapBN bnptr) (1 :: CInt)
return bnptr
+#endif
+
-- TODO: we could make a function which doesn't even allocate BN data if we
-- wanted to be very fast and dangerout. The BIGNUM could point right into the
-- Integer's data. However, I'm not sure about the semantics of the GC; which
@@ -250,6 +237,12 @@ integerToBN v@(J## nlimbs_ bytearray)
withBN :: Integer -> (BigNum -> IO a) -> IO a
withBN dec m = bracket (integerToBN dec) (_free . unwrapBN) m
+foreign import ccall unsafe "BN_bn2mpi"
+ _bn2mpi :: Ptr BIGNUM -> Ptr CChar -> IO CInt
+
+foreign import ccall unsafe "BN_mpi2bn"
+ _mpi2bn :: Ptr CChar -> CInt -> Ptr BIGNUM -> IO (Ptr BIGNUM)
+
-- |This is an alias to 'bnToInteger'.
peekBN :: BigNum -> IO Integer
peekBN = bnToInteger
@@ -258,14 +251,6 @@ peekBN = bnToInteger
newBN :: Integer -> IO BigNum
newBN = integerToBN
-foreign import ccall unsafe "BN_bn2mpi"
- _bn2mpi :: Ptr BIGNUM -> Ptr CChar -> IO CInt
-
-foreign import ccall unsafe "BN_mpi2bn"
- _mpi2bn :: Ptr CChar -> CInt -> Ptr BIGNUM -> IO (Ptr BIGNUM)
-
-#endif
-
-- | Convert a BigNum to an MPI: a serialisation of large ints which has a
-- 4-byte, big endian length followed by the bytes of the number in
-- most-significant-first order.

0 comments on commit 09d32af

Please sign in to comment.
Something went wrong with that request. Please try again.