

/**************************************************************************\

MODULE: GF2

SUMMARY:

The class GF2 represents the field GF(2).
Computationally speaking, it is not a particularly useful class.
Its main use is to make the interfaces to the various finite 
field classes as uniform as possible.

The header file for GF2 also declares the class ref_GF2, which
use used to represent non-const references to GF2's, such as
those obtained from indexing a vec_GF2, which "packs" GF2's
into words.

There are implicit conversions from ref_GF2 to const GF2
and from GF2& to ref_GF2.  Therefore, if you want to declare
a function that takes a non-const reference to a GF2, you
should declare the parameter of type ref_GF2: this will
allow you to pass variables of type GF2 as well as 
elements of vec_GF2's obtained through indexing.

For all functions defined below which take a parameter of type
GF2&, there is also a function that takes a parameter of type ref_GF2.
Theoretically, we could have just defined the functions that take
the ref_GF2 parameter type, because of the implicit conversion
from GF2& to ref_GF2; however, for efficiency reasons, both
flavors are actually provided.   It is recommended that higher-level
functions use the ref_GF2 type exclusively.


\**************************************************************************/

#include <NTL/ZZ.h>
#include <NTL/vector.h>


class GF2 {
public:
   
   GF2(); // initial value 0

   GF2(const GF2& a); // copy constructor
   explicit GF2(long a); // promotion constructor

   GF2& operator=(const GF2& a); // assignment
   GF2& operator=(long a); // assignment

   // typedefs to aid in generic programming
   typedef long rep_type;
   typedef GF2Context context_type;
   typedef GF2Bak bak_type;
   typedef GF2Push push_type;
   typedef GF2X poly_type;

};


long rep(GF2 a); // read-only access to representation of a





/**************************************************************************\

                                  Comparison

\**************************************************************************/


long operator==(GF2 a, GF2 b);
long operator!=(GF2 a, GF2 b);

long IsZero(GF2 a);  // test for 0
long IsOne(GF2 a);  // test for 1

// PROMOTIONS: operators ==, != promote long to GF2 on (a, b).


/**************************************************************************\

                                    Addition 

\**************************************************************************/

// operator notation:

GF2 operator+(GF2 a, GF2 b);
GF2 operator-(GF2 a, GF2 b);

GF2 operator-(GF2 a); // unary -

GF2& operator+=(GF2& x, GF2 a);
GF2& operator+=(GF2& x, long a);

GF2& operator-=(GF2& x, GF2 a);
GF2& operator-=(GF2& x, long a);

GF2& operator++(GF2& x);  // prefix
void operator++(GF2& x, int);  // postfix

GF2& operator--(GF2& x);  // prefix
void operator--(GF2& x, int);  // postfix

// procedural versions:


void add(GF2& x, GF2 a, GF2 b); // x = a + b
void sub(GF2& x, GF2 a, GF2 b); // x = a - b 
void negate(GF2& x, GF2 a); // x = -a

// PROMOTIONS: binary +, -, and procedures add, sub promote
// from long to GF2 on (a, b).


/**************************************************************************\

                                  Multiplication 

\**************************************************************************/

// operator notation:

GF2 operator*(GF2 a, GF2 b);

GF2& operator*=(GF2& x, GF2 a);
GF2& operator*=(GF2& x, long a);

// procedural versions:

void mul(GF2& x, GF2 a, GF2 b); // x = a * b

void sqr(GF2& x, GF2 a); // x = a^2
GF2 sqr(GF2 a); 

// PROMOTIONS: operator * and procedure mul promote from long to GF2
// on (a, b).


/**************************************************************************\

                                  Division

\**************************************************************************/

operator notation:

GF2 operator/(z_p a, GF2 b);

GF2& operator/=(GF2& x, GF2 a);
GF2& operator/=(GF2& x, long a);

procedural versions:

void div(GF2& x, GF2 a, GF2 b);
// x = a/b

void inv(GF2& x, GF2 a);
GF2 inv(GF2 a);
// x = 1/a

// PROMOTIONS: operator / and procedure div promote from long to GF2
// on (a, b).


/**************************************************************************\

                                  Exponentiation

\**************************************************************************/


void power(GF2& x, GF2 a, long e); // x = a^e (e may be negative)
GF2 power(GF2 a, long e); 


/**************************************************************************\

                               Random Elements

\**************************************************************************/


void random(GF2& x);
GF2 random_GF2();
// x = random element in GF2.  Uses RandomBnd from ZZ.


/**************************************************************************\

                                Input/Output

\**************************************************************************/


ostream& operator<<(ostream& s, GF2 a);

istream& operator>>(istream& s, GF2& x);
// a ZZ is read and reduced mod 2


/**************************************************************************\

                               Miscellany

\**************************************************************************/


void clear(GF2& x); // x = 0
void set(GF2& x); // x = 1

void GF2::swap(GF2& x);
void swap(GF2& x, GF2& y);
// swap 

static GF2 GF2::zero();
// GF2::zero() yields a read-only reference to zero

static long GF2::modulus();
// GF2::modulus() returns the value 2

template<> class Vec<GF2>;
// Forward declaration of the explicit specialization
// of Vec<GF2>.  This specialization is defined in <NTL/vec_GF2.h>,
// which must be included in source files that need to use Vec<GF2>.

GF2::GF2(INIT_NO_ALLOC_TYPE);
// provided for consistency with other classes, initialize to zero

GF2::GF2(INIT_ALLOC_TYPE);
// provided for consistency with other classes, initialize to zero

GF2::allocate();
// provided for consistency with other classes, no action



