[Previous] [Up] [Next]

A Tour of NTL: Programming Interface


In this section, we give a general overview of the NTL's programming interface.

Basic Ring Classes

The basic ring classes are:

All these classes all support basic arithmetic operators

   +, -, (unary) -, +=, -=, ++, --, 
   *, *=, /, /=, %, %=.

However, the operations

   %, %=
only exist for integer and polynomial classes, and do not exist for classes
  ZZ_p, zz_p, GF2, ZZ_pE, zz_pE, GF2E.

The standard equality operators (== and !=) are provided for each class. In addition, the class ZZ supports the usual inequality operators.

The integer and polynomial classes also support "shift operators" for left and right shifting. For polynomial classes, this means multiplication or division by a power of X.

Floating Point Classes

In addition to the above ring classes, NTL also provides three different floating point classes:

Vectors and Matrices

There are also vectors and matrices over

   ZZ ZZ_p zz_p GF2 ZZ_pE zz_pE GF2E RR
which support the usual arithmetic operations.

Functional and Procedural forms

Generally, for any function defined by NTL, there is a functional form, and a procedural form. For example:

   ZZ x, a, n;
   x = InvMod(a, n);  // functional form
   InvMod(x, a, n);   // procedural form

This example illustrates the normal way these two forms differ syntactically. However, there are exceptions. First, if there is a operator that can play the role of the functional form, that is the notation used:

   ZZ x, a, b;
   x = a + b;    // functional form
   add(x, a, b); // procedural form

Second, if the functional form's name would be ambiguous, the return type is simply appended to its name:

   ZZ_p x;
   x = random_ZZ_p();  // functional form
   random(x);          // procedural form

Third, there are a number of conversion functions (see below), whose name in procedural form is conv, but whose name in functional form is conv<T>, where T is the return type:

   ZZ x;  
   double a;

   x = conv<ZZ>(a);  // functional form
   conv(x, a);       // procedural form

The use of the procedural form may be more efficient, since it will generally avoid the creation of a temporary object to store its result. However, it is generally silly to get too worked up about such efficiencies, and the functional form is usually preferable because the resulting code is usually easier to understand.

The above rules converning procedural and functional forms apply to essentially all of the arithmetic classes supported by NTL, with the exception of xdouble and quad_float. These two classes only support the functional/operator notation for arithmetic operations (but do support both forms for conversion).

Conversions and Promotions

As mentioned above, there are numerous explicit conversion routines, which come in both functional and procedural forms. A complete list of these can be found in conversions.txt. This is the only place these are documented; they do not appear in the other ".txt" files.

It is worth mentioning here, however, that generic conversion operators are provided for vectors and matrices, which act component-wise. For example, since there is a conversion from ZZ to RR, there is automatically a conversion from Vec<ZZ> to Vec<RR>.

