monadcryptorandom-0.7.0: 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

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.

newtype CRandT g e m a Source

CRandT is the transformer suggested for MonadCRandom.

Constructors

CRandT 

Fields

unCRandT :: StateT g (ExceptT 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