{-# LANGUAGE DataKinds #-}
{-# LANGUAGE FlexibleContexts #-}
{-# LANGUAGE NoImplicitPrelude #-}
{-# LANGUAGE PartialTypeSignatures #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeFamilies #-}
{-# LANGUAGE TypeOperators #-}
{-# OPTIONS_GHC -fno-warn-partial-type-signatures #-}
module Crypto.Lol.Benchmarks.CycRepBenches (cycRepBenches1, cycRepBenches2) where
import Control.Monad.Random hiding (lift)
import Crypto.Lol.Cyclotomic.CycRep
import Crypto.Lol.Prelude
import Crypto.Lol.Types
import Crypto.Lol.Utils.Benchmarks (Benchmark, bgroup, mkBench, mkBenchIO)
import Crypto.Random
{-# ANN module "HLint: ignore Use camelCase" #-}
{-# INLINABLE cycRepBenches1 #-}
cycRepBenches1 :: forall t m r gen . _ => Proxy '(t,m,r) -> Proxy gen -> Benchmark
cycRepBenches1 ptmr pgen =
let zDec = zero :: CycRep t D m r
zPow = zero :: CycRep t P m r
zEC = zero :: CycRepEC t m r
zPC = ecToPC zEC
errorBench = mkBenchIO "error" (bench_errRounded ptmr pgen 0.1)
benches = [
mkBench "zipWith (*)" (bench_mul zPC) zPC,
mkBench "crt" bench_crt zPow,
mkBench "crtInv" bench_crtInv zPC,
mkBench "l" bench_l zDec,
mkBench "lInv" bench_lInv zPow,
mkBench "*g Pow" bench_mulgPow zPow,
mkBench "*g Dec" bench_mulgDec zDec,
mkBench "*g CRT" bench_mulgCRT zPC,
mkBench "divG Pow" bench_divGPow zPow,
mkBench "divG Dec" bench_divGDec zDec,
mkBench "divG CRT" bench_divGCRT zPC,
mkBench "lift" bench_liftPow zPow] in
bgroup "CycRep" (benches ++ [errorBench])
{-# INLINE cycRepBenches2 #-}
cycRepBenches2 :: forall t m m' r . _ => Proxy '(t,m,m',r) -> Benchmark
cycRepBenches2 ptmmr =
let zPow' = zero :: CycRep t P m' r
zDec' = zero :: CycRep t D m' r
zEC' = zero :: CycRepEC t m' r
zPC' = ecToPC zEC'
zPow = zero :: CycRep t P m r
zEC = zero :: CycRepEC t m r
zPC = ecToPC zEC
benches = [
mkBench "twacePow" (bench_twacePow ptmmr) zPow',
mkBench "twaceDec" (bench_twaceDec ptmmr) zDec',
mkBench "twaceCRT" (bench_twaceCRT ptmmr) zPC',
mkBench "embedPow" (bench_embedPow ptmmr) zPow,
mkBench "embedCRT" (bench_embedCRT ptmmr) zPC] in
bgroup "CycRep" benches
pcToEC :: CycRepPC t m r -> CycRepEC t m r
pcToEC (Right x) = Right x
ecToPC :: CycRepEC t m r -> CycRepPC t m r
ecToPC (Right x) = Right x
{-# INLINE bench_mul #-}
bench_mul :: _ => CycRepPC t m r -> CycRepPC t m r -> CycRepEC t m r
bench_mul a b = pcToEC a * pcToEC b
{-# INLINE bench_crt #-}
bench_crt :: _ => CycRep t P m r -> CycRepEC t m r
bench_crt = toCRT
{-# INLINABLE bench_crtInv #-}
bench_crtInv :: _ => CycRepPC t m r -> CycRep t P m r
bench_crtInv (Right a) = toPow a
{-# INLINE bench_l #-}
bench_l :: _ => CycRep t D m r -> CycRep t P m r
bench_l = toPow
{-# INLINE bench_lInv #-}
bench_lInv :: _ => CycRep t P m r -> CycRep t D m r
bench_lInv = toDec
{-# INLINABLE bench_liftPow #-}
bench_liftPow :: _ => CycRep t P m r -> CycRep t P m r'
bench_liftPow = lift
{-# INLINABLE bench_mulgPow #-}
bench_mulgPow :: _ => CycRep t P m r -> CycRep t P m r
bench_mulgPow = mulGPow
{-# INLINABLE bench_mulgDec #-}
bench_mulgDec :: _ => CycRep t D m r -> CycRep t D m r
bench_mulgDec = mulGDec
{-# INLINABLE bench_mulgCRT #-}
bench_mulgCRT :: _ => CycRepPC t m r -> CycRep t C m r
bench_mulgCRT (Right a) = mulGCRTC a
{-# INLINABLE bench_divGPow #-}
bench_divGPow :: _ => CycRep t P m r -> Maybe (CycRep t P m r)
bench_divGPow = divGPow . mulGPow
{-# INLINABLE bench_divGDec #-}
bench_divGDec :: _ => CycRep t D m r -> Maybe (CycRep t D m r)
bench_divGDec = divGDec . mulGDec
{-# INLINABLE bench_divGCRT #-}
bench_divGCRT :: _ => CycRepPC t m r -> CycRep t C m r
bench_divGCRT = either (error "bench_divGCRT expected a CRTC") divGCRTC
{-# INLINABLE bench_errRounded #-}
bench_errRounded :: forall t m r gen . _
=> Proxy '(t,m,r) -> Proxy gen -> Double -> IO (CycRep t D m (LiftOf r))
bench_errRounded _ _ v = do
gen <- newGenIO
let e = roundedGaussian v :: Rand (CryptoRand gen) (CycRep t D m (LiftOf r))
return $ evalRand e gen
{-# INLINE bench_twacePow #-}
bench_twacePow :: forall t m m' r . _
=> Proxy '(t,m,m',r) -> CycRep t P m' r -> CycRep t P m r
bench_twacePow _ = twacePow
{-# INLINE bench_twaceDec #-}
bench_twaceDec :: forall t m m' r . _
=> Proxy '(t,m,m',r) -> CycRep t D m' r -> CycRep t D m r
bench_twaceDec _ = twaceDec
{-# INLINE bench_twaceCRT #-}
bench_twaceCRT :: forall t m m' r . _
=> Proxy '(t,m,m',r) -> CycRepPC t m' r -> CycRepPC t m r
bench_twaceCRT _ (Right a) = twaceCRTC a
{-# INLINE bench_embedPow #-}
bench_embedPow :: forall t m m' r . _
=> Proxy '(t,m,m',r) -> CycRep t P m r -> CycRep t P m' r
bench_embedPow _ = embedPow
{-# INLINE bench_embedCRT #-}
bench_embedCRT :: forall t m m' r . _
=> Proxy '(t,m,m',r) -> CycRepPC t m r -> CycRepPC t m' r
bench_embedCRT _ (Right a) = embedCRTC a