/*============================================================================== | | NAME | | ppArith.h | | DESCRIPTION | | Header file for miscellaneous integer multiple precision math routines. | | LEGAL | | Primpoly Version 9.1 - A Program for Computing Primitive | Polynomials. Copyright (C) 1999-2008 by Sean Erik O'Connor. | All Rights Reserved. | | This program is free software; you can redistribute it and/or | modify it under the terms of the GNU General Public License as | published by the Free Software Foundation; version 2 of the | License. | | This program is distributed in the hope that it will be useful, | but WITHOUT ANY WARRANTY; without even the implied warranty of | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | GNU General Public License for more details. | | You should have received a copy of the GNU General Public | License along with this program; if not, write to the Free | Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, | MA 02111-1307, USA. | | The author's address is artifex@seanerikoconnor.freeservers.com | ==============================================================================*/ #ifndef __PPARITH_H__ #define __PPARITH_H__ /*============================================================================= | | NAME | | ArithModPException | | DESCRIPTION | | Helper class for recording data thrown by ArithModP. | +============================================================================*/ class ArithModPException : public runtime_error { public: // This form will be used most often in a throw. ArithModPException( const string & description ) : runtime_error( description ) { } ; // Throw with no error message. ArithModPException() : runtime_error( "BigInt exception: " ) { } ; } ; // end class ArithModPException /*============================================================================= | | NAME | | ArithModP | ModP | PowerMod | IsPrimitiveRoot | InverseModP | | DESCRIPTION | | Abstract classes for modulo p arithmetic operations on integers. | | uint p = 7 ; | ModP modp( p ) ; // Set p = 7 for all subsequent operations. | uint sevenmodp = modp( 33 ) ; // Use as a functionoid. | | PowerMod power_mod( p ) ; | uint threeToTheTenthmodp = power_mod( 3, 10 ) ; // Use as a functionoid. | | NOTES | | Use the functionoid approach so we can (1) save state and (2) have | a function interface. | The member functions and friends are documented in detail ppArith.cpp | +============================================================================*/ class ArithModP { public: ArithModP() : p_( 2 ) {} ; ArithModP( int p ) : p_( p ) {} ; bool const_coeff_test( int a, int a0, int n ) ; bool const_coeff_is_primitive_root( int a0, int n ) ; protected: int p_ ; // Modulus for all arithmetic operations. private: // Don't let anyone define the default constructor, // copy constructor or assignment // operator for this class. ArithModP( const ArithModP & ) ; ArithModP & operator=( const ArithModP & ) ; } ; class ModP { public: ModP( int p ) { p_ = p ; } ; ModP( const ModP & mod ) : p_( mod.p_ ) { } void set( int p ) { p_ = p ; } virtual int operator()( int n ) ; protected: int p_ ; // Modulus for all arithmetic operations. } ; // Generic class. template class PowerMod { public: PowerMod( const IntType & p ) { p_ = p ; } IntType operator()( const IntType & a, const IntType & n ) ; protected: IntType p_ ; // Modulus for all arithmetic operations. } ; class InverseModP { public: InverseModP( int p ) { p_ = p ; } ; virtual int operator()( int a ) ; protected: int p_ ; // Modulus for all arithmetic operations. } ; class IsPrimitiveRoot { public: IsPrimitiveRoot( int p ) { p_ = p ; } ; bool operator()( int a ) ; protected: int p_ ; // Modulus for all arithmetic operations. } ; template IntType gcd( const IntType & u, const IntType & v ) ; #endif // __PPARITH_H__