{
    "mode": "perldoc",
    "parameter": "Crypt::OpenSSL::Bignum",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3AOpenSSL%3A%3ABignum/json",
    "generated": "2026-06-09T21:13:17Z",
    "synopsis": "use Crypt::OpenSSL::Bignum;\nmy $bn = Crypt::OpenSSL::Bignum->newfromdecimal( \"1000\" );\n# or\nmy $bn = Crypt::OpenSSL::Bignum->newfromword( 1000 );\n# or\nmy $bn = Crypt::OpenSSL::Bignum->newfromhex(\"3e8\"); # no leading 0x\n# or\nmy $bn = Crypt::OpenSSL::Bignum->newfrombin(pack( \"C*\", 3, 232 ))\nuse Crypt::OpenSSL::Bignum::CTX;\nsub printfactorial\n{\nmy( $n ) = @;\nmy $fac = Crypt::OpenSSL::Bignum->one();\nmy $ctx = Crypt::OpenSSL::Bignum::CTX->new();\nforeach my $i (1 .. $n)\n{\n$fac->mul( Crypt::OpenSSL::Bignum->newfromword( $i ), $ctx, $fac );\n}\nprint \"$n factorial is \", $fac->todecimal(), \"\\n\";\n}",
    "sections": {
        "NAME": {
            "content": "Crypt::OpenSSL::Bignum - OpenSSL's multiprecision integer arithmetic\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Crypt::OpenSSL::Bignum;\n\nmy $bn = Crypt::OpenSSL::Bignum->newfromdecimal( \"1000\" );\n# or\nmy $bn = Crypt::OpenSSL::Bignum->newfromword( 1000 );\n# or\nmy $bn = Crypt::OpenSSL::Bignum->newfromhex(\"3e8\"); # no leading 0x\n# or\nmy $bn = Crypt::OpenSSL::Bignum->newfrombin(pack( \"C*\", 3, 232 ))\n\nuse Crypt::OpenSSL::Bignum::CTX;\n\nsub printfactorial\n{\nmy( $n ) = @;\nmy $fac = Crypt::OpenSSL::Bignum->one();\nmy $ctx = Crypt::OpenSSL::Bignum::CTX->new();\nforeach my $i (1 .. $n)\n{\n$fac->mul( Crypt::OpenSSL::Bignum->newfromword( $i ), $ctx, $fac );\n}\nprint \"$n factorial is \", $fac->todecimal(), \"\\n\";\n}\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Crypt::OpenSSL::Bignum provides access to OpenSSL multiprecision integer arithmetic libraries.\nPresently, many though not all of the arithmetic operations that OpenSSL provides are exposed to\nperl. In addition, this module can be used to provide access to bignum values produced by other\nOpenSSL modules, such as key parameters from Crypt::OpenSSL::RSA.\n\n*NOTE*: Many of the methods in this package can croak, so use eval, or Error.pm's try/catch\nmechanism to capture errors.\n",
            "subsections": []
        },
        "Constructors": {
            "content": "newfromdecimal\nmy $bn = Crypt::OpenSSL::Bignum->newfromdecimal($decimalstring);\n\nCreate a new Crypt::OpenSSL::Bignum object whose value is specified by the given decimal\nrepresentation.\n\nnewfromhex\nmy $bn = Crypt::OpenSSL::Bignum->newfromhex($hexstring); #no leading '0x'\n\nCreate a new Crypt::OpenSSL::Bignum object whose value is specified by the given hexadecimal\nrepresentation.\n\nnewfromword\nmy $bn = Crypt::OpenSSL::Bignum->newfromword($unsignedinteger);\n\nCreate a new Crypt::OpenSSL::Bignum object whose value will be the word given. Note that\nnumbers represented by objects created using this method are necessarily between 0 and 2^32\n- 1.\n\nnewfrombin\nmy $bn = Crypt::OpenSSL::Bignum->newfrombin($binbuffer);\n\nCreate a new Crypt::OpenSSL::Bignum object whose value is specified by the given packed\nbinary string (created by \"tobin\"). Note that objects created using this method are\nnecessarily nonnegative.\n\nnew\nmy $bn = Crypt::OpenSSL::Bignum->new;\n\nReturns a new Crypt::OpenSSL::Bignum object representing 0\n\nzero\nmy $bn = Crypt::OpenSSL::Bignum->zero;\n\nReturns a new Crypt::OpenSSL::Bignum object representing 0 (same as new)\n\none\nmy $bn = Crypt::OpenSSL::Bignum->one;\n\nReturns a new Crypt::OpenSSL::Bignum object representing 1\n\nrand\nmy $bn = Crypt::OpenSSL::Bignum->rand($bits, $top, $bottom)\n# $bits, $top, $bottom are integers\n\ngenerates a cryptographically strong pseudo-random number of bits bits in length and stores\nit in rnd. If top is -1, the most significant bit of the random number can be zero. If top\nis 0, it is set to 1, and if top is 1, the two most significant bits of the number will be\nset to 1, so that the product of two such random numbers will always have 2*bits length. If\nbottom is true, the number will be odd.\n\npseudorand\nmy $bn = Crypt::OpenSSL::Bignum->pseudorand($bits, $top, $bottom)\n# $bits, $top, $bottom are integers\n\ndoes the same, but pseudo-random numbers generated by this function are not necessarily\nunpredictable. They can be used for non-cryptographic purposes and for certain purposes in\ncryptographic protocols, but usually not for key generation etc.\n\nrandrange\nmy $bn = Crypt::OpenSSL::Bignum->randrange($bnrange)\n\ngenerates a cryptographically strong pseudo-random number rnd in the range 0 <lt>= rnd <\nrange. BNpseudorandrange() does the same, but is based on BNpseudorand(), and hence\nnumbers generated by it are not necessarily unpredictable.\n\nblesspointer\nmy $bn = Crypt::OpenSSL::Bignum->blesspointer($BIGNUMptr)\n\nGiven a pointer to a OpenSSL BIGNUM object in memory, construct and return\nCrypt::OpenSSL::Bignum object around this. Note that the underlying BIGNUM object will be\ndestroyed (via BNclearfree(3ssl)) when the returned Crypt::OpenSSL::Bignum object is no\nlonger referenced, so the pointer passed to this method should only be referenced via the\nreturned perl object after calling blesspointer.\n\nThis method is intended only for use by XSUB writers writing code that interfaces with\nOpenSSL library methods, and who wish to be able to return a BIGNUM structure to perl as a\nCrypt::OpenSSL::Bignum object.\n",
            "subsections": []
        },
        "Instance Methods": {
            "content": "todecimal\nmy $decimalstring = $self->todecimal;\n\nReturn a decimal string representation of this object.\n\ntohex\nmy $hexstring = $self->tohex;\n\nReturn a hexadecimal string representation of this object.\n\ntobin\nmy $binbuffer = $self->tobin;\n\nReturn a packed binary string representation of this object. Note that sign is ignored, so\nthat to bin called on a Crypt::OpenSSL::Bignum object representing a negative number returns\nthe same value as it would called on an object representing that number's absolute value.\n\ngetword\nmy $unsignedint = $self->getword;\n\nReturn a scalar integer representation of this object, if it can be represented as an\nunsigned long.\n\niszero\nmy $bool = $self->iszero;\n\nReturns true of this object represents 0.\n\nisone\nmy $bool = $self->isone;\n\nReturns true of this object represents 1.\n\nisodd\nmy $bool = $self->isodd;\n\nReturns true of this object represents an odd number.\n\nadd\nmy $newbnobject = $self->add($bnb); # $newbnobject = $self + $bnb\n# or\n$self->add($bnb, $resultbn);         # $resultbn = $self + $bnb\n\nThis method returns the sum of this object and the first argument. If only one argument is\npassed, a new Crypt::OpenSSL::Bignum object is created for the return value; otherwise, the\nvalue of second argument is set to the result and returned.\n\nsub\nmy $newbnobject = $self->sub($bnb); # $newbnobject = $self - $bnb\n# or\n$self->sub($bnb, $resultbn);         # $resultbn = $self - $bnb\n\nThis method returns the difference of this object and the first argument. If only one\nargument is passed, a new Crypt::OpenSSL::Bignum object is created for the return value;\notherwise, the value of second argument is set to the result and returned.\n\nmul\nmy $newbnobject = $self->mul($bnb, $ctx); # $newbnobject = $self * $bnb\n# or\n$self->mul($bnb, $ctx, $resultbn);         # $resultbn = $self * $bnb\n\nThis method returns the product of this object and the first argument, using the second\nargument, a Crypt::OpenSSL::Bignum::CTX object, as a scratchpad. If only two arguments are\npassed, a new Crypt::OpenSSL::Bignum object is created for the return value; otherwise, the\nvalue of third argument is set to the result and returned.\n\ndiv\nmy ($quotient, $remainder) = $self->div($bnb, $ctx);\n# or\n$self->div($bnb, $ctx, $quotient, $remainder);\n\nThis method returns a list consisting of quotient and the remainder obtained by dividing\nthis object by the first argument, using the second argument, a Crypt::OpenSSL::Bignum::CTX\nobject, as a scratchpad. If only two arguments are passed, new Crypt::OpenSSL::Bignum\nobjects are created for both return values. If a third argument is passed, otherwise, the\nvalue of third argument is set to the quotient. If a fourth argument is passed, the value of\nthe fourth argument is set to the remainder.\n\nmod\nmy $remainder = $self->mod($bnb, $ctx);\n# or\n$self->mod($bnb, $ctx, $remainder);\n\nThis method returns the remainder obtained by dividing this object by the first argument, a\nCrypt::OpenSSL::Bignum::CTX object, as a scratchpad. Crypt::OpenSSL::Bignum object is\ncreated for the return value. If a third argument is passed, the value of third argument is\nset to the remainder.\n\nsqr\nmy $newbnobject = $self->sqr($ctx);\n# new object is created $self is not modified\n\nThis method returns the square (\"$self  2\") of Crypt::OpenSSL::Bignum object.\n\nexp\nmy $newbnobject = $self->exp($bnexp, $ctx);\n# new object is created $self is not modified\n\nThis method returns the product of this object exponentiated by the first argument\n(Crypt::OpenSSL::Bignum object), using the second argument, a Crypt::OpenSSL::Bignum::CTX\nobject, as a scratchpad.\n\nmodexp\nmy $newbnobject = $self->expmod($bnexp, $bnmod, $ctx);\n# new object is created $self is not modified\n\nThis method returns the product of this object exponentiated by the first argument\n(Crypt::OpenSSL::Bignum object), modulo the second argument (also Crypt::OpenSSL::Bignum\nobject), using the third argument, a Crypt::OpenSSL::Bignum::CTX object, as a scratchpad.\n\nmodmul\nmy $newbnobject = $self->modmul($bnb, $bnmod, $ctx);\n# new object is created $self is not modified\n\nThis method returns \"($self * $bnb) % $bnmod\", using the third argument, a\nCrypt::OpenSSL::Bignum::CTX object, as a scratchpad.\n\nmodinverse\nmy $newbnobject = $self->modinverse($bnn, $ctx);\n# new object is created $self is not modified\n\nComputes the inverse of $self modulo $bnn and returns the result in a new\nCrypt::OpenSSL::Bignum object, using the second argument, a Crypt::OpenSSL::Bignum::CTX\nobject, as a scratchpad.\n\ngcd\nmy $newbnobject = $self->gcd($bnb, $ctx);\n# new object is created $self is not modified\n\nComputes the greatest common divisor of $self and $bnb and returns the result in a new\nCrypt::OpenSSL::Bignum object, using the second argument, a Crypt::OpenSSL::Bignum::CTX\nobject, as a scratchpad.\n\ncmp\nmy $result = $self->cmp($bnb);\n#returns:\n# -1 if self <  bnb\n#  0 if self == bnb\n#  1 if self >  bnb\n\nComparison of values $self and $bnb (Crypt::OpenSSL::Bignum objects).\n\nucmp\nmy $result = $self->ucmp($bnb);\n#returns:\n# -1 if |self| <  |bnb|\n#  0 if |self| == |bnb|\n#  1 if |self| >  |bnb|\n\nComparison using the absolute values of $self and $bnb (Crypt::OpenSSL::Bignum objects).\n\nequals\nmy $result = $self->equals($bnb);\n#returns:\n# 1 if self == bnb\n# 0 otherwise\n\nnumbits\nmy $bits = $self->numbits;\n\nReturns the number of significant bits in a word. If we take 0x00000432 as an example, it\nreturns 11, not 16, not 32. Basically, except for a zero, it returns \"floor(log2(w)) + 1\".\n\nnumbytes\nmy $bytes = $self->numbytes;\n\nReturns the size of binary represenatation in bytes.\n\nrshift\nmy $newbnobject = $self->rshift($n);\n# new object is created $self is not modified\n\nShifts a right by $n (integer) bits and places the result into a newly created\nCrypt::OpenSSL::Bignum object.\n\nlshift\nmy $newbnobject = $self->lshift($n);\n# new object is created $self is not modified\n\nShifts a left by $n (integer) bits and places the result into a newly created\nCrypt::OpenSSL::Bignum object.\n\nswap\nmy $bna = Crypt::OpenSSL::Bignum->newfromdecimal(\"1234567890001\");\nmy $bnb = Crypt::OpenSSL::Bignum->newfromdecimal(\"1234567890002\");\n\n$bna->swap($bnb);\n# or\n$bnb->swap($bna);\n\nExchanges the values of two Crypt::OpenSSL::Bignum objects.\n\ncopy\nmy $newbnobject = $self->copy;\n\nReturns a copy of this object.\n\npointercopy\nmy $clonedBIGNUMptr = $self->pointercopy($BIGNUMptr);\n\nThis method is intended only for use by XSUB writers wanting to have access to the\nunderlying BIGNUM structure referenced by a Crypt::OpenSSL::Bignum perl object so that they\ncan pass them to other routines in the OpenSSL library. It returns a perl scalar whose IV\ncan be cast to a BIGNUM* value. This can then be passed to an XSUB which can work with the\nBIGNUM directly. Note that the BIGNUM object pointed to will be a copy of the BIGNUM object\nwrapped by the instance; it is thus the responsibility of the client to free space allocated\nby this BIGNUM object if and when it is done with it. See also blesspointer.\n",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Ian Robertson, iroberts@cpan.org\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "<https://www.openssl.org/docs/crypto/bn.html>\n",
            "subsections": []
        }
    },
    "summary": "Crypt::OpenSSL::Bignum - OpenSSL's multiprecision integer arithmetic",
    "flags": [],
    "examples": [],
    "see_also": []
}