2 Copyright (C) 2000-2007 MySQL AB
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; version 2 of the License.
8 This program is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 GNU General Public License for more details.
13 You should have received a copy of the GNU General Public License
14 along with this program; see the file COPYING. If not, write to the
15 Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston,
20 /* based on Wei Dai's modarith.h from CryptoPP */
23 #ifndef TAO_CRYPT_MODARITH_HPP
24 #define TAO_CRYPT_MODARITH_HPP
27 #include "algebra.hpp"
33 class ModularArithmetic
: public AbstractRing
37 typedef int RandomizationParameter
;
38 typedef Integer Element
;
40 ModularArithmetic(const Integer
&modulus
= Integer::One())
41 : modulus(modulus
), result((word
)0, modulus
.reg_
.size()) {}
43 ModularArithmetic(const ModularArithmetic
&ma
)
45 modulus(ma
.modulus
), result((word
)0, modulus
.reg_
.size()) {}
47 const Integer
& GetModulus() const {return modulus
;}
48 void SetModulus(const Integer
&newModulus
)
51 result
.reg_
.resize(modulus
.reg_
.size());
54 virtual bool IsMontgomeryRepresentation() const {return false;}
56 virtual Integer
ConvertIn(const Integer
&a
) const
59 virtual Integer
ConvertOut(const Integer
&a
) const
62 const Integer
& Half(const Integer
&a
) const;
64 bool Equal(const Integer
&a
, const Integer
&b
) const
67 const Integer
& Identity() const
68 {return Integer::Zero();}
70 const Integer
& Add(const Integer
&a
, const Integer
&b
) const;
72 Integer
& Accumulate(Integer
&a
, const Integer
&b
) const;
74 const Integer
& Inverse(const Integer
&a
) const;
76 const Integer
& Subtract(const Integer
&a
, const Integer
&b
) const;
78 Integer
& Reduce(Integer
&a
, const Integer
&b
) const;
80 const Integer
& Double(const Integer
&a
) const
83 const Integer
& MultiplicativeIdentity() const
84 {return Integer::One();}
86 const Integer
& Multiply(const Integer
&a
, const Integer
&b
) const
87 {return result1
= a
*b
%modulus
;}
89 const Integer
& Square(const Integer
&a
) const
90 {return result1
= a
.Squared()%modulus
;}
92 bool IsUnit(const Integer
&a
) const
93 {return Integer::Gcd(a
, modulus
).IsUnit();}
95 const Integer
& MultiplicativeInverse(const Integer
&a
) const
96 {return result1
= a
.InverseMod(modulus
);}
98 const Integer
& Divide(const Integer
&a
, const Integer
&b
) const
99 {return Multiply(a
, MultiplicativeInverse(b
));}
101 Integer
CascadeExponentiate(const Integer
&x
, const Integer
&e1
,
102 const Integer
&y
, const Integer
&e2
) const;
104 void SimultaneousExponentiate(Element
*results
, const Element
&base
,
105 const Integer
*exponents
, unsigned int exponentsCount
) const;
107 unsigned int MaxElementBitLength() const
108 {return (modulus
-1).BitCount();}
110 unsigned int MaxElementByteLength() const
111 {return (modulus
-1).ByteCount();}
114 static const RandomizationParameter DefaultRandomizationParameter
;
118 mutable Integer result
, result1
;
124 //! do modular arithmetics in Montgomery representation for increased speed
125 class MontgomeryRepresentation
: public ModularArithmetic
128 MontgomeryRepresentation(const Integer
&modulus
); // modulus must be odd
130 bool IsMontgomeryRepresentation() const {return true;}
132 Integer
ConvertIn(const Integer
&a
) const
133 {return (a
<<(WORD_BITS
*modulus
.reg_
.size()))%modulus
;}
135 Integer
ConvertOut(const Integer
&a
) const;
137 const Integer
& MultiplicativeIdentity() const
138 {return result1
= Integer::Power2(WORD_BITS
*modulus
.reg_
.size())%modulus
;}
140 const Integer
& Multiply(const Integer
&a
, const Integer
&b
) const;
142 const Integer
& Square(const Integer
&a
) const;
144 const Integer
& MultiplicativeInverse(const Integer
&a
) const;
146 Integer
CascadeExponentiate(const Integer
&x
, const Integer
&e1
,
147 const Integer
&y
, const Integer
&e2
) const
148 {return AbstractRing::CascadeExponentiate(x
, e1
, y
, e2
);}
150 void SimultaneousExponentiate(Element
*results
, const Element
&base
,
151 const Integer
*exponents
, unsigned int exponentsCount
) const
152 {AbstractRing::SimultaneousExponentiate(results
, base
,
153 exponents
, exponentsCount
);}
157 mutable AlignedWordBlock workspace
;
165 #endif // TAO_CRYPT_MODARITH_HPP