// polynomi.cpp - written and placed in the public domain by Wei Dai // Part of the code for polynomial evaluation and interpolation // originally came from Hal Finney's public domain secsplit.c. #include "pch.h" #include "polynomi.h" #include "secblock.h" #include // can't use because GCC 2.95.2 doesn't have it #include NAMESPACE_BEGIN(CryptoPP) template void PolynomialOver::Randomize(RandomNumberGenerator &rng, const RandomizationParameter ¶meter, const Ring &ring) { m_coefficients.resize(parameter.m_coefficientCount); for (unsigned int i=0; i void PolynomialOver::FromStr(const char *str, const Ring &ring) { std::istrstream in((char *)str); bool positive = true; CoefficientType coef; unsigned int power; while (in) { std::ws(in); if (in.peek() == 'x') coef = ring.MultiplicativeIdentity(); else in >> coef; std::ws(in); if (in.peek() == 'x') { in.get(); std::ws(in); if (in.peek() == '^') { in.get(); in >> power; } else power = 1; } else power = 0; if (!positive) coef = ring.Inverse(coef); SetCoefficient(power, coef, ring); std::ws(in); switch (in.get()) { case '+': positive = true; break; case '-': positive = false; break; default: return; // something's wrong with the input string } } } template unsigned int PolynomialOver::CoefficientCount(const Ring &ring) const { unsigned count = m_coefficients.size(); while (count && ring.Equal(m_coefficients[count-1], ring.Identity())) count--; const_cast &>(m_coefficients).resize(count); return count; } template typename PolynomialOver::CoefficientType PolynomialOver::GetCoefficient(unsigned int i, const Ring &ring) const { return (i < m_coefficients.size()) ? m_coefficients[i] : ring.Identity(); } template PolynomialOver& PolynomialOver::operator=(const PolynomialOver& t) { if (this != &t) { m_coefficients.resize(t.m_coefficients.size()); for (unsigned int i=0; i PolynomialOver& PolynomialOver::Accumulate(const PolynomialOver& t, const Ring &ring) { unsigned int count = t.CoefficientCount(ring); if (count > CoefficientCount(ring)) m_coefficients.resize(count, ring.Identity()); for (unsigned int i=0; i PolynomialOver& PolynomialOver::Reduce(const PolynomialOver& t, const Ring &ring) { unsigned int count = t.CoefficientCount(ring); if (count > CoefficientCount(ring)) m_coefficients.resize(count, ring.Identity()); for (unsigned int i=0; i typename PolynomialOver::CoefficientType PolynomialOver::EvaluateAt(const CoefficientType &x, const Ring &ring) const { int degree = Degree(ring); if (degree < 0) return ring.Identity(); CoefficientType result = m_coefficients[degree]; for (int j=degree-1; j>=0; j--) { result = ring.Multiply(result, x); ring.Accumulate(result, m_coefficients[j]); } return result; } template PolynomialOver& PolynomialOver::ShiftLeft(unsigned int n, const Ring &ring) { unsigned int i = CoefficientCount(ring) + n; m_coefficients.resize(i, ring.Identity()); while (i > n) { i--; m_coefficients[i] = m_coefficients[i-n]; } while (i) { i--; m_coefficients[i] = ring.Identity(); } return *this; } template PolynomialOver& PolynomialOver::ShiftRight(unsigned int n, const Ring &ring) { unsigned int count = CoefficientCount(ring); if (count > n) { for (unsigned int i=0; i void PolynomialOver::SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring) { if (i >= m_coefficients.size()) m_coefficients.resize(i+1, ring.Identity()); m_coefficients[i] = value; } template void PolynomialOver::Negate(const Ring &ring) { unsigned int count = CoefficientCount(ring); for (unsigned int i=0; i void PolynomialOver::swap(PolynomialOver &t) { m_coefficients.swap(t.m_coefficients); } template bool PolynomialOver::Equals(const PolynomialOver& t, const Ring &ring) const { unsigned int count = CoefficientCount(ring); if (count != t.CoefficientCount(ring)) return false; for (unsigned int i=0; i PolynomialOver PolynomialOver::Plus(const PolynomialOver& t, const Ring &ring) const { unsigned int i; unsigned int count = CoefficientCount(ring); unsigned int tCount = t.CoefficientCount(ring); if (count > tCount) { PolynomialOver result(ring, count); for (i=0; i result(ring, tCount); for (i=0; i PolynomialOver PolynomialOver::Minus(const PolynomialOver& t, const Ring &ring) const { unsigned int i; unsigned int count = CoefficientCount(ring); unsigned int tCount = t.CoefficientCount(ring); if (count > tCount) { PolynomialOver result(ring, count); for (i=0; i result(ring, tCount); for (i=0; i PolynomialOver PolynomialOver::Inverse(const Ring &ring) const { unsigned int count = CoefficientCount(ring); PolynomialOver result(ring, count); for (unsigned int i=0; i PolynomialOver PolynomialOver::Times(const PolynomialOver& t, const Ring &ring) const { if (IsZero(ring) || t.IsZero(ring)) return PolynomialOver(); unsigned int count1 = CoefficientCount(ring), count2 = t.CoefficientCount(ring); PolynomialOver result(ring, count1 + count2 - 1); for (unsigned int i=0; i PolynomialOver PolynomialOver::DividedBy(const PolynomialOver& t, const Ring &ring) const { PolynomialOver remainder, quotient; Divide(remainder, quotient, *this, t, ring); return quotient; } template PolynomialOver PolynomialOver::Modulo(const PolynomialOver& t, const Ring &ring) const { PolynomialOver remainder, quotient; Divide(remainder, quotient, *this, t, ring); return remainder; } template PolynomialOver PolynomialOver::MultiplicativeInverse(const Ring &ring) const { return Degree(ring)==0 ? ring.MultiplicativeInverse(m_coefficients[0]) : ring.Identity(); } template bool PolynomialOver::IsUnit(const Ring &ring) const { return Degree(ring)==0 && ring.IsUnit(m_coefficients[0]); } template std::istream& PolynomialOver::Input(std::istream &in, const Ring &ring) { char c; unsigned int length = 0; SecBlock str(length + 16); bool paren = false; std::ws(in); if (in.peek() == '(') { paren = true; in.get(); } do { in.read(&c, 1); str[length++] = c; if (length >= str.size()) str.Grow(length + 16); } // if we started with a left paren, then read until we find a right paren, // otherwise read until the end of the line while (in && ((paren && c != ')') || (!paren && c != '\n'))); str[length-1] = '\0'; *this = PolynomialOver(str, ring); return in; } template std::ostream& PolynomialOver::Output(std::ostream &out, const Ring &ring) const { unsigned int i = CoefficientCount(ring); if (i) { bool firstTerm = true; while (i--) { if (m_coefficients[i] != ring.Identity()) { if (firstTerm) { firstTerm = false; if (!i || !ring.Equal(m_coefficients[i], ring.MultiplicativeIdentity())) out << m_coefficients[i]; } else { CoefficientType inverse = ring.Inverse(m_coefficients[i]); std::ostrstream pstr, nstr; pstr << m_coefficients[i]; nstr << inverse; if (pstr.pcount() <= nstr.pcount()) { out << " + "; if (!i || !ring.Equal(m_coefficients[i], ring.MultiplicativeIdentity())) out << m_coefficients[i]; } else { out << " - "; if (!i || !ring.Equal(inverse, ring.MultiplicativeIdentity())) out << inverse; } } switch (i) { case 0: break; case 1: out << "x"; break; default: out << "x^" << i; } } } } else { out << ring.Identity(); } return out; } template void PolynomialOver::Divide(PolynomialOver &r, PolynomialOver &q, const PolynomialOver &a, const PolynomialOver &d, const Ring &ring) { unsigned int i = a.CoefficientCount(ring); const int dDegree = d.Degree(ring); if (dDegree < 0) throw DivideByZero(); r = a; q.m_coefficients.resize(STDMAX(0, int(i - dDegree))); while (i > (unsigned int)dDegree) { --i; q.m_coefficients[i-dDegree] = ring.Divide(r.m_coefficients[i], d.m_coefficients[dDegree]); for (int j=0; j<=dDegree; j++) ring.Reduce(r.m_coefficients[i-dDegree+j], ring.Multiply(q.m_coefficients[i-dDegree], d.m_coefficients[j])); } r.CoefficientCount(ring); // resize r.m_coefficients } // ******************************************************** // helper function for Interpolate() and InterpolateAt() template void RingOfPolynomialsOver::CalculateAlpha(std::vector &alpha, const CoefficientType x[], const CoefficientType y[], unsigned int n) const { for (unsigned int j=0; j=k; --j) { m_ring.Reduce(alpha[j], alpha[j-1]); CoefficientType d = m_ring.Subtract(x[j], x[j-k]); if (!m_ring.IsUnit(d)) throw InterpolationFailed(); alpha[j] = m_ring.Divide(alpha[j], d); } } } template typename RingOfPolynomialsOver::Element RingOfPolynomialsOver::Interpolate(const CoefficientType x[], const CoefficientType y[], unsigned int n) const { assert(n > 0); std::vector alpha(n); CalculateAlpha(alpha, x, y, n); std::vector coefficients((size_t)n, m_ring.Identity()); coefficients[0] = alpha[n-1]; for (int j=n-2; j>=0; --j) { for (unsigned int i=n-j-1; i>0; i--) coefficients[i] = m_ring.Subtract(coefficients[i-1], m_ring.Multiply(coefficients[i], x[j])); coefficients[0] = m_ring.Subtract(alpha[j], m_ring.Multiply(coefficients[0], x[j])); } return PolynomialOver(coefficients.begin(), coefficients.end()); } template typename RingOfPolynomialsOver::CoefficientType RingOfPolynomialsOver::InterpolateAt(const CoefficientType &position, const CoefficientType x[], const CoefficientType y[], unsigned int n) const { assert(n > 0); std::vector alpha(n); CalculateAlpha(alpha, x, y, n); CoefficientType result = alpha[n-1]; for (int j=n-2; j>=0; --j) { result = m_ring.Multiply(result, m_ring.Subtract(position, x[j])); m_ring.Accumulate(result, alpha[j]); } return result; } template void PrepareBulkPolynomialInterpolation(const Ring &ring, Element *w, const Element x[], unsigned int n) { for (unsigned int i=0; i void PrepareBulkPolynomialInterpolationAt(const Ring &ring, Element *v, const Element &position, const Element x[], const Element w[], unsigned int n) { assert(n > 0); std::vector a(2*n-1); unsigned int i; for (i=0; i1; i--) a[i-1] = ring.Multiply(a[2*i], a[2*i-1]); a[0] = ring.MultiplicativeIdentity(); for (i=0; i Element BulkPolynomialInterpolateAt(const Ring &ring, const Element y[], const Element v[], unsigned int n) { Element result = ring.Identity(); for (unsigned int i=0; i const PolynomialOverFixedRing &PolynomialOverFixedRing::Zero() { return Singleton().Ref(); } template const PolynomialOverFixedRing &PolynomialOverFixedRing::One() { return Singleton().Ref(); } NAMESPACE_END