# phpman > man > Crypt::DH::GMP(3pm)

## NAME
    [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) - [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown) Using GMP Directly

## SYNOPSIS
      use [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown);

      my $dh = [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown)->new(p => $p, g => $g);
      my $val = $dh->compute_secret();

      # If you want compatibility with [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown) (it uses [Math::BigInt](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown))
      # then use this flag
      # You /think/ you're using [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown), but...
      use [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) qw(-compat);

      my $dh = [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)->new(p => $p, g => $g);
      my $val = $dh->compute_secret();

## DESCRIPTION
    [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) is a (somewhat) portable replacement to [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown), implemented mostly in C.

## RATIONALE
    In the beginning, there was "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)". However, "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)" suffers from a couple of problems:

    GMP/Pari libraries are almost always required
        "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)" works with a plain "[Math::BigInt](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown)", but if you want to use it in production, you
        almost always need to install "[Math::BigInt::GMP](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt%3A%3AGMP/markdown)" or "[Math::BigInt::Pari](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt%3A%3APari/markdown)" because without
        them, the computation that is required by "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)" makes the module pretty much unusable.

        Because of this, "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)" might as well make "[Math::BigInt::GMP](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt%3A%3AGMP/markdown)" a hard requirement.

    [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown) suffers from having [Math::BigInt](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown) in between GMP
        With or without "[Math::BigInt::GMP](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt%3A%3AGMP/markdown)" or "[Math::BigInt::Pari](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt%3A%3APari/markdown)", "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)" makes several round
        trip conversions between Perl scalars, [Math::BigInt](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown) 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](https://www.chedong.com/phpMan.php/perldoc/Net%3A%3AOpenID%3A%3AConsumer/markdown)",
    which requires to make a few calls to "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)".

    "[Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown)" attempts to alleviate these problems by providing a "[Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)"-compatible
    layer, which, instead of doing calculations via [Math::BigInt](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown), 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](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown)) module instead of 3 ([Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown) + [Math::BigInt](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown) +
    [Math::BigInt::GMP](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt%3A%3AGMP/markdown)).

    These add up to a fairly significant increase in performance.

COMPATIBILITY WITH [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)
    [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) 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](https://www.chedong.com/phpMan.php/perldoc/Math%3A%3ABigInt/markdown) objects as your
    return values, you can not use [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) directly. Instead, you need to be explicit about
    it:

      use [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown);
      use [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) qw(-compat); # must be loaded AFTER [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)

    Specifying -compat invokes a very nasty hack that overwrites [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)'s symbol table -- this
    then forces [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown) users to use [Crypt::DH::GMP](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH%3A%3AGMP/markdown) instead, even if you are writing

      my $dh = [Crypt::DH](https://www.chedong.com/phpMan.php/perldoc/Crypt%3A%3ADH/markdown)->new(...);
      $dh->compute_key();

## 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
  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
    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>

