Crypt:...t::MPI(3pm)

๐Ÿ’– Crypt::GCrypt::MPI ๐Ÿ’–

okay so bestie, welcome to Crypt::GCrypt::MPI โœจ โ€” a Perl interface to multi-precision integers from the GNU Cryptographic library. yes it's the perl docs page, yes it's iconic, yes we're doing this together ๐Ÿซถ


๐ŸŒˆ SYNOPSIS ๐ŸŒˆ

use Crypt::GCrypt::MPI;

my $mpi = Crypt::GCrypt::MPI->new();

๐Ÿฆ‹ ABSTRACT ๐Ÿฆ‹

Crypt::GCrypt::MPI gives you an object interface to multi-precision integers, straight from the C libgcrypt library. it's giving "big numbers but make them an object" ๐Ÿ’… no cap.


โœจ BASIC OPERATIONS โœจ

๐ŸŽ€ new()

Create a new multi-precision integer. slay.

my $mpi = Crypt::GCrypt::MPI::new(
  secure => 1,
  value => 20,
);

No parameters are required, she's low maintenance like that. If you pass only one parameter, it gets treated as the value parameter. Here's what you can hand her:

  • value โ€” the initial value of the MPI. This can be an integer, a string, or another Crypt::GCrypt::MPI. (and honestly? it would also be nice to be able to initialize it with a Math::Int. that's the dream ๐Ÿฅฒ)
  • secure โ€” if this parameter evaluates to non-zero, initialize the MPI using secure memory, if possible. ๐Ÿ”
  • format โ€” if the value is a string, the format parameter suggests how to convert that string. check out CONVERSION FORMATS for the available formats. defaults to Crypt::GCrypt::MPI::FMT_STD. ๐Ÿ™ˆ

๐Ÿ” set()

Copies the value of the other Crypt::GCrypt::MPI object.

$mpi->set($othermpi);

๐Ÿ”„ swap()

Exchanges the value with the value of another Crypt::GCrpyt::MPI object (yes that's exactly how it's spelled up in here, we don't gatekeep):

$mpi->swap($othermpi);

๐Ÿ” is_secure()

Returns true if the Crypt::GCrypt::MPI uses secure memory, where possible. vibe check on the security ๐Ÿ•ต๏ธโ™€๏ธ

โš–๏ธ cmp($other)

Compares this object against another Crypt::GCrypt::MPI object, returning 0 if the two values are equal, positive if this value is greater, and negative if $other is greater.

๐Ÿงฌ mutually_prime($other)

Compares this object against another Crypt::GCrypt::MPI object, returning true only if the two values share no factors in common other than 1. iykyk ๐Ÿงš

๐Ÿ“‹ copy()

Returns a new Crypt::GCrypt::MPI object with contents identical to this one. This is different from using the assignment operator (=), which just makes two references to the same object. For example:

 $b = new Crypt::GCrypt::MPI(15);
 $a = $b;
 $b->add(1); # $a points to the same object,
             # so both $a and $b contain 16.

 $a = $b->copy(); # $a and $b are both 16, but
                  # different objects; no risk of
                  # double-free.
 $b->add(1); # $a == 16, $b == 17

If $b is a Crypt::GCrypt::MPI object, then $a = $b->copy(); is identical to $a = Crypt::GCrypt::MPI->new($b); โ€” same result, two routes, both valid ๐Ÿ’–


๐Ÿงฎ CALCULATIONS ๐Ÿงฎ

Heads up bestie: all calculation operations modify the object they're called on and return that same object, so you can chain them like this:

$g->addm($a, $m)->mulm($b, $m)->gcd($x);

If you don't want an operation to affect the initial object, use the copy() operator first:

$h = $g->copy()->addm($a, $m)->mulm($b, $m)->gcd($x);

โž• add($other)

Adds the value of $other to this MPI.

โž• addm($other, $modulus)

Adds the value of $other to this MPI, modulo the value of $modulus.

โž– sub($other)

Subtracts the value of $other from this MPI.

โž– subm($other, $modulus)

Subtracts the value of $other from this MPI, modulo the value of $modulus.

โœ–๏ธ mul($other)

Multiply this MPI by the value of $other.

โœ–๏ธ mulm($other, $modulus)

Multiply this MPI by the value of $other, modulo the value of $modulus.

๐Ÿš€ mul_2exp($e)

Multiply this MPI by 2 raised to the power of $e โ€” that's a leftward bitshift, icon behavior โœจ

โž— div($other)

Divide this MPI by the value of $other, leaving the integer quotient. (this is integer division, so no sad little remainders hanging around)

๐Ÿฉ mod($other)

Divide this MPI by the value of $other, leaving the integer remainder. (this is the modulus operation)

๐Ÿ’ฅ powm($other, $modulus)

Raise this MPI to the power of $other, modulo the value of $modulus.

๐Ÿ”ฎ invm($modulus)

Find the multiplicative inverse of this MPI, modulo $modulus.

๐Ÿค gcd($other)

Find the greatest common divisor of this MPI and $other. friendship math ๐Ÿ’ž


๐Ÿ‘‘ OUTPUT AND DEBUGGING ๐Ÿ‘‘

๐Ÿ› dump()

Send the MPI to the libgcrypt debugging stream. it's giving "tell me everything" ๐Ÿ’€

Return a string with the data of this MPI, in a given format. check CONVERSION FORMATS for the available formats.


๐ŸŒŸ CONVERSION FORMATS ๐ŸŒŸ

All the available printing and scanning formats live in the Crypt::GCrypt::MPI namespace, and they mean exactly the same thing as they do in gcrypt. no reinventing the wheel here ๐ŸŽก

  • FMT_STD โ€” Two's complement representation.
  • FMT_PGP โ€” Same as FMT_STD, but with a two-byte length header, as used in OpenPGP. (โš ๏ธ only works for non-negative values, so mind your signs!)
  • FMT_SSH โ€” Same as FMT_STD, but with a four-byte length header, as used by OpenSSH.
  • FMT_HEX โ€” Hexadecimal string in ASCII.
  • FMT_USG โ€” Simple unsigned integer.

๐Ÿ˜ญ BUGS AND FEEDBACK ๐Ÿ˜ญ

Crypt::GCrypt::MPI does not currently auto-convert to and from Math::BigInt objects, even though it should. (yes we're sad about it. it's giving unmet potential ๐Ÿ’”)

Other than that, there are no known bugs. You are very welcome to write mail to the maintainer (aar@cpan.org) with your contributions, comments, suggestions, bug reports or complaints. slide into those inboxes ๐Ÿซถ


๐Ÿ’œ AUTHORS AND CONTRIBUTORS ๐Ÿ’œ

  • Daniel Kahn Gillmor <dkg@fifthhorseman.net>
  • Alessandro Ranellucci <aar@cpan.org>

Copyright ยฉ Daniel Kahn Gillmor. Crypt::GCrypt::MPI is free software โ€” you may redistribute it and/or modify it under the same terms as Perl itself. periodt ๐Ÿ’…


๐ŸŽ‰ ACKNOWLEDGEMENTS ๐ŸŽ‰

This module was initially inspired by the GCrypt.pm bindings made by Robert Bihlmeyer in 2002. Thanks to users who give feedback and submit patches (see Changelog). we love a community moment ๐ŸŒธ


๐Ÿšจ DISCLAIMER ๐Ÿšจ

This software is provided by the copyright holders and contributors ``as is'' and any express or implied warranties, including, but not limited to, the implied warranties of merchantability and fitness for a particular purpose are disclaimed. In no event shall the regents or contributors be liable for any direct, indirect, incidental, special, exemplary, or consequential damages (including, but not limited to, procurement of substitute goods or services; loss of use, data, or profits; or business interruption) however caused and on any theory of liability, whether in contract, strict liability, or tort (including negligence or otherwise) arising in any way out of the use of this software, even if advised of the possibility of such damage.

bestie that's the whole legal wall, i'm just the messenger ๐Ÿงš read it, don't skim it, it's real.

and that's the whole page!! ๐Ÿ’– this was Crypt::GCrypt::MPI(3pm) from User Contributed Perl Documentation, generated by perl v5.42.3 on 2026-08-20. go forth and do big-integer math, you absolute icon ๐Ÿ‘‘โœจ

๐Ÿ“ฆ where this page comes from