/usr/include/NTL/ZZ_p.h is in libntl-dev 10.5.0-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 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 | #ifndef NTL_ZZ_p__H
#define NTL_ZZ_p__H
#include <NTL/ZZ.h>
#include <NTL/ZZVec.h>
#include <NTL/SmartPtr.h>
#include <NTL/Lazy.h>
NTL_OPEN_NNS
// ZZ_p representation: each ZZ_p is represented by a ZZ in the range 0..p-1.
class ZZ_pFFTInfoT {
private:
ZZ_pFFTInfoT(const ZZ_pFFTInfoT&); // disabled
void operator=(const ZZ_pFFTInfoT&); // disabled
public:
ZZ_pFFTInfoT() { }
long NumPrimes;
long MaxRoot;
ZZ MinusMModP; // -M mod p, M = product of primes
ZZ_CRTStructAdapter crt_struct;
ZZ_RemStructAdapter rem_struct;
// the following arrays are indexed 0..NumPrimes-1
// q[i] = FFTPrime[i]
Vec<long> prime; // prime[i] = q[i]
Vec<double> prime_recip; // prime_recip[i] = 1/double(q[i])
Vec<long> u; // u[i] = (M/q[i])^{-1} mod q[i]
Vec<mulmod_precon_t> uqinv;
ZZ_ReduceStructAdapter reduce_struct;
};
#ifndef NTL_WIZARD_HACK
class MatPrime_crt_helper;
void MatPrime_crt_helper_deleter(MatPrime_crt_helper*);
#endif
class ZZ_pInfoT {
private:
ZZ_pInfoT(); // disabled
ZZ_pInfoT(const ZZ_pInfoT&); // disabled
void operator=(const ZZ_pInfoT&); // disabled
public:
ZZ_pInfoT(const ZZ& NewP);
ZZ p; // the modulus
long size; // p.size()
long ExtendedModulusSize;
Lazy<ZZ_pFFTInfoT> FFTInfo;
#ifndef NTL_WIZARD_HACK
struct MatPrime_crt_helper_deleter_policy {
static void deleter(MatPrime_crt_helper *p) { MatPrime_crt_helper_deleter(p); }
};
Lazy<MatPrime_crt_helper,MatPrime_crt_helper_deleter_policy> MatPrime_crt_helper_info;
// PIMPL
#endif
};
// auxilliary data structures to store space for temporaries
// used by the crt and rem routines in the low-level lip module.
// These used to be stored in data structures managed by the
// lip module, but to achieve thread-safety, they have to be
// externally on a per-thread basis.
class ZZ_pTmpSpaceT {
public:
ZZ_TmpVecAdapter crt_tmp_vec;
ZZ_TmpVecAdapter rem_tmp_vec;
};
extern
NTL_CHEAP_THREAD_LOCAL
ZZ_pInfoT *ZZ_pInfo;
// info for current modulus, initially null
// plain pointer for faster TLS access
extern
NTL_CHEAP_THREAD_LOCAL
ZZ_pTmpSpaceT *ZZ_pTmpSpace;
// space for temps associated with current modulus,
// plain pointer for faster TLS access
extern
NTL_CHEAP_THREAD_LOCAL
bool ZZ_pInstalled;
// flag indicating if current modulus is fully installed
class ZZ_pContext {
private:
SmartPtr<ZZ_pInfoT> ptr;
public:
ZZ_pContext() { }
explicit ZZ_pContext(const ZZ& p) : ptr(MakeSmart<ZZ_pInfoT>(p)) { }
// copy constructor, assignment, destructor: default
void save();
void restore() const;
};
class ZZ_pBak {
private:
ZZ_pContext c;
bool MustRestore;
ZZ_pBak(const ZZ_pBak&); // disabled
void operator=(const ZZ_pBak&); // disabled
public:
void save();
void restore();
ZZ_pBak() : MustRestore(false) { }
~ZZ_pBak();
};
class ZZ_pPush {
private:
ZZ_pBak bak;
ZZ_pPush(const ZZ_pPush&); // disabled
void operator=(const ZZ_pPush&); // disabled
public:
ZZ_pPush() { bak.save(); }
explicit ZZ_pPush(const ZZ_pContext& context) { bak.save(); context.restore(); }
explicit ZZ_pPush(const ZZ& p) { bak.save(); ZZ_pContext c(p); c.restore(); }
};
class ZZ_pX; // forward declaration
class ZZ_p {
public:
typedef ZZ rep_type;
typedef ZZ_pContext context_type;
typedef ZZ_pBak bak_type;
typedef ZZ_pPush push_type;
typedef ZZ_pX poly_type;
ZZ _ZZ_p__rep;
static void init(const ZZ&);
typedef void (*DivHandlerPtr)(const ZZ_p& a); // error-handler for division
static
NTL_CHEAP_THREAD_LOCAL
DivHandlerPtr DivHandler;
// ****** constructors and assignment
ZZ_p() { } // NO_ALLOC
explicit ZZ_p(long a) { *this = a; }
ZZ_p(INIT_NO_ALLOC_TYPE) { } // allocates no space
ZZ_p(INIT_ALLOC_TYPE) { _ZZ_p__rep.SetSize(ZZ_pInfo->size); } // allocates space
~ZZ_p() { }
inline ZZ_p& operator=(long a);
// You can always access the _ZZ_p__representation directly...if you dare.
ZZ& LoopHole() { return _ZZ_p__rep; }
ZZ_p(ZZ_p& x, INIT_TRANS_TYPE) : _ZZ_p__rep(x._ZZ_p__rep, INIT_TRANS) { }
static const ZZ& modulus() { return ZZ_pInfo->p; }
static long ModulusSize() { return ZZ_pInfo->size; }
static long storage() { return ZZ_storage(ZZ_pInfo->size); }
static long ExtendedModulusSize() { return ZZ_pInfo->ExtendedModulusSize; }
static const ZZ_p& zero();
static void DoInstall();
static void install()
{
// we test and set ZZ_pInstalled here, to allow better
// inlining and optimization
if (!ZZ_pInstalled) { DoInstall(); ZZ_pInstalled = true; }
}
static const ZZ_pFFTInfoT* GetFFTInfo()
{
install();
return ZZ_pInfo->FFTInfo.get();
}
static ZZ_pTmpSpaceT* GetTmpSpace()
{
install();
return ZZ_pTmpSpace;
}
ZZ_p(INIT_VAL_TYPE, const ZZ& a);
ZZ_p(INIT_VAL_TYPE, long a);
void swap(ZZ_p& x)
{
_ZZ_p__rep.swap(x._ZZ_p__rep);
}
void allocate()
{
long sz = ZZ_pInfo->size;
if (_ZZ_p__rep.MaxAlloc() < sz)
_ZZ_p__rep.SetSize(sz);
}
// mainly for internal consumption by the ZZ_pWatcher class below
void KillBig() { _ZZ_p__rep.KillBig(); }
};
NTL_DECLARE_RELOCATABLE((ZZ_p*))
// read-only access to _ZZ_p__representation
inline const ZZ& rep(const ZZ_p& a) { return a._ZZ_p__rep; }
// ****** conversion
inline void conv(ZZ_p& x, const ZZ& a)
{ rem(x._ZZ_p__rep, a, ZZ_p::modulus()); }
inline ZZ_p to_ZZ_p(const ZZ& a)
{ return ZZ_p(INIT_VAL, a); }
void conv(ZZ_p& x, long a);
inline ZZ_p to_ZZ_p(long a)
{ return ZZ_p(INIT_VAL, a); }
// ****** some basics
inline void clear(ZZ_p& x)
// x = 0
{ clear(x._ZZ_p__rep); }
inline void set(ZZ_p& x)
// x = 1
{ set(x._ZZ_p__rep); }
inline void swap(ZZ_p& x, ZZ_p& y)
// swap x and y
{ x.swap(y); }
// ****** addition
inline void add(ZZ_p& x, const ZZ_p& a, const ZZ_p& b)
// x = a + b
{ AddMod(x._ZZ_p__rep, a._ZZ_p__rep, b._ZZ_p__rep, ZZ_p::modulus()); }
inline void sub(ZZ_p& x, const ZZ_p& a, const ZZ_p& b)
// x = a - b
{ SubMod(x._ZZ_p__rep, a._ZZ_p__rep, b._ZZ_p__rep, ZZ_p::modulus()); }
inline void negate(ZZ_p& x, const ZZ_p& a)
// x = -a
{ NegateMod(x._ZZ_p__rep, a._ZZ_p__rep, ZZ_p::modulus()); }
// scalar versions
void add(ZZ_p& x, const ZZ_p& a, long b);
inline void add(ZZ_p& x, long a, const ZZ_p& b) { add(x, b, a); }
void sub(ZZ_p& x, const ZZ_p& a, long b);
void sub(ZZ_p& x, long a, const ZZ_p& b);
// ****** multiplication
inline void mul(ZZ_p& x, const ZZ_p& a, const ZZ_p& b)
// x = a*b
{ MulMod(x._ZZ_p__rep, a._ZZ_p__rep, b._ZZ_p__rep, ZZ_p::modulus()); }
inline void sqr(ZZ_p& x, const ZZ_p& a)
// x = a^2
{ SqrMod(x._ZZ_p__rep, a._ZZ_p__rep, ZZ_p::modulus()); }
inline ZZ_p sqr(const ZZ_p& a)
{ ZZ_p x; sqr(x, a); NTL_OPT_RETURN(ZZ_p, x); }
// scalar versions
void mul(ZZ_p& x, const ZZ_p& a, long b);
inline void mul(ZZ_p& x, long a, const ZZ_p& b) { mul(x, b, a); }
// ****** division
void div(ZZ_p& x, const ZZ_p& a, const ZZ_p& b);
// x = a/b
// If b != 0 & b not invertible & DivHandler != 0,
// then DivHandler will be called with the offending b.
// In this case, of course, p is not really prime, and one
// can factor p by taking a gcd with rep(b).
// Otherwise, if b is not invertible, an error occurs.
void inv(ZZ_p& x, const ZZ_p& a);
// x = 1/a
// Error handling is the same as above.
inline ZZ_p inv(const ZZ_p& a)
{ ZZ_p x; inv(x, a); NTL_OPT_RETURN(ZZ_p, x); }
void div(ZZ_p& x, const ZZ_p& a, long b);
void div(ZZ_p& x, long a, const ZZ_p& b);
// operator notation:
inline ZZ_p operator+(const ZZ_p& a, const ZZ_p& b)
{ ZZ_p x; add(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator+(const ZZ_p& a, long b)
{ ZZ_p x; add(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator+(long a, const ZZ_p& b)
{ ZZ_p x; add(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p& operator+=(ZZ_p& x, const ZZ_p& b)
{ add(x, x, b); return x; }
inline ZZ_p& operator+=(ZZ_p& x, long b)
{ add(x, x, b); return x; }
inline ZZ_p operator-(const ZZ_p& a, const ZZ_p& b)
{ ZZ_p x; sub(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator-(const ZZ_p& a, long b)
{ ZZ_p x; sub(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator-(long a, const ZZ_p& b)
{ ZZ_p x; sub(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p& operator-=(ZZ_p& x, const ZZ_p& b)
{ sub(x, x, b); return x; }
inline ZZ_p& operator-=(ZZ_p& x, long b)
{ sub(x, x, b); return x; }
inline ZZ_p operator*(const ZZ_p& a, const ZZ_p& b)
{ ZZ_p x; mul(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator*(const ZZ_p& a, long b)
{ ZZ_p x; mul(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator*(long a, const ZZ_p& b)
{ ZZ_p x; mul(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p& operator*=(ZZ_p& x, const ZZ_p& b)
{ mul(x, x, b); return x; }
inline ZZ_p& operator*=(ZZ_p& x, long b)
{ mul(x, x, b); return x; }
inline ZZ_p operator/(const ZZ_p& a, const ZZ_p& b)
{ ZZ_p x; div(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator/(const ZZ_p& a, long b)
{ ZZ_p x; div(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p operator/(long a, const ZZ_p& b)
{ ZZ_p x; div(x, a, b); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p& operator/=(ZZ_p& x, const ZZ_p& b)
{ div(x, x, b); return x; }
inline ZZ_p& operator/=(ZZ_p& x, long b)
{ div(x, x, b); return x; }
inline ZZ_p operator-(const ZZ_p& a)
{ ZZ_p x; negate(x, a); NTL_OPT_RETURN(ZZ_p, x); }
inline ZZ_p& operator++(ZZ_p& x) { add(x, x, 1); return x; }
inline void operator++(ZZ_p& x, int) { add(x, x, 1); }
inline ZZ_p& operator--(ZZ_p& x) { sub(x, x, 1); return x; }
inline void operator--(ZZ_p& x, int) { sub(x, x, 1); }
// ****** exponentiation
inline void power(ZZ_p& x, const ZZ_p& a, const ZZ& e)
{ PowerMod(x._ZZ_p__rep, a._ZZ_p__rep, e, ZZ_p::modulus()); }
inline ZZ_p power(const ZZ_p& a, const ZZ& e)
{ ZZ_p x; power(x, a, e); NTL_OPT_RETURN(ZZ_p, x); }
inline void power(ZZ_p& x, const ZZ_p& a, long e)
{ PowerMod(x._ZZ_p__rep, a._ZZ_p__rep, e, ZZ_p::modulus()); }
inline ZZ_p power(const ZZ_p& a, long e)
{ ZZ_p x; power(x, a, e); NTL_OPT_RETURN(ZZ_p, x); }
// ****** comparison
inline long IsZero(const ZZ_p& a)
{ return IsZero(a._ZZ_p__rep); }
inline long IsOne(const ZZ_p& a)
{ return IsOne(a._ZZ_p__rep); }
inline long operator==(const ZZ_p& a, const ZZ_p& b)
{ return a._ZZ_p__rep == b._ZZ_p__rep; }
inline long operator!=(const ZZ_p& a, const ZZ_p& b)
{ return !(a == b); }
long operator==(const ZZ_p& a, long b);
inline long operator==(long a, const ZZ_p& b) { return b == a; }
inline long operator!=(const ZZ_p& a, long b) { return !(a == b); }
inline long operator!=(long a, const ZZ_p& b) { return !(a == b); }
// ****** random numbers
inline void random(ZZ_p& x)
// x = random element in ZZ_p
{ RandomBnd(x._ZZ_p__rep, ZZ_p::modulus()); }
inline ZZ_p random_ZZ_p()
{ ZZ_p x; random(x); NTL_OPT_RETURN(ZZ_p, x); }
// ****** input/output
inline NTL_SNS ostream& operator<<(NTL_SNS ostream& s, const ZZ_p& a)
{ return s << a._ZZ_p__rep; }
NTL_SNS istream& operator>>(NTL_SNS istream& s, ZZ_p& x);
inline ZZ_p& ZZ_p::operator=(long a) { conv(*this, a); return *this; }
/* additional legacy conversions for v6 conversion regime */
inline void conv(int& x, const ZZ_p& a) { conv(x, rep(a)); }
inline void conv(unsigned int& x, const ZZ_p& a) { conv(x, rep(a)); }
inline void conv(long& x, const ZZ_p& a) { conv(x, rep(a)); }
inline void conv(unsigned long& x, const ZZ_p& a) { conv(x, rep(a)); }
inline void conv(ZZ& x, const ZZ_p& a) { conv(x, rep(a)); }
inline void conv(ZZ_p& x, const ZZ_p& a) { x = a; }
/* ------------------------------------- */
// overload these functions for Vec<ZZ_p>.
// They are defined in vec_ZZ_p.c
void BlockConstruct(ZZ_p* p, long n);
void BlockConstructFromVec(ZZ_p* p, long n, const ZZ_p* q);
void BlockConstructFromObj(ZZ_p* p, long n, const ZZ_p& q);
void BlockDestroy(ZZ_p* p, long n);
// ZZ_p scratch variables
class ZZ_pWatcher {
public:
ZZ_p& watched;
explicit
ZZ_pWatcher(ZZ_p& _watched) : watched(_watched) { }
~ZZ_pWatcher() { watched.KillBig(); }
};
#define NTL_ZZ_pRegister(x) NTL_TLS_LOCAL(ZZ_p, x); ZZ_pWatcher _WATCHER__ ## x(x); x.allocate()
// FIXME: register variables that are allocated with respect to one modulus
// and then reused with another modulus may have initial values that are
// not in the correct range. This should not cause any problems, though,
// as these register values should always be written to before being read.
// Note also that the underlying integer reps may have space
// allocated that is smaller or *bigger* than the current modulus.
// This may impact future interface design changes --- especially
// one that tries to make "out of context" copy constructors
// safe by reading the allocated space of the source.
NTL_CLOSE_NNS
#endif
|