6 #ifndef CRYPTOPP_GF2N_H
7 #define CRYPTOPP_GF2N_H
17 #if CRYPTOPP_MSC_VERSION
18 # pragma warning(push)
19 # pragma warning(disable: 4231 4275)
38 typedef unsigned int RandomizationParameter;
56 {Decode(encodedPoly, byteCount);}
60 {Decode(encodedPoly, byteCount);}
65 {Randomize(rng, bitcount);}
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);
97 void Encode(
byte *output,
size_t outputLen)
const;
102 void Decode(
const byte *input,
size_t inputLen);
116 unsigned int BitCount()
const;
118 unsigned int ByteCount()
const;
120 unsigned int WordCount()
const;
123 bool GetBit(
size_t n)
const {
return GetCoefficient(n)!=0;}
128 signed int Degree()
const {
return (
signed int)(BitCount()-1U);}
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);}
138 bool IsZero()
const {
return !*
this;}
170 void SetBit(
size_t i,
int value = 1);
172 void SetByte(
size_t n,
byte value);
175 void SetCoefficient(
size_t i,
int value) {SetBit(i, value);}
184 bool operator!()
const;
217 unsigned int Parity()
const;
220 bool IsIrreducible()
const;
228 bool IsUnit()
const {
return Equals(One());}
244 friend std::ostream& operator<<(std::ostream& out,
const PolynomialMod2 &a);
255 inline bool operator==(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b)
256 {
return a.Equals(b);}
258 inline bool operator!=(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b)
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 bool operator< (
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b)
268 {
return a.Degree() < b.Degree();}
270 inline bool operator<=(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b)
271 {
return a.Degree() <= b.Degree();}
273 inline CryptoPP::PolynomialMod2
operator&(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.And(b);}
275 inline CryptoPP::PolynomialMod2
operator^(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.Xor(b);}
277 inline CryptoPP::PolynomialMod2
operator+(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.Plus(b);}
279 inline CryptoPP::PolynomialMod2
operator-(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.Minus(b);}
281 inline CryptoPP::PolynomialMod2
operator*(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.Times(b);}
283 inline CryptoPP::PolynomialMod2 operator/(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.DividedBy(b);}
285 inline CryptoPP::PolynomialMod2 operator%(
const CryptoPP::PolynomialMod2 &a,
const CryptoPP::PolynomialMod2 &b) {
return a.Modulo(b);}
301 virtual GF2NP * Clone()
const {
return new GF2NP(*
this);}
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);}
314 unsigned int MaxElementBitLength()
const
317 unsigned int MaxElementByteLength()
const
318 {
return (
unsigned int)
BitsToBytes(MaxElementBitLength());}
320 Element SquareRoot(
const Element &a)
const;
322 Element HalfTrace(
const Element &a)
const;
325 Element SolveQuadraticEquation(
const Element &a)
const;
336 GF2NT(
unsigned int t0,
unsigned int t1,
unsigned int t2);
338 GF2NP * Clone()
const {
return new GF2NT(*
this);}
341 const Element&
Multiply(
const Element &a,
const Element &b)
const;
343 const Element&
Square(
const Element &a)
const
344 {
return Reduced(a.Squared());}
349 const Element& Reduced(
const Element &a)
const;
362 GF2NT233(
unsigned int t0,
unsigned int t1,
unsigned int t2);
366 const Element&
Multiply(
const Element &a,
const Element &b)
const;
368 const Element&
Square(
const Element &a)
const;
376 GF2NPP(
unsigned int t0,
unsigned int t1,
unsigned int t2,
unsigned int t3,
unsigned int t4)
383 unsigned int t1, t2, t3;
393 template<>
inline void swap(CryptoPP::PolynomialMod2 &a, CryptoPP::PolynomialMod2 &b)
400 #if CRYPTOPP_MSC_VERSION
401 # pragma warning(pop)
Classes for performing mathematics over different fields.
Classes and functions for working with ANS.1 objects.
OID operator+(const OID &lhs, unsigned long rhs)
Append a value to an OID.
bool operator==(const OID &lhs, const OID &rhs)
Compare two OIDs for equality.
bool operator!=(const OID &lhs, const OID &rhs)
Compare two OIDs for inequality.
Abstract Euclidean domain.
Base class for all exceptions thrown by the library.
GF(2^n) with Polynomial Basis.
bool Equal(const Element &a, const Element &b) const
Compare two elements for equality.
bool IsUnit(const Element &a) const
Determines whether an element is a unit in the group.
GF(2^n) with Pentanomial Basis.
GF(2^n) for b233 and k233.
GF(2^n) with Trinomial Basis.
const Element & Square(const Element &a) const
Square an element in the group.
Excpetion thrown when divide by zero is encountered.
Polynomial with Coefficients in GF(2)
unsigned int MinEncodedSize() const
minimum number of bytes to encode this polynomial
PolynomialMod2 MultiplicativeInverse() const
return inverse if *this is a unit, otherwise return 0
signed int Degree() const
the zero polynomial will return a degree of -1
PolynomialMod2(RandomNumberGenerator &rng, size_t bitcount)
Create a uniformly distributed random polynomial.
bool IsUnit() const
only 1 is a unit
static PolynomialMod2 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.
PolynomialMod2 Doubled() const
is always zero since we're working modulo 2
unsigned int CoefficientCount() const
degree + 1
PolynomialMod2(BufferedTransformation &encodedPoly, size_t byteCount)
Construct a PolynomialMod2 from big-endian form stored in a BufferedTransformation.
int operator[](unsigned int i) const
return coefficient for x^i
PolynomialMod2(const byte *encodedPoly, size_t byteCount)
Construct a PolynomialMod2 from big-endian byte array.
int GetCoefficient(size_t i) const
return coefficient for x^i
bool GetBit(size_t n) const
return the n-th bit, n=0 being the least significant bit
const Element & Square(const Element &a) const
Square an element in the group.
const Element & MultiplicativeInverse(const Element &a) const
Calculate the multiplicative inverse of an element in the group.
const Element & Multiply(const Element &a, const Element &b) const
Multiplies elements in the group.
Interface for random number generators.
Abstract base classes that provide a uniform interface to this library.
bool operator>(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
bool operator>=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
bool operator<(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
bool operator<=(const ::PolynomialMod2 &a, const ::PolynomialMod2 &b)
compares degree
inline ::Integer operator&(const ::Integer &a, const ::Integer &b)
Bitwise AND.
inline ::Integer operator-(const ::Integer &a, const ::Integer &b)
Subtraction.
inline ::Integer operator^(const ::Integer &a, const ::Integer &b)
Bitwise XOR.
inline ::Integer operator*(const ::Integer &a, const ::Integer &b)
Multiplication.
Utility functions for the Crypto++ library.
unsigned int Parity(T value)
Returns the parity of a value.
size_t BitsToBytes(size_t bitCount)
Returns the number of 8-bit bytes or octets required for the specified number of bits.
unsigned int GetByte(ByteOrder order, T value, unsigned int index)
Gets a byte from a value.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Crypto++ library namespace.
Classes and functions for secure memory allocations.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.