c++ntl

NTL Library ref_GF2 Run-time Error


I am using the NTL C++ Library. On trying to execute the following code:

NTL::ref_GF2 *zero = new NTL::ref_GF2();
NTL::ref_GF2 *one = new NTL::ref_GF2();
set(*one);

I am getting an EXC_BAD_INSTRUCTION error:

ref_GF2 operator=(long a)
{
   unsigned long rval = a & 1;
   unsigned long lval = *_ref_GF2__ptr;
   lval = (lval & ~(1UL << _ref_GF2__pos)) | (rval << _ref_GF2__pos);
   *_ref_GF2__ptr = lval;
   return *this;
}

The problem seems to stem from the set(*one) line of code.

I've been trying to understand what's going wrong in the code, to no avail. Any help appreciated.


Solution

  • From the documentation:

    The header file for GF2 also declares the class ref_GF2, which use used to represent non-const references to GF2's, [...].

    There are implicit conversions from ref_GF2 to const GF2 and from GF2& to ref_GF2.

    You get the error because your are defining a reference without a target. At the point where you call set(*one), *one does not point to a GF2 and so it throws an error.

    It works fine, if you point to a GF2 before calling set(*one):

    NTL::GF2 x = GF2();
    NTL::set(x);               // x = 1
    
    NTL::ref_GF2 *zero = new NTL::ref_GF2(x);
    NTL::ref_GF2 *one  = new NTL::ref_GF2(x);
    
    // this works now
    NTL::clear(*zero);
    NTL::set(*one);
    
    cout << *zero << endl;     // prints "1"
    cout << *one << endl;      // prints "1"
    

    Notice that ref_GF2 represents a reference to a GF2. My example code shows that zero and one both point to x. Maybe you want to use GF2 instead of ref_GF2.