# Reed-Solomon codes. Part 2 - Arithmetic of Galois Fields

Hello, friends! Last time we started talking about how Reed-Solomon codes help provide the necessary level of data storage reliability. Today we dwell a little more on the arithmetic of the Galois fields, which is used in the calculations. First, a brief excursion into the last part

. We found out - in order to be able to recover lost data, you must first generate “redundant data” in a certain way. Moreover, from a mathematical point of view, this task (coding) is reduced to constructing some generating matrix and performing the operation of multiplying this matrix by a vector of source data. The recovery (decoding) procedure, in turn, consists in reversing the generating matrix and multiplying it by the vector of the saved data. Schematically, this can be represented as follows.

Encoding

Procedure: Decoding Procedure:

## Limitations of Arithmetic of Rational Numbers

We have already noted some difficulties that have to be encountered when implementing encoding / decoding procedures on specific hardware platforms. So, for example, numbers in computer memory are usually represented by a fixed number of bytes. As a result, when multiplying the elements of the generating matrix, one can obtain overflow of bits. Or, when the generating matrix is ​​inverted as its elements, rational numbers can arise that are problematic to store with arbitrary precision. Therefore, today we will talk about how to implement these procedures, avoiding the above problems. The Galois field theory, which we already talked a bit about in a previous article, will help us in this.

I’ll make a few comments right away. Firstly, the article is primarily intended for an engineering audience, so I tried to avoid rigorous mathematical definitions. For those who want a more formal introduction to this area, it makes sense to immediately turn to the relevant literature. For my part, I can recommend Ernest Borisovich Vinberg's book, A Course in Algebra. Secondly, directly for the implementation of redundant coding algorithms, only a basic understanding of the theory of finite fields is enough. You can simply assume that some consistent tables of multiplication, division, addition, subtraction, and all calculations are performed using these tables. This is a hint for those who do not really want to dive deep into this section of algebra.

## Fields and arithmetic operations in finite sets

Before proceeding directly to the discussion of the Galois fields, let us repeat once again what we want to achieve. We want to be able to multiply matrices by vectors, invert matrices. And most importantly, we want to deal only with integers and not worry about overflow when performing operations of multiplication and addition.

Also, to avoid confusion, it makes sense to immediately deal with terminology. First of all, what is a field in terms of general algebra? As Wikipedia tells us, “a field is a set for whose elements the operations of addition, subtraction, multiplication and division (except division by zero) are defined, and the properties of these operations are close to the properties of ordinary numerical operations”. What is the Galois field? Galois Field Is an arbitrary field consisting of a finite number of elements. , - the standard designation of Galois fields, where the number of field elements is indicated in brackets.

In modern computers, a fixed number of bits is usually used to store numbers. It’s easy to calculate that everything exists different n-bit numbers, from 0 to . In other words, we have some finite set of numbers. On this set we will now try in a consistent way to determine the operations of multiplication, division, addition and subtraction. Moreover, so that the result of any operation is alsobit number! In this case, it is said that the set is closed with respect to the introduced operations .

The subtraction operation in systems of finite sets is usually introduced through the concept of zero and opposite elements. Zero element Is an element for which equality is true: where Is an arbitrary element of the set. Elementis the opposite forif equality is true . Usually the opposite element is denoted as. If we want from subtract we find the opposite element  and stack it with . Accordingly, in order to be able to subtract arbitrary elements of a finite set, each element of this set must have the opposite.

The situation is similar with the division operation. The concept of unit and inverse elements is introduced. Single element Is an element for which equality is true where Is an arbitrary element of the set. Element (denoted by ) is called the inverse of , if . As with subtraction, if we want divide by nonzero we must find the inverse element  and multiply it by . In order to be able to divide arbitrary elements, each element of the set (except for the zero) must have the opposite.

## Construction of Galois fields GF (p)

How can we determine the indicated arithmetic operations on a finite set of numbers, so that the set is closed relative to them? In other words, we want to turn an arbitrary finite set of elements into a Galois field. The first thing that comes to mind is to use modular arithmetic. Then if our set contains elements, then the result of the product of numbers  will be the number . The sum of numbers is defined as.

As an exercise, let's compile addition and multiplication tables for a set consisting of elements .

The lower two tables show opposite and opposite values ​​for each element of our set. Armed with these tables, we can perform all the arithmetic operations we need. Hurrah!

## Building Galois Fields 

