summaryrefslogtreecommitdiff
path: root/src/Crypto/Tox.hs
diff options
context:
space:
mode:
authorjoe <joe@jerkface.net>2017-11-06 02:27:29 -0500
committerjoe <joe@jerkface.net>2017-11-06 02:27:29 -0500
commit1b2ae73c28ed275db051b8492e02384a42a4e36a (patch)
tree57866f63300e66cb0ad78c6bdefdeb429536d4e0 /src/Crypto/Tox.hs
parentfe2346fd33d3b91445a3f68fa7191cbb65ebe97d (diff)
More inlining on Ord/Hashable instances for Public/Secret keys.
Diffstat (limited to 'src/Crypto/Tox.hs')
-rw-r--r--src/Crypto/Tox.hs27
1 files changed, 16 insertions, 11 deletions
diff --git a/src/Crypto/Tox.hs b/src/Crypto/Tox.hs
index 88a7372f..665a38dd 100644
--- a/src/Crypto/Tox.hs
+++ b/src/Crypto/Tox.hs
@@ -89,14 +89,14 @@ import qualified Data.ByteString.Internal
89import Control.Concurrent.STM 89import Control.Concurrent.STM
90import Crypto.Error.Types (CryptoFailable (..), throwCryptoError) 90import Crypto.Error.Types (CryptoFailable (..), throwCryptoError)
91import Network.Socket (SockAddr) 91import Network.Socket (SockAddr)
92import GHC.Exts (Word(..)) 92import GHC.Exts (Word(..),inline)
93import GHC.Prim 93import GHC.Prim
94import Data.Word64Map (fitsInInt) 94import Data.Word64Map (fitsInInt)
95import Data.MinMaxPSQ (MinMaxPSQ') 95import Data.MinMaxPSQ (MinMaxPSQ')
96import qualified Data.MinMaxPSQ as MM 96import qualified Data.MinMaxPSQ as MM
97import Data.Time.Clock.POSIX 97import Data.Time.Clock.POSIX
98import Data.Hashable 98import Data.Hashable
99import System.IO.Unsafe (unsafePerformIO) 99import System.IO.Unsafe (unsafeDupablePerformIO)
100 100
101-- | A 16-byte mac and an arbitrary-length encrypted stream. 101-- | A 16-byte mac and an arbitrary-length encrypted stream.
102newtype Encrypted a = Encrypted ByteString 102newtype Encrypted a = Encrypted ByteString
@@ -259,21 +259,26 @@ computeSharedSecret sk recipient = k `seq` \nonce ->
259 k = hsalsa20 shared zeros24 259 k = hsalsa20 shared zeros24
260 260
261unsafeFirstWord64 :: ByteArrayAccess ba => ba -> Word64 261unsafeFirstWord64 :: ByteArrayAccess ba => ba -> Word64
262unsafeFirstWord64 ba = unsafePerformIO $ BA.withByteArray ba peek 262unsafeFirstWord64 ba = unsafeDupablePerformIO $ BA.withByteArray ba peek
263{-# INLINE unsafeFirstWord64 #-}
263 264
264instance Hashable PublicKey where 265instance Hashable PublicKey where
265 hashWithSalt salt pk = hashWithSalt salt (unsafeFirstWord64 pk) 266 hashWithSalt salt pk = hashWithSalt salt (unsafeFirstWord64 pk)
267 {-# INLINE hashWithSalt #-}
266 268
267instance Hashable SecretKey where 269instance Hashable SecretKey where
268 hashWithSalt salt sk = hashWithSalt salt (unsafeFirstWord64 sk) 270 hashWithSalt salt sk = hashWithSalt salt (unsafeFirstWord64 sk)
271 {-# INLINE hashWithSalt #-}
269 272
270instance Ord PublicKey where compare = unsafeCompare32Bytes 273instance Ord PublicKey where compare = unsafeCompare32Bytes
274 {-# INLINE compare #-}
271instance Ord SecretKey where compare = unsafeCompare32Bytes 275instance Ord SecretKey where compare = unsafeCompare32Bytes
276 {-# INLINE compare #-}
272 277
273unsafeCompare32Bytes :: (ByteArrayAccess ba, ByteArrayAccess bb) 278unsafeCompare32Bytes :: (ByteArrayAccess ba, ByteArrayAccess bb)
274 => ba -> bb -> Ordering 279 => ba -> bb -> Ordering
275unsafeCompare32Bytes ba bb = 280unsafeCompare32Bytes ba bb =
276 unsafePerformIO $ BA.withByteArray ba 281 unsafeDupablePerformIO $ BA.withByteArray ba
277 $ \pa -> BA.withByteArray bb 282 $ \pa -> BA.withByteArray bb
278 $ \pb -> unsafeCompare32Bytes' 3 pa pb 283 $ \pb -> unsafeCompare32Bytes' 3 pa pb
279 284
@@ -281,13 +286,13 @@ unsafeCompare32Bytes' :: Int -> Ptr Word64 -> Ptr Word64 -> IO Ordering
281unsafeCompare32Bytes' !n !pa !pb = do 286unsafeCompare32Bytes' !n !pa !pb = do
282 a <- peek pa 287 a <- peek pa
283 b <- peek pb 288 b <- peek pb
284 case compare a b of 289 if n == 0
285 EQ -> if n == 0 290 then return $! inline compare a b
286 then return EQ 291 else case inline compare a b of
287 else unsafeCompare32Bytes' (n - 1) 292 EQ -> unsafeCompare32Bytes' (n - 1)
288 (pa `plusPtr` 8) 293 (pa `plusPtr` 8)
289 (pb `plusPtr` 8) 294 (pb `plusPtr` 8)
290 result -> return result 295 neq -> return neq
291 296
292 297
293 298