Crypt::OpenSSL::Bignum β OpenSSLβs multiprecision integer arithmetic
| Use Case | Command | Description |
|---|---|---|
| Create from decimal string | Crypt::OpenSSL::Bignum->new_from_decimal(β1000β) | π New bignum from decimal |
| Create from hex string | Crypt::OpenSSL::Bignum->new_from_hex(β3e8β) | π New bignum from hex (no 0x) |
| Create from word | Crypt::OpenSSL::Bignum->new_from_word(1000) | π New bignum from unsigned integer (0..2^32-1) |
| Create from binary | Crypt::OpenSSL::Bignum->new_from_bin($packed) | π New bignum from packed binary |
| Create random number | Crypt::OpenSSL::Bignum->rand($bits, $top, $bottom) | π² Cryptographically strong random |
| Addition | $a->add($b) or $a->add($b, $result) | β Add two bignums |
| Subtraction | $a->sub($b) or $a->sub($b, $result) | β Subtract two bignums |
| Multiplication | $a->mul($b, $ctx) or $a->mul($b, $ctx, $result) | βοΈ Multiply two bignums |
| Division | $a->div($b, $ctx) or $a->div($b, $ctx, $q, $r) | β Division with remainder |
| Modular exponentiation | $a->mod_exp($exp, $mod, $ctx) | π’ Compute $a^$exp mod $mod |
| Modular inverse | $a->mod_inverse($n, $ctx) | π Inverse modulo $n |
| GCD | $a->gcd($b, $ctx) | π Greatest common divisor |
| Comparison | $a->cmp($b) | π Compare (returns -1, 0, 1) |
| Bit shift left | $a->lshift($n) | β¬ οΈ Left shift by $n bits |
| Bit shift right | $a->rshift($n) | β‘οΈ Right shift by $n bits |
use Crypt::OpenSSL::Bignum;
my $bn = Crypt::OpenSSL::Bignum->new_from_decimal( "1000" );
# or
my $bn = Crypt::OpenSSL::Bignum->new_from_word( 1000 );
# or
my $bn = Crypt::OpenSSL::Bignum->new_from_hex("3e8"); # no leading 0x
# or
my $bn = Crypt::OpenSSL::Bignum->new_from_bin(pack( "C*", 3, 232 ))
use Crypt::OpenSSL::Bignum::CTX;
sub print_factorial
{
my( $n ) = @_;
my $fac = Crypt::OpenSSL::Bignum->one();
my $ctx = Crypt::OpenSSL::Bignum::CTX->new();
foreach my $i (1 .. $n)
{
$fac->mul( Crypt::OpenSSL::Bignum->new_from_word( $i ), $ctx, $fac );
}
print "$n factorial is ", $fac->to_decimal(), "\n";
}
Crypt::OpenSSL::Bignum provides access to OpenSSL multiprecision integer arithmetic libraries. Presently, many though not all of the arithmetic operations that OpenSSL provides are exposed to perl. In addition, this module can be used to provide access to bignum values produced by other OpenSSL modules, such as key parameters from Crypt::OpenSSL::RSA.
β οΈ NOTE: Many of the methods in this package can croak, so use eval, or Error.pmβs try/catch mechanism to capture errors.
new_from_decimal
my $bn = Crypt::OpenSSL::Bignum->new_from_decimal($decimal_string);
π Create a new Crypt::OpenSSL::Bignum object whose value is specified by the given decimal representation.
new_from_hex
my $bn = Crypt::OpenSSL::Bignum->new_from_hex($hex_string); #no leading '0x'
π Create a new Crypt::OpenSSL::Bignum object whose value is specified by the given hexadecimal representation.
new_from_word
my $bn = Crypt::OpenSSL::Bignum->new_from_word($unsigned_integer);
π Create a new Crypt::OpenSSL::Bignum object whose value will be the word given. Note that numbers represented by objects created using this method are necessarily between 0 and 2^32 β 1.
new_from_bin
my $bn = Crypt::OpenSSL::Bignum->new_from_bin($bin_buffer);
π Create a new Crypt::OpenSSL::Bignum object whose value is specified by the given packed binary string (created by to_bin). Note that objects created using this method are necessarily nonnegative.
new
my $bn = Crypt::OpenSSL::Bignum->new;
π Returns a new Crypt::OpenSSL::Bignum object representing 0.
zero
my $bn = Crypt::OpenSSL::Bignum->zero;
π Returns a new Crypt::OpenSSL::Bignum object representing 0 (same as new).
one
my $bn = Crypt::OpenSSL::Bignum->one;
π Returns a new Crypt::OpenSSL::Bignum object representing 1.
rand
my $bn = Crypt::OpenSSL::Bignum->rand($bits, $top, $bottom)
# $bits, $top, $bottom are integers
π² Generates a cryptographically strong pseudo-random number of $bits bits in length. If $top is -1, the most significant bit can be zero; if 0, it is set to 1; if 1, the two most significant bits are set to 1. If $bottom is true, the number will be odd.
pseudo_rand
my $bn = Crypt::OpenSSL::Bignum->pseudo_rand($bits, $top, $bottom)
# $bits, $top, $bottom are integers
π² Does the same as rand, but pseudo-random numbers generated by this function are not necessarily unpredictable. They can be used for non-cryptographic purposes.
rand_range
my $bn = Crypt::OpenSSL::Bignum->rand_range($bn_range)
π² Generates a cryptographically strong pseudo-random number rnd in the range 0 <= rnd < $bn_range.
bless_pointer
my $bn = Crypt::OpenSSL::Bignum->bless_pointer($BIGNUM_ptr)
π§ Given a pointer to an OpenSSL BIGNUM object in memory, construct and return a Crypt::OpenSSL::Bignum object around it. The underlying BIGNUM will be destroyed when the returned object is no longer referenced. Intended only for XSUB writers.
to_decimal
my $decimal_string = $self->to_decimal;
π’ Return a decimal string representation of this object.
to_hex
my $hex_string = $self->to_hex;
π’ Return a hexadecimal string representation of this object.
to_bin
my $bin_buffer = $self->to_bin;
π’ Return a packed binary string representation. Sign is ignored (negative numbers return the absolute valueβs binary).
get_word
my $unsigned_int = $self->get_word;
π’ Return a scalar integer representation if it can be represented as an unsigned long.
is_zero
my $bool = $self->is_zero;
β Returns true if this object represents 0.
is_one
my $bool = $self->is_one;
β Returns true if this object represents 1.
is_odd
my $bool = $self->is_odd;
β Returns true if this object represents an odd number.
add
my $new_bn_object = $self->add($bn_b); # $new_bn_object = $self + $bn_b
# or
$self->add($bn_b, $result_bn); # $result_bn = $self + $bn_b
β Returns the sum of this object and the first argument. If only one argument is passed, a new Crypt::OpenSSL::Bignum object is created for the return value; otherwise, the value of second argument is set to the result and returned.
sub
my $new_bn_object = $self->sub($bn_b); # $new_bn_object = $self - $bn_b
# or
$self->sub($bn_b, $result_bn); # $result_bn = $self - $bn_b
β Returns the difference of this object and the first argument. If only one argument is passed, a new Crypt::OpenSSL::Bignum object is created for the return value; otherwise, the value of second argument is set to the result and returned.
mul
my $new_bn_object = $self->mul($bn_b, $ctx); # $new_bn_object = $self * $bn_b
# or
$self->mul($bn_b, $ctx, $result_bn); # $result_bn = $self * $bn_b
βοΈ Returns the product of this object and the first argument, using the second argument, a Crypt::OpenSSL::Bignum::CTX object, as a scratchpad. If only two arguments are passed, a new Crypt::OpenSSL::Bignum object is created for the return value; otherwise, the value of third argument is set to the result and returned.
div
my ($quotient, $remainder) = $self->div($bn_b, $ctx);
# or
$self->div($bn_b, $ctx, $quotient, $remainder);
β Returns a list consisting of quotient and remainder obtained by dividing this object by the first argument, using the second argument as a scratchpad. If only two arguments are passed, new Crypt::OpenSSL::Bignum objects are created for both return values. If a third argument is passed, it is set to the quotient; if a fourth, it is set to the remainder.
mod
my $remainder = $self->mod($bn_b, $ctx);
# or
$self->mod($bn_b, $ctx, $remainder);
π Returns the remainder obtained by dividing this object by the first argument, using the second argument as a scratchpad. If a third argument is passed, it is set to the remainder.
sqr
my $new_bn_object = $self->sqr($ctx);
# new object is created; $self is not modified
π² Returns the square ($self ** 2) of this object.
exp
my $new_bn_object = $self->exp($bn_exp, $ctx);
# new object is created; $self is not modified
π’ Returns this object exponentiated by the first argument (Crypt::OpenSSL::Bignum object), using the second argument as a scratchpad.
mod_exp
my $new_bn_object = $self->mod_exp($bn_exp, $bn_mod, $ctx);
# new object is created; $self is not modified
π’ Returns this object exponentiated by the first argument, modulo the second argument, using the third argument as a scratchpad.
mod_mul
my $new_bn_object = $self->mod_mul($bn_b, $bn_mod, $ctx);
# new object is created; $self is not modified
π’ Returns ($self * $bn_b) % $bn_mod, using the third argument as a scratchpad.
mod_inverse
my $new_bn_object = $self->mod_inverse($bn_n, $ctx);
# new object is created; $self is not modified
π Computes the inverse of $self modulo $bn_n and returns the result, using the second argument as a scratchpad.
gcd
my $new_bn_object = $self->gcd($bn_b, $ctx);
# new object is created; $self is not modified
π Computes the greatest common divisor of $self and $bn_b, using the second argument as a scratchpad.
cmp
my $result = $self->cmp($bn_b);
#returns:
# -1 if self < bn_b
# 0 if self == bn_b
# 1 if self > bn_b
π Comparison of values $self and $bn_b (Crypt::OpenSSL::Bignum objects).
ucmp
my $result = $self->ucmp($bn_b);
#returns:
# -1 if |self| < |bn_b|
# 0 if |self| == |bn_b|
# 1 if |self| > |bn_b|
π Comparison using the absolute values of $self and $bn_b.
equals
my $result = $self->equals($bn_b);
#returns:
# 1 if self == bn_b
# 0 otherwise
β Returns 1 if equal, 0 otherwise.
num_bits
my $bits = $self->num_bits;
π Returns the number of significant bits. For 0x00000432, returns 11 (floor(log2(w)) + 1).
num_bytes
my $bytes = $self->num_bytes;
π Returns the size of binary representation in bytes.
rshift
my $new_bn_object = $self->rshift($n);
# new object is created; $self is not modified
β‘οΈ Shifts right by $n (integer) bits and places the result into a new Crypt::OpenSSL::Bignum object.
lshift
my $new_bn_object = $self->lshift($n);
# new object is created; $self is not modified
β¬ οΈ Shifts left by $n (integer) bits and places the result into a new Crypt::OpenSSL::Bignum object.
swap
my $bn_a = Crypt::OpenSSL::Bignum->new_from_decimal("1234567890001");
my $bn_b = Crypt::OpenSSL::Bignum->new_from_decimal("1234567890002");
$bn_a->swap($bn_b);
# or
$bn_b->swap($bn_a);
π Exchanges the values of two Crypt::OpenSSL::Bignum objects.
copy
my $new_bn_object = $self->copy;
π Returns a copy of this object.
pointer_copy
my $cloned_BIGNUM_ptr = $self->pointer_copy($BIGNUM_ptr);
π§ Intended only for XSUB writers. Returns a perl scalar whose IV can be cast to a BIGNUM* value (a copy of the underlying BIGNUM). The caller is responsible for freeing the allocated BIGNUM.
Ian Robertson, iroberts AT cpan.org
Generated by phpman v4.9.26-5-g7740029 Author: Che Dong Under GNU General Public License
2026-08-14 20:49 @2600:1f28:365:80b0:4d23:66fa:c2bb:7bae
CrawledBy CCBot/2.0 (https://commoncrawl.org/faq/)