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 anotherCrypt::GCrypt::MPI. (and honestly? it would also be nice to be able to initialize it with aMath::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, theformatparameter suggests how to convert that string. check out CONVERSION FORMATS for the available formats. defaults toCrypt::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" ๐
๐จ๏ธ print($format)
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 asFMT_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 asFMT_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 AND LICENSE ๐
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
- ubuntu stonking: libcrypt-gcrypt-perl 1.26-7build2 ยท Artistic-1.0-Perl OR GPL-1.0-or-later
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms