zkfold-base-0.1.0.0: ZkFold Symbolic compiler and zero-knowledge proof protocols
Safe HaskellSafe-Inferred
LanguageHaskell2010

ZkFold.Symbolic.Algorithms.Hash.SHA2

Synopsis

Documentation

class AlgorithmSetup (algorithm :: Symbol) a where Source #

SHA2 is a family of hashing functions with almost identical implementations but different constants and parameters. This class links these varying parts with the appropriate algorithm.

Associated Types

type WordSize algorithm :: Natural Source #

The length of words the algorithm operates internally, in bits.

type ChunkSize algorithm :: Natural Source #

Hashing algorithms from SHA2 family require splitting the input message into blocks. This type describes the size of these blocks, in bits.

type ResultSize algorithm :: Natural Source #

The length of the resulting hash, in bits.

Methods

initialHashes :: Vector (ByteString (WordSize algorithm) a) Source #

Initial hash values which will be mixed with the message bits.

roundConstants :: Vector (ByteString (WordSize algorithm) a) Source #

Constants used in the internal loop, one per each round.

truncateResult :: ByteString (8 * WordSize algorithm) a -> ByteString (ResultSize algorithm) a Source #

A function to postprocess the hash. For example, SHA224 requires dropping the last 32 bits of a SHA256 hash.

sigmaShifts :: (Natural, Natural, Natural, Natural, Natural, Natural) Source #

Round rotation values for Sigma in the internal loop.

sumShifts :: (Natural, Natural, Natural, Natural, Natural, Natural) Source #

Round rotation values for Sum in the internal loop.

Instances

Instances details
(FromConstant Natural a, Truncate (ByteString 256 a) (ByteString 224 a)) => AlgorithmSetup "SHA224" a Source # 
Instance details

Defined in ZkFold.Symbolic.Algorithms.Hash.SHA2

Associated Types

type WordSize "SHA224" :: Natural Source #

type ChunkSize "SHA224" :: Natural Source #

type ResultSize "SHA224" :: Natural Source #

FromConstant Natural a => AlgorithmSetup "SHA256" a Source # 
Instance details

Defined in ZkFold.Symbolic.Algorithms.Hash.SHA2

Associated Types

type WordSize "SHA256" :: Natural Source #

type ChunkSize "SHA256" :: Natural Source #

type ResultSize "SHA256" :: Natural Source #

(FromConstant Natural a, Truncate (ByteString 512 a) (ByteString 384 a)) => AlgorithmSetup "SHA384" a Source # 
Instance details

Defined in ZkFold.Symbolic.Algorithms.Hash.SHA2

Associated Types

type WordSize "SHA384" :: Natural Source #

type ChunkSize "SHA384" :: Natural Source #

type ResultSize "SHA384" :: Natural Source #

FromConstant Natural a => AlgorithmSetup "SHA512" a Source # 
Instance details

Defined in ZkFold.Symbolic.Algorithms.Hash.SHA2

Associated Types

type WordSize "SHA512" :: Natural Source #

type ChunkSize "SHA512" :: Natural Source #

type ResultSize "SHA512" :: Natural Source #

(FromConstant Natural a, Truncate (ByteString 512 a) (ByteString 224 a)) => AlgorithmSetup "SHA512/224" a Source # 
Instance details

Defined in ZkFold.Symbolic.Algorithms.Hash.SHA2

Associated Types

type WordSize "SHA512/224" :: Natural Source #

type ChunkSize "SHA512/224" :: Natural Source #

type ResultSize "SHA512/224" :: Natural Source #

(FromConstant Natural a, Truncate (ByteString 512 a) (ByteString 256 a)) => AlgorithmSetup "SHA512/256" a Source # 
Instance details

Defined in ZkFold.Symbolic.Algorithms.Hash.SHA2

Associated Types

type WordSize "SHA512/256" :: Natural Source #

type ChunkSize "SHA512/256" :: Natural Source #

type ResultSize "SHA512/256" :: Natural Source #

type SHA2 algorithm element k = (AlgorithmSetup algorithm element, KnownNat k, Finite element, NFData element, FromConstant Natural element, KnownNat (ChunkSize algorithm), KnownNat (WordSize algorithm), KnownNat (PaddedLength k (ChunkSize algorithm) (2 * WordSize algorithm)), Iso (UInt (WordSize algorithm) element) (ByteString (WordSize algorithm) element), Iso (ByteString (WordSize algorithm) element) (UInt (WordSize algorithm) element), AdditiveSemigroup (UInt (WordSize algorithm) element), BoolType (ByteString (WordSize algorithm) element), ShiftBits (ByteString (WordSize algorithm) element), ShiftBits (ByteString (PaddedLength k (ChunkSize algorithm) (2 * WordSize algorithm)) element), BoolType (ByteString (PaddedLength k (ChunkSize algorithm) (2 * WordSize algorithm)) element), Extend (ByteString k element) (ByteString (PaddedLength k (ChunkSize algorithm) (2 * WordSize algorithm)) element), ToWords (ByteString (ChunkSize algorithm) element) (ByteString (WordSize algorithm) element), Concat (ByteString (WordSize algorithm) element) (ByteString (8 * WordSize algorithm) element), ToWords (ByteString (PaddedLength k (ChunkSize algorithm) (2 * WordSize algorithm)) element) (ByteString (ChunkSize algorithm) element)) Source #

Constraints required for a type-safe SHA2

sha2 :: forall (algorithm :: Symbol) element k. SHA2 algorithm element k => ByteString k element -> ByteString (ResultSize algorithm) element Source #

A generalised version of SHA2. It is agnostic of the ByteString base field. Sample usage:

>>> bs = fromConstant (42 :: Natural) :: ByteString 8 (Zp BLS12_381_Scalar)
>>> hash = sha2 @"SHA256" bs

type SHA2N algorithm element = (AlgorithmSetup algorithm element, Finite element, NFData element, FromConstant Natural element, KnownNat (ChunkSize algorithm), KnownNat (WordSize algorithm), Iso (UInt (WordSize algorithm) element) (ByteString (WordSize algorithm) element), Iso (ByteString (WordSize algorithm) element) (UInt (WordSize algorithm) element), AdditiveSemigroup (UInt (WordSize algorithm) element), BoolType (ByteString (WordSize algorithm) element), ShiftBits (ByteString (WordSize algorithm) element), ToWords (ByteString (ChunkSize algorithm) element) (ByteString (WordSize algorithm) element), Concat (ByteString (WordSize algorithm) element) (ByteString (8 * WordSize algorithm) element)) Source #

Constraints required for a SHA2 of a Natural number.

sha2Natural :: forall (algorithm :: Symbol) element. SHA2N algorithm element => Natural -> Natural -> ByteString (ResultSize algorithm) element Source #

Same as sha2 but accepts a Natural number and length of message in bits instead of a ByteString. Only used for testing.

Orphan instances

(KnownNat n, FromConstant Natural a) => ToWords Natural (ByteString n a) Source #

This allows us to calculate hash of a bytestring represented by a Natural number. This is only useful for testing when the length of the test string is unknown at compile time. This should not be exposed to users (and they probably won't find it useful anyway).

Instance details

Methods

toWords :: Natural -> [ByteString n a] Source #