From 5c42eeef2466b54909c077b3fe3d668bf2b64d4f Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 25 Jul 2016 12:10:41 -0400 Subject: [PATCH 01/21] Added many more benchmarks at all levels of library, and a nice way to display the results. --- lol/Crypto/Lol/Cyclotomic/UCyc.hs | 9 +- lol/benchmarks/BenchParams.hs | 77 ++++++++++++ lol/benchmarks/CTBenches.hs | 44 +++++++ lol/benchmarks/CycBenches.hs | 105 +++++++--------- lol/benchmarks/Main.hs | 172 ++++++++++++++++++++++++-- lol/benchmarks/SimpleTensorBenches.hs | 47 +++++++ lol/benchmarks/SimpleUCycBenches.hs | 52 ++++++++ lol/benchmarks/TensorBenches.hs | 99 +++++++++++++-- lol/benchmarks/UCycBenches.hs | 106 ++++++++++++---- lol/lol.cabal | 11 +- lol/utils/Apply/Cyc.hs | 8 +- 11 files changed, 615 insertions(+), 115 deletions(-) create mode 100644 lol/benchmarks/BenchParams.hs create mode 100644 lol/benchmarks/CTBenches.hs create mode 100644 lol/benchmarks/SimpleTensorBenches.hs create mode 100644 lol/benchmarks/SimpleUCycBenches.hs diff --git a/lol/Crypto/Lol/Cyclotomic/UCyc.hs b/lol/Crypto/Lol/Cyclotomic/UCyc.hs index b6f60c9e..9d9c0db4 100644 --- a/lol/Crypto/Lol/Cyclotomic/UCyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/UCyc.hs @@ -33,7 +33,7 @@ module Crypto.Lol.Cyclotomic.UCyc ( -- * Data types and constraints - UCyc, P, D, C, E, UCycEC, UCRTElt, NFElt + UCyc, P, D, C, E, UCycEC, UCycPC, UCRTElt, NFElt -- * Changing representation , toPow, toDec, toCRT, fmapPow, fmapDec , unzipPow, unzipDec, unzipCRTC, unzipCRTE @@ -93,6 +93,9 @@ data E -- | Convenient synonym for either CRT representation. type UCycEC t m r = Either (UCyc t m E r) (UCyc t m C r) +-- | Convenient synonym for random sampling. +type UCycPC t m r = Either (UCyc t m P r) (UCyc t m C r) + -- | Represents a cyclotomic ring such as \(\Z[\zeta_m]\), -- \(\Z_q[\zeta_m]\), and \(\Q(\zeta_m)\) in an explicit -- representation: @t@ is the 'Tensor' type for storing coefficient tensors; @@ -469,7 +472,7 @@ twaceDec (Dec v) = Dec $ twacePowDec v -- | Twace into a subring, for the CRT basis. (The output is an -- 'Either' because the subring might not support 'C'.) twaceCRTC :: (m `Divides` m', UCRTElt t r) - => UCyc t m' C r -> Either (UCyc t m P r) (UCyc t m C r) + => UCyc t m' C r -> UCycPC t m r {-# INLINABLE twaceCRTC #-} twaceCRTC x@(CRTC s' v) = case crtSentinel of @@ -653,7 +656,7 @@ instance (Random r, UCRTElt t r, Fact m) => Random (UCyc t m D r) where randomR _ = error "randomR non-sensical for UCyc" instance (Random r, UCRTElt t r, Fact m) - => Random (Either (UCyc t m P r) (UCyc t m C r)) where + => Random (UCycPC t m r) where -- create in CRTC basis if possible, otherwise in powerful random = let cons = case crtSentinel of diff --git a/lol/benchmarks/BenchParams.hs b/lol/benchmarks/BenchParams.hs new file mode 100644 index 00000000..c1d9cdee --- /dev/null +++ b/lol/benchmarks/BenchParams.hs @@ -0,0 +1,77 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE UndecidableInstances #-} + +module BenchParams where + +import Utils + +import Crypto.Lol +import Crypto.Lol.Types +import Crypto.Random.DRBG + +import Data.Singletons +import Data.Promotion.Prelude.Eq +import Data.Singletons.TypeRepStar () + + + + +type Tensors = '[T] +type MRCombos = + '[ '(M, R) ] + +type T = CT +type M = F9*F5*F7*F11 -- F64*F9*F25 -- +type R = Zq 34651 -- Zq 14401 -- +type M' = F3*F5*F11 + +testParam :: Proxy '(T, M, R) +testParam = Proxy + +testParam' :: Proxy '(T,M, R, HashDRBG) +testParam' = Proxy + +twoIdxParam :: Proxy '(T, M', M, R) +twoIdxParam = Proxy + +{- +type Tensors = '[CT,RT] +type MRCombos = + '[ '(F1024, Zq 1051649), -- 1024 / 512 + '(F2048, Zq 1054721), -- 2048 / 1024 + '(F64 * F27, Zq 1048897), -- 1728 / 576 + '(F64 * F81, Zq 1073089), -- 5184 / 1728 + '(F64*F9*F25, Zq 1065601) -- 14400 / 3840 + ] +-} + +type MM'RCombos = + '[ '(F8 * F91, F8 * F91 * F4, Zq 8737), + '(F8 * F91, F8 * F91 * F5, Zq 14561), + '(F128, F128 * F91, Zq 23297) + ] + +-- EAC: must be careful where we use Nub: apparently TypeRepStar doesn't work well with the Tensor constructors +type AllParams = ( '(,) <$> Tensors) <*> MRCombos +allParams :: Proxy AllParams +allParams = Proxy + +type LiftParams = ( '(,) <$> Tensors) <*> MRCombos +liftParams :: Proxy LiftParams +liftParams = Proxy + +type TwoIdxParams = ( '(,) <$> Tensors) <*> MM'RCombos +twoIdxParams :: Proxy TwoIdxParams +twoIdxParams = Proxy + +type ErrorParams = ( '(,) <$> '[HashDRBG]) <*> LiftParams +errorParams :: Proxy ErrorParams +errorParams = Proxy + +data Liftable :: TyFun (Factored, *) Bool -> * +type instance Apply Liftable '(m',r) = Int64 :== (LiftOf r) + +data RemoveM :: TyFun (Factored, Factored, *) (Factored, *) -> * +type instance Apply RemoveM '(m,m',r) = '(m',r) diff --git a/lol/benchmarks/CTBenches.hs b/lol/benchmarks/CTBenches.hs new file mode 100644 index 00000000..8fde8c49 --- /dev/null +++ b/lol/benchmarks/CTBenches.hs @@ -0,0 +1,44 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE UndecidableInstances #-} + +module CTBenches (ctBenches) where + +import Control.Applicative +import Control.Monad.Random + +import Crypto.Lol.Cyclotomic.Tensor +import Crypto.Lol.Prelude +import Crypto.Lol.Types +import Crypto.Random.DRBG + +import Criterion +import BenchParams + +ctBenches :: IO Benchmark +ctBenches = do + x1 :: T M (R, R) <- getRandom + x2 :: T M R <- getRandom + x3 :: T M R <- getRandom + gen <- newGenIO + return $ bgroup "CT" [ + bench "unzipPow" $ nf unzipT' x1, + bench "unzipDec" $ nf unzipT' x1, + bench "unzipCRT" $ nf unzipT' x1, + bench "zipWith (*)" $ nf (zipWithT' (*) x2) x3, + bench "crt" $ nf (wrap $ fromJust' "CTBenches.crt" crt') x2, + bench "crtInv" $ nf (wrap $ fromJust' "CTBenches.crtInv" crtinv') x2, + bench "l" $ nf (wrap l') x2, + bench "lInv" $ nf (wrap lInv') x2, + bench "*g Pow" $ nf (wrap mulGPow'') x2, + bench "*g CRT" $ nf (wrap $ fromJust' "CTBenches.gcrt" mulGCRT'') x2, + bench "lift" $ nf (fmapT lift) x2, + bench "error" $ nf (evalRand (fmapT (roundMult one) <$> + (CT <$> cDispatchGaussian + (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (T M Double))) :: CryptoRand HashDRBG -> (T M Int64)) gen + + ] \ No newline at end of file diff --git a/lol/benchmarks/CycBenches.hs b/lol/benchmarks/CycBenches.hs index 9c700277..e071aa89 100644 --- a/lol/benchmarks/CycBenches.hs +++ b/lol/benchmarks/CycBenches.hs @@ -1,13 +1,14 @@ -{-# LANGUAGE DataKinds, FlexibleContexts, - NoImplicitPrelude, RebindableSyntax, - ScopedTypeVariables, TypeFamilies, - TypeOperators, UndecidableInstances #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} module CycBenches (cycBenches) where import Apply.Cyc import Benchmarks -import Utils +import BenchParams import Control.Monad.Random @@ -15,24 +16,25 @@ import Crypto.Lol import Crypto.Lol.Types import Crypto.Random.DRBG -import Data.Singletons -import Data.Promotion.Prelude.Eq -import Data.Singletons.TypeRepStar () cycBenches :: IO Benchmark cycBenches = benchGroup "Cyc" [ - benchGroup "unzipCycPow" $ applyUnzip allParams $ hideArgs bench_unzipCycPow, - benchGroup "unzipCycCRT" $ applyUnzip allParams $ hideArgs bench_unzipCycCRT, - benchGroup "*" $ applyBasic allParams $ hideArgs bench_mul, - benchGroup "crt" $ applyBasic allParams $ hideArgs bench_crt, - benchGroup "crtInv" $ applyBasic allParams $ hideArgs bench_crtInv, - benchGroup "l" $ applyBasic allParams $ hideArgs bench_l, - benchGroup "*g Pow" $ applyBasic allParams $ hideArgs bench_mulgPow, - benchGroup "*g CRT" $ applyBasic allParams $ hideArgs bench_mulgCRT, - benchGroup "lift" $ applyLift liftParams $ hideArgs bench_liftPow, - benchGroup "error" $ applyError errorParams $ hideArgs $ bench_errRounded 0.1, - benchGroup "twace" $ applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, - benchGroup "embed" $ applyTwoIdx twoIdxParams $ hideArgs bench_embedPow + benchGroup "unzipPow" $ [hideArgs bench_unzipCycPow testParam], -- applyUnzip allParams $ hideArgs bench_unzipCycPow, + benchGroup "unzipDec" $ [hideArgs bench_unzipCycDec testParam], + benchGroup "unzipCRT" $ [hideArgs bench_unzipCycCRT testParam], --applyUnzip allParams $ hideArgs bench_unzipCycCRT, + benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], -- applyBasic allParams $ hideArgs bench_mul, + benchGroup "crt" $ [hideArgs bench_crt testParam], --applyBasic allParams $ hideArgs bench_crt, + benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], --applyBasic allParams $ hideArgs bench_crtInv, + benchGroup "l" $ [hideArgs bench_l testParam], --applyBasic allParams $ hideArgs bench_l, + benchGroup "lInv" $ [hideArgs bench_lInv testParam], + benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, + benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, + benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, + benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, + benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], + benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_embedPow + benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam] ] bench_unzipCycPow :: (UnzipCtx t m r) => Cyc t m (r,r) -> Bench '(t,m,r) @@ -40,6 +42,11 @@ bench_unzipCycPow a = let a' = advisePow a in bench unzipCyc a' +bench_unzipCycDec :: (UnzipCtx t m r) => Cyc t m (r,r) -> Bench '(t,m,r) +bench_unzipCycDec a = + let a' = adviseDec a + in bench unzipCyc a' + bench_unzipCycCRT :: (UnzipCtx t m r) => Cyc t m (r,r) -> Bench '(t,m,r) bench_unzipCycCRT a = let a' = adviseCRT a @@ -64,9 +71,13 @@ bench_crtInv x = let y = adviseCRT x in bench advisePow y bench_l :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) bench_l x = let y = adviseDec x in bench advisePow y +-- convert input from Pow basis to Dec basis +bench_lInv :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) +bench_lInv x = let y = advisePow x in bench adviseDec y + -- lift an element in the Pow basis bench_liftPow :: forall t m r . (LiftCtx t m r) => Cyc t m r -> Bench '(t,m,r) -bench_liftPow x = let y = advisePow x in bench (liftCyc Pow :: Cyc t m r -> Cyc t m (LiftOf r)) y +bench_liftPow x = let y = advisePow x in bench (liftCyc Pow) y -- multiply by g when input is in Pow basis bench_mulgPow :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) @@ -89,46 +100,20 @@ bench_twacePow x = let y = advisePow x in bench (twace :: Cyc t m' r -> Cyc t m r) y +bench_twaceCRT :: forall t m m' r . (TwoIdxCtx t m m' r) + => Cyc t m' r -> Bench '(t,m,m',r) +bench_twaceCRT x = + let y = adviseCRT x + in bench (twace :: Cyc t m' r -> Cyc t m r) y + bench_embedPow :: forall t m m' r . (TwoIdxCtx t m m' r) => Cyc t m r -> Bench '(t,m,m',r) bench_embedPow x = let y = advisePow x - in bench (embed :: Cyc t m r -> Cyc t m' r) y - -type Tensors = '[CT,RT] -type MRCombos = - '[ '(F1024, Zq 1051649), -- 1024 / 512 - '(F2048, Zq 1054721), -- 2048 / 1024 - '(F64 * F27, Zq 1048897), -- 1728 / 576 - '(F64 * F81, Zq 1073089), -- 5184 / 1728 - '(F64*F9*F25, Zq 1065601) -- 14400 / 3840 - ] - -type MM'RCombos = - '[ '(F8 * F91, F8 * F91 * F4, Zq 8737), - '(F8 * F91, F8 * F91 * F5, Zq 14561), - '(F128, F128 * F91, Zq 23297) - ] - --- EAC: must be careful where we use Nub: apparently TypeRepStar doesn't work well with the Tensor constructors -type AllParams = ( '(,) <$> Tensors) <*> MRCombos -allParams :: Proxy AllParams -allParams = Proxy - -type LiftParams = ( '(,) <$> Tensors) <*> MRCombos -liftParams :: Proxy LiftParams -liftParams = Proxy - -type TwoIdxParams = ( '(,) <$> Tensors) <*> MM'RCombos -twoIdxParams :: Proxy TwoIdxParams -twoIdxParams = Proxy - -type ErrorParams = ( '(,) <$> '[HashDRBG]) <*> LiftParams -errorParams :: Proxy ErrorParams -errorParams = Proxy - -data Liftable :: TyFun (Factored, *) Bool -> * -type instance Apply Liftable '(m',r) = Int64 :== (LiftOf r) - -data RemoveM :: TyFun (Factored, Factored, *) (Factored, *) -> * -type instance Apply RemoveM '(m,m',r) = '(m',r) + in bench (advisePow . embed :: Cyc t m r -> Cyc t m' r) y + +bench_embedDec :: forall t m m' r . (TwoIdxCtx t m m' r) + => Cyc t m r -> Bench '(t,m,m',r) +bench_embedDec x = + let y = adviseDec x + in bench (adviseDec . embed :: Cyc t m r -> Cyc t m' r) y \ No newline at end of file diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index 7e45342c..b988d471 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -1,15 +1,171 @@ +{- +import TensorBenches +import Criterion.Main + +main :: IO () +main = defaultMain =<< sequence [ + tensorBenches + ] +-} +{-# LANGUAGE BangPatterns, RecordWildCards #-} import CycBenches +import SimpleTensorBenches import TensorBenches +import SimpleUCycBenches import UCycBenches -import ZqBenches -import Criterion.Main +import Criterion.Internal (runAndAnalyseOne) +import Criterion.Main.Options (defaultConfig) +import Criterion.Measurement (secs) +import Criterion.Monad (Criterion, withConfig) +import Criterion.Types +import Control.Monad (foldM, forM_, when) +import Control.Monad.IO.Class (MonadIO, liftIO) + +import Control.Exception (evaluate) + +import Control.DeepSeq (rnf) + +import Data.List (transpose) +import qualified Data.Map as Map +import Data.Maybe + +import Statistics.Resampling.Bootstrap (Estimate(..)) +import System.Console.ANSI +import System.IO +import Text.Printf + +-- table print parameters +colWidth, testNameWidth :: Int +colWidth = 15 +testNameWidth = 40 +verb :: Verb +verb = Abridged + +benches :: [String] +benches = [ + {-"unzipPow", + "unzipDec", + "unzipCRT", + "zipWith (*)", + "crt", + "crtInv", + "l", + "lInv", + "*g Pow", + "*g CRT", + "lift", + "error",-} + "twacePow", + "twaceCRT"{-, + "embedPow", + "embedDec"-} -main :: IO () -main = defaultMain =<< sequence [ - zqBenches, - tensorBenches, - ucycBenches, - cycBenches ] + +data Verb = Progress | Abridged | Full deriving (Eq) + +main :: IO () +main = do + hSetBuffering stdout NoBuffering -- for better printing of progress + reports <- mapM (getReports =<<) [ + simpleTensorBenches, + tensorBenches, + simpleUCycBenches, + ucycBenches, + cycBenches + ] + when (verb == Progress) $ putStrLn "" + printTable $ map reverse reports + +printTable :: [[Report]] -> IO () +printTable rpts = do + let colLbls = map (takeWhile (/= '/') . reportName . head) rpts + printf testName "" + mapM_ (\lbl -> printf col lbl) colLbls + printf "\n" + mapM_ printRow $ transpose rpts + +col, testName :: String +testName = "%-" ++ (show testNameWidth) ++ "s " +col = "%-" ++ (show colWidth) ++ "s " + +printANSI :: (MonadIO m) => Color -> String -> m () +printANSI sgr str = liftIO $ do + setSGR [SetColor Foreground Vivid sgr] + putStrLn str + setSGR [Reset] + +config :: Config +config = defaultConfig {verbosity = if verb == Full then Normal else Quiet} + +getRuntime :: Report -> Double +getRuntime Report{..} = + let SampleAnalysis{..} = reportAnalysis + (builtin, _) = splitAt 1 anRegress + mests = map (\Regression{..} -> Map.lookup "iters" regCoeffs) builtin + [Estimate{..}] = catMaybes mests + in estPoint + +-- See Criterion.Internal.analyseOne +printRow :: [Report] -> IO () +printRow xs@(rpt : _) = do + printf testName $ stripOuterGroup $ reportName rpt + let times = map getRuntime xs + minTime = minimum times + printCol t = + if t > (1.1*minTime) + then do + setSGR [SetColor Foreground Vivid Red] + printf col $ secs t + setSGR [Reset] + else printf col $ secs t + forM_ times printCol + putStrLn "" + +stripOuterGroup :: String -> String +stripOuterGroup = tail . dropWhile (/= '/') + +getReports :: Benchmark -> IO [Report] +getReports = withConfig config . runAndAnalyse + +-- | Run, and analyse, one or more benchmarks. +-- From Criterion.Internal +runAndAnalyse :: Benchmark + -> Criterion [Report] +runAndAnalyse bs = for bs $ \idx desc bm -> do + when (verb == Abridged || verb == Full) $ liftIO $ putStr $ "benchmark " ++ desc + when (verb == Full) $ liftIO $ putStrLn "" + (Analysed rpt) <- runAndAnalyseOne idx desc bm + when (verb == Progress) $ liftIO $ putStr "." + when (verb == Abridged) $ liftIO $ putStrLn $ "..." ++ (secs $ getRuntime rpt) + return rpt + +-- | Iterate over benchmarks. +-- From Criterion.Internal +for :: MonadIO m => Benchmark + -> (Int -> String -> Benchmarkable -> m a) -> m [a] +for bs0 handle = snd <$> go (0::Int, []) ("", bs0) + where + select = flip elem benches . takeWhile (/= '/') . stripOuterGroup + go (!idx,drs) (pfx, Environment mkenv mkbench) + | shouldRun pfx mkbench = do + e <- liftIO $ do + ee <- mkenv + evaluate (rnf ee) + return ee + go (idx,drs) (pfx, mkbench e) + | otherwise = return (idx,drs) + go (!idx, drs) (pfx, Benchmark desc b) + | select desc' = do + x <- handle idx desc' b; + return (idx + 1, x:drs) + | otherwise = return (idx, drs) + where desc' = addPrefix pfx desc + go (!idx,drs) (pfx, BenchGroup desc bs) = + foldM go (idx,drs) [(addPrefix pfx desc, b) | b <- bs] + + shouldRun pfx mkbench = + any (select . addPrefix pfx) . benchNames . mkbench $ + error "Criterion.env could not determine the list of your benchmarks since they force the environment (see the documentation for details)" diff --git a/lol/benchmarks/SimpleTensorBenches.hs b/lol/benchmarks/SimpleTensorBenches.hs new file mode 100644 index 00000000..528fef68 --- /dev/null +++ b/lol/benchmarks/SimpleTensorBenches.hs @@ -0,0 +1,47 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE UndecidableInstances #-} + +module SimpleTensorBenches (simpleTensorBenches) where + +import Control.Applicative +import Control.Monad.Random + +import Crypto.Lol.Prelude +import Crypto.Lol.Cyclotomic.Tensor +import Crypto.Lol.Types +import Crypto.Random.DRBG + +import Criterion +import BenchParams + +simpleTensorBenches :: IO Benchmark +simpleTensorBenches = do + x1 :: T M (R, R) <- getRandom + x2 :: T M R <- getRandom + x3 :: T M R <- getRandom + x4 :: T M' R <- getRandom + gen <- newGenIO + return $ bgroup "STensor" [ + bench "unzipPow" $ nf unzipT x1, + bench "unzipDec" $ nf unzipT x1, + bench "unzipCRT" $ nf unzipT x1, + bench "zipWith (*)" $ nf (zipWithT (*) x2) x3, + bench "crt" $ nf (fromJust' "SimpleTensorBenches.crt" crt) x2, + bench "crtInv" $ nf (fromJust' "SimpleTensorBenches.crtInv" crtInv) x2, + bench "l" $ nf l x2, + bench "lInv" $ nf lInv x2, + bench "*g Pow" $ nf mulGPow x2, + bench "*g CRT" $ nf (fromJust' "SimpleTensorBenches.gcrt" mulGCRT) x2, + bench "lift" $ nf (fmapT lift) x2, + bench "error" $ nf (evalRand (fmapT (roundMult one) <$> + (tGaussianDec (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (T M Double))) :: CryptoRand HashDRBG -> T M Int64) gen, + bench "twacePow" $ nf (twacePowDec :: T M R -> T M' R) x2, + bench "twaceCRT" $ nf (fromJust' "SimpleTensorBenches.twaceCRT" twaceCRT :: T M R -> T M' R) x2, + bench "embedPow" $ nf (embedPow :: T M' R -> T M R) x4, + bench "embedDec" $ nf (embedDec :: T M' R -> T M R) x4 + ] \ No newline at end of file diff --git a/lol/benchmarks/SimpleUCycBenches.hs b/lol/benchmarks/SimpleUCycBenches.hs new file mode 100644 index 00000000..eecde794 --- /dev/null +++ b/lol/benchmarks/SimpleUCycBenches.hs @@ -0,0 +1,52 @@ +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} + +module SimpleUCycBenches (simpleUCycBenches) where + +import Control.Applicative +import Control.Monad.Random +import BenchParams + +import Crypto.Lol.Prelude +import Crypto.Lol.Cyclotomic.UCyc +import Crypto.Lol.Types +import Crypto.Random.DRBG + +import Criterion + +simpleUCycBenches :: IO Benchmark +simpleUCycBenches = do + x1 :: UCyc T M P (R, R) <- getRandom + let x1' = toDec x1 + (Right x2) :: UCycPC T M (R, R) <- getRandom + x3 :: UCycEC T M R <- pcToEC <$> getRandom + x4 :: UCyc T M P R <- getRandom + let x5 = toDec x4 + (Right x6) :: UCycPC T M R <- getRandom + x7 :: UCyc T M' P R <- getRandom + let x8 = toDec x7 + gen <- newGenIO + return $ bgroup "SUCyc" [ + bench "unzipPow" $ nf unzipPow x1, + bench "unzipDec" $ nf unzipDec x1', + bench "unzipCRT" $ nf unzipCRTC x2, + bench "zipWith (*)" $ nf (x3*) x3, + bench "crt" $ nf toCRT x4, + bench "crtInv" $ nf toPow x6, + bench "l" $ nf toPow x5, + bench "lInv" $ nf toDec x4, + bench "*g Pow" $ nf mulG x4, + bench "*g CRT" $ nf mulG x6, + bench "lift" $ nf lift x4, + bench "error" $ nf (evalRand (errorRounded (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (UCyc T M D Int64))) gen, + bench "twacePow" $ nf (twacePow :: UCyc T M P R -> UCyc T M' P R) x4, + bench "twaceCRT" $ nf (twaceCRTC :: UCyc T M C R -> UCycPC T M' R) x6, + bench "embedPow" $ nf (embedPow :: UCyc T M' P R -> UCyc T M P R) x7, + bench "embedDec" $ nf (embedDec :: UCyc T M' D R -> UCyc T M D R) x8 + ] + +pcToEC :: UCycPC t m r -> UCycEC t m r +pcToEC (Right x) = (Right x) \ No newline at end of file diff --git a/lol/benchmarks/TensorBenches.hs b/lol/benchmarks/TensorBenches.hs index 59a61892..2d672707 100644 --- a/lol/benchmarks/TensorBenches.hs +++ b/lol/benchmarks/TensorBenches.hs @@ -1,28 +1,101 @@ -{-# LANGUAGE DataKinds, FlexibleContexts, - NoImplicitPrelude, RebindableSyntax, - ScopedTypeVariables, TypeFamilies, - TypeOperators, UndecidableInstances #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} +{-# LANGUAGE TypeOperators #-} +{-# LANGUAGE UndecidableInstances #-} module TensorBenches (tensorBenches) where import Apply.Cyc import Benchmarks -import Utils +import BenchParams -import Crypto.Lol +import Control.Applicative +import Control.Monad.Random + +import Crypto.Lol.Prelude import Crypto.Lol.Cyclotomic.Tensor import Crypto.Lol.Types +import Crypto.Random.DRBG tensorBenches :: IO Benchmark tensorBenches = benchGroup "Tensor" [ - benchGroup "l" $ applyBasic (Proxy::Proxy QuickParams) $ hideArgs bench_l] + benchGroup "unzipPow" $ [hideArgs bench_unzip testParam], + benchGroup "unzipDec" $ [hideArgs bench_unzip testParam], + benchGroup "unzipCRT" $ [hideArgs bench_unzip testParam], --applyUnzip allParams $ hideArgs bench_unzip, + benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], --applyBasic allParams $ hideArgs bench_mul, + benchGroup "crt" $ [hideArgs bench_crt testParam], --applyBasic allParams $ hideArgs bench_crt, + benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], --applyBasic allParams $ hideArgs bench_crtInv, + benchGroup "l" $ [hideArgs bench_l testParam], --applyBasic allParams $ hideArgs bench_l, + benchGroup "lInv" $ [hideArgs bench_lInv testParam], + benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, + benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, + benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, + benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, + benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], + benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_embedPow-} + benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam] + ] + +bench_unzip :: (UnzipCtx t m r) => t m (r,r) -> Bench '(t,m,r) +bench_unzip = bench unzipT + +-- no CRT conversion, just coefficient-wise multiplication +bench_mul :: (BasicCtx t m r) => t m r -> t m r -> Bench '(t,m,r) +bench_mul a = bench (zipWithT (*) a) + +-- convert input from Pow basis to CRT basis +bench_crt :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_crt = bench (fromJust' "TensorBenches.bench_crt" crt) + +-- convert input from CRT basis to Pow basis +bench_crtInv :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_crtInv = bench (fromJust' "TensorBenches.bench_crtInv" crtInv) -- convert input from Dec basis to Pow basis -bench_l :: (Tensor t, Fact m, Additive r, TElt t r, NFData (t m r)) => t m r -> Bench '(t,m,r) +bench_l :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) bench_l = bench l -type QuickTest = '[ '(F128, Zq 257), - '(F32 * F9, Zq 577), - '(F32 * F9, Int64) ] -type Tensors = '[CT,RT] -type QuickParams = ( '(,) <$> Tensors) <*> QuickTest +-- convert input from Dec basis to Pow basis +bench_lInv :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_lInv = bench lInv + +-- lift an element in the Pow basis +bench_liftPow :: forall t m r . (LiftCtx t m r) => t m r -> Bench '(t,m,r) +bench_liftPow = bench (fmapT lift) + +-- multiply by g when input is in Pow basis +bench_mulgPow :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_mulgPow = bench mulGPow + +-- multiply by g when input is in CRT basis +bench_mulgCRT :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_mulgCRT = bench (fromJust' "TensorBenches.bench_mulgCRT" mulGCRT) + +-- generate a rounded error term +bench_errRounded :: forall t m r gen . (ErrorCtx t m r gen) + => Double -> Bench '(t,m,r,gen) +bench_errRounded v = benchIO $ do + gen <- newGenIO + return $ evalRand + (fmapT (roundMult one) <$> + (tGaussianDec v :: Rand (CryptoRand gen) (t m Double)) :: Rand (CryptoRand gen) (t m (LiftOf r))) gen + +bench_twacePow :: forall t m m' r . (TwoIdxCtx t m m' r) + => t m' r -> Bench '(t,m,m',r) +bench_twacePow = bench (twacePowDec :: t m' r -> t m r) + +bench_twaceCRT :: forall t m m' r . (TwoIdxCtx t m m' r) + => t m' r -> Bench '(t,m,m',r) +bench_twaceCRT = bench (fromJust' "TensorBenches.bench_twaceCRT" twaceCRT :: t m' r -> t m r) + +bench_embedPow :: forall t m m' r . (TwoIdxCtx t m m' r) + => t m r -> Bench '(t,m,m',r) +bench_embedPow = bench (embedPow :: t m r -> t m' r) + +bench_embedDec :: forall t m m' r . (TwoIdxCtx t m m' r) + => t m r -> Bench '(t,m,m',r) +bench_embedDec = bench (embedDec :: t m r -> t m' r) diff --git a/lol/benchmarks/UCycBenches.hs b/lol/benchmarks/UCycBenches.hs index d46a9190..b70b5336 100644 --- a/lol/benchmarks/UCycBenches.hs +++ b/lol/benchmarks/UCycBenches.hs @@ -1,48 +1,108 @@ -{-# LANGUAGE DataKinds, FlexibleContexts, - NoImplicitPrelude, RebindableSyntax, - ScopedTypeVariables, TypeFamilies, - TypeOperators, UndecidableInstances #-} +{-# LANGUAGE DataKinds #-} +{-# LANGUAGE FlexibleContexts #-} +{-# LANGUAGE NoImplicitPrelude #-} +{-# LANGUAGE ScopedTypeVariables #-} +{-# LANGUAGE TypeFamilies #-} module UCycBenches (ucycBenches) where import Apply.Cyc import Benchmarks -import Utils +import BenchParams -import Crypto.Lol +import Control.Monad.Random + +import Crypto.Lol.Prelude import Crypto.Lol.Cyclotomic.UCyc import Crypto.Lol.Types +import Crypto.Random.DRBG ucycBenches :: IO Benchmark ucycBenches = benchGroup "UCyc" [ - benchGroup "l" $ applyBasic (Proxy::Proxy QuickParams) $ hideArgs bench_l, - benchGroup "twace" $ applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, - benchGroup "embed" $ applyTwoIdx twoIdxParams $ hideArgs bench_embedPow + benchGroup "unzipPow" $ [hideArgs bench_unzipUCycPow testParam], -- applyUnzip allParams $ hideArgs bench_unzipUCycPow, + benchGroup "unzipDec" $ [hideArgs bench_unzipUCycDec testParam], + benchGroup "unzipCRT" $ [hideArgs bench_unzipUCycCRT testParam], -- applyUnzip allParams $ hideArgs bench_unzipUCycCRT, + benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], -- applyBasic allParams $ hideArgs bench_mul, + benchGroup "crt" $ [hideArgs bench_crt testParam], -- applyBasic allParams $ hideArgs bench_crt, + benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], -- applyBasic allParams $ hideArgs bench_crtInv, + benchGroup "l" $ [hideArgs bench_l testParam], -- applyBasic allParams $ hideArgs bench_l, + benchGroup "lInv" $ [hideArgs bench_lInv testParam], + benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], -- applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], -- applyBasic allParams $ hideArgs bench_mulgCRT, + benchGroup "lift" $ [hideArgs bench_liftPow testParam], -- applyLift liftParams $ hideArgs bench_liftPow, + benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], -- applyError errorParams $ hideArgs $ bench_errRounded 0.1 + benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], -- applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, + benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], + benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], -- applyTwoIdx twoIdxParams $ hideArgs bench_embedPow + benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam] ] +bench_unzipUCycPow :: (UnzipCtx t m r) => UCyc t m P (r,r) -> Bench '(t,m,r) +bench_unzipUCycPow = bench unzipPow + +bench_unzipUCycDec :: (UnzipCtx t m r) => UCyc t m D (r,r) -> Bench '(t,m,r) +bench_unzipUCycDec = bench unzipDec + +bench_unzipUCycCRT :: (UnzipCtx t m r) => UCycPC t m (r,r) -> Bench '(t,m,r) +bench_unzipUCycCRT (Right a) = bench unzipCRTC a + +pcToEC :: UCycPC t m r -> UCycEC t m r +pcToEC (Right x) = (Right x) + +-- no CRT conversion, just coefficient-wise multiplication +bench_mul :: (BasicCtx t m r) => UCycPC t m r -> UCycPC t m r -> Bench '(t,m,r) +bench_mul a b = + let a' = pcToEC a + b' = pcToEC b + in bench (a' *) b' + +-- convert input from Pow basis to CRT basis +bench_crt :: (BasicCtx t m r) => UCyc t m P r -> Bench '(t,m,r) +bench_crt = bench toCRT + +-- convert input from CRT basis to Pow basis +bench_crtInv :: (BasicCtx t m r) => UCycPC t m r -> Bench '(t,m,r) +bench_crtInv (Right a) = bench toPow a + -- convert input from Dec basis to Pow basis bench_l :: (BasicCtx t m r) => UCyc t m D r -> Bench '(t,m,r) bench_l = bench toPow +-- convert input from Pow basis to Dec basis +bench_lInv :: (BasicCtx t m r) => UCyc t m P r -> Bench '(t,m,r) +bench_lInv = bench toDec + +-- lift an element in the Pow basis +bench_liftPow :: (LiftCtx t m r) => UCyc t m P r -> Bench '(t,m,r) +bench_liftPow = bench lift + +-- multiply by g when input is in Pow basis +bench_mulgPow :: (BasicCtx t m r) => UCyc t m P r -> Bench '(t,m,r) +bench_mulgPow = bench mulG + +-- multiply by g when input is in CRT basis +bench_mulgCRT :: (BasicCtx t m r) => UCycPC t m r -> Bench '(t,m,r) +bench_mulgCRT (Right a) = bench mulG a + +-- generate a rounded error term +bench_errRounded :: forall t m r gen . (ErrorCtx t m r gen) + => Double -> Bench '(t,m,r,gen) +bench_errRounded v = benchIO $ do + gen <- newGenIO + return $ evalRand (errorRounded v :: Rand (CryptoRand gen) (UCyc t m D (LiftOf r))) gen + bench_twacePow :: forall t m m' r . (TwoIdxCtx t m m' r) => UCyc t m' P r -> Bench '(t,m,m',r) bench_twacePow = bench (twacePow :: UCyc t m' P r -> UCyc t m P r) +bench_twaceCRT :: forall t m m' r . (TwoIdxCtx t m m' r) + => UCycPC t m' r -> Bench '(t,m,m',r) +bench_twaceCRT (Right a) = bench (twaceCRTC :: UCyc t m' C r -> UCycPC t m r) a + bench_embedPow :: forall t m m' r . (TwoIdxCtx t m m' r) => UCyc t m P r -> Bench '(t,m,m',r) bench_embedPow = bench (embedPow :: UCyc t m P r -> UCyc t m' P r) -type QuickTest = '[ '(F128, Zq 257), - '(F32 * F9, Zq 577), - '(F32 * F9, Int64) ] -type Tensors = '[CT,RT] -type QuickParams = ( '(,) <$> Tensors) <*> QuickTest - -type MM'RCombos = - '[ '(F8 * F91, F8 * F91 * F4, Zq 8737), - '(F8 * F91, F8 * F91 * F5, Zq 14561), - '(F128, F128 * F91, Zq 23297) - ] -type TwoIdxParams = ( '(,) <$> Tensors) <*> MM'RCombos -twoIdxParams :: Proxy TwoIdxParams -twoIdxParams = Proxy +bench_embedDec :: forall t m m' r . (TwoIdxCtx t m m' r) + => UCyc t m D r -> Bench '(t,m,m',r) +bench_embedDec = bench (embedDec :: UCyc t m D r -> UCyc t m' D r) \ No newline at end of file diff --git a/lol/lol.cabal b/lol/lol.cabal index b0bbd3fe..8da0818a 100644 --- a/lol/lol.cabal +++ b/lol/lol.cabal @@ -205,14 +205,16 @@ Benchmark bench-lol if flag(llvm) ghc-options: -fllvm -optlo-O3 - -- ghc-options: -threaded -rtsopts - ghc-options: -O3 -Odph -funbox-strict-fields -fno-liberate-case -funfolding-use-threshold1000 -funfolding-keeness-factor1000 - -- ghc-options: -O2 -Odph -funbox-strict-fields -fwarn-dodgy-imports -rtsopts - -- ghc-options: -fno-liberate-case -funfolding-use-threshold1000 -funfolding-keeness-factor1000 + ghc-options: -O2 -Odph -funbox-strict-fields + ghc-options: -fno-liberate-case -funfolding-use-threshold1000 -funfolding-keeness-factor1000 + ghc-options: -ddump-to-file -ddump-simpl + ghc-options: -dsuppress-coercions -dsuppress-type-applications -dsuppress-uniques -dsuppress-module-prefixes build-depends: + ansi-terminal, arithmoi, base, + containers, criterion, deepseq, DRBG, @@ -220,6 +222,7 @@ Benchmark bench-lol MonadRandom, mtl, singletons, + statistics, transformers, vector, repa diff --git a/lol/utils/Apply/Cyc.hs b/lol/utils/Apply/Cyc.hs index 9ddce052..bf785d4b 100644 --- a/lol/utils/Apply/Cyc.hs +++ b/lol/utils/Apply/Cyc.hs @@ -46,7 +46,7 @@ applyBasic params g = run params $ \(BC p) -> g p data UnzipCtxD type UnzipCtx t m r = - (Fact m, CElt t (r,r), Random (t m (r,r)), CElt t r, ShowType '(t,m,r), NFElt r, Random r) + (Fact m, CElt t (r,r), Random (t m (r,r)), CElt t r, ShowType '(t,m,r), NFElt r, Random r, NFData (t m r)) data instance ArgsCtx UnzipCtxD where UzC :: (UnzipCtx t m r) => Proxy '(t,m,r) -> ArgsCtx UnzipCtxD instance (params `Satisfy` UnzipCtxD, UnzipCtx t m r) @@ -64,7 +64,7 @@ applyUnzip params g = run params $ \(UzC p) -> g p data LiftCtxD type LiftCtx t m r = (BasicCtx t m r, Lift' r, CElt t (LiftOf r), NFElt (LiftOf r), ToInteger (LiftOf r), - TElt CT r, TElt RT r, TElt CT (LiftOf r), TElt RT (LiftOf r)) + TElt CT r, TElt RT r, TElt CT (LiftOf r), TElt RT (LiftOf r), NFData (t m (LiftOf r))) data instance ArgsCtx LiftCtxD where LC :: (LiftCtx t m r) => Proxy '(t,m,r) -> ArgsCtx LiftCtxD instance (params `Satisfy` LiftCtxD, LiftCtx t m r) @@ -81,7 +81,7 @@ applyLift params g = run params $ \(LC p) -> g p data ErrorCtxD type ErrorCtx t m r gen = (CElt t r, Fact m, ShowType '(t,m,r,gen), CElt t (LiftOf r), NFElt (LiftOf r), Lift' r, - ToInteger (LiftOf r), CryptoRandomGen gen) + ToInteger (LiftOf r), CryptoRandomGen gen, NFData (t m (LiftOf r))) data instance ArgsCtx ErrorCtxD where EC :: (ErrorCtx t m r gen) => Proxy '(t,m,r,gen) -> ArgsCtx ErrorCtxD instance (params `Satisfy` ErrorCtxD, ErrorCtx t m r gen) @@ -97,7 +97,7 @@ applyError params g = run params $ \(EC p) -> g p data TwoIdxCtxD type TwoIdxCtx t m m' r = (m `Divides` m', CElt t r, IntegralDomain r, Eq r, Random r, NFElt r, - ShowType '(t,m,m',r), Random (t m r), Random (t m' r)) + ShowType '(t,m,m',r), Random (t m r), Random (t m' r), NFData (t m r), NFData (t m' r)) data instance ArgsCtx TwoIdxCtxD where TI :: (TwoIdxCtx t m m' r) => Proxy '(t,m,m',r) -> ArgsCtx TwoIdxCtxD instance (params `Satisfy` TwoIdxCtxD, TwoIdxCtx t m m' r) From 6867b312aac214e5403c41403ff381e283157870 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 25 Jul 2016 12:20:24 -0400 Subject: [PATCH 02/21] Fixed unzip/zipWith benchmarks --- lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs | 25 +++++++++++---------- lol/Crypto/Lol/Cyclotomic/UCyc.hs | 2 ++ 2 files changed, 15 insertions(+), 12 deletions(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs index 300a341e..dcbaeb6a 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs @@ -21,8 +21,7 @@ -- | Wrapper for a C++ implementation of the 'Tensor' interface. -module Crypto.Lol.Cyclotomic.Tensor.CTensor -( CT ) where +module Crypto.Lol.Cyclotomic.Tensor.CTensor (CT) where import Algebra.Additive as Additive (C) import Algebra.Module as Module (C) @@ -158,12 +157,12 @@ toZV v@(ZV _) = v zvToCT' :: forall m r . (Storable r) => IZipVector m r -> CT' m r zvToCT' v = coerce (convert $ unIZipVector v :: Vector r) -wrap :: (Storable r) => (CT' l r -> CT' m r) -> (CT l r -> CT m r) +wrap :: (Storable s, Storable r) => (CT' l s -> CT' m r) -> (CT l s -> CT m r) wrap f (CT v) = CT $ f v wrap f (ZV v) = CT $ f $ zvToCT' v -wrapM :: (Storable r, Monad mon) => (CT' l r -> mon (CT' m r)) - -> (CT l r -> mon (CT m r)) +wrapM :: (Storable s, Storable r, Monad mon) => (CT' l s -> mon (CT' m r)) + -> (CT l s -> mon (CT m r)) wrapM f (CT v) = CT <$> f v wrapM f (ZV v) = CT <$> f (zvToCT' v) @@ -287,11 +286,14 @@ instance Tensor CT where fmapT f (CT v) = CT $ coerce (SV.map f) v fmapT f v@(ZV _) = fmapT f $ toCT v - zipWithT f (CT (CT' v1)) (CT (CT' v2)) = CT $ CT' $ SV.zipWith f v1 v2 - zipWithT f v1 v2 = zipWithT f (toCT v1) (toCT v2) + zipWithT f v1' v2' = + let (CT (CT' v1)) = toCT v1' + (CT (CT' v2)) = toCT v2' + in CT $ CT' $ SV.zipWith f v1 v2 - unzipT (CT (CT' v)) = (CT . CT') *** (CT . CT') $ unzip v - unzipT v = unzipT $ toCT v + unzipT v = + let (CT (CT' x)) = toCT v + in (CT . CT') *** (CT . CT') $ unzip x {-# INLINABLE entailIndexT #-} {-# INLINABLE entailEqT #-} @@ -317,9 +319,8 @@ instance Tensor CT where {-# INLINABLE powBasisPow #-} {-# INLINABLE crtSetDec #-} {-# INLINABLE fmapT #-} - {-# INLINABLE zipWithT #-} - {-# INLINABLE unzipT #-} - + {-# INLINE zipWithT #-} + {-# INLINE unzipT #-} coerceTw :: (Functor mon) => TaggedT '(m, m') mon (Vector r -> Vector r) -> mon (CT' m' r -> CT' m r) coerceTw = (coerce <$>) . untagT diff --git a/lol/Crypto/Lol/Cyclotomic/UCyc.hs b/lol/Crypto/Lol/Cyclotomic/UCyc.hs index 9d9c0db4..a037e14d 100644 --- a/lol/Crypto/Lol/Cyclotomic/UCyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/UCyc.hs @@ -334,6 +334,7 @@ unzipCRTC :: (Fact m, UCRTElt t (a,b), UCRTElt t a, UCRTElt t b) => UCyc t m C (a,b) -> (Either (UCyc t m P a) (UCyc t m C a), Either (UCyc t m P b) (UCyc t m C b)) +{-# INLINABLE unzipCRTC #-} unzipCRTC (CRTC s v) = let (ac,bc) = unzipT v (ap,bp) = Pow *** Pow $ unzipT $ crtInvCS s v @@ -347,6 +348,7 @@ unzipCRTE :: (Fact m, UCRTElt t (a,b), UCRTElt t a, UCRTElt t b) => UCyc t m E (a,b) -> (Either (UCyc t m P a) (UCyc t m E a), Either (UCyc t m P b) (UCyc t m E b)) +{-# INLINABLE unzipCRTE #-} unzipCRTE (CRTE _ v) = let (ae,be) = unzipT v (a',b') = unzipT $ fmapT fromExt $ runIdentity crtInv v From cb189b4efe7814d8485c13c30e1bf3fc016d4369 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 25 Jul 2016 13:11:46 -0400 Subject: [PATCH 03/21] Fixed crt/crtInv. Note that the SPECIALIZE pragmas in UCyc actually *slow down* the UCyc and Cyc benches! --- lol/Crypto/Lol/Cyclotomic/Cyc.hs | 8 ++++---- lol/Crypto/Lol/Cyclotomic/Tensor.hs | 2 +- lol/Crypto/Lol/Cyclotomic/UCyc.hs | 5 ----- 3 files changed, 5 insertions(+), 10 deletions(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Cyc.hs b/lol/Crypto/Lol/Cyclotomic/Cyc.hs index 46349365..9245699c 100644 --- a/lol/Crypto/Lol/Cyclotomic/Cyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/Cyc.hs @@ -561,10 +561,10 @@ instance (Correct gad zq, Fact m, CElt t zq) => Correct gad (Cyc t m zq) where ---------- Change of representation (internal use only) ---------- -toPow', toDec', toCRT' :: (Fact m, CElt t r) => Cyc t m r -> Cyc t m r -{-# INLINE toPow' #-} -{-# INLINE toDec' #-} -{-# INLINE toCRT' #-} +toPow', toDec', toCRT' :: (Fact m, UCRTElt t r, ZeroTestable r) => Cyc t m r -> Cyc t m r +{-# INLINABLE toPow' #-} +{-# INLINABLE toDec' #-} +{-# INLINABLE toCRT' #-} -- | Force to powerful-basis representation (for internal use only). toPow' c@(Pow _) = c diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor.hs index f7310f3f..07c6f667 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor.hs @@ -202,7 +202,7 @@ mulGCRT, divGCRT, crt, crtInv :: {-# INLINABLE mulGCRT #-} {-# INLINABLE divGCRT #-} {-# INLINABLE crt #-} -{-# INLINABLE crtInv #-} +{-# INLINE crtInv #-} -- | Multiply by \(g_m\) in the CRT basis. (This function is simply an -- appropriate entry from 'crtFuncs'.) diff --git a/lol/Crypto/Lol/Cyclotomic/UCyc.hs b/lol/Crypto/Lol/Cyclotomic/UCyc.hs index a037e14d..b0dae2dc 100644 --- a/lol/Crypto/Lol/Cyclotomic/UCyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/UCyc.hs @@ -55,8 +55,6 @@ import Crypto.Lol.Cyclotomic.Tensor hiding (embedCRT, embedDec, embedPow, import Crypto.Lol.CRTrans import Crypto.Lol.Cyclotomic.CRTSentinel import qualified Crypto.Lol.Cyclotomic.Tensor as T -import Crypto.Lol.Cyclotomic.Tensor.CTensor (CT) -import Crypto.Lol.Cyclotomic.Tensor.RepaTensor (RT) import Crypto.Lol.Prelude as LP import Crypto.Lol.Types.FiniteField import Crypto.Lol.Types.ZPP @@ -539,9 +537,6 @@ crtSet = --------- Conversion methods ------------------ --- Used to be a problem in #12068. Now we can write the rules, but do they fire? -{-# SPECIALIZE toPow :: (Fact m, UCRTElt CT r) => UCyc CT m rep r -> UCyc CT m P r #-} -{-# SPECIALIZE toPow :: (Fact m, UCRTElt RT r) => UCyc RT m rep r -> UCyc RT m P r #-} -- | Convert to powerful-basis representation. toPow :: (Fact m, UCRTElt t r) => UCyc t m rep r -> UCyc t m P r {-# INLINABLE toPow #-} From fd149a17540b78806ac855f7ea79fe81c877b394 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 25 Jul 2016 13:26:15 -0400 Subject: [PATCH 04/21] Fixed mulG --- lol/Crypto/Lol/Cyclotomic/UCyc.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lol/Crypto/Lol/Cyclotomic/UCyc.hs b/lol/Crypto/Lol/Cyclotomic/UCyc.hs index b0dae2dc..11f75175 100644 --- a/lol/Crypto/Lol/Cyclotomic/UCyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/UCyc.hs @@ -357,7 +357,7 @@ unzipCRTE (CRTE _ v) -- | Multiply by the special element \(g_m\). mulG :: (Fact m, UCRTElt t r) => UCyc t m rep r -> UCyc t m rep r -{-# INLINABLE mulG #-} +{-# INLINE mulG #-} mulG (Pow v) = Pow $ mulGPow v mulG (Dec v) = Dec $ mulGDec v mulG (CRTC s v) = CRTC s $ mulGCRTCS s v From 60848ba3c250ff390c5837b37bdd5b59387b6446 Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 25 Jul 2016 13:35:44 -0400 Subject: [PATCH 05/21] Fixed lift benchmarks --- lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs index dcbaeb6a..a6a11642 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs @@ -283,8 +283,7 @@ instance Tensor CT where crtSetDec = (CT <$>) <$> coerceBasis crtSetDec' - fmapT f (CT v) = CT $ coerce (SV.map f) v - fmapT f v@(ZV _) = fmapT f $ toCT v + fmapT f = wrap $ coerce (SV.map f) zipWithT f v1' v2' = let (CT (CT' v1)) = toCT v1' From 70f2af6b878db93a808375880e7bad08c6d9ef3c Mon Sep 17 00:00:00 2001 From: Eric Date: Mon, 25 Jul 2016 17:45:22 -0400 Subject: [PATCH 06/21] Fixed twacePow --- lol/Crypto/Lol/Cyclotomic/Tensor.hs | 1 + lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs | 1 + lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs | 3 ++- 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor.hs index 07c6f667..cabc3351 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor.hs @@ -413,6 +413,7 @@ indexInfo = let pps = proxy ppsFact (Proxy::Proxy m) -- the index into the powerful\/decoding basis of \(\O_{m'}\) of the -- \(i\)th entry of the powerful/decoding basis of \(\O_m\). extIndicesPowDec :: (m `Divides` m') => Tagged '(m, m') (U.Vector Int) +{-# INLINABLE extIndicesPowDec #-} extIndicesPowDec = do (_, phi, _, tots) <- indexInfo return $ U.generate phi (fromIndexPair tots . (0,)) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs index a6a11642..eea7e94e 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs @@ -158,6 +158,7 @@ zvToCT' :: forall m r . (Storable r) => IZipVector m r -> CT' m r zvToCT' v = coerce (convert $ unIZipVector v :: Vector r) wrap :: (Storable s, Storable r) => (CT' l s -> CT' m r) -> (CT l s -> CT m r) +{-# INLINABLE wrap #-} wrap f (CT v) = CT $ f v wrap f (ZV v) = CT $ f $ zvToCT' v diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs index 0542be62..3903dba5 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs @@ -46,7 +46,7 @@ backpermute' :: (Vector v a) => U.Vector Int -- ^ @is@ index vector (of length @n@) -> v a -- ^ @xs@ value vector -> v a ---{-# INLINE backpermute' #-} +{-# INLINABLE backpermute' #-} backpermute' is v = generate (G.length is) (\i -> v ! (is ! i)) embedPow', embedDec' :: (Additive r, Vector v r, m `Divides` m') @@ -86,6 +86,7 @@ coeffs' = flip (\x -> V.toList . V.map (`backpermute'` x)) -- @m | m'@. twacePowDec' :: forall m m' r v . (Vector v r, m `Divides` m') => Tagged '(m, m') (v r -> v r) +{-# INLINABLE twacePowDec' #-} twacePowDec' = backpermute' <$> extIndicesPowDec From 4e418398ed427a4101e178226094c47e8e510fe6 Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 26 Jul 2016 09:29:29 -0400 Subject: [PATCH 07/21] Fixed(?) twaceCRT. It's still an order of magnitude slower than twacePow, but an order of magnitude faster than when we started. --- lol/Crypto/Lol/Cyclotomic/Cyc.hs | 2 +- lol/Crypto/Lol/Cyclotomic/Tensor.hs | 2 +- lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs | 2 +- lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs | 1 + lol/Crypto/Lol/Cyclotomic/UCyc.hs | 2 +- lol/benchmarks/Main.hs | 4 ++-- 6 files changed, 7 insertions(+), 6 deletions(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Cyc.hs b/lol/Crypto/Lol/Cyclotomic/Cyc.hs index 9245699c..f314594b 100644 --- a/lol/Crypto/Lol/Cyclotomic/Cyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/Cyc.hs @@ -401,7 +401,7 @@ embed' (Sub (c :: Cyc t k r)) = embed' c -- | The "tweaked trace" (twace) function -- \(\Tw(x) = (\hat{m} / \hat{m}') \cdot \Tr((g' / g) \cdot x)\), -- which fixes \(R\) pointwise (i.e., @twace . embed == id@). -twace :: forall t m m' r . (m `Divides` m', CElt t r) +twace :: forall t m m' r . (m `Divides` m', UCRTElt t r, ZeroTestable r) => Cyc t m' r -> Cyc t m r {-# INLINABLE twace #-} twace (Pow u) = Pow $ U.twacePow u diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor.hs index cabc3351..ba6c0a6b 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor.hs @@ -223,6 +223,7 @@ crtInv = (\(_,_,_,_,f) -> f) <$> crtFuncs -- (This function is simply an appropriate entry from 'crtExtFuncs'.) twaceCRT :: forall t m m' mon r . (CRTrans mon r, Tensor t, m `Divides` m', TElt t r) => mon (t m' r -> t m r) +{-# INLINABLE twaceCRT #-} twaceCRT = proxyT hasCRTFuncs (Proxy::Proxy (t m' r)) *> proxyT hasCRTFuncs (Proxy::Proxy (t m r)) *> (fst <$> crtExtFuncs) @@ -447,7 +448,6 @@ baseIndicesDec :: forall m m' . (m `Divides` m') -- of each pair. baseIndicesCRT :: forall m m' . (m `Divides` m') => Tagged '(m, m') (U.Vector Int) - baseIndicesPow = baseWrapper (toIndexPair . totients) -- this one is more complicated; requires the prime powers diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs index 3cde4148..5534b120 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs @@ -314,7 +314,7 @@ instance Tensor CT where {-# INLINABLE embedDec #-} {-# INLINABLE tGaussianDec #-} {-# INLINABLE gSqNormDec #-} - {-# INLINABLE crtExtFuncs #-} + {-# INLINE crtExtFuncs #-} {-# INLINABLE coeffs #-} {-# INLINABLE powBasisPow #-} {-# INLINABLE crtSetDec #-} diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs index 98911dd7..793260c8 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs @@ -98,6 +98,7 @@ kronToVec v = do twaceCRT' :: forall mon m m' r . (Storable r, CRTrans mon r, m `Divides` m') => TaggedT '(m, m') mon (Vector r -> Vector r) +{-# INLINE twaceCRT' #-} twaceCRT' = tagT $ do g' <- proxyT (kronToVec gCRTK) (Proxy::Proxy m') gInv <- proxyT (kronToVec gInvCRTK) (Proxy::Proxy m) diff --git a/lol/Crypto/Lol/Cyclotomic/UCyc.hs b/lol/Crypto/Lol/Cyclotomic/UCyc.hs index 11f75175..8a1dad86 100644 --- a/lol/Crypto/Lol/Cyclotomic/UCyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/UCyc.hs @@ -473,7 +473,7 @@ twaceDec (Dec v) = Dec $ twacePowDec v -- 'Either' because the subring might not support 'C'.) twaceCRTC :: (m `Divides` m', UCRTElt t r) => UCyc t m' C r -> UCycPC t m r -{-# INLINABLE twaceCRTC #-} +{-# INLINE twaceCRTC #-} twaceCRTC x@(CRTC s' v) = case crtSentinel of -- go to CRTC if valid for target, else go to Pow diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index b988d471..60f71398 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -56,8 +56,8 @@ benches = [ "*g Pow", "*g CRT", "lift", - "error",-} - "twacePow", + "error", + "twacePow",-} "twaceCRT"{-, "embedPow", "embedDec"-} From 2f97d14e7f5f9762d3cc604eda9e6c9148d5a490 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 27 Jul 2016 18:43:12 -0400 Subject: [PATCH 08/21] Added benchmark for gDec --- lol/benchmarks/CycBenches.hs | 5 +++++ lol/benchmarks/Main.hs | 15 ++++++++------- lol/benchmarks/SimpleTensorBenches.hs | 1 + lol/benchmarks/SimpleUCycBenches.hs | 1 + lol/benchmarks/TensorBenches.hs | 5 +++++ lol/benchmarks/UCycBenches.hs | 5 +++++ 6 files changed, 25 insertions(+), 7 deletions(-) diff --git a/lol/benchmarks/CycBenches.hs b/lol/benchmarks/CycBenches.hs index e071aa89..1826ec37 100644 --- a/lol/benchmarks/CycBenches.hs +++ b/lol/benchmarks/CycBenches.hs @@ -28,6 +28,7 @@ cycBenches = benchGroup "Cyc" [ benchGroup "l" $ [hideArgs bench_l testParam], --applyBasic allParams $ hideArgs bench_l, benchGroup "lInv" $ [hideArgs bench_lInv testParam], benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, @@ -83,6 +84,10 @@ bench_liftPow x = let y = advisePow x in bench (liftCyc Pow) y bench_mulgPow :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) bench_mulgPow x = let y = advisePow x in bench mulG y +-- multiply by g when input is in Dec basis +bench_mulgDec :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) +bench_mulgDec x = let y = adviseDec x in bench mulG y + -- multiply by g when input is in CRT basis bench_mulgCRT :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) bench_mulgCRT x = let y = adviseCRT x in bench mulG y diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index 60f71398..f331e6fd 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -41,7 +41,7 @@ colWidth, testNameWidth :: Int colWidth = 15 testNameWidth = 40 verb :: Verb -verb = Abridged +verb = Progress benches :: [String] benches = [ @@ -52,13 +52,14 @@ benches = [ "crt", "crtInv", "l", - "lInv", + "lInv",-} "*g Pow", - "*g CRT", + "*g Dec", + "*g CRT"{-, "lift", "error", - "twacePow",-} - "twaceCRT"{-, + "twacePow", + "twaceCRT", "embedPow", "embedDec"-} @@ -71,10 +72,10 @@ main = do hSetBuffering stdout NoBuffering -- for better printing of progress reports <- mapM (getReports =<<) [ simpleTensorBenches, - tensorBenches, + tensorBenches{-, simpleUCycBenches, ucycBenches, - cycBenches + cycBenches-} ] when (verb == Progress) $ putStrLn "" printTable $ map reverse reports diff --git a/lol/benchmarks/SimpleTensorBenches.hs b/lol/benchmarks/SimpleTensorBenches.hs index 528fef68..b7b2eedc 100644 --- a/lol/benchmarks/SimpleTensorBenches.hs +++ b/lol/benchmarks/SimpleTensorBenches.hs @@ -36,6 +36,7 @@ simpleTensorBenches = do bench "l" $ nf l x2, bench "lInv" $ nf lInv x2, bench "*g Pow" $ nf mulGPow x2, + bench "*g Dec" $ nf mulGDec x2, bench "*g CRT" $ nf (fromJust' "SimpleTensorBenches.gcrt" mulGCRT) x2, bench "lift" $ nf (fmapT lift) x2, bench "error" $ nf (evalRand (fmapT (roundMult one) <$> diff --git a/lol/benchmarks/SimpleUCycBenches.hs b/lol/benchmarks/SimpleUCycBenches.hs index eecde794..85b16943 100644 --- a/lol/benchmarks/SimpleUCycBenches.hs +++ b/lol/benchmarks/SimpleUCycBenches.hs @@ -39,6 +39,7 @@ simpleUCycBenches = do bench "l" $ nf toPow x5, bench "lInv" $ nf toDec x4, bench "*g Pow" $ nf mulG x4, + bench "*g Dec" $ nf mulG x5, bench "*g CRT" $ nf mulG x6, bench "lift" $ nf lift x4, bench "error" $ nf (evalRand (errorRounded (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (UCyc T M D Int64))) gen, diff --git a/lol/benchmarks/TensorBenches.hs b/lol/benchmarks/TensorBenches.hs index 2d672707..bc4fbe9c 100644 --- a/lol/benchmarks/TensorBenches.hs +++ b/lol/benchmarks/TensorBenches.hs @@ -31,6 +31,7 @@ tensorBenches = benchGroup "Tensor" [ benchGroup "l" $ [hideArgs bench_l testParam], --applyBasic allParams $ hideArgs bench_l, benchGroup "lInv" $ [hideArgs bench_lInv testParam], benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, @@ -71,6 +72,10 @@ bench_liftPow = bench (fmapT lift) bench_mulgPow :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) bench_mulgPow = bench mulGPow +-- multiply by g when input is in Dec basis +bench_mulgDec :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_mulgDec = bench mulGDec + -- multiply by g when input is in CRT basis bench_mulgCRT :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) bench_mulgCRT = bench (fromJust' "TensorBenches.bench_mulgCRT" mulGCRT) diff --git a/lol/benchmarks/UCycBenches.hs b/lol/benchmarks/UCycBenches.hs index b70b5336..56e70e92 100644 --- a/lol/benchmarks/UCycBenches.hs +++ b/lol/benchmarks/UCycBenches.hs @@ -28,6 +28,7 @@ ucycBenches = benchGroup "UCyc" [ benchGroup "l" $ [hideArgs bench_l testParam], -- applyBasic allParams $ hideArgs bench_l, benchGroup "lInv" $ [hideArgs bench_lInv testParam], benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], -- applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], -- applyBasic allParams $ hideArgs bench_mulgCRT, benchGroup "lift" $ [hideArgs bench_liftPow testParam], -- applyLift liftParams $ hideArgs bench_liftPow, benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], -- applyError errorParams $ hideArgs $ bench_errRounded 0.1 @@ -80,6 +81,10 @@ bench_liftPow = bench lift bench_mulgPow :: (BasicCtx t m r) => UCyc t m P r -> Bench '(t,m,r) bench_mulgPow = bench mulG +-- multiply by g when input is in Dec basis +bench_mulgDec :: (BasicCtx t m r) => UCyc t m D r -> Bench '(t,m,r) +bench_mulgDec = bench mulG + -- multiply by g when input is in CRT basis bench_mulgCRT :: (BasicCtx t m r) => UCycPC t m r -> Bench '(t,m,r) bench_mulgCRT (Right a) = bench mulG a From 6c2a0c6629fbaa18d590b3999499e189a4fbb3dc Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 28 Jul 2016 09:33:00 -0400 Subject: [PATCH 09/21] Fixed embedPow/Dec, except for Cyc --- lol/Crypto/Lol/Cyclotomic/Tensor.hs | 3 ++- lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor.hs index ba6c0a6b..3fbe21cd 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor.hs @@ -440,10 +440,11 @@ baseWrapper f = do -- | A lookup table for 'toIndexPair' applied to indices \([\varphi(m')]\). baseIndicesPow :: forall m m' . (m `Divides` m') => Tagged '(m, m') (U.Vector (Int,Int)) +{-# INLINABLE baseIndicesPow #-} -- | A lookup table for 'baseIndexDec' applied to indices \([\varphi(m')]\). baseIndicesDec :: forall m m' . (m `Divides` m') => Tagged '(m, m') (U.Vector (Maybe (Int,Bool))) - +{-# INLINABLE baseIndicesDec #-} -- | Same as 'baseIndicesPow', but only includes the second component -- of each pair. baseIndicesCRT :: forall m m' . (m `Divides` m') diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs index 793260c8..69e1fc05 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Extension.hs @@ -50,6 +50,8 @@ backpermute' is v = generate (U.length is) (\i -> v ! (is U.! i)) embedPow', embedDec' :: (Additive r, Storable r, m `Divides` m') => Tagged '(m, m') (Vector r -> Vector r) +{-# INLINABLE embedPow' #-} +{-# INLINABLE embedDec' #-} -- | Embeds an vector in the powerful basis of the the mth cyclotomic ring -- to an vector in the powerful basis of the m'th cyclotomic ring when @m | m'@ embedPow' = (\indices arr -> generate (U.length indices) $ \idx -> From 886ca53ff7abb2b3e498d8f30c540504d71f1fe9 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 28 Jul 2016 11:12:23 -0400 Subject: [PATCH 10/21] Added divG* and embedCRT benchmarks --- lol/benchmarks/CycBenches.hs | 27 +++++++++++++++++++++++--- lol/benchmarks/Main.hs | 22 ++++++++++++--------- lol/benchmarks/SimpleTensorBenches.hs | 10 ++++++++-- lol/benchmarks/SimpleUCycBenches.hs | 9 ++++++++- lol/benchmarks/TensorBenches.hs | 26 ++++++++++++++++++++++++- lol/benchmarks/UCycBenches.hs | 28 +++++++++++++++++++++++++-- 6 files changed, 104 insertions(+), 18 deletions(-) diff --git a/lol/benchmarks/CycBenches.hs b/lol/benchmarks/CycBenches.hs index 1826ec37..604962a4 100644 --- a/lol/benchmarks/CycBenches.hs +++ b/lol/benchmarks/CycBenches.hs @@ -16,7 +16,6 @@ import Crypto.Lol import Crypto.Lol.Types import Crypto.Random.DRBG - cycBenches :: IO Benchmark cycBenches = benchGroup "Cyc" [ benchGroup "unzipPow" $ [hideArgs bench_unzipCycPow testParam], -- applyUnzip allParams $ hideArgs bench_unzipCycPow, @@ -30,12 +29,16 @@ cycBenches = benchGroup "Cyc" [ benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, + benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "divg Dec" $ [hideArgs bench_divgDec testParam], + benchGroup "divg CRT" $ [hideArgs bench_divgCRT testParam], benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_embedPow - benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam] + benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam], + benchGroup "embedCRT" $ [hideArgs bench_embedCRT twoIdxParam] ] bench_unzipCycPow :: (UnzipCtx t m r) => Cyc t m (r,r) -> Bench '(t,m,r) @@ -92,6 +95,18 @@ bench_mulgDec x = let y = adviseDec x in bench mulG y bench_mulgCRT :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) bench_mulgCRT x = let y = adviseCRT x in bench mulG y +-- divide by g when input is in Pow basis +bench_divgPow :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) +bench_divgPow x = let y = advisePow $ mulG x in bench divG y + +-- divide by g when input is in Dec basis +bench_divgDec :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) +bench_divgDec x = let y = adviseDec $ mulG x in bench divG y + +-- divide by g when input is in CRT basis +bench_divgCRT :: (BasicCtx t m r) => Cyc t m r -> Bench '(t,m,r) +bench_divgCRT x = let y = adviseCRT x in bench divG y + -- generate a rounded error term bench_errRounded :: forall t m r gen . (ErrorCtx t m r gen) => Double -> Bench '(t,m,r,gen) @@ -121,4 +136,10 @@ bench_embedDec :: forall t m m' r . (TwoIdxCtx t m m' r) => Cyc t m r -> Bench '(t,m,m',r) bench_embedDec x = let y = adviseDec x - in bench (adviseDec . embed :: Cyc t m r -> Cyc t m' r) y \ No newline at end of file + in bench (adviseDec . embed :: Cyc t m r -> Cyc t m' r) y + +bench_embedCRT :: forall t m m' r . (TwoIdxCtx t m m' r) + => Cyc t m r -> Bench '(t,m,m',r) +bench_embedCRT x = + let y = adviseCRT x + in bench (adviseCRT . embed :: Cyc t m r -> Cyc t m' r) y diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index f331e6fd..a97f5f07 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -53,15 +53,19 @@ benches = [ "crtInv", "l", "lInv",-} - "*g Pow", + {-"*g Pow", "*g Dec", - "*g CRT"{-, + "*g CRT", + "divg Pow", + "divg Dec", + "divg CRT",-}{- "lift", "error", "twacePow", - "twaceCRT", - "embedPow", - "embedDec"-} + "twaceCRT",-} + --"embedPow", + "embedDec"--, + --"embedCRT" ] @@ -72,10 +76,10 @@ main = do hSetBuffering stdout NoBuffering -- for better printing of progress reports <- mapM (getReports =<<) [ simpleTensorBenches, - tensorBenches{-, - simpleUCycBenches, - ucycBenches, - cycBenches-} + tensorBenches, + simpleUCycBenches--, + --ucycBenches, + --cycBenches ] when (verb == Progress) $ putStrLn "" printTable $ map reverse reports diff --git a/lol/benchmarks/SimpleTensorBenches.hs b/lol/benchmarks/SimpleTensorBenches.hs index b7b2eedc..5220f66b 100644 --- a/lol/benchmarks/SimpleTensorBenches.hs +++ b/lol/benchmarks/SimpleTensorBenches.hs @@ -25,6 +25,8 @@ simpleTensorBenches = do x2 :: T M R <- getRandom x3 :: T M R <- getRandom x4 :: T M' R <- getRandom + let x2' = mulGPow x2 + x2'' = mulGDec x2 gen <- newGenIO return $ bgroup "STensor" [ bench "unzipPow" $ nf unzipT x1, @@ -37,12 +39,16 @@ simpleTensorBenches = do bench "lInv" $ nf lInv x2, bench "*g Pow" $ nf mulGPow x2, bench "*g Dec" $ nf mulGDec x2, - bench "*g CRT" $ nf (fromJust' "SimpleTensorBenches.gcrt" mulGCRT) x2, + bench "*g CRT" $ nf (fromJust' "SimpleTensorBenches.*gcrt" mulGCRT) x2, + bench "divg Pow" $ nf divGPow x2', + bench "divg Dec" $ nf divGDec x2'', + bench "divg CRT" $ nf (fromJust' "SimpleTensorBenches./gcrt" divGCRT) x2, bench "lift" $ nf (fmapT lift) x2, bench "error" $ nf (evalRand (fmapT (roundMult one) <$> (tGaussianDec (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (T M Double))) :: CryptoRand HashDRBG -> T M Int64) gen, bench "twacePow" $ nf (twacePowDec :: T M R -> T M' R) x2, bench "twaceCRT" $ nf (fromJust' "SimpleTensorBenches.twaceCRT" twaceCRT :: T M R -> T M' R) x2, bench "embedPow" $ nf (embedPow :: T M' R -> T M R) x4, - bench "embedDec" $ nf (embedDec :: T M' R -> T M R) x4 + bench "embedDec" $ nf (embedDec :: T M' R -> T M R) x4, + bench "embedCRT" $ nf (fromJust' "SimpleTensorBenches.embedCRT" embedCRT :: T M' R -> T M R) x4 ] \ No newline at end of file diff --git a/lol/benchmarks/SimpleUCycBenches.hs b/lol/benchmarks/SimpleUCycBenches.hs index 85b16943..d5289fc7 100644 --- a/lol/benchmarks/SimpleUCycBenches.hs +++ b/lol/benchmarks/SimpleUCycBenches.hs @@ -28,6 +28,9 @@ simpleUCycBenches = do (Right x6) :: UCycPC T M R <- getRandom x7 :: UCyc T M' P R <- getRandom let x8 = toDec x7 + x4' = mulG x4 + x5' = mulG x5 + (Right x9) :: UCycPC T M' R <- getRandom gen <- newGenIO return $ bgroup "SUCyc" [ bench "unzipPow" $ nf unzipPow x1, @@ -41,12 +44,16 @@ simpleUCycBenches = do bench "*g Pow" $ nf mulG x4, bench "*g Dec" $ nf mulG x5, bench "*g CRT" $ nf mulG x6, + bench "divg Pow" $ nf divG x4', + bench "divg Dec" $ nf divG x5', + bench "divg CRT" $ nf divG x6, bench "lift" $ nf lift x4, bench "error" $ nf (evalRand (errorRounded (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (UCyc T M D Int64))) gen, bench "twacePow" $ nf (twacePow :: UCyc T M P R -> UCyc T M' P R) x4, bench "twaceCRT" $ nf (twaceCRTC :: UCyc T M C R -> UCycPC T M' R) x6, bench "embedPow" $ nf (embedPow :: UCyc T M' P R -> UCyc T M P R) x7, - bench "embedDec" $ nf (embedDec :: UCyc T M' D R -> UCyc T M D R) x8 + bench "embedDec" $ nf (embedDec :: UCyc T M' D R -> UCyc T M D R) x8, + bench "embedCRT" $ nf (embedCRTC :: UCyc T M' C R -> UCycPC T M R) x9 ] pcToEC :: UCycPC t m r -> UCycEC t m r diff --git a/lol/benchmarks/TensorBenches.hs b/lol/benchmarks/TensorBenches.hs index bc4fbe9c..7703241d 100644 --- a/lol/benchmarks/TensorBenches.hs +++ b/lol/benchmarks/TensorBenches.hs @@ -33,12 +33,16 @@ tensorBenches = benchGroup "Tensor" [ benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, + benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "divg Dec" $ [hideArgs bench_divgDec testParam], + benchGroup "divg CRT" $ [hideArgs bench_divgCRT testParam], benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_embedPow-} - benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam] + benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam], + benchGroup "embedCRT" $ [hideArgs bench_embedCRT twoIdxParam] ] bench_unzip :: (UnzipCtx t m r) => t m (r,r) -> Bench '(t,m,r) @@ -80,6 +84,22 @@ bench_mulgDec = bench mulGDec bench_mulgCRT :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) bench_mulgCRT = bench (fromJust' "TensorBenches.bench_mulgCRT" mulGCRT) +-- divide by g when input is in Pow basis +bench_divgPow :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_divgPow x = + let y = mulGPow x + in bench divGPow y + +-- divide by g when input is in Dec basis +bench_divgDec :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_divgDec x = + let y = mulGDec x + in bench divGDec y + +-- divide by g when input is in CRT basis +bench_divgCRT :: (BasicCtx t m r) => t m r -> Bench '(t,m,r) +bench_divgCRT = bench (fromJust' "TensorBenches.bench_divgCRT" divGCRT) + -- generate a rounded error term bench_errRounded :: forall t m r gen . (ErrorCtx t m r gen) => Double -> Bench '(t,m,r,gen) @@ -104,3 +124,7 @@ bench_embedPow = bench (embedPow :: t m r -> t m' r) bench_embedDec :: forall t m m' r . (TwoIdxCtx t m m' r) => t m r -> Bench '(t,m,m',r) bench_embedDec = bench (embedDec :: t m r -> t m' r) + +bench_embedCRT :: forall t m m' r . (TwoIdxCtx t m m' r) + => t m r -> Bench '(t,m,m',r) +bench_embedCRT = bench (fromJust' "TensorBenches.bench_embedCRT" embedCRT :: t m r -> t m' r) diff --git a/lol/benchmarks/UCycBenches.hs b/lol/benchmarks/UCycBenches.hs index 56e70e92..1766fab2 100644 --- a/lol/benchmarks/UCycBenches.hs +++ b/lol/benchmarks/UCycBenches.hs @@ -30,12 +30,16 @@ ucycBenches = benchGroup "UCyc" [ benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], -- applyBasic allParams $ hideArgs bench_mulgPow, benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], -- applyBasic allParams $ hideArgs bench_mulgCRT, + benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], -- applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "divg Dec" $ [hideArgs bench_divgDec testParam], + benchGroup "divg CRT" $ [hideArgs bench_divgCRT testParam], benchGroup "lift" $ [hideArgs bench_liftPow testParam], -- applyLift liftParams $ hideArgs bench_liftPow, benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], -- applyError errorParams $ hideArgs $ bench_errRounded 0.1 benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], -- applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], -- applyTwoIdx twoIdxParams $ hideArgs bench_embedPow - benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam] + benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam], + benchGroup "embedCRT" $ [hideArgs bench_embedCRT twoIdxParam] ] bench_unzipUCycPow :: (UnzipCtx t m r) => UCyc t m P (r,r) -> Bench '(t,m,r) @@ -89,6 +93,22 @@ bench_mulgDec = bench mulG bench_mulgCRT :: (BasicCtx t m r) => UCycPC t m r -> Bench '(t,m,r) bench_mulgCRT (Right a) = bench mulG a +-- divide by g when input is in Pow basis +bench_divgPow :: (BasicCtx t m r) => UCyc t m P r -> Bench '(t,m,r) +bench_divgPow x = + let y = mulG x + in bench divG y + +-- divide by g when input is in Dec basis +bench_divgDec :: (BasicCtx t m r) => UCyc t m D r -> Bench '(t,m,r) +bench_divgDec x = + let y = mulG x + in bench divG y + +-- divide by g when input is in CRT basis +bench_divgCRT :: (BasicCtx t m r) => UCycPC t m r -> Bench '(t,m,r) +bench_divgCRT (Right a) = bench divG a + -- generate a rounded error term bench_errRounded :: forall t m r gen . (ErrorCtx t m r gen) => Double -> Bench '(t,m,r,gen) @@ -110,4 +130,8 @@ bench_embedPow = bench (embedPow :: UCyc t m P r -> UCyc t m' P r) bench_embedDec :: forall t m m' r . (TwoIdxCtx t m m' r) => UCyc t m D r -> Bench '(t,m,m',r) -bench_embedDec = bench (embedDec :: UCyc t m D r -> UCyc t m' D r) \ No newline at end of file +bench_embedDec = bench (embedDec :: UCyc t m D r -> UCyc t m' D r) + +bench_embedCRT :: forall t m m' r . (TwoIdxCtx t m m' r) + => UCycPC t m r -> Bench '(t,m,m',r) +bench_embedCRT (Right a) = bench (embedCRTC :: UCyc t m C r -> UCycPC t m' r) a From 63cbda1ad6540f506f87a6f39b5ee04b87a84b23 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 28 Jul 2016 14:54:55 -0400 Subject: [PATCH 11/21] Reworked CTensor divG. Now all division and checks happen in C-land, and we properly handle Maybe when divisibility fails (Nothing rather than runtime crash, even in the case of Zq, which Repa crashes on.) --- lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs | 54 +++++---- .../Lol/Cyclotomic/Tensor/CTensor/Backend.hs | 16 +-- .../Lol/Cyclotomic/Tensor/CTensor/common.h | 9 -- .../Lol/Cyclotomic/Tensor/CTensor/g.cpp | 103 ++++++++++++++++-- .../Lol/Cyclotomic/Tensor/CTensor/types.h | 21 ++-- .../Lol/Cyclotomic/Tensor/CTensor/zq.cpp | 5 +- 6 files changed, 141 insertions(+), 67 deletions(-) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs index 5534b120..38ff9b10 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor.hs @@ -43,7 +43,7 @@ import Data.Traversable as T import Data.Vector.Generic as V (fromList, toList, unzip) import Data.Vector.Storable as SV (Vector, convert, foldl', fromList, generate, - length, map, mapM, replicate, + length, map, replicate, replicateM, thaw, thaw, toList, unsafeFreeze, unsafeWith, zipWith, (!)) @@ -164,6 +164,7 @@ wrap f (ZV v) = CT $ f $ zvToCT' v wrapM :: (Storable s, Storable r, Monad mon) => (CT' l s -> mon (CT' m r)) -> (CT l s -> mon (CT m r)) +{-# INLINABLE wrapM #-} wrapM f (CT v) = CT <$> f v wrapM f (ZV v) = CT <$> f (zvToCT' v) @@ -247,15 +248,14 @@ instance Tensor CT where scalarPow = CT . scalarPow' -- Vector code - l = wrap $ untag $ basicDispatch dl - lInv = wrap $ untag $ basicDispatch dlinv + l = wrap $ basicDispatch dl + lInv = wrap $ basicDispatch dlinv - mulGPow = wrap mulGPow' - mulGDec = wrap $ untag $ basicDispatch dmulgdec + mulGPow = wrap $ basicDispatch dmulgpow + mulGDec = wrap $ basicDispatch dmulgdec - divGPow = wrapM divGPow' - -- we divide by p in the C code (for divGDec only(?)), do NOT call checkDiv! - divGDec = wrapM $ Just . untag (basicDispatch dginvdec) + divGPow = wrapM $ dispatchGInv dginvpow + divGDec = wrapM $ dispatchGInv dginvdec crtFuncs = (,,,,) <$> return (CT . repl) <*> @@ -338,12 +338,21 @@ coerceCoeffs = coerce coerceBasis :: Tagged '(m,m') [Vector r] -> Tagged m [CT' m' r] coerceBasis = coerce -mulGPow' :: (TElt CT r, Fact m) => CT' m r -> CT' m r -mulGPow' = untag $ basicDispatch dmulgpow - -divGPow' :: (TElt CT r, Fact m, IntegralDomain r, ZeroTestable r) - => CT' m r -> Maybe (CT' m r) -divGPow' = untag $ checkDiv $ basicDispatch dginvpow +dispatchGInv :: forall m r . (Storable r, Fact m) + => (Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO Int16) + -> CT' m r -> Maybe (CT' m r) +dispatchGInv f = + let factors = proxy (marshalFactors <$> ppsFact) (Proxy::Proxy m) + totm = proxy (fromIntegral <$> totientFact) (Proxy::Proxy m) + numFacts = fromIntegral $ SV.length factors + in \(CT' x) -> unsafePerformIO $ do + yout <- SV.thaw x + ret <- SM.unsafeWith yout (\pout -> + SV.unsafeWith factors (\pfac -> + f pout totm pfac numFacts)) + if ret /= 0 + then Just . CT' <$> unsafeFreeze yout + else return Nothing withBasicArgs :: forall m r . (Fact m, Storable r) => (Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO ()) @@ -361,8 +370,8 @@ withBasicArgs f = basicDispatch :: (Storable r, Fact m) => (Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO ()) - -> Tagged m (CT' m r -> CT' m r) -basicDispatch f = return $ unsafePerformIO . withBasicArgs f + -> CT' m r -> CT' m r +basicDispatch f = unsafePerformIO . withBasicArgs f gSqNormDec' :: (Storable r, Fact m, Dispatch r) => Tagged m (CT' m r -> r) @@ -384,19 +393,6 @@ ctCRTInv = do return $ \x -> unsafePerformIO $ withPtrArray ruinv' (\ruptr -> with mhatInv (flip withBasicArgs x . dcrtinv ruptr)) -checkDiv :: (Storable r, IntegralDomain r, ZeroTestable r, Fact m) - => Tagged m (CT' m r -> CT' m r) -> Tagged m (CT' m r -> Maybe (CT' m r)) -checkDiv f = do - f' <- f - oddRad' <- fromIntegral <$> oddRadicalFact - return $ \x -> - let (CT' y) = f' x - in CT' <$> SV.mapM (`divIfDivis` oddRad') y - -divIfDivis :: (IntegralDomain r, ZeroTestable r) => r -> r -> Maybe r -divIfDivis num den = let (q,r) = num `divMod` den - in if isZero r then Just q else Nothing - cZipDispatch :: (Storable r, Fact m) => (Ptr r -> Ptr r -> Int64 -> IO ()) -> Tagged m (CT' m r -> CT' m r -> CT' m r) diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Backend.hs b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Backend.hs index 9e3e5dfa..8e68e647 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Backend.hs +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/Backend.hs @@ -166,9 +166,9 @@ class (repr ~ CTypeOf r) => Dispatch' repr r where -- | Equivalent to 'Tensor's @mulGDec@. dmulgdec :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO () -- | Equivalent to 'Tensor's @divGPow@. - dginvpow :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO () + dginvpow :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO Int16 -- | Equivalent to 'Tensor's @divGDec@. - dginvdec :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO () + dginvdec :: Ptr r -> Int64 -> Ptr CPP -> Int16 -> IO Int16 -- | Equivalent to @zipWith (*)@ dmul :: Ptr r -> Ptr r -> Int64 -> IO () @@ -315,12 +315,12 @@ foreign import ccall unsafe "tensorGPowC" tensorGPowC :: Int16 -> Ptr (C foreign import ccall unsafe "tensorGDecR" tensorGDecR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO () foreign import ccall unsafe "tensorGDecRq" tensorGDecRq :: Int16 -> Ptr (ZqBasic q Int64) -> Int64 -> Ptr CPP -> Int16 -> Ptr Int64 -> IO () foreign import ccall unsafe "tensorGDecC" tensorGDecC :: Int16 -> Ptr (Complex Double) -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorGInvPowR" tensorGInvPowR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorGInvPowRq" tensorGInvPowRq :: Int16 -> Ptr (ZqBasic q Int64) -> Int64 -> Ptr CPP -> Int16 -> Ptr Int64 -> IO () -foreign import ccall unsafe "tensorGInvPowC" tensorGInvPowC :: Int16 -> Ptr (Complex Double) -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorGInvDecR" tensorGInvDecR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO () -foreign import ccall unsafe "tensorGInvDecRq" tensorGInvDecRq :: Int16 -> Ptr (ZqBasic q Int64) -> Int64 -> Ptr CPP -> Int16 -> Ptr Int64 -> IO () -foreign import ccall unsafe "tensorGInvDecC" tensorGInvDecC :: Int16 -> Ptr (Complex Double) -> Int64 -> Ptr CPP -> Int16 -> IO () +foreign import ccall unsafe "tensorGInvPowR" tensorGInvPowR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO Int16 +foreign import ccall unsafe "tensorGInvPowRq" tensorGInvPowRq :: Int16 -> Ptr (ZqBasic q Int64) -> Int64 -> Ptr CPP -> Int16 -> Ptr Int64 -> IO Int16 +foreign import ccall unsafe "tensorGInvPowC" tensorGInvPowC :: Int16 -> Ptr (Complex Double) -> Int64 -> Ptr CPP -> Int16 -> IO Int16 +foreign import ccall unsafe "tensorGInvDecR" tensorGInvDecR :: Int16 -> Ptr Int64 -> Int64 -> Ptr CPP -> Int16 -> IO Int16 +foreign import ccall unsafe "tensorGInvDecRq" tensorGInvDecRq :: Int16 -> Ptr (ZqBasic q Int64) -> Int64 -> Ptr CPP -> Int16 -> Ptr Int64 -> IO Int16 +foreign import ccall unsafe "tensorGInvDecC" tensorGInvDecC :: Int16 -> Ptr (Complex Double) -> Int64 -> Ptr CPP -> Int16 -> IO Int16 foreign import ccall unsafe "tensorCRTRq" tensorCRTRq :: Int16 -> Ptr (ZqBasic q Int64) -> Int64 -> Ptr CPP -> Int16 -> Ptr (Ptr (ZqBasic q Int64)) -> Ptr Int64 -> IO () foreign import ccall unsafe "tensorCRTC" tensorCRTC :: Int16 -> Ptr (Complex Double) -> Int64 -> Ptr CPP -> Int16 -> Ptr (Ptr (Complex Double)) -> IO () diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/common.h b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/common.h index ad01b7f5..367f43b0 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/common.h +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/common.h @@ -1,17 +1,8 @@ #ifndef COMMON_H_ #define COMMON_H_ -#include -#include #include "types.h" -#define ASSERT(EXP) { \ - if (!(EXP)) { \ - fprintf (stderr, "Assertion in file '%s' line %d : " #EXP " is false\n", __FILE__, __LINE__); \ - exit(-1); \ - } \ -} - // calculates base ** exp hDim_t ipow(hDim_t base, hShort_t exp); diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/g.cpp b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/g.cpp index 3bdebb17..a7f79835 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/g.cpp +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/g.cpp @@ -99,12 +99,11 @@ template void gInvDec (ring* y, hShort_t tupSize, hDim_t lts, hD } ring rp; rp = p; - ring acc = lastOut / rp; - ASSERT ((acc * rp) == lastOut); // this line asserts that lastOut % p == 0, without calling % operator + ring acc = lastOut; for (i = p-2; i > 0; --i) { hDim_t idx = tensorOffset + i*rts; ring tmp = acc; - acc -= y[idx*tupSize]; // we already divided acc by p, do not multiply y[idx] by p + acc -= y[idx*tupSize]*rp; y[idx*tupSize] = tmp; } y[tensorOffset*tupSize] = acc; @@ -144,34 +143,120 @@ extern "C" void tensorGDecC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExp tensorFuserPrime (y, tupSize, gDec, totm, peArr, sizeOfPE, (hInt_t*)0); } -extern "C" void tensorGInvPowR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) +hInt_t oddRad(PrimeExponent* peArr, hShort_t sizeOfPE) { + hInt_t oddrad; + oddrad = 1; + for(int i = 0; i < sizeOfPE; i++) { + hShort_t p = peArr[i].prime; + if (p != 2) { + oddrad *= peArr[i].prime; + } + } + return oddrad; +} + +extern "C" hShort_t tensorGInvPowR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) { tensorFuserPrime (y, tupSize, gInvPow, totm, peArr, sizeOfPE, (hInt_t*)0); + + hInt_t oddrad = oddRad(peArr, sizeOfPE); + + for(int i = 0; i < tupSize*totm; i++) { + if (y[i] % oddrad) { + y[i] /= oddrad; + } + else { + return 0; + } + } + return 1; } -extern "C" void tensorGInvPowRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) +extern "C" hShort_t tensorGInvPowRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) { tensorFuserPrime (y, tupSize, gInvPow, totm, peArr, sizeOfPE, qs); + + hInt_t oddrad = oddRad(peArr, sizeOfPE); + + for(int i = 0; i < tupSize; i++) { + Zq::q = qs[i]; // global update + hInt_t ori = reciprocal(Zq::q, oddrad); + Zq oddradInv; + oddradInv = ori; + if (ori == 0) { + return 0; // error condition + } + for(hDim_t j = 0; j < totm; j++) { + y[j*tupSize+i] *= oddradInv; + } + } + canonicalizeZq(y,tupSize,totm,qs); + return 1; } -extern "C" void tensorGInvPowC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) +extern "C" hShort_t tensorGInvPowC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) { tensorFuserPrime (y, tupSize, gInvPow, totm, peArr, sizeOfPE, (hInt_t*)0); + + hInt_t oddrad = oddRad(peArr, sizeOfPE); + Complex oddradInv; + oddradInv = 1 / oddrad; + for(int i = 0; i < tupSize*totm; i++) { + y[i] *= oddradInv; + } + return 1; } -extern "C" void tensorGInvDecR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) +extern "C" hShort_t tensorGInvDecR (hShort_t tupSize, hInt_t* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) { tensorFuserPrime (y, tupSize, gInvDec, totm, peArr, sizeOfPE, (hInt_t*)0); + + hInt_t oddrad = oddRad(peArr, sizeOfPE); + + for(int i = 0; i < tupSize*totm; i++) { + if (y[i] % oddrad) { + y[i] /= oddrad; + } + else { + return 0; + } + } + return 1; } -extern "C" void tensorGInvDecRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) +extern "C" hShort_t tensorGInvDecRq (hShort_t tupSize, Zq* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE, hInt_t* qs) { tensorFuserPrime (y, tupSize, gInvDec, totm, peArr, sizeOfPE, qs); + + hInt_t oddrad = oddRad(peArr, sizeOfPE); + + for(int i = 0; i < tupSize; i++) { + Zq::q = qs[i]; // global update + hInt_t ori = reciprocal(Zq::q, oddrad); + Zq oddradInv; + oddradInv = ori; + if (ori == 0) { + return 0; // error condition + } + for(hDim_t j = 0; j < totm; j++) { + y[j*tupSize+i] *= oddradInv; + } + } + canonicalizeZq(y,tupSize,totm,qs); + return 1; } -extern "C" void tensorGInvDecC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) +extern "C" hShort_t tensorGInvDecC (hShort_t tupSize, Complex* y, hDim_t totm, PrimeExponent* peArr, hShort_t sizeOfPE) { tensorFuserPrime (y, tupSize, gInvDec, totm, peArr, sizeOfPE, (hInt_t*)0); + + hInt_t oddrad = oddRad(peArr, sizeOfPE); + Complex oddradInv; + oddradInv = 1 / oddrad; + for(int i = 0; i < tupSize*totm; i++) { + y[i] *= oddradInv; + } + return 1; } \ No newline at end of file diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/types.h b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/types.h index 6b25a47a..6b7b3c39 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/types.h +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/types.h @@ -3,6 +3,8 @@ #define TENSORTYPES_H_ #include +#include +#include typedef int64_t hInt_t ; typedef int32_t hDim_t ; @@ -18,6 +20,13 @@ typedef struct hInt_t reciprocal (hInt_t a, hInt_t b); +#define ASSERT(EXP) { \ + if (!(EXP)) { \ + fprintf (stderr, "Assertion in file '%s' line %d : " #EXP " is false\n", __FILE__, __LINE__); \ + exit(-1); \ + } \ +} + //http://stackoverflow.com/questions/37572628 #ifdef __cplusplus //http://stackoverflow.com/a/4421719 @@ -55,14 +64,11 @@ class Zq { Zq binv; binv = reciprocal(q,b.x); + ASSERT (binv.x); // binv == 0 indicates that x is not invertible mod q *this *= binv; return *this; } }; -inline char operator==(Zq a, const Zq& b) -{ - return (a.x == b.x); -} inline Zq operator+(Zq a, const Zq& b) { a += b; @@ -129,13 +135,6 @@ class Complex return *this; } }; -inline char operator==(Complex a, const Complex& b) -{ - // This is only used in divGDec, where we do a divisiblity check. - // The divisibility check should always succeed for Complex since \C is a field, - // however if we actually implement equality, it would fail due to roundoff. - return 1; -} inline Complex operator+(Complex a, const Complex& b) { a += b; diff --git a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/zq.cpp b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/zq.cpp index a2f0d840..5b39a31c 100644 --- a/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/zq.cpp +++ b/lol/Crypto/Lol/Cyclotomic/Tensor/CTensor/zq.cpp @@ -17,7 +17,10 @@ hInt_t reciprocal (hInt_t a, hInt_t b) y = lasty - quotient*y; lasty = tmp; } - ASSERT (a==1); // if this one fails, then b is not invertible mod a + // if a!=1, then b is not invertible mod a + if(a!=1) { + return 0; + } // this actually returns EITHER the reciprocal OR reciprocal + fieldSize hInt_t res = lasty + fieldSize; From 41c0fe88023c5879af04e0b46b79833965cd4b60 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 28 Jul 2016 16:58:32 -0400 Subject: [PATCH 12/21] Added benchmark for twaceDec --- lol/benchmarks/CycBenches.hs | 33 ++++++++++++++++----------- lol/benchmarks/Main.hs | 23 ++++++++++--------- lol/benchmarks/SimpleTensorBenches.hs | 1 + lol/benchmarks/SimpleUCycBenches.hs | 1 + lol/benchmarks/TensorBenches.hs | 25 ++++++++++---------- lol/benchmarks/UCycBenches.hs | 31 ++++++++++++++----------- 6 files changed, 65 insertions(+), 49 deletions(-) diff --git a/lol/benchmarks/CycBenches.hs b/lol/benchmarks/CycBenches.hs index 604962a4..24dbd475 100644 --- a/lol/benchmarks/CycBenches.hs +++ b/lol/benchmarks/CycBenches.hs @@ -18,25 +18,26 @@ import Crypto.Random.DRBG cycBenches :: IO Benchmark cycBenches = benchGroup "Cyc" [ - benchGroup "unzipPow" $ [hideArgs bench_unzipCycPow testParam], -- applyUnzip allParams $ hideArgs bench_unzipCycPow, + benchGroup "unzipPow" $ [hideArgs bench_unzipCycPow testParam], benchGroup "unzipDec" $ [hideArgs bench_unzipCycDec testParam], - benchGroup "unzipCRT" $ [hideArgs bench_unzipCycCRT testParam], --applyUnzip allParams $ hideArgs bench_unzipCycCRT, - benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], -- applyBasic allParams $ hideArgs bench_mul, - benchGroup "crt" $ [hideArgs bench_crt testParam], --applyBasic allParams $ hideArgs bench_crt, - benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], --applyBasic allParams $ hideArgs bench_crtInv, - benchGroup "l" $ [hideArgs bench_l testParam], --applyBasic allParams $ hideArgs bench_l, + benchGroup "unzipCRT" $ [hideArgs bench_unzipCycCRT testParam], + benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], + benchGroup "crt" $ [hideArgs bench_crt testParam], + benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], + benchGroup "l" $ [hideArgs bench_l testParam], benchGroup "lInv" $ [hideArgs bench_lInv testParam], - benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], - benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, - benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], + benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], benchGroup "divg Dec" $ [hideArgs bench_divgDec testParam], benchGroup "divg CRT" $ [hideArgs bench_divgCRT testParam], - benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, - benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, - benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, + benchGroup "lift" $ [hideArgs bench_liftPow testParam], + benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], + benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], + benchGroup "twaceDec" $ [hideArgs bench_twaceDec twoIdxParam], benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], - benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_embedPow + benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam], benchGroup "embedCRT" $ [hideArgs bench_embedCRT twoIdxParam] ] @@ -120,6 +121,12 @@ bench_twacePow x = let y = advisePow x in bench (twace :: Cyc t m' r -> Cyc t m r) y +bench_twaceDec :: forall t m m' r . (TwoIdxCtx t m m' r) + => Cyc t m' r -> Bench '(t,m,m',r) +bench_twaceDec x = + let y = adviseDec x + in bench (twace :: Cyc t m' r -> Cyc t m r) y + bench_twaceCRT :: forall t m m' r . (TwoIdxCtx t m m' r) => Cyc t m' r -> Bench '(t,m,m',r) bench_twaceCRT x = diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index a97f5f07..52e4c987 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -52,20 +52,21 @@ benches = [ "crt", "crtInv", "l", - "lInv",-} - {-"*g Pow", + "lInv", + "*g Pow", "*g Dec", "*g CRT", "divg Pow", "divg Dec", - "divg CRT",-}{- + "divg CRT", "lift", - "error", + "error",-} "twacePow", - "twaceCRT",-} - --"embedPow", - "embedDec"--, - --"embedCRT" + "twaceDec", + "twaceCRT", + "embedPow", + "embedDec", + "embedCRT" ] @@ -77,9 +78,9 @@ main = do reports <- mapM (getReports =<<) [ simpleTensorBenches, tensorBenches, - simpleUCycBenches--, - --ucycBenches, - --cycBenches + simpleUCycBenches, + ucycBenches, + cycBenches ] when (verb == Progress) $ putStrLn "" printTable $ map reverse reports diff --git a/lol/benchmarks/SimpleTensorBenches.hs b/lol/benchmarks/SimpleTensorBenches.hs index 5220f66b..ce29fde6 100644 --- a/lol/benchmarks/SimpleTensorBenches.hs +++ b/lol/benchmarks/SimpleTensorBenches.hs @@ -47,6 +47,7 @@ simpleTensorBenches = do bench "error" $ nf (evalRand (fmapT (roundMult one) <$> (tGaussianDec (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (T M Double))) :: CryptoRand HashDRBG -> T M Int64) gen, bench "twacePow" $ nf (twacePowDec :: T M R -> T M' R) x2, + bench "twaceDec" $ nf (twacePowDec :: T M R -> T M' R) x2, bench "twaceCRT" $ nf (fromJust' "SimpleTensorBenches.twaceCRT" twaceCRT :: T M R -> T M' R) x2, bench "embedPow" $ nf (embedPow :: T M' R -> T M R) x4, bench "embedDec" $ nf (embedDec :: T M' R -> T M R) x4, diff --git a/lol/benchmarks/SimpleUCycBenches.hs b/lol/benchmarks/SimpleUCycBenches.hs index d5289fc7..1288652c 100644 --- a/lol/benchmarks/SimpleUCycBenches.hs +++ b/lol/benchmarks/SimpleUCycBenches.hs @@ -50,6 +50,7 @@ simpleUCycBenches = do bench "lift" $ nf lift x4, bench "error" $ nf (evalRand (errorRounded (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (UCyc T M D Int64))) gen, bench "twacePow" $ nf (twacePow :: UCyc T M P R -> UCyc T M' P R) x4, + bench "twaceDec" $ nf (twaceDec :: UCyc T M D R -> UCyc T M' D R) x5, bench "twaceCRT" $ nf (twaceCRTC :: UCyc T M C R -> UCycPC T M' R) x6, bench "embedPow" $ nf (embedPow :: UCyc T M' P R -> UCyc T M P R) x7, bench "embedDec" $ nf (embedDec :: UCyc T M' D R -> UCyc T M D R) x8, diff --git a/lol/benchmarks/TensorBenches.hs b/lol/benchmarks/TensorBenches.hs index 7703241d..820132e7 100644 --- a/lol/benchmarks/TensorBenches.hs +++ b/lol/benchmarks/TensorBenches.hs @@ -24,23 +24,24 @@ tensorBenches :: IO Benchmark tensorBenches = benchGroup "Tensor" [ benchGroup "unzipPow" $ [hideArgs bench_unzip testParam], benchGroup "unzipDec" $ [hideArgs bench_unzip testParam], - benchGroup "unzipCRT" $ [hideArgs bench_unzip testParam], --applyUnzip allParams $ hideArgs bench_unzip, - benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], --applyBasic allParams $ hideArgs bench_mul, - benchGroup "crt" $ [hideArgs bench_crt testParam], --applyBasic allParams $ hideArgs bench_crt, - benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], --applyBasic allParams $ hideArgs bench_crtInv, - benchGroup "l" $ [hideArgs bench_l testParam], --applyBasic allParams $ hideArgs bench_l, + benchGroup "unzipCRT" $ [hideArgs bench_unzip testParam], + benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], + benchGroup "crt" $ [hideArgs bench_crt testParam], + benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], + benchGroup "l" $ [hideArgs bench_l testParam], benchGroup "lInv" $ [hideArgs bench_lInv testParam], - benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], - benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], --applyBasic allParams $ hideArgs bench_mulgCRT, - benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], --applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], + benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], benchGroup "divg Dec" $ [hideArgs bench_divgDec testParam], benchGroup "divg CRT" $ [hideArgs bench_divgCRT testParam], - benchGroup "lift" $ [hideArgs bench_liftPow testParam], --applyLift liftParams $ hideArgs bench_liftPow, - benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], --applyError errorParams $ hideArgs $ bench_errRounded 0.1, - benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, + benchGroup "lift" $ [hideArgs bench_liftPow testParam], + benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], + benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], + benchGroup "twaceDec" $ [hideArgs bench_twacePow twoIdxParam], -- yes, twacePow is correct here. It's the same function! benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], - benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], --applyTwoIdx twoIdxParams $ hideArgs bench_embedPow-} + benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam], benchGroup "embedCRT" $ [hideArgs bench_embedCRT twoIdxParam] ] diff --git a/lol/benchmarks/UCycBenches.hs b/lol/benchmarks/UCycBenches.hs index 1766fab2..66257c97 100644 --- a/lol/benchmarks/UCycBenches.hs +++ b/lol/benchmarks/UCycBenches.hs @@ -19,25 +19,26 @@ import Crypto.Random.DRBG ucycBenches :: IO Benchmark ucycBenches = benchGroup "UCyc" [ - benchGroup "unzipPow" $ [hideArgs bench_unzipUCycPow testParam], -- applyUnzip allParams $ hideArgs bench_unzipUCycPow, + benchGroup "unzipPow" $ [hideArgs bench_unzipUCycPow testParam], benchGroup "unzipDec" $ [hideArgs bench_unzipUCycDec testParam], - benchGroup "unzipCRT" $ [hideArgs bench_unzipUCycCRT testParam], -- applyUnzip allParams $ hideArgs bench_unzipUCycCRT, - benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], -- applyBasic allParams $ hideArgs bench_mul, - benchGroup "crt" $ [hideArgs bench_crt testParam], -- applyBasic allParams $ hideArgs bench_crt, - benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], -- applyBasic allParams $ hideArgs bench_crtInv, - benchGroup "l" $ [hideArgs bench_l testParam], -- applyBasic allParams $ hideArgs bench_l, + benchGroup "unzipCRT" $ [hideArgs bench_unzipUCycCRT testParam], + benchGroup "zipWith (*)" $ [hideArgs bench_mul testParam], + benchGroup "crt" $ [hideArgs bench_crt testParam], + benchGroup "crtInv" $ [hideArgs bench_crtInv testParam], + benchGroup "l" $ [hideArgs bench_l testParam], benchGroup "lInv" $ [hideArgs bench_lInv testParam], - benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], -- applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g Pow" $ [hideArgs bench_mulgPow testParam], benchGroup "*g Dec" $ [hideArgs bench_mulgDec testParam], - benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], -- applyBasic allParams $ hideArgs bench_mulgCRT, - benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], -- applyBasic allParams $ hideArgs bench_mulgPow, + benchGroup "*g CRT" $ [hideArgs bench_mulgCRT testParam], + benchGroup "divg Pow" $ [hideArgs bench_divgPow testParam], benchGroup "divg Dec" $ [hideArgs bench_divgDec testParam], benchGroup "divg CRT" $ [hideArgs bench_divgCRT testParam], - benchGroup "lift" $ [hideArgs bench_liftPow testParam], -- applyLift liftParams $ hideArgs bench_liftPow, - benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], -- applyError errorParams $ hideArgs $ bench_errRounded 0.1 - benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], -- applyTwoIdx twoIdxParams $ hideArgs bench_twacePow, + benchGroup "lift" $ [hideArgs bench_liftPow testParam], + benchGroup "error" $ [hideArgs (bench_errRounded 0.1) testParam'], + benchGroup "twacePow" $ [hideArgs bench_twacePow twoIdxParam], + benchGroup "twaceDec" $ [hideArgs bench_twaceDec twoIdxParam], benchGroup "twaceCRT" $ [hideArgs bench_twaceCRT twoIdxParam], - benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], -- applyTwoIdx twoIdxParams $ hideArgs bench_embedPow + benchGroup "embedPow" $ [hideArgs bench_embedPow twoIdxParam], benchGroup "embedDec" $ [hideArgs bench_embedDec twoIdxParam], benchGroup "embedCRT" $ [hideArgs bench_embedCRT twoIdxParam] ] @@ -120,6 +121,10 @@ bench_twacePow :: forall t m m' r . (TwoIdxCtx t m m' r) => UCyc t m' P r -> Bench '(t,m,m',r) bench_twacePow = bench (twacePow :: UCyc t m' P r -> UCyc t m P r) +bench_twaceDec :: forall t m m' r . (TwoIdxCtx t m m' r) + => UCyc t m' D r -> Bench '(t,m,m',r) +bench_twaceDec = bench (twaceDec :: UCyc t m' D r -> UCyc t m D r) + bench_twaceCRT :: forall t m m' r . (TwoIdxCtx t m m' r) => UCycPC t m' r -> Bench '(t,m,m',r) bench_twaceCRT (Right a) = bench (twaceCRTC :: UCyc t m' C r -> UCycPC t m r) a From 3ed82a5b6934ad4531473a82e868337d38187486 Mon Sep 17 00:00:00 2001 From: Eric Date: Sat, 30 Jul 2016 20:04:27 -0400 Subject: [PATCH 13/21] Updated for new UCyc divG --- lol/benchmarks/SimpleUCycBenches.hs | 6 +++--- lol/benchmarks/UCycBenches.hs | 6 +++--- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/lol/benchmarks/SimpleUCycBenches.hs b/lol/benchmarks/SimpleUCycBenches.hs index 1288652c..5933f263 100644 --- a/lol/benchmarks/SimpleUCycBenches.hs +++ b/lol/benchmarks/SimpleUCycBenches.hs @@ -44,9 +44,9 @@ simpleUCycBenches = do bench "*g Pow" $ nf mulG x4, bench "*g Dec" $ nf mulG x5, bench "*g CRT" $ nf mulG x6, - bench "divg Pow" $ nf divG x4', - bench "divg Dec" $ nf divG x5', - bench "divg CRT" $ nf divG x6, + bench "divg Pow" $ nf divGPow x4', + bench "divg Dec" $ nf divGDec x5', + bench "divg CRT" $ nf divGCRTC x6, bench "lift" $ nf lift x4, bench "error" $ nf (evalRand (errorRounded (0.1 :: Double) :: Rand (CryptoRand HashDRBG) (UCyc T M D Int64))) gen, bench "twacePow" $ nf (twacePow :: UCyc T M P R -> UCyc T M' P R) x4, diff --git a/lol/benchmarks/UCycBenches.hs b/lol/benchmarks/UCycBenches.hs index 66257c97..793caeab 100644 --- a/lol/benchmarks/UCycBenches.hs +++ b/lol/benchmarks/UCycBenches.hs @@ -98,17 +98,17 @@ bench_mulgCRT (Right a) = bench mulG a bench_divgPow :: (BasicCtx t m r) => UCyc t m P r -> Bench '(t,m,r) bench_divgPow x = let y = mulG x - in bench divG y + in bench divGPow y -- divide by g when input is in Dec basis bench_divgDec :: (BasicCtx t m r) => UCyc t m D r -> Bench '(t,m,r) bench_divgDec x = let y = mulG x - in bench divG y + in bench divGDec y -- divide by g when input is in CRT basis bench_divgCRT :: (BasicCtx t m r) => UCycPC t m r -> Bench '(t,m,r) -bench_divgCRT (Right a) = bench divG a +bench_divgCRT (Right a) = bench divGCRTC a -- generate a rounded error term bench_errRounded :: forall t m r gen . (ErrorCtx t m r gen) From 09f2fe3f1e205ebb15c227d46c28490296114467 Mon Sep 17 00:00:00 2001 From: Eric Date: Sun, 31 Jul 2016 19:29:42 -0400 Subject: [PATCH 14/21] Fixed benchmark time for divGCRT in UCyc --- lol/Crypto/Lol/Cyclotomic/UCyc.hs | 2 +- lol/benchmarks/Main.hs | 8 ++++---- 2 files changed, 5 insertions(+), 5 deletions(-) diff --git a/lol/Crypto/Lol/Cyclotomic/UCyc.hs b/lol/Crypto/Lol/Cyclotomic/UCyc.hs index 44e45402..b815588b 100644 --- a/lol/Crypto/Lol/Cyclotomic/UCyc.hs +++ b/lol/Crypto/Lol/Cyclotomic/UCyc.hs @@ -382,7 +382,7 @@ divGDec (Dec v) = Dec <$> T.divGDec v divGCRTC :: (Fact m, UCRTElt t r) => UCyc t m C r -> Maybe (UCyc t m C r) -{-# INLINABLE divGCRTC #-} +{-# INLINE divGCRTC #-} divGCRTC (CRTC s v) = Just $ CRTC s $ divGCRTCS s v -- | Yield the scaled squared norm of \(g_m \cdot e\) under diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index 52e4c987..a42cba64 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -57,16 +57,16 @@ benches = [ "*g Dec", "*g CRT", "divg Pow", - "divg Dec", - "divg CRT", + "divg Dec",-} + "divg CRT"{-, "lift", - "error",-} + "error", "twacePow", "twaceDec", "twaceCRT", "embedPow", "embedDec", - "embedCRT" + "embedCRT"-} ] From 168899683fa93214d914d9eb76c1d79a9a562f4f Mon Sep 17 00:00:00 2001 From: Eric Date: Tue, 2 Aug 2016 22:03:44 -0400 Subject: [PATCH 15/21] fno-liberate-case was slowing down twacePow, twaceDec, embedPow, and embedDec a LOT! Other optimizations (except for -O2) were not helping anything, so removed in the hopes of decreasing compile time --- lol/lol.cabal | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lol/lol.cabal b/lol/lol.cabal index d550e60c..8002a3e2 100644 --- a/lol/lol.cabal +++ b/lol/lol.cabal @@ -205,8 +205,7 @@ Benchmark bench-lol if flag(llvm) ghc-options: -fllvm -optlo-O3 - ghc-options: -O2 -Odph -funbox-strict-fields - ghc-options: -fno-liberate-case -funfolding-use-threshold1000 -funfolding-keeness-factor1000 + ghc-options: -O2 ghc-options: -ddump-to-file -ddump-simpl ghc-options: -dsuppress-coercions -dsuppress-type-applications -dsuppress-uniques -dsuppress-module-prefixes From a05f632055dedeb8fe14855c0e8b7ff31c9a44f6 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 3 Aug 2016 00:31:52 -0400 Subject: [PATCH 16/21] Removed compiler opts for the library that didn't affect runtime. The only one that helps is funfolding-use-threshold1000 (for lift). --- lol/lol.cabal | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lol/lol.cabal b/lol/lol.cabal index 8002a3e2..ff9b0f47 100644 --- a/lol/lol.cabal +++ b/lol/lol.cabal @@ -84,7 +84,9 @@ library -- ghc optimizations if flag(opt) - ghc-options: -O3 -Odph -funbox-strict-fields -fno-liberate-case -funfolding-use-threshold1000 -funfolding-keeness-factor1000 + ghc-options: -O2 + -- makes lift much faster! + ghc-options: -funfolding-use-threshold1000 exposed-modules: Crypto.Lol From 5f6df89facce31504538ebbe96fe32cb1cc99228 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 3 Aug 2016 11:28:36 -0400 Subject: [PATCH 17/21] Fixed performance for (new and improved) twaceCRT --- lol/Crypto/Lol/Cyclotomic/CRTSentinel.hs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lol/Crypto/Lol/Cyclotomic/CRTSentinel.hs b/lol/Crypto/Lol/Cyclotomic/CRTSentinel.hs index 0c4258e7..ddf0f946 100644 --- a/lol/Crypto/Lol/Cyclotomic/CRTSentinel.hs +++ b/lol/Crypto/Lol/Cyclotomic/CRTSentinel.hs @@ -73,5 +73,5 @@ embedCRTCS _ _ = fromJust embedCRT twaceCRTCS :: (Tensor t, m `Divides` m', CRTrans Maybe r, TElt t r) => CSentinel t m' r -> CSentinel t m r -> t m' r -> t m r twaceCRTCS _ _ = fromJust twaceCRT -{-# INLINABLE twaceCRTCS #-} +{-# INLINE twaceCRTCS #-} From 2bbce7f0a403b88a5596110f78fa6a8643a0eb36 Mon Sep 17 00:00:00 2001 From: Eric Date: Wed, 3 Aug 2016 14:56:49 -0400 Subject: [PATCH 18/21] Removed -O2 from library: it helps RT a little, and doesn't hurt CT at all. --- lol/lol.cabal | 2 -- 1 file changed, 2 deletions(-) diff --git a/lol/lol.cabal b/lol/lol.cabal index ff9b0f47..966334fd 100644 --- a/lol/lol.cabal +++ b/lol/lol.cabal @@ -84,10 +84,8 @@ library -- ghc optimizations if flag(opt) - ghc-options: -O2 -- makes lift much faster! ghc-options: -funfolding-use-threshold1000 - exposed-modules: Crypto.Lol Crypto.Lol.Types From 4b1fb85091ad41265bfb4e9130dbbb21cc8eff51 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 4 Aug 2016 01:58:07 -0400 Subject: [PATCH 19/21] Settings for Chris --- lol/benchmarks/BenchParams.hs | 8 ++++---- lol/benchmarks/Main.hs | 22 +++++++++++----------- 2 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lol/benchmarks/BenchParams.hs b/lol/benchmarks/BenchParams.hs index c1d9cdee..5a46c834 100644 --- a/lol/benchmarks/BenchParams.hs +++ b/lol/benchmarks/BenchParams.hs @@ -22,10 +22,10 @@ type Tensors = '[T] type MRCombos = '[ '(M, R) ] -type T = CT -type M = F9*F5*F7*F11 -- F64*F9*F25 -- -type R = Zq 34651 -- Zq 14401 -- -type M' = F3*F5*F11 +type T = RT +type M = F64*F9*F25 -- F9*F5*F7*F11 -- F64*F9*F25 -- +type R = Zq 1065601 --34651 -- Zq 14401 -- +type M' = M --F3*F5*F11 testParam :: Proxy '(T, M, R) testParam = Proxy diff --git a/lol/benchmarks/Main.hs b/lol/benchmarks/Main.hs index a42cba64..db322fd4 100644 --- a/lol/benchmarks/Main.hs +++ b/lol/benchmarks/Main.hs @@ -45,21 +45,21 @@ verb = Progress benches :: [String] benches = [ - {-"unzipPow", - "unzipDec", - "unzipCRT", + --"unzipPow", + --"unzipDec", + --"unzipCRT", "zipWith (*)", "crt", "crtInv", "l", "lInv", "*g Pow", - "*g Dec", + --"*g Dec", "*g CRT", "divg Pow", - "divg Dec",-} - "divg CRT"{-, - "lift", + "divg Dec", + --"divg CRT", + "lift"{-, "error", "twacePow", "twaceDec", @@ -76,11 +76,11 @@ main :: IO () main = do hSetBuffering stdout NoBuffering -- for better printing of progress reports <- mapM (getReports =<<) [ - simpleTensorBenches, + {-simpleTensorBenches, tensorBenches, - simpleUCycBenches, - ucycBenches, - cycBenches + simpleUCycBenches,-} + ucycBenches{-, + cycBenches-} ] when (verb == Progress) $ putStrLn "" printTable $ map reverse reports From dc0b11010ff4cba64c154dc58d95cee7a8d5507d Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 4 Aug 2016 15:53:58 -0400 Subject: [PATCH 20/21] Fixed bug in beacon byte calculator; added a check in Verify.hs for it. File names now have a rounded variance (3 decimals), and Main complains if optChallDir exists to prevent having multiple challenges with the same challID --- challenges/exec/Beacon.hs | 2 +- challenges/exec/Common.hs | 6 +++--- challenges/exec/Generate.hs | 8 ++++++-- challenges/exec/Main.hs | 6 ++++++ challenges/exec/Verify.hs | 1 + challenges/gen.sh | 4 ++-- 6 files changed, 19 insertions(+), 8 deletions(-) diff --git a/challenges/exec/Beacon.hs b/challenges/exec/Beacon.hs index d281f4f0..c4ca9b90 100644 --- a/challenges/exec/Beacon.hs +++ b/challenges/exec/Beacon.hs @@ -34,7 +34,7 @@ validBeaconAddr (BA epoch offset) = -- beacon if necessary. nextBeaconAddr :: BeaconAddr -> BeaconAddr nextBeaconAddr (BA time byteOffset) = - if byteOffset == bytesPerBeacon + if byteOffset == bytesPerBeacon-1 then BA (time+beaconInterval) 0 else BA time (byteOffset+1) diff --git a/challenges/exec/Common.hs b/challenges/exec/Common.hs index 3aa963fd..ba623d1c 100644 --- a/challenges/exec/Common.hs +++ b/challenges/exec/Common.hs @@ -81,10 +81,10 @@ readProtoType file = do -- | Parse the beacon time/offset used to reveal a challenge. parseBeaconAddr :: (MonadError String m) => Challenge -> m BeaconAddr parseBeaconAddr Challenge{..} = do + let ba = BA beaconEpoch beaconOffset -- validate the time and offset - throwErrorUnless (validBeaconAddr $ BA beaconEpoch beaconOffset) - "Invalid beacon address." - return $ BA beaconEpoch beaconOffset + throwErrorUnless (validBeaconAddr ba) $ "Invalid beacon address: " ++ show ba + return ba -- | Yield the ID of the suppressed secret for a challenge, given a -- beacon record and a byte offset. diff --git a/challenges/exec/Generate.hs b/challenges/exec/Generate.hs index b4a1a5df..67a0efca 100644 --- a/challenges/exec/Generate.hs +++ b/challenges/exec/Generate.hs @@ -42,6 +42,8 @@ import Control.Monad.Random import Data.ByteString.Lazy as BS (writeFile) import Data.Reflection hiding (D) +import Prelude ((^^)) + import System.Directory (createDirectoryIfMissing) import Text.ProtocolBuffers (messagePut) @@ -82,10 +84,12 @@ challengeName :: ChallengeID -> ChallengeParams -> FilePath challengeName challID params = "chall-id" ++ show challID ++ (case params of - C{..} -> "-rlwec-m" ++ show m ++ "-q" ++ show q ++ "-v" ++ show svar - D{..} -> "-rlwed-m" ++ show m ++ "-q" ++ show q ++ "-v" ++ show svar + C{..} -> "-rlwec-m" ++ show m ++ "-q" ++ show q ++ "-v" ++ show (roundPrec svar 3) + D{..} -> "-rlwed-m" ++ show m ++ "-q" ++ show q ++ "-v" ++ show (roundPrec svar 3) R{..} -> "-rlwr-m" ++ show m ++ "-q" ++ show q ++ "-p" ++ show p) ++ "-l" ++ show (P.numSamples params) + where roundPrec :: Double -> Int -> Double + roundPrec f n = (fromInteger $ round $ f * (10^n)) / (10.0^^n) -- | Generate a challenge with the given parameters. genChallengeU :: (MonadRandom rnd) diff --git a/challenges/exec/Main.hs b/challenges/exec/Main.hs index 14e342f2..eeb48cb4 100644 --- a/challenges/exec/Main.hs +++ b/challenges/exec/Main.hs @@ -4,10 +4,12 @@ module Main where +import Control.Monad import Data.Time.Clock.POSIX import Options import System.Console.ANSI +import System.Directory import System.IO import System.IO.Unsafe @@ -68,6 +70,10 @@ main = do generate :: MainOpts -> GenOpts -> [String] -> IO () generate MainOpts{..} GenOpts{..} _ = do + challDirExists <- doesDirectoryExist optChallDir + when challDirExists $ + error $ "The output directory " ++ optChallDir ++ + " already exists. Delete it or choose a new destination." let initBeaconTime = beaconFloor optInitBeaconEpoch initBeacon = BA initBeaconTime 0 currTime <- round <$> getPOSIXTime diff --git a/challenges/exec/Verify.hs b/challenges/exec/Verify.hs index b921a3b7..5a2656cc 100644 --- a/challenges/exec/Verify.hs +++ b/challenges/exec/Verify.hs @@ -91,6 +91,7 @@ readChallenge path challName = do readFullChallenge) liftIO $ putStr msg + _ <- parseBeaconAddr c -- verify that the beacon address is valid readChall path challName c -- | Whether we have an XML file for the beacon at the given epoch. diff --git a/challenges/gen.sh b/challenges/gen.sh index 68324b15..0c893fe5 100755 --- a/challenges/gen.sh +++ b/challenges/gen.sh @@ -23,9 +23,9 @@ echo "Signing the tar file..." # > uid Chris Peikert (Signing key for Ring-LWE challenges) # put 0xB8242E6B below (not sure if this is the same across multiple computers) -gpg -u 0xB8242E6B -s $tarfile --yes +gpg -u 0xB8242E6B --yes -s $tarfile echo "Hashing the tar file..." openssl dgst -sha256 $tarfile -echo "Go to www.originstamp.org to commit to the signature." \ No newline at end of file +echo "Go to www.originstamp.org to commit to the signature." From 01443ee5e7740dd169d0afc4b5810e0dd8cdb906 Mon Sep 17 00:00:00 2001 From: Eric Date: Fri, 12 Aug 2016 22:36:27 -0400 Subject: [PATCH 21/21] Updated challenges back to master --- challenges/exec/Generate.hs | 2 -- challenges/exec/Main.hs | 4 ---- 2 files changed, 6 deletions(-) diff --git a/challenges/exec/Generate.hs b/challenges/exec/Generate.hs index 8f247319..5b34c029 100644 --- a/challenges/exec/Generate.hs +++ b/challenges/exec/Generate.hs @@ -42,8 +42,6 @@ import qualified Data.ByteString.Lazy as BS import Data.Reflection hiding (D) import qualified Data.Tagged as T -import Prelude ((^^)) - import System.Directory (createDirectoryIfMissing) import Text.Printf diff --git a/challenges/exec/Main.hs b/challenges/exec/Main.hs index c9aaba6e..04191ab5 100644 --- a/challenges/exec/Main.hs +++ b/challenges/exec/Main.hs @@ -68,10 +68,6 @@ main = do generate :: MainOpts -> GenOpts -> [String] -> IO () generate MainOpts{..} GenOpts{..} _ = do - challDirExists <- doesDirectoryExist optChallDir - when challDirExists $ - error $ "The output directory " ++ optChallDir ++ - " already exists. Delete it or choose a new destination." let initBeaconTime = beaconFloor optInitBeaconEpoch initBeacon = BA initBeaconTime 0 when (initBeaconTime == 0) $ do