genBits: small addition to random API + largely new implementation that needs code review

Thomas DuBuisson thomas.dubuisson at gmail.com
Wed Jun 29 00:01:59 CEST 2011


Ryan Newton <rrnewton at gmail.com> wrote:
> There implementation has two major pieces:
>     (1) Sources of random bits (class RandomGen)
>     (2) Instances of class Random that map use random bits to create Haskell
> types

I'd say there are three pieces, initial entropy (clock, external seed,
system crypto random generator), deterministic generator interface
(PRNG, the RandomGen class), and the instances of that class.

I tried to answer the first item in System.Crypto.Random.  The 'random' package
never really had an answer for entropy and I'm not sure what the
community thinks about that.  Perhaps answering this problem in slightly
obscure packages is OK.

> The issue is
> that the legacy RandomGen API isn't very good at generating random bits.

Agreed.  This is why CryptoRandomGen uses ByteString (so we can
generate any number of BYTES of random values).

As I tried to argue when I make RandomGen more polymorphic, this is an
issue with hardcoding the type as Int.  Unfortunately, people didn't
accept that alteration and I feel continuing to generating Int while
allowing devs to discover the amount of entropy isn't taking things
far enough.

> I'd also be happy to hear other proposals for API changes and additions.

As I say below, I don't understand why we can't use ByteString.  There
are (or should be) rather fast decodings of all the popular primitive
types from bytestrings, so this takes care of our polymorphism issue.

I accept that, unlike the CryptoRandomGen, we don't want an explicit
failure for RandomGen.  But how about a common interface for
instantiating generators ('newGen')?  Is there a reason not to have that?

--- CODE ---
class RandomGen g where
    next :: g -> (Int, g)
    nextBits :: g -> BitLength -> (ByteString, g)
    genBits :: g -> Int
    newGen :: ByteString -> g
--- END CODE ---

Also, perhaps we could still have an explicit reseed?

--- CODE ---
class Reseedable g where
    reseed :: g -> Bytestring -> g
--- END CODE ---

I'll stop here before I entirely recreate CryptoRandomGen, but without
the explicit errors and in more classes (the next step would be a method
for querying how much entropy is needed for instantiation).

>  System.Random can't depend on bytestring, so I doubt we want block RNG in
> the RandomGen class.

Why can't it?  System.Ranomd isn't part of H2010 and H98 already needs
its own random module.

I'll try to make time to review the code, you'll hear if I do.

Cheers,
Thomas



More information about the Libraries mailing list