Fabcoin Core  0.16.2
P2P Digital Currency
gf2n.h
Go to the documentation of this file.
1 // gf2n.h - written and placed in the public domain by Wei Dai
2 
5 
6 #ifndef CRYPTOPP_GF2N_H
7 #define CRYPTOPP_GF2N_H
8 
9 #include "cryptlib.h"
10 #include "secblock.h"
11 #include "algebra.h"
12 #include "misc.h"
13 #include "asn.h"
14 
15 #include <iosfwd>
16 
18 
19 
22 {
23 public:
25 
26  class DivideByZero : public Exception
28  {
29  public:
30  DivideByZero() : Exception(OTHER_ERROR, "PolynomialMod2: division by zero") {}
31  };
32 
33  typedef unsigned int RandomizationParameter;
35 
37 
38  PolynomialMod2();
41  PolynomialMod2(const PolynomialMod2& t);
42 
47  PolynomialMod2(word value, size_t bitLength=WORD_BITS);
48 
50  PolynomialMod2(const byte *encodedPoly, size_t byteCount)
51  {Decode(encodedPoly, byteCount);}
52 
54  PolynomialMod2(BufferedTransformation &encodedPoly, size_t byteCount)
55  {Decode(encodedPoly, byteCount);}
56 
59  PolynomialMod2(RandomNumberGenerator &rng, size_t bitcount)
60  {Randomize(rng, bitcount);}
61 
64  static PolynomialMod2 CRYPTOPP_API Monomial(size_t i);
67  static PolynomialMod2 CRYPTOPP_API Trinomial(size_t t0, size_t t1, size_t t2);
70  static PolynomialMod2 CRYPTOPP_API Pentanomial(size_t t0, size_t t1, size_t t2, size_t t3, size_t t4);
73  static PolynomialMod2 CRYPTOPP_API AllOnes(size_t n);
74 
77  static const PolynomialMod2 & CRYPTOPP_API Zero();
80  static const PolynomialMod2 & CRYPTOPP_API One();
82 
84 
85 
87  unsigned int MinEncodedSize() const {return STDMAX(1U, ByteCount());}
88 
92  void Encode(byte *output, size_t outputLen) const;
94  void Encode(BufferedTransformation &bt, size_t outputLen) const;
95 
97  void Decode(const byte *input, size_t inputLen);
99  //* Precondition: bt.MaxRetrievable() >= inputLen
100  void Decode(BufferedTransformation &bt, size_t inputLen);
101 
103  void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const;
105  void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length);
107 
109 
110  unsigned int BitCount() const;
113  unsigned int ByteCount() const;
115  unsigned int WordCount() const;
116 
118  bool GetBit(size_t n) const {return GetCoefficient(n)!=0;}
120  byte GetByte(size_t n) const;
121 
123  signed int Degree() const {return (signed int)(BitCount()-1U);}
125  unsigned int CoefficientCount() const {return BitCount();}
127  int GetCoefficient(size_t i) const
128  {return (i/WORD_BITS < reg.size()) ? int(reg[i/WORD_BITS] >> (i % WORD_BITS)) & 1 : 0;}
130  int operator[](unsigned int i) const {return GetCoefficient(i);}
131 
133  bool IsZero() const {return !*this;}
135  bool Equals(const PolynomialMod2 &rhs) const;
137 
139 
140  PolynomialMod2& operator=(const PolynomialMod2& t);
143  PolynomialMod2& operator&=(const PolynomialMod2& t);
145  PolynomialMod2& operator^=(const PolynomialMod2& t);
147  PolynomialMod2& operator+=(const PolynomialMod2& t) {return *this ^= t;}
149  PolynomialMod2& operator-=(const PolynomialMod2& t) {return *this ^= t;}
151  PolynomialMod2& operator*=(const PolynomialMod2& t);
153  PolynomialMod2& operator/=(const PolynomialMod2& t);
155  PolynomialMod2& operator%=(const PolynomialMod2& t);
157  PolynomialMod2& operator<<=(unsigned int);
159  PolynomialMod2& operator>>=(unsigned int);
160 
162  void Randomize(RandomNumberGenerator &rng, size_t bitcount);
163 
165  void SetBit(size_t i, int value = 1);
167  void SetByte(size_t n, byte value);
168 
170  void SetCoefficient(size_t i, int value) {SetBit(i, value);}
171 
173  void swap(PolynomialMod2 &a) {reg.swap(a.reg);}
175 
177 
178  bool operator!() const;
181  PolynomialMod2 operator+() const {return *this;}
183  PolynomialMod2 operator-() const {return *this;}
185 
187 
188  PolynomialMod2 And(const PolynomialMod2 &b) const;
191  PolynomialMod2 Xor(const PolynomialMod2 &b) const;
193  PolynomialMod2 Plus(const PolynomialMod2 &b) const {return Xor(b);}
195  PolynomialMod2 Minus(const PolynomialMod2 &b) const {return Xor(b);}
197  PolynomialMod2 Times(const PolynomialMod2 &b) const;
199  PolynomialMod2 DividedBy(const PolynomialMod2 &b) const;
201  PolynomialMod2 Modulo(const PolynomialMod2 &b) const;
202 
204  PolynomialMod2 operator>>(unsigned int n) const;
206  PolynomialMod2 operator<<(unsigned int n) const;
208 
210 
211  unsigned int Parity() const;
213 
215  bool IsIrreducible() const;
216 
218  PolynomialMod2 Doubled() const {return Zero();}
220  PolynomialMod2 Squared() const;
221 
223  bool IsUnit() const {return Equals(One());}
225  PolynomialMod2 MultiplicativeInverse() const {return IsUnit() ? One() : Zero();}
226 
228  static PolynomialMod2 CRYPTOPP_API Gcd(const PolynomialMod2 &a, const PolynomialMod2 &n);
230  PolynomialMod2 InverseMod(const PolynomialMod2 &) const;
231 
233  static void CRYPTOPP_API Divide(PolynomialMod2 &r, PolynomialMod2 &q, const PolynomialMod2 &a, const PolynomialMod2 &d);
235 
237 
238  friend std::ostream& operator<<(std::ostream& out, const PolynomialMod2 &a);
241 
242 private:
243  friend class GF2NT;
244 
246 };
247 
249 inline bool operator==(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
250 {return a.Equals(b);}
252 inline bool operator!=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
253 {return !(a==b);}
255 inline bool operator> (const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
256 {return a.Degree() > b.Degree();}
258 inline bool operator>=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
259 {return a.Degree() >= b.Degree();}
261 inline bool operator< (const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
262 {return a.Degree() < b.Degree();}
264 inline bool operator<=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
265 {return a.Degree() <= b.Degree();}
267 inline CryptoPP::PolynomialMod2 operator&(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.And(b);}
269 inline CryptoPP::PolynomialMod2 operator^(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Xor(b);}
271 inline CryptoPP::PolynomialMod2 operator+(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Plus(b);}
273 inline CryptoPP::PolynomialMod2 operator-(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Minus(b);}
275 inline CryptoPP::PolynomialMod2 operator*(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Times(b);}
277 inline CryptoPP::PolynomialMod2 operator/(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.DividedBy(b);}
279 inline CryptoPP::PolynomialMod2 operator%(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Modulo(b);}
280 
281 // CodeWarrior 8 workaround: put these template instantiations after overloaded operator declarations,
282 // but before the use of QuotientRing<EuclideanDomainOf<PolynomialMod2> > for VC .NET 2003
288 
290 class CRYPTOPP_DLL GF2NP : public QuotientRing<EuclideanDomainOf<PolynomialMod2> >
291 {
292 public:
293  GF2NP(const PolynomialMod2 &modulus);
294 
295  virtual GF2NP * Clone() const {return new GF2NP(*this);}
296  virtual void DEREncode(BufferedTransformation &bt) const
297  {CRYPTOPP_UNUSED(bt); CRYPTOPP_ASSERT(false);} // no ASN.1 syntax yet for general polynomial basis
298 
299  void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
300  void BERDecodeElement(BufferedTransformation &in, Element &a) const;
301 
302  bool Equal(const Element &a, const Element &b) const
303  {CRYPTOPP_ASSERT(a.Degree() < m_modulus.Degree() && b.Degree() < m_modulus.Degree()); return a.Equals(b);}
304 
305  bool IsUnit(const Element &a) const
306  {CRYPTOPP_ASSERT(a.Degree() < m_modulus.Degree()); return !!a;}
307 
308  unsigned int MaxElementBitLength() const
309  {return m;}
310 
311  unsigned int MaxElementByteLength() const
312  {return (unsigned int)BitsToBytes(MaxElementBitLength());}
313 
314  Element SquareRoot(const Element &a) const;
315 
316  Element HalfTrace(const Element &a) const;
317 
318  // returns z such that z^2 + z == a
319  Element SolveQuadraticEquation(const Element &a) const;
320 
321 protected:
322  unsigned int m;
323 };
324 
326 class CRYPTOPP_DLL GF2NT : public GF2NP
327 {
328 public:
329  // polynomial modulus = x^t0 + x^t1 + x^t2, t0 > t1 > t2
330  GF2NT(unsigned int t0, unsigned int t1, unsigned int t2);
331 
332  GF2NP * Clone() const {return new GF2NT(*this);}
333  void DEREncode(BufferedTransformation &bt) const;
334 
335  const Element& Multiply(const Element &a, const Element &b) const;
336 
337  const Element& Square(const Element &a) const
338  {return Reduced(a.Squared());}
339 
340  const Element& MultiplicativeInverse(const Element &a) const;
341 
342 private:
343  const Element& Reduced(const Element &a) const;
344 
345  unsigned int t0, t1;
347 };
348 
350 class CRYPTOPP_DLL GF2NPP : public GF2NP
351 {
352 public:
353  // polynomial modulus = x^t0 + x^t1 + x^t2 + x^t3 + x^t4, t0 > t1 > t2 > t3 > t4
354  GF2NPP(unsigned int t0, unsigned int t1, unsigned int t2, unsigned int t3, unsigned int t4)
355  : GF2NP(PolynomialMod2::Pentanomial(t0, t1, t2, t3, t4)), t0(t0), t1(t1), t2(t2), t3(t3) {}
356 
357  GF2NP * Clone() const {return new GF2NPP(*this);}
358  void DEREncode(BufferedTransformation &bt) const;
359 
360 private:
361  unsigned int t0, t1, t2, t3;
362 };
363 
364 // construct new GF2NP from the ASN.1 sequence Characteristic-two
366 
368 
369 #ifndef __BORLANDC__
371 template<> inline void swap(CryptoPP::PolynomialMod2 &a, CryptoPP::PolynomialMod2 &b)
372 {
373  a.swap(b);
374 }
376 #endif
377 
378 #endif
Base class for all exceptions thrown by the library.
Definition: cryptlib.h:140
uint8_t byte
Definition: Common.h:57
void swap(PolynomialMod2 &a)
Definition: gf2n.h:173
Utility functions for the Crypto++ library.
PolynomialMod2 Doubled() const
is always zero since we&#39;re working modulo 2
Definition: gf2n.h:218
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:749
unsigned int MaxElementByteLength() const
Definition: gf2n.h:311
GF(2^n) with Trinomial Basis.
Definition: gf2n.h:326
CryptoPP::PolynomialMod2 operator+(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:271
PolynomialMod2 Plus(const PolynomialMod2 &b) const
Definition: gf2n.h:193
int GetCoefficient(size_t i) const
return coefficient for x^i
Definition: gf2n.h:127
#define NAMESPACE_BEGIN(x)
Definition: config.h:200
#define CRYPTOPP_DLL_TEMPLATE_CLASS
Definition: config.h:720
const Element & Square(const Element &a) const
Square an element in the group.
Definition: gf2n.h:337
bool Equal(const Element &a, const Element &b) const
Compare two elements for equality.
Definition: gf2n.h:302
GF2NP * Clone() const
Definition: gf2n.h:332
Abstract base classes that provide a uniform interface to this library.
CryptoPP::PolynomialMod2 operator^(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:269
signed int Degree() const
the zero polynomial will return a degree of -1
Definition: gf2n.h:123
PolynomialMod2 operator+() const
Definition: gf2n.h:181
std::hash for asio::adress
Definition: Common.h:323
Interface for random number generators.
Definition: cryptlib.h:1188
Classes for performing mathematics over different fields.
Interface for buffered transformations.
Definition: cryptlib.h:1352
Quotient ring.
Definition: algebra.h:386
GF2NPP(unsigned int t0, unsigned int t1, unsigned int t2, unsigned int t3, unsigned int t4)
Definition: gf2n.h:354
Polynomial with Coefficients in GF(2)
Definition: gf2n.h:21
PolynomialMod2 MultiplicativeInverse() const
return inverse if *this is a unit, otherwise return 0
Definition: gf2n.h:225
Excpetion thrown when divide by zero is encountered.
Definition: gf2n.h:27
PolynomialMod2(BufferedTransformation &encodedPoly, size_t byteCount)
Construct a PolynomialMod2 from big-endian form stored in a BufferedTransformation.
Definition: gf2n.h:54
GF2NP * Clone() const
Definition: gf2n.h:357
Classes and functions for secure memory allocations.
PolynomialMod2 & operator+=(const PolynomialMod2 &t)
Definition: gf2n.h:147
#define a(i)
#define t4
int operator[](unsigned int i) const
return coefficient for x^i
Definition: gf2n.h:130
CryptoPP::PolynomialMod2 operator*(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:275
bool operator<=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:264
const unsigned int WORD_BITS
Definition: config.h:317
unsigned int m
Definition: gf2n.h:322
PolynomialMod2 result
Definition: gf2n.h:346
bool IsUnit() const
only 1 is a unit
Definition: gf2n.h:223
bool operator!=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:252
CRYPTOPP_DLL GF2NP *CRYPTOPP_API BERDecodeGF2NP(BufferedTransformation &bt)
Definition: gf2n.cpp:875
bool operator==(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:249
virtual void DEREncode(BufferedTransformation &bt) const
Definition: gf2n.h:296
CryptoPP::PolynomialMod2 operator/(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:277
#define t3
PolynomialMod2 Minus(const PolynomialMod2 &b) const
Definition: gf2n.h:195
EuclideanDomainOf< PolynomialMod2 >::Element Element
Definition: algebra.h:29
CryptoPP::PolynomialMod2 operator%(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:279
unsigned int RandomizationParameter
Definition: gf2n.h:33
void Divide(word *R, word *Q, word *T, const word *A, size_t NA, const word *B, size_t NB)
Definition: integer.cpp:2692
#define CRYPTOPP_API
Definition: config.h:705
void SetCoefficient(size_t i, int value)
Definition: gf2n.h:170
#define t1
unsigned int MinEncodedSize() const
minimum number of bytes to encode this polynomial
Definition: gf2n.h:87
PolynomialMod2(const byte *encodedPoly, size_t byteCount)
Construct a PolynomialMod2 from big-endian byte array.
Definition: gf2n.h:50
unsigned int t1
Definition: gf2n.h:345
unsigned int Parity(T value)
Returns the parity of a value.
Definition: misc.h:621
bool GetBit(size_t n) const
return the n-th bit, n=0 being the least significant bit
Definition: gf2n.h:118
#define b(i, j)
#define CRYPTOPP_ASSERT(exp)
Definition: trap.h:92
virtual GF2NP * Clone() const
Definition: gf2n.h:295
std::ostream & operator<<(std::ostream &_out, bytes const &_e)
Definition: CommonIO.h:80
#define t2
Classes and functions for working with ANS.1 objects.
CryptoPP::PolynomialMod2 operator-(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:273
bool IsZero() const
Definition: gf2n.h:133
GF(2^n) with Pentanomial Basis.
Definition: gf2n.h:350
NAMESPACE_END void swap(CryptoPP::PolynomialMod2 &a, CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:371
bool IsUnit(const Element &a) const
Determines whether an element is a unit in the group.
Definition: gf2n.h:305
bool operator>=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:258
GF(2^n) with Polynomial Basis.
Definition: gf2n.h:290
#define CRYPTOPP_UNUSED(x)
Definition: config.h:741
void Multiply(word *R, word *T, const word *A, const word *B, size_t N)
Definition: integer.cpp:2324
bool operator>(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:255
std::istream & operator>>(std::istream &in, Integer &a)
Definition: integer.cpp:3604
CryptoPP::PolynomialMod2 operator&(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
Definition: gf2n.h:267
SecWordBlock reg
Definition: gf2n.h:245
PolynomialMod2(RandomNumberGenerator &rng, size_t bitcount)
Create a uniformly distributed random polynomial.
Definition: gf2n.h:59
unsigned int t3
Definition: gf2n.h:361
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition: misc.h:487
#define NAMESPACE_END
Definition: config.h:201
PolynomialMod2 & operator-=(const PolynomialMod2 &t)
Definition: gf2n.h:149
unsigned int CoefficientCount() const
degree + 1
Definition: gf2n.h:125
#define d(i)
Definition: sha.cpp:732
#define CRYPTOPP_DLL
Definition: config.h:704
bool operator<(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition: gf2n.h:261
PolynomialMod2 operator-() const
Definition: gf2n.h:183
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
Definition: misc.h:1652
word32 word
Definition: config.h:308
unsigned int MaxElementBitLength() const
Definition: gf2n.h:308