{
    "mode": "perldoc",
    "parameter": "Crypt::DH::GMP",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/json",
    "generated": "2026-06-17T19:29:21Z",
    "synopsis": "use Crypt::DH::GMP;\nmy $dh = Crypt::DH::GMP->new(p => $p, g => $g);\nmy $val = $dh->computesecret();\n# If you want compatibility with Crypt::DH (it uses Math::BigInt)\n# then use this flag\n# You /think/ you're using Crypt::DH, but...\nuse Crypt::DH::GMP qw(-compat);\nmy $dh = Crypt::DH->new(p => $p, g => $g);\nmy $val = $dh->computesecret();",
    "sections": {
        "NAME": {
            "content": "Crypt::DH::GMP - Crypt::DH Using GMP Directly\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Crypt::DH::GMP;\n\nmy $dh = Crypt::DH::GMP->new(p => $p, g => $g);\nmy $val = $dh->computesecret();\n\n# If you want compatibility with Crypt::DH (it uses Math::BigInt)\n# then use this flag\n# You /think/ you're using Crypt::DH, but...\nuse Crypt::DH::GMP qw(-compat);\n\nmy $dh = Crypt::DH->new(p => $p, g => $g);\nmy $val = $dh->computesecret();\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Crypt::DH::GMP is a (somewhat) portable replacement to Crypt::DH, implemented mostly in C.\n",
            "subsections": []
        },
        "RATIONALE": {
            "content": "In the beginning, there was \"Crypt::DH\". However, \"Crypt::DH\" suffers from a couple of problems:\n\nGMP/Pari libraries are almost always required\n\"Crypt::DH\" works with a plain \"Math::BigInt\", but if you want to use it in production, you\nalmost always need to install \"Math::BigInt::GMP\" or \"Math::BigInt::Pari\" because without\nthem, the computation that is required by \"Crypt::DH\" makes the module pretty much unusable.\n\nBecause of this, \"Crypt::DH\" might as well make \"Math::BigInt::GMP\" a hard requirement.\n\nCrypt::DH suffers from having Math::BigInt in between GMP\nWith or without \"Math::BigInt::GMP\" or \"Math::BigInt::Pari\", \"Crypt::DH\" makes several round\ntrip conversions between Perl scalars, Math::BigInt objects, and finally its C\nrepresentation (if GMP/Pari are installed).\n\nInstantiating an object comes with a relatively high cost, and if you make many computations\nin one go, your program will suffer dramatically because of this.\n\nThese problems quickly become apparent when you use modules such as \"Net::OpenID::Consumer\",\nwhich requires to make a few calls to \"Crypt::DH\".\n\n\"Crypt::DH::GMP\" attempts to alleviate these problems by providing a \"Crypt::DH\"-compatible\nlayer, which, instead of doing calculations via Math::BigInt, directly works with libgmp in C.\n\nThis means that we've essentially eliminated 2 call stacks worth of expensive Perl method calls\nand we also only load 1 (Crypt::DH::GMP) module instead of 3 (Crypt::DH + Math::BigInt +\nMath::BigInt::GMP).\n\nThese add up to a fairly significant increase in performance.\n\nCOMPATIBILITY WITH Crypt::DH\nCrypt::DH::GMP absolutely refuses to consider using anything other than strings as its\nparameters and/or return values therefore if you would like to use Math::BigInt objects as your\nreturn values, you can not use Crypt::DH::GMP directly. Instead, you need to be explicit about\nit:\n\nuse Crypt::DH;\nuse Crypt::DH::GMP qw(-compat); # must be loaded AFTER Crypt::DH\n\nSpecifying -compat invokes a very nasty hack that overwrites Crypt::DH's symbol table -- this\nthen forces Crypt::DH users to use Crypt::DH::GMP instead, even if you are writing\n\nmy $dh = Crypt::DH->new(...);\n$dh->computekey();\n",
            "subsections": []
        },
        "BENCHMARK": {
            "content": "By NO MEANS is this an exhaustive benchmark, but here's what I get on my MacBook (OS X 10.5.8,\n2.4 GHz Core 2 Duo, 4GB RAM)\n\nBenchmarking instatiation cost...\nRate   pp  gmp\npp   9488/s   -- -79%\ngmp 45455/s 379%   --\n\nBenchmarking key generation cost...\nRate gmp  pp\ngmp 6.46/s  -- -0%\npp  6.46/s  0%  --\n\nBenchmarking computekey cost...\nRate    pp   gmp\npp   12925/s    --  -96%\ngmp 365854/s 2730%    --\n",
            "subsections": []
        },
        "METHODS": {
            "content": "new\np\ng\ncomputekey\ncomputesecret\ngeneratekeys\npubkey\nprivkey\ncomputekeytwoc\nComputes the key, and returns a string that is byte-padded two's compliment in binary form.\n\npubkeytwoc\nReturns the pubkey as a string that is byte-padded two's compliment in binary form.\n\nclone",
            "subsections": []
        },
        "AUTHOR": {
            "content": "Daisuke Maki \"<daisuke@endeworks.jp>\"\n",
            "subsections": []
        },
        "LICENSE": {
            "content": "This program is free software; you can redistribute it and/or modify it under the same terms as\nPerl itself.\n\nSee http://www.perl.com/perl/misc/Artistic.html\n",
            "subsections": []
        }
    },
    "summary": "Crypt::DH::GMP - Crypt::DH Using GMP Directly",
    "flags": [],
    "examples": [],
    "see_also": []
}