13NAMESPACE_BEGIN(CryptoPP)
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();
96 m_coefficients.resize(t.m_coefficients.size());
97 for (
unsigned int i=0; i<m_coefficients.size(); i++)
98 m_coefficients[i] = t.m_coefficients[i];
106 unsigned int count = t.CoefficientCount(ring);
108 if (count > CoefficientCount(ring))
109 m_coefficients.resize(count, ring.Identity());
111 for (
unsigned int i=0; i<count; i++)
120 unsigned int count = t.CoefficientCount(ring);
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();
139 CoefficientType result = m_coefficients[degree];
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]);
200 m_coefficients.swap(t.m_coefficients);
206 unsigned int count = CoefficientCount(ring);
208 if (count != t.CoefficientCount(ring))
211 for (
unsigned int i=0; i<count; i++)
212 if (!ring.Equal(m_coefficients[i], t.m_coefficients[i]))
222 unsigned int count = CoefficientCount(ring);
223 unsigned int tCount = t.CoefficientCount(ring);
229 for (i=0; i<tCount; i++)
230 result.m_coefficients[i] = ring.Add(m_coefficients[i], t.m_coefficients[i]);
232 result.m_coefficients[i] = m_coefficients[i];
240 for (i=0; i<count; i++)
241 result.m_coefficients[i] = ring.Add(m_coefficients[i], t.m_coefficients[i]);
242 for (; i<tCount; i++)
243 result.m_coefficients[i] = t.m_coefficients[i];
253 unsigned int count = CoefficientCount(ring);
254 unsigned int tCount = t.CoefficientCount(ring);
260 for (i=0; i<tCount; i++)
261 result.m_coefficients[i] = ring.Subtract(m_coefficients[i], t.m_coefficients[i]);
263 result.m_coefficients[i] = m_coefficients[i];
271 for (i=0; i<count; i++)
272 result.m_coefficients[i] = ring.Subtract(m_coefficients[i], t.m_coefficients[i]);
273 for (; i<tCount; i++)
274 result.m_coefficients[i] = ring.Inverse(t.m_coefficients[i]);
283 unsigned int count = CoefficientCount(ring);
286 for (
unsigned int i=0; i<count; i++)
287 result.m_coefficients[i] = ring.Inverse(m_coefficients[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++)
303 ring.Accumulate(result.m_coefficients[i+j], ring.Multiply(m_coefficients[i], t.m_coefficients[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];
389 CoefficientType inverse = ring.Inverse(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();
432 unsigned int i = a.CoefficientCount(ring);
433 const int dDegree = d.Degree(ring);
439 q.m_coefficients.resize(
STDMAX(0,
int(i - dDegree)));
441 while (i > (
unsigned int)dDegree)
444 q.m_coefficients[i-dDegree] = ring.Divide(r.m_coefficients[i], d.m_coefficients[dDegree]);
445 for (
int j=0; j<=dDegree; j++)
446 ring.Reduce(r.m_coefficients[i-dDegree+j], ring.Multiply(q.m_coefficients[i-dDegree], d.m_coefficients[j]));
449 r.CoefficientCount(ring);
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]);
467 CoefficientType d = m_ring.Subtract(x[j], x[j-k]);
468 if (!m_ring.IsUnit(d))
469 throw InterpolationFailed();
470 alpha[j] = m_ring.Divide(alpha[j], d);
478 CRYPTOPP_ASSERT(n > 0);
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]));
498typename RingOfPolynomialsOver<T>::CoefficientType
RingOfPolynomialsOver<T>::InterpolateAt(
const CoefficientType &position,
const CoefficientType x[],
const CoefficientType y[],
unsigned int n)
const
500 CRYPTOPP_ASSERT(n > 0);
502 std::vector<CoefficientType> alpha(n);
503 CalculateAlpha(alpha, x, y, n);
505 CoefficientType result = alpha[n-1];
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]);
514template <
class Ring,
class Element>
515void PrepareBulkPolynomialInterpolation(
const Ring &ring, Element *w,
const Element x[],
unsigned int n)
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);
527template <
class Ring,
class Element>
528void PrepareBulkPolynomialInterpolationAt(
const Ring &ring, Element *v,
const Element &position,
const Element x[],
const Element w[],
unsigned int n)
530 CRYPTOPP_ASSERT(n > 0);
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++)
545 std::swap(a[2*i+1], a[2*i+2]);
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]);
554template <
class Ring,
class Element>
555Element BulkPolynomialInterpolateAt(
const Ring &ring,
const Element y[],
const Element v[],
unsigned int n)
557 Element result = ring.Identity();
558 for (
unsigned int i=0; i<n; i++)
559 ring.Accumulate(result, ring.Multiply(y[i], v[i]));
565template <
class T,
int instance>
571template <
class T,
int instance>
division by zero exception
Polynomials over a fixed ring.
represents single-variable polynomials over arbitrary rings
CoefficientType GetCoefficient(unsigned int i, const Ring &ring) const
return coefficient for x^i
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)
void SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring)
set the coefficient for x^i to value
Interface for random number generators.
Ring of polynomials over another ring.
Secure memory block with allocator and cleanup.
Restricts the instantiation of a class to one static object without locks.
CRYPTOPP_NOINLINE const T & Ref(CRYPTOPP_NOINLINE_DOTDOTDOT) const
Return a reference to the inner Singleton object.
const T & STDMAX(const T &a, const T &b)
Replacement function for std::max.
Classes for polynomial basis and operations.
Classes and functions for secure memory allocations.