6#ifndef CRYPTOPP_POLYNOMI_H
7#define CRYPTOPP_POLYNOMI_H
37 RandomizationParameter(
unsigned int coefficientCount,
const typename T::RandomizationParameter &coefficientParameter )
38 : m_coefficientCount(coefficientCount), m_coefficientParameter(coefficientParameter) {}
41 unsigned int m_coefficientCount;
42 typename T::RandomizationParameter m_coefficientParameter;
47 typedef typename T::Element CoefficientType;
57 : m_coefficients((size_t)count, ring.
Identity()) {}
61 : m_coefficients(t.m_coefficients.size()) {*
this = t;}
65 : m_coefficients(1, element) {}
69 : m_coefficients(begin, end) {}
85 {Randomize(rng, parameter, ring);}
91 int Degree(
const Ring &ring)
const {
return int(CoefficientCount(ring))-1;}
93 unsigned int CoefficientCount(
const Ring &ring)
const;
95 CoefficientType
GetCoefficient(
unsigned int i,
const Ring &ring)
const;
104 void Randomize(
RandomNumberGenerator &rng,
const RandomizationParameter ¶meter,
const Ring &ring);
107 void SetCoefficient(
unsigned int i,
const CoefficientType &value,
const Ring &ring);
110 void Negate(
const Ring &ring);
120 bool IsZero(
const Ring &ring)
const {
return CoefficientCount(ring)==0;}
130 bool IsUnit(
const Ring &ring)
const;
140 CoefficientType EvaluateAt(
const CoefficientType &x,
const Ring &ring)
const;
151 std::istream& Input(std::istream &in,
const Ring &ring);
152 std::ostream& Output(std::ostream &out,
const Ring &ring)
const;
156 void FromStr(
const char *str,
const Ring &ring);
158 std::vector<CoefficientType> m_coefficients;
170 typedef typename T::Element CoefficientType;
206 static const ThisType &Zero();
207 static const ThisType &One();
225 ThisType& operator=(
const ThisType& t) {B::operator=(t);
return *
this;}
227 ThisType& operator+=(
const ThisType& t) {Accumulate(t, ms_fixedRing);
return *
this;}
229 ThisType& operator-=(
const ThisType& t) {Reduce(t, ms_fixedRing);
return *
this;}
231 ThisType& operator*=(
const ThisType& t) {
return *
this = *
this*t;}
233 ThisType& operator/=(
const ThisType& t) {
return *
this = *
this/t;}
235 ThisType& operator%=(
const ThisType& t) {
return *
this = *
this%t;}
238 ThisType& operator<<=(
unsigned int n) {ShiftLeft(n, ms_fixedRing);
return *
this;}
240 ThisType& operator>>=(
unsigned int n) {ShiftRight(n, ms_fixedRing);
return *
this;}
246 void Randomize(
RandomNumberGenerator &rng,
const RandomizationParameter ¶meter) {B::Randomize(rng, parameter, ms_fixedRing);}
249 void Negate() {B::Negate(ms_fixedRing);}
251 void swap(ThisType &t) {B::swap(t);}
259 ThisType operator+()
const {
return *
this;}
261 ThisType operator-()
const {
return ThisType(Inverse(ms_fixedRing));}
267 friend ThisType operator>>(ThisType a,
unsigned int n) {
return ThisType(a>>=n);}
269 friend ThisType operator<<(ThisType a,
unsigned int n) {
return ThisType(a<<=n);}
275 ThisType MultiplicativeInverse()
const {
return ThisType(B::MultiplicativeInverse(ms_fixedRing));}
277 bool IsUnit()
const {
return B::IsUnit(ms_fixedRing);}
280 ThisType Doubled()
const {
return ThisType(B::Doubled(ms_fixedRing));}
282 ThisType Squared()
const {
return ThisType(B::Squared(ms_fixedRing));}
284 CoefficientType EvaluateAt(
const CoefficientType &x)
const {
return B::EvaluateAt(x, ms_fixedRing);}
294 friend std::istream& operator>>(std::istream& in, ThisType &a)
295 {
return a.Input(in, ms_fixedRing);}
297 friend std::ostream& operator<<(std::ostream& out,
const ThisType &a)
298 {
return a.Output(out, ms_fixedRing);}
302 struct NewOnePolynomial
304 ThisType * operator()()
const
306 return new ThisType(ms_fixedRing.MultiplicativeIdentity());
310 static const Ring ms_fixedRing;
317 typedef T CoefficientRing;
319 typedef typename Element::CoefficientType CoefficientType;
325 {
return Element(rng, parameter, m_ring);}
328 {
return a.Equals(b, m_ring);}
331 {
return this->result = m_ring.Identity();}
334 {
return this->result = a.Plus(b, m_ring);}
337 {a.Accumulate(b, m_ring);
return a;}
340 {
return this->result = a.Inverse(m_ring);}
343 {
return this->result = a.Minus(b, m_ring);}
346 {
return a.Reduce(b, m_ring);}
349 {
return this->result = a.Doubled(m_ring);}
352 {
return this->result = m_ring.MultiplicativeIdentity();}
355 {
return this->result = a.Times(b, m_ring);}
358 {
return this->result = a.Squared(m_ring);}
361 {
return a.IsUnit(m_ring);}
364 {
return this->result = a.MultiplicativeInverse(m_ring);}
367 {
return this->result = a.DividedBy(b, m_ring);}
370 {
return this->result = a.Modulo(b, m_ring);}
381 Element Interpolate(
const CoefficientType x[],
const CoefficientType y[],
unsigned int n)
const;
384 CoefficientType InterpolateAt(
const CoefficientType &position,
const CoefficientType x[],
const CoefficientType y[],
unsigned int n)
const;
391 void CalculateAlpha(std::vector<CoefficientType> &alpha,
const CoefficientType x[],
const CoefficientType y[],
unsigned int n)
const;
393 CoefficientRing m_ring;
396template <
class Ring,
class Element>
397void PrepareBulkPolynomialInterpolation(
const Ring &ring, Element *w,
const Element x[],
unsigned int n);
398template <
class Ring,
class Element>
399void PrepareBulkPolynomialInterpolationAt(
const Ring &ring, Element *v,
const Element &position,
const Element x[],
const Element w[],
unsigned int n);
400template <
class Ring,
class Element>
401Element BulkPolynomialInterpolateAt(
const Ring &ring,
const Element y[],
const Element v[],
unsigned int n);
404template <
class T,
int instance>
405inline bool operator==(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
406 {
return a.Equals(b, a.ms_fixedRing);}
408template <
class T,
int instance>
409inline bool operator!=(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
413template <
class T,
int instance>
414inline bool operator> (
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
415 {
return a.Degree() > b.Degree();}
417template <
class T,
int instance>
418inline bool operator>=(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
419 {
return a.Degree() >= b.Degree();}
421template <
class T,
int instance>
422inline bool operator< (
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
423 {
return a.Degree() < b.Degree();}
425template <
class T,
int instance>
426inline bool operator<=(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
427 {
return a.Degree() <= b.Degree();}
430template <
class T,
int instance>
431inline CryptoPP::PolynomialOverFixedRing<T, instance>
operator+(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
432 {
return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Plus(b, a.ms_fixedRing));}
434template <
class T,
int instance>
435inline CryptoPP::PolynomialOverFixedRing<T, instance>
operator-(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
436 {
return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Minus(b, a.ms_fixedRing));}
438template <
class T,
int instance>
439inline CryptoPP::PolynomialOverFixedRing<T, instance>
operator*(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
440 {
return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Times(b, a.ms_fixedRing));}
442template <
class T,
int instance>
443inline CryptoPP::PolynomialOverFixedRing<T, instance> operator/(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
444 {
return CryptoPP::PolynomialOverFixedRing<T, instance>(a.DividedBy(b, a.ms_fixedRing));}
446template <
class T,
int instance>
447inline CryptoPP::PolynomialOverFixedRing<T, instance> operator%(
const CryptoPP::PolynomialOverFixedRing<T, instance> &a,
const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
448 {
return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Modulo(b, a.ms_fixedRing));}
453template<
class T>
inline void swap(CryptoPP::PolynomialOver<T> &a, CryptoPP::PolynomialOver<T> &b)
457template<
class T,
int i>
inline void swap(CryptoPP::PolynomialOverFixedRing<T,i> &a, CryptoPP::PolynomialOverFixedRing<T,i> &b)
Classes for performing mathematics over different fields.
bool operator<=(const OID &lhs, const OID &rhs)
Compare two OIDs for ordering.
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 ordering.
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.
Exception(ErrorType errorType, const std::string &s)
Construct a new Exception.
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
division by zero exception
specify the distribution for randomization functions
Polynomials over a fixed ring.
PolynomialOverFixedRing(const byte *encodedPoly, unsigned int byteCount)
convert from big-endian byte array
PolynomialOverFixedRing(RandomNumberGenerator &rng, const RandomizationParameter ¶meter)
create a random PolynomialOverFixedRing
unsigned int CoefficientCount() const
degree + 1
CoefficientType operator[](unsigned int i) const
return coefficient for x^i
CoefficientType GetCoefficient(unsigned int i) const
return coefficient for x^i
void SetCoefficient(unsigned int i, const CoefficientType &value)
set the coefficient for x^i to value
PolynomialOverFixedRing(unsigned int count=0)
creates the zero polynomial
PolynomialOverFixedRing(BufferedTransformation &bt)
convert from BER encoded byte array stored in a BufferedTransformation object
static void Divide(ThisType &r, ThisType &q, const ThisType &a, const ThisType &d)
calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
PolynomialOverFixedRing(const ThisType &t)
copy constructor
PolynomialOverFixedRing(const CoefficientType &element)
construct constant polynomial
int Degree() const
the zero polynomial will return a degree of -1
PolynomialOverFixedRing(const char *str)
convert from string
PolynomialOverFixedRing(const byte *BEREncodedPoly)
convert from Basic Encoding Rules encoded byte array
PolynomialOverFixedRing(Iterator first, Iterator last)
construct polynomial with specified coefficients, starting from coefficient of x^0
represents single-variable polynomials over arbitrary rings
PolynomialOver(RandomNumberGenerator &rng, const RandomizationParameter ¶meter, const Ring &ring)
create a random PolynomialOver<T>
PolynomialOver(const PolynomialOver< Ring > &t)
copy constructor
PolynomialOver(BufferedTransformation &bt)
convert from BER encoded byte array stored in a BufferedTransformation object
int Degree(const Ring &ring) const
the zero polynomial will return a degree of -1
CoefficientType GetCoefficient(unsigned int i, const Ring &ring) const
return coefficient for x^i
PolynomialOver(Iterator begin, Iterator end)
construct polynomial with specified coefficients, starting from coefficient of x^0
PolynomialOver()
creates the zero polynomial
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
PolynomialOver(const byte *encodedPolynomialOver, unsigned int byteCount)
convert from big-endian byte array
PolynomialOver(const byte *BEREncodedPolynomialOver)
convert from Basic Encoding Rules encoded byte array
PolynomialOver(const char *str, const Ring &ring)
convert from string
PolynomialOver(const CoefficientType &element)
construct constant polynomial
Interface for random number generators.
Ring of polynomials over another ring.
const Element & Divide(const Element &a, const Element &b) const
Divides elements in the group.
void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const
Performs the division algorithm on two elements in the ring.
Element & Accumulate(Element &a, const Element &b) const
TODO.
const Element & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
const Element & Add(const Element &a, const Element &b) const
Adds elements in the group.
const Element & Identity() const
Provides the Identity element.
const Element & Inverse(const Element &a) const
Inverts the element in the group.
bool IsUnit(const Element &a) const
Determines whether an element is a unit in the group.
bool Equal(const Element &a, const Element &b) const
Compare two elements for equality.
const Element & Double(const Element &a) const
Doubles an element in the group.
const Element & Square(const Element &a) const
Square an element in the group.
const Element & Multiply(const Element &a, const Element &b) const
Multiplies elements in the group.
Element & Reduce(Element &a, const Element &b) const
Reduces an element in the congruence class.
const Element & Mod(const Element &a, const Element &b) const
Performs a modular reduction in the ring.
const Element & MultiplicativeInverse(const Element &a) const
Calculate the multiplicative inverse of an element in the group.
const Element & Subtract(const Element &a, const Element &b) const
Subtracts elements in the group.
Abstract base classes that provide a uniform interface to this library.
inline ::Integer operator-(const ::Integer &a, const ::Integer &b)
Subtraction.
inline ::Integer operator*(const ::Integer &a, const ::Integer &b)
Multiplication.
Utility functions for the Crypto++ library.
Crypto++ library namespace.
const char * Identity()
ConstByteArrayParameter.
Classes and functions for secure memory allocations.
void swap(::SecBlock< T, A > &a, ::SecBlock< T, A > &b)
Swap two SecBlocks.