Security Scol plugin
polynomi.h
Go to the documentation of this file.
1// polynomi.h - originally written and placed in the public domain by Wei Dai
2
5
6#ifndef CRYPTOPP_POLYNOMI_H
7#define CRYPTOPP_POLYNOMI_H
8
9#include "cryptlib.h"
10#include "secblock.h"
11#include "algebra.h"
12#include "misc.h"
13
14#include <iosfwd>
15#include <vector>
16
17NAMESPACE_BEGIN(CryptoPP)
18
19
21template <class T> class PolynomialOver
22{
23public:
25
26
27 class DivideByZero : public Exception
28 {
29 public:
30 DivideByZero() : Exception(OTHER_ERROR, "PolynomialOver<T>: division by zero") {}
31 };
32
35 {
36 public:
37 RandomizationParameter(unsigned int coefficientCount, const typename T::RandomizationParameter &coefficientParameter )
38 : m_coefficientCount(coefficientCount), m_coefficientParameter(coefficientParameter) {}
39
40 private:
41 unsigned int m_coefficientCount;
42 typename T::RandomizationParameter m_coefficientParameter;
43 friend class PolynomialOver<T>;
44 };
45
46 typedef T Ring;
47 typedef typename T::Element CoefficientType;
49
51
52
54
56 PolynomialOver(const Ring &ring, unsigned int count)
57 : m_coefficients((size_t)count, ring.Identity()) {}
58
61 : m_coefficients(t.m_coefficients.size()) {*this = t;}
62
64 PolynomialOver(const CoefficientType &element)
65 : m_coefficients(1, element) {}
66
68 template <typename Iterator> PolynomialOver(Iterator begin, Iterator end)
69 : m_coefficients(begin, end) {}
70
72 PolynomialOver(const char *str, const Ring &ring) {FromStr(str, ring);}
73
75 PolynomialOver(const byte *encodedPolynomialOver, unsigned int byteCount);
76
78 explicit PolynomialOver(const byte *BEREncodedPolynomialOver);
79
82
84 PolynomialOver(RandomNumberGenerator &rng, const RandomizationParameter &parameter, const Ring &ring)
85 {Randomize(rng, parameter, ring);}
87
89
90
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;
97
99
100
101 PolynomialOver<Ring>& operator=(const PolynomialOver<Ring>& t);
102
104 void Randomize(RandomNumberGenerator &rng, const RandomizationParameter &parameter, const Ring &ring);
105
107 void SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring);
108
110 void Negate(const Ring &ring);
111
113 void swap(PolynomialOver<Ring> &t);
115
116
118
119 bool Equals(const PolynomialOver<Ring> &t, const Ring &ring) const;
120 bool IsZero(const Ring &ring) const {return CoefficientCount(ring)==0;}
121
122 PolynomialOver<Ring> Plus(const PolynomialOver<Ring>& t, const Ring &ring) const;
123 PolynomialOver<Ring> Minus(const PolynomialOver<Ring>& t, const Ring &ring) const;
124 PolynomialOver<Ring> Inverse(const Ring &ring) const;
125
126 PolynomialOver<Ring> Times(const PolynomialOver<Ring>& t, const Ring &ring) const;
127 PolynomialOver<Ring> DividedBy(const PolynomialOver<Ring>& t, const Ring &ring) const;
128 PolynomialOver<Ring> Modulo(const PolynomialOver<Ring>& t, const Ring &ring) const;
129 PolynomialOver<Ring> MultiplicativeInverse(const Ring &ring) const;
130 bool IsUnit(const Ring &ring) const;
131
132 PolynomialOver<Ring>& Accumulate(const PolynomialOver<Ring>& t, const Ring &ring);
133 PolynomialOver<Ring>& Reduce(const PolynomialOver<Ring>& t, const Ring &ring);
134
136 PolynomialOver<Ring> Doubled(const Ring &ring) const {return Plus(*this, ring);}
138 PolynomialOver<Ring> Squared(const Ring &ring) const {return Times(*this, ring);}
139
140 CoefficientType EvaluateAt(const CoefficientType &x, const Ring &ring) const;
141
142 PolynomialOver<Ring>& ShiftLeft(unsigned int n, const Ring &ring);
143 PolynomialOver<Ring>& ShiftRight(unsigned int n, const Ring &ring);
144
146 static void Divide(PolynomialOver<Ring> &r, PolynomialOver<Ring> &q, const PolynomialOver<Ring> &a, const PolynomialOver<Ring> &d, const Ring &ring);
148
150
151 std::istream& Input(std::istream &in, const Ring &ring);
152 std::ostream& Output(std::ostream &out, const Ring &ring) const;
154
155private:
156 void FromStr(const char *str, const Ring &ring);
157
158 std::vector<CoefficientType> m_coefficients;
159};
160
162
163template <class T, int instance> class PolynomialOverFixedRing : private PolynomialOver<T>
164{
165 typedef PolynomialOver<T> B;
167
168public:
169 typedef T Ring;
170 typedef typename T::Element CoefficientType;
171 typedef typename B::DivideByZero DivideByZero;
173
175
176
177 PolynomialOverFixedRing(unsigned int count = 0) : B(ms_fixedRing, count) {}
178
181
182 explicit PolynomialOverFixedRing(const B &t) : B(t) {}
183
185 PolynomialOverFixedRing(const CoefficientType &element) : B(element) {}
186
188 template <typename Iterator> PolynomialOverFixedRing(Iterator first, Iterator last)
189 : B(first, last) {}
190
192 explicit PolynomialOverFixedRing(const char *str) : B(str, ms_fixedRing) {}
193
195 PolynomialOverFixedRing(const byte *encodedPoly, unsigned int byteCount) : B(encodedPoly, byteCount) {}
196
198 explicit PolynomialOverFixedRing(const byte *BEREncodedPoly) : B(BEREncodedPoly) {}
199
202
204 PolynomialOverFixedRing(RandomNumberGenerator &rng, const RandomizationParameter &parameter) : B(rng, parameter, ms_fixedRing) {}
205
206 static const ThisType &Zero();
207 static const ThisType &One();
209
211
212
213 int Degree() const {return B::Degree(ms_fixedRing);}
215 unsigned int CoefficientCount() const {return B::CoefficientCount(ms_fixedRing);}
217 CoefficientType GetCoefficient(unsigned int i) const {return B::GetCoefficient(i, ms_fixedRing);}
219 CoefficientType operator[](unsigned int i) const {return B::GetCoefficient(i, ms_fixedRing);}
221
223
224
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;}
236
238 ThisType& operator<<=(unsigned int n) {ShiftLeft(n, ms_fixedRing); return *this;}
240 ThisType& operator>>=(unsigned int n) {ShiftRight(n, ms_fixedRing); return *this;}
241
243 void SetCoefficient(unsigned int i, const CoefficientType &value) {B::SetCoefficient(i, value, ms_fixedRing);}
244
246 void Randomize(RandomNumberGenerator &rng, const RandomizationParameter &parameter) {B::Randomize(rng, parameter, ms_fixedRing);}
247
249 void Negate() {B::Negate(ms_fixedRing);}
250
251 void swap(ThisType &t) {B::swap(t);}
253
255
256
257 bool operator!() const {return CoefficientCount()==0;}
259 ThisType operator+() const {return *this;}
261 ThisType operator-() const {return ThisType(Inverse(ms_fixedRing));}
263
265
266
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);}
271
273
274
275 ThisType MultiplicativeInverse() const {return ThisType(B::MultiplicativeInverse(ms_fixedRing));}
277 bool IsUnit() const {return B::IsUnit(ms_fixedRing);}
278
280 ThisType Doubled() const {return ThisType(B::Doubled(ms_fixedRing));}
282 ThisType Squared() const {return ThisType(B::Squared(ms_fixedRing));}
283
284 CoefficientType EvaluateAt(const CoefficientType &x) const {return B::EvaluateAt(x, ms_fixedRing);}
285
287 static void Divide(ThisType &r, ThisType &q, const ThisType &a, const ThisType &d)
288 {B::Divide(r, q, a, d, ms_fixedRing);}
290
292
293
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);}
300
301private:
302 struct NewOnePolynomial
303 {
304 ThisType * operator()() const
305 {
306 return new ThisType(ms_fixedRing.MultiplicativeIdentity());
307 }
308 };
309
310 static const Ring ms_fixedRing;
311};
312
314template <class T> class RingOfPolynomialsOver : public AbstractEuclideanDomain<PolynomialOver<T> >
315{
316public:
317 typedef T CoefficientRing;
319 typedef typename Element::CoefficientType CoefficientType;
321
322 RingOfPolynomialsOver(const CoefficientRing &ring) : m_ring(ring) {}
323
324 Element RandomElement(RandomNumberGenerator &rng, const RandomizationParameter &parameter)
325 {return Element(rng, parameter, m_ring);}
326
327 bool Equal(const Element &a, const Element &b) const
328 {return a.Equals(b, m_ring);}
329
330 const Element& Identity() const
331 {return this->result = m_ring.Identity();}
332
333 const Element& Add(const Element &a, const Element &b) const
334 {return this->result = a.Plus(b, m_ring);}
335
336 Element& Accumulate(Element &a, const Element &b) const
337 {a.Accumulate(b, m_ring); return a;}
338
339 const Element& Inverse(const Element &a) const
340 {return this->result = a.Inverse(m_ring);}
341
342 const Element& Subtract(const Element &a, const Element &b) const
343 {return this->result = a.Minus(b, m_ring);}
344
345 Element& Reduce(Element &a, const Element &b) const
346 {return a.Reduce(b, m_ring);}
347
348 const Element& Double(const Element &a) const
349 {return this->result = a.Doubled(m_ring);}
350
352 {return this->result = m_ring.MultiplicativeIdentity();}
353
354 const Element& Multiply(const Element &a, const Element &b) const
355 {return this->result = a.Times(b, m_ring);}
356
357 const Element& Square(const Element &a) const
358 {return this->result = a.Squared(m_ring);}
359
360 bool IsUnit(const Element &a) const
361 {return a.IsUnit(m_ring);}
362
363 const Element& MultiplicativeInverse(const Element &a) const
364 {return this->result = a.MultiplicativeInverse(m_ring);}
365
366 const Element& Divide(const Element &a, const Element &b) const
367 {return this->result = a.DividedBy(b, m_ring);}
368
369 const Element& Mod(const Element &a, const Element &b) const
370 {return this->result = a.Modulo(b, m_ring);}
371
372 void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const
373 {Element::Divide(r, q, a, d, m_ring);}
374
376 {
377 public:
378 InterpolationFailed() : Exception(OTHER_ERROR, "RingOfPolynomialsOver<T>: interpolation failed") {}
379 };
380
381 Element Interpolate(const CoefficientType x[], const CoefficientType y[], unsigned int n) const;
382
383 // a faster version of Interpolate(x, y, n).EvaluateAt(position)
384 CoefficientType InterpolateAt(const CoefficientType &position, const CoefficientType x[], const CoefficientType y[], unsigned int n) const;
385/*
386 void PrepareBulkInterpolation(CoefficientType *w, const CoefficientType x[], unsigned int n) const;
387 void PrepareBulkInterpolationAt(CoefficientType *v, const CoefficientType &position, const CoefficientType x[], const CoefficientType w[], unsigned int n) const;
388 CoefficientType BulkInterpolateAt(const CoefficientType y[], const CoefficientType v[], unsigned int n) const;
389*/
390protected:
391 void CalculateAlpha(std::vector<CoefficientType> &alpha, const CoefficientType x[], const CoefficientType y[], unsigned int n) const;
392
393 CoefficientRing m_ring;
394};
395
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);
402
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)
410 {return !(a==b);}
411
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();}
428
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));}
449
450NAMESPACE_END
451
452NAMESPACE_BEGIN(std)
453template<class T> inline void swap(CryptoPP::PolynomialOver<T> &a, CryptoPP::PolynomialOver<T> &b)
454{
455 a.swap(b);
456}
457template<class T, int i> inline void swap(CryptoPP::PolynomialOverFixedRing<T,i> &a, CryptoPP::PolynomialOverFixedRing<T,i> &b)
458{
459 a.swap(b);
460}
461NAMESPACE_END
462
463#endif
Classes for performing mathematics over different fields.
Abstract Euclidean domain.
Definition algebra.h:277
Interface for buffered transformations.
Definition cryptlib.h:1652
Base class for all exceptions thrown by the library.
Definition cryptlib.h:159
@ OTHER_ERROR
Some other error occurred not belonging to other categories.
Definition cryptlib.h:177
division by zero exception
Definition polynomi.h:28
specify the distribution for randomization functions
Definition polynomi.h:35
Polynomials over a fixed ring.
Definition polynomi.h:164
PolynomialOverFixedRing(const byte *encodedPoly, unsigned int byteCount)
convert from big-endian byte array
Definition polynomi.h:195
PolynomialOverFixedRing(RandomNumberGenerator &rng, const RandomizationParameter &parameter)
create a random PolynomialOverFixedRing
Definition polynomi.h:204
unsigned int CoefficientCount() const
degree + 1
Definition polynomi.h:215
CoefficientType operator[](unsigned int i) const
return coefficient for x^i
Definition polynomi.h:219
CoefficientType GetCoefficient(unsigned int i) const
return coefficient for x^i
Definition polynomi.h:217
void SetCoefficient(unsigned int i, const CoefficientType &value)
set the coefficient for x^i to value
Definition polynomi.h:243
PolynomialOverFixedRing(unsigned int count=0)
creates the zero polynomial
Definition polynomi.h:177
PolynomialOverFixedRing(BufferedTransformation &bt)
convert from BER encoded byte array stored in a BufferedTransformation object
Definition polynomi.h:201
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))
Definition polynomi.h:287
PolynomialOverFixedRing(const ThisType &t)
copy constructor
Definition polynomi.h:180
PolynomialOverFixedRing(const CoefficientType &element)
construct constant polynomial
Definition polynomi.h:185
int Degree() const
the zero polynomial will return a degree of -1
Definition polynomi.h:213
PolynomialOverFixedRing(const char *str)
convert from string
Definition polynomi.h:192
PolynomialOverFixedRing(const byte *BEREncodedPoly)
convert from Basic Encoding Rules encoded byte array
Definition polynomi.h:198
PolynomialOverFixedRing(Iterator first, Iterator last)
construct polynomial with specified coefficients, starting from coefficient of x^0
Definition polynomi.h:188
represents single-variable polynomials over arbitrary rings
Definition polynomi.h:22
PolynomialOver(RandomNumberGenerator &rng, const RandomizationParameter &parameter, const Ring &ring)
create a random PolynomialOver<T>
Definition polynomi.h:84
PolynomialOver(const PolynomialOver< Ring > &t)
copy constructor
Definition polynomi.h:60
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
Definition polynomi.h:91
CoefficientType GetCoefficient(unsigned int i, const Ring &ring) const
return coefficient for x^i
Definition polynomi.cpp:86
PolynomialOver(Iterator begin, Iterator end)
construct polynomial with specified coefficients, starting from coefficient of x^0
Definition polynomi.h:68
PolynomialOver()
creates the zero polynomial
Definition polynomi.h:53
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)
Definition polynomi.cpp:430
void SetCoefficient(unsigned int i, const CoefficientType &value, const Ring &ring)
set the coefficient for x^i to value
Definition polynomi.cpp:182
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
Definition polynomi.h:72
PolynomialOver(const CoefficientType &element)
construct constant polynomial
Definition polynomi.h:64
Interface for random number generators.
Definition cryptlib.h:1435
Ring of polynomials over another ring.
Definition polynomi.h:315
void DivisionAlgorithm(Element &r, Element &q, const Element &a, const Element &d) const
Performs the division algorithm on two elements in the ring.
Definition polynomi.h:372
const Element & MultiplicativeIdentity() const
Retrieves the multiplicative identity.
Definition polynomi.h:351
const Element & Identity() const
Provides the Identity element.
Definition polynomi.h:330
const Element & Mod(const Element &a, const Element &b) const
Performs a modular reduction in the ring.
Definition polynomi.h:369
Square block cipher.
Definition square.h:25
Abstract base classes that provide a uniform interface to this library.
Utility functions for the Crypto++ library.
Classes and functions for secure memory allocations.