diff options
author | Joey Hess <joeyh@joeyh.name> | 2016-09-13 22:15:18 -0400 |
---|---|---|
committer | Joey Hess <joeyh@joeyh.name> | 2016-09-13 22:32:13 -0400 |
commit | 68eb14fdf6debf1e26921a1b2dddf34dbd031471 (patch) | |
tree | b179ae7f113fd89c674862d5c9619282d545a17d /Cost.hs | |
parent | 27aef01ba665a14924ece95d5ef4674e3945ef7e (diff) | |
download | keysafe-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.hs | 13 |
1 files changed, 7 insertions, 6 deletions
@@ -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) |