// ============================================================ // // ZnI.h // // Copyright 2002, Dennis Meilicke and Rene Peralta // // ============================================================ // // Definition: // // Template class for modular arithmetic. // // ============================================================ #ifndef __nttlHeader_ZnI__ #define __nttlHeader_ZnI__ #include #include template< class T, int seq > inline Zn::Zn( ) : _x(0) { } template< class T, int seq > inline Zn::Zn( const Zn& x ) : _x(x._x) { } template< class T, int seq > inline Zn::Zn( const T& x ) : _x(x) { Normalize( ); } template< class T, int seq > inline Zn& Zn::operator=( const Zn& x ) { _x = x._x; return *this; } template< class T, int seq > inline Zn& Zn::operator=( const T& x ) { _x = x; Normalize( ); return *this; } template< class T, int seq > inline Zn Zn::operator+( const Zn& x ) const { Zn r; r._x = _x + x._x; r.Normalize( true ); return r; } template< class T, int seq > inline Zn& Zn::operator+=( const Zn& x ) { this->_x += x._x; Normalize( true ); return (*this); } template< class T, int seq > inline Zn& Zn::operator++( ) { this->_x++; Normalize( true ); return (*this); } template< class T, int seq > inline Zn Zn::operator++( int ) { Zn temp(this->_x++); Normalize( true ); return (temp); } template< class T, int seq > inline Zn Zn::operator-( const Zn& x ) const { Zn r(*this); r._x -= x._x; Normalize( true ); return r; } template< class T, int seq > inline Zn& Zn::operator-=( const Zn& x ) { this->_x -= x._x; Normalize( true ); return (*this); } template< class T, int seq > inline Zn& Zn::operator--( ) { this->_x--; Normalize( true ); return (*this); } template< class T, int seq > inline Zn Zn::operator--( int ) { Zn temp(this->_x--); Normalize( true ); return (temp); } template< class T, int seq > inline Zn Zn::operator*( const Zn& x ) const { Zn r(*this); r._x *= x._x; r.Normalize( ); return r; } template< class T, int seq > inline Zn& Zn::operator*=( const Zn& x ) { this->_x *= x._x; Normalize( ); return (*this); } template< class T, int seq > inline Zn Zn::operator/( const Zn& x ) const { Zn r(0); T Inv = Inverse( x, *Modulus( ) ); if( Inv == 0 ) return r; if( Inv < 0 ) Inv += *Modulus( ); r._x = this->_x * Inv; r.Normalize( ); return r; } template< class T, int seq > inline Zn& Zn::operator/=( const Zn& x ) { T Inv = Inverse( x, *Modulus( ) ); if( Inv == 0 ) this->_x = 0; else { if( Inv < 0 ) Inv += *Modulus( ); this->_x *= Inv; this->Normalize( ); } return (*this); } template< class T, int seq > inline bool Zn::operator==( const Zn& x ) const { return (this->_x == x._x); } template< class T, int seq > inline bool Zn::operator!=( const Zn& x ) const { return (this->_x != x._x); } template< class T, int seq > inline bool Zn::operator< ( const Zn& x ) const { return (this->_x < x._x); } template< class T, int seq > inline bool Zn::operator<=( const Zn& x ) const { return (this->_x <= x._x); } template< class T, int seq > inline bool Zn::operator> ( const Zn& x ) const { return (this->_x > x._x); } template< class T, int seq > inline bool Zn::operator>=( const Zn& x ) const { return (this->_x >= x._x); } template< class T, int seq > inline Zn Zn::operator<<( size_t d ) const { Zn r(*this); r._x <<= d; r.Normalize( true ); return r; } template< class T, int seq > inline Zn& Zn::operator<<=( size_t d ) { _x <<= d; Normalize( true ); return (*this); } template< class T, int seq > inline Zn Zn::operator>>( size_t d ) const { Zn r(*this); r._x >>= d; return r; } template< class T, int seq > inline Zn& Zn::operator>>=( size_t d ) { _x >>= d; return (*this); } template< class T, int seq > inline Zn Zn::Square( void ) const { static nttlTraits t; Zn r( t.Square( this->_x ) ); r.Normalize( ); return r; } template< class T, int seq > inline void Zn::Normalize( bool addition ) { if( addition ) { if( _x >= (*Modulus()) ) _x -= (*Modulus()); } else { if( _x >= (*Modulus()) ) _x %= (*Modulus()); } if( _x < 0 ) _x += (*Modulus( )); } template< class T, int seq > inline void Zn::SetValue( const T& x ) { _x = x; Normalize( ); } template< class T, int seq > inline T Zn::GetValue( void ) const { return _x; } template< class T, int seq > inline ostream& Zn::Print( ostream &stream ) const { stream << this->_x; return stream; } template< class T, int seq > inline ostream& operator<<( ostream &stream, const Zn& X ) { return X.Print( stream ); } template< class T, int seq > inline istream& Zn::Read( istream &stream ) { stream >> this->_x; return stream; } template< class T, int seq > inline istream& operator>>( istream &stream, Zn& X ) { return X.Read( stream ); } #endif // __nttlHeader_ZnI__