Even though there are no implicity conversions, users of NTL can still have most of their benefits. This is because all of the basic arithmetic operations (in both their functional and procedural forms), comparison operators, and assignment are overloaded to get the effect of automatic "promotions". For example:

   ZZ x, a;

   x = a + 1;
   if (x < 0
      mul(x, 2, a);
   else
      x = -1;

These promotions are documented in the ".txt" files, usually using a kind of "short hand" notation. For example:

ZZ operator+(const ZZ& a, const ZZ& b);

// PROMOTIONS: operator + promotes long to ZZ on (a, b).

This means that in addition to the declared function, there are two other functions that are logically equivalent to the following:

ZZ operator+(long a, const ZZ& b) { return ZZ(a) + b; }
ZZ operator+(const ZZ& a, long b) { return a + ZZ(b); }

Note that this is not how NTL actually implements these functions. It is in generally more efficient to write

   x = y + 2;

than it is to write

   x = y + ZZ(2);

The former notation avoids the creation and destruction of a temporary ZZ object to hold the value 2.

Also, don't have any inhibitions about writing tests like

   if (x == 0) ...

and assignments like

   x = 1

These are all optimized, and do not execute significaltly slower than the "lower level" (and much less natural)

   if (IsZero(x)) ...

and

   set(x);

Some types have even more promotions. For example, the type ZZ_pX has promotions from long and ZZ_p. Thus, the add function for ZZ_pX takes the following argument types:

   (ZZ_pX, ZZ_pX), (ZZ_pX, ZZ_p), (ZZ_pX, long), (ZZ_p, ZZ_pX), (long, ZZ_pX)
Each of these functions effectively converts the argument to be promoted to a ZZ_pX.

Note that when promoting a pair of arguments, at least one of the arguments must be of the target type.

I have tried to be very consistent with these promotions so that one usually won't need to hunt through the documentation. For a given type, there is a natural, fixed set of types that promote to it. Here is the complete list:

   destination  source
   
   xdouble      double
   quad_float   double
   RR           double
   ZZ           long
   ZZ_p         long
   ZZ_pX        long, ZZ_p
   zz_p         long
   zz_pX        long, zz_p
   ZZX          long, ZZ
   GF2          long
   GF2X         long, GF2
   GF2E         long, GF2
   GF2EX        long, GF2, GF2E
   ZZ_pE        long, ZZ_p
   ZZ_pEX       long, ZZ_p, ZZ_pE
   zz_pE        long, zz_p
   zz_pEX       long, zz_p, zz_pE

All the promotions are documented, but here are a few general rules describing the available promotions:

Some Conversion and Promotion Technicalities

Usually, conversions and promotions are semantically equivalent. There are three exceptions, however.

One exception is conversion of floating point double to ZZ. The safest way to do this is to apply an explicit conversion operator, and not to rely on promotions. For example, consider

   ZZ a; double x;

   a = a + x;

This is equivialent to

   a = a + long(x);

and to

   a = a + ZZ(x);

One could also use an explicit conversion function:

   a = a + conv<ZZ>(x);

This last version guarantees that there is no loss of precision, and also guarantees that the floor of x is computed. With the first version, one may lose precision when x is converted to a long, and also the direction of truncation for negative numbers is implementation dependent (usually truncating towards zero, instead of computing the floor).

The second exception is conversion of unsigned int or unsigned long to ZZ. Again, the safest way to do this is with an explicit conversion operator. As above, if one relies on promotions, the unsigned integer will be first converted to a signed long, which is most likely not what was intended.

The third exception can occur on 64-bit machines when converting a signed or unsigned long to one of NTL's extended precision floating-point types (RR or quad_float). These types only provide promotions from double, and converting a long to a double on a 64-bit machine can lead to a loss of precision. Again, if one uses the appropriate NTL conversion routine, no loss of precision will occur.

Another pitfall too avoid is initialzing ZZ's with integer constants that are too big. Consider the following:

   ZZ x;
   x = 1234567890123456789012;

This integer constant is too big, and this overflow condition may or may not cause your compiler to give you a warning or an error. The easiest way to introduce such large constants into your program is as follows:

   ZZ x;
   x = conv<ZZ>("1234567890123456789012");

Conversion functions are provided for converting C character strings to the types ZZ, RR, quad_float, and xdouble.

One should also be careful when converting to RR. All of these conversions round to the current working precision, which is usually, but not always, what one wants.

Aliasing

An important feature of NTL is that aliasing of input and output parameters is generally allowed. For example, if you write mul(x, a, b), then a or b may alias (have the same address as) x (or any object that x contains, e.g., scalar/vector or scalar/polynomial multiplication).

One exception to this rule: the generic conversions provided for vectors and matrices assume that their inputs do not alias their outputs.

Constructors, Destructors, and Memory Management

NTL generally takes care of managing the space occupied by large, dynamically sized objects, like objects of class ZZ or any of NTL's dynamic vectors. However, it is helpful to understand a little of what is happening behind the scenes.

Almost all classes are implemented as a pointer, and the default constructor just sets this pointer to 0. Space is allocated for the object as needed, and when the object's destructor is called, the space is freed.

Copies are "deep" rather than "shallow". This means the data itself is copied, and not just a pointer to the data. If the destination object does not have enough space to hold the source data, then the space held by the destination object is "grown". This is done using the C routine realloc(). Note, however, that if the source object is smaller than the destination object, the space held by the destination object is retained. This strategy usually yields reasonable behaviour; however, one can take explicit control of the situation if necessary, since almost all NTL classes have a method kill() which frees all space held by the object, and sets its state to the default initial state (a value 0 or a zero-length vector).

The only exception to the above is the class ZZ_pContext, and the analogous classes for zz_p, ZZ_pE, zz_pE, and GF2E. These objects are implemented as referenced-counted pointers, and copies are "shallow".

While we are discussing initialization, there is one technical point worth mentioning. It is safe to declare global objects of any NTL type as long as one uses only the default constructor. For example, the global declarations

   ZZ global_integer;
   Vec<ZZ_p> global_vector;

should always work, since their initialization only involves setting a pointer to 0. However, one should avoid initializing global objects with non-default constructors, and should avoid doing anything that would lead to non-trivial computations with NTL objects prior to the beginning of the execution of routine main(). The reasons for this are quite esoteric and can only be appreciated by a true C++ afficianado. Actually, most such initializations and computations probably will work, but it is somewhat platform dependant.

Normal people usually do none of these things, so all of this should not matter too much. There is, however, one possible exception to this. A programmer might want to have a global constant initialized like this:

   const quad_float Pi = conv<quad_float>("3.1415926535897932384626433832795029");

While this probably will work fine on most platforms, it may not be an entirely portable construction, since it will involve a non-trivial computation before execution of main() begins. A more portable strategy is to define a function returning a read-only reference:

   const quad_float& Pi()
   {
      static quad_float pi = 
         conv<quad_float>("3.1415926535897932384626433832795029");
      return pi;
   }

and then call the function Pi() to get a read-only reference to this constant value:

   area = Pi()*r*r;

The initialization will then take place the first time Pi() is called, which is presumably after main() starts, and so everything should work fine. This is a very simple and general strategy that most C++ experts recommend using whenever the initialization of a non-global object requires non-trivial computation.

Residue class rings and modulus switching

NTL provides a number of classes to represent residue class rings:

   ZZ_p, zz_p, GF2, ZZ_pE, lzz_pE, GF2E.
For each such class, except GF2, there is a global, current modulus.

We focus on the class ZZ_p, but similar comments apply to the other residue class types. For example, for ZZ_p, you can set the current modulus to p as follows:

   ZZ_p::init(p);

The current modulus must be initialized before any operations on ZZ_p's are performed. The modulus may be changed, and a mechanism is provided for saving and restoring a modulus.

Here is what you do to save the current modulus, temporarily set it to p, and automatically restore it:

   { 
      ZZ_pPush push(p); 

      ...

   }

The constructor for push will save the current modulus, and install p as the current modulus. The destructor for push will restore the old modulus when the scope enclosing it exits. This is the so-called RAII (resource acquisition is initialization) paradigm.

You could also do the following:

   {
      ZZ_pPush push(); // just backup current modulus

        ...

      ZZ_p::init(p1); // install p1 

        ...

      ZZ_p::init(p2); // install p2

      // reinstall original modulus as close of scope
   }

The ZZ_pPush interface is good for implementing simple stack-like modulus "context switching". For more general context switching, see the class ZZ_pContext.

It is critical that ZZ_p objects created under one ZZ_p modulus are not used in any non-trivial way "out of context", i.e., under a different (or undefined) ZZ_p modulus. However, for ease-of-use, some operations may be safely performed out of context. These safe operations include: the default and copy constructor, the destructor, and the assignment operator. In addition it is generally safe to read any ZZ_p object out of context (i.e., printing it out, or fetching its underlying representive using the rep() function).

Any unsafe uses out of context are not in general checked, and may lead to unpredictable behavior.

The implementations of Vec<ZZ_p>, Vec<GF2E>, and Vec<GF2> are specialized to manage memory more efficiently than in the default implementation of Vec<T>.

Contiguous elements in a Vec<ZZ_p> are allocated in a contiguous region of memory. This reduces the number of calls to the memory allocator, and leads to greater locality of reference. A consequence of this implementation is that any calls to SetLength on a Vec<ZZ_p> object will need to use information about the current modulus, and so such calls should only be done "in context". That said, it is still safe to construct a Vec<ZZ_p> using the default or copy contructor, and to assign or append one Vec<ZZ_p> to another "out of context".

The same strategy is used for Vec<GF2E>'s.

In any case, the above restrictions adhere to the general rules for safely using residue class ring objects "out of context".

Vec<GF2>'s are implemented by packing coefficients (which are just bits) into words. A mechanism is provided to make indexing these vectors behave like normal vectors, via a class the mimics ordinary references to GF2's.

[Previous] [Up] [Next]