6 #ifndef CRYPTOPP_IMPORTS 21 ANONYMOUS_NAMESPACE_BEGIN
23 using CryptoPP::PolynomialMod2;
25 #if defined(HAVE_GCC_INIT_PRIORITY) 28 #elif defined(HAVE_MSC_INIT_PRIORITY) 29 #pragma warning(disable: 4075) 30 #pragma init_seg(".CRT$XCU") 33 #pragma warning(default: 4075) 34 #elif defined(HAVE_XLC_INIT_PRIORITY) 40 ANONYMOUS_NAMESPACE_END
56 SetWords(reg+1, 0, reg.
size()-1);
63 CopyWords(reg, t.reg, reg.
size());
68 const size_t nbytes = nbits/8 + 1;
71 buf[0] = (byte)
Crop(buf[0], nbits % 8);
79 if (bitLength%WORD_BITS)
80 result.reg[result.reg.
size()-1] = (word)
Crop(result.reg[result.reg.
size()-1], bitLength%WORD_BITS);
84 void PolynomialMod2::SetBit(
size_t n,
int value)
89 reg[n/WORD_BITS] |= (word(1) << (n%WORD_BITS));
93 if (n/WORD_BITS < reg.
size())
94 reg[n/WORD_BITS] &= ~(word(1) << (n%WORD_BITS));
100 if (n/WORD_SIZE >= reg.
size())
103 return byte(reg[n/WORD_SIZE] >> ((n%WORD_SIZE)*8));
109 reg[n/WORD_SIZE] &= ~(word(0xff) << 8*(n%WORD_SIZE));
110 reg[n/WORD_SIZE] |= (word(value) << 8*(n%WORD_SIZE));
151 #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) || defined(HAVE_XLC_INIT_PRIORITY) 153 #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) 163 #if defined(HAVE_GCC_INIT_PRIORITY) || defined(HAVE_MSC_INIT_PRIORITY) || defined(HAVE_XLC_INIT_PRIORITY) 165 #elif defined(CRYPTOPP_CXX11_DYNAMIC_INIT) 173 void PolynomialMod2::Decode(
const byte *input,
size_t inputLen)
176 Decode(store, inputLen);
193 for (
size_t i=inputLen; i > 0; i--)
197 reg[(i-1)/WORD_SIZE] |= word(b) << ((i-1)%WORD_SIZE)*8;
203 for (
size_t i=outputLen; i > 0; i--)
217 if (!dec.IsDefiniteLength() || dec.RemainingLength() != length)
225 return (
unsigned int)CountWords(reg, reg.
size());
232 return (wordCount-1)*WORD_SIZE +
BytePrecision(reg[wordCount-1]);
241 return (wordCount-1)*WORD_BITS +
BitPrecision(reg[wordCount-1]);
250 for (i=0; i<reg.
size(); i++)
264 XorWords(reg, t.reg, t.reg.
size());
270 if (b.reg.size() >= reg.
size())
273 XorWords(result.reg, reg, b.reg, reg.
size());
274 CopyWords(result.reg+reg.
size(), b.reg+reg.
size(), b.reg.size()-reg.
size());
280 XorWords(result.reg, reg, b.reg, b.reg.size());
281 CopyWords(result.reg+b.reg.size(), reg+b.reg.size(), reg.
size()-b.reg.size());
289 AndWords(result.reg, reg, b.reg, result.reg.size());
297 for (
int i=b.Degree(); i>=0; i--)
301 XorWords(result.reg, reg, reg.
size());
308 static const word map[16] = {0, 1, 4, 5, 16, 17, 20, 21, 64, 65, 68, 69, 80, 81, 84, 85};
312 for (
unsigned i=0; i<reg.
size(); i++)
316 for (j=0; j<WORD_BITS; j+=8)
317 result.reg[2*i] |= map[(reg[i] >> (j/2)) % 16] << j;
319 for (j=0; j<WORD_BITS; j+=8)
320 result.reg[2*i+1] |= map[(reg[i] >> (j/2 + WORD_BITS/2)) % 16] << j;
332 int degree = divisor.
Degree();
339 for (
int i=dividend.
Degree(); i>=0; i--)
342 remainder.reg[0] |= dividend[i];
343 if (remainder[degree])
345 remainder -= divisor;
367 #if defined(CRYPTOPP_DEBUG) 368 int x=0; CRYPTOPP_UNUSED(x);
386 *r = (u << 1) | carry;
387 carry = u >> (WORD_BITS-1);
394 reg[reg.
size()-1] = carry;
400 const int shiftWords = n / WORD_BITS;
401 const int shiftBits = n % WORD_BITS;
409 *r = (u << shiftBits) | carry;
410 carry = u >> (WORD_BITS-shiftBits);
418 const size_t carryIndex = reg.
size();
419 reg.
Grow(reg.
size()+shiftWords+!!shiftBits);
420 reg[carryIndex] = carry;
427 for (i = (
int)reg.
size()-1; i>=shiftWords; i--)
428 reg[i] = reg[i-shiftWords];
441 int shiftWords = n / WORD_BITS;
442 int shiftBits = n % WORD_BITS;
447 word *r=reg+reg.
size()-1;
455 *r = (u >> shiftBits) | carry;
456 carry = u << (WORD_BITS-shiftBits);
463 for (i=0; i<reg.
size()-shiftWords; i++)
464 reg[i] = reg[i+shiftWords];
465 for (; i<reg.
size(); i++)
484 bool PolynomialMod2::operator!()
const 486 for (
unsigned i=0; i<reg.
size(); i++)
487 if (reg[i])
return false;
495 for (i=0; i<smallerSize; i++)
496 if (reg[i] != rhs.reg[i])
return false;
498 for (i=smallerSize; i<reg.
size(); i++)
499 if (reg[i] != 0)
return false;
501 for (i=smallerSize; i<rhs.reg.
size(); i++)
502 if (rhs.reg[i] != 0)
return false;
507 std::ostream& operator<<(std::ostream& out,
const PolynomialMod2 &a)
510 long f = out.flags() & std::ios::basefield;
532 return out <<
'0' << suffix;
537 static const char upper[]=
"0123456789ABCDEF";
538 static const char lower[]=
"0123456789abcdef";
539 const char*
const vec = (out.flags() & std::ios::uppercase) ? upper : lower;
541 for (i=0; i*bits < a.BitCount(); i++)
544 for (
int j=0; j<bits; j++)
545 digit |= a[i*bits+j] << j;
552 if (i && (i%block)==0)
556 return out << suffix;
577 for (
int i=1; i<=d/2; i++)
579 u = u.Squared()%(*this);
593 GF2NP::Element GF2NP::SquareRoot(
const Element &a)
const 596 for (
unsigned int i=1; i<m; i++)
601 GF2NP::Element GF2NP::HalfTrace(
const Element &a)
const 605 for (
unsigned int i=1; i<=(m-1)/2; i++)
610 GF2NP::Element GF2NP::SolveQuadraticEquation(
const Element &a)
const 621 for (
unsigned int i=1; i<=m-1; i++)
628 }
while (w.IsZero());
637 GF2NT::GF2NT(
unsigned int c0,
unsigned int c1,
unsigned int c2)
645 const GF2NT::Element& GF2NT::MultiplicativeInverse(
const Element &a)
const 647 if (t0-t1 < WORD_BITS)
652 word *c = T+m_modulus.reg.size();
653 word *f = T+2*m_modulus.reg.size();
654 word *g = T+3*m_modulus.reg.size();
655 size_t bcLen=1, fgLen=m_modulus.reg.size();
658 SetWords(T, 0, 3*m_modulus.reg.size());
661 CopyWords(f, a.reg, a.reg.size());
662 CopyWords(g, m_modulus.reg, m_modulus.reg.size());
669 ShiftWordsRightByWords(f, fgLen, 1);
673 ShiftWordsLeftByWords(c, bcLen, 1);
686 if (t==1 && CountWords(f, fgLen)==1)
691 ShiftWordsRightByBits(f, fgLen, 1);
692 t=ShiftWordsLeftByBits(c, bcLen, 1);
696 ShiftWordsRightByBits(f, fgLen, i);
697 t=ShiftWordsLeftByBits(c, bcLen, i);
706 if (f[fgLen-1]==0 && g[fgLen-1]==0)
709 if (f[fgLen-1] < g[fgLen-1])
715 XorWords(f, g, fgLen);
716 XorWords(b, c, bcLen);
719 while (k >= WORD_BITS)
728 for (
unsigned int j=0; j<WORD_BITS-t1; j++)
732 const unsigned int shift = t1 + j;
734 temp ^= (shift < WORD_BITS) ? (((temp >> j) & 1) << shift) : 0;
737 b[t1/WORD_BITS-1] ^= temp << t1%WORD_BITS;
740 b[t1/WORD_BITS] ^= temp >> (WORD_BITS - t1%WORD_BITS);
744 b[t0/WORD_BITS-1] ^= temp << t0%WORD_BITS;
745 b[t0/WORD_BITS] ^= temp >> (WORD_BITS - t0%WORD_BITS);
748 b[t0/WORD_BITS-1] ^= temp;
755 word temp = b[0] << (WORD_BITS - k);
760 for (
unsigned int j=0; j<WORD_BITS-t1; j++)
764 const unsigned int shift = t1 + j;
766 temp ^= (shift < WORD_BITS) ? (((temp >> j) & 1) << shift) : 0;
771 b[t1/WORD_BITS-1] ^= temp << t1%WORD_BITS;
775 b[t1/WORD_BITS] ^= temp >> (WORD_BITS - t1%WORD_BITS);
779 b[t0/WORD_BITS-1] ^= temp << t0%WORD_BITS;
780 b[t0/WORD_BITS] ^= temp >> (WORD_BITS - t0%WORD_BITS);
783 b[t0/WORD_BITS-1] ^= temp;
786 CopyWords(result.reg.
begin(), b, result.reg.
size());
790 const GF2NT::Element& GF2NT::Multiply(
const Element &a,
const Element &b)
const 792 size_t aSize =
STDMIN(a.reg.size(), result.reg.
size());
793 Element r((word)0, m);
795 for (
int i=m-1; i>=0; i--)
799 ShiftWordsLeftByBits(r.reg.begin(), r.reg.size(), 1);
800 XorWords(r.reg.begin(), m_modulus.reg, r.reg.size());
803 ShiftWordsLeftByBits(r.reg.begin(), r.reg.size(), 1);
806 XorWords(r.reg.begin(), a.reg, aSize);
810 r.reg.begin()[r.reg.size()-1] = (word)
Crop(r.reg[r.reg.size()-1], m%WORD_BITS);
812 CopyWords(result.reg.
begin(), r.reg.begin(), result.reg.
size());
816 const GF2NT::Element& GF2NT::Reduced(
const Element &a)
const 818 if (t0-t1 < WORD_BITS)
819 return m_domain.Mod(a, m_modulus);
830 b[i-t0/WORD_BITS] ^= temp >> t0%WORD_BITS;
831 b[i-t0/WORD_BITS-1] ^= temp << (WORD_BITS - t0%WORD_BITS);
834 b[i-t0/WORD_BITS] ^= temp;
836 if ((t0-t1)%WORD_BITS)
838 b[i-(t0-t1)/WORD_BITS] ^= temp >> (t0-t1)%WORD_BITS;
839 b[i-(t0-t1)/WORD_BITS-1] ^= temp << (WORD_BITS - (t0-t1)%WORD_BITS);
842 b[i-(t0-t1)/WORD_BITS] ^= temp;
847 word mask = ((word)1<<(t0%WORD_BITS))-1;
848 word temp = b[i] & ~mask;
851 b[i-t0/WORD_BITS] ^= temp >> t0%WORD_BITS;
853 if ((t0-t1)%WORD_BITS)
855 b[i-(t0-t1)/WORD_BITS] ^= temp >> (t0-t1)%WORD_BITS;
856 if ((t0-t1)%WORD_BITS > t0%WORD_BITS)
857 b[i-(t0-t1)/WORD_BITS-1] ^= temp << (WORD_BITS - (t0-t1)%WORD_BITS);
862 b[i-(t0-t1)/WORD_BITS] ^= temp;
865 SetWords(result.reg.
begin(), 0, result.reg.
size());
872 a.DEREncodeAsOctetString(out, MaxElementByteLength());
877 a.BERDecodeAsOctetString(in, MaxElementByteLength());
883 ASN1::characteristic_two_field().DEREncode(seq);
886 ASN1::tpBasis().DEREncode(parameters);
888 parameters.MessageEnd();
895 ASN1::characteristic_two_field().DEREncode(seq);
898 ASN1::ppBasis().DEREncode(parameters);
903 pentanomial.MessageEnd();
904 parameters.MessageEnd();
913 if (
OID(seq) != ASN1::characteristic_two_field())
919 if (oid == ASN1::tpBasis())
923 result.reset(
new GF2NT(m, t1, 0));
925 else if (oid == ASN1::ppBasis())
927 unsigned int t1, t2, t3;
932 pentanomial.MessageEnd();
933 result.reset(
new GF2NPP(m, t3, t2, t1, 0));
940 parameters.MessageEnd();
943 return result.release();
Element & Accumulate(Element &a, const Element &b) const
const Element & Add(const Element &a, const Element &b) const
An invalid argument was detected.
static const PolynomialMod2 & Zero()
The Zero polinomial.
Randomness Pool based on AES-256.
bool SafeConvert(T1 from, T2 &to)
Tests whether a conversion from -> to is safe to perform.
Utility functions for the Crypto++ library.
PolynomialMod2()
Construct the zero polynomial.
Restricts the instantiation of a class to one static object without locks.
void CleanNew(size_type newSize)
Change size without preserving contents.
Class file for Randomness Pool.
size_t DEREncodeUnsigned(BufferedTransformation &out, T w, byte asnTag=INTEGER)
DER Encode unsigned value.
virtual void GenerateBlock(byte *output, size_t size)
Generate random array of bytes.
bool IsUnit() const
only 1 is a unit
GF(2^n) with Trinomial Basis.
size_t BitsToWords(size_t bitCount)
Returns the number of words required for the specified number of bits.
unsigned int BytePrecision(const T &value)
Returns the number of 8-bit bytes or octets required for a value.
void CleanGrow(size_type newSize)
Change size and preserve contents.
Secure memory block with allocator and cleanup.
Abstract base classes that provide a uniform interface to this library.
static const PolynomialMod2 & One()
The One polinomial.
void BERDecodeUnsigned(BufferedTransformation &in, T &w, byte asnTag=INTEGER, T minValue=0, T maxValue=T(0xffffffff))
BER Decode unsigned value.
void Encode(byte *output, size_t outputLen) const
encode in big-endian format
ASN.1 object identifiers for algorthms and schemes.
Classes for automatic resource management.
Library configuration file.
Interface for random number generators.
size_t BytesToWords(size_t byteCount)
Returns the number of words required for the specified number of bytes.
static PolynomialMod2 Monomial(size_t i)
Provides x^i.
const Element & Square(const Element &a) const
Classes for performing mathematics over different fields.
bool IsIrreducible() const
check for irreducibility
unsigned int WordCount() const
number of significant words = ceiling(ByteCount()/sizeof(word))
Polynomial with Coefficients in GF(2)
unsigned int BitCount() const
number of significant bits = Degree() + 1
Excpetion thrown when divide by zero is encountered.
static PolynomialMod2 Gcd(const PolynomialMod2 &a, const PolynomialMod2 &n)
greatest common divisor
Copy input to a memory buffer.
const Element & Multiply(const Element &a, const Element &b) const
T Crop(T value, size_t bits)
Truncates the value to the specified number of bits.
void Assign(const T *ptr, size_type len)
Set contents and size from an array.
Classes and functions for schemes over GF(2^n)
unsigned int Parity(T value)
Returns the parity of a value.
const T & STDMIN(const T &a, const T &b)
Replacement function for std::min.
String-based implementation of Store interface.
#define CRYPTOPP_ASSERT(exp)
Debugging and diagnostic assertion.
void SetByte(size_t n, byte value)
set the n-th byte to value
void BERDecodeError()
Raises a BERDecodeErr.
Classes and functions for working with ANS.1 objects.
iterator begin()
Provides an iterator pointing to the first element in the memory block.
Implementation of BufferedTransformation's attachment interface.
GF(2^n) with Pentanomial Basis.
static PolynomialMod2 AllOnes(size_t n)
Provides x^(n-1) + ...
GF(2^n) with Polynomial Basis.
PolynomialMod2 InverseMod(const PolynomialMod2 &) const
calculate multiplicative inverse of *this mod n
PolynomialMod2 MultiplicativeInverse() const
return inverse if *this is a unit, otherwise return 0
void DEREncodeAsOctetString(BufferedTransformation &bt, size_t length) const
encode value as big-endian octet string
static void Divide(PolynomialMod2 &r, PolynomialMod2 &q, const PolynomialMod2 &a, const PolynomialMod2 &d)
calculate r and q such that (a == d*q + r) && (deg(r) < deg(d))
unsigned int ByteCount() const
number of significant bytes = ceiling(BitCount()/8)
byte GetByte(size_t n) const
return the n-th byte
signed int Degree() const
the zero polynomial will return a degree of -1
void Grow(size_type newSize)
Change size and preserve contents.
void BERDecodeAsOctetString(BufferedTransformation &bt, size_t length)
decode value as big-endian octet string
Crypto++ library namespace.
const Element & MultiplicativeInverse(const Element &a) const
static PolynomialMod2 Trinomial(size_t t0, size_t t1, size_t t2)
Provides x^t0 + x^t1 + x^t2.
unsigned int Parity() const
sum modulo 2 of all coefficients
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.
unsigned int BitPrecision(const T &value)
Returns the number of bits required for a value.
size_type size() const
Provides the count of elements in the SecBlock.
#define SIZE_MAX
The maximum value of a machine word.