Crypt::OpenSSL::Bignum - OpenSSL's multiprecision integer arithmetic
| Use Case | Command | Description |
|---|---|---|
| 🔢 Create from decimal | Crypt::OpenSSL::Bignum->new_from_decimal($decimal_string) | Bignum from decimal string |
| 🔢 Create from hex | Crypt::OpenSSL::Bignum->new_from_hex($hex_string) | Bignum from hex string (no leading 0x) |
| 🔢 Create from binary | Crypt::OpenSSL::Bignum->new_from_bin($bin_buffer) | Bignum from packed binary |
| ➕ Addition | $bn->add($bn_b) | Sum of two bignums |
| ➖ Subtraction | $bn->sub($bn_b) | Difference of two bignums |
| ✖️ Multiplication | $bn->mul($bn_b, $ctx) | Product (requires CTX) |
| ➗ Division | $bn->div($bn_b, $ctx) | Returns quotient and remainder |
| 🔢 Modular exponentiation | $bn->mod_exp($exp, $mod, $ctx) | ($self ** $exp) % $mod |
| 🔢 Modular inverse | $bn->mod_inverse($n, $ctx) | Inverse modulo $n |
| ⚖️ Comparison | $bn->cmp($bn_b) | Returns -1, 0, or 1 |
| 🔀 Random number | Crypt::OpenSSL::Bignum->rand($bits, $top, $bottom) | Cryptographically strong random |
| 🔀 Pseudo-random | Crypt::OpenSSL::Bignum->pseudo_rand($bits, $top, $bottom) | Non-cryptographic random |
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($decimal_string) — Create a new Crypt::OpenSSL::Bignum object whose value is specified by the given decimal representation.
my $bn = Crypt::OpenSSL::Bignum->new_from_decimal($decimal_string);
new_from_hex($hex_string) — Create a new Crypt::OpenSSL::Bignum object whose value is specified by the given hexadecimal representation (no leading '0x').
my $bn = Crypt::OpenSSL::Bignum->new_from_hex($hex_string);
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.
my $bn = Crypt::OpenSSL::Bignum->new_from_word($unsigned_integer);
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.
my $bn = Crypt::OpenSSL::Bignum->new_from_bin($bin_buffer);
new — Returns a new Crypt::OpenSSL::Bignum object representing 0.
my $bn = Crypt::OpenSSL::Bignum->new;
zero — Returns a new Crypt::OpenSSL::Bignum object representing 0 (same as new).
my $bn = Crypt::OpenSSL::Bignum->zero;
one — Returns a new Crypt::OpenSSL::Bignum object representing 1.
my $bn = Crypt::OpenSSL::Bignum->one;
rand($bits, $top, $bottom) — Generates a cryptographically strong pseudo-random number of $bits bits in length. $top controls the most significant bits: -1 allows zero MSB, 0 sets it to 1, 1 sets the two MSBs to 1. $bottom if true forces the number to be odd.
my $bn = Crypt::OpenSSL::Bignum->rand($bits, $top, $bottom);
pseudo_rand($bits, $top, $bottom) — Same as rand but numbers are not necessarily unpredictable. Suitable for non-cryptographic purposes.
my $bn = Crypt::OpenSSL::Bignum->pseudo_rand($bits, $top, $bottom);
rand_range($bn_range) — Generates a cryptographically strong pseudo-random number in the range 0 <= rnd < range.
my $bn = Crypt::OpenSSL::Bignum->rand_range($bn_range);
bless_pointer($BIGNUM_ptr) — Intended for XSUB writers. Given a pointer to an OpenSSL BIGNUM object, wraps it in a Crypt::OpenSSL::Bignum object. The underlying BIGNUM will be freed when the Perl object is destroyed.
my $bn = Crypt::OpenSSL::Bignum->bless_pointer($BIGNUM_ptr);
to_decimal — Return a decimal string representation.
my $decimal_string = $self->to_decimal;
to_hex — Return a hexadecimal string representation.
my $hex_string = $self->to_hex;
to_bin — Return a packed binary string representation. Sign is ignored.
my $bin_buffer = $self->to_bin;
get_word — Return a scalar integer if representable as unsigned long.
my $unsigned_int = $self->get_word;
is_zero — Returns true if this object represents 0.
my $bool = $self->is_zero;
is_one — Returns true if this object represents 1.
my $bool = $self->is_one;
is_odd — Returns true if this object represents an odd number.
my $bool = $self->is_odd;
add($bn_b [, $result_bn]) — Addition. If only one argument, returns new object; otherwise stores result in $result_bn.
my $new_bn_object = $self->add($bn_b);
# or
$self->add($bn_b, $result_bn);
sub($bn_b [, $result_bn]) — Subtraction. Same calling convention as add.
my $new_bn_object = $self->sub($bn_b);
# or
$self->sub($bn_b, $result_bn);
mul($bn_b, $ctx [, $result_bn]) — Multiplication. Requires a Crypt::OpenSSL::Bignum::CTX object as scratchpad.
my $new_bn_object = $self->mul($bn_b, $ctx);
# or
$self->mul($bn_b, $ctx, $result_bn);
div($bn_b, $ctx [, $quotient, $remainder]) — Division. Returns list of quotient and remainder. If only two arguments, new objects are created.
my ($quotient, $remainder) = $self->div($bn_b, $ctx);
# or
$self->div($bn_b, $ctx, $quotient, $remainder);
mod($bn_b, $ctx [, $remainder]) — Modulo operation.
my $remainder = $self->mod($bn_b, $ctx);
# or
$self->mod($bn_b, $ctx, $remainder);
sqr($ctx) — Square of the number. Returns a new object; $self is not modified.
my $new_bn_object = $self->sqr($ctx);
exp($bn_exp, $ctx) — Exponentiation. Returns $self ** $bn_exp.
my $new_bn_object = $self->exp($bn_exp, $ctx);
mod_exp($bn_exp, $bn_mod, $ctx) — Modular exponentiation: ($self ** $bn_exp) % $bn_mod.
my $new_bn_object = $self->mod_exp($bn_exp, $bn_mod, $ctx);
mod_mul($bn_b, $bn_mod, $ctx) — Modular multiplication: ($self * $bn_b) % $bn_mod.
my $new_bn_object = $self->mod_mul($bn_b, $bn_mod, $ctx);
mod_inverse($bn_n, $ctx) — Computes the modular inverse of $self modulo $bn_n.
my $new_bn_object = $self->mod_inverse($bn_n, $ctx);
gcd($bn_b, $ctx) — Computes the greatest common divisor of $self and $bn_b.
my $new_bn_object = $self->gcd($bn_b, $ctx);
cmp($bn_b) — Comparison. Returns -1 if self < bn_b, 0 if equal, 1 if self > bn_b.
my $result = $self->cmp($bn_b);
ucmp($bn_b) — Comparison using absolute values. Returns -1, 0, or 1.
my $result = $self->ucmp($bn_b);
equals($bn_b) — Returns 1 if self == bn_b, 0 otherwise.
my $result = $self->equals($bn_b);
num_bits — Returns the number of significant bits. For 0x00000432, returns 11.
my $bits = $self->num_bits;
num_bytes — Returns the size of binary representation in bytes.
my $bytes = $self->num_bytes;
rshift($n) — Right shift by $n bits. Returns a new object.
my $new_bn_object = $self->rshift($n);
lshift($n) — Left shift by $n bits. Returns a new object.
my $new_bn_object = $self->lshift($n);
swap($bn_b) — Exchanges the values of two Crypt::OpenSSL::Bignum objects.
$bn_a->swap($bn_b);
$bn_b->swap($bn_a);
copy — Returns a copy of this object.
my $new_bn_object = $self->copy;
pointer_copy($BIGNUM_ptr) — Intended for XSUB writers. Returns a copy of the underlying BIGNUM pointer. The caller is responsible for freeing the copied BIGNUM.
my $cloned_BIGNUM_ptr = $self->pointer_copy($BIGNUM_ptr);
Ian Robertson, iroberts AT cpan.org
Generated by phpman v4.9.26-5-g7740029 · Markdown · JSON · MCP Author: Che Dong Under GNU General Public License
2026-08-22 05:45 @216.73.216.206
CrawledBy Mozilla/5.0 AppleWebKit/537.36 (KHTML, like Gecko; compatible; ClaudeBot/1.0; +claudebot@anthropic.com)