{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE ScopedTypeVariables #-}

-- |
-- Module: Data.Cuckoo.Internal.HashFunctions
-- Copyright: Copyright © 2021 Lars Kuhtz <lakuhtz@gmail.com>
-- License: BSD3
-- Maintainer: Lars Kuhtz <lakuhtz@gmail.com>
-- Stability: experimental
--
module Data.Cuckoo.Internal.HashFunctions
(
-- * Salted Hash Functions
  saltedFnv1aPtr
, saltedFnv1aStorable
, saltedFnv1aByteString

-- ** Salted Sip Hash
, saltedSipHashPtr
, saltedSipHashStorable
, saltedSipHashByteString

-- * Internal Use Only
, sipHashInternal
) where

import qualified Data.ByteString as B
import Data.Hash.FNV1
import Data.Hash.SipHash

import Foreign

import Data.Cuckoo.Internal

-- -------------------------------------------------------------------------- --
-- FNV1a

-- | Computes a 64 bit Fnv1a hash for a value that is an instance of
-- 'BA.ByteArrayAccess'.
--
-- The first argument is use as a salt.
--
saltedFnv1aPtr
    :: Int
        -- ^ Salt
    -> Ptr Word8
        -- ^ Bytes that are hashed
    -> Int
        -- ^ Number of bytes
    -> Word64
saltedFnv1aPtr :: Int -> Ptr Word8 -> Int -> Word64
saltedFnv1aPtr Int
s Ptr Word8
p Int
ps = (Ptr Word8 -> Int -> Word64 -> IO Word64)
-> Ptr Word8 -> Int -> Word64 -> Word64
forall b.
(Ptr Word8 -> Int -> b -> IO b) -> Ptr Word8 -> Int -> b -> b
hashPtr_ Ptr Word8 -> Int -> Word64 -> IO Word64
fnv1a_64_ Ptr Word8
p Int
ps
    (Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$! (Ptr Word8 -> Int -> IO Word64) -> Int -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable Ptr Word8 -> Int -> IO Word64
fnv1a_64 Int
s
{-# INLINE saltedFnv1aPtr #-}

-- | Computes a 64 bit Fnv1a hash for a value that has an 'Storable' instance.
--
-- The first argument is use as a salt.
--
saltedFnv1aStorable
    :: Storable a
    => Int
        -- ^ Salt
    -> a
        -- ^ Value that is hashed
    -> Word64
saltedFnv1aStorable :: Int -> a -> Word64
saltedFnv1aStorable Int
s a
x = (Ptr Word8 -> Int -> Word64 -> IO Word64) -> a -> Word64 -> Word64
forall a b.
Storable a =>
(Ptr Word8 -> Int -> b -> IO b) -> a -> b -> b
hashStorable_ Ptr Word8 -> Int -> Word64 -> IO Word64
fnv1a_64_ a
x
    (Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$! (Ptr Word8 -> Int -> IO Word64) -> Int -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable Ptr Word8 -> Int -> IO Word64
fnv1a_64 Int
s
{-# INLINE saltedFnv1aStorable #-}

saltedFnv1aByteString
    :: Int
        -- ^ Salt
    -> B.ByteString
        -- ^ Bytes that are hashed
    -> Word64
saltedFnv1aByteString :: Int -> ByteString -> Word64
saltedFnv1aByteString Int
s ByteString
b = (Ptr Word8 -> Int -> Word64 -> IO Word64)
-> ByteString -> Word64 -> Word64
forall b. (Ptr Word8 -> Int -> b -> IO b) -> ByteString -> b -> b
hashByteString_ Ptr Word8 -> Int -> Word64 -> IO Word64
fnv1a_64_ ByteString
b
    (Word64 -> Word64) -> Word64 -> Word64
forall a b. (a -> b) -> a -> b
$! (Ptr Word8 -> Int -> IO Word64) -> Int -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable Ptr Word8 -> Int -> IO Word64
fnv1a_64 Int
s
{-# INLINE saltedFnv1aByteString #-}

-- -------------------------------------------------------------------------- --
-- Sip Hash

-- | Computes a Sip hash for a value that is represented as byte pointer.
--
-- The first argument is a salt value that is used to derive the key for the
-- hash computation.
--
saltedSipHashPtr
    :: Int
        -- ^ Salt
    -> Ptr Word8
        -- ^ Bytes that is hashed
    -> Int
        -- ^ Number of bytes
    -> Word64
saltedSipHashPtr :: Int -> Ptr Word8 -> Int -> Word64
saltedSipHashPtr Int
s Ptr Word8
ptr Int
l = (Ptr Word8 -> Int -> IO Word64) -> Ptr Word8 -> Int -> Word64
forall b. (Ptr Word8 -> Int -> IO b) -> Ptr Word8 -> Int -> b
hashPtr (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s) Word64
1043639) Ptr Word8
ptr Int
l
{-# INLINE saltedSipHashPtr #-}

-- | Computes a Sip hash for a value that has an 'Storable' instance.
--
-- The first argument is a salt value that is used to derive the key for the
-- hash computation.
--
saltedSipHashStorable
    :: Storable a
    => Int
        -- ^ Salt
    -> a
        -- ^ Value that is hashed
    -> Word64
saltedSipHashStorable :: Int -> a -> Word64
saltedSipHashStorable Int
s = (Ptr Word8 -> Int -> IO Word64) -> a -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s) Word64
914279)
{-# INLINE saltedSipHashStorable #-}

-- | Computes a Sip hash for a value that has an 'Storable' instance.
--
-- The first argument is a salt value that is used to derive the key for the
-- hash computation.
--
saltedSipHashByteString
    :: Int
        -- ^ Salt
    -> B.ByteString
        -- ^ Value that is hashed
    -> Word64
saltedSipHashByteString :: Int -> ByteString -> Word64
saltedSipHashByteString Int
s = (Ptr Word8 -> Int -> IO Word64) -> ByteString -> Word64
forall b. (Ptr Word8 -> Int -> IO b) -> ByteString -> b
hashByteString (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s) Word64
914279)
{-# INLINE saltedSipHashByteString #-}

-- -------------------------------------------------------------------------- --
-- Interal Use Only (Do Not Use)

-- | An version of a Sip hash that is used only internally. In order to avoid
-- dependencies between different hash computations, it shouldn't be used in the
-- implementation of instances of 'Data.Cuckoo.CuckooFilterHash'.
--
sipHashInternal :: Storable a => Int -> a -> Word64
sipHashInternal :: Int -> a -> Word64
sipHashInternal Int
s = (Ptr Word8 -> Int -> IO Word64) -> a -> Word64
forall a b. Storable a => (Ptr Word8 -> Int -> IO b) -> a -> b
hashStorable (Word64 -> Word64 -> Ptr Word8 -> Int -> IO Word64
sipHash24 Word64
994559 (Int -> Word64
forall a b. (Integral a, Num b) => a -> b
int Int
s Word64 -> Word64 -> Word64
forall a. Num a => a -> a -> a
* Word64
713243))
{-# INLINE sipHashInternal #-}