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
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);
00028
00029 virtual ModularArithmetic * Clone() const {return new ModularArithmetic(*this);}
00030
00031 void DEREncode(BufferedTransformation &bt) const;
00032
00033 void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
00034 void BERDecodeElement(BufferedTransformation &in, Element &a) const;
00035
00036 const Integer& GetModulus() const {return modulus;}
00037 void SetModulus(const Integer &newModulus) {modulus = newModulus; result.reg.resize(modulus.reg.size());}
00038
00039 virtual bool IsMontgomeryRepresentation() const {return false;}
00040
00041 virtual Integer ConvertIn(const Integer &a) const
00042 {return a%modulus;}
00043
00044 virtual Integer ConvertOut(const Integer &a) const
00045 {return a;}
00046
00047 const Integer& Half(const Integer &a) const;
00048
00049 bool Equal(const Integer &a, const Integer &b) const
00050 {return a==b;}
00051
00052 const Integer& Identity() const
00053 {return Integer::Zero();}
00054
00055 const Integer& Add(const Integer &a, const Integer &b) const;
00056
00057 Integer& Accumulate(Integer &a, const Integer &b) const;
00058
00059 const Integer& Inverse(const Integer &a) const;
00060
00061 const Integer& Subtract(const Integer &a, const Integer &b) const;
00062
00063 Integer& Reduce(Integer &a, const Integer &b) const;
00064
00065 const Integer& Double(const Integer &a) const
00066 {return Add(a, a);}
00067
00068 const Integer& MultiplicativeIdentity() const
00069 {return Integer::One();}
00070
00071 const Integer& Multiply(const Integer &a, const Integer &b) const
00072 {return result1 = a*b%modulus;}
00073
00074 const Integer& Square(const Integer &a) const
00075 {return result1 = a.Squared()%modulus;}
00076
00077 bool IsUnit(const Integer &a) const
00078 {return Integer::Gcd(a, modulus).IsUnit();}
00079
00080 const Integer& MultiplicativeInverse(const Integer &a) const
00081 {return result1 = a.InverseMod(modulus);}
00082
00083 const Integer& Divide(const Integer &a, const Integer &b) const
00084 {return Multiply(a, MultiplicativeInverse(b));}
00085
00086 Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const;
00087
00088 void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const;
00089
00090 unsigned int MaxElementBitLength() const
00091 {return (modulus-1).BitCount();}
00092
00093 unsigned int MaxElementByteLength() const
00094 {return (modulus-1).ByteCount();}
00095
00096 Element RandomElement( RandomNumberGenerator &rng , const RandomizationParameter &ignore_for_now = 0 ) const
00097
00098 {
00099 return Element( rng , Integer( (long) 0) , modulus - Integer( (long) 1 ) ) ;
00100 }
00101
00102 static const RandomizationParameter DefaultRandomizationParameter ;
00103
00104 protected:
00105 Integer modulus;
00106 mutable Integer result, result1;
00107
00108 };
00109
00110
00111
00112
00113 class MontgomeryRepresentation : public ModularArithmetic
00114 {
00115 public:
00116 MontgomeryRepresentation(const Integer &modulus);
00117
00118 virtual ModularArithmetic * Clone() const {return new MontgomeryRepresentation(*this);}
00119
00120 bool IsMontgomeryRepresentation() const {return true;}
00121
00122 Integer ConvertIn(const Integer &a) const
00123 {return (a<<(WORD_BITS*modulus.reg.size()))%modulus;}
00124
00125 Integer ConvertOut(const Integer &a) const;
00126
00127 const Integer& MultiplicativeIdentity() const
00128 {return result1 = Integer::Power2(WORD_BITS*modulus.reg.size())%modulus;}
00129
00130 const Integer& Multiply(const Integer &a, const Integer &b) const;
00131
00132 const Integer& Square(const Integer &a) const;
00133
00134 const Integer& MultiplicativeInverse(const Integer &a) const;
00135
00136 Integer CascadeExponentiate(const Integer &x, const Integer &e1, const Integer &y, const Integer &e2) const
00137 {return AbstractRing<Integer>::CascadeExponentiate(x, e1, y, e2);}
00138
00139 void SimultaneousExponentiate(Element *results, const Element &base, const Integer *exponents, unsigned int exponentsCount) const
00140 {AbstractRing<Integer>::SimultaneousExponentiate(results, base, exponents, exponentsCount);}
00141
00142 private:
00143 Integer u;
00144 mutable SecAlignedWordBlock workspace;
00145 };
00146
00147 NAMESPACE_END
00148
00149 #endif