⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 gf2x.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
/**************************************************************************\MODULE: GF2XSUMMARY:The class GF2X implements polynomial arithmetic modulo 2.Polynomial arithmetic is implemented using a combination of classicalroutines and Karatsuba.\**************************************************************************/#include <NTL/GF2.h>#include <NTL/vec_GF2.h>class GF2X {public:   GF2X(); // initial value 0   GF2X(const GF2X& a); // copy   GF2X& operator=(const GF2X& a); // assignment   GF2X& operator=(GF2 a);    GF2X& operator=(long a);    ~GF2X(); // destructor   GF2X(long i, GF2 c); // initialize to X^i*c   GF2X(long i, long c);    };// SIZE INVARIANT: for any f in GF2X, def(f)+1 < 2^(NTL_BITS_PER_LONG-4)./**************************************************************************\                                  Comparison\**************************************************************************/long operator==(const GF2X& a, const GF2X& b);long operator!=(const GF2X& a, const GF2X& b);long IsZero(const GF2X& a); // test for 0long IsOne(const GF2X& a); // test for 1// PROMOTIONS: operators ==, != promote {long, GF2} to GF2X on (a, b)/**************************************************************************\                                   Addition\**************************************************************************/// operator notation:GF2X operator+(const GF2X& a, const GF2X& b);GF2X operator-(const GF2X& a, const GF2X& b);GF2X operator-(const GF2X& a); // unary -GF2X& operator+=(GF2X& x, const GF2X& a);GF2X& operator+=(GF2X& x, GF2 a);GF2X& operator+=(GF2X& x, long a);GF2X& operator-=(GF2X& x, const GF2X& a);GF2X& operator-=(GF2X& x, GF2 a);GF2X& operator-=(GF2X& x, long a);GF2X& operator++(GF2X& x);  // prefixvoid operator++(GF2X& x, int);  // postfixGF2X& operator--(GF2X& x);  // prefixvoid operator--(GF2X& x, int);  // postfix// procedural versions:void add(GF2X& x, const GF2X& a, const GF2X& b); // x = a + bvoid sub(GF2X& x, const GF2X& a, const GF2X& b); // x = a - bvoid negate(GF2X& x, const GF2X& a); // x = -a// PROMOTIONS: binary +, - and procedures add, sub promote {long, GF2}// to GF2X on (a, b)./**************************************************************************\                               Multiplication\**************************************************************************/// operator notation:GF2X operator*(const GF2X& a, const GF2X& b);GF2X& operator*=(GF2X& x, const GF2X& a);GF2X& operator*=(GF2X& x, GF2 a);GF2X& operator*=(GF2X& x, long a);// procedural versions:void mul(GF2X& x, const GF2X& a, const GF2X& b); // x = a * bvoid sqr(GF2X& x, const GF2X& a); // x = a^2GF2X sqr(const GF2X& a);// PROMOTIONS: operator * and procedure mul promote {long, GF2} to GF2X// on (a, b)./**************************************************************************\                               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:GF2X operator<<(const GF2X& a, long n);GF2X operator>>(const GF2X& a, long n);GF2X& operator<<=(GF2X& x, long n);GF2X& operator>>=(GF2X& x, long n);// procedural versions:void LeftShift(GF2X& x, const GF2X& a, long n); GF2X LeftShift(const GF2X& a, long n);void RightShift(GF2X& x, const GF2X& a, long n); GF2X RightShift(const GF2X& a, long n); void MulByX(GF2X& x, const GF2X& a); GF2X MulByX(const GF2X& a); /**************************************************************************\                                  Division\**************************************************************************/// operator notation:GF2X operator/(const GF2X& a, const GF2X& b);GF2X operator%(const GF2X& a, const GF2X& b);GF2X& operator/=(GF2X& x, const GF2X& a);GF2X& operator/=(GF2X& x, GF2 a);GF2X& operator/=(GF2X& x, long a);GF2X& operator%=(GF2X& x, const GF2X& b);// procedural versions:void DivRem(GF2X& q, GF2X& r, const GF2X& a, const GF2X& b);// q = a/b, r = a%bvoid div(GF2X& q, const GF2X& a, const GF2X& b);// q = a/bvoid rem(GF2X& r, const GF2X& a, const GF2X& b);// r = a%blong divide(GF2X& q, const GF2X& a, const GF2X& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0long divide(const GF2X& a, const GF2X& b);// if b | a, sets q = a/b and returns 1; otherwise returns 0// PROMOTIONS: operator / and procedure div promote {long, GF2} to GF2X// on (a, b)./**************************************************************************\                                   GCD's\**************************************************************************/void GCD(GF2X& x, const GF2X& a, const GF2X& b);GF2X GCD(const GF2X& a, const GF2X& b); // x = GCD(a, b) (zero if a==b==0).void XGCD(GF2X& d, GF2X& s, GF2X& t, const GF2X& a, const GF2X& 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 0 or 1, anda_n not zero (the zero polynomial is [ ]).  On input, the coefficientsmay be arbitrary integers which are reduced modulo 2, and leading zerosstripped.There is also a more compact hex I/O format.  To output in thisformat, set GF2X::HexOutput to a nonzero value.  On input, if the firstnon-blank character read is 'x' or 'X', then a hex format is assumed.\**************************************************************************/istream& operator>>(istream& s, GF2X& x);ostream& operator<<(ostream& s, const GF2X& a);/**************************************************************************\                              Some utility routines\**************************************************************************/long deg(const GF2X& a);  // return deg(a); deg(0) == -1.GF2 coeff(const GF2X& a, long i);// returns the coefficient of X^i, or zero if i not in rangeGF2 LeadCoeff(const GF2X& a);// returns leading term of a, or zero if a == 0GF2 ConstTerm(const GF2X& a);// returns constant term of a, or zero if a == 0void SetCoeff(GF2X& x, long i, GF2 a);void SetCoeff(GF2X& x, long i, long a);// makes coefficient of X^i equal to a; error is raised if i < 0void SetCoeff(GF2X& x, long i);// makes coefficient of X^i equal to 1;  error is raised if i < 0void SetX(GF2X& x); // x is set to the monomial Xlong IsX(const GF2X& a); // test if x = Xvoid diff(GF2X& x, const GF2X& a);GF2X diff(const GF2X& a); // x = derivative of avoid reverse(GF2X& x, const GF2X& a, long hi);GF2X reverse(const GF2X& a, long hi);void reverse(GF2X& x, const GF2X& a);GF2X reverse(const GF2X& a);// x = reverse of a[0]..a[hi] (hi >= -1);// hi defaults to deg(a) in second versionvoid VectorCopy(vec_GF2& x, const GF2X& a, long n);vec_GF2 VectorCopy(const GF2X& a, long n);// x = copy of coefficient vector of a of length exactly n.// input is truncated or padded with zeroes as appropriate.// Note that there is also a conversion routine from GF2X to vec_GF2// that makes the length of the vector match the number of coefficients// of the polynomial.long weight(const GF2X& a);// returns the # of nonzero coefficients in avoid GF2XFromBytes(GF2X& x, const unsigned char *p, long n);GF2X GF2XFromBytes(const unsigned char *p, long n);// conversion from byte vector to polynomial.// x = sum(p[i]*X^(8*i), i = 0..n-1), where the bits of p[i] are interpretted// as a polynomial in the natural way (i.e., p[i] = 1 is interpretted as 1,// p[i] = 2 is interpretted as X, p[i] = 3 is interpretted as X+1, etc.).// In the unusual event that characters are wider than 8 bits,// only the low-order 8 bits of p[i] are used.void BytesFromGF2X(unsigned char *p, const GF2X& a, long n);// conversion from polynomial to byte vector.// p[0..n-1] are computed so that //     a = sum(p[i]*X^(8*i), i = 0..n-1) mod X^(8*n),// where the values p[i] are interpretted as polynomials as in GF2XFromBytes// above.long NumBits(const GF2X& a);// returns number of bits of a, i.e., deg(a) + 1.long NumBytes(const GF2X& a);// returns number of bytes of a, i.e., floor((NumBits(a)+7)/8)/**************************************************************************\                             Random Polynomials\**************************************************************************/void random(GF2X& x, long n);GF2X random_GF2X(long n);// x = random polynomial of degree < n /**************************************************************************\                       Arithmetic mod X^nRequired: n >= 0; otherwise, an error is raised.\**************************************************************************/void trunc(GF2X& x, const GF2X& a, long n); // x = a % X^nGF2X trunc(const GF2X& a, long n); void MulTrunc(GF2X& x, const GF2X& a, const GF2X& b, long n);GF2X MulTrunc(const GF2X& a, const GF2X& b, long n);// x = a * b % X^nvoid SqrTrunc(GF2X& x, const GF2X& a, long n);GF2X SqrTrunc(const GF2X& a, long n);// x = a^2 % X^nvoid InvTrunc(GF2X& x, const GF2X& a, long n);GF2X InvTrunc(const GF2X& a, long n);// computes x = a^{-1} % X^n.  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 theGF2XModulus data structure and associated routines below for betterperformance.\**************************************************************************/void MulMod(GF2X& x, const GF2X& a, const GF2X& b, const GF2X& f);GF2X MulMod(const GF2X& a, const GF2X& b, const GF2X& f);// x = (a * b) % fvoid SqrMod(GF2X& x, const GF2X& a, const GF2X& f);GF2X SqrMod(const GF2X& a, const GF2X& f);// x = a^2 % fvoid MulByXMod(GF2X& x, const GF2X& a, const GF2X& f);GF2X MulByXMod(const GF2X& a, const GF2X& f);// x = (a * X) mod fvoid InvMod(GF2X& x, const GF2X& a, const GF2X& f);

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -