monadcryptorandom-0.7.2: A monad for using CryptoRandomGen

MaintainerThomas.DuBuisson@gmail.com
Stabilitybeta
Portabilityportable
Safe HaskellNone
LanguageHaskell98

Control.Monad.CryptoRandom

Description

Much like the MonadRandom package (Control.Monad.Random), this module provides plumbing for the CryptoRandomGen generators.

Synopsis

Documentation

class CRandom a where Source #

CRandom a is much like the Random class from the System.Random module in the "random" package. The main difference is CRandom builds on "crypto-api"'s CryptoRandomGen, so it allows explicit failure.

crandomR (low,high) g as typically instantiated will generate a value between [low, high] inclusively, swapping the pair if high < low.

Provided instances for crandom g generates randoms between the bounds and between +/- 2^256 for Integer.

The crandomR function has degraded (theoretically unbounded, probabilistically decent) performance the closer your range size (high - low) is to 2^n (from the top).

Minimal complete definition

crandom

Methods

crandom :: CryptoRandomGen g => g -> Either GenError (a, g) Source #

crandoms :: CryptoRandomGen g => g -> [a] Source #

Instances

CRandom Bool Source # 
CRandom Int Source # 
CRandom Int8 Source # 
CRandom Int16 Source # 
CRandom Int32 Source # 
CRandom Int64 Source # 
CRandom Word8 Source # 
CRandom Word16 Source # 
CRandom Word32 Source # 
CRandom Word64 Source # 

class CRandomR a where Source #

Minimal complete definition

crandomR

Methods

crandomR :: CryptoRandomGen g => (a, a) -> g -> Either GenError (a, g) Source #

crandomRs :: CryptoRandomGen g => (a, a) -> g -> [a] Source #

Instances

CRandomR Int Source # 

Methods

crandomR :: CryptoRandomGen g => (Int, Int) -> g -> Either GenError (Int, g) Source #

crandomRs :: CryptoRandomGen g => (Int, Int) -> g -> [Int] Source #

CRandomR Int8 Source # 

Methods

crandomR :: CryptoRandomGen g => (Int8, Int8) -> g -> Either GenError (Int8, g) Source #

crandomRs :: CryptoRandomGen g => (Int8, Int8) -> g -> [Int8] Source #

CRandomR Int16 Source # 
CRandomR Int32 Source # 
CRandomR Int64 Source # 
CRandomR Integer Source # 
CRandomR Word8 Source # 
CRandomR Word16 Source # 
CRandomR Word32 Source # 
CRandomR Word64 Source # 

class (ContainsGenError e, MonadError e m) => MonadCRandom e m where Source #

MonadCRandom m represents a monad that can produce random values (or fail with a GenError). It is suggested you use the CRandT transformer in your monad stack.

Minimal complete definition

getCRandom, getBytes, getBytesWithEntropy, doReseed

Instances

