2017-01-27 12:05:45 +00:00
|
|
|
// polynomi.h - originally written and placed in the public domain by Wei Dai
|
2015-11-18 20:32:28 +00:00
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \file
|
|
|
|
/// \headerfile polynomi.h
|
|
|
|
/// \brief Classes for polynomial basis and operations
|
2015-11-18 20:32:28 +00:00
|
|
|
|
|
|
|
|
2015-11-05 06:59:46 +00:00
|
|
|
#ifndef CRYPTOPP_POLYNOMI_H
|
|
|
|
#define CRYPTOPP_POLYNOMI_H
|
|
|
|
|
|
|
|
/*! \file */
|
|
|
|
|
|
|
|
#include "cryptlib.h"
|
|
|
|
#include "secblock.h"
|
|
|
|
#include "algebra.h"
|
|
|
|
#include "misc.h"
|
|
|
|
|
|
|
|
#include <iosfwd>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(CryptoPP)
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// represents single-variable polynomials over arbitrary rings
|
2015-11-05 06:59:46 +00:00
|
|
|
/*! \nosubgrouping */
|
|
|
|
template <class T> class PolynomialOver
|
|
|
|
{
|
|
|
|
public:
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name ENUMS, EXCEPTIONS, and TYPEDEFS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
/// division by zero exception
|
2016-09-10 08:57:48 +00:00
|
|
|
class DivideByZero : public Exception
|
2015-11-05 06:59:46 +00:00
|
|
|
{
|
2016-09-10 08:57:48 +00:00
|
|
|
public:
|
2015-11-05 06:59:46 +00:00
|
|
|
DivideByZero() : Exception(OTHER_ERROR, "PolynomialOver<T>: division by zero") {}
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// specify the distribution for randomization functions
|
2015-11-05 06:59:46 +00:00
|
|
|
class RandomizationParameter
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
RandomizationParameter(unsigned int coefficientCount, const typename T::RandomizationParameter &coefficientParameter )
|
|
|
|
: m_coefficientCount(coefficientCount), m_coefficientParameter(coefficientParameter) {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
unsigned int m_coefficientCount;
|
|
|
|
typename T::RandomizationParameter m_coefficientParameter;
|
|
|
|
friend class PolynomialOver<T>;
|
|
|
|
};
|
|
|
|
|
|
|
|
typedef T Ring;
|
|
|
|
typedef typename T::Element CoefficientType;
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name CREATORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
/// creates the zero polynomial
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver() {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver(const Ring &ring, unsigned int count)
|
|
|
|
: m_coefficients((size_t)count, ring.Identity()) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// copy constructor
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver(const PolynomialOver<Ring> &t)
|
|
|
|
: m_coefficients(t.m_coefficients.size()) {*this = t;}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// construct constant polynomial
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver(const CoefficientType &element)
|
|
|
|
: m_coefficients(1, element) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// construct polynomial with specified coefficients, starting from coefficient of x^0
|
2015-11-05 06:59:46 +00:00
|
|
|
template <typename Iterator> PolynomialOver(Iterator begin, Iterator end)
|
|
|
|
: m_coefficients(begin, end) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from string
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver(const char *str, const Ring &ring) {FromStr(str, ring);}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from big-endian byte array
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver(const byte *encodedPolynomialOver, unsigned int byteCount);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from Basic Encoding Rules encoded byte array
|
2015-11-05 06:59:46 +00:00
|
|
|
explicit PolynomialOver(const byte *BEREncodedPolynomialOver);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from BER encoded byte array stored in a BufferedTransformation object
|
2015-11-05 06:59:46 +00:00
|
|
|
explicit PolynomialOver(BufferedTransformation &bt);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// create a random PolynomialOver<T>
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver(RandomNumberGenerator &rng, const RandomizationParameter ¶meter, const Ring &ring)
|
|
|
|
{Randomize(rng, parameter, ring);}
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name ACCESSORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
/// the zero polynomial will return a degree of -1
|
2015-11-05 06:59:46 +00:00
|
|
|
int Degree(const Ring &ring) const {return int(CoefficientCount(ring))-1;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
unsigned int CoefficientCount(const Ring &ring) const;
|
2017-11-29 15:54:33 +00:00
|
|
|
/// return coefficient for x^i
|
2015-11-05 06:59:46 +00:00
|
|
|
CoefficientType GetCoefficient(unsigned int i, const Ring &ring) const;
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name MANIPULATORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver<Ring>& operator=(const PolynomialOver<Ring>& t);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
void Randomize(RandomNumberGenerator &rng, const RandomizationParameter ¶meter, const Ring &ring);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// set the coefficient for x^i to value
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
void Negate(const Ring &ring);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
void swap(PolynomialOver<Ring> &t);
|
|
|
|
//@}
|
|
|
|
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name BASIC ARITHMETIC ON POLYNOMIALS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
|
|
|
bool Equals(const PolynomialOver<Ring> &t, const Ring &ring) const;
|
|
|
|
bool IsZero(const Ring &ring) const {return CoefficientCount(ring)==0;}
|
|
|
|
|
|
|
|
PolynomialOver<Ring> Plus(const PolynomialOver<Ring>& t, const Ring &ring) const;
|
|
|
|
PolynomialOver<Ring> Minus(const PolynomialOver<Ring>& t, const Ring &ring) const;
|
|
|
|
PolynomialOver<Ring> Inverse(const Ring &ring) const;
|
|
|
|
|
|
|
|
PolynomialOver<Ring> Times(const PolynomialOver<Ring>& t, const Ring &ring) const;
|
|
|
|
PolynomialOver<Ring> DividedBy(const PolynomialOver<Ring>& t, const Ring &ring) const;
|
|
|
|
PolynomialOver<Ring> Modulo(const PolynomialOver<Ring>& t, const Ring &ring) const;
|
|
|
|
PolynomialOver<Ring> MultiplicativeInverse(const Ring &ring) const;
|
|
|
|
bool IsUnit(const Ring &ring) const;
|
|
|
|
|
|
|
|
PolynomialOver<Ring>& Accumulate(const PolynomialOver<Ring>& t, const Ring &ring);
|
|
|
|
PolynomialOver<Ring>& Reduce(const PolynomialOver<Ring>& t, const Ring &ring);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver<Ring> Doubled(const Ring &ring) const {return Plus(*this, ring);}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOver<Ring> Squared(const Ring &ring) const {return Times(*this, ring);}
|
|
|
|
|
|
|
|
CoefficientType EvaluateAt(const CoefficientType &x, const Ring &ring) const;
|
|
|
|
|
|
|
|
PolynomialOver<Ring>& ShiftLeft(unsigned int n, const Ring &ring);
|
|
|
|
PolynomialOver<Ring>& ShiftRight(unsigned int n, const Ring &ring);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// calculate r and q such that (a == d*q + r) && (0 <= degree of r < degree of d)
|
2015-11-05 06:59:46 +00:00
|
|
|
static void Divide(PolynomialOver<Ring> &r, PolynomialOver<Ring> &q, const PolynomialOver<Ring> &a, const PolynomialOver<Ring> &d, const Ring &ring);
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name INPUT/OUTPUT
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
|
|
|
std::istream& Input(std::istream &in, const Ring &ring);
|
|
|
|
std::ostream& Output(std::ostream &out, const Ring &ring) const;
|
|
|
|
//@}
|
|
|
|
|
|
|
|
private:
|
|
|
|
void FromStr(const char *str, const Ring &ring);
|
|
|
|
|
|
|
|
std::vector<CoefficientType> m_coefficients;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// Polynomials over a fixed ring
|
2015-11-05 06:59:46 +00:00
|
|
|
/*! Having a fixed ring allows overloaded operators */
|
|
|
|
template <class T, int instance> class PolynomialOverFixedRing : private PolynomialOver<T>
|
|
|
|
{
|
|
|
|
typedef PolynomialOver<T> B;
|
|
|
|
typedef PolynomialOverFixedRing<T, instance> ThisType;
|
|
|
|
|
|
|
|
public:
|
|
|
|
typedef T Ring;
|
|
|
|
typedef typename T::Element CoefficientType;
|
|
|
|
typedef typename B::DivideByZero DivideByZero;
|
|
|
|
typedef typename B::RandomizationParameter RandomizationParameter;
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name CREATORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
/// creates the zero polynomial
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOverFixedRing(unsigned int count = 0) : B(ms_fixedRing, count) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// copy constructor
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOverFixedRing(const ThisType &t) : B(t) {}
|
|
|
|
|
|
|
|
explicit PolynomialOverFixedRing(const B &t) : B(t) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// construct constant polynomial
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOverFixedRing(const CoefficientType &element) : B(element) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// construct polynomial with specified coefficients, starting from coefficient of x^0
|
2015-11-05 06:59:46 +00:00
|
|
|
template <typename Iterator> PolynomialOverFixedRing(Iterator first, Iterator last)
|
|
|
|
: B(first, last) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from string
|
2015-11-05 06:59:46 +00:00
|
|
|
explicit PolynomialOverFixedRing(const char *str) : B(str, ms_fixedRing) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from big-endian byte array
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOverFixedRing(const byte *encodedPoly, unsigned int byteCount) : B(encodedPoly, byteCount) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from Basic Encoding Rules encoded byte array
|
2015-11-05 06:59:46 +00:00
|
|
|
explicit PolynomialOverFixedRing(const byte *BEREncodedPoly) : B(BEREncodedPoly) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// convert from BER encoded byte array stored in a BufferedTransformation object
|
2015-11-05 06:59:46 +00:00
|
|
|
explicit PolynomialOverFixedRing(BufferedTransformation &bt) : B(bt) {}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// create a random PolynomialOverFixedRing
|
2015-11-05 06:59:46 +00:00
|
|
|
PolynomialOverFixedRing(RandomNumberGenerator &rng, const RandomizationParameter ¶meter) : B(rng, parameter, ms_fixedRing) {}
|
|
|
|
|
|
|
|
static const ThisType &Zero();
|
|
|
|
static const ThisType &One();
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name ACCESSORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
/// the zero polynomial will return a degree of -1
|
2015-11-05 06:59:46 +00:00
|
|
|
int Degree() const {return B::Degree(ms_fixedRing);}
|
2017-11-29 15:54:33 +00:00
|
|
|
/// degree + 1
|
2015-11-05 06:59:46 +00:00
|
|
|
unsigned int CoefficientCount() const {return B::CoefficientCount(ms_fixedRing);}
|
2017-11-29 15:54:33 +00:00
|
|
|
/// return coefficient for x^i
|
2015-11-05 06:59:46 +00:00
|
|
|
CoefficientType GetCoefficient(unsigned int i) const {return B::GetCoefficient(i, ms_fixedRing);}
|
2017-11-29 15:54:33 +00:00
|
|
|
/// return coefficient for x^i
|
2015-11-05 06:59:46 +00:00
|
|
|
CoefficientType operator[](unsigned int i) const {return B::GetCoefficient(i, ms_fixedRing);}
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name MANIPULATORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator=(const ThisType& t) {B::operator=(t); return *this;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator+=(const ThisType& t) {Accumulate(t, ms_fixedRing); return *this;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator-=(const ThisType& t) {Reduce(t, ms_fixedRing); return *this;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator*=(const ThisType& t) {return *this = *this*t;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator/=(const ThisType& t) {return *this = *this/t;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator%=(const ThisType& t) {return *this = *this%t;}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator<<=(unsigned int n) {ShiftLeft(n, ms_fixedRing); return *this;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType& operator>>=(unsigned int n) {ShiftRight(n, ms_fixedRing); return *this;}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// set the coefficient for x^i to value
|
2015-11-05 06:59:46 +00:00
|
|
|
void SetCoefficient(unsigned int i, const CoefficientType &value) {B::SetCoefficient(i, value, ms_fixedRing);}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
void Randomize(RandomNumberGenerator &rng, const RandomizationParameter ¶meter) {B::Randomize(rng, parameter, ms_fixedRing);}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
void Negate() {B::Negate(ms_fixedRing);}
|
|
|
|
|
|
|
|
void swap(ThisType &t) {B::swap(t);}
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name UNARY OPERATORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
bool operator!() const {return CoefficientCount()==0;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType operator+() const {return *this;}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType operator-() const {return ThisType(Inverse(ms_fixedRing));}
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name BINARY OPERATORS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
friend ThisType operator>>(ThisType a, unsigned int n) {return ThisType(a>>=n);}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
friend ThisType operator<<(ThisType a, unsigned int n) {return ThisType(a<<=n);}
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name OTHER ARITHMETIC FUNCTIONS
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType MultiplicativeInverse() const {return ThisType(B::MultiplicativeInverse(ms_fixedRing));}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
bool IsUnit() const {return B::IsUnit(ms_fixedRing);}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType Doubled() const {return ThisType(B::Doubled(ms_fixedRing));}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
ThisType Squared() const {return ThisType(B::Squared(ms_fixedRing));}
|
|
|
|
|
|
|
|
CoefficientType EvaluateAt(const CoefficientType &x) const {return B::EvaluateAt(x, ms_fixedRing);}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// calculate r and q such that (a == d*q + r) && (0 <= r < abs(d))
|
2015-11-05 06:59:46 +00:00
|
|
|
static void Divide(ThisType &r, ThisType &q, const ThisType &a, const ThisType &d)
|
|
|
|
{B::Divide(r, q, a, d, ms_fixedRing);}
|
|
|
|
//@}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// \name INPUT/OUTPUT
|
2015-11-05 06:59:46 +00:00
|
|
|
//@{
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
friend std::istream& operator>>(std::istream& in, ThisType &a)
|
|
|
|
{return a.Input(in, ms_fixedRing);}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
friend std::ostream& operator<<(std::ostream& out, const ThisType &a)
|
|
|
|
{return a.Output(out, ms_fixedRing);}
|
|
|
|
//@}
|
|
|
|
|
|
|
|
private:
|
|
|
|
struct NewOnePolynomial
|
|
|
|
{
|
|
|
|
ThisType * operator()() const
|
|
|
|
{
|
|
|
|
return new ThisType(ms_fixedRing.MultiplicativeIdentity());
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
static const Ring ms_fixedRing;
|
|
|
|
};
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
/// Ring of polynomials over another ring
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T> class RingOfPolynomialsOver : public AbstractEuclideanDomain<PolynomialOver<T> >
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
typedef T CoefficientRing;
|
|
|
|
typedef PolynomialOver<T> Element;
|
|
|
|
typedef typename Element::CoefficientType CoefficientType;
|
|
|
|
typedef typename Element::RandomizationParameter RandomizationParameter;
|
|
|
|
|
|
|
|
RingOfPolynomialsOver(const CoefficientRing &ring) : m_ring(ring) {}
|
|
|
|
|
|
|
|
Element RandomElement(RandomNumberGenerator &rng, const RandomizationParameter ¶meter)
|
|
|
|
{return Element(rng, parameter, m_ring);}
|
|
|
|
|
|
|
|
bool Equal(const Element &a, const Element &b) const
|
|
|
|
{return a.Equals(b, m_ring);}
|
|
|
|
|
|
|
|
const Element& Identity() const
|
|
|
|
{return this->result = m_ring.Identity();}
|
|
|
|
|
|
|
|
const Element& Add(const Element &a, const Element &b) const
|
|
|
|
{return this->result = a.Plus(b, m_ring);}
|
|
|
|
|
|
|
|
Element& Accumulate(Element &a, const Element &b) const
|
|
|
|
{a.Accumulate(b, m_ring); return a;}
|
|
|
|
|
|
|
|
const Element& Inverse(const Element &a) const
|
|
|
|
{return this->result = a.Inverse(m_ring);}
|
|
|
|
|
|
|
|
const Element& Subtract(const Element &a, const Element &b) const
|
|
|
|
{return this->result = a.Minus(b, m_ring);}
|
|
|
|
|
|
|
|
Element& Reduce(Element &a, const Element &b) const
|
|
|
|
{return a.Reduce(b, m_ring);}
|
|
|
|
|
|
|
|
const Element& Double(const Element &a) const
|
|
|
|
{return this->result = a.Doubled(m_ring);}
|
|
|
|
|
|
|
|
const Element& MultiplicativeIdentity() const
|
|
|
|
{return this->result = m_ring.MultiplicativeIdentity();}
|
|
|
|
|
|
|
|
const Element& Multiply(const Element &a, const Element &b) const
|
|
|
|
{return this->result = a.Times(b, m_ring);}
|
|
|
|
|
|
|
|
const Element& Square(const Element &a) const
|
|
|
|
{return this->result = a.Squared(m_ring);}
|
|
|
|
|
|
|
|
bool IsUnit(const Element &a) const
|
|
|
|
{return a.IsUnit(m_ring);}
|
|
|
|
|
|
|
|
const Element& MultiplicativeInverse(const Element &a) const
|
|
|
|
{return this->result = a.MultiplicativeInverse(m_ring);}
|
|
|
|
|
|
|
|
const Element& Divide(const Element &a, const Element &b) const
|
|
|
|
{return this->result = a.DividedBy(b, m_ring);}
|
|
|
|
|
|
|
|
const Element& Mod(const Element &a, const Element &b) const
|
|
|
|
{return this->result = a.Modulo(b, m_ring);}
|
|
|
|
|
|
|
|
void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const
|
|
|
|
{Element::Divide(r, q, a, d, m_ring);}
|
|
|
|
|
|
|
|
class InterpolationFailed : public Exception
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
InterpolationFailed() : Exception(OTHER_ERROR, "RingOfPolynomialsOver<T>: interpolation failed") {}
|
|
|
|
};
|
|
|
|
|
|
|
|
Element Interpolate(const CoefficientType x[], const CoefficientType y[], unsigned int n) const;
|
|
|
|
|
|
|
|
// a faster version of Interpolate(x, y, n).EvaluateAt(position)
|
|
|
|
CoefficientType InterpolateAt(const CoefficientType &position, const CoefficientType x[], const CoefficientType y[], unsigned int n) const;
|
|
|
|
/*
|
|
|
|
void PrepareBulkInterpolation(CoefficientType *w, const CoefficientType x[], unsigned int n) const;
|
|
|
|
void PrepareBulkInterpolationAt(CoefficientType *v, const CoefficientType &position, const CoefficientType x[], const CoefficientType w[], unsigned int n) const;
|
|
|
|
CoefficientType BulkInterpolateAt(const CoefficientType y[], const CoefficientType v[], unsigned int n) const;
|
|
|
|
*/
|
|
|
|
protected:
|
|
|
|
void CalculateAlpha(std::vector<CoefficientType> &alpha, const CoefficientType x[], const CoefficientType y[], unsigned int n) const;
|
|
|
|
|
|
|
|
CoefficientRing m_ring;
|
|
|
|
};
|
|
|
|
|
|
|
|
template <class Ring, class Element>
|
|
|
|
void PrepareBulkPolynomialInterpolation(const Ring &ring, Element *w, const Element x[], unsigned int n);
|
|
|
|
template <class Ring, class Element>
|
|
|
|
void PrepareBulkPolynomialInterpolationAt(const Ring &ring, Element *v, const Element &position, const Element x[], const Element w[], unsigned int n);
|
|
|
|
template <class Ring, class Element>
|
|
|
|
Element BulkPolynomialInterpolateAt(const Ring &ring, const Element y[], const Element v[], unsigned int n);
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline bool operator==(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return a.Equals(b, a.ms_fixedRing);}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline bool operator!=(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return !(a==b);}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline bool operator> (const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return a.Degree() > b.Degree();}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline bool operator>=(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return a.Degree() >= b.Degree();}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline bool operator< (const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return a.Degree() < b.Degree();}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline bool operator<=(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return a.Degree() <= b.Degree();}
|
|
|
|
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline CryptoPP::PolynomialOverFixedRing<T, instance> operator+(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Plus(b, a.ms_fixedRing));}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline CryptoPP::PolynomialOverFixedRing<T, instance> operator-(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Minus(b, a.ms_fixedRing));}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline CryptoPP::PolynomialOverFixedRing<T, instance> operator*(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Times(b, a.ms_fixedRing));}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline CryptoPP::PolynomialOverFixedRing<T, instance> operator/(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return CryptoPP::PolynomialOverFixedRing<T, instance>(a.DividedBy(b, a.ms_fixedRing));}
|
2017-11-29 15:54:33 +00:00
|
|
|
///
|
2015-11-05 06:59:46 +00:00
|
|
|
template <class T, int instance>
|
|
|
|
inline CryptoPP::PolynomialOverFixedRing<T, instance> operator%(const CryptoPP::PolynomialOverFixedRing<T, instance> &a, const CryptoPP::PolynomialOverFixedRing<T, instance> &b)
|
|
|
|
{return CryptoPP::PolynomialOverFixedRing<T, instance>(a.Modulo(b, a.ms_fixedRing));}
|
|
|
|
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
NAMESPACE_BEGIN(std)
|
|
|
|
template<class T> inline void swap(CryptoPP::PolynomialOver<T> &a, CryptoPP::PolynomialOver<T> &b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
template<class T, int i> inline void swap(CryptoPP::PolynomialOverFixedRing<T,i> &a, CryptoPP::PolynomialOverFixedRing<T,i> &b)
|
|
|
|
{
|
|
|
|
a.swap(b);
|
|
|
|
}
|
|
|
|
NAMESPACE_END
|
|
|
|
|
|
|
|
#endif
|