SYNOPSIS

        use Data::Entropy::Source;

        $source = Data::Entropy::Source->new($handle, "sysread");

        $c = $source->get_octet;
        $str = $source->get_bits(17);
        $i = $source->get_int(12345);
        $i = $source->get_int(Math::BigInt->new("1000000000000"));
        $j = $source->get_prob(1, 2);

DESCRIPTION

An object of this class encapsulates a source of entropy (randomness). Methods allow entropy to be dispensed in any quantity required, even fractional bits. An entropy source object should not normally be used directly. Rather, it should be used to support higher-level entropy-consuming algorithms, such as those in Data::Entropy::Algorithms.

This type of object is constructed as a layer over a raw entropy source which does not supply methods to extract arbitrary amounts of entropy. The raw entropy source is expected to dispense only entire octets at a time. The /dev/random devices on some versions of Unix constitute such a source, for example. The raw entropy source is accessed via the \*(C`IO::Handle\*(C' interface. This interface may be supplied by classes other than \*(C`IO::Handle\*(C' itself, as is done for example by \*(C`Data::Entropy::RawSource::CryptCounter\*(C'.

If two entropy sources of this class are given exactly the same raw entropy data, for example by reading from the same file, and exactly the same sequence of \*(C`get_\*(C' method calls is made to them, then they will return exactly the same values from those calls. (Calls with numerical arguments that have the same numerical value but are of different types count as the same for this purpose.) This means that a run of an entropy-using algorithm can be made completely deterministic if desired.

CONSTRUCTOR

Data::Entropy::Source->new(\s-1RAW_SOURCE\s0, \s-1READ_STYLE\s0)

Constructs and returns an entropy source object based on the given raw source. \s-1RAW_SOURCE\s0 must be an I/O handle referring to a source of entropy that can be read one octet at a time. Specifically, it must support either the \*(C`getc\*(C' or \*(C`sysread\*(C' method described in IO::Handle. \s-1READ_STYLE\s0 must be a string, either \*(L"getc\*(R" or \*(L"sysread\*(R", indicating which method should be used to read from the raw source. No methods other than the one specified will ever be called on the raw source handle, so a full implementation of \*(C`IO::Handle\*(C' is not required. The \*(C`sysread\*(C' method should be used with /dev/random and its ilk, because buffering would be very wasteful of entropy and might consequently block other processes that require entropy. \*(C`getc\*(C' should be preferred when reading entropy from a regular file, and it is the more convenient interface to implement when a non-I/O object is being used for the handle.

METHODS

Returns an octet of entropy, as a string of length one. This provides direct access to the raw entropy source. Returns \s-1NBITS\s0 bits of entropy, as a string of octets. If \s-1NBITS\s0 is not a multiple of eight then the last octet in the string has its most significant bits set to zero. \s-1LIMIT\s0 must be a positive integer. Returns a uniformly-distributed random number between zero inclusive and \s-1LIMIT\s0 exclusive. \s-1LIMIT\s0 may be either a native integer, a \*(C`Math::BigInt\*(C' object, or an integer-valued \*(C`Math::BigRat\*(C' object; the returned number is of the same type. This method dispenses a non-integer number of bits of entropy. For example, if \s-1LIMIT\s0 is 10 then the result contains approximately 3.32 bits of entropy. The minimum non-zero amount of entropy that can be obtained is 1 bit, with \s-1LIMIT\s0 = 2. \s-1PROB0\s0 and \s-1PROB1\s0 must be non-negative integers, not both zero. They may each be either a native integer, a \*(C`Math::BigInt\*(C' object, or an integer-valued \*(C`Math::BigRat\*(C' objects; types may be mixed. Returns either 0 or 1, with relative probabilities \s-1PROB0\s0 and \s-1PROB1\s0. That is, the probability of returning 0 is \s-1PROB0/\s0(\s-1PROB0+PROB1\s0), and the probability of returning 1 is \s-1PROB1/\s0(\s-1PROB0+PROB1\s0). This method dispenses a fraction of a bit of entropy. The maximum amount of entropy that can be obtained is 1 bit, with \s-1PROB0\s0 = \s-1PROB1\s0. The more different the probabilities are the less entropy is obtained. For example, if \s-1PROB0\s0 = 1 and \s-1PROB1\s0 = 2 then the result contains approximately 0.918 bits of entropy.

RELATED TO Data::Entropy::Source…

Data::Entropy, Data::Entropy::Algorithms, Data::Entropy::RawSource::CryptCounter, Data::Entropy::RawSource::Local, Data::Entropy::RawSource::RandomOrg, IO::Handle

AUTHOR

Andrew Main (Zefram) <[email protected]>

COPYRIGHT

Copyright (C) 2006, 2007, 2009, 2011 Andrew Main (Zefram) <[email protected]>

LICENSE

This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself.