Team Fortress 2 Source Code as on 22/4/2020
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.

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