Skip to content

Commit

Permalink
Refactor #8 and #9
Browse files Browse the repository at this point in the history
This avoids the use of CPP where possible; the last remaining CPP
use-site could in principle be avoided too by using cabal-level
conditional compilation but for now we've just reduced the source-code
exposed to the CPP phase to a bare minimum.

While at it, also fixup the package description and upgrade to
cabal-version:2.0 as there's little reason not to in 2021.
  • Loading branch information
hvr committed Jan 20, 2021
1 parent 7c14be1 commit c498992
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 24 deletions.
24 changes: 13 additions & 11 deletions cryptohash-sha256.cabal
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
cabal-version: 1.12
cabal-version: 2.0
name: cryptohash-sha256
version: 0.11.102.0
version: 0.11.102.1

synopsis: Fast, pure and practical SHA-256 implementation
description: {
Expand Down Expand Up @@ -75,17 +75,19 @@ library
default-language: Haskell2010
other-extensions: BangPatterns
CApiFFI
CPP
Trustworthy
Unsafe

build-depends: base >= 4.5 && < 4.15
, bytestring >= 0.9.2 && < 0.12
build-depends: base >= 4.5 && < 4.15
, bytestring ^>= 0.9.2 || ^>= 0.10.0 || ^>= 0.11.0

ghc-options: -Wall

hs-source-dirs: src
exposed-modules: Crypto.Hash.SHA256
other-modules: Crypto.Hash.SHA256.FFI
Compat
include-dirs: cbits

executable sha256sum
Expand All @@ -99,7 +101,7 @@ executable sha256sum
, base
, bytestring

, base16-bytestring >= 0.1.1 && < 1.1
, base16-bytestring ^>= 0.1.1 || ^>= 1.0.0
else
buildable: False

Expand All @@ -114,11 +116,11 @@ test-suite test-sha256
, base
, bytestring

, base16-bytestring >= 0.1.1 && < 1.1
, SHA >= 1.6.4 && < 1.7
, tasty == 1.1.*
, tasty-quickcheck == 0.10.*
, tasty-hunit == 0.10.*
, base16-bytestring ^>= 0.1.1 || ^>= 1.0.0
, SHA ^>= 1.6.4
, tasty ^>= 1.1
, tasty-quickcheck ^>= 0.10
, tasty-hunit ^>= 0.10

benchmark bench-sha256
default-language: Haskell2010
Expand All @@ -129,4 +131,4 @@ benchmark bench-sha256
build-depends: cryptohash-sha256
, base
, bytestring
, criterion == 1.5.*
, criterion ^>= 1.5
14 changes: 8 additions & 6 deletions src-tests/test-sha256.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE FlexibleInstances #-}

module Main (main) where

Expand Down Expand Up @@ -126,12 +126,14 @@ splitB l b
where
(b1, b2) = B.splitAt l b

-- Minor hack to paper over backward incompat changes introduced in base16-bytestring-1.0
class B16DecRes a where b16DecRes :: a -> ByteString
instance B16DecRes (Either String ByteString) where b16DecRes = either error id
instance B16DecRes (ByteString, ByteString) where b16DecRes = fst

b16decode :: ByteString -> ByteString
#if MIN_VERSION_base16_bytestring(1,0,0)
b16decode = either error id . B16.decode
#else
b16decode = fst . B16.decode
#endif
b16decode = b16DecRes . B16.decode


rfc4231Vectors :: [(ByteString,ByteString,ByteString)]
rfc4231Vectors = -- (secrect,msg,mac)
Expand Down
24 changes: 24 additions & 0 deletions src/Compat.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}

-- |
-- Module : Compat
-- License : BSD-3
-- Maintainer : Herbert Valerio Riedel <[email protected]>
-- Stability : stable
--
-- Compat layer to reduce code exposure to CPP to a bare minimum
--
module Compat (constructBS) where

import Foreign.ForeignPtr (ForeignPtr)
import Data.Word (Word8)
import Data.ByteString.Internal (ByteString (..))

-- | Directly construct a 'ByteString', unsafely
constructBS :: ForeignPtr Word8 -> Int -> ByteString
#if MIN_VERSION_bytestring(0,11,0)
constructBS = BS
#else
constructBS = \fp -> PS fp 0
#endif
10 changes: 3 additions & 7 deletions src/Crypto/Hash/SHA256.hs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
{-# LANGUAGE BangPatterns #-}
{-# LANGUAGE CPP #-}
{-# LANGUAGE Trustworthy #-}

-- |
Expand Down Expand Up @@ -93,7 +92,7 @@ module Crypto.Hash.SHA256
import Data.Bits (xor)
import Data.ByteString (ByteString)
import qualified Data.ByteString as B
import Data.ByteString.Internal (ByteString (..), create,
import Data.ByteString.Internal (create,
createAndTrim, mallocByteString,
memcpy, toForeignPtr)
import qualified Data.ByteString.Lazy as L
Expand All @@ -106,6 +105,7 @@ import Foreign.Ptr
import Prelude hiding (init)
import System.IO.Unsafe (unsafeDupablePerformIO)

import Compat (constructBS)
import Crypto.Hash.SHA256.FFI

-- | perform IO for hashes that do allocation and ffi.
Expand Down Expand Up @@ -137,11 +137,7 @@ create' :: Int -> (Ptr Word8 -> IO a) -> IO (ByteString,a)
create' l f = do
fp <- mallocByteString l
x <- withForeignPtr fp $ \p -> f p
#if MIN_VERSION_bytestring(0,11,0)
let bs = BS fp l
#else
let bs = PS fp 0 l
#endif
let bs = constructBS fp l
return $! x `seq` bs `seq` (bs,x)

copyCtx :: Ptr Ctx -> Ptr Ctx -> IO ()
Expand Down

0 comments on commit c498992

Please sign in to comment.