#include <polynomial.h>
Collaboration diagram for libecc::polynomial< m, k >:
Public Methods | |
polynomial (void) | |
polynomial (bitset_digit_t coefficients) | |
polynomial (polynomial const &p) | |
polynomial (bitset< m > const &coefficients) | |
polynomial (std::string const &coefficients) | |
polynomial (Operator::bitsetExpression< m, false, false, Operator::bitsetXOR > const &expression) | |
polynomial & | operator= (polynomial const &p) |
polynomial & | operator= (bitset< m > const &coefficients) |
polynomial & | operator= (Operator::bitsetExpression< m, false, false, Operator::bitsetXOR > const &expression) |
polynomial (polynomial const &b, polynomial const &c) | |
polynomial & | square (bitset_digit_t *tmpbuf) const |
bool | sqrt (void) |
polynomial & | operator+= (polynomial const &p) |
polynomial & | operator-= (polynomial const &p) |
polynomial & | operator *= (polynomial const &p) |
polynomial & | operator/= (polynomial const &p) |
bitset< m > const & | get_bitset (void) const |
bitset< m > & | get_bitset (void) |
Static Public Methods | |
polynomial const & | unity (void) |
Static Public Attributes | |
unsigned int const | square_digits |
Friends | |
Operator::bitsetExpression< m, false, false, Operator::bitsetXOR > | operator+ (polynomial const &p1, polynomial const &p2) |
Operator::bitsetExpression< m, false, false, Operator::bitsetXOR > | operator- (polynomial const &p1, polynomial const &p2) |
polynomial | operator * (polynomial const &p1, polynomial const &p2) |
polynomial | operator/ (polynomial const &p1, polynomial const &p2) |
bool | operator== (polynomial const &p1, polynomial const &p2) |
bool | operator!= (polynomial const &p1, polynomial const &p2) |
std::ostream & | operator<< (std::ostream &os, polynomial const &p) |
This class represents a polynomial with binairy coefficients (0 or 1) for a finite field with the fixed reduction polynomial:
.
The well known complex number
is defined by the equation
. Likewise, it is possible to define a
by means of a so called reduction polynomial:
. In order to be sure that all roots of this equation are of degree
, this polynomial must be irreducible. Irreducible means that it is not possible to write the polynomial as a product of two polynomials of lesser degree. For example, consider the equation
Then
isn't very special as it could still be an ordinary complex number:
which has, among others,
as roots.
Apart from the reduction polynomial, the field elements are further restricted to polynomials with binairy coefficients. This is reflected in the fact that
, in other words that
. As a result, all polynomials will be of a degree less than
and the total number of different polynomials will be finite.
[ Note: if the coefficients of a given polynomial F(t) are done modulo some integer p, then we say: the polynomial is defined over GF(p). GF stands for Galois Field and means that it is a finite field (of p elements thus). The characteristics of a finite field are completely determined by the number of its elements, hence that p is also called 'the characteristic' of the field. For any two field representations with the same characteristic, it is possible to map the elements of the two representations one on one upon eachother without loss of generality. The complex number i, as defined by
, is not defined over GF(2) of course. The solutions of t of
over GF(2) are +1, -1, i and -i. ]
Example
Let the reduction polynomial be of degree
.
Then the total number of field elements is
:
Polynomials of a higher degree do not exist because we can always write those as a polynomial of degree 3 or less. For example, let the (irreducible) reduction polynomial of degree 4 be
. Now suppose
would be a field element, then we can simply prove that this polynomial is equivalent with one of the sixteen listed above by using the reduction polynomial:
.
The polynomials can thus be represented by a bitset of
bits. Each bit in turn then represents a coefficient of the polynomial. This is very convenient for a computer.
Furthermore, because the algebra of the coefficients of the polynomials has to be done modulo two (as per our definition), addition (and substraction) of two polynomials is equivalent to the exclusive-or of those bitsets. For example,
, which is equivalent with 0110 xor 0101 = 0011.
Also multiplication can be implemented rather easily. Because
it is obvious that multiplication with a single power of
is just a left shift. We do have to take into account the reduction polynomial though - when the degree of the resulting polynomial becomes too large.
Multiplication of two arbitrary polynomials exists of a few shifts and exclusive-or operations, followed by reduction with the reduction polynomial if needed.
For example,
10011 (reduction polynomial)
1101
0111
1101 0111 * ------ 1101 11010 110100 + (addition is exclusive-or!) ------ 100011 10011 - (t * reduction_polynomial) ------ 101And thus
The reduction polynomial could be any irreducible polynomial, but by using a trinomial (a polynomial with only three terms), reduction can be implemented much faster. The power of the second term (
) should be chosen as small as possible because also that will speed up the process of reduction. Therefore, for a given
, one should use the smallest
for which
is irreducible.
The reason that the reduction polynomial has to be irreducible is to ensure that the elements of the field indeed form a Field. First of all, multiplication of field elements should be commutative, that is
for any
and
element of the field. Now suppose that we would use a reduction polynomial that could be written as
where the (irreducible) polynomials
and
are of respectively degrees
and
with
. Then
and
are elements of the "field" (their degree is less than
). Furthermore, there should exist a
such that
(
is the multiplicative inverse of
). Then note that
! If the reduction polynomial is not irreducible then we don't have a field thus!
Finding an irreducible trinomial
An irreducible trinomial is a polynomial of three terms,
which cannot be written as the product of two polynomials of lower degree. Finding an irreducible polynomial is based on the theorem that
is the product of all irreducible polynomials whose degree divides m.
When
is prime then all we need to do in order to find the smallest
is trying all values of
, starting at
, until we find that
, or in other words that
. If it equals
then the trinomial
is irreducible. [ When
is not prime then we also need to check that for each divisor
of
(
) that
].
More in general, let
be a polynomial element of the field, and let
be the smallest positive integer for which
, then
is called the order of
. All the polynomials
that can be formed by running
over the values
till
:
form a group of
polynomials that can be generated by
.
A polynomial of order
is called a generator as it generates all elements of the field except
. If
is generator of a given polynomial field over
, then the reduction polynomial
is called primitive, which is more restrictive than irreducible. The chance that an arbitrary irreducible polynomial of prime degree is also primitive is very likely (and for large m, even for non-primes), by far most irreducible trinomials of prime degree are also primitive.
This is a nice moment to note the equivalence with the Random Number Generator (see libecc::rng). What we have there is a shift register with feedback points using exclusive-or; this is exactly equivalent with multiplying with t and using a reduction polynomial! Consider a RNG with 3 bits and a feedback point to bit 0 and 1:
RNG state | Polynomial | Reduction of ![]() ![]() | |||
001 | ![]() | ![]() | ![]() | ![]() | |
010 | ![]() | ![]() | ![]() | ![]() | |
100 | ![]() | ![]() | ![]() | ![]() | |
011 | ![]() | ![]() | ![]() | ![]() | |
110 | ![]() | ![]() | ![]() | ![]() | |
111 | ![]() | ![]() | ![]() | ![]() | |
101 | ![]() | ![]() | ![]() | ![]() | |
001 | ![]() | ![]() | ![]() | ![]() |
The state of the random number generator can be seen as a polynomial over
with a reduction polynomial that is determined by the feedback points. Each step then corresponds with multiplying the polynomial with
. The period of the RNG will be equal to the order of
, having a maximum value of
when the reduction polynomial is primitive.
In order to check if a given irreducible polynomial is primitive, one needs to know the factorization of
. After all, even while
, then
is not necessarily the smallest positive integer
for which
. For example, when
and the order of
is
so that
, then also
. Therefore, one must make sure that every possible fraction of
is not the real order.
Using the factorization tables of the Cunningham Project, we have calculated a list of primitive trinomials. The program that was used for this can be found in testsuite/polynomial
.
Further reading
See also http://www.certicom.com/resources/ecc/math8.html and chapter 4.5 of The Handbook Of Applied Cryptography.
More information on primitive trinomials and Random Number Generators can be found on Richard Brent's page. There you can also find links to sites about factorization.
|
Construct an uninitialized field element. |
|
Construct a polynomial of a low degree. |
|
Copy constructor. |
|
Construct a polynomial with binairy coefficients given by the bitset coefficients. |
|
Construct a polynomial with binairy coefficients given by the hexadecimal value in the string coefficients. |
|
Construct a polynomial that is the sum of two other polynomials. This constructor is for implicit conversions like in
polynomial<m, k> p1("1"), p2("2"), p3("3"); p1 = p2 * (p3 + p1);
Here this constructor is used to implicitely convert |
|
Construct a polynomial |
|
Return the underlaying bitset representing the coefficients. |
|
Return the underlaying bitset representing the coefficients. |
|
Multiply this polynomial with polynomial p. |
|
Add polynomial p to this polynomial. |
|
Subtract polynomial p from this polynomial. Has the same effect as adding it. |
|
Devide this polynomial by polynomial p. |
|
Add two polynomials and assign the result to this polynomial. This operator is used when doing |
|
Assignment operator, set the coefficients of this polynomial equal to coefficients. |
|
Assignment operator, set this polynomial equal to p. |
|
Calculate the square root of this polynomial. |
|
Calculate the square of this polynomial. The result is written into tmpbuf which is returned, casted to a polynomial. Usage:
libecc:polynomial<m, k> p1(init);
libecc::bitset_digit_t p2buf[libecc:polynomial<m, k>::square_digits];
libecc:polynomial<m, k>& p2 = p1.square(p2buf); // p2 becomes the square of p1.
|
|
Returns the multiplicative unity for the field (1). |
|
Multiply polynomial p1 with polynomial p2. |
|
Compare two polynomials. Returns |
|
Prepare the addition of two polynomials. Returns a dummy object with two pointers to the polynomials to be added. The addition does not take place until the final destination polynomial is known as well. |
|
Prepare the subtraction of two polynomials. Returns a dummy object with two pointers to the polynomials to be subtracted. The subtraction does not take place until the final destination polynomial is known as well. |
|
Devide polynomial p1 by polynomial p2. |
|
Write the binary coefficients of the polynomial p to os.
For example, an output string |
|
Compare two polynomials. Returns |
|
Number of digits needed for temporary buffer used to calculate a square. |