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

📄 gf2ex.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
NOTE: A GF2EX may be used wherever a GF2EXModulus is required,and a GF2EXModulus may be used wherever a GF2EX is required.\**************************************************************************/class GF2EXModulus {public:   GF2EXModulus(); // initially in an unusable state   GF2EXModulus(const GF2EX& f); // initialize with f, deg(f) > 0   GF2EXModulus(const GF2EXModulus&); // copy   GF2EXModulus& operator=(const GF2EXModulus&); // assignment   ~GF2EXModulus(); // destructor   operator const GF2EX& () const; // implicit read-only access to f   const GF2EX& val() const; // explicit read-only access to f};void build(GF2EXModulus& F, const GF2EX& f);// pre-computes information about f and stores it in F.  Must have// deg(f) > 0.  Note that the declaration GF2EXModulus F(f) is// equivalent to GF2EXModulus F; build(F, f).// In the following, f refers to the polynomial f supplied to the// build routine, and n = deg(f).long deg(const GF2EXModulus& F);  // return n=deg(f)void MulMod(GF2EX& x, const GF2EX& a, const GF2EX& b, const GF2EXModulus& F);GF2EX MulMod(const GF2EX& a, const GF2EX& b, const GF2EXModulus& F);// x = (a * b) % f; deg(a), deg(b) < nvoid SqrMod(GF2EX& x, const GF2EX& a, const GF2EXModulus& F);GF2EX SqrMod(const GF2EX& a, const GF2EXModulus& F);// x = a^2 % f; deg(a) < nvoid PowerMod(GF2EX& x, const GF2EX& a, const ZZ& e, const GF2EXModulus& F);GF2EX PowerMod(const GF2EX& a, const ZZ& e, const GF2EXModulus& F);void PowerMod(GF2EX& x, const GF2EX& a, long e, const GF2EXModulus& F);GF2EX PowerMod(const GF2EX& a, long e, const GF2EXModulus& F);// x = a^e % f; e >= 0, deg(a) < n.  Uses a sliding window algorithm.// (e may be negative)void PowerXMod(GF2EX& x, const ZZ& e, const GF2EXModulus& F);GF2EX PowerXMod(const ZZ& e, const GF2EXModulus& F);void PowerXMod(GF2EX& x, long e, const GF2EXModulus& F);GF2EX PowerXMod(long e, const GF2EXModulus& F);// x = X^e % f (e may be negative)void rem(GF2EX& x, const GF2EX& a, const GF2EXModulus& F);// x = a % fvoid DivRem(GF2EX& q, GF2EX& r, const GF2EX& a, const GF2EXModulus& F);// q = a/f, r = a%fvoid div(GF2EX& q, const GF2EX& a, const GF2EXModulus& F);// q = a/f// operator notation:GF2EX operator/(const GF2EX& a, const GF2EXModulus& F);GF2EX operator%(const GF2EX& a, const GF2EXModulus& F);GF2EX& operator/=(GF2EX& x, const GF2EXModulus& F);GF2EX& operator%=(GF2EX& x, const GF2EXModulus& F);/**************************************************************************\                             vectors of GF2EX's\**************************************************************************/NTL_vector_decl(GF2EX,vec_GF2EX)// vec_GF2EXNTL_eq_vector_decl(GF2EX,vec_GF2EX)// == and !=NTL_io_vector_decl(GF2EX,vec_GF2EX)// I/O operators/**************************************************************************\                              Modular CompositionModular composition is the problem of computing g(h) mod f forpolynomials f, g, and h.The algorithm employed is that of Brent & Kung (Fast algorithms formanipulating formal power series, JACM 25:581-595, 1978), which usesO(n^{1/2}) modular polynomial multiplications, and O(n^2) scalaroperations.\**************************************************************************/void CompMod(GF2EX& x, const GF2EX& g, const GF2EX& h, const GF2EXModulus& F);GF2EX CompMod(const GF2EX& g, const GF2EX& h,                     const GF2EXModulus& F);// x = g(h) mod f; deg(h) < nvoid Comp2Mod(GF2EX& x1, GF2EX& x2, const GF2EX& g1, const GF2EX& g2,              const GF2EX& h, const GF2EXModulus& F);// xi = gi(h) mod f (i=1,2); deg(h) < n.void Comp3Mod(GF2EX& x1, GF2EX& x2, GF2EX& x3,               const GF2EX& g1, const GF2EX& g2, const GF2EX& g3,              const GF2EX& h, const GF2EXModulus& F);// xi = gi(h) mod f (i=1..3); deg(h) < n./**************************************************************************\                     Composition with Pre-ConditioningIf a single h is going to be used with many g's then you should builda GF2EXArgument for h, and then use the compose routine below.  Theroutine build computes and stores h, h^2, ..., h^m mod f.  After thispre-computation, composing a polynomial of degree roughly n with htakes n/m multiplies mod f, plus n^2 scalar multiplies.  Thus,increasing m increases the space requirement and the pre-computationtime, but reduces the composition time.\**************************************************************************/struct GF2EXArgument {   vec_GF2EX H;};void build(GF2EXArgument& H, const GF2EX& h, const GF2EXModulus& F, long m);// Pre-Computes information about h.  m > 0, deg(h) < n.void CompMod(GF2EX& x, const GF2EX& g, const GF2EXArgument& H,              const GF2EXModulus& F);GF2EX CompMod(const GF2EX& g, const GF2EXArgument& H,                     const GF2EXModulus& F);extern long GF2EXArgBound;// Initially 0.  If this is set to a value greater than zero, then// composition routines will allocate a table of no than about// GF2EXArgBound KB.  Setting this value affects all compose routines// and the power projection and minimal polynomial routines below, // and indirectly affects many routines in GF2EXFactoring./**************************************************************************\                     power projection routines\**************************************************************************/void project(GF2E& x, const GF2EVector& a, const GF2EX& b);GF2E project(const GF2EVector& a, const GF2EX& b);// x = inner product of a with coefficient vector of bvoid ProjectPowers(vec_GF2E& x, const vec_GF2E& a, long k,                   const GF2EX& h, const GF2EXModulus& F);vec_GF2E ProjectPowers(const vec_GF2E& a, long k,                   const GF2EX& h, const GF2EXModulus& F);// Computes the vector//    project(a, 1), project(a, h), ..., project(a, h^{k-1} % f).  // This operation is the "transpose" of the modular composition operation.void ProjectPowers(vec_GF2E& x, const vec_GF2E& a, long k,                   const GF2EXArgument& H, const GF2EXModulus& F);vec_GF2E ProjectPowers(const vec_GF2E& a, long k,                   const GF2EXArgument& H, const GF2EXModulus& F);// same as above, but uses a pre-computed GF2EXArgumentclass GF2EXTransMultiplier { /* ... */ };void build(GF2EXTransMultiplier& B, const GF2EX& b, const GF2EXModulus& F);void UpdateMap(vec_GF2E& x, const vec_GF2E& a,               const GF2EXMultiplier& B, const GF2EXModulus& F);vec_GF2E UpdateMap(const vec_GF2E& a,               const GF2EXMultiplier& B, const GF2EXModulus& F);// Computes the vector//    project(a, b), project(a, (b*X)%f), ..., project(a, (b*X^{n-1})%f)// Restriction: a.length() <= deg(F), deg(b) < deg(F).// This is "transposed" MulMod by B.// Input may have "high order" zeroes stripped.// Output always has high order zeroes stripped./**************************************************************************\                              Minimum PolynomialsThese routines should be used only when GF2E is a field.All of these routines implement the algorithm from [Shoup, J. SymbolicComp. 17:371-391, 1994] and [Shoup, J. Symbolic Comp. 20:363-397,1995], based on transposed modular composition and theBerlekamp/Massey algorithm.\**************************************************************************/void MinPolySeq(GF2EX& h, const vec_GF2E& a, long m);GF2EX MinPolySeq(const vec_GF2E& a, long m);// computes the minimum polynomial of a linealy generated sequence; m// is a bound on the degree of the polynomial; required: a.length() >=// 2*mvoid ProbMinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F, long m);GF2EX ProbMinPolyMod(const GF2EX& g, const GF2EXModulus& F, long m);void ProbMinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F);GF2EX ProbMinPolyMod(const GF2EX& g, const GF2EXModulus& F);// computes the monic minimal polynomial if (g mod f).  m = a bound on// the degree of the minimal polynomial; in the second version, this// argument defaults to n.  The algorithm is probabilistic, always// returns a divisor of the minimal polynomial, and returns a proper// divisor with probability at most m/2^{GF2E::degree()}.void MinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F, long m);GF2EX MinPolyMod(const GF2EX& g, const GF2EXModulus& F, long m);void MinPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F);GF2EX MinPolyMod(const GF2EX& g, const GF2EXModulus& F);// same as above, but guarantees that result is correctvoid IrredPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F, long m);GF2EX IrredPolyMod(const GF2EX& g, const GF2EXModulus& F, long m);void IrredPolyMod(GF2EX& h, const GF2EX& g, const GF2EXModulus& F);GF2EX IrredPolyMod(const GF2EX& g, const GF2EXModulus& F);// same as above, but assumes that f is irreducible, or at least that// the minimal poly of g is itself irreducible.  The algorithm is// deterministic (and is always correct)./**************************************************************************\           Composition and Minimal Polynomials in towersThese are implementations of algorithms that will be describedand analyzed in a forthcoming paper.GF2E need not be a field.\**************************************************************************/void CompTower(GF2EX& x, const GF2X& g, const GF2EXArgument& h,             const GF2EXModulus& F);GF2EX CompTower(const GF2X& g, const GF2EXArgument& h,             const GF2EXModulus& F);void CompTower(GF2EX& x, const GF2X& g, const GF2EX& h,             const GF2EXModulus& F);GF2EX CompTower(const GF2X& g, const GF2EX& h,             const GF2EXModulus& F);// x = g(h) mod fvoid ProbMinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F,                      long m);GF2X ProbMinPolyTower(const GF2EX& g, const GF2EXModulus& F, long m);void ProbMinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F);GF2X ProbMinPolyTower(const GF2EX& g, const GF2EXModulus& F);// Uses a probabilistic algorithm to compute the minimal// polynomial of (g mod f) over GF2.// The parameter m is a bound on the degree of the minimal polynomial// (default = deg(f)*GF2E::degree()).// In general, the result will be a divisor of the true minimimal// polynomial.  For correct results, use the MinPoly routines below.void MinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F, long m);GF2X MinPolyTower(const GF2EX& g, const GF2EXModulus& F, long m);void MinPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F);GF2X MinPolyTower(const GF2EX& g, const GF2EXModulus& F);// Same as above, but result is always correct.void IrredPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F, long m);GF2X IrredPolyTower(const GF2EX& g, const GF2EXModulus& F, long m);void IrredPolyTower(GF2X& h, const GF2EX& g, const GF2EXModulus& F);GF2X IrredPolyTower(const GF2EX& g, const GF2EXModulus& F);// Same as above, but assumes the minimal polynomial is// irreducible, and uses a slightly faster, deterministic algorithm./**************************************************************************\                   Traces, norms, resultants\**************************************************************************/void TraceMod(GF2E& x, const GF2EX& a, const GF2EXModulus& F);GF2E TraceMod(const GF2EX& a, const GF2EXModulus& F);void TraceMod(GF2E& x, const GF2EX& a, const GF2EX& f);GF2E TraceMod(const GF2EX& a, const GF2EXModulus& f);// x = Trace(a mod f); deg(a) < deg(f)void TraceVec(vec_GF2E& S, const GF2EX& f);vec_GF2E TraceVec(const GF2EX& f);// S[i] = Trace(X^i mod f), i = 0..deg(f)-1; 0 < deg(f)// The above trace routines implement the asymptotically fast trace// algorithm from [von zur Gathen and Shoup, Computational Complexity,// 1992].void NormMod(GF2E& x, const GF2EX& a, const GF2EX& f);GF2E NormMod(const GF2EX& a, const GF2EX& f);// x = Norm(a mod f); 0 < deg(f), deg(a) < deg(f)void resultant(GF2E& x, const GF2EX& a, const GF2EX& b);GF2E resultant(const GF2EX& a, const GF2EX& b);// x = resultant(a, b)// NormMod and resultant require that GF2E is a field./**************************************************************************\                           MiscellanyA GF2EX f is represented as a vec_GF2E, which can be accessed asf.rep.  The constant term is f.rep[0] and the leading coefficient isf.rep[f.rep.length()-1], except if f is zero, in which casef.rep.length() == 0.  Note that the leading coefficient is alwaysnonzero (unless f is zero).  One can freely access and modify f.rep,but one should always ensure that the leading coefficient is nonzero,which can be done by invoking f.normalize().\**************************************************************************/void clear(GF2EX& x) // x = 0void set(GF2EX& x); // x = 1void GF2EX::normalize();  // f.normalize() strips leading zeros from f.rep.void GF2EX::SetMaxLength(long n);// f.SetMaxLength(n) pre-allocate spaces for n coefficients.  The// polynomial that f represents is unchanged.void GF2EX::kill();// f.kill() sets f to 0 and frees all memory held by f.  Equivalent to// f.rep.kill().GF2EX::GF2EX(INIT_SIZE_TYPE, long n);// GF2EX(INIT_SIZE, n) initializes to zero, but space is pre-allocated// for n coefficientsstatic const GF2EX& zero();// GF2EX::zero() is a read-only reference to 0void swap(GF2EX& x, GF2EX& y); // swap x and y (via "pointer swapping")

⌨️ 快捷键说明

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