Crypt::DH::GMP(3)
π NAME π
Crypt::DH::GMP β Crypt::DH Using GMP Directly
okay bestie, yes β this is the User Contributed Perl Documentation page for Crypt::DH::GMP(3), and weβre gonna get you comfy with it π
β¨
β¨ SYNOPSIS β¨
use Crypt::DH::GMP;
my $dh = Crypt::DH::GMP->new(p => $p, g => $g);
my $val = $dh->compute_secret();
# If you want compatibility with Crypt::DH (it uses Math::BigInt)
# then use this flag
# You /think/ you're using Crypt::DH, but...
use Crypt::DH::GMP qw(-compat);
my $dh = Crypt::DH->new(p => $p, g => $g);
my $val = $dh->compute_secret();
π¦ DESCRIPTION π¦
Crypt::DH::GMP is a (somewhat) portable replacement to Crypt::DH, implemented mostly in C. periodt π
πΈ RATIONALE πΈ
okay so back in the day there was Crypt::DH, but bestieβ¦ it had a couple of problems π
GMP/Pari libraries are almost always required
Crypt::DH works with a plain Math::BigInt, but if you want to use it in production, you almost always need to install Math::BigInt::GMP or Math::BigInt::Pari β because without them, the computation that is required by Crypt::DH makes the module pretty much unusable.
Because of this, Crypt::DH might as well make Math::BigInt::GMP a hard requirement.
Crypt::DH suffers from having Math::BigInt in between GMP
With or without Math::BigInt::GMP or Math::BigInt::Pari, Crypt::DH makes several round trip conversions between Perl scalars, Math::BigInt objects, and finally its C representation (if GMP/Pari are installed).
Instantiating an object comes with a relatively high cost, and if you make many computations in one go, your program will suffer dramatically because of this.
These problems quickly become apparent when you use modules such as Net::OpenID::Consumer, which requires to make a few calls to Crypt::DH.
Crypt::DH::GMP attempts to alleviate these problems by providing a Crypt::DH-compatible layer, which, instead of doing calculations via Math::BigInt, directly works with libgmp in C.
This means that weβve essentially eliminated 2 call stacks worth of expensive Perl method calls and we also only load 1 (Crypt::DH::GMP) module instead of 3 (Crypt::DH + Math::BigInt + Math::BigInt::GMP).
These add up to a fairly significant increase in performance. slay π₯
π COMPATIBILITY WITH Crypt::DH π
Crypt::DH::GMP absolutely refuses to consider using anything other than strings as its parameters and/or return values β therefore if you would like to use Math::BigInt objects as your return values, you can not use Crypt::DH::GMP directly. Instead, you need to be explicit about it:
use Crypt::DH;
use Crypt::DH::GMP qw(-compat); # must be loaded AFTER Crypt::DH
Specifying -compat invokes a very nasty hack that overwrites Crypt::DHβs symbol table β this then forces Crypt::DH users to use Crypt::DH::GMP instead, even if you are writing
my $dh = Crypt::DH->new(...);
$dh->compute_key();
π«Ά yes bestie, that is as chaotic as it sounds β the original says βvery nasty hack,β and I am not gonna pretend otherwise π
π₯ BENCHMARK π₯
By NO MEANS is this an exhaustive benchmark, but hereβs what I get on my MacBook (OS X 10.5.8, 2.4 GHz Core 2 Duo, 4GB RAM)
Benchmarking instatiation cost...
Rate pp gmp
pp 9488/s -- -79%
gmp 45455/s 379% --
Benchmarking key generation cost...
Rate gmp pp
gmp 6.46/s -- -0%
pp 6.46/s 0% --
Benchmarking compute_key cost...
Rate pp gmp
pp 12925/s -- -96%
gmp 365854/s 2730% --
π METHODS π
hereβs the method roster, bestie π«Ά
new
p
g
compute_key
compute_secret
generate_keys
pub_key
priv_key
compute_key_twoc
Computes the key, and returns a string that is byte-padded two's compliment in binary form.
pub_key_twoc
Returns the pub_key as a string that is byte-padded two's compliment in binary form.
clone
π AUTHOR π
by Daisuke Maki <daisuke@endeworks.jp> π
π LICENSE π
This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
See http://www.perl.com/perl/misc/Artistic.html
β okay thatβs the whole page, bestie β Crypt::DH::GMP(3), from perl v5.42.0, dated 2017-10-12. byeeee π
β¨
π¦ where this page comes from
- opensuse-leap 16.0: perl-Crypt-DH-GMP 0.00012-bp160.1.5 Β· Artistic-1.0 or GPL-1.0+
- opensuse-leap 16.0: perl-Crypt-DH-GMP 0.00012-bp160.1.5 Β· Artistic-1.0 or GPL-1.0+
- opensuse-leap 16.0: perl-Crypt-DH-GMP 0.00012-bp160.1.5 Β· Artistic-1.0 or GPL-1.0+
- opensuse-leap 16.0: perl-Crypt-DH-GMP 0.00012-bp160.1.5 Β· Artistic-1.0 or GPL-1.0+
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms