/**************************************************************************\ MODULE: ZZ_pE SUMMARY: The class ZZ_pE is used to represent polynomials in Z_p[X] modulo a polynomial P. The modulus P may be any polynomial with deg(P) > 0, not necessarily irreducible. The modulus p defining Z_p need not be prime either. Objects of the class ZZ_pE are represented as a ZZ_pX of degree < deg(P). An executing program maintains a "current modulus", which is set to P using ZZ_pE::init(P). The current modulus *must* be initialized before any ZZ_pE constructors are invoked. The modulus may be changed, and a mechanism is provided for saving and restoring a modulus (see classes ZZ_pEBak and ZZ_pEContext below). \**************************************************************************/ #include class ZZ_pE { public: ZZ_pE(); // initial value 0 ZZ_pE(const ZZ_pE& a); // copy constructor ZZ_pE& operator=(const ZZ_pE& a); // assignment ZZ_pE& operator=(const ZZ_p& a); // assignment ZZ_pE& operator=(long a); // assignment ~ZZ_pE(); // destructor void init(const ZZ_pX& P); // ZZ_pE::init(P) initializes the current modulus to P; // required: deg(P) >= 1. static const ZZ_pXModulus& modulus(); // ZZ_pE::modulus() yields read-only reference to the current modulus static long degree(); // ZZ_pE::degree() returns deg(P) }; const ZZ_pX& rep(const ZZ_pE& a); // read-only access to representation of a /**************************************************************************\ Comparison \**************************************************************************/ long operator==(const ZZ_pE& a, const ZZ_pE& b); long operator!=(const ZZ_pE& a, const ZZ_pE& b); long IsZero(const ZZ_pE& a); // test for 0 long IsOne(const ZZ_pE& a); // test for 1 // PROMOTIONS: ==, != promote {long, ZZ_p} to ZZ_pE on (a, b). /**************************************************************************\ Addition \**************************************************************************/ // operator notation: ZZ_pE operator+(const ZZ_pE& a, const ZZ_pE& b); ZZ_pE operator-(const ZZ_pE& a, const ZZ_pE& b); ZZ_pE operator-(const ZZ_pE& a); ZZ_pE& operator+=(ZZ_pE& x, const ZZ_pE& a); ZZ_pE& operator+=(ZZ_pE& x, const ZZ_p& a); ZZ_pE& operator+=(ZZ_pE& x, long a); ZZ_pE& operator++(ZZ_pE& x); // prefix void operator++(ZZ_pE& x, int); // postfix ZZ_pE& operator-=(ZZ_pE& x, const ZZ_pE& a); ZZ_pE& operator-=(ZZ_pE& x, const ZZ_p& a); ZZ_pE& operator-=(ZZ_pE& x, long a); ZZ_pE& operator--(ZZ_pE& x); // prefix void operator--(ZZ_pE& x, int); // postfix // procedural versions: void add(ZZ_pE& x, const ZZ_pE& a, const ZZ_pE& b); // x = a + b void sub(ZZ_pE& x, const ZZ_pE& a, const ZZ_pE& b); // x = a - b void negate(ZZ_pE& x, const ZZ_pE& a); // x = - a // PROMOTIONS: +, -, add, sub promote {long, ZZ_p} to ZZ_pE on (a, b). /**************************************************************************\ Multiplication \**************************************************************************/ // operator notation: ZZ_pE operator*(const ZZ_pE& a, const ZZ_pE& b); ZZ_pE& operator*=(ZZ_pE& x, const ZZ_pE& a); ZZ_pE& operator*=(ZZ_pE& x, const ZZ_p& a); ZZ_pE& operator*=(ZZ_pE& x, long a); // procedural versions: void mul(ZZ_pE& x, const ZZ_pE& a, const ZZ_pE& b); // x = a * b void sqr(ZZ_pE& x, const ZZ_pE& a); // x = a^2 ZZ_pE sqr(const ZZ_pE& a); // PROMOTIONS: *, mul promote {long, ZZ_p} to ZZ_pE on (a, b). /**************************************************************************\ Division \**************************************************************************/ // operator notation: ZZ_pE operator/(const ZZ_pE& a, const ZZ_pE& b); ZZ_pE& operator/=(ZZ_pE& x, const ZZ_pE& a); ZZ_pE& operator/=(ZZ_pE& x, const ZZ_p& a); ZZ_pE& operator/=(ZZ_pE& x, long a); // procedural versions: void div(ZZ_pE& x, const ZZ_pE& a, const ZZ_pE& b); // x = a/b. If b is not invertible, an error is raised. void inv(ZZ_pE& x, const ZZ_pE& a); ZZ_pE inv(const ZZ_pE& a); // x = 1/a PROMOTIONS: /, div promote {long, ZZ_p} to ZZ_pE on (a, b). /**************************************************************************\ Exponentiation \**************************************************************************/ void power(ZZ_pE& x, const ZZ_pE& a, const ZZ& e); ZZ_pE power(const ZZ_pE& a, const ZZ& e); void power(ZZ_pE& x, const ZZ_pE& a, long e); ZZ_pE power(const ZZ_pE& a, long e); // x = a^e (e may be negative) /**************************************************************************\ Random Elements \**************************************************************************/ void random(ZZ_pE& x); ZZ_pE random_ZZ_pE(); // x = random element in ZZ_pE. /**************************************************************************\ Norms and Traces \**************************************************************************/ void trace(ZZ_p& x, const ZZ_pE& a); // x = trace of a ZZ_p trace(const ZZ_pE& a); void norm(ZZ_p& x, const ZZ_pE& a); // x = norm of a ZZ_p norm(const ZZ_pE& a); /**************************************************************************\ Input/Output \**************************************************************************/ ostream& operator<<(ostream& s, const ZZ_pE& a); istream& operator>>(istream& s, ZZ_pE& x); // a ZZ_pX is read and reduced mod p /**************************************************************************\ Modulus Switching A class ZZ_pEBak is provided for "backing up" the current modulus. Here is what you do to save the current modulus, temporarily set it to something new, and then restore it: ZZ_pEBak bak; bak.save(); // save current modulus (if any) ZZ_pE::init(P); // set modulus to desired value P // ... bak.restore(); // restore old modulus (if any) Note that between the save and restore, you may have several calls to ZZ_pE::init, each of which simply clobbers the previous modulus. The ZZ_pEBak interface is good for implementing simple stack-like modulus "context switching". For more general context switching, see ZZ_pEContext below. .......................................................................... When the current modulus is changed, there may be extant ZZ_pE objects. If the old modulus was saved and then later restored, these objects can be used again as if the modulus had never changed. Note, however, that if a ZZ_pE object is created under one modulus and then used in any way (except destroyed) under another, program behavior is not predictable. This condition is not explicitly checked for, but an error is likely to be raised. One should also not presume that things will work properly if the modulus is changed, but its value happens to be the same--- one should restore the same "context", from either a ZZ_pEBak or a ZZ_pEContext object. This is anyway more efficient. \**************************************************************************/ class ZZ_pEBak { public: // To describe this logic, think of a ZZ_pEBak object // of having two components: a modulus Q (possibly "null") and // an "auto-restore bit" b. // There is also a global current modulus P (initially "null"). ZZ_pEBak(); // Q = "null", b = 0 ~ZZ_pEBak(); // if (b) P = Q void save(); // Q = P, b = 1 void restore(); // P = Q, b = 0 private: ZZ_pEBak(const ZZ_pEBak&); // copy disabled void operator=(const ZZ_pEBak&); // assignment disabled }; // more general context switching: class ZZ_pEContext { // A ZZ_pEContext object has a modulus Q (possibly "null"), // but has no auto-restore bit like a ZZ_pEBak object. // However, these objects can be initialized and copied with // complete generality. // As above, P is the current global modulus (initially "null") public: ZZ_pEContext(); // Q = "null" ZZ_pEContext(const ZZ_pX& new_Q); // Q = new_Q void save(); // Q = P void restore() const; // P = Q ZZ_pEContext(const ZZ_pEContext&); // copy ZZ_pEContext& operator=(const ZZ_pEContext&); // assignment ~ZZ_pEContext(); // destructor }; /**************************************************************************\ Miscellany \**************************************************************************/ void clear(ZZ_pE& x); // x = 0 void set(ZZ_pE& x); // x = 1 static const ZZ_pE& ZZ_pE::zero(); // ZZ_pE::zero() yields a read-only reference to zero void swap(ZZ_pE& x, ZZ_pE& y); // swap x and y (done by "pointer swapping", if possible). static ZZ& ZZ_pE::cardinality(); // yields the cardinality, i.e., p^{ZZ_pE::degree()}