Each byte in AES is viewed as an element of a binary finite field of 256 elements, where it can always be represented as a polynomial of degree at most 7 with coefficients in. The construction of the finite field is made as the quotient ring, whereis an irreducible polynomial of degree 8 inso the ring becomes a field.
In AES, the choice foris
We can check with SageMath that it is irreducible:
F2 = GF(2)K.<x> = F2f = x^8 + x^4 + x^3 + x + 1f.is_irreducible()# True
A byte is composed of 8 bits and is matched to a polynomial as
For instance, take the byte
3a whose binary decomposition is and becomes the polynomial
Polynomials of degree 8 or more can always be reduced, using the fact that in the finite field, we have , so we have the relation
Why not ? In fact, that's also true, but the coefficient are in so the additive inverse of is itself.
In SageMath, this reduction can be produced in one of the following methods.
Method 1: Remainder of an Euclidean division by
(x^8 + x^6 + x^4 + 1) % f# x^6 + x^3 + x
Method 2: Image in the quotient ring
R = K.quotient(f)R(x^8 + x^6 + x^4 + 1)# xbar^6 + xbar^3 + xbar
Method 3: Using the Finite Field class of SageMath directly.
F.<x> = GF(2^8, modulus=x^8 + x^4 + x^3 + x + 1)x^8 + x^6 + x^4 + 1# x^6 + x^3 + x
On this page we use this last method. Also, this helper converts an element of the finite field to the hexadecimal representation of a byte, and could be useful in the examples:
def F_to_hex(a):return ZZ(a.integer_representation()).hex()b = x^4 + x^3 + x + 1F_to_hex(b)# '1b'
The addition of two polynomials is done by adding the coefficients corresponding of each monomial:
And as the addition of the coefficients is in, it corresponds to the bitwise
xor operation on the byte.
Multiplication of two polynomials is more complex (one example would be the Karatsuba algorithm, more efficient than the naive algorithm). For an implementation of AES, it is possible to only use the multiplication by , whose byte representation is
Letan element and we consider the multiplication by:
All coefficients are shifted to a monomial one degree higher. Then, there are two cases:
Ifis, then we have a polynomial of degree at most 7 and we are done;
Ifis, we can replacebyduring the reduction phase:
This can be used to implement a very efficient multiplication bywith the byte representation:
A bitwise shiftleft operation:
(b << 1) & 0xff;
Followed by a conditional addition with
1b if the top bit of is .
Here an example in SageMath (we use the finite field construction of method 3):
b = x^7 + x^5 + x^4 + x^2 + 1F_to_hex(b)# 'b5'(2*0xb5 & 0xff) ^^ 0x1b).hex() == F_to_hex(x*b) # the xor in Sage is "^^"# True