# 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

```python
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
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://www.ctfrecipes.com/cryptography/symmetric-cryptography/aes/block-encryption-procedure/shift-row.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
