28 #ifndef _CHAINCON_RINGZP_H_ 29 #define _CHAINCON_RINGZP_H_ 39 #include "chomp/system/config.h" 47 template <
class intType>
94 template <
class intType>
97 if ((n == 1) || (n == (q - 1)))
101 LongerInt result (1);
102 LongerInt nLonger (n);
109 result = (result * nLonger) % q;
114 nLonger = (nLonger * nLonger) % q;
119 return (static_cast<intType> (result));
126 template <
class intType>
129 if ((n == 1) || (n == (q - 1)))
140 result = (result * m) % q;
154 template <
class intType>
178 template <
class intType>
181 template <
class intType>
186 throw "Division by zero in the ring Z_p.";
197 intType q = a.
_n / b.
_n;
198 intType r = a.
_n % b.
_n;
215 template <
class intType>
226 explicit tZp (
const intType &n);
230 operator intType ()
const;
233 intType delta ()
const;
251 static void setp (
const intType &p);
254 static const intType &getp ();
257 static std::string ringsymbol ();
261 friend bool divide<intType>
276 template <
class intType>
281 template <
class intType>
287 template <
class intType>
300 intType r = (-n) % _p;
301 _n = r ? (_p - r) : 0;
306 else if (n - _p < _p)
318 template <
class intType>
324 template <
class intType>
330 return (_n >= 0) ? _n : -_n;
333 template <
class intType>
346 template <
class intType>
354 else if ((_n != -1) && (_n != 1))
355 throw "Trying to invert a noninvertible integer number.";
359 template <
class intType>
363 if (_p && (_n >= _p))
368 template <
class intType>
372 if (_p && (_n >= _p))
377 template <
class intType>
388 template <
class intType>
393 throw "Trying to set p < 0 for the ring Z_p.";
408 throw "Can't increase p to a prime number " 424 throw "Int type too small for p requested in Z_p.";
434 template <
class intType>
440 template <
class intType>
443 std::ostringstream out;
454 template <
class intType>
457 return static_cast<int_t
> (
static_cast<intType
> (n));
463 template <
class intType>
466 return static_cast<int_t
> (
static_cast<intType
> (n)) ^ 0xA5A5;
472 template <
class intType>
482 template <
class intType>
483 inline std::ostream &operator << (std::ostream &out, const tZp<intType> &n)
485 out << static_cast<intType> (n);
493 template <
class intType1,
class intType2>
496 return (static_cast<intType1> (n1) == static_cast<intType2> (n2));
501 template <
class intType1,
class intType2>
504 return (static_cast<intType1> (n1) == n2);
509 template <
class intType1,
class intType2>
512 return (n1 == static_cast<intType2> (n2));
516 #endif // _CHAINCON_RINGZP_H_ tZp< intType > & operator*=(const tZp< intType > &another)
Multiplies by another ring element.
void swap(tZp< intType > &another)
Swaps the internal data with another ring element.
short LongerInt
The name of a twice longer integer type.
int_t hashkey2(const tZp< intType > &n)
Generates a hashing key no.
A collection of selected features of integer numbers.
tZp< intType > & invert()
Inverts the ring element. Assumes that 'p' is a prime number.
tZp< intType > & operator+=(const tZp< intType > &another)
Adds another ring element.
static const intType & getp()
Gets the currently set number p for the ring.
bool operator==(const tZp< intType1 > &n1, const tZp< intType2 > &n2)
Operator == for checking whether two elements of possibly different rings are equal.
bool numberIsPrime(const intType &n)
Verifies if the given number is a positive prime number.
bool divide(const tZp< intType > &a, const tZp< intType > &b, tZp< intType > "ient, tZp< intType > &remainder)
int LongerInt
The name of a twice longer integer type.
tZp()
Default constructor of a ring element.
intType delta() const
The delta function.
long LongerInt
The name of a twice longer integer type.
int_t hashkey1(const tZp< intType > &n)
Generates a hashing key no.
tZp< intType > & negate()
Negates the ring element.
static std::string ringsymbol()
Returns the symbol of the ring.
intType invertModuloSmallP(const intType &n, const intType &q)
Inverts a number in the modulo 'p' arithmetic, provided that 'p' is a prime number.
static intType _p
The number that defines the ring.
An element of the ring Z_p, where p is globally set.
intType _n
The number corresponding to the ring element.
std::istream & operator>>(std::istream &in, tZp< intType > &n)
Reads an element from an input stream.
intType invertModuloLargeP(const intType &n, const intType &q)
Inverts a number in the modulo 'p' arithmetic, provided that 'p' is a prime number.
static void setp(const intType &p)
Sets the number p for the ring.