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