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