Security Scol plugin
gf2n.h
Go to the documentation of this file.
1// gf2n.h - originally 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
17#if CRYPTOPP_MSC_VERSION
18# pragma warning(push)
19# pragma warning(disable: 4231 4275)
20#endif
21
22NAMESPACE_BEGIN(CryptoPP)
23
24
26class CRYPTOPP_DLL PolynomialMod2
27{
28public:
30
31
32 class DivideByZero : public Exception
33 {
34 public:
35 DivideByZero() : Exception(OTHER_ERROR, "PolynomialMod2: division by zero") {}
36 };
37
38 typedef unsigned int RandomizationParameter;
40
42
43
47
52 PolynomialMod2(word value, size_t bitLength=WORD_BITS);
53
55 PolynomialMod2(const byte *encodedPoly, size_t byteCount)
56 {Decode(encodedPoly, byteCount);}
57
59 PolynomialMod2(BufferedTransformation &encodedPoly, size_t byteCount)
60 {Decode(encodedPoly, byteCount);}
61
65 {Randomize(rng, bitcount);}
66
69 static PolynomialMod2 CRYPTOPP_API Monomial(size_t i);
72 static PolynomialMod2 CRYPTOPP_API Trinomial(size_t t0, size_t t1, size_t t2);
75 static PolynomialMod2 CRYPTOPP_API Pentanomial(size_t t0, size_t t1, size_t t2, size_t t3, size_t t4);
78 static PolynomialMod2 CRYPTOPP_API AllOnes(size_t n);
79
82 static const PolynomialMod2 & CRYPTOPP_API Zero();
85 static const PolynomialMod2 & CRYPTOPP_API One();
87
89
90
92 unsigned int MinEncodedSize() const {return STDMAX(1U, ByteCount());}
93
97 void Encode(byte *output, size_t outputLen) const;
99 void Encode(BufferedTransformation &bt, size_t outputLen) const;
100
102 void Decode(const byte *input, size_t inputLen);
104 //* Precondition: bt.MaxRetrievable() >= inputLen
105 void Decode(BufferedTransformation &bt, size_t inputLen);
106
108 void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const;
110 void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length);
112
114
115
116 unsigned int BitCount() const;
118 unsigned int ByteCount() const;
120 unsigned int WordCount() const;
121
123 bool GetBit(size_t n) const {return GetCoefficient(n)!=0;}
125 byte GetByte(size_t n) const;
126
128 signed int Degree() const {return (signed int)(BitCount()-1U);}
130 unsigned int CoefficientCount() const {return BitCount();}
132 int GetCoefficient(size_t i) const
133 {return (i/WORD_BITS < reg.size()) ? int(reg[i/WORD_BITS] >> (i % WORD_BITS)) & 1 : 0;}
135 int operator[](unsigned int i) const {return GetCoefficient(i);}
136
138 bool IsZero() const {return !*this;}
140 bool Equals(const PolynomialMod2 &rhs) const;
142
144
145
146 PolynomialMod2& operator=(const PolynomialMod2& t);
148 PolynomialMod2& operator&=(const PolynomialMod2& t);
150 PolynomialMod2& operator^=(const PolynomialMod2& t);
152 PolynomialMod2& operator+=(const PolynomialMod2& t) {return *this ^= t;}
154 PolynomialMod2& operator-=(const PolynomialMod2& t) {return *this ^= t;}
156 PolynomialMod2& operator*=(const PolynomialMod2& t);
158 PolynomialMod2& operator/=(const PolynomialMod2& t);
160 PolynomialMod2& operator%=(const PolynomialMod2& t);
162 PolynomialMod2& operator<<=(unsigned int);
164 PolynomialMod2& operator>>=(unsigned int);
165
167 void Randomize(RandomNumberGenerator &rng, size_t bitcount);
168
170 void SetBit(size_t i, int value = 1);
172 void SetByte(size_t n, byte value);
173
175 void SetCoefficient(size_t i, int value) {SetBit(i, value);}
176
178 void swap(PolynomialMod2 &a) {reg.swap(a.reg);}
180
182
183
184 bool operator!() const;
186 PolynomialMod2 operator+() const {return *this;}
188 PolynomialMod2 operator-() const {return *this;}
190
192
193
194 PolynomialMod2 And(const PolynomialMod2 &b) const;
196 PolynomialMod2 Xor(const PolynomialMod2 &b) const;
198 PolynomialMod2 Plus(const PolynomialMod2 &b) const {return Xor(b);}
200 PolynomialMod2 Minus(const PolynomialMod2 &b) const {return Xor(b);}
202 PolynomialMod2 Times(const PolynomialMod2 &b) const;
204 PolynomialMod2 DividedBy(const PolynomialMod2 &b) const;
206 PolynomialMod2 Modulo(const PolynomialMod2 &b) const;
207
209 PolynomialMod2 operator>>(unsigned int n) const;
211 PolynomialMod2 operator<<(unsigned int n) const;
213
215
216
217 unsigned int Parity() const;
218
220 bool IsIrreducible() const;
221
223 PolynomialMod2 Doubled() const {return Zero();}
225 PolynomialMod2 Squared() const;
226
228 bool IsUnit() const {return Equals(One());}
230 PolynomialMod2 MultiplicativeInverse() const {return IsUnit() ? One() : Zero();}
231
233 static PolynomialMod2 CRYPTOPP_API Gcd(const PolynomialMod2 &a, const PolynomialMod2 &n);
235 PolynomialMod2 InverseMod(const PolynomialMod2 &) const;
236
238 static void CRYPTOPP_API Divide(PolynomialMod2 &r, PolynomialMod2 &q, const PolynomialMod2 &a, const PolynomialMod2 &d);
240
242
243
244 friend std::ostream& operator<<(std::ostream& out, const PolynomialMod2 &a);
246
247private:
248 friend class GF2NT;
249 friend class GF2NT233;
250
251 SecWordBlock reg;
252};
253
255inline bool operator==(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
256{return a.Equals(b);}
258inline bool operator!=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
259{return !(a==b);}
261inline bool operator> (const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
262{return a.Degree() > b.Degree();}
264inline bool operator>=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
265{return a.Degree() >= b.Degree();}
267inline bool operator< (const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
268{return a.Degree() < b.Degree();}
270inline bool operator<=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
271{return a.Degree() <= b.Degree();}
273inline CryptoPP::PolynomialMod2 operator&(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.And(b);}
275inline CryptoPP::PolynomialMod2 operator^(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Xor(b);}
277inline CryptoPP::PolynomialMod2 operator+(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Plus(b);}
279inline CryptoPP::PolynomialMod2 operator-(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Minus(b);}
281inline CryptoPP::PolynomialMod2 operator*(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Times(b);}
283inline CryptoPP::PolynomialMod2 operator/(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.DividedBy(b);}
285inline CryptoPP::PolynomialMod2 operator%(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b) {return a.Modulo(b);}
286
287// CodeWarrior 8 workaround: put these template instantiations after overloaded operator declarations,
288// but before the use of QuotientRing<EuclideanDomainOf<PolynomialMod2> > for VC .NET 2003
289CRYPTOPP_DLL_TEMPLATE_CLASS AbstractGroup<PolynomialMod2>;
290CRYPTOPP_DLL_TEMPLATE_CLASS AbstractRing<PolynomialMod2>;
291CRYPTOPP_DLL_TEMPLATE_CLASS AbstractEuclideanDomain<PolynomialMod2>;
292CRYPTOPP_DLL_TEMPLATE_CLASS EuclideanDomainOf<PolynomialMod2>;
293CRYPTOPP_DLL_TEMPLATE_CLASS QuotientRing<EuclideanDomainOf<PolynomialMod2> >;
294
296class CRYPTOPP_DLL GF2NP : public QuotientRing<EuclideanDomainOf<PolynomialMod2> >
297{
298public:
299 GF2NP(const PolynomialMod2 &modulus);
300
301 virtual GF2NP * Clone() const {return new GF2NP(*this);}
302 virtual void DEREncode(BufferedTransformation &bt) const
303 {CRYPTOPP_UNUSED(bt); CRYPTOPP_ASSERT(false);} // no ASN.1 syntax yet for general polynomial basis
304
305 void DEREncodeElement(BufferedTransformation &out, const Element &a) const;
306 void BERDecodeElement(BufferedTransformation &in, Element &a) const;
307
308 bool Equal(const Element &a, const Element &b) const
309 {CRYPTOPP_ASSERT(a.Degree() < m_modulus.Degree() && b.Degree() < m_modulus.Degree()); return a.Equals(b);}
310
311 bool IsUnit(const Element &a) const
312 {CRYPTOPP_ASSERT(a.Degree() < m_modulus.Degree()); return !!a;}
313
314 unsigned int MaxElementBitLength() const
315 {return m;}
316
317 unsigned int MaxElementByteLength() const
318 {return (unsigned int)BitsToBytes(MaxElementBitLength());}
319
320 Element SquareRoot(const Element &a) const;
321
322 Element HalfTrace(const Element &a) const;
323
324 // returns z such that z^2 + z == a
325 Element SolveQuadraticEquation(const Element &a) const;
326
327protected:
328 unsigned int m;
329};
330
332class CRYPTOPP_DLL GF2NT : public GF2NP
333{
334public:
335 // polynomial modulus = x^t0 + x^t1 + x^t2, t0 > t1 > t2
336 GF2NT(unsigned int t0, unsigned int t1, unsigned int t2);
337
338 GF2NP * Clone() const {return new GF2NT(*this);}
339 void DEREncode(BufferedTransformation &bt) const;
340
341 const Element& Multiply(const Element &a, const Element &b) const;
342
343 const Element& Square(const Element &a) const
344 {return Reduced(a.Squared());}
345
346 const Element& MultiplicativeInverse(const Element &a) const;
347
348protected:
349 const Element& Reduced(const Element &a) const;
350
351 unsigned int t0, t1;
352 mutable PolynomialMod2 result;
353};
354
358class CRYPTOPP_DLL GF2NT233 : public GF2NT
359{
360public:
361 // polynomial modulus = x^t0 + x^t1 + x^t2, t0 > t1 > t2
362 GF2NT233(unsigned int t0, unsigned int t1, unsigned int t2);
363
364 GF2NP * Clone() const {return new GF2NT233(*this);}
365
366 const Element& Multiply(const Element &a, const Element &b) const;
367
368 const Element& Square(const Element &a) const;
369};
370
372class CRYPTOPP_DLL GF2NPP : public GF2NP
373{
374public:
375 // polynomial modulus = x^t0 + x^t1 + x^t2 + x^t3 + x^t4, t0 > t1 > t2 > t3 > t4
376 GF2NPP(unsigned int t0, unsigned int t1, unsigned int t2, unsigned int t3, unsigned int t4)
377 : GF2NP(PolynomialMod2::Pentanomial(t0, t1, t2, t3, t4)), t1(t1), t2(t2), t3(t3) {}
378
379 GF2NP * Clone() const {return new GF2NPP(*this);}
380 void DEREncode(BufferedTransformation &bt) const;
381
382private:
383 unsigned int t1, t2, t3;
384};
385
386// construct new GF2NP from the ASN.1 sequence Characteristic-two
387CRYPTOPP_DLL GF2NP * CRYPTOPP_API BERDecodeGF2NP(BufferedTransformation &bt);
388
389NAMESPACE_END
390
391#ifndef __BORLANDC__
392NAMESPACE_BEGIN(std)
393template<> inline void swap(CryptoPP::PolynomialMod2 &a, CryptoPP::PolynomialMod2 &b)
394{
395 a.swap(b);
396}
397NAMESPACE_END
398#endif
399
400#if CRYPTOPP_MSC_VERSION
401# pragma warning(pop)
402#endif
403
404#endif
Classes for performing mathematics over different fields.
Classes and functions for working with ANS.1 objects.
Abstract Euclidean domain.
Definition algebra.h:277
Abstract group.
Definition algebra.h:27
Abstract ring.
Definition algebra.h:119
Interface for buffered transformations.
Definition cryptlib.h:1652
Euclidean domain.
Definition algebra.h:316
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
GF(2^n) with Polynomial Basis.
Definition gf2n.h:297
bool Equal(const Element &a, const Element &b) const
Compare two elements for equality.
Definition gf2n.h:308
bool IsUnit(const Element &a) const
Determines whether an element is a unit in the group.
Definition gf2n.h:311
GF(2^n) with Pentanomial Basis.
Definition gf2n.h:373
GF(2^n) for b233 and k233.
Definition gf2n.h:359
GF(2^n) with Trinomial Basis.
Definition gf2n.h:333
const Element & Square(const Element &a) const
Square an element in the group.
Definition gf2n.h:343
Exception thrown when divide by zero is encountered.
Definition gf2n.h:33
Polynomial with Coefficients in GF(2)
Definition gf2n.h:27
unsigned int MinEncodedSize() const
minimum number of bytes to encode this polynomial
Definition gf2n.h:92
PolynomialMod2 MultiplicativeInverse() const
return inverse if *this is a unit, otherwise return 0
Definition gf2n.h:230
signed int Degree() const
the zero polynomial will return a degree of -1
Definition gf2n.h:128
PolynomialMod2(RandomNumberGenerator &rng, size_t bitcount)
Create a uniformly distributed random polynomial.
Definition gf2n.h:64
bool IsUnit() const
only 1 is a unit
Definition gf2n.h:228
static PolynomialMod2 CRYPTOPP_API Pentanomial(size_t t0, size_t t1, size_t t2, size_t t3, size_t t4)
Provides x^t0 + x^t1 + x^t2 + x^t3 + x^t4.
Definition gf2n.cpp:145
PolynomialMod2 Doubled() const
is always zero since we're working modulo 2
Definition gf2n.h:223
unsigned int CoefficientCount() const
degree + 1
Definition gf2n.h:130
PolynomialMod2(BufferedTransformation &encodedPoly, size_t byteCount)
Construct a PolynomialMod2 from big-endian form stored in a BufferedTransformation.
Definition gf2n.h:59
int operator[](unsigned int i) const
return coefficient for x^i
Definition gf2n.h:135
PolynomialMod2(const byte *encodedPoly, size_t byteCount)
Construct a PolynomialMod2 from big-endian byte array.
Definition gf2n.h:55
int GetCoefficient(size_t i) const
return coefficient for x^i
Definition gf2n.h:132
bool GetBit(size_t n) const
return the n-th bit, n=0 being the least significant bit
Definition gf2n.h:123
Quotient ring.
Definition algebra.h:387
Interface for random number generators.
Definition cryptlib.h:1435
Square block cipher.
Definition square.h:25
const unsigned int WORD_BITS
Size of a platform word in bits.
Definition config_int.h:249
Abstract base classes that provide a uniform interface to this library.
bool operator>=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition gf2n.h:264
bool operator>(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition gf2n.h:261
bool operator<=(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition gf2n.h:270
bool operator<(const CryptoPP::PolynomialMod2 &a, const CryptoPP::PolynomialMod2 &b)
compares degree
Definition gf2n.h:267
Utility functions for the Crypto++ library.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Definition misc.h:666
unsigned int Parity(T value)
Returns the parity of a value.
Definition misc.h:807
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:938
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
Definition misc.h:2010
Classes and functions for secure memory allocations.