# perldoc > Crypt::OpenSSL::Bignum

---
type: CommandReference
command: Crypt::OpenSSL::Bignum
mode: perldoc
section: 
source: perldoc
---

## Quick Reference

- `Crypt::OpenSSL::Bignum->new_from_decimal("1000")` — create from decimal string
- `Crypt::OpenSSL::Bignum->new_from_hex("3e8")` — create from hex string (no 0x prefix)
- `Crypt::OpenSSL::Bignum->new_from_word(1000)` — create from unsigned integer (0..2^32-1)
- `Crypt::OpenSSL::Bignum->new_from_bin(pack("C*",3,232))` — create from packed binary
- `$bn->add($other, $result)` — addition with optional result object
- `$bn->mul($other, $ctx, $result)` — multiplication using a CTX scratchpad
- `$bn->mod_exp($exp, $mod, $ctx)` — modular exponentiation
- `$bn->to_decimal()` — convert to decimal string

## Name

OpenSSL's multiprecision integer arithmetic for Perl.

## Synopsis

perl
use Crypt::OpenSSL::Bignum;
use Crypt::OpenSSL::Bignum::CTX;

# Construction
my $bn = Crypt::OpenSSL::Bignum->new_from_decimal("1000");
my $bn = Crypt::OpenSSL::Bignum->new_from_word(1000);
my $bn = Crypt::OpenSSL::Bignum->new_from_hex("3e8"); # no leading 0x
my $bn = Crypt::OpenSSL::Bignum->new_from_bin(pack("C*", 3, 232));

# Example: factorial
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";
}
## Methods

### Constructors

- `new_from_decimal($decimal_string)` — create from decimal string representation.
- `new_from_hex($hex_string)` — create from hex string (no leading `0x`).
- `new_from_word($unsigned_integer)` — create from unsigned integer (0..2^32-1).
- `new_from_bin($bin_buffer)` — create from packed binary string (nonnegative).
- `new` — returns a new object representing 0.
- `zero` — same as `new`.
- `one` — returns a new object representing 1.
- `rand($bits, $top, $bottom)` — cryptographically strong pseudo-random number of `$bits` bits. `$top` controls MSB; `$bottom` true makes it odd.
- `pseudo_rand($bits, $top, $bottom)` — same as `rand` but not necessarily unpredictable.
- `rand_range($bn_range)` — cryptographically strong random number in [0, range).
- `bless_pointer($BIGNUM_ptr)` — wrap a raw OpenSSL BIGNUM pointer (for XSUB authors).

### Instance Methods

- `to_decimal` — decimal string representation.
- `to_hex` — hex string representation.
- `to_bin` — packed binary string (ignores sign).
- `get_word` — scalar integer if representable as unsigned long.
- `is_zero` — true if 0.
- `is_one` — true if 1.
- `is_odd` — true if odd.
- `add($bn_b, [$result])` — returns `$self + $bn_b`; optional result object.
- `sub($bn_b, [$result])` — returns `$self - $bn_b`; optional result object.
- `mul($bn_b, $ctx, [$result])` — returns `$self * $bn_b` using CTX scratchpad.
- `div($bn_b, $ctx, [$quotient, $remainder])` — returns list of quotient and remainder.
- `mod($bn_b, $ctx, [$remainder])` — returns remainder of `$self / $bn_b`.
- `sqr($ctx)` — returns `$self ** 2` (new object).
- `exp($bn_exp, $ctx)` — returns `$self ** $bn_exp` (new object).
- `mod_exp($bn_exp, $bn_mod, $ctx)` — returns `$self ** $bn_exp % $bn_mod`.
- `mod_mul($bn_b, $bn_mod, $ctx)` — returns `($self * $bn_b) % $bn_mod`.
- `mod_inverse($bn_n, $ctx)` — multiplicative inverse of `$self` modulo `$bn_n`.
- `gcd($bn_b, $ctx)` — greatest common divisor of `$self` and `$bn_b`.
- `cmp($bn_b)` — returns -1, 0, 1 for less, equal, greater.
- `ucmp($bn_b)` — same using absolute values.
- `equals($bn_b)` — returns 1 if equal, 0 otherwise.
- `num_bits` — number of significant bits (floor(log2(w))+1).
- `num_bytes` — size of binary representation in bytes.
- `rshift($n)` — right shift by `$n` bits (new object).
- `lshift($n)` — left shift by `$n` bits (new object).
- `swap($bn)` — exchange values of two objects.
- `copy` — returns a copy of the object.
- `pointer_copy($BIGNUM_ptr)` — for XSUB authors; returns a copy of the underlying BIGNUM pointer.

## See Also

- [Crypt::OpenSSL::Bignum::CTX](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3AOpenSSL%3A%3ABignum%3A%3ACTX/markdown) — scratchpad context for arithmetic operations
- [Crypt::OpenSSL::RSA](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3AOpenSSL%3A%3ARSA/markdown) — RSA encryption using OpenSSL
- [OpenSSL BN documentation](https://www.openssl.org/docs/crypto/bn.html) — underlying C library