📄 gf2ex.txt
字号:
/**************************************************************************\MODULE: GF2EXSUMMARY:The class GF2EX represents polynomials over GF2E,and so can be used, for example, for arithmentic in GF(2^n)[X].However, except where mathematically necessary (e.g., GCD computations),GF2E need not be a field.\**************************************************************************/#include <NTL/GF2E.h>#include <NTL/vec_GF2E.h>class GF2EX {public: GF2EX(); // initial value 0 GF2EX(const GF2EX& a); // copy GF2EX& operator=(const GF2EX& a); // assignment GF2EX& operator=(const GF2E& a); GF2EX& operator=(GF2 a); GF2EX& operator=(long a); ~GF2EX(); // destructor GF2EX(long i, const GF2E& c); // initilaize to X^i*c GF2EX(long i, GF2 c); GF2EX(long i, long c); };/**************************************************************************\ Comparison\**************************************************************************/long operator==(const GF2EX& a, const GF2EX& b);long operator!=(const GF2EX& a, const GF2EX& b);long IsZero(const GF2EX& a); // test for 0long IsOne(const GF2EX& a); // test for 1// PROMOTIONS: ==, != promote {long,GF2,GF2E} to GF2EX on (a, b)./**************************************************************************\ Addition\**************************************************************************/// operator notation:GF2EX operator+(const GF2EX& a, const GF2EX& b);GF2EX operator-(const GF2EX& a, const GF2EX& b);GF2EX operator-(const GF2EX& a);GF2EX& operator+=(GF2EX& x, const GF2EX& a);GF2EX& operator+=(GF2EX& x, const GF2E& a);GF2EX& operator+=(GF2EX& x, GF2 a);GF2EX& operator+=(GF2EX& x, long a);GF2EX& operator++(GF2EX& x); // prefixvoid operator++(GF2EX& x, int); // postfixGF2EX& operator-=(GF2EX& x, const GF2EX& a);GF2EX& operator-=(GF2EX& x, const GF2E& a);GF2EX& operator-=(GF2EX& x, GF2 a);GF2EX& operator-=(GF2EX& x, long a);GF2EX& operator--(GF2EX& x); // prefixvoid operator--(GF2EX& x, int); // postfix// procedural versions:void add(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a + bvoid sub(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a - b void negate(GF2EX& x, const GF2EX& a); // x = - a // PROMOTIONS: +, -, add, sub promote {long,GF2,GF2E} to GF2EX on (a, b)./**************************************************************************\ Multiplication\**************************************************************************/// operator notation:GF2EX operator*(const GF2EX& a, const GF2EX& b);GF2EX& operator*=(GF2EX& x, const GF2EX& a);GF2EX& operator*=(GF2EX& x, const GF2E& a);GF2EX& operator*=(GF2EX& x, GF2 a);GF2EX& operator*=(GF2EX& x, long a);// procedural versions:void mul(GF2EX& x, const GF2EX& a, const GF2EX& b); // x = a * bvoid sqr(GF2EX& x, const GF2EX& a); // x = a^2GF2EX sqr(const GF2EX& a); // PROMOTIONS: *, mul promote {long,GF2,GF2E} to GF2EX on (a, b).void power(GF2EX& x, const GF2EX& a, long e); // x = a^e (e >= 0)GF2EX power(const GF2EX& a, long e);/**************************************************************************\ Shift OperationsLeftShift by n means multiplication by X^nRightShift by n means division by X^nA negative shift amount reverses the direction of the shift.\**************************************************************************/// operator notation:GF2EX operator<<(const GF2EX& a, long n);GF2EX operator>>(const GF2EX& a, long n);GF2EX& operator<<=(GF2EX& x, long n);GF2EX& operator>>=(GF2EX& x, long n);// procedural versions:void LeftShift(GF2EX& x, const GF2EX& a, long n); GF2EX LeftShift(const GF2EX& a, long n);void RightShift(GF2EX& x, const GF2EX& a, long n); GF2EX RightShift(const GF2EX& a, long n); /**************************************************************************\ Division\**************************************************************************/// operator notation:GF2EX operator/(const GF2EX& a, const GF2EX& b);GF2EX operator/(const GF2EX& a, const GF2E& b);GF2EX operator/(const GF2EX& a, GF2 b);GF2EX operator/(const GF2EX& a, long b);GF2EX operator%(const GF2EX& a, const GF2EX& b);GF2EX& operator/=(GF2EX& x, const GF2EX& a);GF2EX& operator/=(GF2EX& x, const GF2E& a);GF2EX& operator/=(GF2EX& x, GF2 a);GF2EX& operator/=(GF2EX& x, long a);GF2EX& operator%=(GF2EX& x, const GF2EX& a);// procedural versions:void DivRem(GF2EX& q, GF2EX& r, const GF2EX& a, const GF2EX& b);// q = a/b, r = a%bvoid div(GF2EX& q, const GF2EX& a, const GF2EX& b);void div(GF2EX& q, const GF2EX& a, const GF2E& b);void div(GF2EX& q, const GF2EX& a, GF2 b);void div(GF2EX& q, const GF2EX& a, long b);// q = a/bvoid rem(GF2EX& r, const GF2EX& a, const GF2EX& b);// r = a%blong divide(GF2EX& q, const GF2EX& a, const GF2EX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0long divide(const GF2EX& a, const GF2EX& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0/**************************************************************************\ GCD'sThese routines are intended for use when GF2E is a field.\**************************************************************************/void GCD(GF2EX& x, const GF2EX& a, const GF2EX& b);GF2EX GCD(const GF2EX& a, const GF2EX& b); // x = GCD(a, b), x is always monic (or zero if a==b==0).void XGCD(GF2EX& d, GF2EX& s, GF2EX& t, const GF2EX& a, const GF2EX& b);// d = gcd(a,b), a s + b t = d /**************************************************************************\ Input/OutputI/O format: [a_0 a_1 ... a_n],represents the polynomial a_0 + a_1*X + ... + a_n*X^n.On output, all coefficients will be polynomials of degree < GF2E::degree() anda_n not zero (the zero polynomial is [ ]). On input, the coefficientsare arbitrary polynomials which are reduced modulo GF2E::modulus(), and leadingzeros stripped.\**************************************************************************/istream& operator>>(istream& s, GF2EX& x);ostream& operator<<(ostream& s, const GF2EX& a);/**************************************************************************\ Some utility routines\**************************************************************************/long deg(const GF2EX& a); // return deg(a); deg(0) == -1.const GF2E& coeff(const GF2EX& a, long i);// returns a read-only reference to the coefficient of X^i, or zero if// i not in rangeconst GF2E& LeadCoeff(const GF2EX& a);// read-only reference to leading term of a, or zero if a == 0const GF2E& ConstTerm(const GF2EX& a);// read-only reference to constant term of a, or zero if a == 0void SetCoeff(GF2EX& x, long i, const GF2E& a);void SetCoeff(GF2EX& x, long i, GF2 a);void SetCoeff(GF2EX& x, long i, long a);// makes coefficient of X^i equal to a; error is raised if i < 0void SetCoeff(GF2EX& x, long i);// makes coefficient of X^i equal to 1; error is raised if i < 0void SetX(GF2EX& x); // x is set to the monomial Xlong IsX(const GF2EX& a); // test if x = Xvoid diff(GF2EX& x, const GF2EX& a); // x = derivative of aGF2EX diff(const GF2EX& a); void MakeMonic(GF2EX& x); // if x != 0 makes x into its monic associate; LeadCoeff(x) must be// invertible in this casevoid reverse(GF2EX& x, const GF2EX& a, long hi);GF2EX reverse(const GF2EX& a, long hi);void reverse(GF2EX& x, const GF2EX& a);GF2EX reverse(const GF2EX& a);// x = reverse of a[0]..a[hi] (hi >= -1);// hi defaults to deg(a) in second versionvoid VectorCopy(vec_GF2E& x, const GF2EX& a, long n);vec_GF2E VectorCopy(const GF2EX& a, long n);// x = copy of coefficient vector of a of length exactly n.// input is truncated or padded with zeroes as appropriate./**************************************************************************\ Random Polynomials\**************************************************************************/void random(GF2EX& x, long n);GF2EX random_GF2EX(long n);// x = random polynomial of degree < n /**************************************************************************\ Polynomial Evaluation and related problems\**************************************************************************/void BuildFromRoots(GF2EX& x, const vec_GF2E& a);GF2EX BuildFromRoots(const vec_GF2E& a);// computes the polynomial (X-a[0]) ... (X-a[n-1]), where n = a.length()void eval(GF2E& b, const GF2EX& f, const GF2E& a);GF2E eval(const GF2EX& f, const GF2E& a);// b = f(a)void eval(GF2E& b, const GF2X& f, const GF2E& a);GF2E eval(const GF2EX& f, const GF2E& a);// b = f(a); uses ModComp algorithm for GF2Xvoid eval(vec_GF2E& b, const GF2EX& f, const vec_GF2E& a);vec_GF2E eval(const GF2EX& f, const vec_GF2E& a);// b.SetLength(a.length()); b[i] = f(a[i]) for 0 <= i < a.length()void interpolate(GF2EX& f, const vec_GF2E& a, const vec_GF2E& b);GF2EX interpolate(const vec_GF2E& a, const vec_GF2E& b);// interpolates the polynomial f satisfying f(a[i]) = b[i]. /**************************************************************************\ Arithmetic mod X^nRequired: n >= 0; otherwise, an error is raised.\**************************************************************************/void trunc(GF2EX& x, const GF2EX& a, long n); // x = a % X^nGF2EX trunc(const GF2EX& a, long n); void MulTrunc(GF2EX& x, const GF2EX& a, const GF2EX& b, long n);GF2EX MulTrunc(const GF2EX& a, const GF2EX& b, long n);// x = a * b % X^nvoid SqrTrunc(GF2EX& x, const GF2EX& a, long n);GF2EX SqrTrunc(const GF2EX& a, long n);// x = a^2 % X^nvoid InvTrunc(GF2EX& x, const GF2EX& a, long n);GF2EX InvTrunc(GF2EX& x, const GF2EX& a, long n);// computes x = a^{-1} % X^m. Must have ConstTerm(a) invertible./**************************************************************************\ Modular Arithmetic (without pre-conditioning)Arithmetic mod f.All inputs and outputs are polynomials of degree less than deg(f), anddeg(f) > 0.NOTE: if you want to do many computations with a fixed f, use theGF2EXModulus data structure and associated routines below for betterperformance.\**************************************************************************/void MulMod(GF2EX& x, const GF2EX& a, const GF2EX& b, const GF2EX& f);GF2EX MulMod(const GF2EX& a, const GF2EX& b, const GF2EX& f);// x = (a * b) % fvoid SqrMod(GF2EX& x, const GF2EX& a, const GF2EX& f);GF2EX SqrMod(const GF2EX& a, const GF2EX& f);// x = a^2 % fvoid MulByXMod(GF2EX& x, const GF2EX& a, const GF2EX& f);GF2EX MulByXMod(const GF2EX& a, const GF2EX& f);// x = (a * X) mod fvoid InvMod(GF2EX& x, const GF2EX& a, const GF2EX& f);GF2EX InvMod(const GF2EX& a, const GF2EX& f);// x = a^{-1} % f, error is a is not invertiblelong InvModStatus(GF2EX& x, const GF2EX& a, const GF2EX& f);// if (a, f) = 1, returns 0 and sets x = a^{-1} % f; otherwise,// returns 1 and sets x = (a, f)/**************************************************************************\ Modular Arithmetic with Pre-ConditioningIf you need to do a lot of arithmetic modulo a fixed f, buildGF2EXModulus F for f. This pre-computes information about f thatspeeds up subsequent computations.As an example, the following routine the product modulo f of a vectorof polynomials.#include <NTL/GF2EX.h>void product(GF2EX& x, const vec_GF2EX& v, const GF2EX& f){ GF2EXModulus F(f); GF2EX res; res = 1; long i; for (i = 0; i < v.length(); i++) MulMod(res, res, v[i], F); x = res;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -