Counter Strike : Global Offensive Source Code
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

369 lines
12 KiB

  1. #ifndef CRYPTOPP_GF2N_H
  2. #define CRYPTOPP_GF2N_H
  3. /*! \file */
  4. #include "cryptlib.h"
  5. #include "secblock.h"
  6. #include "misc.h"
  7. #include "algebra.h"
  8. #include <iosfwd>
  9. NAMESPACE_BEGIN(CryptoPP)
  10. //! Polynomial with Coefficients in GF(2)
  11. /*! \nosubgrouping */
  12. class CRYPTOPP_DLL PolynomialMod2
  13. {
  14. public:
  15. //! \name ENUMS, EXCEPTIONS, and TYPEDEFS
  16. //@{
  17. //! divide by zero exception
  18. class DivideByZero : public Exception
  19. {
  20. public:
  21. DivideByZero() : Exception(OTHER_ERROR, "PolynomialMod2: division by zero") {}
  22. };
  23. typedef unsigned int RandomizationParameter;
  24. //@}
  25. //! \name CREATORS
  26. //@{
  27. //! creates the zero polynomial
  28. PolynomialMod2();
  29. //! copy constructor
  30. PolynomialMod2(const PolynomialMod2& t);
  31. //! convert from word
  32. /*! value should be encoded with the least significant bit as coefficient to x^0
  33. and most significant bit as coefficient to x^(WORD_BITS-1)
  34. bitLength denotes how much memory to allocate initially
  35. */
  36. PolynomialMod2(word value, size_t bitLength=WORD_BITS);
  37. //! convert from big-endian byte array
  38. PolynomialMod2(const byte *encodedPoly, size_t byteCount)
  39. {Decode(encodedPoly, byteCount);}
  40. //! convert from big-endian form stored in a BufferedTransformation
  41. PolynomialMod2(BufferedTransformation &encodedPoly, size_t byteCount)
  42. {Decode(encodedPoly, byteCount);}
  43. //! create a random polynomial uniformly distributed over all polynomials with degree less than bitcount
  44. PolynomialMod2(RandomNumberGenerator &rng, size_t bitcount)
  45. {Randomize(rng, bitcount);}
  46. //! return x^i
  47. static PolynomialMod2 CRYPTOPP_API Monomial(size_t i);
  48. //! return x^t0 + x^t1 + x^t2
  49. static PolynomialMod2 CRYPTOPP_API Trinomial(size_t t0, size_t t1, size_t t2);
  50. //! return x^t0 + x^t1 + x^t2 + x^t3 + x^t4
  51. static PolynomialMod2 CRYPTOPP_API Pentanomial(size_t t0, size_t t1, size_t t2, size_t t3, size_t t4);
  52. //! return x^(n-1) + ... + x + 1
  53. static PolynomialMod2 CRYPTOPP_API AllOnes(size_t n);
  54. //!
  55. static const PolynomialMod2 & CRYPTOPP_API Zero();
  56. //!
  57. static const PolynomialMod2 & CRYPTOPP_API One();
  58. //@}
  59. //! \name ENCODE/DECODE
  60. //@{
  61. //! minimum number of bytes to encode this polynomial
  62. /*! MinEncodedSize of 0 is 1 */
  63. unsigned int MinEncodedSize() const {return STDMAX(1U, ByteCount());}
  64. //! encode in big-endian format
  65. /*! if outputLen < MinEncodedSize, the most significant bytes will be dropped
  66. if outputLen > MinEncodedSize, the most significant bytes will be padded
  67. */
  68. void Encode(byte *output, size_t outputLen) const;
  69. //!
  70. void Encode(BufferedTransformation &bt, size_t outputLen) const;
  71. //!
  72. void Decode(const byte *input, size_t inputLen);
  73. //!
  74. //* Precondition: bt.MaxRetrievable() >= inputLen
  75. void Decode(BufferedTransformation &bt, size_t inputLen);
  76. //! encode value as big-endian octet string
  77. void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const;
  78. //! decode value as big-endian octet string
  79. void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length);
  80. //@}
  81. //! \name ACCESSORS
  82. //@{
  83. //! number of significant bits = Degree() + 1
  84. unsigned int BitCount() const;
  85. //! number of significant bytes = ceiling(BitCount()/8)
  86. unsigned int ByteCount() const;
  87. //! number of significant words = ceiling(ByteCount()/sizeof(word))
  88. unsigned int WordCount() const;
  89. //! return the n-th bit, n=0 being the least significant bit
  90. bool GetBit(size_t n) const {return GetCoefficient(n)!=0;}
  91. //! return the n-th byte
  92. byte GetByte(size_t n) const;
  93. //! the zero polynomial will return a degree of -1
  94. signed int Degree() const {return BitCount()-1;}
  95. //! degree + 1
  96. unsigned int CoefficientCount() const {return BitCount();}
  97. //! return coefficient for x^i
  98. int GetCoefficient(size_t i) const
  99. {return (i/WORD_BITS < reg.size()) ? int(reg[i/WORD_BITS] >> (i % WORD_BITS)) & 1 : 0;}
  100. //! return coefficient for x^i
  101. int operator[](unsigned int i) const {return GetCoefficient(i);}
  102. //!
  103. bool IsZero() const {return !*this;}
  104. //!
  105. bool Equals(const PolynomialMod2 &rhs) const;
  106. //@}
  107. //! \name MANIPULATORS
  108. //@{
  109. //!
  110. PolynomialMod2& operator=(const PolynomialMod2& t);
  111. //!
  112. PolynomialMod2& operator&=(const PolynomialMod2& t);
  113. //!
  114. PolynomialMod2& operator^=(const PolynomialMod2& t);
  115. //!
  116. PolynomialMod2& operator+=(const PolynomialMod2& t) {return *this ^= t;}
  117. //!
  118. PolynomialMod2& operator-=(const PolynomialMod2& t) {return *this ^= t;}
  119. //!
  120. PolynomialMod2& operator*=(const PolynomialMod2& t);
  121. //!
  122. PolynomialMod2& operator/=(const PolynomialMod2& t);
  123. //!
  124. PolynomialMod2& operator%=(const PolynomialMod2& t);
  125. //!
  126. PolynomialMod2& operator<<=(unsigned int);
  127. //!
  128. PolynomialMod2& operator>>=(unsigned int);
  129. //!
  130. void Randomize(RandomNumberGenerator &rng, size_t bitcount);
  131. //!
  132. void SetBit(size_t i, int value = 1);
  133. //! set the n-th byte to value
  134. void SetByte(size_t n, byte value);
  135. //!
  136. void SetCoefficient(size_t i, int value) {SetBit(i, value);}
  137. //!
  138. void swap(PolynomialMod2 &a) {reg.swap(a.reg);}
  139. //@}
  140. //! \name UNARY OPERATORS
  141. //@{
  142. //!
  143. bool operator!() const;
  144. //!
  145. PolynomialMod2 operator+() const {return *this;}
  146. //!
  147. PolynomialMod2 operator-() const {return *this;}
  148. //@}
  149. //! \name BINARY OPERATORS
  150. //@{
  151. //!
  152. PolynomialMod2 And(const PolynomialMod2 &b) const;
  153. //!
  154. PolynomialMod2 Xor(const PolynomialMod2 &b) const;
  155. //!
  156. PolynomialMod2 Plus(const PolynomialMod2 &b) const {return Xor(b);}
  157. //!
  158. PolynomialMod2 Minus(const PolynomialMod2 &b) const {return Xor(b);}
  159. //!
  160. PolynomialMod2 Times(const PolynomialMod2 &b) const;
  161. //!
  162. PolynomialMod2 DividedBy(const PolynomialMod2 &b) const;
  163. //!
  164. PolynomialMod2 Modulo(const PolynomialMod2 &b) const;
  165. //!
  166. PolynomialMod2 operator>>(unsigned int n) const;
  167. //!
  168. PolynomialMod2 operator<<(unsigned int n) const;
  169. //@}
  170. //! \name OTHER ARITHMETIC FUNCTIONS
  171. //@{
  172. //! sum modulo 2 of all coefficients
  173. unsigned int Parity() const;
  174. //! check for irreducibility
  175. bool IsIrreducible() const;
  176. //! is always zero since we're working modulo 2
  177. PolynomialMod2 Doubled() const {return Zero();}
  178. //!
  179. PolynomialMod2 Squared() const;
  180. //! only 1 is a unit
  181. bool IsUnit() const {return Equals(One());}
  182. //! return inverse if *this is a unit, otherwise return 0
  183. PolynomialMod2 MultiplicativeInverse() const {return IsUnit() ? One() : Zero();}
  184. //! greatest common divisor
  185. static PolynomialMod2 CRYPTOPP_API Gcd(const PolynomialMod2 &a, const PolynomialMod2 &n);
  186. //! calculate multiplicative inverse of *this mod n
  187. PolynomialMod2 InverseMod(const PolynomialMod2 &) const;
  188. //! calculate r and q such that (a == d*q + r) && (deg(r) < deg(d))
  189. static void CRYPTOPP_API Divide(PolynomialMod2 &r, PolynomialMod2 &q, const PolynomialMod2 &a, const PolynomialMod2 &d);
  190. //@}
  191. //! \name INPUT/OUTPUT
  192. //@{
  193. //!
  194. friend std::ostream& operator<<(std::ostream& out, const PolynomialMod2 &a);
  195. //@}
  196. private:
  197. friend class GF2NT;
  198. SecWordBlock reg;
  199. };
  200. //!
  201. inline bool operator==(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
  202. {return a.Equals(b);}
  203. //!
  204. inline bool operator!=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
  205. {return !(a==b);}
  206. //! compares degree
  207. inline bool operator> (const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
  208. {return a.Degree() > b.Degree();}
  209. //! compares degree
  210. inline bool operator>=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
  211. {return a.Degree() >= b.Degree();}
  212. //! compares degree
  213. inline bool operator< (const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
  214. {return a.Degree() < b.Degree();}
  215. //! compares degree
  216. inline bool operator<=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
  217. {return a.Degree() <= b.Degree();}
  218. //!
  219. inline CryptoPP::PolynomialMod2 operator&(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.And(b);}
  220. //!
  221. inline CryptoPP::PolynomialMod2 operator^(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Xor(b);}
  222. //!
  223. inline CryptoPP::PolynomialMod2 operator+(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Plus(b);}
  224. //!
  225. inline CryptoPP::PolynomialMod2 operator-(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Minus(b);}
  226. //!
  227. inline CryptoPP::PolynomialMod2 operator*(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Times(b);}
  228. //!
  229. inline CryptoPP::PolynomialMod2 operator/(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.DividedBy(b);}
  230. //!
  231. inline CryptoPP::PolynomialMod2 operator%(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Modulo(b);}
  232. // CodeWarrior 8 workaround: put these template instantiations after overloaded operator declarations,
  233. // but before the use of QuotientRing<EuclideanDomainOf<PolynomialMod2> > for VC .NET 2003
  234. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<PolynomialMod2>;
  235. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractRing<PolynomialMod2>;
  236. CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<PolynomialMod2>;
  237. CRYPTOPP_DLL_TEMPLATE_CLASS EuclideanDomainOf<PolynomialMod2>;
  238. CRYPTOPP_DLL_TEMPLATE_CLASS QuotientRing<EuclideanDomainOf<PolynomialMod2> >;
  239. //! GF(2^n) with Polynomial Basis
  240. class CRYPTOPP_DLL GF2NP : public QuotientRing<EuclideanDomainOf<PolynomialMod2> >
  241. {
  242. public:
  243. GF2NP(const PolynomialMod2 &modulus);
  244. virtual GF2NP * Clone() const {return new GF2NP(*this);}
  245. virtual void DEREncode(BufferedTransformation &bt) const
  246. {assert(false);} // no ASN.1 syntax yet for general polynomial basis
  247. void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
  248. void BERDecodeElement(BufferedTransformation &in, Element &a) const;
  249. bool Equal(const Element &a, const Element &b) const
  250. {assert(a.Degree() < m_modulus.Degree() && b.Degree() < m_modulus.Degree()); return a.Equals(b);}
  251. bool IsUnit(const Element &a) const
  252. {assert(a.Degree() < m_modulus.Degree()); return !!a;}
  253. unsigned int MaxElementBitLength() const
  254. {return m;}
  255. unsigned int MaxElementByteLength() const
  256. {return (unsigned int)BitsToBytes(MaxElementBitLength());}
  257. Element SquareRoot(const Element &a) const;
  258. Element HalfTrace(const Element &a) const;
  259. // returns z such that z^2 + z == a
  260. Element SolveQuadraticEquation(const Element &a) const;
  261. protected:
  262. unsigned int m;
  263. };
  264. //! GF(2^n) with Trinomial Basis
  265. class CRYPTOPP_DLL GF2NT : public GF2NP
  266. {
  267. public:
  268. // polynomial modulus = x^t0 + x^t1 + x^t2, t0 > t1 > t2
  269. GF2NT(unsigned int t0, unsigned int t1, unsigned int t2);
  270. GF2NP * Clone() const {return new GF2NT(*this);}
  271. void DEREncode(BufferedTransformation &bt) const;
  272. const Element& Multiply(const Element &a, const Element &b) const;
  273. const Element& Square(const Element &a) const
  274. {return Reduced(a.Squared());}
  275. const Element& MultiplicativeInverse(const Element &a) const;
  276. private:
  277. const Element& Reduced(const Element &a) const;
  278. unsigned int t0, t1;
  279. mutable PolynomialMod2 result;
  280. };
  281. //! GF(2^n) with Pentanomial Basis
  282. class CRYPTOPP_DLL GF2NPP : public GF2NP
  283. {
  284. public:
  285. // polynomial modulus = x^t0 + x^t1 + x^t2 + x^t3 + x^t4, t0 > t1 > t2 > t3 > t4
  286. GF2NPP(unsigned int t0, unsigned int t1, unsigned int t2, unsigned int t3, unsigned int t4)
  287. : GF2NP(PolynomialMod2::Pentanomial(t0, t1, t2, t3, t4)), t0(t0), t1(t1), t2(t2), t3(t3) {}
  288. GF2NP * Clone() const {return new GF2NPP(*this);}
  289. void DEREncode(BufferedTransformation &bt) const;
  290. private:
  291. unsigned int t0, t1, t2, t3;
  292. };
  293. // construct new GF2NP from the ASN.1 sequence Characteristic-two
  294. CRYPTOPP_DLL GF2NP * CRYPTOPP_API BERDecodeGF2NP(BufferedTransformation &bt);
  295. NAMESPACE_END
  296. #ifndef __BORLANDC__
  297. NAMESPACE_BEGIN(std)
  298. template<> inline void swap(CryptoPP::PolynomialMod2 &a, CryptoPP::PolynomialMod2 &b)
  299. {
  300. a.swap(b);
  301. }
  302. NAMESPACE_END
  303. #endif
  304. #endif