/usr/include/givaro/givpoly1proot.inl is in libgivaro-dev 3.2.13-1.2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | // =================================================================== //
// Givaro / Athapascan-1
// Irreducible polynomial finder
// Primitive root finder
// Time-stamp: <10 Jul 07 15:31:18 Jean-Guillaume.Dumas@imag.fr>
// =================================================================== //
#ifndef _GIVARO_POLY_PRIMITIVE_ROOT_
#define _GIVARO_POLY_PRIMITIVE_ROOT_
// Reasonible cyclotomic polynomial size
#define CYCLO_DEGREE_BOUND 1000
#define CYCLO_TIMES_FACTOR 8
#include <stdlib.h>
#include <list>
#include <vector>
#include <givaro/givinteger.h>
#include <givaro/givintnumtheo.h>
#include <givaro/givdegree.h>
#include <givaro/givpoly1factor.h>
#include <givaro/givpoly1cyclo.inl>
// ---------------------------------------------------------------
// Monic irreducible polynomial of degree n over Z/pZ
// having 2, 3 nonzero terms or dividing a cyclotomic polynomial
// of degree < CYCLO_DEGREE_BOUND or a random one.
// ---------------------------------------------------------------
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Element& Poly1FactorDom<Domain,Tag, RandIter>::creux_random_irreducible (Element& R, Degree n) const {
init(R, n, _domain.one);
Residu_t MOD = _domain.residu();
// Search for an irreducible BINOMIAL : X^n + a
// WARNING : Here we may have X^n + x,
// where a = representation of x, and sometimes a != x.
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R))
return R;
}
// Search for an irreducible TRINOMIAL : X^n + b*X^i + a
// Precondition : n >= 2
// WARNING : same warning as for the binomial.
// JGD 21.10.02
// for(Residu_t d=2;d<n.value();++d) {
for(long d=1;d<=(n.value()/2);++d) {
for(Residu_t b=0; b<MOD; ++b) {
_domain.assign(R[d],b);
for(Residu_t a=1; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R))
return R;
}
}
// _domain.assign(R[0],_domain.zero);
// JGD 21.10.02
_domain.assign(R[d],_domain.zero);
}
// Search for a monic irreducible Polynomial
// with random Elements
do {
this->random( (RandIter&)_g, R, n); // must cast away const
_domain.assign(R[n.value()],_domain.one);
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R))
return R;
}
} while(1);
}
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Element& Poly1FactorDom<Domain,Tag, RandIter>::random_irreducible (Element& R, Degree n) const {
// Search for a monic irreducible Polynomial
// with random Elements
init(R, n, _domain.one);
Residu_t MOD = _domain.residu();
do {
this->random( (RandIter&)_g, R, n); // must cast away const
_domain.assign(R[n.value()],_domain.one);
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R))
return R;
}
} while(1);
}
// ---------------------------------------------------------------
// Monic irreducible polynomial of degree n over Z/pZ
// having 2, 3 nonzero terms or or a random one,
// with X as a primitive root.
// ---------------------------------------------------------------
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Element& Poly1FactorDom<Domain,Tag, RandIter>::ixe_irreducible (Element& R, Degree n) const {
init(R, n, _domain.one);
Element IXE;
init(IXE,Degree(1),_domain.one);
Residu_t MOD = _domain.residu();
// Search for an irreducible BINOMIAL : X^n + a
// WARNING : Here we may have X^n + x,
// where a = representation of x, and sometimes a != x.
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R) && (is_prim_root(IXE,R) ))
return R;
}
// Search for an irreducible TRINOMIAL : X^n + b*X^i + a
// Precondition : n >= 2
// WARNING : same warning as for the binomial.
// // JGD 21.10.02
// for(unsigned long d=2;d<n.value();++d) {
for(long d=2;d<=(n.value()/2);++d) {
for(Residu_t b=0; b<MOD; ++b) {
_domain.assign(R[d],b);
for(Residu_t a=1; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R) && (is_prim_root(IXE,R) ))
return R;
}
}
// _domain.assign(R[0],_domain.zero);
// JGD 21.10.02
_domain.assign(R[d],_domain.zero);
}
// Search for a monic irreducible Polynomial
// with random Elements
do {
this->random( (RandIter&)_g, R, n); // must cast away const
_domain.assign(R[n.value()],_domain.one);
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible(R) && (is_prim_root(IXE,R) ))
return R;
}
} while(1);
}
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Element& Poly1FactorDom<Domain,Tag, RandIter>::ixe_irreducible2 (Element& R, Degree n) const {
init(R, n, _domain.one);
Element IXE;
init(IXE,Degree(1),_domain.one);
Residu_t MOD = _domain.residu();
// Search for an irreducible BINOMIAL : X^n + a
// WARNING : Here we may have X^n + x,
// where a = representation of x, and sometimes a != x.
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible2(R) && (is_prim_root(IXE,R) ))
return R;
}
// Search for an irreducible TRINOMIAL : X^n + b*X^i + a
// Precondition : n >= 2
// WARNING : same warning as for the binomial.
for(Residu_t d=2;d<n.value();++d) {
for(Residu_t b=0; b<MOD; ++b) {
_domain.assign(R[d],b);
for(Residu_t a=1; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible2(R) && (is_prim_root(IXE,R) ))
return R;
}
}
_domain.assign(R[0],_domain.zero);
}
// Search for a monic irreducible Polynomial
// with random Elements
do {
this->random( (RandIter&)_g, R, n); // must cast away const
_domain.assign(R[n.value()],_domain.one);
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_irreducible2(R) && (is_prim_root(IXE,R) ))
return R;
}
} while(1);
}
// ---------------------------------------------------------------
// Irreducibility tests
// ---------------------------------------------------------------
template<class Domain, class Tag, class RandIter>
inline bool Poly1FactorDom<Domain,Tag, RandIter>::is_irreducible2(
const Rep& P
, Residu_t MOD ) const {
// Square free ?
Rep W,D; this->gcd(W,diff(D,P),P);
Degree d, dP;
if (degree(d,W) > 0) return 0;
IntFactorDom<> FD;
long n = degree(dP,P).value();
IntFactorDom<>::Rep qn;
FD.pow( qn, IntFactorDom<>::Rep(MOD), n);
Rep Unit, G1; init(Unit, Degree(1), _domain.one);
this->powmod(G1, Unit, qn, P);
if (degree(d, sub(D,G1,Unit)) >= 0) return 0;
std::vector<IntFactorDom<>::Rep> Lp; std::vector<unsigned long> Le;
FD.set(Lp, Le, n );
for( std::vector<IntFactorDom<>::Rep>::const_iterator p = Lp.begin(); p != Lp.end(); ++p) {
long ttmp;
FD.pow( qn, IntFactorDom<>::Rep(MOD), n/FD.convert(ttmp,*p) );
this->powmod(G1, Unit, qn, P);
if (degree(d, sub(D,G1,Unit)) < 0) return 0;
}
return 1;
}
// ---------------------------------------------------------------
// Primitive Root over Z/pZ / F
// returns 1 if P is a generator.
// ---------------------------------------------------------------
template<class Domain, class Tag, class RandIter>
bool Poly1FactorDom<Domain,Tag, RandIter>::is_prim_root( const Rep& P, const Rep& F) const {
bool isproot = 0;
Rep A, G; mod(A,P,F);
Degree d;
if ( degree(d, this->gcd(G,A,F)) == 0) {
Residu_t MOD = _domain.residu();
IntFactorDom<> FD;
IntFactorDom<>::Element IMOD( MOD ), q, qp;
degree(d,F);
// FD.pow(q ,IMOD, d.value());
// FD.sub(qp, q, FD.one);
FD.subin( FD.pow(qp ,IMOD, d.value()) , FD.one);
std::list< IntFactorDom<>::Element > L;
FD.set(L, qp);
L.sort();
std::list< IntFactorDom<>::Element >::iterator li = L.begin();
isproot = 1;
for(;(li != L.end()) && isproot; ++li)
isproot = ( ! this->isOne(this->powmod(G, A, FD.div(q, qp , *li), F) ) );
}
return isproot;
}
template<class Domain, class Tag, class RandIter>
inline typename IntegerDom::Element Poly1FactorDom<Domain,Tag, RandIter>::order( const Rep& P, const Rep& F) const {
bool isproot = 0;
Rep A, G; mod(A,P,F);
Degree d;
if ( degree(d, this->gcd(G,A,F)) == 0) {
Residu_t MOD = _domain.residu();
IntFactorDom<> FD;
IntFactorDom<>::Element IMOD( MOD ), g, gg, tt, qp;
degree(d,F);
// FD.pow(q ,IMOD, d.value());
// FD.sub(qp, q, FD.one);
FD.subin( FD.pow(qp ,IMOD, d.value()) , FD.one);
std::list< IntFactorDom<>::Element > L;
FD.set(L, qp);
L.sort();
std::list< IntFactorDom<>::Element >::iterator li = L.begin();
isproot = 1;
for(;(li != L.end()) && isproot; ++li)
isproot = ( ! this->isOne(this->powmod(G, A, FD.div(g, qp , *li), F) ) );
if (isproot)
return qp;
else {
for(--li;li!=L.end();++li)
while ( FD.isZero(FD.mod(tt,g,*li)) && (this->isOne(this->powmod(G, A, FD.div(gg,g,*li), F))))
g.copy(gg);
return g;
}
}
IntegerDom ID;
return ID.zero;
}
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Rep& Poly1FactorDom<Domain,Tag, RandIter>::give_prim_root(Rep& R, const Rep& F) const {
Degree n; degree(n,F);
Residu_t MOD = _domain.residu();
// this->write(std::cout << "Give Pr: ", F) << std::endl;
// Search for a primitive BINOMIAL : X^i + a
for(Degree di=1;di<n;++di) {
init(R, di, _domain.one);
// for(Residu_t a=MOD; a--; ) {
for(Residu_t a=0; a<MOD;++a ) {
_domain.assign(R[0],a);
if (is_prim_root(R,F))
return R;
}
}
// Search for a primitive TRINOMIAL : X^i + b*X^j + a
for(Degree di=2;di<n;++di) {
init(R, di, _domain.one);
for(Degree dj=1;dj<di;++dj)
// for(Residu_t b=MOD; b--;) {
for(Residu_t b=0; b<MOD;++b) {
_domain.assign(R[dj.value()],b);
// for(Residu_t a=MOD; a--;) {
for(Residu_t a=0; a<MOD;++a ) {
_domain.assign(R[0],a);
if (is_prim_root(R,F))
return R;
}
}
}
// Search for a primitive Polynomial
// with random Elements
do {
this->random( (RandIter&)_g, R, n); // must cast away const
_domain.assign(R[n.value()],_domain.one);
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_prim_root(R,F))
return R;
}
} while(1);
}
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Rep& Poly1FactorDom<Domain,Tag, RandIter>::give_random_prim_root(Rep& R, const Rep& F) const {
Degree n; degree(n,F);
Residu_t MOD = _domain.residu();
// Search for a primitive Polynomial
// with random Elements
do {
this->random( (RandIter&)_g, R, n); // must cast away const
_domain.assign(R[n.value()],_domain.one);
for(Residu_t a=0; a<MOD; ++a) {
_domain.assign(R[0],a);
if (is_prim_root(R,F))
return R;
}
} while(1);
}
template<class Domain, class Tag, class RandIter >
inline typename Poly1FactorDom<Domain,Tag, RandIter>::Rep& Poly1FactorDom<Domain,Tag, RandIter>::random_prim_root(Rep& P, Rep& R, Degree n) const {
// P is irreducible
// R is a primitive root. i.e R generates (Z_p)/P.
// returns R
return give_prim_root(R, random_irreducible(P,n));
}
#endif
|