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

📄 zz_px.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
   for (i = 0; i < v.length(); i++)      MulMod(res, res, v[i], F);    x = res;}Note that automatic conversions are provided so that a ZZ_pX canbe used wherever a ZZ_pXModulus is required, and a ZZ_pXModuluscan be used wherever a ZZ_pX is required.\**************************************************************************/class ZZ_pXModulus {public:   ZZ_pXModulus(); // initially in an unusable state   ZZ_pXModulus(const ZZ_pXModulus&);  // copy   ZZ_pXModulus& operator=(const ZZ_pXModulus&); // assignment   ~ZZ_pXModulus();   ZZ_pXModulus(const ZZ_pX& f); // initialize with f, deg(f) > 0   operator const ZZ_pX& () const;    // read-only access to f, implicit conversion operator   const ZZ_pX& val() const;    // read-only access to f, explicit notation};void build(ZZ_pXModulus& F, const ZZ_pX& f);// pre-computes information about f and stores it in F.// Note that the declaration ZZ_pXModulus F(f) is equivalent to// ZZ_pXModulus 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 ZZ_pXModulus& F);  // return n=deg(f)void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pX& b, const ZZ_pXModulus& F);ZZ_pX MulMod(const ZZ_pX& a, const ZZ_pX& b, const ZZ_pXModulus& F);// x = (a * b) % f; deg(a), deg(b) < nvoid SqrMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F);ZZ_pX SqrMod(const ZZ_pX& a, const ZZ_pXModulus& F);// x = a^2 % f; deg(a) < nvoid PowerMod(ZZ_pX& x, const ZZ_pX& a, const ZZ& e, const ZZ_pXModulus& F);ZZ_pX PowerMod(const ZZ_pX& a, const ZZ& e, const ZZ_pXModulus& F);void PowerMod(ZZ_pX& x, const ZZ_pX& a, long e, const ZZ_pXModulus& F);ZZ_pX PowerMod(const ZZ_pX& a, long e, const ZZ_pXModulus& F);// x = a^e % f; deg(a) < n (e may be negative)void PowerXMod(ZZ_pX& x, const ZZ& e, const ZZ_pXModulus& F);ZZ_pX PowerXMod(const ZZ& e, const ZZ_pXModulus& F);void PowerXMod(ZZ_pX& x, long e, const ZZ_pXModulus& F);ZZ_pX PowerXMod(long e, const ZZ_pXModulus& F);// x = X^e % f (e may be negative)void PowerXPlusAMod(ZZ_pX& x, const ZZ_p& a, const ZZ& e,                     const ZZ_pXModulus& F);ZZ_pX PowerXPlusAMod(const ZZ_p& a, const ZZ& e,                            const ZZ_pXModulus& F);void PowerXPlusAMod(ZZ_pX& x, const ZZ_p& a, long e,                     const ZZ_pXModulus& F);ZZ_pX PowerXPlusAMod(const ZZ_p& a, long e,                            const ZZ_pXModulus& F);// x = (X + a)^e % f (e may be negative)void rem(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXModulus& F);// x = a % fvoid DivRem(ZZ_pX& q, ZZ_pX& r, const ZZ_pX& a, const ZZ_pXModulus& F);// q = a/f, r = a%fvoid div(ZZ_pX& q, const ZZ_pX& a, const ZZ_pXModulus& F);// q = a/f// operator notation:ZZ_pX operator/(const ZZ_pX& a, const ZZ_pXModulus& F);ZZ_pX operator%(const ZZ_pX& a, const ZZ_pXModulus& F);ZZ_pX& operator/=(ZZ_pX& x, const ZZ_pXModulus& F);ZZ_pX& operator%=(ZZ_pX& x, const ZZ_pXModulus& F);/**************************************************************************\                                More Pre-ConditioningIf you need to compute a * b % f for a fixed b, but for many a's, itis much more efficient to first build a ZZ_pXMultiplier B for b, andthen use the MulMod routine below.Here is an example that multiplies each element of a vector by a fixedpolynomial modulo f.#include <NTL/ZZ_pX.h>void mul(vec_ZZ_pX& v, const ZZ_pX& b, const ZZ_pX& f){   ZZ_pXModulus F(f);   ZZ_pXMultiplier B(b, F);   long i;   for (i = 0; i < v.length(); i++)      MulMod(v[i], v[i], B, F);}\**************************************************************************/class ZZ_pXMultiplier {public:   ZZ_pXMultiplier(); // initially zero   ZZ_pXMultiplier(const ZZ_pX& b, const ZZ_pXModulus& F);      // initializes with b mod F, where deg(b) < deg(F)   ZZ_pXMultiplier(const ZZ_pXMultiplier&);  // copy   ZZ_pXMultiplier& operator=(const ZZ_pXMultiplier&);  // assignment   ~ZZ_pXMultiplier();   const ZZ_pX& val() const; // read-only access to b};void build(ZZ_pXMultiplier& B, const ZZ_pX& b, const ZZ_pXModulus& F);// pre-computes information about b and stores it in B; deg(b) <// deg(F)void MulMod(ZZ_pX& x, const ZZ_pX& a, const ZZ_pXMultiplier& B,                                      const ZZ_pXModulus& F);// x = (a * b) % F; deg(a) < deg(F)/**************************************************************************\                             vectors of ZZ_pX's\**************************************************************************/NTL_vector_decl(ZZ_pX,vec_ZZ_pX)// vec_ZZ_pXNTL_eq_vector_decl(ZZ_pX,vec_ZZ_pX)// == and !=NTL_io_vector_decl(ZZ_pX,vec_ZZ_pX)// 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(ZZ_pX& x, const ZZ_pX& g, const ZZ_pX& h, const ZZ_pXModulus& F);ZZ_pX CompMod(const ZZ_pX& g, const ZZ_pX& h,                     const ZZ_pXModulus& F);// x = g(h) mod f; deg(h) < nvoid Comp2Mod(ZZ_pX& x1, ZZ_pX& x2, const ZZ_pX& g1, const ZZ_pX& g2,              const ZZ_pX& h, const ZZ_pXModulus& F);// xi = gi(h) mod f (i=1,2); deg(h) < n.void Comp3Mod(ZZ_pX& x1, ZZ_pX& x2, ZZ_pX& x3,               const ZZ_pX& g1, const ZZ_pX& g2, const ZZ_pX& g3,              const ZZ_pX& h, const ZZ_pXModulus& 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 ZZ_pXArgument 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 ZZ_pXArgument {   vec_ZZ_pX H;};void build(ZZ_pXArgument& H, const ZZ_pX& h, const ZZ_pXModulus& F, long m);// Pre-Computes information about h.  m > 0, deg(h) < n.void CompMod(ZZ_pX& x, const ZZ_pX& g, const ZZ_pXArgument& H,              const ZZ_pXModulus& F);ZZ_pX CompMod(const ZZ_pX& g, const ZZ_pXArgument& H,                     const ZZ_pXModulus& F);extern long ZZ_pXArgBound;// Initially 0.  If this is set to a value greater than zero, then// composition routines will allocate a table of no than about// ZZ_pXArgBound KB.  Setting this value affects all compose routines// and the power projection and minimal polynomial routines below, // and indirectly affects many routines in ZZ_pXFactoring./**************************************************************************\                     power projection routines\**************************************************************************/void project(ZZ_p& x, const ZZ_pVector& a, const ZZ_pX& b);ZZ_p project(const ZZ_pVector& a, const ZZ_pX& b);// x = inner product of a with coefficient vector of bvoid ProjectPowers(vec_ZZ_p& x, const vec_ZZ_p& a, long k,                   const ZZ_pX& h, const ZZ_pXModulus& F);vec_ZZ_p ProjectPowers(const vec_ZZ_p& a, long k,                   const ZZ_pX& h, const ZZ_pXModulus& 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_ZZ_p& x, const vec_ZZ_p& a, long k,                   const ZZ_pXArgument& H, const ZZ_pXModulus& F);vec_ZZ_p ProjectPowers(const vec_ZZ_p& a, long k,                   const ZZ_pXArgument& H, const ZZ_pXModulus& F);// same as above, but uses a pre-computed ZZ_pXArgumentvoid UpdateMap(vec_ZZ_p& x, const vec_ZZ_p& a,               const ZZ_pXMultiplier& B, const ZZ_pXModulus& F);vec_ZZ_p UpdateMap(const vec_ZZ_p& a,               const ZZ_pXMultiplier& B, const ZZ_pXModulus& F);// Computes the vector//    project(a, b), project(a, (b*X)%f), ..., project(a, (b*X^{n-1})%f)// Restriction: must have a.length() <= deg(F).// This is "transposed" MulMod by B.// Input may have "high order" zeroes stripped.// Output will always have high order zeroes stripped./**************************************************************************\                              Minimum PolynomialsThese routines should be used with prime p.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(ZZ_pX& h, const vec_ZZ_p& a, long m);ZZ_pX MinPolySeq(const vec_ZZ_p& 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(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m);ZZ_pX ProbMinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m);void ProbMinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F);ZZ_pX ProbMinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& 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/p.void MinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m);ZZ_pX MinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m);void MinPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F);ZZ_pX MinPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F);// same as above, but guarantees that result is correctvoid IrredPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F, long m);ZZ_pX IrredPolyMod(const ZZ_pX& g, const ZZ_pXModulus& F, long m);void IrredPolyMod(ZZ_pX& h, const ZZ_pX& g, const ZZ_pXModulus& F);ZZ_pX IrredPolyMod(const ZZ_pX& g, const ZZ_pXModulus& 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)./**************************************************************************\                   Traces, norms, resultantsThese routines should be used with prime p.\**************************************************************************/void TraceMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pXModulus& F);ZZ_p TraceMod(const ZZ_pX& a, const ZZ_pXModulus& F);void TraceMod(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& f);ZZ_p TraceMod(const ZZ_pX& a, const ZZ_pXModulus& f);// x = Trace(a mod f); deg(a) < deg(f)void TraceVec(vec_ZZ_p& S, const ZZ_pX& f);vec_ZZ_p TraceVec(const ZZ_pX& 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(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& f);ZZ_p NormMod(const ZZ_pX& a, const ZZ_pX& f);// x = Norm(a mod f); 0 < deg(f), deg(a) < deg(f)void resultant(ZZ_p& x, const ZZ_pX& a, const ZZ_pX& b);ZZ_p resultant(const ZZ_pX& a, const ZZ_pX& b);// x = resultant(a, b)void CharPolyMod(ZZ_pX& g, const ZZ_pX& a, const ZZ_pX& f);ZZ_pX CharPolyMod(const ZZ_pX& a, const ZZ_pX& f);// g = charcteristic polynomial of (a mod f); 0 < deg(f), deg(g) <// deg(f);  this routine works for arbitrary f;  if f is irreducible,// it is faster to use the IrredPolyMod routine, and then exponentiate// if necessary (since in this case the CharPoly is just a power of// the IrredPoly)./**************************************************************************\                           MiscellanyA ZZ_pX f is represented as a vec_ZZ_p, 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(ZZ_pX& x) // x = 0void set(ZZ_pX& x); // x = 1void ZZ_pX::normalize();  // f.normalize() strips leading zeros from f.rep.void ZZ_pX::SetMaxLength(long n);// f.SetMaxLength(n) pre-allocate spaces for n coefficients.  The// polynomial that f represents is unchanged.void ZZ_pX::kill();// f.kill() sets f to 0 and frees all memory held by f; Equivalent to// f.rep.kill().ZZ_pX::ZZ_pX(INIT_SIZE_TYPE, long n);// ZZ_pX(INIT_SIZE, n) initializes to zero, but space is pre-allocated// for n coefficientsstatic const ZZ_pX& ZZ_pX::zero();// ZZ_pX::zero() is a read-only reference to 0void swap(ZZ_pX& x, ZZ_pX& y); // swap x and y (via "pointer swapping")

⌨️ 快捷键说明

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