Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | File List | Namespace Members | Class Members | File Members

modarith.h

00001 #ifndef CRYPTOPP_MODARITH_H
00002 #define CRYPTOPP_MODARITH_H
00003 
00004 // implementations are in integer.cpp
00005 
00006 #include "cryptlib.h"
00007 #include "misc.h"
00008 #include "integer.h"
00009 #include "algebra.h"
00010 
00011 NAMESPACE_BEGIN(CryptoPP)
00012 
00013 #ifndef SKIP_EXPLICIT_INSTANTIATION
00014 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<Integer>;
00015 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractRing<Integer>;
00016 CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<Integer>;
00017 #endif // SKIP_EXPLICIT_INSTANTIATION
00018 
00019 //! ring of congruence classes modulo n
00020 /*! \note this implementation represents each congruence class as the smallest non-negative integer in that class */
00021 class CRYPTOPP_DLL ModularArithmetic : public AbstractRing<Integer>
00022 {
00023 public:
00024 
00025         typedef int RandomizationParameter;
00026         typedef Integer Element;
00027 
00028         ModularArithmetic(const Integer &modulus = Integer::One())
00029                 : m_modulus(modulus), m_result((word)0, modulus.reg.size()) {}
00030 
00031         ModularArithmetic(const ModularArithmetic &ma)
00032                 : m_modulus(ma.m_modulus), m_result((word)0, m_modulus.reg.size()) {}
00033 
00034         ModularArithmetic(BufferedTransformation &bt);  // construct from BER encoded parameters
00035 
00036         virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
00037 
00038         void DEREncode(BufferedTransformation &bt) const;
00039 
00040         void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
00041         void BERDecodeElement(BufferedTransformation &in, Element &a) const;
00042 
00043         const Integer& GetModulus() const {return m_modulus;}
00044         void SetModulus(const Integer &newModulus) {m_modulus = newModulus; m_result.reg.resize(m_modulus.reg.size());}
00045 
00046         virtual bool IsMontgomeryRepresentation() const {return false;}
00047 
00048         virtual Integer ConvertIn(const Integer &a) const
00049                 {return a%m_modulus;}
00050 
00051         virtual Integer ConvertOut(const Integer &a) const
00052                 {return a;}
00053 
00054         const Integer& Half(const Integer &a) const;
00055 
00056         bool Equal(const Integer &a, const Integer &b) const
00057                 {return a==b;}
00058 
00059         const Integer& Identity() const
00060                 {return Integer::Zero();}
00061 
00062         const Integer& Add(const Integer &a, const Integer &b) const;
00063 
00064         Integer& Accumulate(Integer &a, const Integer &b) const;
00065 
00066         const Integer& Inverse(const Integer &a) const;
00067 
00068         const Integer& Subtract(const Integer &a, const Integer &b) const;
00069 
00070         Integer& Reduce(Integer &a, const Integer &b) const;
00071 
00072         const Integer& Double(const Integer &a) const
00073                 {return Add(a, a);}
00074 
00075         const Integer& MultiplicativeIdentity() const
00076                 {return Integer::One();}
00077 
00078         const Integer& Multiply(const Integer &a, const Integer &b) const
00079                 {return m_result1 = a*b%m_modulus;}
00080 
00081         const Integer& Square(const Integer &a) const
00082                 {return m_result1 = a.Squared()%m_modulus;}
00083 
00084         bool IsUnit(const Integer &a) const
00085                 {return Integer::Gcd(a, m_modulus).IsUnit();}
00086 
00087         const Integer& MultiplicativeInverse(const Integer &a) const
00088                 {return m_result1 = a.InverseMod(m_modulus);}
00089 
00090         const Integer& Divide(const Integer &a, const Integer &b) const
00091                 {return Multiply(a, MultiplicativeInverse(b));}
00092 
00093         Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
00094 
00095         void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
00096 
00097         unsigned int MaxElementBitLength() const
00098                 {return (m_modulus-1).BitCount();}
00099 
00100         unsigned int MaxElementByteLength() const
00101                 {return (m_modulus-1).ByteCount();}
00102 
00103         Element RandomElement( RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0 ) const
00104                 // left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
00105         { 
00106                 return Element( rng , Integer( (long) 0) , m_modulus - Integer( (long) 1 )   ) ; 
00107         }   
00108 
00109         bool operator==(const ModularArithmetic &rhs) const
00110                 {return m_modulus == rhs.m_modulus;}
00111 
00112         static const RandomizationParameter DefaultRandomizationParameter ;
00113 
00114 protected:
00115         Integer m_modulus;
00116         mutable Integer m_result, m_result1;
00117 
00118 };
00119 
00120 // const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
00121 
00122 //! do modular arithmetics in Montgomery representation for increased speed
00123 /*! \note the Montgomery representation represents each congruence class [a] as a*r%n, where r is a convenient power of 2 */
00124 class CRYPTOPP_DLL MontgomeryRepresentation : public ModularArithmetic
00125 {
00126 public:
00127         MontgomeryRepresentation(const Integer &modulus);       // modulus must be odd
00128 
00129         virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
00130 
00131         bool IsMontgomeryRepresentation() const {return true;}
00132 
00133         Integer ConvertIn(const Integer &a) const
00134                 {return (a<<(WORD_BITS*m_modulus.reg.size()))%m_modulus;}
00135 
00136         Integer ConvertOut(const Integer &a) const;
00137 
00138         const Integer& MultiplicativeIdentity() const
00139                 {return m_result1 = Integer::Power2(WORD_BITS*m_modulus.reg.size())%m_modulus;}
00140 
00141         const Integer& Multiply(const Integer &a, const Integer &b) const;
00142 
00143         const Integer& Square(const Integer &a) const;
00144 
00145         const Integer& MultiplicativeInverse(const Integer &a) const;
00146 
00147         Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
00148                 {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
00149 
00150         void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
00151                 {AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
00152 
00153 private:
00154         Integer m_u;
00155         mutable SecAlignedWordBlock m_workspace;
00156 };
00157 
00158 NAMESPACE_END
00159 
00160 #endif

Generated on Tue Aug 16 08:38:42 2005 for Crypto++ by  doxygen 1.3.9.1