summaryrefslogtreecommitdiffhomepage
path: root/Types/Cost.hs
blob: 2aa6ee7aeb6979fab82a3084ff9e2b224c491db7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
{-# LANGUAGE GeneralizedNewtypeDeriving, MultiParamTypeClasses, EmptyDataDecls #-}

{- Copyright 2016 Joey Hess <id@joeyh.name>
 -
 - Licensed under the GNU AGPL version 3 or higher.
 -}

module Types.Cost where

import Utility.HumanTime

-- | An estimated cost to perform an operation.
data Cost op 
	= CPUCost Seconds -- ^ using 1 CPU core
	deriving (Show, Eq, Ord)

unknownCost :: Cost op
unknownCost = CPUCost (Seconds 0)

newtype Seconds = Seconds Integer
	deriving (Num, Eq, Ord)

instance Show Seconds where
	show (Seconds n) = fromDuration (Duration n)

data UsingHardware = UsingCPU | UsingGPU | UsingASIC
	deriving (Show)

instance Monoid (Cost t) where
	mempty = CPUCost (Seconds 0)
	CPUCost (Seconds a) `mappend` CPUCost (Seconds b) =
		CPUCost (Seconds (a+b))

mapCost :: (Integer -> Integer) -> Cost op -> Cost op
mapCost f (CPUCost (Seconds n)) = CPUCost (Seconds (f n))

showCostMinutes :: Cost op -> String
showCostMinutes (CPUCost (Seconds n))
	| n < 61 = "1 minute"
	| otherwise = show (n `div` 60) ++ " minutes"

-- | Operations whose cost can be measured.
data DecryptionOp
data CreationOp
data BruteForceOp

-- | Things that track their creation cost.
class HasCreationCost t where
	getCreationCost :: t -> Cost CreationOp

-- | Things that track their decryption cost.
class HasDecryptionCost t where
	getDecryptionCost :: t -> Cost DecryptionOp

-- | Calculation of a cost that depends on some amount of entropy.
type CostCalc op t = Entropy t -> Cost op

unknownCostCalc :: CostCalc op t
unknownCostCalc = \_e -> error "No cost calculation available"

-- | Number of bits of entropy
newtype Entropy t = Entropy Int
	deriving (Num, Show)

class CalcEntropy d t where
	calcEntropy :: d -> Entropy t

-- | Entropy can never go negative when subtracting bits from it.
reduceEntropy :: Entropy t -> Int -> Entropy t
reduceEntropy (Entropy a) b = Entropy (max 0 (a - b))

-- | Things that can be brute-forced track their CostCalc.
class Bruteforceable t a where
	getBruteCostCalc :: t -> CostCalc BruteForceOp a

-- | Things that can have entropy
data UnknownPassword
data UnknownName