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