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

📄 lzz_px.txt

📁 密码大家Shoup写的数论算法c语言实现
💻 TXT
📖 第 1 页 / 共 2 页
字号:
\**************************************************************************/class zz_pXModulus {public:   zz_pXModulus(); // initially in an unusable state   ~zz_pXModulus();   zz_pXModulus(const zz_pXModulus&);  // copy   zz_pXModulus& operator=(const zz_pXModulus&);  // assignment   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 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 "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);}Note that a (trivial) conversion operator from zz_pXMultiplier to zz_pXis provided, so that a zz_pXMultiplier can be used in a contextwhere a zz_pX is required.\**************************************************************************/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&);   zz_pXMultiplier& operator=(const zz_pXMultiplier&);   ~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);zz_pX MulMod(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 CompMod3(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) < nvoid 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.// Input and output may have "high order" zeroes stripped.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: a.length() <= deg(F).// This is "transposed" MulMod by B.// Input vector may have "high order" zeroes striped.// The 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);// 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 IrredPoly(zz_pX& h, const zz_pX& g, const zz_pXModulus& F, long m);zz_pX IrredPoly(const zz_pX& g, const zz_pXModulus& F, long m);void IrredPoly(zz_pX& h, const zz_pX& g, const zz_pXModulus& F);zz_pX IrredPoly(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 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_pX resultant(zz_p& x, 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.  For irreducible f,// is it faster to use IrredPolyMod, and then exponentiate as// necessary, since in this case the characterstic polynomial// is a power of the minimal polynomial./**************************************************************************\                           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& 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 + -