(Monoid w, MonadCRandom e m) => MonadCRandom e (WriterT w m) Source # 
(Monoid w, MonadCRandom e m) => MonadCRandom e (WriterT w m) Source # 
MonadCRandom e m => MonadCRandom e (StateT s m) Source # 
MonadCRandom e m => MonadCRandom e (StateT s m) Source # 
(ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandom e (CRandT g e m) Source # 
MonadCRandom e m => MonadCRandom e (ReaderT * r m) Source # 
(Monoid w, MonadCRandom e m) => MonadCRandom e (RWST r w s m) Source # 
(Monoid w, MonadCRandom e m) => MonadCRandom e (RWST r w s m) Source # 

class (ContainsGenError e, MonadError e m) => MonadCRandomR e m where Source #

Minimal complete definition

getCRandomR

Methods

getCRandomR :: CRandomR a => (a, a) -> m a Source #

Instances

(MonadCRandomR e m, Monoid w) => MonadCRandomR e (WriterT w m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> WriterT w m a Source #

(MonadCRandomR e m, Monoid w) => MonadCRandomR e (WriterT w m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> WriterT w m a Source #

MonadCRandomR e m => MonadCRandomR e (StateT s m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> StateT s m a Source #

MonadCRandomR e m => MonadCRandomR e (StateT s m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> StateT s m a Source #

MonadCRandomR e m => MonadCRandomR e (ReaderT * r m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> ReaderT * r m a Source #

(ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandomR e (CRandT g e m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> CRandT g e m a Source #

(MonadCRandomR e m, Monoid w) => MonadCRandomR e (RWST r w s m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> RWST r w s m a Source #

(MonadCRandomR e m, Monoid w) => MonadCRandomR e (RWST r w s m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> RWST r w s m a Source #

newtype CRandT g e m a Source #

CRandT is the transformer suggested for MonadCRandom.

Constructors

CRandT 

Fields

Instances

MonadWriter w m => MonadWriter w (CRandT g e m) Source # 

Methods

writer :: (a, w) -> CRandT g e m a #

tell :: w -> CRandT g e m () #

listen :: CRandT g e m a -> CRandT g e m (a, w) #

pass :: CRandT g e m (a, w -> w) -> CRandT g e m a #

MonadState s m => MonadState s (CRandT g e m) Source # 

Methods

get :: CRandT g e m s #

put :: s -> CRandT g e m () #

state :: (s -> (a, s)) -> CRandT g e m a #

MonadReader r m => MonadReader r (CRandT g e m) Source # 

Methods

ask :: CRandT g e m r #

local :: (r -> r) -> CRandT g e m a -> CRandT g e m a #

reader :: (r -> a) -> CRandT g e m a #

Monad m => MonadError e (CRandT g e m) Source # 

Methods

throwError :: e -> CRandT g e m a #

catchError :: CRandT g e m a -> (e -> CRandT g e m a) -> CRandT g e m a #

(ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandomR e (CRandT g e m) Source # 

Methods

getCRandomR :: CRandomR a => (a, a) -> CRandT g e m a Source #

(ContainsGenError e, Monad m, CryptoRandomGen g) => MonadCRandom e (CRandT g e m) Source # 
MonadTrans (CRandT g e) Source # 

Methods

lift :: Monad m => m a -> CRandT g e m a #

Monad m => Monad (CRandT g e m) Source # 

Methods

(>>=) :: CRandT g e m a -> (a -> CRandT g e m b) -> CRandT g e m b #

(>>) :: CRandT g e m a -> CRandT g e m b -> CRandT g e m b #

return :: a -> CRandT g e m a #

fail :: String -> CRandT g e m a #

Functor m => Functor (CRandT g e m) Source # 

Methods

fmap :: (a -> b) -> CRandT g e m a -> CRandT g e m b #

(<$) :: a -> CRandT g e m b -> CRandT g e m a #

MonadFix m => MonadFix (CRandT g e m) Source # 

Methods

mfix :: (a -> CRandT g e m a) -> CRandT g e m a #

Monad m => Applicative (CRandT g e m) Source # 

Methods

pure :: a -> CRandT g e m a #

(<*>) :: CRandT g e m (a -> b) -> CRandT g e m a -> CRandT g e m b #

liftA2 :: (a -> b -> c) -> CRandT g e m a -> CRandT g e m b -> CRandT g e m c #

(*>) :: CRandT g e m a -> CRandT g e m b -> CRandT g e m b #

(<*) :: CRandT g e m a -> CRandT g e m b -> CRandT g e m a #

MonadIO m => MonadIO (CRandT g e m) Source # 

Methods

liftIO :: IO a -> CRandT g e m a #

MonadThrow m => MonadThrow (CRandT g e m) Source #

Throws exceptions into the base monad.

Since: 0.7.1

Methods

throwM :: Exception e => e -> CRandT g e m a #

MonadCatch m => MonadCatch (CRandT g e m) Source #

Catches exceptions from the base monad.

Since: 0.7.1

Methods

catch :: Exception e => CRandT g e m a -> (e -> CRandT g e m a) -> CRandT g e m a #

MonadCont m => MonadCont (CRandT g e m) Source # 

Methods

callCC :: ((a -> CRandT g e m b) -> CRandT g e m a) -> CRandT g e m a #

type CRand g e = CRandT g e Identity Source #

Simple users of generators can use CRand for quick and easy generation of randoms. See below for a simple use of newGenIO (from "crypto-api"), getCRandom, getBytes, and runCRandom.

getRandPair = do
  int <- getCRandom
  bytes <- getBytes 100
  return (int, bytes)

func = do
  g <- newGenIO
  case runCRand getRandPair g of
    Right ((int,bytes), g') -> useRandomVals (int,bytes)
    Left x -> handleGenError x

runCRandT :: ContainsGenError e => CRandT g e m a -> g -> m (Either e (a, g)) Source #

evalCRandT :: (ContainsGenError e, Monad m) => CRandT g e m a -> g -> m (Either e a) Source #

runCRand :: ContainsGenError e => CRand g e a -> g -> Either e (a, g) Source #

liftCRand :: (g -> Either e (a, g)) -> CRand g e a Source #

liftCRandT :: Monad m => (g -> Either e (a, g)) -> CRandT g e m a Source #