summaryrefslogtreecommitdiffhomepage
path: root/Cost.hs
diff options
context:
space:
mode:
authorJoey Hess <joeyh@joeyh.name>2016-09-13 22:15:18 -0400
committerJoey Hess <joeyh@joeyh.name>2016-09-13 22:32:13 -0400
commit68eb14fdf6debf1e26921a1b2dddf34dbd031471 (patch)
treeb179ae7f113fd89c674862d5c9619282d545a17d /Cost.hs
parent27aef01ba665a14924ece95d5ef4674e3945ef7e (diff)
downloadkeysafe-68eb14fdf6debf1e26921a1b2dddf34dbd031471.tar.gz
use less expensive hash for proof of work
The server has to run the hash once to verify a request, so a hash that took 4 seconds could make the server do too much work if it's being flooded with requests. So, made the hash much less expensive. This required keeping track of fractional seconds. Actually, I used Rational for them, to avoid most rounding problems. That turned out nice. I've only tuned the proofOfWorkHashTunable on my fanless overheating laptop so far. It seems to be fairly reasonablly tuned though.
Diffstat (limited to 'Cost.hs')
-rw-r--r--Cost.hs13
1 files changed, 7 insertions, 6 deletions
diff --git a/Cost.hs b/Cost.hs
index 77c2c4c..dc2438e 100644
--- a/Cost.hs
+++ b/Cost.hs
@@ -22,7 +22,7 @@ totalCost (CPUCost s _) = (s, [UsingCPU])
raiseCostPower :: Cost c -> Entropy e -> Cost c
raiseCostPower c (Entropy e) = mapCost (* 2^e) c
-mapCost :: (Integer -> Integer) -> Cost op -> Cost op
+mapCost :: (Rational-> Rational) -> Cost op -> Cost op
mapCost f (CPUCost (Seconds n) d) = CPUCost (Seconds (f n)) d
type NumCores = Integer
@@ -30,14 +30,15 @@ type NumCores = Integer
showCostMinutes :: NumCores -> Cost op -> String
showCostMinutes numcores (CPUCost (Seconds n) (Divisibility d))
| n' < 61 = "1 minute"
- | otherwise = show (n' `div` 60) ++ " minutes"
+ | otherwise = show (n' / 60) ++ " minutes"
where
- n' = n `div` min numcores d
+ n' :: Double
+ n' = fromRational n / fromIntegral (min numcores d)
-- If an operation took n seconds on a number of cores,
-- multiply to get the CPUCost, which is for a single core.
coreCost :: NumCores -> Seconds -> Divisibility -> Cost op
-coreCost cores (Seconds n) d = CPUCost (Seconds (cores * n)) d
+coreCost cores (Seconds n) d = CPUCost (Seconds (fromIntegral cores * n)) d
castCost :: Cost a -> Cost b
castCost (CPUCost s d) = CPUCost s d
@@ -82,11 +83,11 @@ estimateAttackCost :: DataCenterPrice -> Cost BruteForceOp -> Dollars
estimateAttackCost dc opcost = centsToDollars $ costcents
where
(Seconds cpuseconds) = fst (totalCost opcost)
- cpuyears = cpuseconds `div` (60*60*24*365)
+ cpuyears = cpuseconds / (60*60*24*365)
costpercpuyear = Cents $
fromIntegral (instanceCostPerHour dc) * 24 * 365
`div` (instanceCpuCores dc * instanceCpuCoreMultiplier dc)
- costcents = Cents cpuyears * costpercpuyear
+ costcents = Cents (ceiling cpuyears) * costpercpuyear
newtype Cents = Cents Integer
deriving (Num, Integral, Enum, Real, Ord, Eq, Show)