We seem to be on the right track to success. The only thing that may not suit us yet is the size of our field - 5. It is clear that to simplify the software implementation, it makes sense to work with sets containingnumbers. Then we can operate with nibbles, bytes, words, etc.

At first glance it seems to me that the difference is 5 or 256 elements in the set, we take the result of the operation of multiplication or addition by the corresponding module and we are done. Let's try again to make a multiplication table for a set, but this time containing element - . Here's what happened:

If you look closely at the resulting table, you can find one serious drawback in it. Pay attention to the line for element 2. There is no single element in this line! This means that we will not be able to divide the other elements by 2, since the opposite does not exist for it! This means that it is necessary to construct addition and multiplication tables in some other way. Modular arithmetic, unfortunately, no longer works.

So, we found out that if the set containsobject, then modular arithmetic does not allow you to specify consistent multiplication operations. In fact, similar problems will arise for any set, the number of elementswhich is not a prime number. But what else can you think of?

## Addition to GF (p ^ n)

Let us assume for simplicity that the set on which we want to define arithmetic operations contains elements. These are, in fact, numbers. Any number from this set can be represented as a decomposition:



In essence, this is the usual binary representation of a number. Thus, we can also consider any number from our set as a vector of lengthwhose elements are zeros and ones:



We introduce the operation of adding any two numbers by adding the corresponding binary decomposition vectors. Moreover, the addition of the components of the vectors will be carried out modulo 2 (in the general case, if the number of elementsmodulo ) Thus, the resulting vector will also be a binary representation of a certain number and, as a consequence, will belong to our set (the set will be closed with respect to the addition operation).



It is easy to notice that the addition operation we introduced is equivalent to the XOR bitwise operation. Which is very good, since modern processors can perform this operation extremely quickly. But that is not all. It's obvious that for an arbitrary number . This means that the opposite element to is himself . So in the field addition is the same as subtraction!

## Multiplication in GF (p ^ n)

It remains to introduce the multiplication operation on our set. This is done as follows. Let's imagine for a while that any number from our set is a polynomial of the following form:



Here the coefficients of the polynomial are taken from the binary expansion of the number . With this approach, for example, the number will be mapped to a polynomial .

The operation of multiplication of two numbers we can determine through the multiplication of polynomials corresponding to given numbers:



Moreover, when multiplying, all operations with coefficients of polynomials are performed modulo 2.

But now, obviously, another problem may arise. When multiplying two polynomials of degree, we can get a polynomial of a higher degree, which will not correspond to any of the numbers of our set. This is solved as follows. An irreducible polynomial of degree is chosen. Roughly speaking, this is such a polynomial that cannot be represented as a product of other polynomials, more in the article . After that, using the algorithm for dividing polynomials by a column , which many can still remember from the school curriculum, we divide the result of the product into an irreducible polynomial. I remind you once again that when dividing by a column, operations with coefficients of polynomials are also performed modulo 2. As a result, we obtain a polynomial of degree no higher than, which we take as the result of the product of two numbers. Here is an example of doing the multiplication of two numbers over a field using irreducible polynomial . This polynomial is used in the AES / Rijndael encryption algorithm.

We examined the field. Custom fields are constructed in a similar way, only instead of the binary representation of the number is used -one and all calculations are carried out modulo .

## Isomorphism of fields and Galois fields of arbitrary size

But what happens if we choose another irreducible polynomial? You can even pose a more general question. Suppose I don’t like all these polynomials and column divisions at all, so I’d better come up with my consistent multiplication table using some other principle. Will this be something fundamentally new? The answer is no. All these obtained fields will be isomorphic .

Translating from the "mathematical" language, isomorphism means that the differences are only in the designation of field elements and one can be reduced to another by choosing the appropriate mapping rule.

The finite sets of elements that we have examined so far have been of two types. The first type of sets had a number of elements equal to some prime number. On such sets, we were able to set multiplication and addition tables using only modular arithmetic. The second type includes sets having,  - simple number of items. On such sets it is possible to specify multiplication / addition using a scheme with polynomials. Is it possible to set multiplication / addition in a similar way for sets of arbitrary size, say containing 6 elements? The answer is no, it is impossible.

As a matter of fact, finite fields are called Galois fields, because he discovered their following important properties:

1. The number of elements of a finite field always has the form where  prime and  any natural.
2. All size fields  are isomorphic.

## Conclusion

The second article of the cycle is coming to an end. The first two articles presented brief theoretical information related to Reed-Solomon codes and Galois fields. In the next part I plan to dwell in more detail on the features of the software implementation of the above algorithms. Thanks.