Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound 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 
00014 class ModularArithmetic : public AbstractRing<Integer>
00015 {
00016 public:
00017 
00018         typedef int RandomizationParameter;
00019         typedef Integer Element;
00020 
00021         ModularArithmetic(const Integer &modulus = Integer::One())
00022                 : modulus(modulus), result((word)0, modulus.reg.size) {}
00023 
00024         ModularArithmetic(const ModularArithmetic &ma)
00025                 : modulus(ma.modulus), result((word)0, modulus.reg.size) {}
00026 
00027         ModularArithmetic(BufferedTransformation &bt);  // construct from BER encoded parameters
00028 
00029         void DEREncode(BufferedTransformation &bt) const;
00030 
00031         void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
00032         void BERDecodeElement(BufferedTransformation &in, Element &a) const;
00033 
00034         const Integer& GetModulus() const {return modulus;}
00035         void SetModulus(const Integer &newModulus) {modulus = newModulus; result.reg.Resize(modulus.reg.size);}
00036 
00037         virtual Integer ConvertIn(const Integer &a) const
00038                 {return a%modulus;}
00039 
00040         virtual Integer ConvertOut(const Integer &a) const
00041                 {return a;}
00042 
00043         const Integer& Half(const Integer &a) const;
00044 
00045         bool Equal(const Integer &a, const Integer &b) const
00046                 {return a==b;}
00047 
00048         const Integer& Zero() const
00049                 {return Integer::Zero();}
00050 
00051         const Integer& Add(const Integer &a, const Integer &b) const;
00052 
00053         Integer& Accumulate(Integer &a, const Integer &b) const;
00054 
00055         const Integer& Inverse(const Integer &a) const;
00056 
00057         const Integer& Subtract(const Integer &a, const Integer &b) const;
00058 
00059         Integer& Reduce(Integer &a, const Integer &b) const;
00060 
00061         const Integer& Double(const Integer &a) const
00062                 {return Add(a, a);}
00063 
00064         const Integer& One() const
00065                 {return Integer::One();}
00066 
00067         const Integer& Multiply(const Integer &a, const Integer &b) const
00068                 {return result1 = a*b%modulus;}
00069 
00070         const Integer& Square(const Integer &a) const
00071                 {return result1 = a.Squared()%modulus;}
00072 
00073         bool IsUnit(const Integer &a) const
00074                 {return Integer::Gcd(a, modulus).IsUnit();}
00075 
00076         const Integer& MultiplicativeInverse(const Integer &a) const
00077                 {return result1 = a.InverseMod(modulus);}
00078 
00079         const Integer& Divide(const Integer &a, const Integer &b) const
00080                 {return Multiply(a, MultiplicativeInverse(b));}
00081 
00082         Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
00083 
00084         void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
00085 
00086         unsigned int MaxElementBitLength() const
00087                 {return (modulus-1).BitCount();}
00088 
00089         unsigned int MaxElementByteLength() const
00090                 {return (modulus-1).ByteCount();}
00091 
00092         Element RandomElement( RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0 ) const
00093                 // left RandomizationParameter arg as ref in case RandomizationParameter becomes a more complicated struct
00094         { 
00095                 return Element( rng , Integer( (long) 0) , modulus - Integer( (long) 1 )   ) ; 
00096         }   
00097 
00098         static const RandomizationParameter DefaultRandomizationParameter ;
00099 
00100 protected:
00101         Integer modulus;
00102         mutable Integer result, result1;
00103 
00104 };
00105 
00106 // const ModularArithmetic::RandomizationParameter ModularArithmetic::DefaultRandomizationParameter = 0 ;
00107 
00109 class MontgomeryRepresentation : public ModularArithmetic
00110 {
00111 public:
00112         MontgomeryRepresentation(const Integer &modulus);       // modulus must be odd
00113 
00114         Integer ConvertIn(const Integer &a) const
00115                 {return (a<<(WORD_BITS*modulus.reg.size))%modulus;}
00116 
00117         Integer ConvertOut(const Integer &a) const;
00118 
00119         const Integer& One() const
00120                 {return result1 = Integer::Power2(WORD_BITS*modulus.reg.size)%modulus;}
00121 
00122         const Integer& Multiply(const Integer &a, const Integer &b) const;
00123 
00124         const Integer& Square(const Integer &a) const;
00125 
00126         const Integer& MultiplicativeInverse(const Integer &a) const;
00127 
00128         Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
00129                 {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
00130 
00131         void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
00132                 {AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
00133 
00134 private:
00135         Integer u;
00136         SecWordBlock workspace;
00137 };
00138 
00139 NAMESPACE_END
00140 
00141 #endif

Generated at Mon Jan 15 01:16:34 2001 for Crypto++ by doxygen1.2.4 written by Dimitri van Heesch, © 1997-2000