A Tour of NTL: Using NTL with GMP

#

A Tour of NTL: Using NTL with GMP

GMP is the GNU Multi-Precision library. You can get more information about it, as well as the latest version from here.

Briefly, GMP is a library for long integer arithmetic. It has hand-crafted assembly routines for a wide variety of architectures. For basic operations, like integer multiplication, it can be two to three (and sometimes bit more) times faster than NTL's traditional long integer package. The speedup is most dramatic on x86 machines.

As of version 9.6.3, NTL uses GMP by default. You can disable GMP by passing `NTL_GMP_LIP=off` as an option to NTL's `configure` script. If you disable the use of GMP, NTL uses a long integer package derived from Arjen Lenstra's LIP package. This is not recommended: GMP is much faster.

Even if you do not use GMP, you should still read the section below on backward compatabilty so that you can write portable code and avoid deprecated constructs.

Note: GMP is thread safe, so you should feel free to use it in a thread-safe build of NTL. However, the current version of GMP (v6.1) is not entirely exception friendly (it may abort a running program, but only in some very extreme and unusal circumstances).

## Downloading and building GMP

Many unix distributions now include GMP by default. But if not, it is pretty easy to install it directly from source, as follows.

Download GMP from here. You will get a file `gmp-XXX.tar.gz`.

Now do the following:

       % gunzip gmp-XXX.tar.gz
       % tar xf gmp-XXX.tar
       % cd gmp-XXX
       % ./configure --prefix=$HOME/sw
       % make
       % make check
       % make install

This will build, test, and install GMP in `$HOME/sw`. Of course, change $HOME/sw to whatever you want (the default is `/usr/local`). You will find the GMP header files in `$HOME/sw/include` and the compiled binaries in `$HOME/sw/lib`.

You can also supply the option `--disable-shared` to the `configure` script, if you only want static libraries. However, if you ultimately want to build NTL as a shared library, then you must also buld GMP as a shared library.

You must ensure that NTL and GMP have the same ABI. Usually, GMP's configure script will automatically choose a 64-bit ABI if available.

## Building and using NTL with GMP

When you are installing NTL, if you installed GMP in `$HOME/sw` as above, and you also want to install NTL in `$HOME/sw`, you execute:

       % ./configure PREFIX=$HOME/sw GMP_PREFIX=$HOME/sw

You can write this more simply as

       % ./configure DEF_PREFIX=$HOME/sw

Here, `DEF_PREFIX` is a variable that is used to specify the location of all software, and it defaults to `/usr/local`.

If you installed GMP in `/usr/local` (or some other standard system directory where your compiler will look by default) then simply

       % ./configure PREFIX=$HOME/sw

does the job. Moreover, if NTL is also to be installed in `/usr/local`, then

       % ./configure

does the job.

When compiling programs that use NTL with GMP, you need to link with the GMP library. If GMP is installed as above in `$HOME/sw`, rather than in a standard system directory, this just means adding `-L$HOME/sw/lib -lgmp` to the compilation command. If you installed GMP in a standard system directory, then just `-lgmp` does the job. Note that `-lgmp` must come after `-lntl` on the command line. Finally, if NTL and GMP are installed as shared libraries, then you don't even need `-lgmp`.

NTL has been tested and works correctly with GMP versions 3.1, 3.1.1, 4.1.4, 5.1, 6.0, and 6.1 (among others). It is not possible to use versions of GMP prior to 3.1 with NTL.

When using NTL with GMP, as a user of NTL, you do not need to know or understand anything about the the GMP library. So while there is detailed documentation available about how to use GMP, you do not have to read it. The programming interface to the long integer package completely hides implementation details.

## Backward compatbility

With version 5.0 of NTL, some aspects of the programming interface are 'deprecated' so as to allow the use of another long integer package, such as GMP, as the long integer package.

Prior to version 5.0, the macro `NTL_NBITS` was defined, along with the macro `NTL_RADIX` defined to be `(1L << NTL_NBITS)`. While these macros are still available when using NTL's traditional long integer package (i.e., when `NTL_GMP_LIP` is not set), they are not available when using the GMP as the long integer package (i.e., when `NTL_GMP_LIP` is set). Furthermore, when writing portable programs, one should avoid these macros.

Also, the static function `long ZZ::digit(const ZZ &, long);` is defined when using traditional long integer arithmetic, but is not available when using GMP as the long integer package, and in any case, its use should be avoided when writing portable programs.

Instead of the above macros, one should use the followng macros:

       NTL_ZZ_NBITS -- number of bits in a limb;
                       a ZZ is represented as a sequence of limbs.

       NTL_SP_NBITS -- max number of bits in a "single-precision" number

       NTL_WSP_NBITS -- max number of bits in a "wide single-precision" number

The following relations hold:

       NTL_SP_NBITS <= NTL_WSP_NBITS <= NTL_ZZ_NBITS
       26 <= NTL_SP_NBITS <= min(NTL_BITS_PER_LONG-2, NTL_DOUBLE_PRECISION-3)
       NTL_WSP_NBITS <= NTL_BITS_PER_LONG-2

Note that `NTL_ZZ_NBITS` may be less than, equal to, or greater than `NTL_BITS_PER_LONG` -- no particular relationship should be assumed to hold. In particular, expressions like `(1L << NTL_ZZ_BITS)` might overflow.

"single-precision" numbers are meant to be used in conjunction with the single-precision modular arithmetic routines.

"wide single-precision" numbers are meant to be used in conjunction with the `ZZ` arithmetic routines for optimal efficiency.

Note that when using traditional long integer arithmetic, we have

        NTL_ZZ_NBITS = NTL_SP_NBITS = NTL_WSP_NBITS = NTL_NBITS.

The following auxilliary macros are also defined:

    NTL_FRADIX -- double-precision value of 2^NTL_ZZ_NBITS
    NTL_SP_BOUND -- (1L << NTL_SP_NBITS)
    NTL_WSP_BOUND -- (1L << NTL_WSP_NBITS)

Note that for a `ZZ` `n`, `n.size()` returns the number of "limbs" of `n`. This is supported with either traditional or GMP integer arithemtic. Note, however, that some old codes might write `n.size() <= 1` as a way to test if `NumBits(n) <= NTL_NBITS`. This is no longer the right thing to do, if one wants portable code that works with either traditional or GMP long integer arithmetic. First, one has to decide whether one wants to test if `NumBits(n)` is bounded by `NTL_ZZ_NBITS`, `NTL_SP_NBITS`, or `NTL_WSP_NBITS`. In the first case, `n.size() <= 1` is still the right way to test this. In the second case, write this as `n.SinglePrecision()`. In the third case, write this as `n.WideSinglePrecision()`. The routines `SinglePrecision` and `WideSinglePrecision` are new to NTL version 5.0.

Most "high level" applications that use NTL should not be affected by these changes to NTL's programming interface, and if they are, changing the programs should be quite easy.