I am trying to generate large random prime numbers (1024 bit-ish) so I need a way to generate large positive random numbers.
I began with System.Random
but want to move to Crypto.Random
from the crypto-api
package.
The Crypto.Random
only produces bytestrings, so I need a way to convert to an Integer
type. What is the best way to do this?
Data.Binary
. I can easily convertInteger
types to bytestrings, but often converting from random bytestrings toInteger
type causes an error. I don't know the internal representation ofInteger
but I assume generating random bytes can make invalid numbers. – SentimentBinary
instance forInteger
writes a tag for whether it's a small integer that fits in a few bytes or a big one that is in many bytes, and for large integers writes a sign byte and a list of bytes preceded with a length; if the length doesn't match the number of bytes you'd get an error. – DumahBinary
instance forNatural
is in the same file. Neither of these is how they're represented in memory. Both end up reading a list of bytes and folding it one byte at a time. Instead of mucking around in formatting your byte string into one of these formats, fold it yourself as shown in my answer. – Dumah