# AES mix column stage

In a PDF related to AES algorithm for mix column stage is written:

The multiplication mentioned above is performed over a Galois Field. The mathematics behind this is beyond the scope of this paper. This section will instead concentrate on the implementation of the multiplication which can be done quite easily with the use of the following two tables in (HEX).

Is there a formula (suitable for programming) to do this multiplication?

What they are trying to say that an effective way to multiply $a$ and $b$ in $GF(2^8)$ is to first check if either $a$ or $b$ are zero (if either are zero, the result of the multiplication is zero), and if neither are zero, then:

$$a \otimes b = E( L(a) + L(b) \bmod 255 )$$

where $E$ and $L$ are lookups in the ETable, LTable given, and the addition is over the integers (and not addition in the $GF(2^8)$ field).

There are more mathematically elegant ways of expressing it; however this is just about the most efficient way to implement it on a standard CPU.

However, this has doesn't have that much to do with AES; yes, AES does computation within $GF(2^8)$, however it never requires us to multiply two variables.

What the MixCollumn step asks for is multiplication by the fixed values 1, 2 and 3.

Now, computing $1 \otimes x$ is easy; $1 \otimes x = x$

Computing $2 \otimes x$ is only slightly more involved; because AES uses a polynomial representation of $GF(2^8)$, this can be simplified by shifting left by one, and then xor'ing the fixed value $0x1b$ if the original msbit was one; in a C-type notation, $2 \otimes x$ would be the value:

((x<<1) & 0xff) ^ (x & 0x80 ? 0x1b : 0x00)


Once we can compute $2 \otimes x$, computing $3 \otimes x$ is easy; $3 \otimes x = (2 \otimes x) \oplus (1 \otimes x) = (2 \otimes x) \oplus x$

• Code Golf style, the C formula for $2\otimes x$ can be shortened to x<<1^283&-(x>>7); adding decoration, that is (x<<1) ^ (0x11b & (0x200-(x>>7))). It has the real benefit of being constant-time, when the ? operator is often not – fgrieu Nov 5 '14 at 8:51