Crypt::OpenSSL::Bignum#
Arbitrary-precision integers, computed by OpenSSL’s BIGNUM library.
A Crypt::OpenSSL::Bignum holds one integer of any size. Build it from a decimal or hexadecimal string, an unsigned machine word or big-endian bytes, compute with it, and read it back the same ways. The methods that need scratch space take a Crypt::OpenSSL::Bignum::CTX, which can be reused across calls. A failed operation croaks with OpenSSL’s reason, as in OpenSSL error: div by zero.
The arithmetic is the one in the system’s libcrypto 3, reached through the built-in Net::SSLeay. Bignums are also how Crypt::OpenSSL::RSA hands out and takes key parameters.
This is a native implementation of Crypt::OpenSSL::Bignum 0.09’s Bignum.xs; its Crypt/OpenSSL/Bignum.pm and Crypt/OpenSSL/Bignum/CTX.pm are used unchanged.
Synopsis#
use Crypt::OpenSSL::Bignum;
use Crypt::OpenSSL::Bignum::CTX;
my $ctx = Crypt::OpenSSL::Bignum::CTX->new;
my $fac = Crypt::OpenSSL::Bignum->one;
$fac->mul(Crypt::OpenSSL::Bignum->new_from_word($_), $ctx, $fac) for 1 .. 30;
print $fac->to_decimal, "\n"; # 265252859812191058636308480000000
Modules#
Crypt::OpenSSL::Bignum::CTX- Perl interface to the OpenSSL BN_CTX structure.
Functions#
Construction#
new_from_word#
Crypt::OpenSSL::Bignum->new_from_word($word): the number of an unsigned machine word.
new_from_decimal#
Crypt::OpenSSL::Bignum->new_from_decimal($string): the number a decimal string spells, with an optional leading -.
new_from_hex#
Crypt::OpenSSL::Bignum->new_from_hex($string): the number a hexadecimal string spells, without a 0x prefix.
new_from_bin#
Crypt::OpenSSL::Bignum->new_from_bin($bytes): the unsigned number whose big-endian bytes these are.
new#
Crypt::OpenSSL::Bignum->new: the number 0.
zero#
Crypt::OpenSSL::Bignum->zero: the number 0.
one#
Crypt::OpenSSL::Bignum->one: the number 1.
rand#
Crypt::OpenSSL::Bignum->rand($bits, $top, $bottom): a random number of $bits bits. $top -1 lets the top bit be 0, 0 sets it, 1 sets the top two; a true $bottom makes the number odd.
pseudo_rand#
Crypt::OpenSSL::Bignum->pseudo_rand($bits, $top, $bottom): the same as rand.
rand_range#
Crypt::OpenSSL::Bignum->rand_range($range): a random number at least 0 and below $range.
bless_pointer#
Crypt::OpenSSL::Bignum->bless_pointer($address): a bignum object over the BIGNUM at that address, which it then owns - the form Crypt::OpenSSL::RSA’s get_key_parameters hands numbers out in.
Conversion#
to_decimal#
$bn->to_decimal: the number as a decimal string.
to_hex#
$bn->to_hex: the number as an upper-case hexadecimal string.
to_bin#
$bn->to_bin: the magnitude as big-endian bytes; the empty string for 0.
get_word#
$bn->get_word: the number as an unsigned machine word; all bits set when it does not fit.
Arithmetic#
add#
$bn->add($other [, $target]): the sum, stored in $target when one is given (which is returned), else in a new bignum.
sub#
$bn->sub($other [, $target]): the difference, stored as add does.
mul#
$bn->mul($other, $ctx [, $target]): the product, stored as add does.
mod#
$bn->mod($other, $ctx [, $target]): the remainder of dividing by $other, with the sign of the number, stored as add does.
div#
$bn->div($other, $ctx [, $quotient [, $remainder]]): the quotient and the remainder, each stored in the target given for it (which is returned) or a new bignum.
sqr#
$bn->sqr($ctx): the square, a new bignum.
mod_mul#
$bn->mod_mul($other, $modulus, $ctx): the product modulo $modulus, a new bignum.
mod_exp#
$bn->mod_exp($exponent, $modulus, $ctx): the power modulo $modulus, a new bignum.
exp#
$bn->exp($exponent, $ctx): the power, a new bignum.
gcd#
$bn->gcd($other, $ctx): the greatest common divisor, a new bignum.
mod_inverse#
$bn->mod_inverse($modulus, $ctx): the inverse modulo $modulus, a new bignum; croaks when there is none.
rshift#
$bn->rshift($n): the number shifted right by $n bits, a new bignum.
lshift#
$bn->lshift($n): the number shifted left by $n bits, a new bignum.
swap#
$bn->swap($other): exchange the two numbers’ values.
copy#
$bn->copy: a new bignum of the same value.
pointer_copy#
$bn->pointer_copy: the address of a new BIGNUM of the same value, which the receiver owns - how Crypt::OpenSSL::RSA takes key parameters.
Comparison#
is_zero#
$bn->is_zero: 1 when the number is 0, else 0.
is_one#
$bn->is_one: 1 when the number is 1, else 0.
is_odd#
$bn->is_odd: 1 when the number is odd, else 0.
equals#
$bn->equals($other): 1 when the two are equal, else 0.
cmp#
$bn->cmp($other): -1, 0 or 1 as the number is less than, equal to or greater than $other.
ucmp#
$bn->ucmp($other): cmp of the absolute values.
num_bits#
$bn->num_bits: the number of significant bits.
num_bytes#
$bn->num_bytes: the number of bytes to_bin returns.