<commit_before>{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE RecordWildCards           #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE ForeignFunctionInterface   #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE FlexibleContexts           #-}
{- CFILES raaz/hash/sha1/portable.c     -}

module Raaz.Hash.Blake2.Internal
       ( -- * The blake2 types
         BLAKE2, BLAKE2b, BLAKE2s
       , Blake2bMem, Blake2sMem
       , blake2Pad
       ) where

import           Control.Applicative
import           Control.Monad.IO.Class
import           Data.Bits           ( xor          )
import           Data.String
import           Data.Word
import           Foreign.Storable    ( Storable(..) )
import           Prelude      hiding ( zipWith      )

import           Raaz.Core
import           Raaz.Core.Transfer
import           Raaz.Hash.Internal

----------------------------- The blake2 type ---------------------------------

-- | The BLAKE2 type.
newtype BLAKE2 w = BLAKE2 (Tuple 8 w)
               deriving (Eq, Equality, Storable, EndianStore)

-- | Word type for Blake2b
type Word2b = LE Word64

-- | Word type for Blake2s
type Word2s = LE Word32

-- | The BLAKE2b hash type.
type BLAKE2b = BLAKE2 Word2b

-- | The BLAKE2s hash type.
type BLAKE2s = BLAKE2 Word2s

instance Encodable BLAKE2b
instance Encodable BLAKE2s


instance IsString BLAKE2b where
  fromString = fromBase16

instance IsString BLAKE2s where
  fromString = fromBase16

instance Show BLAKE2b where
  show =  showBase16

instance Show BLAKE2s where
  show =  showBase16

instance Primitive BLAKE2b where
  blockSize _ = BYTES 128
  type Implementation BLAKE2b = SomeHashI BLAKE2b

instance Primitive BLAKE2s where
  blockSize _ = BYTES 64
  type Implementation BLAKE2s = SomeHashI BLAKE2s


-- | The initial value to start the blake2b hashing. This is equal to
-- the iv `xor` the parameter block.
hash2b0 :: BLAKE2b
hash2b0 = BLAKE2 $ unsafeFromList [ 0x6a09e667f3bcc908 `xor` 0x01010040
                                  , 0xbb67ae8584caa73b
                                  , 0x3c6ef372fe94f82b
                                  , 0xa54ff53a5f1d36f1
                                  , 0x510e527fade682d1
                                  , 0x9b05688c2b3e6c1f
                                  , 0x1f83d9abfb41bd6b
                                  , 0x5be0cd19137e2179
                                  ]

-- | The initial value to start the blake2b hashing. This is equal to
-- the iv `xor` the parameter block.
hash2s0 :: BLAKE2s
hash2s0 = BLAKE2 $ unsafeFromList [ 0x6a09e667 `xor` 0x01010020
                                  , 0xbb67ae85
                                  , 0x3c6ef372
                                  , 0xa54ff53a
                                  , 0x510e527f
                                  , 0x9b05688c
                                  , 0x1f83d9ab
                                  , 0x5be0cd19
                                  ]

---------------------------------- Memory element for BLAKE2b -----------------------

data Blake2bMem = Blake2bMem { blake2bCell :: MemoryCell BLAKE2b
                             , uLengthCell :: MemoryCell (BYTES Word64)<commit_msg>hash instance of blake2b and blake2s.<commit_after>{-# LANGUAGE GeneralizedNewtypeDeriving #-}
{-# LANGUAGE TypeFamilies               #-}
{-# LANGUAGE RecordWildCards           #-}
{-# LANGUAGE FlexibleInstances          #-}
{-# LANGUAGE ForeignFunctionInterface   #-}
{-# LANGUAGE MultiParamTypeClasses      #-}
{-# LANGUAGE DataKinds                  #-}
{-# LANGUAGE FlexibleContexts           #-}
{- CFILES raaz/hash/sha1/portable.c     -}

module Raaz.Hash.Blake2.Internal
       ( -- * The blake2 types
         BLAKE2, BLAKE2b, BLAKE2s
       , Blake2bMem, Blake2sMem
       , blake2Pad
       ) where

import           Control.Applicative
import           Control.Monad.IO.Class
import           Data.Bits           ( xor          )
import           Data.String
import           Data.Word
import           Foreign.Storable    ( Storable(..) )
import           Prelude      hiding ( zipWith      )

import           Raaz.Core
import           Raaz.Core.Transfer
import           Raaz.Hash.Internal

----------------------------- The blake2 type ---------------------------------

-- | The BLAKE2 type.
newtype BLAKE2 w = BLAKE2 (Tuple 8 w)
               deriving (Eq, Equality, Storable, EndianStore)

-- | Word type for Blake2b
type Word2b = LE Word64

-- | Word type for Blake2s
type Word2s = LE Word32

-- | The BLAKE2b hash type.
type BLAKE2b = BLAKE2 Word2b

-- | The BLAKE2s hash type.
type BLAKE2s = BLAKE2 Word2s

instance Encodable BLAKE2b
instance Encodable BLAKE2s


instance IsString BLAKE2b where
  fromString = fromBase16

instance IsString BLAKE2s where
  fromString = fromBase16

instance Show BLAKE2b where
  show =  showBase16

instance Show BLAKE2s where
  show =  showBase16

instance Primitive BLAKE2b where
  blockSize _ = BYTES 128
  type Implementation BLAKE2b = SomeHashI BLAKE2b

instance Hash BLAKE2b where
  additionalPadBlocks _ = toEnum 1

instance Primitive BLAKE2s where
  blockSize _ = BYTES 64
  type Implementation BLAKE2s = SomeHashI BLAKE2s

instance Hash BLAKE2s where
  additionalPadBlocks _ = toEnum 1

-- | The initial value to start the blake2b hashing. This is equal to
-- the iv `xor` the parameter block.
hash2b0 :: BLAKE2b
hash2b0 = BLAKE2 $ unsafeFromList [ 0x6a09e667f3bcc908 `xor` 0x01010040
                                  , 0xbb67ae8584caa73b
                                  , 0x3c6ef372fe94f82b
                                  , 0xa54ff53a5f1d36f1
                                  , 0x510e527fade682d1
                                  , 0x9b05688c2b3e6c1f
                                  , 0x1f83d9abfb41bd6b
                                  , 0x5be0cd19137e2179
                                  ]

-- | The initial value to start the blake2b hashing. This is equal to
-- the iv `xor` the parameter block.
hash2s0 :: BLAKE2s
hash2s0 = BLAKE2 $ unsafeFromList [ 0x6a09e667 `xor` 0x01010020
                                  , 0xbb67ae85
                                  , 0x3c6ef372
                                  , 0xa54ff53a
                                  , 0x510e527f
                                  , 0x9b05688c
                                  , 0x1f83d9ab
                                  , 0x5be0cd19
                                  ]

---------------------------------- Memory element for BLAKE2b -----------------------

data Blake2bMem = Blake2bMem { blake2bCell :: MemoryCell BLAKE2b
                             , uLengthCell :: MemoryCell (BYTES Word64)
{'fraction_non_alphanumeric': 0.09198637238927566, 'fraction_numerical': 0.06695304399348245, 'mean_word_length': 1.4114285714285715, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 28, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6859243', 'n_tokens_mistral': 2349, 'n_tokens_neox': 2102, 'n_words': 496}