Skip to content

Commit eae7939

Browse files
author
=
committed
style: standardize docstrings to match aes_128_encrypt_block format
1 parent b52fe06 commit eae7939

1 file changed

Lines changed: 57 additions & 24 deletions

File tree

ciphers/aes_128.py

Lines changed: 57 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,16 @@ def sub_bytes(state: list[int]) -> None:
278278
"""
279279
Applies the AES S-Box substitution to each byte in the state.
280280
281-
>>> state = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
282-
>>> sub_bytes(state)
283-
>>> state[:4]
284-
[99, 124, 119, 123]
281+
Args:
282+
state: A list of 16 integers representing the state matrix.
283+
284+
Examples:
285+
>>> state = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
286+
>>> sub_bytes(state)
287+
>>> state[:4]
288+
[99, 124, 119, 123]
285289
"""
290+
286291
for i in range(16):
287292
state[i] = S_BOX[state[i]]
288293

@@ -291,10 +296,14 @@ def shift_rows(state: list[int]) -> None:
291296
"""
292297
Shifts the rows of the 4x4 state matrix.
293298
294-
>>> state = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
295-
>>> shift_rows(state)
296-
>>> state
297-
[0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]
299+
Args:
300+
state: A list of 16 integers representing the state matrix.
301+
302+
Examples:
303+
>>> state = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]
304+
>>> shift_rows(state)
305+
>>> state
306+
[0, 5, 10, 15, 4, 9, 14, 3, 8, 13, 2, 7, 12, 1, 6, 11]
298307
"""
299308
# Row 0: unchanged
300309
# Row 1: shifted left by 1
@@ -309,10 +318,18 @@ def galois_multiply(multiplicand: int, multiplier: int) -> int:
309318
"""
310319
Multiplies two numbers in the GF(2^8) Galois field.
311320
312-
>>> galois_multiply(2, 3)
313-
6
314-
>>> galois_multiply(87, 19)
315-
254
321+
Args:
322+
multiplicand: The first integer to multiply.
323+
multiplier: The second integer to multiply.
324+
325+
Returns:
326+
The product of the two numbers in the Galois field.
327+
328+
Examples:
329+
>>> galois_multiply(2, 3)
330+
6
331+
>>> galois_multiply(87, 19)
332+
254
316333
"""
317334
p = 0
318335
for _ in range(8):
@@ -330,10 +347,14 @@ def mix_columns(state: list[int]) -> None:
330347
"""
331348
Mixes the columns of the state matrix to provide diffusion.
332349
333-
>>> state = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
334-
>>> mix_columns(state)
335-
>>> state
336-
[2, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
350+
Args:
351+
state: A list of 16 integers representing the state matrix.
352+
353+
Examples:
354+
>>> state = [1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
355+
>>> mix_columns(state)
356+
>>> state
357+
[2, 1, 1, 3, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
337358
"""
338359
for i in range(4):
339360
col = state[i * 4 : (i + 1) * 4]
@@ -355,11 +376,16 @@ def add_round_key(state: list[int], round_key: list[int]) -> None:
355376
"""
356377
XORs the state matrix with the current round key.
357378
358-
>>> state = [0] * 16
359-
>>> round_key = [1] * 16
360-
>>> add_round_key(state, round_key)
361-
>>> state
362-
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
379+
Args:
380+
state: A list of 16 integers representing the state matrix.
381+
round_key: A list of 16 integers representing the round key.
382+
383+
Examples:
384+
>>> state = [0] * 16
385+
>>> round_key = [1] * 16
386+
>>> add_round_key(state, round_key)
387+
>>> state
388+
[1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]
363389
"""
364390
for i in range(16):
365391
state[i] ^= round_key[i]
@@ -369,9 +395,16 @@ def key_expansion(key: bytes) -> list[int]:
369395
"""
370396
Expands a 16-byte key into 11 round keys (176 bytes total).
371397
372-
>>> key = bytes([0] * 16)
373-
>>> len(key_expansion(key))
374-
176
398+
Args:
399+
key: Exactly 16 bytes of encryption key.
400+
401+
Returns:
402+
A list of 176 integers representing the expanded key schedule.
403+
404+
Examples:
405+
>>> key = bytes([0] * 16)
406+
>>> len(key_expansion(key))
407+
176
375408
"""
376409
key_schedule = list(key)
377410
for i in range(4, 44):

0 commit comments

Comments
 (0)