Skip to content

Commit

Permalink
unify core functions by number of rounds into one function
Browse files Browse the repository at this point in the history
  • Loading branch information
oxarbitrage committed Sep 27, 2023
1 parent 42b64aa commit 33d2890
Showing 1 changed file with 15 additions and 48 deletions.
63 changes: 15 additions & 48 deletions src/Hash.hs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ module Hash
(
coreCompute,
coreDisplay,
core1Compute,
core1Display,
core1Equations,
core2Compute,
core2Display,
core2Equations,
coreEquations,
salsa20Compute,
salsa20Display,
salsa20Equations,
Expand All @@ -34,60 +29,32 @@ import Doubleround
import Utils

-- |The core expression computed.
coreCompute :: [Word32] -> [Word32]
coreCompute input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] = modMatrix (doubleroundRCompute input 10) input
coreCompute _ = error "input to `coreCompute` must be a list of 16 `Word32` numbers"
coreCompute :: [Word32] -> Int -> [Word32]
coreCompute input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] rounds = modMatrix (doubleroundRCompute input rounds) input
coreCompute _ _ = error "input to `coreCompute` must be a list of 16 `Word32` numbers"

-- |The core expression as a string.
coreDisplay :: [String] -> [String]
coreDisplay input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =
modMatrixDisplay (doubleroundRDisplay input 10) input
coreDisplay _ = error "input to `coreDisplay` must be a list of 16 `String` strings"
coreDisplay :: [String] -> Int -> [String]
coreDisplay input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] rounds =
modMatrixDisplay (doubleroundRDisplay input rounds) input
coreDisplay _ _ = error "input to `coreDisplay` must be a list of 16 `String` strings"

-- |The core1 expression computed.
core1Compute :: [Word32] -> [Word32]
core1Compute input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] = modMatrix (doubleroundCompute input) input
core1Compute _ = error "input to `coreCompute` must be a list of 16 `Word32` numbers"

-- |The core1 expression as a string.
core1Display :: [String] -> [String]
core1Display input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =
modMatrixDisplay (doubleroundDisplay input) input
core1Display _ = error "input to `core1Display` must be a list of 16 `String` strings"

-- |The core1 expression as a list of equations.
core1Equations :: [String] -> [String]
core1Equations input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =
[printf "z%d = %s" (idx :: Int) eq | (idx, eq) <- zip [0..] (core1Display input)]
core1Equations _ = error "input to `core1Equations` must be a list of 16 `String` strings"

-- |The core2 expression computed.
core2Compute :: [Word32] -> [Word32]
core2Compute input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] = modMatrix (doubleroundRCompute input 2) input
core2Compute _ = error "input to `core2Compute` must be a list of 16 `Word32` numbers"

-- |The core2 expression as a string.
core2Display :: [String] -> [String]
core2Display input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =
modMatrixDisplay (doubleroundRDisplay input 2) input
core2Display _ = error "input to `core2Display` must be a list of 16 `String` strings"

-- |The core2 expression as a list of equations.
core2Equations :: [String] -> [String]
core2Equations input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] =
[printf "z%d = %s" (idx :: Int) eq | (idx, eq) <- zip [0..] (core2Display input)]
core2Equations _ = error "input to `core2Equations` must be a list of 16 `String` strings"
-- |The core expression as a list of equations.
coreEquations :: [String] -> Int -> [String]
coreEquations input@[_, _, _, _, _, _, _, _, _, _, _, _, _, _, _, _] rounds =
[printf "z%d = %s" (idx :: Int) eq | (idx, eq) <- zip [0..] (coreDisplay input rounds)]
coreEquations _ _ = error "input to `core2Equations` must be a list of 16 `String` strings"

-- | The salsa20 expression computed.
salsa20Compute :: [Word32] -> [Word32]
salsa20Compute input
| length input == 64 = aument $ coreCompute $ reduce input
| length input == 64 = aument $ coreCompute (reduce input) 10
| otherwise = error "input to `salsa20Compute` must be a list of 64 `Word32` numbers"

-- |The salsa20 expression as a string using `core1Display` which is only one round of doubleround.
salsa20Display :: [String] -> [String]
salsa20Display input
| length input == 64 = aumentDisplay $ core1Display $ reduceDisplay input
| length input == 64 = aumentDisplay $ coreDisplay (reduceDisplay input) 10
| otherwise = error "input to `salsa20Display` must be a list of 64 `String` strings"

-- |The salsa20 expression as a list of equations.
Expand Down

0 comments on commit 33d2890

Please sign in to comment.