Shift Row

The "Shift Rows" operates on the rows of the 4x4 state matrix.

It shifts each row cyclically to the left by a certain number of bytes, with the shift amount depending on the row number.

input :
+-------------+
| 00 01 02 03 |
| 10 11 12 13 |
| 20 21 22 23 |
| 30 31 32 33 |
+-------------+

output : 
+-------------+
| 00 01 02 03 | --> Row 0; no shift
| 11 12 13 10 | --> Row 1; Shift 1 to the left
| 22 23 20 21 | --> Row 2; Shift 2 to the left
| 33 30 31 32 | --> Row 3; Shift 3 to the left
+-------------+

This permutation operation provides diffusion and confusion in the state matrix, which enhances the security of the AES algorithm.

Python implementation

def shift_rows(s):
    s[0][1], s[1][1], s[2][1], s[3][1] = s[1][1], s[2][1], s[3][1], s[0][1]
    s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
    s[0][3], s[1][3], s[2][3], s[3][3] = s[3][3], s[0][3], s[1][3], s[2][3]
    return s


def inv_shift_rows(s):
    s[0][1], s[1][1], s[2][1], s[3][1] = s[3][1], s[0][1], s[1][1], s[2][1]
    s[0][2], s[1][2], s[2][2], s[3][2] = s[2][2], s[3][2], s[0][2], s[1][2]
    s[0][3], s[1][3], s[2][3], s[3][3] = s[1][3], s[2][3], s[3][3], s[0][3]
    return s

Last updated