18 m_coefficients.resize(parameter.m_coefficientCount);
19 for (
unsigned int i=0; i<m_coefficients.size(); ++i)
20 m_coefficients[i] = ring.RandomElement(rng, parameter.m_coefficientParameter);
26 std::istringstream in((
char *)str);
35 coef = ring.MultiplicativeIdentity();
56 coef = ring.Inverse(coef);
58 SetCoefficient(power, coef, ring);
78 unsigned count = m_coefficients.size();
79 while (count && ring.Equal(m_coefficients[count-1], ring.Identity()))
81 const_cast<std::vector<CoefficientType> &
>(m_coefficients).resize(count);
88 return (i < m_coefficients.size()) ? m_coefficients[i] : ring.Identity();
97 for (
unsigned int i=0; i<m_coefficients.size(); i++)
108 if (count > CoefficientCount(ring))
109 m_coefficients.resize(count, ring.Identity());
111 for (
unsigned int i=0; i<
count; i++)
122 if (count > CoefficientCount(ring))
123 m_coefficients.resize(count, ring.Identity());
125 for (
unsigned int i=0; i<
count; i++)
134 int degree = Degree(ring);
137 return ring.Identity();
140 for (
int j=degree-1; j>=0; j--)
142 result = ring.Multiply(result, x);
143 ring.Accumulate(result, m_coefficients[j]);
151 unsigned int i = CoefficientCount(ring) + n;
152 m_coefficients.resize(i, ring.Identity());
156 m_coefficients[i] = m_coefficients[i-n];
161 m_coefficients[i] = ring.Identity();
169 unsigned int count = CoefficientCount(ring);
172 for (
unsigned int i=0; i<count-n; i++)
173 m_coefficients[i] = m_coefficients[i+n];
174 m_coefficients.resize(count-n, ring.Identity());
177 m_coefficients.resize(0, ring.Identity());
184 if (i >= m_coefficients.size())
185 m_coefficients.resize(i+1, ring.Identity());
186 m_coefficients[i] = value;
192 unsigned int count = CoefficientCount(ring);
193 for (
unsigned int i=0; i<
count; i++)
194 m_coefficients[i] = ring.Inverse(m_coefficients[i]);
206 unsigned int count = CoefficientCount(ring);
211 for (
unsigned int i=0; i<
count; i++)
222 unsigned int count = CoefficientCount(ring);
229 for (i=0; i<tCount; i++)
240 for (i=0; i<
count; i++)
242 for (; i<tCount; i++)
253 unsigned int count = CoefficientCount(ring);
260 for (i=0; i<tCount; i++)
271 for (i=0; i<
count; i++)
273 for (; i<tCount; i++)
283 unsigned int count = CoefficientCount(ring);
286 for (
unsigned int i=0; i<
count; i++)
295 if (IsZero(ring) || t.
IsZero(ring))
298 unsigned int count1 = CoefficientCount(ring), count2 = t.
CoefficientCount(ring);
301 for (
unsigned int i=0; i<count1; i++)
302 for (
unsigned int j=0; j<count2; j++)
312 Divide(remainder, quotient, *
this, t, ring);
320 Divide(remainder, quotient, *
this, t, ring);
327 return Degree(ring)==0 ? ring.MultiplicativeInverse(m_coefficients[0]) : ring.Identity();
333 return Degree(ring)==0 && ring.IsUnit(m_coefficients[0]);
340 unsigned int length = 0;
346 if (in.peek() ==
'(')
356 if (length >= str.
size())
357 str.
Grow(length + 16);
361 while (in && ((paren && c !=
')') || (!paren && c !=
'\n')));
363 str[length-1] =
'\0';
372 unsigned int i = CoefficientCount(ring);
375 bool firstTerm =
true;
379 if (m_coefficients[i] != ring.Identity())
384 if (!i || !ring.Equal(m_coefficients[i], ring.MultiplicativeIdentity()))
385 out << m_coefficients[i];
390 std::ostringstream pstr, nstr;
392 pstr << m_coefficients[i];
395 if (pstr.str().size() <= nstr.str().size())
398 if (!i || !ring.Equal(m_coefficients[i], ring.MultiplicativeIdentity()))
399 out << m_coefficients[i];
404 if (!i || !ring.Equal(inverse, ring.MultiplicativeIdentity()))
424 out << ring.Identity();
433 const int dDegree = d.
Degree(ring);
441 while (i > (
unsigned int)dDegree)
445 for (
int j=0; j<=dDegree; j++)
458 for (
unsigned int j=0; j<n; ++j)
461 for (
unsigned int k=1; k<n; ++k)
463 for (
unsigned int j=n-1; j>=k; --j)
465 m_ring.Reduce(alpha[j], alpha[j-1]);
468 if (!m_ring.IsUnit(d))
470 alpha[j] = m_ring.Divide(alpha[j], d);
480 std::vector<CoefficientType> alpha(n);
481 CalculateAlpha(alpha, x, y, n);
483 std::vector<CoefficientType> coefficients((
size_t)n, m_ring.Identity());
484 coefficients[0] = alpha[n-1];
486 for (
int j=n-2; j>=0; --j)
488 for (
unsigned int i=n-j-1; i>0; i--)
489 coefficients[i] = m_ring.Subtract(coefficients[i-1], m_ring.Multiply(coefficients[i], x[j]));
491 coefficients[0] = m_ring.Subtract(alpha[j], m_ring.Multiply(coefficients[0], x[j]));
502 std::vector<CoefficientType> alpha(n);
503 CalculateAlpha(alpha, x, y, n);
506 for (
int j=n-2; j>=0; --j)
508 result = m_ring.Multiply(result, m_ring.Subtract(position, x[j]));
509 m_ring.Accumulate(result, alpha[j]);
514 template <
class Ring,
class Element>
517 for (
unsigned int i=0; i<n; i++)
519 Element t = ring.MultiplicativeIdentity();
520 for (
unsigned int j=0; j<n; j++)
522 t = ring.Multiply(t, ring.Subtract(x[i], x[j]));
523 w[i] = ring.MultiplicativeInverse(t);
527 template <
class Ring,
class Element>
532 std::vector<Element>
a(2*n-1);
536 a[n-1+i] = ring.Subtract(position, x[i]);
538 for (i=n-1; i>1; i--)
539 a[i-1] = ring.Multiply(a[2*i], a[2*i-1]);
541 a[0] = ring.MultiplicativeIdentity();
543 for (i=0; i<n-1; i++)
546 a[2*i+1] = ring.Multiply(a[i], a[2*i+1]);
547 a[2*i+2] = ring.Multiply(a[i], a[2*i+2]);
551 v[i] = ring.Multiply(a[n-1+i], w[i]);
554 template <
class Ring,
class Element>
557 Element result = ring.Identity();
558 for (
unsigned int i=0; i<n; i++)
559 ring.Accumulate(result, ring.Multiply(y[i], v[i]));
565 template <
class T,
int instance>
571 template <
class T,
int instance>
PolynomialOver< Ring > & operator=(const PolynomialOver< Ring > &t)
specify the distribution for randomization functions
Element::CoefficientType CoefficientType
void SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring)
set the coefficient for x^i to value
Restricts the instantiation of a class to one static object without locks.
void swap(dev::eth::Watch &_a, dev::eth::Watch &_b)
PolynomialOver< Ring > Minus(const PolynomialOver< Ring > &t, const Ring &ring) const
#define NAMESPACE_BEGIN(x)
PolynomialOver< Ring > & ShiftRight(unsigned int n, const Ring &ring)
Secure memory block with allocator and cleanup.
void swap(PolynomialOver< Ring > &t)
CoefficientType GetCoefficient(unsigned int i, const Ring &ring) const
return coefficient for x^i
size_type size() const
Provides the count of elements in the SecBlock.
Interface for random number generators.
bool Equals(const PolynomialOver< Ring > &t, const Ring &ring) const
PolynomialOver< Ring > Inverse(const Ring &ring) const
unsigned int CoefficientCount(const Ring &ring) const
represents single-variable polynomials over arbitrary rings
T::Element CoefficientType
bool IsUnit(const Ring &ring) const
Classes and functions for secure memory allocations.
CoefficientType EvaluateAt(const CoefficientType &x, const Ring &ring) const
bool IsZero(const Ring &ring) const
PolynomialOver< Ring > Plus(const PolynomialOver< Ring > &t, const Ring &ring) const
Element Interpolate(const CoefficientType x[], const CoefficientType y[], unsigned int n) const
void FromStr(const char *str, const Ring &ring)
CoefficientType InterpolateAt(const CoefficientType &position, const CoefficientType x[], const CoefficientType y[], unsigned int n) const
void CalculateAlpha(std::vector< CoefficientType > &alpha, const CoefficientType x[], const CoefficientType y[], unsigned int n) const
PolynomialOver< Ring > & Accumulate(const PolynomialOver< Ring > &t, const Ring &ring)
PolynomialOver< Ring > Modulo(const PolynomialOver< Ring > &t, const Ring &ring) const
void Divide(word *R, word *Q, word *T, const word *A, size_t NA, const word *B, size_t NB)
Polynomials over a fixed ring.
static const ThisType & Zero()
PolynomialOver< Ring > & ShiftLeft(unsigned int n, const Ring &ring)
PolynomialOver< Ring > DividedBy(const PolynomialOver< Ring > &t, const Ring &ring) const
#define CRYPTOPP_ASSERT(exp)
Classes for polynomial basis and operations.
void PrepareBulkPolynomialInterpolationAt(const Ring &ring, Element *v, const Element &position, const Element x[], const Element w[], unsigned int n)
static const ThisType & One()
static void Divide(PolynomialOver< Ring > &r, PolynomialOver< Ring > &q, const PolynomialOver< Ring > &a, const PolynomialOver< Ring > &d, const Ring &ring)
calculate r and q such that (a == d*q + r) && (0 <= degree of r < degree of d)
PolynomialOver< Ring > & Reduce(const PolynomialOver< Ring > &t, const Ring &ring)
std::ostream & Output(std::ostream &out, const Ring &ring) const
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
void Grow(size_type newSize)
Change size and preserve contents.
std::vector< CoefficientType > m_coefficients
division by zero exception
void PrepareBulkPolynomialInterpolation(const Ring &ring, Element *w, const Element x[], unsigned int n)
PolynomialOver< Ring > Times(const PolynomialOver< Ring > &t, const Ring &ring) const
void Negate(const Ring &ring)
Element BulkPolynomialInterpolateAt(const Ring &ring, const Element y[], const Element v[], unsigned int n)
int Degree(const Ring &ring) const
the zero polynomial will return a degree of -1
PolynomialOver< Ring > MultiplicativeInverse(const Ring &ring) const
std::istream & Input(std::istream &in, const Ring &ring)